mschachter 01/06/20 14:16:20
Modified: resources/src/java/org/apache/commons/resources
AbstractResource.java FileResource.java
Resource.java
Added: resources/src/java/org/apache/commons/resources
ConfigurationReader.java LocalStrings.properties
ResourceException.java ResourceFactory.java
ResourceManager.java
Log:
- Add ResourceManager, ResourceFactory, ConfigurationReader, ResourceExcpetion,
and LocalStrings
- Internationlize exceptions using MessageResources
Revision Changes Path
1.2 +3 -3
jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/AbstractResource.java
Index: AbstractResource.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/AbstractResource.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- AbstractResource.java 2001/06/18 22:50:36 1.1
+++ AbstractResource.java 2001/06/20 21:16:17 1.2
@@ -82,7 +82,7 @@
* of the String is implementation dependent.
*/
public String getString(String key, Locale locale, TimeZone timeZone)
- throws MissingResourceException {
+ throws ResourceException {
return new String(getData(key, locale, timeZone));
}
@@ -99,7 +99,7 @@
* the time zone argument all together.
*/
public InputStream getStream(String key, Locale locale, TimeZone timeZone)
- throws MissingResourceException {
+ throws ResourceException {
return new ByteArrayInputStream(getData(key, locale, timeZone));
}
@@ -115,7 +115,7 @@
* the time zone argument all together.
*/
public Reader getReader(String key, Locale locale, TimeZone timeZone)
- throws MissingResourceException {
+ throws ResourceException {
return new StringReader(getString(key, locale, timeZone));
}
1.2 +22 -14
jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/FileResource.java
Index: FileResource.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/FileResource.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FileResource.java 2001/06/18 23:54:20 1.1
+++ FileResource.java 2001/06/20 21:16:17 1.2
@@ -24,6 +24,13 @@
public static String ID_SEPARATOR = "_";
/**
+ * The message resources for this class
+ */
+ protected static MessageResources messageResources =
+ MessageResourcesFactory.createFactory().createResources(
+ "org.apache.commons.resources.LocalStrings");
+
+ /**
* The logical name of this resource
*/
protected String name;
@@ -72,7 +79,7 @@
* the default time zone
*/
public byte[] getData(String key, Locale locale, TimeZone timeZone)
- throws MissingResourceException {
+ throws ResourceException {
byte[] data = null;
File file = null;
@@ -87,11 +94,10 @@
data = baos.toByteArray();
}
catch (IOException ioe) {
- throw new MissingResourceException("IOException while reading " +
- "file '" + file.getAbsolutePath() + "': " + ioe.getMessage(),
- baseDir, key);
- }
-
+ throw new ResourceException(
+ messageResources.getMessage("resource.file.ioexception",
+ file.getAbsolutePath()), ioe);
+ }
return data;
}
@@ -106,7 +112,7 @@
* the default time zone
*/
public InputStream getStream(String key, Locale locale, TimeZone timeZone)
- throws MissingResourceException {
+ throws ResourceException {
InputStream stream = null;
@@ -116,18 +122,20 @@
stream = new FileInputStream(file);
}
catch (FileNotFoundException fnfe) {
- throw new MissingResourceException("File '" +
file.getAbsolutePath() +
- "' not found" , baseDir, key);
+ throw new ResourceException(
+ messageResources.getMessage("resource.file.filenotfound",
+ file.getAbsolutePath()), fnfe);
}
catch (IOException ioe) {
- throw new MissingResourceException("IOException while reading " +
- "file '" + file.getAbsolutePath() + "': " + ioe.getMessage(),
- baseDir, key);
+ throw new ResourceException(
+ messageResources.getMessage("resource.file.ioexception",
+ file.getAbsolutePath()), ioe);
}
}
else {
- throw new MissingResourceException("File '" + file.getAbsolutePath() +
- "' cannot be found", baseDir, key);
+ throw new ResourceException(
+ messageResources.getMessage("resource.file.filenotfound",
+ file.getAbsolutePath()));
}
return stream;
}
1.2 +4 -4
jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/Resource.java
Index: Resource.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/Resource.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Resource.java 2001/06/18 22:50:37 1.1
+++ Resource.java 2001/06/20 21:16:18 1.2
@@ -58,7 +58,7 @@
* the time zone argument all together.
*/
public byte[] getData(String key, Locale locale, TimeZone timeZone)
- throws MissingResourceException;
+ throws ResourceException;
/**
* Retrieves content based on the locale and time zone specified. Note
@@ -74,7 +74,7 @@
* of the String is implementation dependent.
*/
public String getString(String key, Locale locale, TimeZone timeZone)
- throws MissingResourceException;
+ throws ResourceException;
/**
* Retrieves an InputStream representing the content based on the key, locale,
@@ -87,7 +87,7 @@
* the time zone argument all together.
*/
public InputStream getStream(String key, Locale locale, TimeZone timeZone)
- throws MissingResourceException;
+ throws ResourceException;
/**
* Retrieves a Reader representing the content based on the key, locale,
@@ -100,7 +100,7 @@
* the time zone argument all together.
*/
public Reader getReader(String key, Locale locale, TimeZone timeZone)
- throws MissingResourceException;
+ throws ResourceException;
/**
* This method is guaranteed to be called before the init() method for this
1.1
jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ConfigurationReader.java
Index: ConfigurationReader.java
===================================================================
package org.apache.commons.resources;
import java.io.InputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Collection;
/**
* This class is responsible for reading configuration data for the resouces
* and exposing methods that the ResourceManager uses to get data from
* resources.
*/
public class ConfigurationReader {
/**
* The Map that represents the resources, where the key is the name of the
* resource and the value is the uninitialized Resource object
*/
protected Map resources;
/**
* Retrieve a Map of the resources where the key is the name of the resource
* and the value is the uninitialized Resource object
*/
public Map getResourceMap() {
return resources;
}
/**
* Retrieve a Collection of the resources for this configuration
*/
public Collection getResources() {
return resources.values();
}
/**
* Read the URL "config" and populate the internal resource Map with data
*/
public void read(InputStream inputStream) throws IOException {
;
}
}
1.1
jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/LocalStrings.properties
Index: LocalStrings.properties
===================================================================
resource.file.ioexception=[FileResource] problem opening file {0}
resource.file.filenotfound=[FileResource] file {0} not found
resource.manager.ioexception=[ResourceManager] IOException
1.1
jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ResourceException.java
Index: ResourceException.java
===================================================================
package org.apache.commons.resources;
/**
* This class is a general purpose wrapper exception for problems
* pertaining to Resources
*/
public class ResourceException extends Exception {
protected String message = "";
protected Exception rootCause = null;
public ResourceException(String message) {
this(message, null);
}
public ResourceException(String message, Exception rootCause) {
this.message = message;
this.rootCause = rootCause;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Exception getRootCause() {
return rootCause;
}
}
1.1
jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ResourceFactory.java
Index: ResourceFactory.java
===================================================================
package org.apache.commons.resources;
import java.io.Serializable;
/**
* This class represents a basic factory pattern class for creating resources.
* When creating a resource, you must also create a Factory class to go with it.
* The factory class is what's defined in resources-config.xml, not the resource
* itself.<br />
* When developing an sub-class of a ResourceFactory, keep in mind that in
* all likelyhood it will be instantiated using Class.forName(), so all
* initialization should go in a no-argument constructor.
*/
public abstract class ResourceFactory implements Serializable {
/**
* Basic factory method, subclasses will create a resource based on
* the value of config
* @param config A String representing how the resource will configure
* itself, whether it be a file path, a URL, or a long
* name-value pair String
*/
public abstract Resource createResource(String config);
}
1.1
jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ResourceManager.java
Index: ResourceManager.java
===================================================================
package org.apache.commons.resources;
import java.net.URL;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Collection;
import java.io.InputStream;
import java.io.IOException;
/**
* This class knows how to load resources specified in an xml file
* and maintain them.
*/
public class ResourceManager {
/**
* A map of resources, the key being the name of the resource
* and the value being the resource itself
*/
protected Map resources;
/**
* The message resources for this class
*/
protected static MessageResources messageResources =
MessageResourcesFactory.createFactory().createResources(
"org.apache.commons.resources.LocalStrings");
/**
* The constructor takes the InputStream, reads the data
* and populates it's internal list of resources
* @param configReader The ConfigurationReader implementation
* to use for the resource information
*/
public ResourceManager(URL configURL) throws ResourceException {
ConfigurationReader configReader = new ConfigurationReader();
InputStream inputStream = null;
try {
inputStream = configURL.openStream();
configReader.read(inputStream);
}
catch (IOException ioe) {
throw new ResourceException(
messageResources.getMessage("resource.manager.ioexception", ioe));
}
resources = configReader.getResourceMap();
initResources();
}
/**
* Retrieves the specfied resource, or <code>null</code> if the resource
* cannot be found
*/
public Resource getResource(String name) {
return (Resource) resources.get(name);
}
/**
* Retrieves all the resources in this manager
*/
public Collection getResources() {
return resources.values();
}
protected void initResources() throws ResourceException {
Collection resourceCollection = resources.values();
Iterator iterator = resourceCollection.iterator();
while (iterator.hasNext()) {
Resource resource = (Resource) iterator.next();
resource.init();
}
}
}