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]


Reply via email to