KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > inject > AbstractModule


1 /**
2  * Copyright (C) 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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  */

16
17 package com.google.inject;
18
19 import com.google.inject.binder.ConstantBindingBuilder;
20 import com.google.inject.binder.AnnotatedBindingBuilder;
21 import com.google.inject.binder.LinkedBindingBuilder;
22 import com.google.inject.binder.AnnotatedConstantBindingBuilder;
23 import com.google.inject.matcher.Matcher;
24 import com.google.inject.spi.SourceProviders;
25 import com.google.inject.util.Objects;
26 import java.lang.annotation.Annotation JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import org.aopalliance.intercept.MethodInterceptor;
29
30 /**
31  * A support class for {@link Module}s which reduces repetition and results in
32  * a more readable configuration. Simply extend this class, implement {@link
33  * #configure()}, and call the inherited methods which mirror those found in
34  * {@link Binder}. For example:
35  *
36  * <pre>
37  * import static com.google.inject.Names.named;
38  *
39  * public class MyModule extends AbstractModule {
40  * protected void configure() {
41  * bind(Foo.class).to(FooImpl.class).in(Scopes.SINGLETON);
42  * bind(BarImpl.class);
43  * link(Bar.class).to(BarImpl.class);
44  * bindConstant(named("port")).to(8080);
45  * }
46  * }
47  * </pre>
48  *
49  * @author crazybob@google.com (Bob Lee)
50  */

51 public abstract class AbstractModule implements Module {
52
53   static {
54     SourceProviders.skip(AbstractModule.class);
55   }
56
57   Binder builder;
58
59   public final synchronized void configure(Binder builder) {
60     try {
61       if (this.builder != null) {
62         throw new IllegalStateException JavaDoc("Re-entry is not allowed.");
63       }
64       this.builder = Objects.nonNull(builder, "builder");
65
66       configure();
67
68     }
69     finally {
70       this.builder = null;
71     }
72   }
73
74   /**
75    * Configures a {@link Binder} via the exposed methods.
76    */

77   protected abstract void configure();
78
79   /**
80    * Gets direct access to the underlying {@code Binder}.
81    */

82   protected Binder binder() {
83     return builder;
84   }
85
86   /**
87    * @see Binder#bindScope(Class, Scope)
88    */

89   protected void bindScope(Class JavaDoc<? extends Annotation JavaDoc> scopeAnnotation,
90       Scope scope) {
91     builder.bindScope(scopeAnnotation, scope);
92   }
93
94   /**
95    * @see Binder#bind(Key)
96    */

97   protected <T> LinkedBindingBuilder<T> bind(Key<T> key) {
98     return builder.bind(key);
99   }
100
101   /**
102    * @see Binder#bind(TypeLiteral)
103    */

104   protected <T> AnnotatedBindingBuilder<T> bind(TypeLiteral<T> typeLiteral) {
105     return builder.bind(typeLiteral);
106   }
107
108   /**
109    * @see Binder#bind(Class)
110    */

111   protected <T> AnnotatedBindingBuilder<T> bind(Class JavaDoc<T> clazz) {
112     return builder.bind(clazz);
113   }
114
115   /**
116    * @see Binder#bindConstant()
117    */

118   protected AnnotatedConstantBindingBuilder bindConstant() {
119     return builder.bindConstant();
120   }
121
122   /**
123    * @see Binder#install(Module)
124    */

125   protected void install(Module module) {
126     builder.install(module);
127   }
128
129   /**
130    * @see Binder#addError(String, Object[])
131    */

132   protected void addError(String JavaDoc message, Object JavaDoc... arguments) {
133     builder.addError(message, arguments);
134   }
135
136   /**
137    * @see Binder#addError(Throwable)
138    */

139   protected void addError(Throwable JavaDoc t) {
140     builder.addError(t);
141   }
142
143   /**
144    * @see Binder#requestStaticInjection(Class[])
145    */

146   protected void requestStaticInjection(Class JavaDoc<?>... types) {
147     builder.requestStaticInjection(types);
148   }
149
150   /**
151    * @see Binder#bindInterceptor(com.google.inject.matcher.Matcher,
152    * com.google.inject.matcher.Matcher,
153    * org.aopalliance.intercept.MethodInterceptor[])
154    */

155   protected void bindInterceptor(Matcher<? super Class JavaDoc<?>> classMatcher,
156       Matcher<? super Method JavaDoc> methodMatcher,
157       MethodInterceptor... interceptors) {
158     builder.bindInterceptor(classMatcher, methodMatcher, interceptors);
159   }
160 }
161
Popular Tags