For those who find it useful, here is my final code: In the same package as CacheFactory, a file jcs.default with the default values is expected. (Using the same property names as regularly specified in JCS cache.ccf file)
import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.apache.jcs.JCS; import org.apache.jcs.access.exception.CacheException; import org.apache.jcs.engine.CompositeCacheAttributes; import org.apache.jcs.engine.ElementAttributes; import org.apache.jcs.config.PropertySetter; public class CacheFactory { private static final String DEFAULT_VALUES_SETTINGS_FILE = "jcs.default"; private static Properties defaultProperties = new Properties(); private static String defaultsFile = null; private CacheFactory() { } /** * An auxiliary method to create JCS caches. * This method looks for the file jcs.properties in the same package as the * provided Class. The cache is created by using the region cache and * element attributes specified in the jcs.properties file. * * Note that region names are global. Region names must not contain "/" or "." * If you ask two times for the same region, you will get a different JCS object * but they act over the same region, therefore elements in the cache are accesible * to both. * * The key and elements to be inserted in the cache MUST be Serializable. This is * expected by JCS because it can cache elements to disk or provide them to a remote * agent, even if the cache is just defined as in-memory cache. * * @param clazz - A clazz used to specify the location of the jcs.properties file * @param region - The region name * @return a JCS cache object with access to the specified region */ public static JCS createCache(Class clazz, String region) { if( defaultProperties.isEmpty() ) { String pkg = CacheFactory.class.getPackage().getName(); pkg = pkg.replace(".", "/"); defaultsFile = "/" + pkg + "/" + DEFAULT_VALUES_SETTINGS_FILE; JCS.setConfigFilename( defaultsFile ); try { defaultProperties.load( CacheFactory.class.getResourceAsStream(DEFAULT_VALUES_SETTINGS_FILE )); } catch (IOException ioe) { throw new RuntimeException("Unable to find resource ", ioe ); } } Properties props = new Properties(); CompositeCacheAttributes cca = new CompositeCacheAttributes(); ElementAttributes eca = new ElementAttributes(); try { // First apply the default values from jcs.default PropertySetter.setProperties(cca, defaultProperties, "jcs.default.cacheattributes."); PropertySetter.setProperties(eca, defaultProperties, "jcs.default.elementattributes."); // Now apply the values from a specific jcs.propeties file InputStream is = clazz.getResourceAsStream("jcs.properties"); if( is != null ) { props.load( is ); PropertySetter.setProperties(cca,props, "jcs.region." + region + ".cacheattributes."); PropertySetter.setProperties(eca,props, "jcs.region." + region + ".elementattributes."); } } catch (IOException ioe) { throw new RuntimeException("Unable to read jcs.properties from package " + clazz.getPackage().getName() , ioe); } JCS resp = null; try { resp = JCS.getInstance(region, cca); resp.setDefaultElementAttributes(eca); } catch (CacheException e) { throw new RuntimeException("Unable to set initialize JCS cache for region name [" + region +"]",e); } return resp; } } -----Original Message----- From: Niall Gallagher [mailto:[EMAIL PROTECTED] Sent: Friday, May 02, 2008 7:38 AM To: JCS Users List Subject: RE: Using JCS from many libraries Thanks for sharing that Jorge- might come in useful! On Thu, 2008-05-01 at 18:27 -0400, Jorge Medina wrote: > Never mind. I solved my problem. > > JCS.setDefaultElementAttributes(IElementAttributes) does the trick. > > Now I specify global cache defaults in the properties file jcs.default, that > resides in the same place as my CacheFactory. > > The cache attributes and default element attributes can be specified now in > independent jcs.properties files living in the same package -path- of the > class doing the caching. > > > import org.apache.jcs.JCS; > import org.apache.jcs.access.exception.CacheException; > import org.apache.jcs.engine.CompositeCacheAttributes; > import org.apache.jcs.engine.ElementAttributes; > import org.apache.jcs.config.PropertySetter; > > public class CacheFactory { > > > private CacheFactory() { > > } > > public static JCS createCache(Class clazz, String region) { > String pkg = CacheFactory.class.getPackage().getName(); > pkg = pkg.replace(".", "/"); > String file = "/" + pkg + "/jcs.default"; > > > Properties props = new Properties(); > CompositeCacheAttributes cca = new CompositeCacheAttributes(); > ElementAttributes eca = new ElementAttributes(); > > try { > props.load( clazz.getResourceAsStream("jcs.properties") > ); > PropertySetter.setProperties(cca,props, "jcs.region." + > region + ".cacheattributes."); > PropertySetter.setProperties(eca,props, "jcs.region." + > region + ".elementattributes."); > } catch (IOException ioe) { > throw new RuntimeException("Unable to read > jcs.properties from package " + clazz.getPackage().getName() , ioe); > } > JCS resp = null; > try { > > JCS.setConfigFilename( file ); > resp = JCS.getInstance(region, cca); > resp.setDefaultElementAttributes(eca); > } catch (CacheException e) { > throw new RuntimeException("Unable to set initialize > JCS cache for region name [" + > region +"]",e); > } > return resp; > } > } > > -----Original Message----- > From: Jorge Medina [mailto:[EMAIL PROTECTED] > Sent: Thursday, May 01, 2008 6:04 PM > To: JCS Users List > Subject: RE: Using JCS from many libraries > > > In other words, is there any way to configure the default element attributes > for a region programmatically? > > -Jorge > > -----Original Message----- > From: Jorge Medina [mailto:[EMAIL PROTECTED] > Sent: Thursday, May 01, 2008 5:54 PM > To: jcs-users@jakarta.apache.org > Subject: Using JCS from many libraries > > Hi, > Here is what I have run into: > > A group in the company has created a java library, component1.jar, > which extensively uses JCS and bundles cache.ccf within the jar file. It has > been unit tested and it works fine. > A second group develops another java library, component2.jar, which > also uses JCS and bundles cache.ccf within the jar file. It has been unit > tested and it works fine. > > An application requires to use component1.jar and component2.jar but > now there are two cache.ccf and only one is being loaded. > The regions used by the second component are created with the default > cache and element attributes found in the first cache.ccf. Only the first > call to JCS.setConfigFilename( file ) has effect. > > Extracting cache.ccf from the jars and creating a single file is not a > viable solution if JCS is used in many more components or subsystems. For > example, another application may make use of component2.jar and > component5.jar ...besides, there is no way to guarantee that anyone knows all > places where JCS is used - and loaded from the same class loader . > > What have you do to go around this? Has anybody have had the same > problem? > > I have tried to create a CacheFactory but the JCS class allows to > specify the cache attributes on the getInstance( regionName, ICacheAtributes) > method, but it does not allow to specify the default element attributes for > this region. The default elements attributes are applied but from the first > config file.( jcs.properties file in my code) > > Any help is appreciated. > > -Jorge > > > import java.io.IOException; > import java.util.Properties; > > import org.apache.jcs.JCS; > import org.apache.jcs.access.exception.CacheException; > import org.apache.jcs.engine.CompositeCacheAttributes; > import org.apache.jcs.engine.ElementAttributes; > import org.apache.jcs.config.PropertySetter; > > public class CacheFactory { > > > private CacheFactory() { > > } > > // clazz is used to find the the location of jcs.properties (instead of > /cache.ccf ) for the region. > // Each developer would use a class in their own package. (for > practical purposes, the class caching the objects ) > > public static JCS createCache(Class clazz, String region) { > String pkg = clazz.getPackage().getName(); > pkg = pkg.replace(".", "/"); > String file = "/" + pkg + "/jcs.properties"; > > Properties props = new Properties(); > CompositeCacheAttributes cca = new CompositeCacheAttributes(); > ElementAttributes eca = new ElementAttributes(); > > try { > props.load( clazz.getResourceAsStream("jcs.properties") > ); > PropertySetter.setProperties(cca,props, "jcs.region." + > region + ".cacheattributes."); > PropertySetter.setProperties(eca,props, "jcs.region." + > region + ".elementattributes."); > } catch (IOException e1) { > } > JCS resp = null; > try { > JCS.setConfigFilename( file ); > resp = JCS.getInstance(region, cca); // ← No way to > specify default element attributes for this cache > } catch (CacheException e) { > throw new RuntimeException("Unable to set initialize > JCS cache for region name [" + > region +"]",e); > } > return resp; > } > } > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]