01: // Copyright 2006 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // 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.tapestry.ioc.util;
16:
17: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newCaseInsensitiveMap;
18:
19: import java.util.Map;
20:
21: import org.apache.tapestry.ioc.MessageFormatter;
22: import org.apache.tapestry.ioc.Messages;
23: import org.apache.tapestry.ioc.internal.util.ConcurrentBarrier;
24: import org.apache.tapestry.ioc.internal.util.Invokable;
25: import org.apache.tapestry.ioc.internal.util.MessageFormatterImpl;
26:
27: /**
28: * Abstract implementation of {@link Messages} that doesn't know where values come from (that
29: * information is supplied in a subclass, via the {@link #valueForKey(String)} method).
30: */
31: public abstract class AbstractMessages implements Messages {
32: private final ConcurrentBarrier _barrier = new ConcurrentBarrier();
33:
34: /** String key to MF instance. */
35: private final Map<String, MessageFormatter> _cache = newCaseInsensitiveMap();
36:
37: /**
38: * Invoked to provide the value for a particular key. This may be invoked multiple times even
39: * for the same key. The implementation should <em>ignore the case of the key</em>.
40: *
41: * @param key
42: * the key to obtain a value for (case insensitive)
43: * @return the value for the key, or null if this instance can not provide the value
44: */
45: protected abstract String valueForKey(String key);
46:
47: public boolean contains(String key) {
48: return valueForKey(key) != null;
49: }
50:
51: public String get(String key) {
52: if (contains(key))
53: return valueForKey(key);
54:
55: return String.format("[[missing key: %s]]", key);
56: }
57:
58: public MessageFormatter getFormatter(final String key) {
59: MessageFormatter result = _barrier
60: .withRead(new Invokable<MessageFormatter>() {
61: public MessageFormatter invoke() {
62: return _cache.get(key);
63: }
64: });
65:
66: if (result != null)
67: return result;
68:
69: final MessageFormatter newFormatter = buildMessageFormatter(key);
70:
71: _barrier.withWrite(new Runnable() {
72: public void run() {
73: _cache.put(key, newFormatter);
74: };
75: });
76:
77: return newFormatter;
78: }
79:
80: private MessageFormatter buildMessageFormatter(String key) {
81: String format = get(key);
82:
83: return new MessageFormatterImpl(format);
84: }
85:
86: public String format(String key, Object... args) {
87: return getFormatter(key).format(args);
88: }
89:
90: }
|