View Javadoc

1   //////////////////////////////////////////////////////////////////////////////
2   // Clirr: compares two versions of a java library for binary compatibility
3   // Copyright (C) 2003 - 2005  Lars Kühne
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library 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 GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  //////////////////////////////////////////////////////////////////////////////
19  
20  package net.sf.clirr.core;
21  
22  import java.util.ResourceBundle;
23  import java.util.Locale;
24  import java.util.Enumeration;
25  
26  /***
27   * ResourceBundle that implements the default locale by delegating to the english bundle.
28   * This solves the bug described in
29   * https://sourceforge.net/tracker/index.php?func=detail&aid=594469&group_id=29721&atid=397078
30   * without having to duplicate any resource bundles, leading to a simpler build and a smaller jar.
31   *
32   * @author lkuehne
33   */
34  public class EventMessages extends ResourceBundle
35  {
36      /***
37       * The base name of the resource bundle from which message descriptions
38       * are read.
39       */
40      public static final String RESOURCE_NAME = EventMessages.class.getName();
41  
42      private ResourceBundle delegate = null;
43  
44      /***
45       * Control variable used in synchronized blocks that delegate to the {@link #delegate} bundle.
46       * The delegate bundle has "this" as it's parent.
47       * To prevent infinite loops in the lookup process when searching for
48       * non-existent keys we set isUsingDelegate to true to break out of the loop.
49       */
50      private boolean isUsingDelegate = false;
51  
52      /***
53       * Constructor.
54       * @deprecated Typical user code never calls this directly but uses
55       * {@link java.util.ResourceBundle#getBundle(java.lang.String)} or one of it's variants instead.
56       */
57      public EventMessages()
58      {
59      }
60  
61      private ResourceBundle getDelegate()
62      {
63          if (delegate == null)
64          {
65              delegate = ResourceBundle.getBundle(RESOURCE_NAME, Locale.ENGLISH);
66          }
67          return delegate;
68      }
69  
70      /*** @see java.util.ResourceBundle#handleGetObject */
71      protected final synchronized Object handleGetObject(String key)
72      {
73          try
74          {
75              if (isUsingDelegate)
76              {
77                  // the underlying bundle is delegating the call back to us
78                  // this means that the key is unknown, so we return null
79                  return null;
80              }
81              else
82              {
83                  isUsingDelegate = true;
84                  return getDelegate().getObject(key);
85              }
86          }
87          finally
88          {
89              isUsingDelegate = false;
90          }
91      }
92  
93      /*** @see java.util.ResourceBundle#getKeys */
94      public final synchronized Enumeration getKeys()
95      {
96          try
97          {
98              if (isUsingDelegate)
99              {
100                 // the underlying bundle is delegating the call back to us
101                 // this means that the key is unknown, so we return null
102                 return null;
103             }
104             else
105             {
106                 isUsingDelegate = true;
107                 return getDelegate().getKeys();
108             }
109         }
110         finally
111         {
112             isUsingDelegate = false;
113         }
114     }
115 
116     /*** @see java.util.ResourceBundle#getLocale */
117     public final Locale getLocale()
118     {
119         return getDelegate().getLocale();
120     }
121 
122 }