View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.i18n;
35  
36  import info.magnolia.cms.util.ClasspathResourcesUtil;
37  
38  import java.io.IOException;
39  import java.io.InputStream;
40  import java.io.Serializable;
41  import java.util.Iterator;
42  import java.util.Locale;
43  import java.util.MissingResourceException;
44  import java.util.PropertyResourceBundle;
45  import java.util.ResourceBundle;
46  
47  import org.apache.commons.collections.IteratorUtils;
48  import org.apache.commons.io.IOUtils;
49  import org.apache.commons.lang.StringUtils;
50  
51  
52  /**
53   * Loads the message from properties files accessed as classpath resources.
54   * @author Philipp Bracher
55   * @version $Revision: 49481 $ ($Author: ndesmarais $)
56   */
57  public class DefaultMessagesImpl extends AbstractMessagesImpl implements Serializable {
58  
59      private static final long serialVersionUID = 6557257240676973699L;
60  
61      protected DefaultMessagesImpl(String basename, Locale locale) {
62          super(basename, locale);
63      }
64  
65      /**
66       * Get the message from the bundle.
67       * @param key the key
68       * @return message
69       */
70      @Override
71      public String get(String key) {
72          if(key == null){
73              return "??????";
74          }
75          try {
76              return getBundle().getString(key);
77          }
78          catch (MissingResourceException e) {
79              return "???" + key + "???";
80          }
81      }
82  
83      /**
84       * Returns the bundle for the current basename.
85       */
86      protected ResourceBundle getBundle() {
87          if (bundle == null) {
88              InputStream stream = null;
89              try {
90                  // TODO : isnt this what ResourceBundle does? except maybe for some ClasspathResourcesUtil magic ?
91                  final String path = "/" + StringUtils.replace(basename, ".", "/");
92                  final Locale locale = getLocale();
93                  final Locale defaultLocale = MessagesManager.getInstance().getDefaultLocale();
94  
95                  stream = ClasspathResourcesUtil.getStream(path + "_" + locale.getLanguage() + "_" + locale.getCountry() + ".properties", false);
96                  if (stream == null) {
97                      stream = ClasspathResourcesUtil.getStream(path + "_" + locale.getLanguage() + ".properties", false);
98                  }
99                  if (stream == null) {
100                     stream = ClasspathResourcesUtil.getStream(path + "_" + defaultLocale.getLanguage() + ".properties", false);
101                 }
102                 if (stream == null) {
103                     stream = ClasspathResourcesUtil.getStream(path + ".properties", false);
104                 }
105 
106                 if (stream != null) {
107                     bundle = new PropertyResourceBundle(stream);
108                 }
109                 else {
110                     bundle = ResourceBundle.getBundle(getBasename(), locale);
111                 }
112             }
113             catch (IOException e) {
114                 log.error("can't load messages for " + basename);
115             } finally {
116                 IOUtils.closeQuietly(stream);
117             }
118         }
119         return bundle;
120     }
121 
122     @Override
123     public void reload() throws Exception {
124         this.bundle = null;
125     }
126 
127     /**
128      * Iterate over the keys.
129      */
130     @Override
131     public Iterator<String> keys() {
132         ResourceBundle bundle = this.getBundle();
133         if(bundle != null) {
134             return IteratorUtils.asIterator(bundle.getKeys());
135         }
136         return IteratorUtils.EMPTY_ITERATOR;
137     }
138 }