How to change JCS cache.ccf file's path?

9k Views Asked by At

I'm trying to change path of cache.ccf file about an hour...
When I'm calling JCS.getInstance("myRegion"); I'm getting this error:

Exception in thread "main" java.lang.IllegalStateException: Failed to load properties for name [/cache.ccf]

I tried to put cache.ccf into src folder. In this case everything's OK. But I want it to be in ./config/ directory, not in ./src. I tried to change config file name:

JCS.setConfigFilename("../config/cache.ccf");

But it's not working and I'm getting the same error:

Exception in thread "main" java.lang.IllegalStateException: Failed to load properties for name [../config/cache.ccf]

It seams that JCS tries to find the file named "../config/cache.ccf" in src directory.
Here i found this sentence:
The classpath should include the directory where this file is located or the file should be placed at the root of the classpath, since it is discovered automatically.

But my applilcation don't work even if cache.ccf file is in the root directory of project.
How can I change cache.ccf file's path?

4

There are 4 best solutions below

0
On

You may want to check your classpath. Seems like only src is in the classpath and not config folder. For me I have my *.ccf files placed in config directory which is in my classpath and I need only to specify the path to the ccf file as /client_cache.ccf for JCS to pick it up.

It also depends on your deployment environment. However if you have /config listed in your classpath it should work.

1
On

I've had this problem - and because there's multiple class loaders around in my projects (axis2, tomcat), it can be pretty hard to figure out where to put the cache.ccf file. I ended up not using a .properties file and configuring it directly - here's how I did it...

CompositeCacheManager ccm = CompositeCacheManager.getUnconfiguredInstance();
Properties props = new Properties();

props.put("jcs.default","DC");
props.put("jcs.default.cacheattributes",
          "org.apache.jcs.engine.CompositeCacheAttributes");
// lots more props.put - this is basically the contents of cache.ccf

ccm.configure(props);
JCS sessionCache = JCS.getInstance("bbSessionCache");
0
On

Expanding on Jon's answer above. Custom code to load config from file. Put it before instantiating JCS.

    final String configFilename = System.getProperty("jcs.configurationFile");

    if (configFilename != null)
    {
        try (FileReader f = new FileReader(configFilename))
        {
            Properties props = new Properties();
            props.load(f);
            CompositeCacheManager ccm = CompositeCacheManager.getUnconfiguredInstance();
            ccm.configure(props);       
        }
    }
0
On

I found this relevant information

https://commons.apache.org/proper/commons-jcs/faq.html#configuration-file

CompositeCacheManager ccm = CompositeCacheManager.getUnconfiguredInstance(); 
Properties props = new Properties(); 
props.load(/* load properties from some location defined by your app */); 
ccm.configure(props);