001: /*
002: * $Id: MessageResourcesFactory.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.util;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.struts.config.MessageResourcesConfig;
026:
027: import java.io.Serializable;
028:
029: /**
030: * Factory for <code>MessageResources</code> instances. The general usage
031: * pattern for this class is:
032: *
033: * <ul>
034: *
035: * <li>Call <code>MessageResourcesFactory().createFactory()</code> to retrieve
036: * a <code>MessageResourcesFactory</code> instance.</li> <li>Set properties as
037: * required to configure this factory instance to create
038: * <code>MessageResources</code> instances with desired characteristics.</li>
039: *
040: * <li>Call the <code>createResources()</code> method of the factory to
041: * retrieve a newly instantiated <code>MessageResources</code> instance.</li>
042: *
043: * </ul>
044: *
045: * @version $Rev: 471754 $ $Date: 2005-08-29 23:57:50 -0400 (Mon, 29 Aug 2005)
046: * $
047: */
048: public abstract class MessageResourcesFactory implements Serializable {
049: // ------------------------------------------------------ Static Properties
050:
051: /**
052: * The Java class to be used for <code>MessageResourcesFactory</code>
053: * instances.
054: */
055: protected static transient Class clazz = null;
056:
057: /**
058: * Commons Logging instance.
059: */
060: private static Log LOG = LogFactory
061: .getLog(MessageResourcesFactory.class);
062:
063: /**
064: * The fully qualified class name to be used for <code>MessageResourcesFactory</code>
065: * instances.
066: */
067: protected static String factoryClass = "org.apache.struts.util.PropertyMessageResourcesFactory";
068:
069: // ---------------------------------------------------- Instance Properties
070:
071: /**
072: * Configuration information for Message Resources.
073: */
074: protected MessageResourcesConfig config;
075:
076: /**
077: * The "return null" property value to which newly created
078: * MessageResourcess should be initialized.
079: */
080: protected boolean returnNull = true;
081:
082: /**
083: * Set the configuration information for Message Resources.
084: *
085: * @since Struts 1.2.8
086: */
087: public MessageResourcesConfig getConfig() {
088: return config;
089: }
090:
091: /**
092: * Return the configuration information for Message Resources.
093: *
094: * @since Struts 1.2.8
095: */
096: public void setConfig(MessageResourcesConfig config) {
097: this .config = config;
098: }
099:
100: /**
101: * Get default value of the "returnNull" property used to initialize newly
102: * created MessageResourcess.
103: *
104: * @return default value of the "returnNull" property newly created
105: * MessageResourcess are initialized to.
106: */
107: public boolean getReturnNull() {
108: return (this .returnNull);
109: }
110:
111: /**
112: * Set the default value of the "returnNull" property newly created
113: * MessageResourcess are initialized to.
114: *
115: * @param returnNull default value of the "returnNull" MessageResourcess
116: * are initialized to.
117: */
118: public void setReturnNull(boolean returnNull) {
119: this .returnNull = returnNull;
120: }
121:
122: // --------------------------------------------------------- Public Methods
123:
124: /**
125: * Create and return a newly instansiated <code>MessageResources</code>.
126: * This method must be implemented by concrete subclasses.
127: *
128: * @param config Configuration parameter(s) for the requested bundle
129: */
130: public abstract MessageResources createResources(String config);
131:
132: /**
133: * The fully qualified class name that is used for <code>MessageResourcesFactory</code>
134: * instances.
135: *
136: * @return class name that is used for <code>MessageResourcesFactory</code>
137: * instances
138: */
139: public static String getFactoryClass() {
140: return (MessageResourcesFactory.factoryClass);
141: }
142:
143: /**
144: * Set the fully qualified class name that is used for
145: * <code>MessageResourcesFactory</code> instances.
146: *
147: * @param factoryClass name that is used for <code>MessageResourcesFactory</code>
148: * instances
149: */
150: public static void setFactoryClass(String factoryClass) {
151: MessageResourcesFactory.factoryClass = factoryClass;
152: MessageResourcesFactory.clazz = null;
153: }
154:
155: // --------------------------------------------------------- Static Methods
156:
157: /**
158: * Create and return a <code>MessageResourcesFactory</code> instance of
159: * the appropriate class, which can be used to create customized
160: * <code>MessageResources</code> instances. If no such factory can be
161: * created, return <code>null</code> instead.
162: */
163: public static MessageResourcesFactory createFactory() {
164: // Construct a new instance of the specified factory class
165: try {
166: if (clazz == null) {
167: clazz = RequestUtils.applicationClass(factoryClass);
168: }
169:
170: MessageResourcesFactory factory = (MessageResourcesFactory) clazz
171: .newInstance();
172:
173: return (factory);
174: } catch (Throwable t) {
175: LOG.error("MessageResourcesFactory.createFactory", t);
176:
177: return (null);
178: }
179: }
180: }
|