Author: kkolinko
Date: Thu Jan 21 17:25:01 2016
New Revision: 1726031

URL: http://svn.apache.org/viewvc?rev=1726031&view=rev
Log:
Allow singleton Server instance stored by ServerFactory to be cleared.

Like the previous fix to ResourceLinkFactory this is intended for use by unit 
tests.
Discussion: ("Unit tests for Tomcat 6", 2011-10)
http://tomcat.markmail.org/thread/ko7ip7obvyaftwe4

Added:
    tomcat/tc6.0.x/trunk/test/org/apache/catalina/TestServerFactory.java   
(with props)
Modified:
    tomcat/tc6.0.x/trunk/java/org/apache/catalina/ServerFactory.java
    tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml

Modified: tomcat/tc6.0.x/trunk/java/org/apache/catalina/ServerFactory.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/java/org/apache/catalina/ServerFactory.java?rev=1726031&r1=1726030&r2=1726031&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/java/org/apache/catalina/ServerFactory.java (original)
+++ tomcat/tc6.0.x/trunk/java/org/apache/catalina/ServerFactory.java Thu Jan 21 
17:25:01 2016
@@ -18,6 +18,8 @@
 
 package org.apache.catalina;
 
+import java.util.concurrent.atomic.AtomicReference;
+
 import org.apache.catalina.core.StandardServer;
 
 
@@ -41,7 +43,7 @@ public class ServerFactory {
     /**
      * The singleton <code>Server</code> instance for this JVM.
      */
-    private static Server server = null;
+    private static final AtomicReference<Server> server = new 
AtomicReference<Server>();
 
 
     // --------------------------------------------------------- Public Methods
@@ -51,10 +53,29 @@ public class ServerFactory {
      * Return the singleton <code>Server</code> instance for this JVM.
      */
     public static Server getServer() {
-        if( server==null )
-            server=new StandardServer();
-        return (server);
+        return getServer(true);
+    }
+
 
+    /**
+     * Return the singleton <code>Server</code> instance for this JVM.
+     *
+     * @param create
+     *            <code>true</code> to create a server if none is available and
+     *            always return a <code>Server</code> instance,
+     *            <code>false</code> to peek the current value and return
+     *            <code>null</code> if no server has been created
+     * @return Server instance or null
+     */
+    @SuppressWarnings("unused")
+    public static Server getServer(boolean create) {
+        Server s = server.get();
+        if (s == null && create) {
+            // Note that StandardServer() constructor calls setServer()
+            new StandardServer();
+            s = server.get();
+        }
+        return s;
     }
 
 
@@ -68,10 +89,17 @@ public class ServerFactory {
      */
     public static void setServer(Server theServer) {
 
-        if (server == null)
-            server = theServer;
+        server.compareAndSet(null, theServer);
 
     }
 
 
+    /**
+     * Clears the singleton <code>Server</code> instance for this JVM. Allows 
to
+     * run several instances of Tomcat sequentially in the same JVM. Unit tests
+     * use this feature.
+     */
+    public static void clear() {
+        server.set(null);
+    }
 }

Added: tomcat/tc6.0.x/trunk/test/org/apache/catalina/TestServerFactory.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/test/org/apache/catalina/TestServerFactory.java?rev=1726031&view=auto
==============================================================================
--- tomcat/tc6.0.x/trunk/test/org/apache/catalina/TestServerFactory.java (added)
+++ tomcat/tc6.0.x/trunk/test/org/apache/catalina/TestServerFactory.java Thu 
Jan 21 17:25:01 2016
@@ -0,0 +1,47 @@
+/*
+ * 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.catalina;
+
+import org.junit.After;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.catalina.core.StandardServer;
+
+public class TestServerFactory {
+
+    @After
+    public void tearDown() {
+        ServerFactory.clear();
+    }
+
+    @Test
+    public void test1() {
+        assertNull(ServerFactory.getServer(false));
+        assertTrue(ServerFactory.getServer() instanceof StandardServer);
+
+        ServerFactory.clear();
+        assertNull(ServerFactory.getServer(false));
+
+        Server s = new StandardServer();
+        assertEquals(s, ServerFactory.getServer());
+        assertEquals(s, ServerFactory.getServer(true));
+    }
+}

Propchange: tomcat/tc6.0.x/trunk/test/org/apache/catalina/TestServerFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml
URL: 
http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml?rev=1726031&r1=1726030&r2=1726031&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml Thu Jan 21 17:25:01 2016
@@ -119,12 +119,16 @@
         processing a forwarded request where the target includes a query string
         that contains a parameter with no value. (markt/kkolinko)
       </fix>
-      <fix>
-        Allow ResourceLinkFactory to be initialized more than once. This is
-        used by unit tests when running several copies of Tomcat sequentially
-        in the same JVM. When running with a SecurityManager the initialization
-        method is protected by requiring a RuntimePermission. (kkolinko)
-      </fix>
+      <add>
+        Allow singleton server instance stored by <code>ServerFactory</code>
+        to be cleared.
+        Allow <code>ResourceLinkFactory</code> to be initialized more than 
once.
+        This is used by unit tests when running several copies of Tomcat
+        sequentially in the same JVM.
+        When running with a SecurityManager the initialization method of
+        <code>ResourceLinkFactory</code> is protected by requiring a
+        <code>RuntimePermission</code>. (kkolinko)
+      </add>
     </changelog>
   </subsection>
   <subsection name="Coyote">



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

Reply via email to