View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.services.i18n;
20  
21  import java.io.IOException;
22  import java.util.List;
23  import java.util.Locale;
24  import java.util.MissingResourceException;
25  import java.util.PropertyResourceBundle;
26  import java.util.ResourceBundle;
27  import java.util.ResourceBundle.Control;
28  
29  import org.apache.logging.log4j.LogManager;
30  import org.apache.logging.log4j.Logger;
31  import org.mycore.common.config.MCRComponent;
32  import org.mycore.common.config.MCRConfigurationInputStream;
33  import org.mycore.datamodel.language.MCRLanguageFactory;
34  
35  import com.google.common.collect.Lists;
36  
37  /**
38   * A {@link Control} that stacks ResourceBundles of {@link MCRComponent}.
39   * 
40   * @author Thomas Scheffler (yagee)
41   * @since 2014.04
42   */
43  public class MCRCombinedResourceBundleControl extends Control {
44      private static Logger LOGGER = LogManager.getLogger(MCRCombinedResourceBundleControl.class);
45  
46      private Locale defaultLocale = MCRLanguageFactory.instance().getDefaultLanguage().getLocale();
47  
48      private static final ResourceBundle.Control CONTROL_HELPER = new ResourceBundle.Control() {
49      };
50  
51      @Override
52      public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
53          throws IllegalAccessException, InstantiationException, IOException {
54          if (LOGGER.isDebugEnabled()) {
55              LOGGER.debug("New bundle: {}, locale {}", baseName, locale);
56          }
57          if (locale.equals(Locale.ROOT)) {
58              //MCR-1064 fallback should be default language, if property key does not exist
59              locale = defaultLocale;
60          }
61          String bundleName = baseName.substring(baseName.indexOf(':') + 1);
62          String filename = CONTROL_HELPER.toBundleName(bundleName, locale) + ".properties";
63          try (MCRConfigurationInputStream propertyStream = new MCRConfigurationInputStream(filename)) {
64              if (propertyStream.isEmpty()) {
65                  String className = bundleName + "_" + locale;
66                  throw new MissingResourceException(
67                      "Can't find bundle for base name " + baseName + ", locale " + locale, className, "");
68              }
69              return new PropertyResourceBundle(propertyStream);
70          }
71      }
72  
73      @Override
74      public List<String> getFormats(String baseName) {
75          return Lists.newArrayList("mycore");
76      }
77  
78      @Override
79      public Locale getFallbackLocale(String baseName, Locale locale) {
80          return defaultLocale.equals(locale) ? null : defaultLocale;
81      }
82  
83      @Override
84      public long getTimeToLive(String baseName, Locale locale) {
85          //JAR files never change in runtime
86          return Long.MAX_VALUE;
87      }
88  
89      @Override
90      public boolean needsReload(String baseName, Locale locale, String format, ClassLoader loader,
91          ResourceBundle bundle, long loadTime) {
92          //JAR files never change in runtime
93          return false;
94      }
95  
96  }