01: /**
02: * Copyright (C) 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */package com.bm.ejb3guice.name;
16:
17: import com.bm.ejb3guice.inject.Binder;
18: import com.bm.ejb3guice.inject.Key;
19: import com.bm.ejb3guice.spi.SourceProvider;
20: import com.bm.ejb3guice.spi.SourceProviders;
21:
22: import java.util.Map;
23: import java.util.Properties;
24:
25: /**
26: * Utility methods for use with {@code @}{@link Named}.
27: *
28: * @author crazybob@google.com (Bob Lee)
29: */
30: public class Names {
31:
32: private Names() {
33: }
34:
35: static {
36: SourceProviders.skip(Names.class);
37: }
38:
39: /**
40: * Creates a {@link Named} annotation with {@code name} as the value.
41: */
42: public static Named named(String name) {
43: return new NamedImpl(name);
44: }
45:
46: /**
47: * Creates a constant binding to {@code @Named(key)} for each property.
48: */
49: public static void bindProperties(final Binder binder,
50: final Map<String, String> properties) {
51: SourceProviders.withDefault(new SimpleSourceProvider(
52: SourceProviders.defaultSource()), new Runnable() {
53: public void run() {
54: for (Map.Entry<String, String> entry : properties
55: .entrySet()) {
56: String key = entry.getKey();
57: String value = entry.getValue();
58: binder.bind(
59: Key.get(String.class, new NamedImpl(key)))
60: .toInstance(value);
61: }
62: }
63: });
64: }
65:
66: /**
67: * Creates a constant binding to {@code @Named(key)} for each property.
68: */
69: public static void bindProperties(final Binder binder,
70: final Properties properties) {
71: SourceProviders.withDefault(new SimpleSourceProvider(
72: SourceProviders.defaultSource()), new Runnable() {
73: public void run() {
74: for (Map.Entry<Object, Object> entry : properties
75: .entrySet()) {
76: String key = (String) entry.getKey();
77: String value = (String) entry.getValue();
78: binder.bind(
79: Key.get(String.class, new NamedImpl(key)))
80: .toInstance(value);
81: }
82: }
83: });
84: }
85:
86: static class SimpleSourceProvider implements SourceProvider {
87:
88: final Object source;
89:
90: SimpleSourceProvider(Object source) {
91: this .source = source;
92: }
93:
94: public Object source() {
95: return source;
96: }
97: }
98: }
|