geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject cvs commit: incubator-geronimo/modules/kernel/src/test/org/apache/geronimo/kernel BootstrapTest.java
Date Thu, 05 Feb 2004 05:26:33 GMT
adc         2004/02/04 21:26:33

  Modified:    modules/kernel/src/java/org/apache/geronimo/kernel
                        Kernel.java
               modules/kernel/src/test/org/apache/geronimo/kernel
                        BootstrapTest.java
  Log:
  Tidier map management comme the Lock class.
  
  Revision  Changes    Path
  1.15      +56 -19    incubator-geronimo/modules/kernel/src/java/org/apache/geronimo/kernel/Kernel.java
  
  Index: Kernel.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/kernel/src/java/org/apache/geronimo/kernel/Kernel.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- Kernel.java	5 Feb 2004 00:41:30 -0000	1.14
  +++ Kernel.java	5 Feb 2004 05:26:33 -0000	1.15
  @@ -73,6 +73,7 @@
   import java.io.File;
   import java.io.IOException;
   import java.io.Serializable;
  +import java.lang.ref.ReferenceQueue;
   import java.lang.ref.WeakReference;
   import java.net.URI;
   import java.net.URL;
  @@ -127,6 +128,7 @@
       public static final ObjectName CONFIG_STORE = JMXUtil.getObjectName("geronimo.boot:role=ConfigurationStore");
   
       private static final Map kernels = new Hashtable();
  +    private static final ReferenceQueue queue = new ReferenceQueue();
       private final String kernelName;
       private final String domainName;
       private final GBeanInfo storeInfo;
  @@ -152,8 +154,6 @@
           this.domainName = domainName;
           this.storeInfo = storeInfo;
           this.configStore = configStore;
  -
  -        kernels.put(kernelName, new WeakReference(this));
       }
   
       /**
  @@ -210,15 +210,17 @@
        * @return the kernel that was registered with that name
        */
       public static Kernel getKernel(String name) {
  -        WeakReference reference = (WeakReference) kernels.get(name);
  -
  -        if (reference == null) return null;
  -
  -        Kernel result = (Kernel) reference.get();
  -        if (result == null) {
  -            kernels.remove(name);
  +        synchronized (kernels) {
  +            processQueue();
  +            KernelReference ref = (KernelReference) kernels.get(name);
  +            if (ref != null) {
  +                Kernel kernel = (Kernel) ref.get();
  +                if (kernel != null) {
  +                    return kernel;
  +                }
  +            }
           }
  -        return result;
  +        return null;
       }
   
       /**
  @@ -230,15 +232,19 @@
        * @throws IllegalStateException if more than one
        */
       public static Kernel getSingleKernel() {
  -        int size = kernels.size();
  -        if (size > 1) throw new IllegalStateException("More than one kernel has been
registered.");
  -        if (size < 1) return null;
  -
  -        Kernel result = (Kernel) ((WeakReference) kernels.values().iterator().next()).get();
  -        if (result == null) {
  -            kernels.clear();
  +        synchronized (kernels) {
  +            processQueue();
  +
  +            int size = kernels.size();
  +            if (size > 1) throw new IllegalStateException("More than one kernel has
been registered.");
  +            if (size < 1) return null;
  +
  +            Kernel result = (Kernel) ((KernelReference) kernels.values().iterator().next()).get();
  +            if (result == null) {
  +                kernels.clear();
  +            }
  +            return result;
           }
  -        return result;
       }
   
       public static ObjectName getConfigObjectName(URI configID) throws MalformedObjectNameException
{
  @@ -431,6 +437,13 @@
           }
           log = LogFactory.getLog(Kernel.class.getName());
           log.info("Starting boot");
  +
  +        if (kernelName != null) {
  +            synchronized (kernels) {
  +                kernels.put(kernelName, new KernelReference(kernelName, this));
  +            }
  +        }
  +
           mbServer = MBeanServerFactory.createMBeanServer(domainName);
           mbServer.registerMBean(this, KERNEL);
           mbServer.registerMBean(new DependencyService(), DEPENDENCY_SERVICE);
  @@ -488,6 +501,13 @@
           synchronized (this) {
               notify();
           }
  +
  +        if (kernelName != null) {
  +            synchronized (kernels) {
  +                kernels.remove(kernelName);
  +            }
  +        }
  +
           log.info("Kernel shutdown complete");
       }
   
  @@ -506,4 +526,21 @@
       public void removeNotificationListener(NotificationListener listener) throws ListenerNotFoundException
{
       }
   
  +    private static void processQueue() {
  +        KernelReference kernelRef;
  +        while ((kernelRef = (KernelReference) queue.poll()) != null) {
  +            synchronized (kernels) {
  +                kernels.remove(kernelRef.key);
  +            }
  +        }
  +    }
  +
  +    private static class KernelReference extends WeakReference {
  +        private final Object key;
  +
  +        public KernelReference(Object key, Object kernel) {
  +            super(kernel, queue);
  +            this.key = key;
  +        }
  +    }
   }
  
  
  
  1.4       +10 -1     incubator-geronimo/modules/kernel/src/test/org/apache/geronimo/kernel/BootstrapTest.java
  
  Index: BootstrapTest.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/kernel/src/test/org/apache/geronimo/kernel/BootstrapTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- BootstrapTest.java	4 Feb 2004 05:42:57 -0000	1.3
  +++ BootstrapTest.java	5 Feb 2004 05:26:33 -0000	1.4
  @@ -78,23 +78,32 @@
   
       public void testCreate() throws Exception {
           Kernel kernel = new Kernel("test.kernel", "geronimo", storeInfo, configRoot);
  +        assertEquals("No kernel should be registered", null, Kernel.getKernel("test.kernel"));
           kernel.boot();
  +        assertEquals("test.kernel kernel should be registered", kernel, Kernel.getKernel("test.kernel"));
           kernel.shutdown();
  +        assertEquals("No kernel should be registered", null, Kernel.getKernel("test.kernel"));
       }
   
       public void testPersist() throws Exception {
           Kernel kernel = new Kernel("test.kernel", "geronimo", storeInfo, configRoot);
  +        assertEquals("No kernel should be registered", null, Kernel.getKernel("test.kernel"));
           kernel.boot();
  +        assertEquals("test.kernel kernel should be registered", kernel, Kernel.getKernel("test.kernel"));
           ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(kernelState));
           oos.writeObject(kernel);
           oos.close();
           kernel.shutdown();
  +        assertEquals("No kernel should be registered", null, Kernel.getKernel("test.kernel"));
   
           ObjectInputStream ois = new ObjectInputStream(new FileInputStream(kernelState));
           kernel = (Kernel) ois.readObject();
           ois.close();
  +        assertEquals("No kernel should be registered", null, Kernel.getKernel("test.kernel"));
           kernel.boot();
  +        assertEquals("test.kernel kernel should be registered", kernel, Kernel.getKernel("test.kernel"));
           kernel.shutdown();
  +        assertEquals("No kernel should be registered", null, Kernel.getKernel("test.kernel"));
       }
   
       protected void setUp() throws Exception {
  
  
  

Mime
View raw message