View Javadoc

1   /*
2    * $Id: MessageResourcesFactory.java 421119 2006-07-12 04:49:11Z wsmoak $
3    *
4    * Copyright 1999-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts.util;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.struts.config.MessageResourcesConfig;
23  
24  import java.io.Serializable;
25  
26  /***
27   * Factory for <code>MessageResources</code> instances.  The general usage
28   * pattern for this class is:
29   *
30   * <ul>
31   *
32   * <li>Call <code>MessageResourcesFactory().createFactory()</code> to retrieve
33   * a <code>MessageResourcesFactory</code> instance.</li> <li>Set properties as
34   * required to configure this factory instance to create
35   * <code>MessageResources</code> instances with desired characteristics.</li>
36   *
37   * <li>Call the <code>createResources()</code> method of the factory to
38   * retrieve a newly instantiated <code>MessageResources</code> instance.</li>
39   *
40   * </ul>
41   *
42   * @version $Rev: 421119 $ $Date: 2005-08-29 23:57:50 -0400 (Mon, 29 Aug 2005)
43   *          $
44   */
45  public abstract class MessageResourcesFactory implements Serializable {
46      // ------------------------------------------------------ Static Properties
47  
48      /***
49       * The Java class to be used for <code>MessageResourcesFactory</code>
50       * instances.
51       */
52      protected static transient Class clazz = null;
53  
54      /***
55       * Commons Logging instance.
56       */
57      private static Log LOG = LogFactory.getLog(MessageResourcesFactory.class);
58  
59      /***
60       * The fully qualified class name to be used for <code>MessageResourcesFactory</code>
61       * instances.
62       */
63      protected static String factoryClass =
64          "org.apache.struts.util.PropertyMessageResourcesFactory";
65  
66      // ---------------------------------------------------- Instance Properties
67  
68      /***
69       * Configuration information for Message Resources.
70       */
71      protected MessageResourcesConfig config;
72  
73      /***
74       * The "return null" property value to which newly created
75       * MessageResourcess should be initialized.
76       */
77      protected boolean returnNull = true;
78  
79      /***
80       * Set the configuration information for Message Resources.
81       *
82       * @since Struts 1.2.8
83       */
84      public MessageResourcesConfig getConfig() {
85          return config;
86      }
87  
88      /***
89       * Return the configuration information for Message Resources.
90       *
91       * @since Struts 1.2.8
92       */
93      public void setConfig(MessageResourcesConfig config) {
94          this.config = config;
95      }
96  
97      /***
98       * Get default value of the "returnNull" property used to initialize newly
99       * created MessageResourcess.
100      *
101      * @return default value of the "returnNull" property newly created
102      *         MessageResourcess are initialized to.
103      */
104     public boolean getReturnNull() {
105         return (this.returnNull);
106     }
107 
108     /***
109      * Set the default value of the "returnNull" property newly created
110      * MessageResourcess are initialized to.
111      *
112      * @param returnNull default value of the "returnNull" MessageResourcess
113      *                   are initialized to.
114      */
115     public void setReturnNull(boolean returnNull) {
116         this.returnNull = returnNull;
117     }
118 
119     // --------------------------------------------------------- Public Methods
120 
121     /***
122      * Create and return a newly instansiated <code>MessageResources</code>.
123      * This method must be implemented by concrete subclasses.
124      *
125      * @param config Configuration parameter(s) for the requested bundle
126      */
127     public abstract MessageResources createResources(String config);
128 
129     /***
130      * The fully qualified class name that is used for <code>MessageResourcesFactory</code>
131      * instances.
132      *
133      * @return class name that is used for <code>MessageResourcesFactory</code>
134      *         instances
135      */
136     public static String getFactoryClass() {
137         return (MessageResourcesFactory.factoryClass);
138     }
139 
140     /***
141      * Set the fully qualified class name that is used for
142      * <code>MessageResourcesFactory</code> instances.
143      *
144      * @param factoryClass name that is used for <code>MessageResourcesFactory</code>
145      *                     instances
146      */
147     public static void setFactoryClass(String factoryClass) {
148         MessageResourcesFactory.factoryClass = factoryClass;
149         MessageResourcesFactory.clazz = null;
150     }
151 
152     // --------------------------------------------------------- Static Methods
153 
154     /***
155      * Create and return a <code>MessageResourcesFactory</code> instance of
156      * the appropriate class, which can be used to create customized
157      * <code>MessageResources</code> instances.  If no such factory can be
158      * created, return <code>null</code> instead.
159      */
160     public static MessageResourcesFactory createFactory() {
161         // Construct a new instance of the specified factory class
162         try {
163             if (clazz == null) {
164                 clazz = RequestUtils.applicationClass(factoryClass);
165             }
166 
167             MessageResourcesFactory factory =
168                 (MessageResourcesFactory) clazz.newInstance();
169 
170             return (factory);
171         } catch (Throwable t) {
172             LOG.error("MessageResourcesFactory.createFactory", t);
173 
174             return (null);
175         }
176     }
177 }