Here is patch that will load a config file from the file system. It
follows the same pattern as Torque and Velocity for initialization:
try
{
JCS.init(filename);
}
catch(IOException e)
{
// Do something.
}
Byron
Index: src/java/org/apache/jcs/JCS.java
===================================================================
RCS file: /home/cvspublic/jakarta-turbine-jcs/src/java/org/apache/jcs/JCS.java,v
retrieving revision 1.7
diff -u -r1.7 JCS.java
--- src/java/org/apache/jcs/JCS.java 22 Aug 2003 11:57:17 -0000 1.7
+++ src/java/org/apache/jcs/JCS.java 5 Sep 2003 01:48:26 -0000
@@ -54,6 +54,9 @@
* <http://www.apache.org/>.
*/
+import java.io.File;
+import java.io.IOException;
+
import org.apache.jcs.access.GroupCacheAccess;
import org.apache.jcs.access.exception.CacheException;
import org.apache.jcs.engine.behavior.ICompositeCacheAttributes;
@@ -74,8 +77,12 @@
*/
public class JCS extends GroupCacheAccess
{
+ // Resource path of config file
private static String configFilename = null;
+ // File system path of config file
+ private static File configFilePath = null;
+
private static CompositeCacheManager cacheMgr;
/**
@@ -130,25 +137,46 @@
{
if ( cacheMgr == null )
{
- if ( configFilename == null )
+ if (configFilename != null)
{
- cacheMgr = CompositeCacheManager.getInstance();
+ cacheMgr = CompositeCacheManager.getUnconfiguredInstance();
+ cacheMgr.configure( configFilename );
}
- else
+ else if (configFilePath != null)
{
cacheMgr = CompositeCacheManager.getUnconfiguredInstance();
-
- cacheMgr.configure( configFilename );
+ cacheMgr.configure(configFilePath);
+ }
+ else
+ {
+ cacheMgr = CompositeCacheManager.getInstance();
}
}
}
/**
- * Set the filename that the cache manager will be initialized with. Only
- * matters before the instance is initialized.
+ * Set the filename resource path that the cache manager will be
+ * initialized with. Only matters before the instance is
+ * initialized.
*/
public static void setConfigFilename( String configFilename )
{
JCS.configFilename = configFilename;
}
+
+ /**
+ * Sets the file path within the file system to a configuration
+ * file to load cache properties from.
+ */
+ public static void init(String filename)
+ throws IOException
+ {
+ File file = new File(filename);
+ if (!file.canRead())
+ {
+ throw new IOException("File '" + filename + "' cannot be read");
+ }
+ JCS.configFilePath = file;
+ }
+
}
Index: src/java/org/apache/jcs/engine/control/CompositeCacheManager.java
===================================================================
RCS file:
/home/cvspublic/jakarta-turbine-jcs/src/java/org/apache/jcs/engine/control/CompositeCacheManager.java,v
retrieving revision 1.7
diff -u -r1.7 CompositeCacheManager.java
--- src/java/org/apache/jcs/engine/control/CompositeCacheManager.java 22 Aug
2003 11:57:18 -0000 1.7
+++ src/java/org/apache/jcs/engine/control/CompositeCacheManager.java 5 Sep
2003 01:48:27 -0000
@@ -56,6 +56,8 @@
import java.io.IOException;
import java.io.InputStream;
+import java.io.FileInputStream;
+import java.io.File;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.Hashtable;
@@ -227,6 +229,35 @@
configure( props );
}
+ /**
+ * Load config properties from the file system, and not from the
+ * classpath.
+ * @param configFile Path to file on file system.
+ */
+ public void configure(File configFile)
+ {
+ Properties props = new Properties();
+ InputStream is = null;
+ try
+ {
+ is = new FileInputStream(configFile);
+ props.load(is);
+ is.close();
+ }
+ catch ( IOException ex )
+ {
+ // We have already checked to make sure that the configFile
+ // exists and is readable, so if something goes wrong here
+ // it is a big problem. However to be consistent with
+ // configure(String) we ignore it.
+ log.error( "Failed to load properties", ex );
+ throw new IllegalStateException( ex.getMessage() );
+ }
+
+ configure( props );
+ }
+
+
/**
* Configure from properties object
*
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-jcs-dev-unsubscribe@xxxxxxxxxxxxxxxxxx
For additional commands, e-mail: turbine-jcs-dev-help@xxxxxxxxxxxxxxxxxx
|