Author: markt
Date: Sat Feb 28 19:33:08 2015
New Revision: 1662990

URL: http://svn.apache.org/r1662990
Log:
Provide a common method for making optional use of features available in JRE 
versions later than the minimum on which Tomcat is required to run.

Added:
    tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/
    tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/Jre7Compat.java   
(with props)
    tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/Jre8Compat.java   
(with props)
    tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/JreCompat.java   
(with props)
    
tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/LocalStrings.properties 
  (with props)

Added: tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/Jre7Compat.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/Jre7Compat.java?rev=1662990&view=auto
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/Jre7Compat.java 
(added)
+++ tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/Jre7Compat.java Sat 
Feb 28 19:33:08 2015
@@ -0,0 +1,58 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.tomcat.util.compat;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Locale;
+
+class Jre7Compat extends JreCompat {
+
+    private static final Method forLanguageTagMethod;
+
+
+    static {
+        Method m = null;
+        try {
+            m = Locale.class.getMethod("forLanguageTag", String.class);
+        } catch (SecurityException e) {
+            // Should never happen
+        } catch (NoSuchMethodException e) {
+            // Expected on Java < 7
+        }
+        forLanguageTagMethod = m;
+    }
+
+
+    static boolean isSupported() {
+        return forLanguageTagMethod != null;
+    }
+
+
+    @Override
+    public Locale forLanguageTag(String languageTag) {
+        try {
+            return (Locale) forLanguageTagMethod.invoke(null, languageTag);
+        } catch (IllegalArgumentException e) {
+            return null;
+        } catch (IllegalAccessException e) {
+            return null;
+        } catch (InvocationTargetException e) {
+            return null;
+        }
+    }
+}

Propchange: 
tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/Jre7Compat.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/Jre8Compat.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/Jre8Compat.java?rev=1662990&view=auto
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/Jre8Compat.java 
(added)
+++ tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/Jre8Compat.java Sat 
Feb 28 19:33:08 2015
@@ -0,0 +1,89 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.tomcat.util.compat;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import javax.net.ssl.SSLEngine;
+import javax.net.ssl.SSLParameters;
+import javax.net.ssl.SSLServerSocket;
+
+class Jre8Compat extends Jre7Compat {
+
+    private static final Method getSSLParametersMethod;
+    private static final Method setUseCipherSuitesOrderMethod;
+
+
+    static {
+        Method m1 = null;
+        Method m2 = null;
+        try {
+            // Get this class first since it is Java 8+ only
+            Class<?> c2 = Class.forName("javax.net.ssl.SSLParameters");
+            m1 = SSLServerSocket.class.getMethod("getSSLParameters");
+            m2 = c2.getMethod("setUseCipherSuitesOrder", boolean.class);
+        } catch (SecurityException e) {
+            // Should never happen
+        } catch (NoSuchMethodException e) {
+            // Expected on Java < 8
+        } catch (ClassNotFoundException e) {
+            // Expected on Java < 7
+        }
+        getSSLParametersMethod = m1;
+        setUseCipherSuitesOrderMethod = m2;
+    }
+
+
+    static boolean isSupported() {
+        return setUseCipherSuitesOrderMethod != null;
+    }
+
+
+    @Override
+    public void setUseServerCipherSuitesOrder(SSLServerSocket socket,
+            boolean useCipherSuitesOrder) {
+        try {
+            Object sslParameters = getSSLParametersMethod.invoke(socket);
+            setUseCipherSuitesOrderMethod.invoke(
+                    sslParameters, Boolean.valueOf(useCipherSuitesOrder));
+            return;
+        } catch (IllegalArgumentException e) {
+            throw new UnsupportedOperationException(e);
+        } catch (IllegalAccessException e) {
+            throw new UnsupportedOperationException(e);
+        } catch (InvocationTargetException e) {
+            throw new UnsupportedOperationException(e);
+        }
+    }
+
+
+    @Override
+    public void setUseServerCipherSuitesOrder(SSLEngine engine,
+            boolean useCipherSuitesOrder) {
+        SSLParameters sslParameters = engine.getSSLParameters();
+        try {
+            setUseCipherSuitesOrderMethod.invoke(sslParameters, 
Boolean.valueOf(useCipherSuitesOrder));
+        } catch (IllegalArgumentException e) {
+            throw new UnsupportedOperationException(e);
+        } catch (IllegalAccessException e) {
+            throw new UnsupportedOperationException(e);
+        } catch (InvocationTargetException e) {
+            throw new UnsupportedOperationException(e);
+        }
+    }
+}

