|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
MessagesImpl.java | 88.9% | 93.3% | 100% | 93.1% |
|
1 |
// Copyright 2004 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.IOException;
|
|
18 |
import java.io.InputStream;
|
|
19 |
import java.net.URL;
|
|
20 |
import java.text.MessageFormat;
|
|
21 |
import java.util.ArrayList;
|
|
22 |
import java.util.List;
|
|
23 |
import java.util.Locale;
|
|
24 |
import java.util.Properties;
|
|
25 |
|
|
26 |
import org.apache.commons.logging.Log;
|
|
27 |
import org.apache.commons.logging.LogFactory;
|
|
28 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
29 |
import org.apache.hivemind.HiveMind;
|
|
30 |
import org.apache.hivemind.Messages;
|
|
31 |
import org.apache.hivemind.Resource;
|
|
32 |
import org.apache.hivemind.util.LocalizedNameGenerator;
|
|
33 |
|
|
34 |
/**
|
|
35 |
* Implementation of {@link org.apache.hivemind.Messages} for
|
|
36 |
* a {@link org.apache.hivemind.internal.Module}.
|
|
37 |
*
|
|
38 |
* TODO: Refactor this and {@link org.apache.hivemind.impl.MessageFormatter} into
|
|
39 |
* common base classes.
|
|
40 |
*
|
|
41 |
* @author Howard Lewis Ship
|
|
42 |
*/
|
|
43 |
public final class MessagesImpl implements Messages |
|
44 |
{ |
|
45 |
private static final Log LOG = LogFactory.getLog(MessagesImpl.class); |
|
46 |
|
|
47 |
private Properties _properties;
|
|
48 |
private Locale _locale;
|
|
49 |
|
|
50 | 14 |
public MessagesImpl(Resource moduleLocation, Locale locale)
|
51 |
{ |
|
52 | 14 |
_locale = locale; |
53 |
|
|
54 | 14 |
initialize(moduleLocation); |
55 |
} |
|
56 |
|
|
57 | 14 |
private void initialize(Resource moduleLocation) |
58 |
{ |
|
59 | 14 |
if (LOG.isDebugEnabled())
|
60 | 0 |
LOG.debug("Reading message properties for module at " + moduleLocation);
|
61 |
|
|
62 | 14 |
String descriptorName = moduleLocation.getName(); |
63 | 14 |
int dotx = descriptorName.lastIndexOf('.');
|
64 | 14 |
String baseName = descriptorName.substring(0, dotx); |
65 |
|
|
66 | 14 |
LocalizedNameGenerator g = new LocalizedNameGenerator(baseName, _locale, ".properties"); |
67 | 14 |
List urls = new ArrayList();
|
68 |
|
|
69 | 14 |
while (g.more())
|
70 |
{ |
|
71 | 33 |
String name = g.next(); |
72 | 33 |
Resource l = moduleLocation.getRelativeResource(name); |
73 | 33 |
URL url = l.getResourceURL(); |
74 |
|
|
75 | 33 |
if (url != null) |
76 | 14 |
urls.add(url); |
77 |
} |
|
78 |
|
|
79 |
// Now read an assemble them, least specific to most specific.
|
|
80 |
// More specific keys overwrite less specific keys.
|
|
81 |
|
|
82 | 14 |
int count = urls.size();
|
83 |
|
|
84 | 14 |
_properties = new Properties();
|
85 |
|
|
86 | 14 |
for (int i = count - 1; i >= 0; i--) |
87 |
{ |
|
88 | 14 |
URL url = (URL) urls.get(i); |
89 |
|
|
90 | 14 |
if (LOG.isDebugEnabled())
|
91 | 0 |
LOG.debug("Reading message properties from " + url);
|
92 |
|
|
93 | 14 |
try
|
94 |
{ |
|
95 | 14 |
InputStream stream = url.openStream(); |
96 |
|
|
97 | 14 |
_properties.load(stream); |
98 |
|
|
99 | 14 |
stream.close(); |
100 |
} |
|
101 |
catch (IOException ex)
|
|
102 |
{ |
|
103 | 0 |
throw new ApplicationRuntimeException(ImplMessages.unabelToReadMessages(url), ex); |
104 |
} |
|
105 |
} |
|
106 |
|
|
107 |
} |
|
108 |
|
|
109 | 13 |
public String getMessage(String key)
|
110 |
{ |
|
111 | 13 |
String result = _properties.getProperty(key); |
112 |
|
|
113 | 13 |
if (result == null) |
114 | 2 |
result = "[" + key.toUpperCase() + "]"; |
115 |
|
|
116 |
// Reasons not to write the result back in as a new property:
|
|
117 |
// 1) This is a developer error that should be resolved before an app goes into production
|
|
118 |
// 2) Would have to synchronize getMessage() methods
|
|
119 |
// 3) Would screw up #getMessage(String, String)
|
|
120 |
|
|
121 | 13 |
return result;
|
122 |
} |
|
123 |
|
|
124 | 2 |
public String getMessage(String key, String defaultValue)
|
125 |
{ |
|
126 | 2 |
return _properties.getProperty(key, defaultValue);
|
127 |
} |
|
128 |
|
|
129 | 6 |
public String format(String key, Object[] args)
|
130 |
{ |
|
131 | 6 |
String pattern = getMessage(key); |
132 |
|
|
133 | 6 |
for (int i = 0; i < args.length; i++) |
134 |
{ |
|
135 | 12 |
Object arg = args[i]; |
136 |
|
|
137 | 12 |
if (arg != null && arg instanceof Throwable) |
138 | 2 |
args[i] = extractMessage((Throwable) arg); |
139 |
} |
|
140 |
|
|
141 |
// This ugliness is mandated for JDK 1.3 compatibility, which has a bug
|
|
142 |
// in MessageFormat ... the
|
|
143 |
// pattern is applied in the constructor, using the system default Locale,
|
|
144 |
// regardless of what locale is later specified!
|
|
145 |
// It appears that the problem does not exist in JDK 1.4.
|
|
146 |
|
|
147 | 6 |
MessageFormat messageFormat = new MessageFormat(""); |
148 | 6 |
messageFormat.setLocale(_locale); |
149 | 6 |
messageFormat.applyPattern(pattern); |
150 |
|
|
151 | 6 |
return messageFormat.format(args);
|
152 |
} |
|
153 |
|
|
154 | 2 |
private String extractMessage(Throwable t)
|
155 |
{ |
|
156 | 2 |
String message = t.getMessage(); |
157 |
|
|
158 | 2 |
return HiveMind.isNonBlank(message) ? message : t.getClass().getName();
|
159 |
} |
|
160 |
|
|
161 | 3 |
public String format(String key, Object arg0)
|
162 |
{ |
|
163 | 3 |
return format(key, new Object[] { arg0 }); |
164 |
} |
|
165 |
|
|
166 | 1 |
public String format(String key, Object arg0, Object arg1)
|
167 |
{ |
|
168 | 1 |
return format(key, new Object[] { arg0, arg1 }); |
169 |
} |
|
170 |
|
|
171 | 1 |
public String format(String key, Object arg0, Object arg1, Object arg2)
|
172 |
{ |
|
173 | 1 |
return format(key, new Object[] { arg0, arg1, arg2 }); |
174 |
} |
|
175 |
|
|
176 |
} |
|
177 |
|
|