KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > impl > MessageFinderImpl


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.impl;
16
17 import java.io.BufferedInputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Locale JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Properties JavaDoc;
29
30 import org.apache.hivemind.ApplicationRuntimeException;
31 import org.apache.hivemind.Resource;
32 import org.apache.hivemind.internal.MessageFinder;
33 import org.apache.hivemind.util.Defense;
34 import org.apache.hivemind.util.IOUtils;
35 import org.apache.hivemind.util.LocalizedNameGenerator;
36
37 /**
38  * @author Howard M. Lewis Ship
39  * @since 1.1
40  */

41 public class MessageFinderImpl implements MessageFinder
42 {
43     private static final String JavaDoc EXTENSION = ".properties";
44
45     private static class Localization
46     {
47         private Locale JavaDoc _locale;
48
49         private Resource _resource;
50
51         Localization(Locale JavaDoc locale, Resource resource)
52         {
53             _locale = locale;
54             _resource = resource;
55         }
56
57         public Locale JavaDoc getLocale()
58         {
59             return _locale;
60         }
61
62         public Resource getResource()
63         {
64             return _resource;
65         }
66
67     }
68
69     private Resource _baseResource;
70
71     private String JavaDoc _baseName;
72
73     private Map JavaDoc _propertiesMap = new HashMap JavaDoc();
74
75     private Properties JavaDoc _emptyProperties = new Properties JavaDoc();
76
77     public MessageFinderImpl(Resource baseResource)
78     {
79         Defense.notNull(baseResource, "baseResource");
80
81         _baseResource = baseResource;
82
83         // Strip off the extension to form the base name
84
// when building new (localized) resources.
85

86         String JavaDoc name = _baseResource.getName();
87         int dotx = name.lastIndexOf('.');
88         if (dotx < 1) {
89             _baseName = name;
90         } else {
91             _baseName = name.substring(0, dotx);
92         }
93     }
94
95     public String JavaDoc getMessage(String JavaDoc key, Locale JavaDoc locale)
96     {
97         return findProperties(locale).getProperty(key);
98     }
99
100     private synchronized Properties JavaDoc findProperties(Locale JavaDoc locale)
101     {
102         Properties JavaDoc result = (Properties JavaDoc) _propertiesMap.get(locale);
103
104         // If doesn't exist, build it (which will update the
105
// propertiesMap as a side effect.
106

107         if (result == null)
108             result = buildProperties(locale);
109
110         return result;
111     }
112
113     private Properties JavaDoc buildProperties(Locale JavaDoc locale)
114     {
115         Properties JavaDoc result = _emptyProperties;
116
117         List JavaDoc localizations = findLocalizations(locale);
118
119         Iterator JavaDoc i = localizations.iterator();
120         while (i.hasNext())
121         {
122             Localization l = (Localization) i.next();
123
124             result = readProperties(l.getLocale(), l.getResource(), result);
125         }
126
127         return result;
128     }
129
130     /**
131      * Returns the properties, reading them if necessary. Properties may have been previously read
132      * for this locale, in which case the cached value is returned. Also, if the resource doesn't
133      * exist, then the parent is returned as is. Updates the propertiesMap cache.
134      */

135
136     private Properties JavaDoc readProperties(Locale JavaDoc locale, Resource propertiesResource, Properties JavaDoc parent)
137     {
138         Properties JavaDoc result = (Properties JavaDoc) _propertiesMap.get(locale);
139
140         if (result != null)
141             return result;
142
143         URL JavaDoc url = propertiesResource.getResourceURL();
144
145         if (url == null)
146             result = parent;
147         else
148             result = readPropertiesFile(url, parent);
149
150         _propertiesMap.put(locale, result);
151
152         return result;
153     }
154
155     private Properties JavaDoc readPropertiesFile(URL JavaDoc url, Properties JavaDoc parent)
156     {
157         InputStream JavaDoc stream = null;
158
159         Properties JavaDoc result = new Properties JavaDoc(parent);
160
161         try
162         {
163             stream = new BufferedInputStream JavaDoc(url.openStream());
164
165             result.load(stream);
166
167             stream.close();
168
169             stream = null;
170         }
171         catch (IOException JavaDoc ex)
172         {
173             throw new ApplicationRuntimeException(ImplMessages.unableToReadMessages(url), ex);
174
175         }
176         finally
177         {
178             IOUtils.close(stream);
179         }
180
181         return result;
182     }
183
184     /**
185      * Returns a List of Localizations, in order from most generic (i.e., hivemodule.properties) to
186      * most specific (i.e., hivemodule_en_US_yokel.properties).
187      */

188
189     private List JavaDoc findLocalizations(Locale JavaDoc locale)
190     {
191         List JavaDoc result = new ArrayList JavaDoc();
192
193         LocalizedNameGenerator g = new LocalizedNameGenerator(_baseName, locale, EXTENSION);
194
195         while (g.more())
196         {
197             String JavaDoc name = g.next();
198
199             Localization l = new Localization(g.getCurrentLocale(), _baseResource
200                     .getRelativeResource(name));
201
202             result.add(l);
203         }
204
205         Collections.reverse(result);
206
207         return result;
208     }
209 }
Popular Tags