/* * JBoss, Home of Professional Open Source. * Copyright 2000 - 2008, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.cache; import org.jboss.cache.annotations.Compat; import org.jboss.cache.config.Configuration; import org.jboss.cache.config.ConfigurationException; import org.jboss.cache.config.LegacyConfigurationException; import org.jboss.cache.config.parsing.XmlConfigurationParser; import org.jboss.cache.config.parsing.XmlConfigurationParser2x; import org.jboss.cache.factories.ComponentFactory; import org.jboss.cache.factories.ComponentRegistry; import org.jboss.cache.invocation.CacheInvocationDelegate; import org.jboss.cache.jmx.PlatformMBeanServerRegistration; import java.io.InputStream; /** * Default implementation of the {@link org.jboss.cache.CacheFactory} interface. *

* This is a special instance of a {@link ComponentFactory} which contains bootstrap information for the * {@link org.jboss.cache.factories.ComponentRegistry}. *

* * @author Manik Surtani (manik AT jboss DOT org) * @see org.jboss.cache.factories.ComponentFactory */ public class DefaultCacheFactory extends ComponentFactory implements CacheFactory { private ClassLoader defaultClassLoader; /** * This is here for backward compatibility only. Use new DefaultCacheFactory() instead. * * @deprecated */ @SuppressWarnings("unchecked") @Deprecated @Compat public static CacheFactory getInstance() { return new DefaultCacheFactory(); } public Cache createCache() throws ConfigurationException { return createCache(true); } public Cache createCache(boolean start) throws ConfigurationException { return createCache(new Configuration(), start); } public Cache createCache(String configFileName) throws ConfigurationException { return createCache(configFileName, true); } public Cache createCache(String configFileName, boolean start) throws ConfigurationException { XmlConfigurationParser parser = new XmlConfigurationParser(); Configuration c; try { c = parser.parseFile(configFileName); } catch (ConfigurationException e) { XmlConfigurationParser2x oldParser = new XmlConfigurationParser2x(); c = oldParser.parseFile(configFileName); } return createCache(c, start); } /** * This implementation clones the configuration passed in before using it. * * @param configuration to use * @return a cache * @throws ConfigurationException if there are problems with the cfg */ public Cache createCache(Configuration configuration) throws ConfigurationException { return createCache(configuration, true); } /** * This implementation clones the configuration passed in before using it. * * @param configuration to use * @param start whether to start the cache * @return a cache * @throws ConfigurationException if there are problems with the cfg */ public Cache createCache(Configuration configuration, boolean start) throws ConfigurationException { try { CacheSPI cache = createAndWire(configuration); if (start) cache.start(); return cache; } catch (ConfigurationException ce) { throw ce; } catch (RuntimeException re) { throw re; } catch (Exception e) { throw new RuntimeException(e); } } protected CacheSPI createAndWire(Configuration configuration) throws Exception { CacheSPI spi = new CacheInvocationDelegate(); bootstrap(spi, configuration); return spi; } /** * Bootstraps this factory with a Configuration and a ComponentRegistry. */ private void bootstrap(CacheSPI spi, Configuration configuration) { // injection bootstrap stuff componentRegistry = new ComponentRegistry(configuration, spi); componentRegistry.registerDefaultClassLoader(defaultClassLoader); this.configuration = configuration; componentRegistry.registerComponent(spi, CacheSPI.class); componentRegistry.registerComponent(new PlatformMBeanServerRegistration(), PlatformMBeanServerRegistration.class); } /** * Allows users to specify a default class loader to use for both the construction and running of the cache. * * @param loader class loader to use as a default. */ public void setDefaultClassLoader(ClassLoader loader) { this.defaultClassLoader = loader; } public Cache createCache(InputStream is) throws ConfigurationException { XmlConfigurationParser parser = new XmlConfigurationParser(); Configuration c = null; try { c = parser.parseStream(is); } catch (ConfigurationException e) { XmlConfigurationParser2x oldParser = new XmlConfigurationParser2x(); c = oldParser.parseStream(is); if (c != null && log.isInfoEnabled()) log.info("Detected legacy configuration file format when parsing configuration file. Migrating to the new (3.x) file format is recommended. See FAQs for details."); } return createCache(c); } public Cache createCache(InputStream is, boolean start) throws ConfigurationException { XmlConfigurationParser parser = new XmlConfigurationParser(); Configuration c = parser.parseStream(is); return createCache(c, start); } @Override protected T construct(Class componentType) { throw new UnsupportedOperationException("Should never be invoked - this is a bootstrap factory."); } }