Author: markt
Date: Wed May  9 15:46:02 2018
New Revision: 1831262

URL: http://svn.apache.org/viewvc?rev=1831262&view=rev
Log:
Towards the fix for BZ 50019
Add the remaining plumbing necessary to support lookup-name.
Based on a patch by Gurkan Erdogdu

Added:
    tomcat/trunk/java/org/apache/naming/LookupRef.java   (with props)
    tomcat/trunk/java/org/apache/naming/factory/LookupFactory.java   (with 
props)
Modified:
    tomcat/trunk/java/org/apache/naming/factory/Constants.java
    tomcat/trunk/java/org/apache/naming/factory/LocalStrings.properties

Added: tomcat/trunk/java/org/apache/naming/LookupRef.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/naming/LookupRef.java?rev=1831262&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/naming/LookupRef.java (added)
+++ tomcat/trunk/java/org/apache/naming/LookupRef.java Wed May  9 15:46:02 2018
@@ -0,0 +1,53 @@
+/*
+ * 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.naming;
+
+import javax.naming.RefAddr;
+import javax.naming.StringRefAddr;
+
+/**
+ * Represents a reference to lookup.
+ */
+public class LookupRef extends AbstractRef {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * JNDI name for the lookup
+     */
+    public static final String LOOKUP_NAME = "lookup-name";
+
+
+    public LookupRef(String resourceType, String lookupName) {
+        this(resourceType, null, null, lookupName);
+    }
+
+
+    public LookupRef(String resourceType, String factory, String 
factoryLocation, String lookupName) {
+        super(resourceType, factory, factoryLocation);
+        if (lookupName != null && !lookupName.equals("")) {
+            RefAddr ref = new StringRefAddr(LOOKUP_NAME, lookupName);
+            add(ref);
+        }
+    }
+
+
+    @Override
+    protected String getDefaultFactoryClassName() {
+        return org.apache.naming.factory.Constants.DEFAULT_LOOKUP_JNDI_FACTORY;
+    }
+}

Propchange: tomcat/trunk/java/org/apache/naming/LookupRef.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: tomcat/trunk/java/org/apache/naming/factory/Constants.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/naming/factory/Constants.java?rev=1831262&r1=1831261&r2=1831262&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/naming/factory/Constants.java (original)
+++ tomcat/trunk/java/org/apache/naming/factory/Constants.java Wed May  9 
15:46:02 2018
@@ -42,6 +42,8 @@ public final class Constants {
 
     public static final String OPENEJB_EJB_FACTORY = Package + 
".OpenEjbFactory";
 
+    public static final String DEFAULT_LOOKUP_JNDI_FACTORY = Package + 
".LookupFactory";
+
     public static final String FACTORY = "factory";
 
 }

Modified: tomcat/trunk/java/org/apache/naming/factory/LocalStrings.properties
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/naming/factory/LocalStrings.properties?rev=1831262&r1=1831261&r2=1831262&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/naming/factory/LocalStrings.properties 
(original)
+++ tomcat/trunk/java/org/apache/naming/factory/LocalStrings.properties Wed May 
 9 15:46:02 2018
@@ -13,6 +13,11 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+lookupFactory.circularReference=Found a circular reference involving [{0}]
+lookupFactory.createFailed=Could not create instance of JNDI lookup factory 
class
+lookupFactory.loadFailed=Could not load JNDI lookup factory class
+lookupFactory.typeMismatch=The JNDI reference [{0}] was expected to be of type 
[{1}] but the lookup [{2}] return an object of type [{3}]
+
 resourceLinkFactory.nullType=The local resource link [{0}] that refers to 
global resource [{1}] does not specify the required attribute type
 resourceLinkFactory.unknownType=The local resource link [{0}] that refers to 
global resource [{1}] specified the unknown type [{2}]
 resourceLinkFactory.wrongType=The local resource link [{0}] that refers to 
global resource [{1}] was expected to return an instance of [{2}] but returned 
an instance of [{3}]
\ No newline at end of file

