You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2010/11/04 18:59:21 UTC

svn commit: r1031112 - in /tomcat/trunk: java/org/apache/catalina/core/ java/org/apache/catalina/deploy/ java/org/apache/naming/ test/org/apache/naming/resources/ webapps/docs/ webapps/docs/config/

Author: markt
Date: Thu Nov  4 17:59:20 2010
New Revision: 1031112

URL: http://svn.apache.org/viewvc?rev=1031112&view=rev
Log:
https://issues.apache.org/bugzilla/show_bug.cgi?id=50159
Add a new attribute for <Resource> elements, singleton that controls whether or not a new object is created every time a JNDI lookup is performed to obtain the resource. The default value is true, which will return the same instance of the resource in every JNDI lookup.

Modified:
    tomcat/trunk/java/org/apache/catalina/core/NamingContextListener.java
    tomcat/trunk/java/org/apache/catalina/deploy/ContextResource.java
    tomcat/trunk/java/org/apache/naming/NamingContext.java
    tomcat/trunk/java/org/apache/naming/ResourceRef.java
    tomcat/trunk/test/org/apache/naming/resources/TestNamingContext.java
    tomcat/trunk/webapps/docs/changelog.xml
    tomcat/trunk/webapps/docs/config/context.xml

Modified: tomcat/trunk/java/org/apache/catalina/core/NamingContextListener.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/NamingContextListener.java?rev=1031112&r1=1031111&r2=1031112&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/NamingContextListener.java (original)
+++ tomcat/trunk/java/org/apache/catalina/core/NamingContextListener.java Thu Nov  4 17:59:20 2010
@@ -849,7 +849,8 @@ public class NamingContextListener
     /**
      * Set the specified local EJBs in the naming context.
      */
-    public void addLocalEjb(ContextLocalEjb localEjb) {
+    public void addLocalEjb(
+            @SuppressWarnings("unused") ContextLocalEjb localEjb) {
         // NO-OP
     }
 
@@ -992,7 +993,8 @@ public class NamingContextListener
         // Create a reference to the resource.
         Reference ref = new ResourceRef
             (resource.getType(), resource.getDescription(),
-             resource.getScope(), resource.getAuth());
+             resource.getScope(), resource.getAuth(),
+             resource.getSingleton());
         // Adding the additional parameters, if any
         Iterator<String> params = resource.listProperties();
         while (params.hasNext()) {

Modified: tomcat/trunk/java/org/apache/catalina/deploy/ContextResource.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/deploy/ContextResource.java?rev=1031112&r1=1031111&r2=1031112&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/deploy/ContextResource.java (original)
+++ tomcat/trunk/java/org/apache/catalina/deploy/ContextResource.java Thu Nov  4 17:59:20 2010
@@ -65,6 +65,21 @@ public class ContextResource extends Res
     }
 
 
+    /**
+     * Is this resource known to be a singleton resource. The default value is
+     * true since this is what users expect although the JavaEE spec implies
+     * that the default should be false.
+     */
+    private boolean singleton = true;
+    
+    public boolean getSingleton() {
+        return singleton;
+    }
+    
+    public void setSingleton(boolean singleton) {
+        this.singleton = singleton;
+    }
+
     // --------------------------------------------------------- Public Methods
 
 

