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