Added: tomcat/trunk/java/org/apache/naming/factory/LookupFactory.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/naming/factory/LookupFactory.java?rev=1831262&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/naming/factory/LookupFactory.java (added)
+++ tomcat/trunk/java/org/apache/naming/factory/LookupFactory.java Wed May  9 
15:46:02 2018
@@ -0,0 +1,147 @@
+/*
+ * 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.naming.factory;
+
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Set;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.Name;
+import javax.naming.NamingException;
+import javax.naming.RefAddr;
+import javax.naming.Reference;
+import javax.naming.spi.ObjectFactory;
+
+import org.apache.juli.logging.Log;
+import org.apache.juli.logging.LogFactory;
+import org.apache.naming.LookupRef;
+import org.apache.naming.StringManager;
+
+/**
+ * Object factory for lookups.
+ */
+public class LookupFactory implements ObjectFactory {
+
+    private static final Log log = LogFactory.getLog(LookupFactory.class);
+    private static final StringManager sm = 
StringManager.getManager(LookupFactory.class);
+
+    private static final ThreadLocal<Set<String>> names = new 
ThreadLocal<Set<String>>() {
+
+        @Override
+        protected Set<String> initialValue() {
+            return new HashSet<>();
+        }
+    };
+
+    /**
+     * Create a new Resource env instance.
+     *
+     * @param obj The reference object describing the DataSource
+     */
+    @Override
+    public Object getObjectInstance(Object obj, Name name, Context nameCtx,
+            Hashtable<?, ?> environment) throws Exception {
+
+        String lookupName = null;
+        Object result = null;
+
+        if (obj instanceof LookupRef) {
+            Reference ref = (Reference) obj;
+            ObjectFactory factory = null;
+            RefAddr lookupNameRefAddr = ref.get(LookupRef.LOOKUP_NAME);
+            if (lookupNameRefAddr != null) {
+                lookupName = lookupNameRefAddr.getContent().toString();
+            }
+
+            try {
+                if (lookupName != null) {
+                    if (!names.get().add(lookupName)) {
+                        String msg = 
sm.getString("lookupFactory.circularReference", lookupName);
+                        NamingException ne = new NamingException(msg);
+                        log.warn(msg, ne);
+                        throw ne;
+                    }
+                }
+                RefAddr factoryRefAddr = ref.get(Constants.FACTORY);
+                if (factoryRefAddr != null) {
+                    // Using the specified factory
+                    String factoryClassName = 
factoryRefAddr.getContent().toString();
+                    // Loading factory
+                    ClassLoader tcl = 
Thread.currentThread().getContextClassLoader();
+                    Class<?> factoryClass = null;
+                    if (tcl != null) {
+                        try {
+                            factoryClass = tcl.loadClass(factoryClassName);
+                        } catch (ClassNotFoundException e) {
+                            NamingException ex = new NamingException(
+                                    sm.getString("lookupFactory.loadFailed"));
+                            ex.initCause(e);
+                            throw ex;
+                        }
+                    } else {
+                        try {
+                            factoryClass = Class.forName(factoryClassName);
+                        } catch (ClassNotFoundException e) {
+                            NamingException ex = new NamingException(
+                                    sm.getString("lookupFactory.loadFailed"));
+                            ex.initCause(e);
+                            throw ex;
+                        }
+                    }
+                    if (factoryClass != null) {
+                        try {
+                            factory = (ObjectFactory) 
factoryClass.newInstance();
+                        } catch (Throwable t) {
+                            if (t instanceof NamingException)
+                                throw (NamingException) t;
+                            NamingException ex = new NamingException(
+                                    
sm.getString("lookupFactory.createFailed"));
+                            ex.initCause(t);
+                            throw ex;
+                        }
+                    }
+                }
+                // Note: No defaults here
+                if (factory != null) {
+                    result = factory.getObjectInstance(obj, name, nameCtx, 
environment);
+                } else {
+                    if (lookupName == null) {
+                        throw new 
NamingException(sm.getString("lookupFactory.createFailed"));
+                    } else {
+                        result = new InitialContext().lookup(lookupName);
+                    }
+                }
+
+                Class<?> clazz = Class.forName(ref.getClassName());
+                if (result != null && 
!clazz.isAssignableFrom(result.getClass())) {
+                    String msg = sm.getString("lookupFactory.typeMismatch",
+                            name, ref.getClassName(), lookupName, 
result.getClass().getName());
+                    NamingException ne = new NamingException(msg);
+                    log.warn(msg, ne);
+                    throw ne;
+                }
+            } finally {
+                names.get().remove(lookupName);
+            }
+        }
+
+
+        return result;
+    }
+}

Propchange: tomcat/trunk/java/org/apache/naming/factory/LookupFactory.java
------------------------------------------------------------------------------
    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