[jbosscache-commits] JBoss Cache SVN: r7316 - in core/branches/flat/src/main/java/org/jboss/starobrno: manager and 1 other directory.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Fri Dec 12 07:17:09 EST 2008


Author: manik.surtani at jboss.com
Date: 2008-12-12 07:17:09 -0500 (Fri, 12 Dec 2008)
New Revision: 7316

Modified:
   core/branches/flat/src/main/java/org/jboss/starobrno/config/Configuration.java
   core/branches/flat/src/main/java/org/jboss/starobrno/manager/CacheManager.java
Log:
Fleshed out cache manager stuff

Modified: core/branches/flat/src/main/java/org/jboss/starobrno/config/Configuration.java
===================================================================
--- core/branches/flat/src/main/java/org/jboss/starobrno/config/Configuration.java	2008-12-12 11:55:35 UTC (rev 7315)
+++ core/branches/flat/src/main/java/org/jboss/starobrno/config/Configuration.java	2008-12-12 12:17:09 UTC (rev 7316)
@@ -31,11 +31,7 @@
 import org.w3c.dom.Element;
 
 import java.net.URL;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
+import java.util.*;
 
 /**
  * Encapsulates the configuration of a Cache.
@@ -1039,4 +1035,9 @@
    {
       return getEvictionConfig() != null;
    }
+
+   public void applyOverrides(Configuration overrides)
+   {
+      // TODO: Manik: Implement me
+   }
 }

Modified: core/branches/flat/src/main/java/org/jboss/starobrno/manager/CacheManager.java
===================================================================
--- core/branches/flat/src/main/java/org/jboss/starobrno/manager/CacheManager.java	2008-12-12 11:55:35 UTC (rev 7315)
+++ core/branches/flat/src/main/java/org/jboss/starobrno/manager/CacheManager.java	2008-12-12 12:17:09 UTC (rev 7316)
@@ -21,12 +21,15 @@
  */
 package org.jboss.starobrno.manager;
 
+import org.jboss.cache.DefaultCacheFactory;
 import org.jboss.starobrno.Cache;
 import org.jboss.starobrno.config.Configuration;
 import org.jboss.starobrno.config.ConfigurationException;
-import org.jboss.starobrno.config.parsing.XmlConfigurationParserJBC3;
+import org.jboss.starobrno.config.parsing.XmlConfigurationParser;
+import org.jboss.starobrno.config.parsing.XmlConfigurationParserImpl;
 import org.jboss.starobrno.lifecycle.Lifecycle;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
@@ -126,10 +129,9 @@
     * path.
     *
     * @param configurationFile name of configuration file to use as a template for all caches created
-    * @throws org.jboss.starobrno.config.ConfigurationException
-    *          if there is a problem with the configuration file.
+    * @throws java.io.IOException if there is a problem with the configuration file.
     */
-   public CacheManager(String configurationFile) throws ConfigurationException
+   public CacheManager(String configurationFile) throws IOException
    {
       this(configurationFile, true);
    }
@@ -141,22 +143,14 @@
     *
     * @param configurationFile name of configuration file to use as a template for all caches created
     * @param start             if true, the cache manager is started
-    * @throws org.jboss.starobrno.config.ConfigurationException
-    *          if there is a problem with the configuration file.
+    * @throws java.io.IOException if there is a problem with the configuration file.
     */
