View Javadoc

1   /** ------------------------------------
2    * SweetDEV RIA library
3    * Copyright [2006 - 2010] [Ideo Technologies]
4    * ------------------------------------
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   * For more information, please contact us at:
20   *         Ideo Technologies S.A
21   *        124 rue de Verdun
22   *        92800 Puteaux - France
23   *
24   *      France & Europe Phone : +33 1.46.25.09.60
25   *
26   *
27   *        web : http://www.ideotechnologies.com
28   *        email : SweetDEV-RIA@ideotechnologies.com
29   *
30   *
31   * @version ${pom.version}
32   * @author Ideo Technologies
33   */
34  package com.ideo.sweetdevria.util;
35  
36  import java.io.Serializable;
37  import java.util.Locale;
38  
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  
42  /**
43   * Messages resources abstract class.
44   * Initially from org.apache.struts.util.MessageResourcesFactory.
45   */
46  public abstract class MessageResources implements Serializable {
47  
48      /**
49       * Logger.
50       */
51      protected static final Log LOG = LogFactory.getLog(MessageResources.class);
52  
53      /**
54       * Properties file name.
55       */
56      protected String propertiesFileName;
57  
58      /**
59       * Factory.
60       */
61      protected MessageResourcesFactory factory;
62  
63      /**
64       * Return null ? (not used here)
65       */
66      protected boolean returnNull;
67  
68      /**
69       * Default factory.
70       */
71      protected static MessageResourcesFactory defaultFactory = null;
72  
73      /**
74       * Constructor.
75       * @param factory Factory used.
76       * @param propertiesFileName Properties file name.
77       */
78      public MessageResources(final MessageResourcesFactory factory, final String propertiesFileName) {
79          this(factory, propertiesFileName, false);
80      }
81  
82      /**
83       * Constructor.
84       * @param factory Factory used.
85       * @param propertiesFileName Properties file name.
86       * @param returnNull Return null ? (not used here)
87       */
88      public MessageResources(final MessageResourcesFactory factory, final String propertiesFileName, final boolean returnNull) {
89          this.factory = factory;
90          this.propertiesFileName = propertiesFileName;
91          this.returnNull = returnNull;
92      }
93  
94      /**
95       * Get message from the specific locale.
96       * @param locale locale in wich get message.
97       * @param messageKey message key.
98       * @return message value in specified locale or in default locale (English) if not available.
99       */
100     public abstract String getMessage(final Locale locale, final String messageKey);
101 
102     /**
103      * Get message from the default locale (English).
104      * @param messageKey message key.
105      * @return message value in default locale (English).
106      */
107     public abstract String getMessage(final String messageKey);
108 
109     /**
110      * Get message by using object in argument.
111      * @param key message key.
112      * @param object Object used to get the locale (i.e. PageContext from where extract the locale).
113      * @return messave value.
114      */
115     public abstract String getMessage(final String key, final Object object);
116 
117     /**
118      * Get the config.
119      * @return  Returns the config.
120      */
121     public String getConfig() {
122         return propertiesFileName;
123     }
124 
125     /**
126      * Get the factory.
127      * @return  Returns the factory.
128      */
129     public MessageResourcesFactory getFactory() {
130         return factory;
131     }
132 
133     /**
134      * Get the returnNull.
135      * @return  Returns the returnNull.
136      */
137     public boolean getReturnNull() {
138         return returnNull;
139     }
140 
141     /**
142      * Set the returnNull.
143      * @param returnNull  The returnNull to set.
144      */
145     public void setReturnNull(final boolean returnNull) {
146         this.returnNull = returnNull;
147     }
148 
149     /**
150      * Get messages resources.
151      * @param propertiesFileName Properties file name.
152      * @return a message resources object corresponding to a properties file
153      */
154     public static synchronized MessageResources getMessageResources(final String propertiesFileName) {
155         if (defaultFactory == null) {
156             defaultFactory = MessageResourcesFactory.createFactory();
157         }
158         return defaultFactory.createResources(propertiesFileName);
159     }
160 
161 }