Source code: org/apache/struts/util/MessageResourcesFactory.java
1 /*
2 * $Id: MessageResourcesFactory.java 54929 2004-10-16 16:38:42Z germuska $
3 *
4 * Copyright 1999-2004 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
19
20 package org.apache.struts.util;
21
22
23 import java.io.Serializable;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27
28 /**
29 * Factory for <code>MessageResources</code> instances. The general usage
30 * pattern for this class is:
31 * <ul>
32 * <li>Call <code>MessageResourcesFactory().createFactory()</code> to retrieve
33 * a <code>MessageResourcesFactory</code> instance.</li>
34 * <li>Set properties as required to configure this factory instance to create
35 * <code>MessageResources</code> instances with desired
36 * characteristics.</li>
37 * <li>Call the <code>createResources()</code> method of the factory to
38 * retrieve a newly instantiated <code>MessageResources</code>
39 * instance.</li>
40 * </ul>
41 *
42 * @version $Rev: 54929 $ $Date: 2004-10-16 09:38:42 -0700 (Sat, 16 Oct 2004) $
43 */
44
45 public abstract class MessageResourcesFactory implements Serializable {
46
47
48 // ---------------------------------------------------- Instance Properties
49
50
51 /**
52 * The "return null" property value to which newly created
53 * MessageResourcess should be initialized.
54 */
55 protected boolean returnNull = true;
56
57 /**
58 * Get default value of the "returnNull" property used to initialize newly created
59 * MessageResourcess.
60 * @return default value of the "returnNull" property newly created
61 * MessageResourcess are initialized to.
62 */
63 public boolean getReturnNull() {
64 return (this.returnNull);
65 }
66
67 /**
68 * Set the default value of the "returnNull" property newly created
69 * MessageResourcess are initialized to.
70 * @param returnNull default value of the "returnNull" MessageResourcess are initialized to.
71 */
72 public void setReturnNull(boolean returnNull) {
73 this.returnNull = returnNull;
74 }
75
76
77 // --------------------------------------------------------- Public Methods
78
79
80 /**
81 * Create and return a newly instansiated <code>MessageResources</code>.
82 * This method must be implemented by concrete subclasses.
83 *
84 * @param config Configuration parameter(s) for the requested bundle
85 */
86 public abstract MessageResources createResources(String config);
87
88
89 // ------------------------------------------------------ Static Properties
90
91
92 /**
93 * The Java class to be used for
94 * <code>MessageResourcesFactory</code> instances.
95 */
96 protected static transient Class clazz = null;
97
98
99 /**
100 * Commons Logging instance.
101 */
102 private static Log LOG = LogFactory.getLog(MessageResourcesFactory.class);
103
104
105 /**
106 * The fully qualified class name to be used for
107 * <code>MessageResourcesFactory</code> instances.
108 */
109 protected static String factoryClass =
110 "org.apache.struts.util.PropertyMessageResourcesFactory";
111
112 /**
113 * The fully qualified class name that is used for
114 * <code>MessageResourcesFactory</code> instances.
115 * @return class name that is used for
116 * <code>MessageResourcesFactory</code> instances
117 */
118 public static String getFactoryClass() {
119 return (MessageResourcesFactory.factoryClass);
120 }
121
122 /**
123 * Set the fully qualified class name that is used for
124 * <code>MessageResourcesFactory</code> instances.
125 * @param factoryClass name that is used for
126 * <code>MessageResourcesFactory</code> instances
127 */
128 public static void setFactoryClass(String factoryClass) {
129 MessageResourcesFactory.factoryClass = factoryClass;
130 MessageResourcesFactory.clazz = null;
131 }
132
133
134 // --------------------------------------------------------- Static Methods
135
136
137 /**
138 * Create and return a <code>MessageResourcesFactory</code> instance of the
139 * appropriate class, which can be used to create customized
140 * <code>MessageResources</code> instances. If no such factory can be
141 * created, return <code>null</code> instead.
142 */
143 public static MessageResourcesFactory createFactory() {
144
145 // Construct a new instance of the specified factory class
146 try {
147 if (clazz == null)
148 clazz = RequestUtils.applicationClass(factoryClass);
149 MessageResourcesFactory factory =
150 (MessageResourcesFactory) clazz.newInstance();
151 return (factory);
152 } catch (Throwable t) {
153 LOG.error("MessageResourcesFactory.createFactory", t);
154 return (null);
155 }
156
157 }
158
159
160 }