Modified: tomcat/trunk/java/org/apache/naming/NamingContext.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/naming/NamingContext.java?rev=1031112&r1=1031111&r2=1031112&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/naming/NamingContext.java (original)
+++ tomcat/trunk/java/org/apache/naming/NamingContext.java Thu Nov  4 17:59:20 2010
@@ -825,6 +825,13 @@ public class NamingContext implements Co
                 try {
                     Object obj = NamingManager.getObjectInstance
                         (entry.value, name, this, env);
+                    boolean singleton = Boolean.parseBoolean(
+                            (String) ((ResourceRef) entry.value).get(
+                                    "singleton").getContent());
+                    if (singleton) {
+                        entry.type = NamingEntry.ENTRY;
+                        entry.value = obj;
+                    }
                     return obj; 
                 } catch (NamingException e) {
                     throw e;

Modified: tomcat/trunk/java/org/apache/naming/ResourceRef.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/naming/ResourceRef.java?rev=1031112&r1=1031111&r2=1031112&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/naming/ResourceRef.java (original)
+++ tomcat/trunk/java/org/apache/naming/ResourceRef.java Thu Nov  4 17:59:20 2010
@@ -32,13 +32,13 @@ import javax.naming.StringRefAddr;
  * @version $Id$
  */
 
-public class ResourceRef
-    extends Reference {
+public class ResourceRef extends Reference {
 
+    private static final long serialVersionUID = 1L;
 
+    
     // -------------------------------------------------------------- Constants
 
-
     /**
      * Default factory for this reference.
      */
@@ -64,6 +64,11 @@ public class ResourceRef
     public static final String AUTH = "auth";
 
 
+    /**
+     * Is this resource a singleton
+     */
+    public static final String SINGLETON = "singleton";
+
     // ----------------------------------------------------------- Constructors
 
 
@@ -75,8 +80,8 @@ public class ResourceRef
      * @param auth Resource authentication
      */
     public ResourceRef(String resourceClass, String description, 
-                       String scope, String auth) {
-        this(resourceClass, description, scope, auth, null, null);
+                       String scope, String auth, boolean singleton) {
+        this(resourceClass, description, scope, auth, singleton, null, null);
     }
 
 
@@ -88,8 +93,8 @@ public class ResourceRef
      * @param auth Resource authentication
      */
     public ResourceRef(String resourceClass, String description, 
-                       String scope, String auth, String factory,
-                       String factoryLocation) {
+                       String scope, String auth, boolean singleton,
+                       String factory, String factoryLocation) {
         super(resourceClass, factory, factoryLocation);
         StringRefAddr refAddr = null;
         if (description != null) {
@@ -104,6 +109,9 @@ public class ResourceRef
             refAddr = new StringRefAddr(AUTH, auth);
             add(refAddr);
         }
+        // singleton is a boolean so slightly different handling
+        refAddr = new StringRefAddr(SINGLETON, Boolean.toString(singleton));
+        add(refAddr);
     }
 
 

Modified: tomcat/trunk/test/org/apache/naming/resources/TestNamingContext.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/naming/resources/TestNamingContext.java?rev=1031112&r1=1031111&r2=1031112&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/naming/resources/TestNamingContext.java (original)
+++ tomcat/trunk/test/org/apache/naming/resources/TestNamingContext.java Thu Nov  4 17:59:20 2010
@@ -37,7 +37,15 @@ import org.apache.tomcat.util.buf.ByteCh
 
 public class TestNamingContext extends TomcatBaseTest {
 
-    public void testLookup() throws Exception {
+    public void testLookupSingletonResource() throws Exception {
+        doTestLookup(true);
+    }
+    
+    public void testLookupNonSingletonResource() throws Exception {
+        doTestLookup(false);
+    }
+    
+    public void doTestLookup(boolean useSingletonResource) throws Exception {
         Tomcat tomcat = getTomcatInstance();
         tomcat.enableNaming();
         
@@ -50,6 +58,7 @@ public class TestNamingContext extends T
         cr.setName("list/foo");
         cr.setType("org.apache.naming.resources.TesterObject");
         cr.setProperty("factory", "org.apache.naming.resources.TesterFactory");
+        cr.setSingleton(useSingletonResource);
         ctx.getNamingResources().addResource(cr);
         
         // Map the test Servlet
@@ -60,7 +69,14 @@ public class TestNamingContext extends T
         tomcat.start();
 
         ByteChunk bc = getUrl("http://localhost:" + getPort() + "/");
-        assertEquals("OK", bc.toString());
+        
+        String expected;
+        if (useSingletonResource) {
+            expected = "EQUAL";
+        } else {
+            expected = "NOTEQUAL";
+        }
+        assertEquals(expected, bc.toString());
 
     }
 
@@ -80,9 +96,9 @@ public class TestNamingContext extends T
                 Object obj1 = ctx.lookup("java:comp/env/list/foo");
                 Object obj2 = ctx.lookup("java:comp/env/list/foo");
                 if (obj1 == obj2) {
-                    out.print("FAIL");
+                    out.print("EQUAL");
                 } else {
-                    out.print("OK");
+                    out.print("NOTEQUAL");
                 }
             } catch (NamingException ne) {
                 ne.printStackTrace(out);

Modified: tomcat/trunk/webapps/docs/changelog.xml
URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1031112&r1=1031111&r2=1031112&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/trunk/webapps/docs/changelog.xml Thu Nov  4 17:59:20 2010
@@ -73,6 +73,13 @@
         <bug>50157</bug>: Ensure MapperListener is only added to a container
         object once. (markt)
       </fix>
+      <fix>
+        <bug>50159</bug>: Add a new attribute for <code>&lt;Resource&gt;</code>
+        elements, <code>singleton</code>, that controls whether or not a new
+        object is created every time a JNDI lookup is performed to obtain the
+        resource. The default value is <code>true</code>, which will return the
+        same instance of the resource in every JNDI lookup. (markt)
+      </fix>
       <add>
         Improve debug logging for MapperListener registration. (markt)
       </add>

Modified: tomcat/trunk/webapps/docs/config/context.xml
URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/config/context.xml?rev=1031112&r1=1031111&r2=1031112&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/config/context.xml (original)
+++ tomcat/trunk/webapps/docs/config/context.xml Thu Nov  4 17:59:20 2010
@@ -878,6 +878,16 @@
         connections are assumed to be shareable.</p>
       </attribute>
 
+      <attribute name="singleton" required="false">
+        <p>Specify whether this resource definition is for a singleton resource,
+        i.e. one where there is only a single instance of the resource. If this
+        attribute is <code>true</code>, multiple JNDI lookups for this resource
+        will return the same object. If this attribute is <code>false</code>,
+        multiple JNDI lookups for this resource will return different objects.
+        The value of this attribute must be <code>true</code> or
+        <code>false</code>. By default, this attribute is <code>true</code>.</p>
+      </attribute>
+
       <attribute name="type" required="true">
         <p>The fully qualified Java class name expected by the web
         application when it performs a lookup for this resource.</p>



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