View Javadoc

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