You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by cr...@apache.org on 2003/01/07 03:40:53 UTC
cvs commit: jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl WebappPropertyResources.java WebappPropertyResourcesFactory.java WebappXMLResources.java WebappXMLResourcesFactory.java
craigmcc 2003/01/06 18:40:53
Modified: resources build.xml
resources/src/java overview.html
Added: resources/src/java/org/apache/commons/resources
ResourcesFactoryFinder.java
resources/src/java/org/apache/commons/resources/impl
WebappPropertyResources.java
WebappPropertyResourcesFactory.java
WebappXMLResources.java
WebappXMLResourcesFactory.java
Log:
Add ResourcesFactoryFinder, which uses the discovery mechanism described
for the commons-discovery process to locate the appropriate implementation
of o.a.c.r.ResourcesFactory.
Add webapp-aware versions of PropertyResources and XMLResources that let you
use context-relative resource paths to specify the resource files. You must
call setServletContext() on the corresponding factory instance before using
it to create Resources instances.
Revision Changes Path
1.21 +12 -2 jakarta-commons-sandbox/resources/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/resources/build.xml,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- build.xml 7 Jan 2003 01:59:33 -0000 1.20
+++ build.xml 7 Jan 2003 02:40:52 -0000 1.21
@@ -51,6 +51,9 @@
version 3.7 or later -->
<property name="junit.home" value="/usr/local/junit3.7" />
+ <!-- The home directory for the servlet.jar file -->
+ <property name="servlet.home" value="/usr/local/jakarta-tomcat/common/lib"/>
+
<!-- ========== Derived Values ============================================ -->
@@ -71,7 +74,10 @@
<property name="commons-logging.jar" value="${commons-logging.home}/commons-logging.jar" />
<!-- The pathname of the "junit.jar" JAR file -->
- <property name="junit.jar" value="${junit.home}/junit.jar"/>
+ <property name="junit.jar" value="${junit.home}/junit.jar"/>
+
+ <!-- The pathname of the "servlet.jar" JAR file -->
+ <property name="servlet.jar" value="${servlet.home}/servlet.jar"/>
<!-- ========== Component Declarations ==================================== -->
@@ -123,6 +129,7 @@
<!-- Should Java compilations set the 'optimize' compiler option? -->
<property name="compile.optimize" value="true"/>
+
<!-- Construct compile classpath -->
<path id="compile.classpath">
<pathelement location="${build.home}/classes"/>
@@ -131,6 +138,7 @@
<pathelement location="${commons-digester.jar}"/>
<pathelement location="${commons-discovery.jar}"/>
<pathelement location="${commons-logging.jar}"/>
+ <pathelement location="${servlet.jar}"/>
</path>
@@ -147,7 +155,9 @@
<pathelement location="${commons-discovery.jar}"/>
<pathelement location="${commons-logging.jar}"/>
<pathelement location="${junit.jar}"/>
+ <pathelement location="${servlet.jar}"/>
</path>
+
<!-- Should all tests fail if one does? -->
<property name="test.failonerror" value="true"/>
1.2 +19 -3 jakarta-commons-sandbox/resources/src/java/overview.html
Index: overview.html
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/overview.html,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- overview.html 7 Jan 2003 01:59:34 -0000 1.1
+++ overview.html 7 Jan 2003 02:40:52 -0000 1.2
@@ -67,6 +67,16 @@
that share a common base URL, a Locale-based suffix (following the
same rules as for <code>java.util.ResourceBundle</code>), and
a <code>.xml</code> extension.</li>
+<li><a href="org/apache/commons/resources/impl/WebappPropertyResources.html">
+ WebappPropertyResources</a> - Specialized version of
+ <code>PropertyResources</code> where the configuration parameter is the
+ base name that will be passed to
+ <code>ServletContext.getResource()</code> calls.</li>
+<li><a href="org/apache/commons/resources/impl/WebappXMLResources.html">
+ WebappXMLResources</a> - Specialized version of
+ <code>PropertyResources</code> where the configuration parameter is the
+ base name that will be passed to
+ <code>ServletContext.getResource()</code> calls.</li>
</ul>
@@ -78,15 +88,21 @@
as follows:</p>
<ul>
<li><strong>commons-beanutils.jar</strong> (Version 1.5 or later) -
- Only required if you are using <code>XMLResources</code>.</li>
+ Only required if you are using <code>XMLResources</code>
+ or <code>WebappXMLResources</code>.</li>
<li><strong>commons-collections.jar</strong> (Version 2.1 or later) -
- Only required if you are using <code>XMLResources</code>.</li>
+ Only required if you are using <code>XMLResources</code>
+ or <code>WebappXMLResources</code>.</li>
<li><strong>commons-digester.jar</strong> (Version 1.3 or later) -
- Only required if you are using <code>XMLResources</code>.</li>
+ Only required if you are using <code>XMLResources</code>
+ or <code>WebappXMLResources</code>.</li>
<li><strong>commons-discovery.jar</strong> (Version 1.0 or later) -
Only required if you are using <code>ResourcesFactoryFinder</code>.</li>
<li><strong>commons-logging.jar</strong> (Version 1.0.2 or later) -
Required by all standard <code>Resources</code> implementations.</li>
+<li><strong>servlet.jar</strong> (Version 2.2 or later) -
+ Only requird if you are using <code>WebappPropertyResources</code>
+ or <code>WebappXMLResources</code>.</li>
<li><strong>XML Parser</strong> (Version JAXP/1.1 or later) -
Only required if you are using <code>XMLResources</code>.</li>
</ul>
1.1 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ResourcesFactoryFinder.java
Index: ResourcesFactoryFinder.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ResourcesFactoryFinder.java,v 1.1 2003/01/07 02:40:52 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2003/01/07 02:40:52 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.resources;
import org.apache.commons.discovery.tools.DiscoverClass;
/**
* <p>Convenience factory class for instantiating an implementation of
* {@link ResourcesFactory} based on the discovery features of the
* <code>commons-discovery</code> package.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/01/07 02:40:52 $
*/
public class ResourcesFactoryFinder {
/**
* <p>Create and return a {@link ResourcesFactory} instance based on
* dynamic discovery of the desired implementation class. No
* default implementation is specified.</p>
*
* @exception Exception if thrown during service discovery
*/
public static ResourcesFactory getResourcesFactory() throws Exception {
Class spiInterface = ResourcesFactory.class;
Class spiClass =
new DiscoverClass().find(spiInterface);
ResourcesFactory spiImpl = (ResourcesFactory) spiClass.newInstance();
return (spiImpl);
}
}
1.1 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/WebappPropertyResources.java
Index: WebappPropertyResources.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/WebappPropertyResources.java,v 1.1 2003/01/07 02:40:52 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2003/01/07 02:40:52 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.resources.impl;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.resources.Resources;
/**
* <p>Concrete implementation of {@link Resources} that wraps a family
* (one per <code>Locale</code> of properties files that share a base
* context-relative path for servlet context resources, and have
* name suffixes reflecting the <code>Locale</code> for which
* the document's messages apply. Resources are looked up in a hierarchy
* of properties files in a manner identical to that performed by
* <code>java.util.ResourceBundle.getBundle().</code>.</p>
*
* <p>The base resource path passed to our constructor must contain the
* context-relative base name of the properties file family.
* For example, if the base path is passed as
* <code>http://localhost/foo/Bar</code>, the resources for the
* <code>en_US</code> Locale would be stored under URL
* <code>http://localhost/foo/Bar_en_US.properties</code>, and the default
* resources would be stored in
* <code>http://localhost/foo/Bar.properties</code>.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/01/07 02:40:52 $
*/
public class WebappPropertyResources extends CollectionResourcesBase {
// ----------------------------------------------------------- Constructors
/**
* <p>Create a new {@link Resources} instance with the specified
* logical name and base resource URL.</p>
*
* @param name Logical name of the new instance
* @param base Base URL of the family of properties files that contain
* the resource keys and values
* @param servletContext the <code>ServletContext</code> instance
* to use for resolving resource references
*/
public WebappPropertyResources(String name, String base,
ServletContext servletContext) {
super(name, base);
this.servletContext = servletContext;
}
// ----------------------------------------------------- Instance Variables
/**
* <p>The <code>Log</code> instance for this class.</p>
*/
protected static final Log log =
LogFactory.getLog(PropertyResources.class);
/**
* <p>The <code>ServletContext</code> instance for resolving
* our resources references.</p>
*/
private ServletContext servletContext = null;
// ------------------------------------------------------ Protected Methods
/**
* <p>Return a <code>Map</code> containing the name-value mappings for
* the specified base URL and requested <code>Locale</code>, if there
* are any. If there are no defined mappings for the specified
* <code>Locale</code>, return an empty <code>Map</code> instead.</p>
*
* <p>Concrete subclasses must override this method to perform the
* appropriate lookup. A typical implementation will construct an
* absolute URL based on the specified base URL and <code>Locale</code>,
* retrieve the specified resource file (if any), and parse it into
* a <code>Map</code> structure.</p>
*
* <p>Caching of previously retrieved <code>Map</code>s (if any) should
* be performed by callers of this method. Therefore, this method should
* always attempt to retrieve the specified resource and load it
* appropriately.</p>
*
* @param base Base URL of the resource files for this {@link Resources}
* instance
* @param locale <code>Locale</code> for which name-value mappings
* are requested
*/
protected Map getLocaleMap(String base, Locale locale) {
if (log.isDebugEnabled()) {
log.debug("Loading locale '" + locale + "' resources from base '" +
base + "'");
}
Properties props = new Properties();
String name = base + getLocaleSuffix(locale) + ".properties";
InputStream stream = null;
try {
// Open an input stream to the URL for this locale (if any)
if (log.isTraceEnabled()) {
log.trace("Complete path is '" + name + "'");
}
stream = servletContext.getResourceAsStream(name);
// Parse the input stream and populate the name-value mappings map
if (stream != null) {
if (log.isTraceEnabled()) {
log.trace("Parsing input resource");
}
props.load(stream);
}
} catch (FileNotFoundException e) {
// Log and swallow this exception
if (log.isDebugEnabled()) {
log.debug("No resources for locale '" + locale +
"' from base '" + base + "'");
}
props.clear();
} catch (IOException e) {
log.warn("IOException loading locale '" + locale +
"' from base '" + base + "'", e);
props.clear();
} finally {
// Close the input stream that was opened earlier
if (stream != null) {
try {
stream.close();
} catch (Exception e) {
;
}
stream = null;
}
}
// Return the populated (or empty) properties
return (props);
}
}
1.1 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/WebappPropertyResourcesFactory.java
Index: WebappPropertyResourcesFactory.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/WebappPropertyResourcesFactory.java,v 1.1 2003/01/07 02:40:52 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2003/01/07 02:40:52 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.resources.impl;
import javax.servlet.ServletContext;
import org.apache.commons.resources.Resources;
import org.apache.commons.resources.ResourcesException;
import org.apache.commons.resources.ResourcesFactory;
/**
* <p>Concrete implementation of {@link ResourcesFactory} that creates
* {@link Resources} instances that wrap a family (one per Locale) of
* property files that share a base context-relative path for
* servlet context resources, and have name suffixes reflecting
* the Locale for which the file's messages apply. Resources are
* looked up in a hierarchy of files in a manner identical to that
* performed by <code>java.util.ResourceBundle.getBundle()</code>.</p>
*
* <p>The configuration variable passed to the <code>createResources()</code>
* method must be the context-relative base name of the properties file family,
* and must begin with a slash ('/') character.
* For example, if the configuration URL is passed as
* <code>/WEB-INF/resources/MyResources</code>, the resources for the
* <code>en_US</code> Locale would be stored under context resource
* <code>/WEB-INF/resources/MyReesources_en_US.properties</code>, and the
* default resources would be stored in
* <code>/WEB-INF/resources/MyResources.properties</code>.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/01/07 02:40:52 $
*/
public class WebappPropertyResourcesFactory extends ResourcesFactoryBase {
// --------------------------------------------------------- Public Methods
// ------------------------------------------------------------- Properties
/**
* <p>The <code>ServletContext</code> instance for resolving
* our resources references.</p>
*/
private ServletContext servletContext = null;
/**
* <p>Return the <code>ServletContext</code> instance for
* resolving our resources references.</p>
*/
public ServletContext getServletContext() {
return (this.servletContext);
}
/**
* <p>Set the <code>ServletContext</code> instance for
* resolving our resources references.</p>
*/
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
// ------------------------------------------------------ Protected Methods
/**
* <p>Create and return a new {@link Resources} instance with the
* specified logical name, after calling its <code>init()</code>
* method and delegating the relevant properties.</p>
*
* @param name Logical name of the {@link Resources} instance to create
* @param config Configuration string for this resource (if any)
*
* @exception ResourcesException if a {@link Resources} instance
* of the specified logical name cannot be created.
*/
protected Resources createResources(String name, String config)
throws ResourcesException {
Resources resources =
new WebappPropertyResources(name, config, servletContext);
resources.setReturnNull(isReturnNull());
resources.init();
return (resources);
}
}
1.1 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/WebappXMLResources.java
Index: WebappXMLResources.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/WebappXMLResources.java,v 1.1 2003/01/07 02:40:52 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2003/01/07 02:40:52 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.resources.impl;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.digester.Digester;
import org.apache.commons.resources.Resources;
import org.xml.sax.SAXException;
/**
* <p>Concrete implementation of {@link Resources} that wraps a family
* (one per <code>Locale</code> of XML documents that share a base
* context-relative path for servlet context resources, and have
* name suffixes reflecting the <code>Locale</code> for which
* the document's messages apply. Resources are looked up in a hierarchy
* of properties files in a manner identical to that performed by
* <code>java.util.ResourceBundle.getBundle().</code>.</p>
*
* <p>The base resource path passed to our constructor must contain the
* context-relative base name of the properties file family.
* For example, if the base path is passed as
* <code>http://localhost/foo/Bar</code>, the resources for the
* <code>en_US</code> Locale would be stored under URL
* <code>http://localhost/foo/Bar_en_US.xml</code>, and the default
* resources would be stored in
* <code>http://localhost/foo/Bar.xml</code>.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/01/07 02:40:52 $
*/
public class WebappXMLResources extends CollectionResourcesBase {
// ----------------------------------------------------------- Constructors
/**
* <p>Create a new {@link Resources} instance with the specified
* logical name and base resource URL.</p>
*
* @param name Logical name of the new instance
* @param base Base URL of the family of properties files that contain
* the resource keys and values
* @param servletContext the <code>ServletContext</code> instance
* to use for resolving resource references
*/
public WebappXMLResources(String name, String base,
ServletContext servletContext) {
super(name, base);
this.servletContext = servletContext;
}
// ----------------------------------------------------- Instance Variables
/**
* <p>The <code>Log</code> instance for this class.</p>
*/
protected static final Log log =
LogFactory.getLog(XMLResources.class);
/**
* <p>The <code>ServletContext</code> instance for resolving
* our resources references.</p>
*/
private ServletContext servletContext = null;
// ------------------------------------------------------ Protected Methods
/**
* <p>Return a <code>Map</code> containing the name-value mappings for
* the specified base URL and requested <code>Locale</code>, if there
* are any. If there are no defined mappings for the specified
* <code>Locale</code>, return an empty <code>Map</code> instead.</p>
*
* <p>Concrete subclasses must override this method to perform the
* appropriate lookup. A typical implementation will construct an
* absolute URL based on the specified base URL and <code>Locale</code>,
* retrieve the specified resource file (if any), and parse it into
* a <code>Map</code> structure.</p>
*
* <p>Caching of previously retrieved <code>Map</code>s (if any) should
* be performed by callers of this method. Therefore, this method should
* always attempt to retrieve the specified resource and load it
* appropriately.</p>
*
* @param base Base URL of the resource files for this {@link Resources}
* instance
* @param locale <code>Locale</code> for which name-value mappings
* are requested
*/
protected Map getLocaleMap(String base, Locale locale) {
if (log.isDebugEnabled()) {
log.debug("Loading locale '" + locale + "' resources from base '" +
base + "'");
}
Map map = new HashMap();
String name = base + getLocaleSuffix(locale) + ".xml";
InputStream stream = null;
try {
// Open an input stream to the URL for this locale (if any)
if (log.isTraceEnabled()) {
log.trace("Complete path is '" + name + "'");
}
stream = servletContext.getResourceAsStream(name);
// Create and configure a new Digester instance
if (stream != null) {
if (log.isTraceEnabled()) {
log.trace("Creating Digester instance");
}
Digester digester = new Digester();
digester.setNamespaceAware(false);
digester.setValidating(false);
digester.push(map);
digester.addCallMethod("resources/resource", "put", 2,
new String[] { "java.lang.Object",
"java.lang.Object" });
digester.addCallParam("resources/resource", 0, "id");
digester.addCallParam("resources/resource", 1);
// Parse the input stream and populate the name-value mappings
if (log.isTraceEnabled()) {
log.trace("Parsing input resource");
}
digester.parse(stream);
}
} catch (FileNotFoundException e) {
// Log and swallow this exception
if (log.isDebugEnabled()) {
log.debug("No resources for locale '" + locale +
"' from base '" + base + "'");
}
map.clear();
} catch (IOException e) {
// Log and swallow this exception
log.warn("IOException loading locale '" + locale +
"' from base '" + base + "'", e);
map.clear();
} catch (SAXException e) {
// Log and swallow this exception
log.warn("SAXException loading locale '" + locale +
"' from base '" + base + "'", e);
map.clear();
} finally {
// Close the input stream that was opened earlier
if (stream != null) {
try {
stream.close();
} catch (Exception e) {
;
}
stream = null;
}
}
// Return the populated (or empty) map
return (map);
}
}
1.1 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/WebappXMLResourcesFactory.java
Index: WebappXMLResourcesFactory.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/WebappXMLResourcesFactory.java,v 1.1 2003/01/07 02:40:52 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2003/01/07 02:40:52 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.resources.impl;
import javax.servlet.ServletContext;
import org.apache.commons.resources.Resources;
import org.apache.commons.resources.ResourcesException;
import org.apache.commons.resources.ResourcesFactory;
/**
* <p>Concrete implementation of {@link ResourcesFactory} that creates
* {@link Resources} instances that wrap a family (one per Locale) of
* XML documents that share a base context-relative path for
* servlet context resources, and have name suffixes reflecting
* the Locale for which the document's messages apply. Resources are
* looked up in a hierarchy of documents in a manner identical to that
* performed by <code>java.util.ResourceBundle.getBundle()</code>.</p>
*
* <p>The configuration variable passed to the <code>createResources()</code>
* method must be the context-relative base name of the XML document family,
* and must begin with a slash ('/') character.
* For example, if the configuration URL is passed as
* <code>/WEB-INF/resources/MyResources</code>, the resources for the
* <code>en_US</code> Locale would be stored under context resource
* <code>/WEB-INF/resources/MyReesources_en_US.xml</code>, and the default
* resources would be stored in
* <code>/WEB-INF/resources/MyResources.xml</code>.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/01/07 02:40:52 $
*/
public class WebappXMLResourcesFactory extends ResourcesFactoryBase {
// --------------------------------------------------------- Public Methods
// ------------------------------------------------------------- Properties
/**
* <p>The <code>ServletContext</code> instance for resolving
* our resources references.</p>
*/
private ServletContext servletContext = null;
/**
* <p>Return the <code>ServletContext</code> instance for
* resolving our resources references.</p>
*/
public ServletContext getServletContext() {
return (this.servletContext);
}
/**
* <p>Set the <code>ServletContext</code> instance for
* resolving our resources references.</p>
*/
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
// ------------------------------------------------------ Protected Methods
/**
* <p>Create and return a new {@link Resources} instance with the
* specified logical name, after calling its <code>init()</code>
* method and delegating the relevant properties.</p>
*
* @param name Logical name of the {@link Resources} instance to create
* @param config Configuration string for this resource (if any)
*
* @exception ResourcesException if a {@link Resources} instance
* of the specified logical name cannot be created.
*/
protected Resources createResources(String name, String config)
throws ResourcesException {
Resources resources =
new WebappXMLResources(name, config, servletContext);
resources.setReturnNull(isReturnNull());
resources.init();
return (resources);
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>