crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 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 | |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 19 | import com.google.inject.util.Objects; |
| 20 | import static com.google.inject.util.Objects.nonNull; |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame^] | 21 | import com.google.inject.spi.ErrorMessage; |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 22 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 23 | import java.lang.reflect.Member; |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.Arrays; |
| 26 | import java.util.HashMap; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 27 | import java.util.List; |
| 28 | import java.util.Map; |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 29 | import java.util.Properties; |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 30 | import java.util.Set; |
| 31 | import java.util.LinkedHashSet; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 32 | import java.util.logging.Logger; |
| 33 | |
| 34 | /** |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 35 | * Builds a dependency injection {@link Container}. Binds {@link Key}s to |
| 36 | * implementations. For example, a binding implementation could be anything |
| 37 | * from a constant value to an object in the HTTP session. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 38 | * |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 39 | * <p>Not safe for concurrent use. |
| 40 | * |
| 41 | * <p>Default bindings include: |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 42 | * |
| 43 | * <ul> |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 44 | * <li>A {@code Factory<T>} for each binding of type {@code T} |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 45 | * <li>The {@link Container} iself |
| 46 | * <li>The {@link Logger} for the class being injected |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 47 | * </ul> |
| 48 | * |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 49 | * <p>Converts constants as needed from {@code String} to any primitive type |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 50 | * in addition to {@code enum} and {@code Class<?>}. |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 51 | * |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 52 | * @author crazybob@google.com (Bob Lee) |
| 53 | */ |
| 54 | public final class ContainerBuilder { |
| 55 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 56 | private static final Logger logger = |
| 57 | Logger.getLogger(ContainerBuilder.class.getName()); |
| 58 | |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 59 | final List<BindingBuilder<?>> bindingBuilders = |
| 60 | new ArrayList<BindingBuilder<?>>(); |
| 61 | final List<ConstantBindingBuilder> constantBindingBuilders = |
| 62 | new ArrayList<ConstantBindingBuilder>(); |
| 63 | final List<LinkedBindingBuilder<?>> linkedBindingBuilders = |
| 64 | new ArrayList<LinkedBindingBuilder<?>>(); |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 65 | final Map<String, Scope> scopes = new HashMap<String, Scope>(); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 66 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 67 | final List<Class<?>> staticInjections = new ArrayList<Class<?>>(); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 68 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 69 | boolean created; |
| 70 | |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 71 | /** |
| 72 | * Keeps error messages in order and prevents duplicates. |
| 73 | */ |
| 74 | Set<ErrorMessage> errorMessages = new LinkedHashSet<ErrorMessage>(); |
| 75 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 76 | private static final InternalFactory<Container> CONTAINER_FACTORY = |
| 77 | new InternalFactory<Container>() { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 78 | public Container get(InternalContext context) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 79 | return context.getContainer(); |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | private static final InternalFactory<Logger> LOGGER_FACTORY = |
| 84 | new InternalFactory<Logger>() { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 85 | public Logger get(InternalContext context) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 86 | Member member = context.getExternalContext().getMember(); |
| 87 | return member == null ? Logger.getAnonymousLogger() |
| 88 | : Logger.getLogger(member.getDeclaringClass().getName()); |
| 89 | } |
| 90 | }; |
| 91 | |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 92 | static final String UNKNOWN_SOURCE = "[unknown source]"; |
| 93 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 94 | static final Scope DEFAULT_SCOPE = new Scope() { |
| 95 | public <T> Factory<T> scope(Key<T> key, Factory<T> creator) { |
| 96 | // We actually optimize around this. |
| 97 | throw new UnsupportedOperationException(); |
| 98 | } |
| 99 | }; |
| 100 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 101 | /** |
| 102 | * Constructs a new builder. |
| 103 | */ |
| 104 | public ContainerBuilder() { |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 105 | put(Scopes.DEFAULT, DEFAULT_SCOPE); |
| 106 | put(Scopes.SINGLETON, SingletonScope.INSTANCE); |
| 107 | |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 108 | bind(Container.class).to(CONTAINER_FACTORY); |
| 109 | bind(Logger.class).to(LOGGER_FACTORY); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 110 | } |
| 111 | |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame^] | 112 | final List<Validation> validations = new ArrayList<Validation>(); |
| 113 | |
| 114 | /** |
| 115 | * Registers a type to be validated for injection when we create the |
| 116 | * container. |
| 117 | */ |
| 118 | void validate(final Object source, final Class<?> type) { |
| 119 | validations.add(new Validation() { |
| 120 | public void run(ContainerImpl container) { |
| 121 | ErrorHandler previous = container.getErrorHandler(); |
| 122 | try { |
| 123 | container.setErrorHandler(new ConfigurationErrorHandler(source)); |
| 124 | container.getConstructor(type); |
| 125 | } finally { |
| 126 | container.setErrorHandler(previous); |
| 127 | } |
| 128 | } |
| 129 | }); |
| 130 | } |
| 131 | |
| 132 | interface Validation { |
| 133 | void run(ContainerImpl container); |
| 134 | } |
| 135 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 136 | /** |
crazyboblee | dac4991 | 2007-01-29 23:17:41 +0000 | [diff] [blame] | 137 | * Creates an object pointing to the current location within the |
| 138 | * configuration. If we run into a problem later, we'll be able to trace it |
| 139 | * back to the original source. Useful for debugging. The default |
| 140 | * implementation returns {@code ContainerBuilder}'s caller's {@code |
| 141 | * StackTraceElement}. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 142 | */ |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 143 | protected Object source() { |
| 144 | // Search up the stack until we find a class outside of this one. |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame^] | 145 | for (final StackTraceElement element : new Throwable().getStackTrace()) { |
| 146 | String className = element.getClassName(); |
| 147 | if (!className.equals(ContainerBuilder.class.getName()) |
| 148 | && !className.equals(AbstractModule.class.getName())) |
| 149 | return element.getFileName() + ":" |
| 150 | + element.getLineNumber() + ":"; |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 151 | } |
| 152 | throw new AssertionError(); |
| 153 | } |
| 154 | |
| 155 | /** |
crazyboblee | dac4991 | 2007-01-29 23:17:41 +0000 | [diff] [blame] | 156 | * Maps a {@link Scope} instance to a given scope name. Scopes should be |
| 157 | * mapped before used in bindings. @{@link Scoped#value()} references this |
| 158 | * name. |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 159 | */ |
| 160 | public void put(String name, Scope scope) { |
| 161 | if (scopes.containsKey(nonNull(name, "name"))) { |
| 162 | add(new ErrorMessage(source(), "Scope named '" + name |
| 163 | + "' is already defined.")); |
| 164 | } else { |
| 165 | scopes.put(nonNull(name, "name"), nonNull(scope, "scope")); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 170 | * Binds the given key. |
| 171 | */ |
| 172 | public <T> BindingBuilder<T> bind(Key<T> key) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 173 | ensureNotCreated(); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 174 | BindingBuilder<T> builder = new BindingBuilder<T>(key).from(source()); |
| 175 | bindingBuilders.add(builder); |
| 176 | return builder; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /** |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 180 | * Binds the given type. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 181 | */ |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 182 | public <T> BindingBuilder<T> bind(TypeToken<T> typeToken) { |
| 183 | return bind(Key.get(typeToken)); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 187 | * Binds the given type. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 188 | */ |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 189 | public <T> BindingBuilder<T> bind(Class<T> clazz) { |
| 190 | return bind(Key.get(clazz)); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /** |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 194 | * Links the given key to another key effectively creating an alias for a |
| 195 | * binding. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 196 | */ |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 197 | public <T> LinkedBindingBuilder<T> link(Key<T> key) { |
| 198 | ensureNotCreated(); |
| 199 | LinkedBindingBuilder<T> builder = |
| 200 | new LinkedBindingBuilder<T>(key).from(source()); |
| 201 | linkedBindingBuilders.add(builder); |
| 202 | return builder; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /** |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 206 | * Binds a constant to the given name. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 207 | */ |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 208 | public ConstantBindingBuilder bind(String name) { |
| 209 | ensureNotCreated(); |
| 210 | return bind(name, source()); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /** |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 214 | * Binds a constant to the given name from the given source. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 215 | */ |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 216 | private ConstantBindingBuilder bind(String name, Object source) { |
| 217 | ConstantBindingBuilder builder = |
| 218 | new ConstantBindingBuilder(Objects.nonNull(name, "name")).from(source); |
| 219 | constantBindingBuilders.add(builder); |
| 220 | return builder; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 224 | * Binds a string constant for each property. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 225 | */ |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 226 | public void bindProperties(Map<String, String> properties) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 227 | ensureNotCreated(); |
| 228 | Object source = source(); |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 229 | for (Map.Entry<String, String> entry : properties.entrySet()) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 230 | String key = entry.getKey(); |
| 231 | String value = entry.getValue(); |
| 232 | bind(key, source).to(value); |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 233 | } |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /** |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 237 | * Binds a string constant for each property. |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 238 | */ |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 239 | public void bindProperties(Properties properties) { |
donald.brown | 263c5bc | 2006-11-16 18:39:55 +0000 | [diff] [blame] | 240 | ensureNotCreated(); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 241 | Object source = source(); |
| 242 | for (Map.Entry<Object, Object> entry : properties.entrySet()) { |
| 243 | String key = (String) entry.getKey(); |
| 244 | String value = (String) entry.getValue(); |
| 245 | bind(key, source).to(value); |
donald.brown | 263c5bc | 2006-11-16 18:39:55 +0000 | [diff] [blame] | 246 | } |
donald.brown | 263c5bc | 2006-11-16 18:39:55 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** |
crazyboblee | 4776db7 | 2007-01-29 23:27:30 +0000 | [diff] [blame] | 250 | * Upon successful creation, the {@link Container} will inject static fields |
| 251 | * and methods in the given classes. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 252 | * |
| 253 | * @param types for which static members will be injected |
| 254 | */ |
crazyboblee | 4776db7 | 2007-01-29 23:27:30 +0000 | [diff] [blame] | 255 | public void requestStaticInjection(Class<?>... types) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 256 | staticInjections.addAll(Arrays.asList(types)); |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Applies the given module to this builder. |
| 261 | */ |
| 262 | public void apply(Module module) { |
| 263 | module.configure(this); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /** |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 267 | * Adds an error message to be reported at creation time. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 268 | */ |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 269 | void add(ErrorMessage errorMessage) { |
| 270 | errorMessages.add(errorMessage); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Creates a {@link Container} instance. Injects static members for classes |
crazyboblee | 4776db7 | 2007-01-29 23:27:30 +0000 | [diff] [blame] | 275 | * which were registered using {@link #requestStaticInjection(Class...)}. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 276 | * |
| 277 | * @param loadSingletons If true, the container will load all singletons |
| 278 | * now. If false, the container will lazily load singletons. Eager loading |
| 279 | * is appropriate for production use while lazy loading can speed |
| 280 | * development. |
| 281 | * @throws IllegalStateException if called more than once |
| 282 | */ |
| 283 | public Container create(boolean loadSingletons) { |
| 284 | ensureNotCreated(); |
| 285 | created = true; |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 286 | |
| 287 | HashMap<Key<?>, InternalFactory<?>> factories = |
| 288 | new HashMap<Key<?>, InternalFactory<?>>(); |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 289 | ContainerImpl container = new ContainerImpl(factories); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 290 | |
| 291 | for (ConstantBindingBuilder builder : constantBindingBuilders) { |
| 292 | if (builder.hasValue()) { |
crazyboblee | fc9337f | 2007-01-26 00:51:34 +0000 | [diff] [blame] | 293 | Key<?> key = builder.getKey(); |
| 294 | InternalFactory<?> factory = builder.getInternalFactory(); |
| 295 | factories.put(key, factory); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 296 | } else { |
| 297 | add(new ErrorMessage(builder.getSource(), |
| 298 | "Constant value isn't set.")); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | final List<ContainerImpl.ContextualCallable<Void>> singletonLoaders = |
| 303 | new ArrayList<ContainerImpl.ContextualCallable<Void>>(); |
| 304 | |
| 305 | for (BindingBuilder<?> builder : bindingBuilders) { |
| 306 | final Key<?> key = builder.getKey(); |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 307 | final InternalFactory<?> factory = builder.getInternalFactory(container); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 308 | factories.put(key, factory); |
| 309 | |
| 310 | if (builder.isSingleton()) { |
| 311 | singletonLoaders.add(new ContainerImpl.ContextualCallable<Void>() { |
| 312 | public Void call(InternalContext context) { |
| 313 | context.setExternalContext( |
| 314 | ExternalContext.newInstance(null, key, |
| 315 | context.getContainerImpl())); |
| 316 | try { |
| 317 | factory.get(context); |
| 318 | return null; |
| 319 | } finally { |
| 320 | context.setExternalContext(null); |
| 321 | } |
| 322 | } |
| 323 | }); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | for (LinkedBindingBuilder<?> builder : linkedBindingBuilders) { |
| 328 | // TODO: Support alias to a later-declared alias. |
| 329 | Key<?> destination = builder.getDestination(); |
| 330 | if (destination == null) { |
| 331 | add(new ErrorMessage(builder.getSource(), |
| 332 | "Link destination isn't set.")); |
| 333 | continue; |
| 334 | } |
| 335 | |
| 336 | InternalFactory<?> factory = factories.get(destination); |
| 337 | if (factory == null) { |
| 338 | add(new ErrorMessage(builder.getSource(), |
| 339 | "Destination of link binding not found: " + destination)); |
| 340 | continue; |
| 341 | } |
| 342 | |
| 343 | factories.put(builder.getKey(), factory); |
| 344 | } |
| 345 | |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame^] | 346 | // Run validations. |
| 347 | for (Validation validation : validations) { |
| 348 | validation.run(container); |
| 349 | } |
| 350 | |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 351 | if (!errorMessages.isEmpty()) { |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame^] | 352 | StringBuilder error = new StringBuilder(); |
| 353 | error.append("Configuration errors:\n"); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 354 | for (ErrorMessage errorMessage : errorMessages) { |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame^] | 355 | error.append(errorMessage).append('\n'); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 356 | } |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame^] | 357 | logger.severe(error.toString()); |
| 358 | throw new ConfigurationException("Encountered configuration errors." |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 359 | + " See the log for details."); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 360 | } |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame^] | 361 | container.setErrorHandler(new RuntimeErrorHandler()); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 362 | |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 363 | container.injectStatics(staticInjections); |
| 364 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 365 | if (loadSingletons) { |
| 366 | container.callInContext(new ContainerImpl.ContextualCallable<Void>() { |
| 367 | public Void call(InternalContext context) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 368 | for (ContainerImpl.ContextualCallable<Void> singletonLoader |
| 369 | : singletonLoaders) { |
| 370 | singletonLoader.call(context); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 371 | } |
| 372 | return null; |
| 373 | } |
| 374 | }); |
| 375 | } |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 376 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 377 | return container; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Currently we only support creating one Container instance per builder. |
| 382 | * If we want to support creating more than one container per builder, |
| 383 | * we should move to a "factory factory" model where we create a factory |
| 384 | * instance per Container. Right now, one factory instance would be |
| 385 | * shared across all the containers, singletons synchronize on the |
| 386 | * container when lazy loading, etc. |
| 387 | */ |
| 388 | private void ensureNotCreated() { |
| 389 | if (created) { |
| 390 | throw new IllegalStateException("Container already created."); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /** |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 395 | * Binds a {@link Key} to an implementation in a given scope. |
| 396 | */ |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 397 | public class BindingBuilder<T> { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 398 | |
| 399 | Object source = ContainerBuilder.UNKNOWN_SOURCE; |
| 400 | Key<T> key; |
| 401 | InternalFactory<? extends T> factory; |
| 402 | Scope scope; |
| 403 | |
| 404 | BindingBuilder(Key<T> key) { |
| 405 | this.key = nonNull(key, "key"); |
| 406 | } |
| 407 | |
| 408 | Key<T> getKey() { |
| 409 | return key; |
| 410 | } |
| 411 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 412 | BindingBuilder<T> from(Object source) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 413 | this.source = source; |
| 414 | return this; |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Sets the name of this binding. |
| 419 | */ |
| 420 | public BindingBuilder<T> named(String name) { |
| 421 | if (!this.key.hasDefaultName()) { |
| 422 | add(new ErrorMessage(source, "Name set more than once.")); |
| 423 | } |
| 424 | |
| 425 | this.key = this.key.named(name); |
| 426 | return this; |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Binds to instances of the given implementation class. The {@link |
| 431 | * Container} will inject the implementation instances as well. Sets the |
| 432 | * scope based on the @{@link Scoped} annotation on the implementation |
| 433 | * class if present. |
| 434 | */ |
| 435 | public <I extends T> BindingBuilder<T> to(Class<I> implementation) { |
| 436 | return to(TypeToken.get(implementation)); |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Binds to instances of the given implementation type. The {@link |
| 441 | * Container} will inject the implementation instances as well. Sets the |
| 442 | * scope based on the @{@link Scoped} annotation on the implementation |
| 443 | * class if present. |
| 444 | */ |
| 445 | public <I extends T> BindingBuilder<T> to(TypeToken<I> implementation) { |
| 446 | ensureImplementationIsNotSet(); |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame^] | 447 | validate(source, implementation.getRawType()); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 448 | this.factory = new DefaultFactory<I>(implementation); |
| 449 | setScopeFromType(implementation.getRawType()); |
| 450 | return this; |
| 451 | } |
| 452 | |
| 453 | private void setScopeFromType(Class<?> implementation) { |
| 454 | Scoped scoped = implementation.getAnnotation(Scoped.class); |
| 455 | if (scoped != null) { |
| 456 | in(scoped.value()); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Binds to instances from the given factory. |
| 462 | */ |
| 463 | public BindingBuilder<T> to( |
| 464 | final ContextualFactory<? extends T> factory) { |
| 465 | ensureImplementationIsNotSet(); |
| 466 | |
| 467 | this.factory = new InternalFactory<T>() { |
| 468 | public T get(InternalContext context) { |
| 469 | return factory.get(context.getExternalContext()); |
| 470 | } |
| 471 | |
| 472 | public String toString() { |
| 473 | return factory.toString(); |
| 474 | } |
| 475 | }; |
| 476 | |
| 477 | return this; |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * Binds to instances from the given factory. |
| 482 | */ |
| 483 | public BindingBuilder<T> to(final Factory<? extends T> factory) { |
| 484 | ensureImplementationIsNotSet(); |
| 485 | |
| 486 | this.factory = new InternalFactory<T>() { |
| 487 | public T get(InternalContext context) { |
| 488 | return factory.get(); |
| 489 | } |
| 490 | |
| 491 | public String toString() { |
| 492 | return factory.toString(); |
| 493 | } |
| 494 | }; |
| 495 | |
| 496 | return this; |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Binds to the given instance. |
| 501 | */ |
| 502 | BindingBuilder<T> to(T instance) { |
| 503 | ensureImplementationIsNotSet(); |
| 504 | this.factory = new ConstantFactory<T>(instance); |
| 505 | return this; |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * Binds to instances from the given factory. |
| 510 | */ |
| 511 | BindingBuilder<T> to(final InternalFactory<? extends T> factory) { |
| 512 | ensureImplementationIsNotSet(); |
| 513 | this.factory = factory; |
| 514 | return this; |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * Adds an error message if the implementation has already been bound. |
| 519 | */ |
| 520 | private void ensureImplementationIsNotSet() { |
| 521 | if (factory != null) { |
| 522 | add(new ErrorMessage(source, "Implementation set more than once.")); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /** |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 527 | * Specifies the scope. References the name passed to {@link |
| 528 | * ContainerBuilder#put(String, Scope)}. |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 529 | */ |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 530 | public BindingBuilder<T> in(String scopeName) { |
| 531 | ensureScopeNotSet(); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 532 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 533 | // We could defer this lookup to when we create the container, but this |
| 534 | // is fine for now. |
| 535 | this.scope = scopes.get(scopeName); |
| 536 | if (this.scope == null) { |
| 537 | add(new ErrorMessage(source, "Scope named '" + scopeName |
| 538 | + "' not found.")); |
| 539 | } |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 540 | return this; |
| 541 | } |
| 542 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 543 | /** |
| 544 | * Specifies the scope. |
| 545 | */ |
| 546 | public BindingBuilder<T> in(Scope scope) { |
| 547 | ensureScopeNotSet(); |
| 548 | |
| 549 | this.scope = nonNull(scope, "scope"); |
| 550 | return this; |
| 551 | } |
| 552 | |
| 553 | private void ensureScopeNotSet() { |
| 554 | if (this.scope != null) { |
| 555 | add(new ErrorMessage(source, "Scope set more than once.")); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | InternalFactory<? extends T> getInternalFactory( |
| 560 | final ContainerImpl container) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 561 | // If an implementation wasn't specified, use the injection type. |
| 562 | if (this.factory == null) { |
| 563 | to(key.getTypeToken()); |
| 564 | } |
| 565 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 566 | if (scope == null || scope == DEFAULT_SCOPE) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 567 | return this.factory; |
| 568 | } |
| 569 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 570 | // TODO: This is a little hairy. |
| 571 | final InternalFactory<? extends T> internalFactory = this.factory; |
| 572 | final Factory<T> factory = scope.scope(this.key, new Factory<T>() { |
| 573 | public T get() { |
| 574 | return container.callInContext( |
| 575 | new ContainerImpl.ContextualCallable<T>() { |
| 576 | public T call(InternalContext context) { |
| 577 | return internalFactory.get(context); |
| 578 | } |
| 579 | }); |
| 580 | } |
| 581 | |
| 582 | public String toString() { |
| 583 | return internalFactory.toString(); |
| 584 | } |
| 585 | }); |
| 586 | |
| 587 | return new InternalFactory<T>() { |
| 588 | public T get(InternalContext context) { |
| 589 | return factory.get(); |
| 590 | } |
| 591 | |
| 592 | public String toString() { |
| 593 | return factory.toString(); |
| 594 | } |
| 595 | }; |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | boolean isSingleton() { |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 599 | return this.scope == SingletonScope.INSTANCE; |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /** |
| 603 | * Injects new instances of the specified implementation class. |
| 604 | */ |
| 605 | private class DefaultFactory<I extends T> implements InternalFactory<I> { |
| 606 | |
| 607 | volatile ContainerImpl.ConstructorInjector<I> constructor; |
| 608 | |
| 609 | private final TypeToken<I> implementation; |
| 610 | |
| 611 | public DefaultFactory(TypeToken<I> implementation) { |
| 612 | // TODO: Ensure this is a concrete implementation. |
| 613 | this.implementation = implementation; |
| 614 | } |
| 615 | |
| 616 | @SuppressWarnings("unchecked") |
| 617 | public I get(InternalContext context) { |
| 618 | if (constructor == null) { |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame^] | 619 | // This unnecessary cast is a workaround for an annoying compiler |
| 620 | // bug I keep running into. |
| 621 | Object c = context.getContainerImpl().getConstructor(implementation); |
| 622 | this.constructor = (ContainerImpl.ConstructorInjector<I>) c; |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 623 | } |
| 624 | return (I) constructor.construct(context, key.getRawType()); |
| 625 | } |
| 626 | |
| 627 | public String toString() { |
| 628 | return implementation.toString(); |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Builds a constant binding. |
| 635 | */ |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 636 | public class ConstantBindingBuilder { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 637 | |
| 638 | final String name; |
| 639 | Class<?> type; |
| 640 | Object value; |
| 641 | Object source = ContainerBuilder.UNKNOWN_SOURCE; |
| 642 | |
| 643 | ConstantBindingBuilder(String name) { |
| 644 | this.name = name; |
| 645 | } |
| 646 | |
| 647 | Key<?> getKey() { |
| 648 | return Key.get(type, name); |
| 649 | } |
| 650 | |
| 651 | boolean hasValue() { |
| 652 | return type != null; |
| 653 | } |
| 654 | |
| 655 | Object getSource() { |
| 656 | return source; |
| 657 | } |
| 658 | |
| 659 | InternalFactory<?> getInternalFactory() { |
| 660 | return new ConstantFactory<Object>(value); |
| 661 | } |
| 662 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 663 | ConstantBindingBuilder from(Object source) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 664 | this.source = source; |
| 665 | return this; |
| 666 | } |
| 667 | |
| 668 | /** |
| 669 | * Binds constant to the given value. |
| 670 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 671 | public void to(String value) { |
| 672 | to(String.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /** |
| 676 | * Binds constant to the given value. |
| 677 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 678 | public void to(int value) { |
| 679 | to(int.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /** |
| 683 | * Binds constant to the given value. |
| 684 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 685 | public void to(long value) { |
| 686 | to(long.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /** |
| 690 | * Binds constant to the given value. |
| 691 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 692 | public void to(boolean value) { |
| 693 | to(boolean.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | /** |
| 697 | * Binds constant to the given value. |
| 698 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 699 | public void to(double value) { |
| 700 | to(double.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | /** |
| 704 | * Binds constant to the given value. |
| 705 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 706 | public void to(float value) { |
| 707 | to(float.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /** |
| 711 | * Binds constant to the given value. |
| 712 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 713 | public void to(short value) { |
| 714 | to(short.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | /** |
| 718 | * Binds constant to the given value. |
| 719 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 720 | public void to(char value) { |
| 721 | to(char.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | /** |
| 725 | * Binds constant to the given value. |
| 726 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 727 | public void to(Class<?> value) { |
| 728 | to(Class.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | /** |
| 732 | * Binds constant to the given value. |
| 733 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 734 | public <E extends Enum<E>> void to(E value) { |
| 735 | to(value.getDeclaringClass(), value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | /** |
| 739 | * Maps a constant value to the given type and name. |
| 740 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 741 | <T> void to(final Class<T> type, final T value) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 742 | this.type = type; |
| 743 | this.value = value; |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | |
| 747 | /** |
| 748 | * Links one binding to another. |
| 749 | */ |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 750 | public class LinkedBindingBuilder<T> { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 751 | |
| 752 | Key<T> key; |
| 753 | Key<? extends T> destination; |
| 754 | Object source = ContainerBuilder.UNKNOWN_SOURCE; |
| 755 | |
| 756 | LinkedBindingBuilder(Key<T> key) { |
| 757 | this.key = key; |
| 758 | } |
| 759 | |
| 760 | Object getSource() { |
| 761 | return source; |
| 762 | } |
| 763 | |
| 764 | Key<T> getKey() { |
| 765 | return key; |
| 766 | } |
| 767 | |
| 768 | Key<? extends T> getDestination() { |
| 769 | return destination; |
| 770 | } |
| 771 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 772 | LinkedBindingBuilder<T> from(Object source) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 773 | this.source = source; |
| 774 | return this; |
| 775 | } |
| 776 | |
| 777 | /** |
| 778 | * Links to another binding with the given key. |
| 779 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 780 | public void to(Key<? extends T> destination) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 781 | this.destination = destination; |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 782 | } |
| 783 | } |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame^] | 784 | |
| 785 | class ConfigurationErrorHandler implements ErrorHandler { |
| 786 | |
| 787 | final Object source; |
| 788 | |
| 789 | ConfigurationErrorHandler(Object source) { |
| 790 | this.source = source; |
| 791 | } |
| 792 | |
| 793 | public void handle(String message) { |
| 794 | add(new ErrorMessage(source, message)); |
| 795 | } |
| 796 | |
| 797 | public void handle(Throwable t) { |
| 798 | add(new ErrorMessage(source, t.getMessage())); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | class RuntimeErrorHandler implements ErrorHandler { |
| 803 | |
| 804 | public void handle(String message) { |
| 805 | throw new ConfigurationException(message); |
| 806 | } |
| 807 | |
| 808 | public void handle(Throwable t) { |
| 809 | throw new ConfigurationException(t); |
| 810 | } |
| 811 | } |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 812 | } |