From hibernate-commits at lists.jboss.org Sun Dec 23 09:55:36 2007 Content-Type: multipart/mixed; boundary="===============5135069280207228267==" MIME-Version: 1.0 From: hibernate-commits at lists.jboss.org To: hibernate-commits at lists.jboss.org Subject: [hibernate-commits] Hibernate SVN: r14262 - core/trunk/cache-jbosscache2/src/main/java/org/hibernate/cache/jbc2/builder. Date: Sun, 23 Dec 2007 09:55:36 -0500 Message-ID: --===============5135069280207228267== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Author: bstansberry(a)jboss.com Date: 2007-12-23 09:55:36 -0500 (Sun, 23 Dec 2007) New Revision: 14262 Removed: core/trunk/cache-jbosscache2/src/main/java/org/hibernate/cache/jbc2/buil= der/JBossCacheFactory.java core/trunk/cache-jbosscache2/src/main/java/org/hibernate/cache/jbc2/buil= der/JBossCacheFactoryImpl.java Modified: core/trunk/cache-jbosscache2/src/main/java/org/hibernate/cache/jbc2/buil= der/JndiMultiplexingCacheInstanceManager.java core/trunk/cache-jbosscache2/src/main/java/org/hibernate/cache/jbc2/buil= der/MultiplexingCacheInstanceManager.java Log: Convert from JBossCacheFactory prototype to the real JBC CacheManager Deleted: core/trunk/cache-jbosscache2/src/main/java/org/hibernate/cache/jbc= 2/builder/JBossCacheFactory.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- core/trunk/cache-jbosscache2/src/main/java/org/hibernate/cache/jbc2/bui= lder/JBossCacheFactory.java 2007-12-23 14:53:06 UTC (rev 14261) +++ core/trunk/cache-jbosscache2/src/main/java/org/hibernate/cache/jbc2/bui= lder/JBossCacheFactory.java 2007-12-23 14:55:36 UTC (rev 14262) @@ -1,81 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2007, Red Hat Middleware LLC or third-party contributors = as - * indicated by the @author tags or express copyright attribution - * statements applied by the authors. =C2=A0All third-party contributions = are - * distributed under license by Red Hat Middleware LLC. - * - * This copyrighted material is made available to anyone wishing to use, m= odify, - * copy, or redistribute it subject to the terms and conditions of the GNU - * Lesser General Public License, as published by the Free Software Founda= tion. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT= ABILITY - * 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 distribution; if not, write to: - * Free Software Foundation, Inc. - * 51 Franklin Street, Fifth Floor - * Boston, MA 02110-1301 USA - */ -package org.hibernate.cache.jbc2.builder; - -import java.util.Set; - -import org.jboss.cache.Cache; - -/** - * Factory and registry for JBoss Cache instances configured using - * named configurations. - * = - * @author Brian Stansberry - * @version $Revision: 1 $ - */ -public interface JBossCacheFactory { - - /** - * Gets all the names of all the configurations of which this object - * is aware. - * = - * @return - */ - Set getConfigurationNames(); - = - /** - * Get a cache configured according to the given configuration name. - *

- * The caller is free to invoke the {@link Cache#create()} and - * {@link Cache#start()} lifecycle methods on the returned cache, but - * the @link Cache#stop()} and {@link Cache#destroy()} methods should = not - * be invoked, since it is quite possible other session factories are - * still using the cache. Use {@link #releaseCache(String)} to notify = this = - * factory that the caller is no longer using a cache; let the factory = - * control stopping and destroying the underlying cache. - *

- * = - * @param configName the name of the configuration - * @param create should the cache be instantiated if it - * hasn't already been? - * @return the cache, or null if = - * create is false and the cache has= n't - * been created previously. - * = - * @throws IllegalArgumentException if this object is unaware of = - * configName - * @throws Exception if there is a problem instantiating the cache - */ - Cache getCache(String configName, boolean create) throws Exception; - = - /** - * Notifies the factory that the caller is no longer using the given - * cache. The factory may perform cleanup operations, such as = - * stopping and destroying the cache. - * = - * @param configName - */ - void releaseCache(String configName); - -} \ No newline at end of file Deleted: core/trunk/cache-jbosscache2/src/main/java/org/hibernate/cache/jbc= 2/builder/JBossCacheFactoryImpl.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- core/trunk/cache-jbosscache2/src/main/java/org/hibernate/cache/jbc2/bui= lder/JBossCacheFactoryImpl.java 2007-12-23 14:53:06 UTC (rev 14261) +++ core/trunk/cache-jbosscache2/src/main/java/org/hibernate/cache/jbc2/bui= lder/JBossCacheFactoryImpl.java 2007-12-23 14:55:36 UTC (rev 14262) @@ -1,368 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2007, Red Hat Middleware LLC or third-party contributors = as - * indicated by the @author tags or express copyright attribution - * statements applied by the authors. =C2=A0All third-party contributions = are - * distributed under license by Red Hat Middleware LLC. - * - * This copyrighted material is made available to anyone wishing to use, m= odify, - * copy, or redistribute it subject to the terms and conditions of the GNU - * Lesser General Public License, as published by the Free Software Founda= tion. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT= ABILITY - * 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 distribution; if not, write to: - * Free Software Foundation, Inc. - * 51 Franklin Street, Fifth Floor - * Boston, MA 02110-1301 USA - */ -package org.hibernate.cache.jbc2.builder; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.InputStream; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; -import java.util.Map.Entry; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; - -import org.hibernate.cache.CacheException; -import org.jboss.cache.Cache; -import org.jboss.cache.CacheStatus; -import org.jboss.cache.DefaultCacheFactory; -import org.jboss.cache.config.Configuration; -import org.jboss.cache.config.ConfigurationException; -import org.jboss.cache.xml.XmlHelper; -import org.jgroups.ChannelFactory; -import org.jgroups.JChannelFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.w3c.dom.DOMImplementation; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.w3c.dom.ls.DOMImplementationLS; -import org.w3c.dom.ls.LSOutput; -import org.w3c.dom.ls.LSSerializer; - -/** - * A JBossCacheConfigurationFactory. This is a basic prototype of a - * JBCACHE-1156 solution; only in Hibernate code base for a very short - * period. - * = - * @author Brian Stansberry - * @version $Revision: 1 $ - */ -public class JBossCacheFactoryImpl implements JBossCacheFactory { - = - private static final Logger log =3D LoggerFactory.getLogger(JBossCache= FactoryImpl.class); - - private static final String DOCUMENT_ROOT =3D "cache-configs"; - private static final String CONFIG_ROOT =3D "cache-config"; - private static final String CONFIG_NAME =3D "name"; - - private static JBossCacheFactoryImpl sharedFactory; - private static String sharedChannelFactoryCfg; - = - private XmlConfigurationParser parser; - private String configResource; - private Map configs =3D new HashMap(); - private Map caches =3D new HashMap(); - private Map checkouts =3D new HashMap(); - private ChannelFactory channelFactory; - private boolean started; - = - public JBossCacheFactoryImpl(String configResource, ChannelFactory fac= tory) { - = - parser =3D new XmlConfigurationParser(); - this.configResource =3D configResource; - this.channelFactory =3D factory; - } - = - public static synchronized JBossCacheFactory getSharedInstance(String = cacheConfigResource, String channelFactoryConfigResource) { - = - if (sharedFactory =3D=3D null) { - ChannelFactory cf =3D new JChannelFactory(); - try { - cf.setMultiplexerConfig(channelFactoryConfigResource); - } - catch (Exception e) { - throw new CacheException("Problem setting ChannelFactory c= onfig", e); - } - sharedFactory =3D new JBossCacheFactoryImpl(cacheConfigResourc= e, cf); - sharedChannelFactoryCfg =3D channelFactoryConfigResource; - } - else { - // Validate that the provided resources match the existing sin= gleton - if (!sharedFactory.getConfigResource().equals(cacheConfigResou= rce)) { - throw new CacheException("Provided cacheConfigResource doe= s " + - "not match the existing shared factory: provided =3D " + = - cacheConfigResource + "; existing =3D " + sharedFactory.= getConfigResource()); - } - else if (!sharedChannelFactoryCfg.equals(channelFactoryConfigR= esource)) { - throw new IllegalStateException("Provided channelFactoryCo= nfigResource does " + - "not match the existing shared factory: provided = =3D " + = - channelFactoryConfigResource + "; existing =3D " += sharedChannelFactoryCfg); - = - } - } - = - return sharedFactory; - } - = - public void start() { - if (!started) { - this.configs =3D parser.parseConfigs(configResource); - started =3D true; - } - } - = - public void stop() { - if (started) { - synchronized (caches) { - for (Iterator it =3D caches.entrySet().iterator(); it.hasN= ext(); ) { - Map.Entry entry =3D (Entry) it.next(); - destroyCache((Cache) entry.getValue()); - it.remove(); - } - caches.clear(); - checkouts.clear(); - configs.clear(); - } - started =3D false; - } - } - = - public String getConfigResource() { - return configResource; - } - - public ChannelFactory getChannelFactory() { - return channelFactory; - } - - public Set getConfigurationNames() - { - return new HashSet(configs.keySet()); - } - = - public Cache getCache(String configName, boolean create) throws Except= ion - { - Cache cache =3D null; - synchronized (caches) { - cache =3D (Cache) caches.get(configName); - if (cache =3D=3D null && create) { - Configuration config =3D getConfiguration(configName); - cache =3D DefaultCacheFactory.getInstance().createCache(co= nfig, false); - registerCache(cache, configName); - } - else if (cache !=3D null) { - incrementCheckout(configName); - } - } - = - return cache; - } - - private int incrementCheckout(String configName) { - synchronized (checkouts) { - Integer count =3D (Integer) checkouts.get(configName); - if (count =3D=3D null) - count =3D new Integer(0); - Integer newVal =3D new Integer(count.intValue() + 1); - checkouts.put(configName, newVal); - return newVal.intValue(); - } - } - - private int decrementCheckout(String configName) { - synchronized (checkouts) { - Integer count =3D (Integer) checkouts.get(configName); - if (count =3D=3D null || count.intValue() < 1) - throw new IllegalStateException("invalid count of " + coun= t + " for " + configName); - - Integer newVal =3D new Integer(count.intValue() - 1); - checkouts.put(configName, newVal); - return newVal.intValue(); - } - } - = - public void registerCache(Cache cache, String configName) { - synchronized (caches) { - if (caches.containsKey(configName)) - throw new IllegalStateException(configName + " already reg= istered"); - caches.put(configName, cache); - incrementCheckout(configName); - } - } - = - public void releaseCache(String configName) { - - synchronized (caches) { - if (!caches.containsKey(configName)) - throw new IllegalStateException(configName + " not registe= red"); - if (decrementCheckout(configName) =3D=3D 0) { - Cache cache =3D (Cache) caches.remove(configName); - destroyCache(cache); - } - } - } - - private void destroyCache(Cache cache) { - if (cache.getCacheStatus() =3D=3D CacheStatus.STARTED) { - cache.stop(); - } - if (cache.getCacheStatus() !=3D CacheStatus.DESTROYED - && cache.getCacheStatus() !=3D CacheStatus.INSTANTIATED) { - cache.destroy(); - } - } - = - public Configuration getConfiguration(String configName) throws Except= ion { - Element element =3D (Element) configs.get(configName); - if (element =3D=3D null) - throw new IllegalArgumentException("unknown config " + configN= ame); - Configuration config =3D parser.parseConfig(element); - if (channelFactory !=3D null && config.getMultiplexerStack() !=3D = null) { - config.getRuntimeConfig().setMuxChannelFactory(channelFactory); - } - return config; - } - = - class XmlConfigurationParser extends org.jboss.cache.factories.XmlConf= igurationParser { - - public Map parseConfigs(String configs) { - InputStream is =3D getAsInputStreamFromClassLoader(configs); - if (is =3D=3D null) - { - if (log.isDebugEnabled()) - log.debug("Unable to find configuration file " + configs= + " in classpath; searching for this file on the filesystem instead."); - try - { - is =3D new FileInputStream(configs); - } - catch (FileNotFoundException e) - { - throw new ConfigurationException("Unable to find config = file " + configs + " either in classpath or on the filesystem!", e); - } - } - - return parseConfigs(is); - } - = - public Map parseConfigs(InputStream stream) { - = - // loop through all elements in XML. - Element root =3D XmlHelper.getDocumentRoot(stream); - NodeList list =3D root.getElementsByTagName(CONFIG_ROOT); - if (list =3D=3D null || list.getLength() =3D=3D 0) = - throw new ConfigurationException("Can't find " + CONFIG_RO= OT + " tag"); - = - Map result =3D new HashMap(); - = - for (int i =3D 0; i < list.getLength(); i++) - { - org.w3c.dom.Node node =3D list.item(i); - if (node.getNodeType() !=3D org.w3c.dom.Node.ELEMENT_NODE) - { - continue; - } - = - Element element =3D (Element) node; - String name =3D element.getAttribute(CONFIG_NAME); - if (name =3D=3D null || name.trim().length() =3D=3D 0) - throw new ConfigurationException("Element " + element += " has no name attribute"); - = - result.put(name.trim(), element); - } - = - return result; - } - = - public Configuration parseConfig(Element config) throws Exception { - - DocumentBuilderFactory docBuilderFactory =3D DocumentBuilderFa= ctory.newInstance(); - DocumentBuilder builder =3D docBuilderFactory.newDocumentBuild= er(); - Document doc =3D builder.newDocument(); - Element root =3D doc.createElement(DOCUMENT_ROOT); - doc.appendChild(root); - Node imported =3D doc.importNode(config, true); - root.appendChild(imported); - = - DOMImplementation domImpl =3D doc.getImplementation(); - - DOMImplementationLS impl =3D = - (DOMImplementationLS)domImpl.getFeature("LS", "3.0"); - - LSSerializer writer =3D impl.createLSSerializer(); - LSOutput output =3D impl.createLSOutput(); - output.setEncoding("UTF-8"); - ByteArrayOutputStream baos =3D new ByteArrayOutputStream(); - output.setByteStream(baos); - writer.write(doc, output); - = - ByteArrayInputStream is =3D new ByteArrayInputStream(baos.toBy= teArray()); = - return parseStream(is); - } = - - - @Override - protected Element getMBeanElement(Element root) - { - // This is following JBoss convention. - NodeList list =3D root.getElementsByTagName(CONFIG_ROOT); - if (list =3D=3D null) throw new ConfigurationException("Can't f= ind " + CONFIG_ROOT + " tag"); - - if (list.getLength() > 1) throw new ConfigurationException("Has= multiple " + CONFIG_ROOT + " tag"); - - Node node =3D list.item(0); - Element element =3D null; - if (node.getNodeType() =3D=3D org.w3c.dom.Node.ELEMENT_NODE) - { - element =3D (Element) node; - } - else - { - throw new ConfigurationException("Can't find " + CONFIG_ROOT= + " element"); - } - return element; - } - = - = - } - = - public static void main(String[] args) - { - try - { - JChannelFactory cf =3D new JChannelFactory(); - cf.setMultiplexerConfig("stacks.xml"); - JBossCacheFactoryImpl factory =3D new JBossCacheFactoryImpl("j= bc2-configs.xml", cf); - for (Iterator iter =3D factory.getConfigurationNames().iterato= r(); iter.hasNext(); ) - { - String name =3D (String) iter.next(); - Cache c =3D factory.getCache(name, true); - c.start(); - System.out.println(name + " =3D=3D " + c); - factory.releaseCache(name); - System.out.println(name + " =3D=3D " + c.getCacheStatus()); - } - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(System.out); - } - } -} Modified: core/trunk/cache-jbosscache2/src/main/java/org/hibernate/cache/jb= c2/builder/JndiMultiplexingCacheInstanceManager.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- core/trunk/cache-jbosscache2/src/main/java/org/hibernate/cache/jbc2/bui= lder/JndiMultiplexingCacheInstanceManager.java 2007-12-23 14:53:06 UTC (rev= 14261) +++ core/trunk/cache-jbosscache2/src/main/java/org/hibernate/cache/jbc2/bui= lder/JndiMultiplexingCacheInstanceManager.java 2007-12-23 14:55:36 UTC (rev= 14262) @@ -33,6 +33,7 @@ import org.hibernate.cfg.Settings; import org.hibernate.util.NamingHelper; import org.hibernate.util.PropertiesHelper; +import org.jboss.cache.CacheManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; = @@ -49,7 +50,7 @@ private static final Logger log =3D LoggerFactory.getLogger(JndiMultip= lexingCacheInstanceManager.class); = /** - * Specifies the JNDI name under which the {@link JBossCacheFactory} t= o use is bound. + * Specifies the JNDI name under which the {@link CacheManager} to use= is bound. * There is no default value -- the user must specify the property. */ public static final String CACHE_FACTORY_RESOURCE_PROP =3D "hibernate.= cache.region.jbc2.cachefactory"; @@ -68,18 +69,18 @@ if (name =3D=3D null) throw new CacheException("Configuration property " + CACHE_FAC= TORY_RESOURCE_PROP + " not set"); = - JBossCacheFactory cf =3D locateCacheFactory( name, NamingHelper.ge= tJndiProperties( properties ) ); + CacheManager cf =3D locateCacheFactory( name, NamingHelper.getJndi= Properties( properties ) ); setCacheFactory( cf ); = = super.start(settings, properties); } = - private JBossCacheFactory locateCacheFactory(String jndiNamespace, Pro= perties jndiProperties) { + private CacheManager locateCacheFactory(String jndiNamespace, Properti= es jndiProperties) { = Context ctx =3D null; try { ctx =3D new InitialContext( jndiProperties ); - return (JBossCacheFactory) ctx.lookup( jndiNamespace ); + return (CacheManager) ctx.lookup( jndiNamespace ); } catch (NamingException ne) { String msg =3D "Unable to retreive Cache from JNDI [" + jndiNa= mespace + "]"; Modified: core/trunk/cache-jbosscache2/src/main/java/org/hibernate/cache/jb= c2/builder/MultiplexingCacheInstanceManager.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- core/trunk/cache-jbosscache2/src/main/java/org/hibernate/cache/jbc2/bui= lder/MultiplexingCacheInstanceManager.java 2007-12-23 14:53:06 UTC (rev 142= 61) +++ core/trunk/cache-jbosscache2/src/main/java/org/hibernate/cache/jbc2/bui= lder/MultiplexingCacheInstanceManager.java 2007-12-23 14:55:36 UTC (rev 142= 62) @@ -27,6 +27,8 @@ import javax.transaction.TransactionManager; = import org.jboss.cache.Cache; +import org.jboss.cache.CacheManager; +import org.jboss.cache.CacheManagerImpl; import org.jboss.cache.CacheStatus; import org.jboss.cache.config.Configuration; import org.jgroups.ChannelFactory; @@ -106,9 +108,7 @@ public static final String DEF_CACHE_FACTORY_RESOURCE =3D "org/hiberna= te/cache/jbc2/builder/jbc2-configs.xml"; = /** * Default value for {@link #CHANNEL_FACTORY_RESOURCE_PROP}. Specifies - * "stacks.xml", which can be found in the root of the JGroups jar fil= e. - * Thus, leaving this value at default means using the default protocol - * stack configs provided by JGroups. + * the "jgroups-stacks.xml" file in this package. */ public static final String DEF_MULTIPLEXER_RESOURCE =3D "org/hibernate= /cache/jbc2/builder/jgroups-stacks.xml"; /** @@ -142,7 +142,7 @@ private String tsConfig =3D null; = /** Our cache factory */ - private JBossCacheFactory jbcFactory; + private CacheManager jbcFactory; /** Our channel factory */ private ChannelFactory channelFactory; /** = @@ -187,7 +187,7 @@ * * @return Value for property 'cacheFactory'. */ - public JBossCacheFactory getCacheFactory() { + public CacheManager getCacheFactory() { return jbcFactory; } = @@ -197,7 +197,7 @@ * * @param factory Value to set for property 'cacheFactory'. */ - public void setCacheFactory(JBossCacheFactory factory) { + public void setCacheFactory(CacheManager factory) { this.jbcFactory =3D factory; } = @@ -288,9 +288,8 @@ } = String factoryRes =3D PropertiesHelper.getString(CACHE_FAC= TORY_RESOURCE_PROP, properties, DEF_CACHE_FACTORY_RESOURCE); - // FIXME use an impl from JBossCache - jbcFactory =3D new JBossCacheFactoryImpl(factoryRes, chann= elFactory); - ((JBossCacheFactoryImpl) jbcFactory).start(); + jbcFactory =3D new CacheManagerImpl(factoryRes, channelFac= tory); + ((CacheManagerImpl) jbcFactory).start(); selfCreatedFactory =3D true; } = @@ -390,7 +389,7 @@ public void stop() { releaseCaches(); if (selfCreatedFactory) { - ((JBossCacheFactoryImpl) jbcFactory).stop(); + ((CacheManagerImpl) jbcFactory).stop(); } } = --===============5135069280207228267==--