ITrackerResourceBundle.java

  1. /*
  2.  * This software was designed and created by Jason Carroll.
  3.  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
  4.  * The author can be reached at jcarroll@cowsultants.com
  5.  * ITracker website: http://www.cowsultants.com
  6.  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it only under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  */

  18. package org.itracker.core.resources;
  19. import org.itracker.ITrackerDirtyResourceException;
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;
  22. import java.util.*;

  23. public class ITrackerResourceBundle extends ResourceBundle {

  24.     private static final Logger log = LoggerFactory
  25.             .getLogger(ITrackerResourceBundle.class);
  26.     private final Properties data = new Properties();

  27.     private ResourceBundle propertiesBundle;


  28.     static ResourceBundle loadBundle() {
  29.         return new ITrackerResourceBundle();
  30.     }

  31.     static ResourceBundle loadBundle(Locale locale) {
  32.         return new ITrackerResourceBundle(locale);
  33.     }


  34.     static ResourceBundle loadBundle(Locale locale, Properties items) {
  35.         return new ITrackerResourceBundle(locale, items);
  36.     }

  37.     private ITrackerResourceBundle() {
  38.         super.setParent(ResourceBundle.getBundle(
  39.                 ITrackerResources.RESOURCE_BUNDLE_NAME, new Locale(
  40.                 ITrackerResources.getDefaultLocale())));
  41.     }

  42.     /**
  43.      * @param locale
  44.      */
  45.     private ITrackerResourceBundle(Locale locale) {
  46.         if (null == locale) {
  47.             locale = ITrackerResources.getLocale(ITrackerResources
  48.                     .getDefaultLocale());
  49.         }
  50.         this.propertiesBundle = ResourceBundle.getBundle(
  51.                 ITrackerResources.RESOURCE_BUNDLE_NAME, locale,
  52.                 Control.getNoFallbackControl(Control.FORMAT_DEFAULT));

  53.         if (!locale.equals(ITrackerResources
  54.                 .getLocale(ITrackerResources.BASE_LOCALE))) {
  55.             if (locale.getCountry().length() > 0) {
  56.                 setParent(ITrackerResources.getBundle(new Locale(locale
  57.                         .getLanguage())));
  58.             } else if (locale.getLanguage().length() > 0) {
  59.                 setParent(ITrackerResources.getBundle(ITrackerResources
  60.                         .getLocale(ITrackerResources.BASE_LOCALE)));
  61.             }
  62.         }

  63.     }

  64.     public ITrackerResourceBundle(Locale locale, Properties items) {
  65.         this(locale);
  66.         if (null != items) {
  67.             this.data.putAll(items);
  68.         }
  69.     }

  70.     public static ResourceBundle getBundle() {
  71.         return ITrackerResources.getBundle();
  72.     }

  73.     public static ResourceBundle getBundle(Locale locale) {
  74.         return ITrackerResources.getBundle(locale);
  75.     }

  76.     /**
  77.      * @deprecated used still for testing
  78.      */
  79.     public ITrackerResourceBundle(Locale locale, Object[][] data) {
  80.         this(locale);
  81.         setContents(data);
  82.     }

  83.     /**
  84.      * @param content
  85.      */
  86.     @Deprecated
  87.     private void setContents(Object[][] content) {
  88.         if (content != null && content.length == 2
  89.                 && content[0].length == content[1].length) {
  90.             synchronized (data) {
  91.                 data.clear();
  92.                 for (int i = 0; i < content[0].length; i++) {
  93.                     data.put(content[0][i], content[1][i]);
  94.                 }
  95.             }
  96.         }
  97.     }

  98.     @Override
  99.     public Locale getLocale() {
  100.         Locale l = super.getLocale();
  101.         if (null == l && null != propertiesBundle) {
  102.             l = propertiesBundle.getLocale();
  103.         }
  104.         return l;
  105.     }

  106.     public boolean isDirty(String key) {
  107.         try {
  108.             handleGetObject(key);
  109.         } catch (ITrackerDirtyResourceException exception) {
  110.             return true;
  111.         }
  112.         return false;
  113.     }


  114.     public void updateValue(String key, String value) {
  115.         if (null == key) {
  116.             throw new IllegalArgumentException("key must not be null");
  117.         }
  118.         if (null == value) {
  119.             throw new IllegalArgumentException("value must not be null");
  120.         }
  121.         synchronized (data) {
  122.             data.put(key, value);
  123.         }
  124.     }


  125.     public void removeValue(String key, boolean markDirty) {
  126.         if (key != null) {
  127.             synchronized (data) {
  128.                 if (markDirty) {
  129.                     data.put(key, new DirtyKey() {
  130.                     });
  131.                 } else {
  132.                     data.remove(key);
  133.                 }
  134.             }
  135.         }
  136.     }

  137.     /**
  138.      * Implementation of ResourceBundle.handleGetObject. Returns the request key
  139.      * from the internal data map.
  140.      */
  141.     public final Object handleGetObject(String key) {
  142.         Object value = data.get(key);
  143.         if (value instanceof DirtyKey) {
  144.             throw new ITrackerDirtyResourceException(
  145.                     "The requested key has been marked dirty.",
  146.                     "ITrackerResourceBundle_" + getLocale(), key);
  147.         }
  148.         if (null == value) {
  149.             try {
  150.                 value = propertiesBundle.getObject(key);
  151.             } catch (MissingResourceException e) {
  152.                 if (log.isDebugEnabled()) {
  153.                     log.debug("handleGetObject: " + key, e);
  154.                 }
  155.             }
  156.         }
  157.         return value;
  158.     }

  159.     /**
  160.      * Implementation of ResourceBundle.getKeys. Since it returns an
  161.      * enumeration, It creates a new Set, and returns that collections
  162.      * enumerator.
  163.      */
  164.     public Enumeration<String> getKeys() {
  165.                 Set set = new TreeSet(data.keySet());
  166.         if (null != parent) {
  167.             Enumeration<String> keys = parent.getKeys();
  168.             String key;
  169.             while (keys.hasMoreElements()) {
  170.                 key = keys.nextElement();
  171.                 set.add(key);
  172.             }
  173.         }
  174.         if (null != propertiesBundle) {
  175.             Enumeration<String> keys = propertiesBundle.getKeys();
  176.             String key;
  177.             while (keys.hasMoreElements()) {
  178.                 key = keys.nextElement();
  179.                 set.add(key);
  180.             }
  181.         }
  182.         return Collections.enumeration(set);
  183.     }

  184.     public interface DirtyKey {
  185.     }
  186. }