-   public CacheManager(String configurationFile, boolean start) throws ConfigurationException
+   public CacheManager(String configurationFile, boolean start) throws IOException
    {
       try
       {
-         // todo - need to update parser as per https://docspace.corp.redhat.com/clearspace/docs/DOC-16643
-         // todo - need to store named cache overrides
-         XmlConfigurationParserJBC3 parser = new XmlConfigurationParserJBC3();
-         configuration = parser.parseFile(configurationFile);
+         initialize(new XmlConfigurationParserImpl(configurationFile));
       }
-      catch (ConfigurationException ce)
-      {
-         throw ce;
-      }
       catch (RuntimeException re)
       {
          throw new ConfigurationException(re);
@@ -168,10 +162,9 @@
     * Constructs and starts a new instance of the CacheManager, using the input stream passed in to read configuration file contents.
     *
     * @param configurationStream stream containing configuration file contents, to use as a template for all caches created
-    * @throws org.jboss.starobrno.config.ConfigurationException
-    *          if there is a problem with the configuration stream.
+    * @throws java.io.IOException if there is a problem with the configuration stream.
     */
-   public CacheManager(InputStream configurationStream) throws ConfigurationException
+   public CacheManager(InputStream configurationStream) throws IOException
    {
       this(configurationStream, true);
    }
@@ -181,22 +174,14 @@
     *
     * @param configurationStream stream containing configuration file contents, to use as a template for all caches created
     * @param start               if true, the cache manager is started
-    * @throws org.jboss.starobrno.config.ConfigurationException
-    *          if there is a problem with the configuration stream.
+    * @throws java.io.IOException if there is a problem reading the configuration stream
     */
-   public CacheManager(InputStream configurationStream, boolean start) throws ConfigurationException
+   public CacheManager(InputStream configurationStream, boolean start) throws IOException
    {
       try
       {
-         // todo - need to update parser as per https://docspace.corp.redhat.com/clearspace/docs/DOC-16643
-         // todo - need to store named cache overrides
-         XmlConfigurationParserJBC3 parser = new XmlConfigurationParserJBC3();
-         configuration = parser.parseStream(configurationStream);
+         initialize(new XmlConfigurationParserImpl(configurationStream));
       }
-      catch (ConfigurationException ce)
-      {
-         throw ce;
-      }
       catch (RuntimeException re)
       {
          throw new ConfigurationException(re);
@@ -204,6 +189,12 @@
       if (start) start();
    }
 
+   private void initialize(XmlConfigurationParser initializedParser)
+   {
+      configuration = initializedParser.parseDefaultConfiguration();
+      configurationOverrides.putAll(initializedParser.parseNamedConfigurations());
+   }
+
    /**
     * Defines a named cache.  Named caches can be defined by using this method, in which case the configuration
     * passed in is used to override the default configuration used when this cache manager instance was created.
@@ -237,13 +228,20 @@
     */
    public Cache getCache()
    {
-      return getOrCreateCache(DEFAULT_CACHE_NAME);
+      try
+      {
+         return getCache(DEFAULT_CACHE_NAME);
+      }
+      catch (NamedCacheNotFoundException e)
+      {
+         throw new RuntimeException("Default cache not found; this should NEVER happen!", e);
+      }
    }
 
    /**
-    * Retrieves a named cache from the system.  The named cache must have been created previously using the {@link #createCache(String)}
-    * or {@link #getOrCreateCache(String)} methods, or have been declared in the configuration file used by this <tt>CacheManager</tt>
-    * instance, or this method will throw a {@link org.jboss.starobrno.manager.NamedCacheNotFoundException}.
+    * Retrieves a named cache from the system.  The named cache must have been defined previously using the {@link #defineCache(String, org.jboss.starobrno.config.Configuration)}
+    * method or have been declared in the configuration file used by this <tt>CacheManager</tt>
+    * instance, otherwise this method will throw a {@link org.jboss.starobrno.manager.NamedCacheNotFoundException}.
     *
     * @param cacheName name of cache to retrieve
     * @return a cache instance identified by cacheName
@@ -251,26 +249,38 @@
     */
    public Cache getCache(String cacheName) throws NamedCacheNotFoundException
    {
-      if (cacheName == null) throw new NullPointerException("Null arguments not allowed");
-      return getOrCreateCache(cacheName);
-   }
+      if (cacheName == null)
+         throw new NullPointerException("Null arguments not allowed");
 
-   private Cache getOrCreateCache(String cacheName)
-   {
-      // todo
-      return null;
+      if (caches.containsKey(cacheName))
+         return caches.get(cacheName);
+
+      if (!cacheName.equals(DEFAULT_CACHE_NAME) && !configurationOverrides.containsKey(cacheName))
+         throw new NamedCacheNotFoundException("No such cache named " + cacheName + " defined or declared with this CacheManager!");
+
+      return createCache(cacheName);
    }
 
-
    private Cache createCache(String cacheName)
    {
-      // todo
-//      if (!caches.containsKey(cacheName))
-//         caches.putIfAbsent(cacheName, createNewCache());
+      Configuration c = configuration.clone();
+      if (!cacheName.equals(DEFAULT_CACHE_NAME))
+      {
+         Configuration overrides = configurationOverrides.get(cacheName);
+         c.applyOverrides(overrides);
+      }
 
-      Cache c = caches.get(cacheName);
-      c.start();
-      return c;
+      Cache cache = new DefaultCacheFactory().createCache(c, false);
+      Cache other = caches.putIfAbsent(cacheName, cache);
+      if (other == null)
+      {
+         cache.start();
+         return cache;
+      }
+      else
+      {
+         return other;
+      }
    }
 
    public void start()
@@ -280,6 +290,6 @@
 
    public void stop()
    {
-      // todo
+      for (Cache cache : caches.values()) cache.stop();
    }
 }




More information about the jbosscache-commits mailing list