Propchange: 
tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/Jre8Compat.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/JreCompat.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/JreCompat.java?rev=1662990&view=auto
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/JreCompat.java 
(added)
+++ tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/JreCompat.java Sat 
Feb 28 19:33:08 2015
@@ -0,0 +1,124 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.tomcat.util.compat;
+
+import java.util.Locale;
+
+import javax.net.ssl.SSLEngine;
+import javax.net.ssl.SSLServerSocket;
+
+import org.apache.tomcat.util.res.StringManager;
+
+/**
+ * This is the base implementation class for JRE compatibility and provides an
+ * implementation based on Java 6. Sub-classes may extend this class and 
provide
+ * alternative implementations for later JRE versions
+ */
+public class JreCompat {
+
+    private static final JreCompat instance;
+    private static StringManager sm =
+            StringManager.getManager(JreCompat.class.getPackage().getName());
+    private static final boolean jre8Available;
+    
+    
+    static {
+        // This is Tomcat 7 with a minimum Java version of Java 6. The latest
+        // Java version the optional features require is Java 8.
+        // Look for the highest supported JVM first
+        if (Jre8Compat.isSupported()) {
+            instance = new Jre8Compat();
+            jre8Available = true;
+        } else if (Jre7Compat.isSupported()) {
+            instance = new Jre7Compat();
+            jre8Available = false;
+        } else {
+            instance = new JreCompat();
+            jre8Available = false;
+        }
+    }
+    
+    
+    public static JreCompat getInstance() {
+        return instance;
+    }
+    
+    
+    // Java 7 methods
+    
+    public Locale forLanguageTag(String languageTag) {
+        // Extract the language and country for this entry
+        String language = null;
+        String country = null;
+        String variant = null;
+        int dash = languageTag.indexOf('-');
+        if (dash < 0) {
+            language = languageTag;
+            country = "";
+            variant = "";
+        } else {
+            language = languageTag.substring(0, dash);
+            country = languageTag.substring(dash + 1);
+            int vDash = country.indexOf('-');
+            if (vDash > 0) {
+                String cTemp = country.substring(0, vDash);
+                variant = country.substring(vDash + 1);
+                country = cTemp;
+            } else {
+                variant = "";
+            }
+        }
+        if (!isAlpha(language) || !isAlpha(country) || !isAlpha(variant)) {
+            return null;
+        }
+
+        return new Locale(language, country, variant);
+    }
+    
+    
+    private static final boolean isAlpha(String value) {
+        for (int i = 0; i < value.length(); i++) {
+            char c = value.charAt(i);
+            if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) {
+                return false;
+            }
+        }
+        return true;
+    }
+   
+    
+    // Java 8 methods
+    
+    public static boolean isJre8Available() {
+        return jre8Available;
+    }
+    
+    
+    @SuppressWarnings("unused")
+    public void setUseServerCipherSuitesOrder(SSLServerSocket socket,
+            boolean useCipherSuitesOrder) {
+        throw new 
UnsupportedOperationException(sm.getString("jreCompat.noServerCipherSuiteOrder"));
+    }
+    
+    
+    @SuppressWarnings("unused")
+    public void setUseServerCipherSuitesOrder(SSLEngine engine,
+            boolean useCipherSuitesOrder) {
+        throw new 
UnsupportedOperationException(sm.getString("jreCompat.noServerCipherSuiteOrder"));
+    }
+
+}

Propchange: 
tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/JreCompat.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/LocalStrings.properties
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/LocalStrings.properties?rev=1662990&view=auto
==============================================================================
--- 
tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/LocalStrings.properties 
(added)
+++ 
tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/LocalStrings.properties 
Sat Feb 28 19:33:08 2015
@@ -0,0 +1,16 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+jreCompat.noServerCipherSuiteOrder=Java Runtime does not support 
"useServerCipherSuitesOrder". You must use Java 8 or later to use this feature.
\ No newline at end of file

Propchange: 
tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/compat/LocalStrings.properties
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to