KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > factories > DefaultCacheFactory


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.factories;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.jboss.cache.Cache;
12 import org.jboss.cache.CacheImpl;
13 import org.jboss.cache.config.Configuration;
14 import org.jboss.cache.config.ConfigurationException;
15
16 /**
17  * Default implementation of the Cache Factory that contains only convenient static methods
18  *
19  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
20  */

21 public class DefaultCacheFactory implements CacheFactory
22 {
23    private static CacheFactory singleton = null;
24    private Log log = LogFactory.getLog(DefaultCacheFactory.class);
25
26    /**
27     * Protected constructor to prevent instantiation by user code
28     */

29    protected DefaultCacheFactory()
30    {
31       // protected constructor to prevent instantiation
32
}
33
34    public static CacheFactory getInstance()
35    {
36       if (singleton == null)
37       {
38          synchronized (DefaultCacheFactory.class)
39          {
40             if (singleton == null)
41             {
42                singleton = new DefaultCacheFactory();
43             }
44          }
45       }
46       return singleton;
47    }
48
49    public Cache createCache() throws ConfigurationException
50    {
51       // don't bother cloning the cfg since we're creating it here.
52
return createCache(new Configuration(), true, false);
53    }
54
55    public Cache createCache(String JavaDoc configFileName) throws ConfigurationException
56    {
57       return createCache(configFileName, true);
58    }
59
60    public Cache createCache(String JavaDoc configFileName, boolean start) throws ConfigurationException
61    {
62       XmlConfigurationParser parser = new XmlConfigurationParser();
63       Configuration c = parser.parseFile(configFileName);
64       // don't bother cloning the cfg since we're creating it here.
65
return createCache(c, start, false);
66    }
67
68    /**
69     * This implementation clones the configuration passed in before using it.
70     *
71     * @param configuration to use
72     * @return a cache
73     * @throws ConfigurationException if there are problems with the cfg
74     */

75    public Cache createCache(Configuration configuration) throws ConfigurationException
76    {
77       // make sure we clone any configs passed in from the outside world.
78
return createCache(configuration, true, true);
79    }
80
81    /**
82     * This implementation clones the configuration passed in before using it.
83     *
84     * @param configuration to use
85     * @param start whether to start the cache
86     * @return a cache
87     * @throws ConfigurationException if there are problems with the cfg
88     */

89    public Cache createCache(Configuration configuration, boolean start) throws ConfigurationException
90    {
91       // make sure we clone any configs passed in from the outside world, in case it is reused by several caches.
92
// We don't want updates to one cache instance be reflected in others.
93
return createCache(configuration, start, true);
94    }
95
96    /**
97     * Creates a cache with the option of starting the cache and cloning the configuration.
98     *
99     * @param configuration with which to create a cache
100     * @param start whether we start the cache or not
101     * @param cloneConfiguration whether we clone the cfg or not
102     * @return a cache
103     * @throws ConfigurationException if there are problems with the cfg
104     */

105    private Cache createCache(Configuration configuration, boolean start, boolean cloneConfiguration) throws ConfigurationException
106    {
107       Configuration toUse = configuration;
108       if (cloneConfiguration)
109       {
110          try
111          {
112             toUse = configuration.clone();
113          }
114          catch (CloneNotSupportedException JavaDoc e)
115          {
116             log.error("Unable to clone configuration?", e);
117          }
118       }
119       try
120       {
121          CacheImpl cache = new CacheImpl();
122          cache.setConfiguration(toUse);
123          if (start) cache.start();
124          return cache;
125       }
126       catch (Exception JavaDoc e)
127       {
128          if (e instanceof ConfigurationException) throw (ConfigurationException) e;
129          throw new RuntimeException JavaDoc(e);
130       }
131    }
132 }
133
Popular Tags