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; |
crazyboblee | ef83bd2 | 2007-02-01 00:52:57 +0000 | [diff] [blame] | 20 | import com.google.inject.util.Stopwatch; |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame^] | 21 | import com.google.inject.util.ToStringBuilder; |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 22 | import static com.google.inject.util.Objects.nonNull; |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame^] | 23 | import com.google.inject.spi.ConstructionProxyFactory; |
| 24 | import com.google.inject.spi.DefaultConstructionProxyFactory; |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 25 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 26 | import java.lang.reflect.Member; |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.Arrays; |
| 29 | import java.util.HashMap; |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 30 | import java.util.LinkedHashSet; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 31 | import java.util.List; |
| 32 | import java.util.Map; |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 33 | import java.util.Properties; |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 34 | import java.util.Set; |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 35 | import java.util.HashSet; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 36 | import java.util.logging.Logger; |
| 37 | |
| 38 | /** |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 39 | * Builds a dependency injection {@link Container}. Binds {@link Key}s to |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 40 | * implementations. A binding implementation could be anything from a constant |
| 41 | * value to an object in the HTTP session. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 42 | * |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 43 | * <p>Not safe for concurrent use. |
| 44 | * |
| 45 | * <p>Default bindings include: |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 46 | * |
| 47 | * <ul> |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 48 | * <li>A {@code Factory<T>} for each binding of type {@code T} |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 49 | * <li>The {@link Container} iself |
| 50 | * <li>The {@link Logger} for the class being injected |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 51 | * </ul> |
| 52 | * |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 53 | * <p>Converts constants as needed from {@code String} to any primitive type |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 54 | * in addition to {@code enum} and {@code Class<?>}. |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 55 | * |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 56 | * @author crazybob@google.com (Bob Lee) |
| 57 | */ |
| 58 | public final class ContainerBuilder { |
| 59 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 60 | private static final Logger logger = |
| 61 | Logger.getLogger(ContainerBuilder.class.getName()); |
| 62 | |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 63 | final List<BindingBuilder<?>> bindingBuilders = |
| 64 | new ArrayList<BindingBuilder<?>>(); |
| 65 | final List<ConstantBindingBuilder> constantBindingBuilders = |
| 66 | new ArrayList<ConstantBindingBuilder>(); |
| 67 | final List<LinkedBindingBuilder<?>> linkedBindingBuilders = |
| 68 | new ArrayList<LinkedBindingBuilder<?>>(); |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 69 | final Map<String, Scope> scopes = new HashMap<String, Scope>(); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 70 | |
crazyboblee | ef83bd2 | 2007-02-01 00:52:57 +0000 | [diff] [blame] | 71 | final List<StaticInjection> staticInjections = |
| 72 | new ArrayList<StaticInjection>(); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 73 | |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 74 | ContainerImpl container; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 75 | |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 76 | /** |
| 77 | * Keeps error messages in order and prevents duplicates. |
| 78 | */ |
| 79 | Set<ErrorMessage> errorMessages = new LinkedHashSet<ErrorMessage>(); |
| 80 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 81 | private static final InternalFactory<Container> CONTAINER_FACTORY = |
| 82 | new InternalFactory<Container>() { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 83 | public Container get(InternalContext context) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 84 | return context.getContainer(); |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | private static final InternalFactory<Logger> LOGGER_FACTORY = |
| 89 | new InternalFactory<Logger>() { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 90 | public Logger get(InternalContext context) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 91 | Member member = context.getExternalContext().getMember(); |
| 92 | return member == null ? Logger.getAnonymousLogger() |
| 93 | : Logger.getLogger(member.getDeclaringClass().getName()); |
| 94 | } |
| 95 | }; |
| 96 | |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 97 | static final String UNKNOWN_SOURCE = "[unknown source]"; |
| 98 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 99 | static final Scope DEFAULT_SCOPE = new Scope() { |
| 100 | public <T> Factory<T> scope(Key<T> key, Factory<T> creator) { |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 101 | // We special case optimize default scope, so this never actually runs. |
| 102 | throw new AssertionError(); |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 103 | } |
| 104 | }; |
| 105 | |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame^] | 106 | final ConstructionProxyFactory constructionProxyFactory; |
| 107 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 108 | /** |
| 109 | * Constructs a new builder. |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame^] | 110 | * |
| 111 | * @param constructionProxyFactory to use when constructing objects |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 112 | */ |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame^] | 113 | public ContainerBuilder(ConstructionProxyFactory constructionProxyFactory) { |
crazyboblee | 2af0637 | 2007-02-01 02:07:53 +0000 | [diff] [blame] | 114 | put(Scopes.DEFAULT_SCOPE, DEFAULT_SCOPE); |
| 115 | put(Scopes.CONTAINER_SCOPE, ContainerScope.INSTANCE); |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 116 | |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 117 | bind(Container.class).to(CONTAINER_FACTORY); |
| 118 | bind(Logger.class).to(LOGGER_FACTORY); |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame^] | 119 | |
| 120 | this.constructionProxyFactory = nonNull(constructionProxyFactory, |
| 121 | "construction proxy factory"); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Constructs a new builder. |
| 126 | */ |
| 127 | public ContainerBuilder() { |
| 128 | this(new DefaultConstructionProxyFactory()); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 129 | } |
| 130 | |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 131 | final List<Validation> validations = new ArrayList<Validation>(); |
| 132 | |
| 133 | /** |
| 134 | * Registers a type to be validated for injection when we create the |
| 135 | * container. |
| 136 | */ |
| 137 | void validate(final Object source, final Class<?> type) { |
| 138 | validations.add(new Validation() { |
crazyboblee | ef83bd2 | 2007-02-01 00:52:57 +0000 | [diff] [blame] | 139 | public void run(final ContainerImpl container) { |
| 140 | container.withErrorHandler(new ConfigurationErrorHandler(source), |
| 141 | new Runnable() { |
| 142 | public void run() { |
| 143 | container.getConstructor(type); |
| 144 | } |
| 145 | }); |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 146 | } |
| 147 | }); |
| 148 | } |
| 149 | |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 150 | /** |
| 151 | * A validation command to run after we create the container but before |
| 152 | * we return it to the client. |
| 153 | */ |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 154 | interface Validation { |
| 155 | void run(ContainerImpl container); |
| 156 | } |
| 157 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 158 | /** |
crazyboblee | dac4991 | 2007-01-29 23:17:41 +0000 | [diff] [blame] | 159 | * Maps a {@link Scope} instance to a given scope name. Scopes should be |
| 160 | * mapped before used in bindings. @{@link Scoped#value()} references this |
| 161 | * name. |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 162 | */ |
| 163 | public void put(String name, Scope scope) { |
| 164 | if (scopes.containsKey(nonNull(name, "name"))) { |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 165 | addError(source(), ErrorMessage.DUPLICATE_SCOPES, name); |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 166 | } else { |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 167 | scopes.put(nonNull(name, "name"), nonNull(scope, "scope")); |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 172 | * Binds the given key. |
| 173 | */ |
| 174 | public <T> BindingBuilder<T> bind(Key<T> key) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 175 | ensureNotCreated(); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 176 | BindingBuilder<T> builder = new BindingBuilder<T>(key).from(source()); |
| 177 | bindingBuilders.add(builder); |
| 178 | return builder; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /** |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 182 | * Binds the given type. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 183 | */ |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 184 | public <T> BindingBuilder<T> bind(TypeLiteral<T> typeLiteral) { |
| 185 | return bind(Key.get(typeLiteral)); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /** |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 189 | * Binds the given type. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 190 | */ |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 191 | public <T> BindingBuilder<T> bind(Class<T> clazz) { |
| 192 | return bind(Key.get(clazz)); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /** |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 196 | * Links the given key to another key effectively creating an alias for a |
| 197 | * binding. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 198 | */ |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 199 | public <T> LinkedBindingBuilder<T> link(Key<T> key) { |
| 200 | ensureNotCreated(); |
| 201 | LinkedBindingBuilder<T> builder = |
| 202 | new LinkedBindingBuilder<T>(key).from(source()); |
| 203 | linkedBindingBuilders.add(builder); |
| 204 | return builder; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /** |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 208 | * Binds a constant to the given name. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 209 | */ |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 210 | public ConstantBindingBuilder bind(String name) { |
| 211 | ensureNotCreated(); |
| 212 | return bind(name, source()); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 216 | * Binds a constant to the given name from the given source. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 217 | */ |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 218 | private ConstantBindingBuilder bind(String name, Object source) { |
| 219 | ConstantBindingBuilder builder = |
| 220 | new ConstantBindingBuilder(Objects.nonNull(name, "name")).from(source); |
| 221 | constantBindingBuilders.add(builder); |
| 222 | return builder; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /** |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 226 | * Binds a string constant for each property. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 227 | */ |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 228 | public void bindProperties(Map<String, String> properties) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 229 | ensureNotCreated(); |
| 230 | Object source = source(); |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 231 | for (Map.Entry<String, String> entry : properties.entrySet()) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 232 | String key = entry.getKey(); |
| 233 | String value = entry.getValue(); |
| 234 | bind(key, source).to(value); |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 235 | } |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /** |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 239 | * Binds a string constant for each property. |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 240 | */ |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 241 | public void bindProperties(Properties properties) { |
donald.brown | 263c5bc | 2006-11-16 18:39:55 +0000 | [diff] [blame] | 242 | ensureNotCreated(); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 243 | Object source = source(); |
| 244 | for (Map.Entry<Object, Object> entry : properties.entrySet()) { |
| 245 | String key = (String) entry.getKey(); |
| 246 | String value = (String) entry.getValue(); |
| 247 | bind(key, source).to(value); |
donald.brown | 263c5bc | 2006-11-16 18:39:55 +0000 | [diff] [blame] | 248 | } |
donald.brown | 263c5bc | 2006-11-16 18:39:55 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /** |
crazyboblee | 4776db7 | 2007-01-29 23:27:30 +0000 | [diff] [blame] | 252 | * Upon successful creation, the {@link Container} will inject static fields |
| 253 | * and methods in the given classes. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 254 | * |
| 255 | * @param types for which static members will be injected |
| 256 | */ |
crazyboblee | 4776db7 | 2007-01-29 23:27:30 +0000 | [diff] [blame] | 257 | public void requestStaticInjection(Class<?>... types) { |
crazyboblee | ef83bd2 | 2007-02-01 00:52:57 +0000 | [diff] [blame] | 258 | staticInjections.add(new StaticInjection(source(), types)); |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Applies the given module to this builder. |
| 263 | */ |
| 264 | public void apply(Module module) { |
| 265 | module.configure(this); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 266 | } |
| 267 | |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 268 | void addError(Object source, String message, Object... arguments) { |
| 269 | new ConfigurationErrorHandler(source).handle(message, arguments); |
| 270 | } |
| 271 | |
| 272 | void addError(Object source, String message) { |
| 273 | new ConfigurationErrorHandler(source).handle(message); |
| 274 | } |
| 275 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 276 | /** |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 277 | * Adds an error message to be reported at creation time. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 278 | */ |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 279 | void add(ErrorMessage errorMessage) { |
| 280 | errorMessages.add(errorMessage); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 281 | } |
| 282 | |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 283 | Stopwatch stopwatch = new Stopwatch(); |
| 284 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 285 | /** |
| 286 | * Creates a {@link Container} instance. Injects static members for classes |
crazyboblee | 4776db7 | 2007-01-29 23:27:30 +0000 | [diff] [blame] | 287 | * which were registered using {@link #requestStaticInjection(Class...)}. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 288 | * |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 289 | * @param preload If true, the container will load all container-scoped |
| 290 | * bindings now. If false, the container will lazily load them. Eager |
| 291 | * loading is appropriate for production use (catch errors early and take |
| 292 | * any performance hit up front) while lazy loading can speed development. |
| 293 | * |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 294 | * @throws ContainerCreationException if configuration errors are found. The |
| 295 | * expectation is that the application will log this exception and exit. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 296 | * @throws IllegalStateException if called more than once |
| 297 | */ |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 298 | public synchronized Container create(boolean preload) |
| 299 | throws ContainerCreationException { |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 300 | stopwatch.resetAndLog(logger, "Configuration"); |
| 301 | |
| 302 | // Create the container. |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 303 | ensureNotCreated(); |
| 304 | Map<Key<?>, Binding<?>> bindings = |
| 305 | new HashMap<Key<?>, Binding<?>>(); |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame^] | 306 | container = new ContainerImpl(constructionProxyFactory, bindings); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 307 | |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 308 | createConstantBindings(); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 309 | |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 310 | // Commands to execute before returning the Container instance. |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 311 | final List<ContextualCallable<Void>> preloaders = |
| 312 | new ArrayList<ContextualCallable<Void>>(); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 313 | |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 314 | createBindings(preload, preloaders); |
| 315 | createLinkedBindings(); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 316 | |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 317 | stopwatch.resetAndLog(logger, "Binding creation"); |
| 318 | |
| 319 | // Run validations. |
| 320 | for (Validation validation : validations) { |
| 321 | validation.run(container); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 322 | } |
| 323 | |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 324 | stopwatch.resetAndLog(logger, "Validation"); |
| 325 | |
crazyboblee | ef83bd2 | 2007-02-01 00:52:57 +0000 | [diff] [blame] | 326 | for (StaticInjection staticInjection : staticInjections) { |
| 327 | staticInjection.createInjectors(container); |
| 328 | } |
| 329 | |
| 330 | stopwatch.resetAndLog(logger, "Static validation"); |
| 331 | |
crazyboblee | abc4dd0 | 2007-02-01 01:44:36 +0000 | [diff] [blame] | 332 | // Blow up if we encountered errors. |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 333 | if (!errorMessages.isEmpty()) { |
| 334 | throw new ContainerCreationException(createErrorMessage()); |
| 335 | } |
| 336 | |
| 337 | // Switch to runtime error handling. |
| 338 | container.setErrorHandler(new RuntimeErrorHandler()); |
| 339 | |
| 340 | // Inject static members. |
crazyboblee | ef83bd2 | 2007-02-01 00:52:57 +0000 | [diff] [blame] | 341 | for (StaticInjection staticInjection : staticInjections) { |
| 342 | staticInjection.runInjectors(container); |
| 343 | } |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 344 | |
| 345 | stopwatch.resetAndLog(logger, "Static member injection"); |
| 346 | |
| 347 | // Run preloading commands. |
crazyboblee | abc4dd0 | 2007-02-01 01:44:36 +0000 | [diff] [blame] | 348 | runPreloaders(container, preloaders); |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 349 | |
| 350 | stopwatch.resetAndLog(logger, "Preloading"); |
| 351 | |
| 352 | return container; |
| 353 | } |
| 354 | |
crazyboblee | abc4dd0 | 2007-02-01 01:44:36 +0000 | [diff] [blame] | 355 | private void runPreloaders(ContainerImpl container, |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 356 | final List<ContextualCallable<Void>> preloaders) { |
| 357 | container.callInContext(new ContextualCallable<Void>() { |
crazyboblee | abc4dd0 | 2007-02-01 01:44:36 +0000 | [diff] [blame] | 358 | public Void call(InternalContext context) { |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 359 | for (ContextualCallable<Void> preloader : preloaders) { |
crazyboblee | abc4dd0 | 2007-02-01 01:44:36 +0000 | [diff] [blame] | 360 | preloader.call(context); |
| 361 | } |
| 362 | return null; |
| 363 | } |
| 364 | }); |
| 365 | } |
| 366 | |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 367 | private String createErrorMessage() { |
| 368 | StringBuilder error = new StringBuilder(); |
| 369 | error.append("Guice configuration errors:\n\n"); |
| 370 | int index = 1; |
| 371 | for (ErrorMessage errorMessage : errorMessages) { |
| 372 | error.append(index++) |
| 373 | .append(") ") |
| 374 | .append("Error at ") |
| 375 | .append(errorMessage.getSource()) |
| 376 | .append(':') |
| 377 | .append('\n') |
| 378 | .append(" ") |
| 379 | .append(errorMessage.getMessage()) |
| 380 | .append("\n\n"); |
| 381 | } |
| 382 | error.append(errorMessages.size()).append(" error[s]\n"); |
| 383 | return error.toString(); |
| 384 | } |
| 385 | |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 386 | private void createLinkedBindings() { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 387 | for (LinkedBindingBuilder<?> builder : linkedBindingBuilders) { |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 388 | createLinkedBinding(builder); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 389 | } |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 390 | } |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 391 | |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 392 | private <T> void createLinkedBinding( |
| 393 | LinkedBindingBuilder<T> builder) { |
| 394 | // TODO: Support linking to a later-declared link? |
| 395 | Key<? extends T> destinationKey = builder.getDestination(); |
| 396 | if (destinationKey == null) { |
| 397 | addError(builder.getSource(), ErrorMessage.MISSING_LINK_DESTINATION); |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | Binding<? extends T> destination = getBinding(destinationKey); |
| 402 | if (destination == null) { |
| 403 | addError(builder.getSource(), ErrorMessage.LINK_DESTINATION_NOT_FOUND, |
| 404 | destinationKey); |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | Binding<?> binding = |
| 409 | Binding.newInstance(container, builder.getKey(), builder.getSource(), |
| 410 | destination.getInternalFactory()); |
| 411 | |
| 412 | putBinding(binding); |
| 413 | } |
| 414 | |
| 415 | @SuppressWarnings({"unchecked"}) |
| 416 | private <T> Binding<T> getBinding(Key<T> destinationKey) { |
| 417 | return (Binding<T>) container.internalBindings().get(destinationKey); |
| 418 | } |
| 419 | |
| 420 | private void createBindings(boolean preload, |
| 421 | List<ContextualCallable<Void>> preloaders) { |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 422 | for (BindingBuilder<?> builder : bindingBuilders) { |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 423 | createBinding(builder, preload, preloaders); |
| 424 | } |
| 425 | } |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 426 | |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 427 | private <T> void createBinding(BindingBuilder<T> builder, |
| 428 | boolean preload, List<ContextualCallable<Void>> preloaders) { |
| 429 | final Key<T> key = builder.getKey(); |
| 430 | final InternalFactory<? extends T> factory = |
| 431 | builder.getInternalFactory(container); |
| 432 | Binding<?> binding = Binding.newInstance( |
| 433 | container, key, builder.getSource(), factory); |
| 434 | |
| 435 | putBinding(binding); |
| 436 | |
| 437 | // Register to preload if necessary. |
| 438 | if (builder.isInContainerScope()) { |
| 439 | if (preload || builder.shouldPreload()) { |
| 440 | preloaders.add(new BindingPreloader(key, factory)); |
| 441 | } |
| 442 | } else { |
| 443 | if (builder.shouldPreload()) { |
| 444 | addError(builder.getSource(), ErrorMessage.PRELOAD_NOT_ALLOWED); |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 445 | } |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 446 | } |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 447 | } |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 448 | |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 449 | private void createConstantBindings() { |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 450 | for (ConstantBindingBuilder builder : constantBindingBuilders) { |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 451 | createConstantBinding(builder); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | @SuppressWarnings({"unchecked"}) |
| 456 | private <T> void createConstantBinding(ConstantBindingBuilder builder) { |
| 457 | if (builder.hasValue()) { |
| 458 | Key<T> key = (Key<T>) builder.getKey(); |
| 459 | InternalFactory<? extends T> factory = |
| 460 | (InternalFactory<? extends T>) builder.getInternalFactory(); |
| 461 | Binding<?> binding = |
| 462 | Binding.newInstance(container, key, builder.getSource(), factory); |
| 463 | putBinding(binding); |
| 464 | } else { |
| 465 | addError(builder.getSource(), ErrorMessage.MISSING_CONSTANT_VALUE); |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 466 | } |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 467 | } |
| 468 | |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 469 | void putFactory(Object source, Map<Key<?>, InternalFactory<?>> factories, |
| 470 | Key<?> key, InternalFactory<?> factory) { |
| 471 | if (factories.containsKey(key)) { |
| 472 | addError(source, ErrorMessage.BINDING_ALREADY_SET, key); |
| 473 | } else { |
| 474 | factories.put(key, factory); |
| 475 | } |
| 476 | } |
| 477 | |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 478 | void putBinding(Binding<?> binding) { |
| 479 | Key<?> key = binding.getKey(); |
| 480 | Map<Key<?>, Binding<?>> bindings = container.internalBindings(); |
| 481 | Binding<?> original = bindings.get(key); |
| 482 | if (bindings.containsKey(key)) { |
| 483 | addError(binding.getSource(), ErrorMessage.BINDING_ALREADY_SET, key, |
| 484 | original.getSource()); |
| 485 | } else { |
| 486 | bindings.put(key, binding); |
| 487 | } |
| 488 | } |
| 489 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 490 | /** |
| 491 | * Currently we only support creating one Container instance per builder. |
| 492 | * If we want to support creating more than one container per builder, |
| 493 | * we should move to a "factory factory" model where we create a factory |
| 494 | * instance per Container. Right now, one factory instance would be |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 495 | * shared across all the containers, which means container-scoped objects |
| 496 | * would be shared, etc. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 497 | */ |
| 498 | private void ensureNotCreated() { |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 499 | if (container != null) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 500 | throw new IllegalStateException("Container already created."); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | /** |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 505 | * Binds a {@link Key} to an implementation in a given scope. |
| 506 | */ |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 507 | public class BindingBuilder<T> { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 508 | |
| 509 | Object source = ContainerBuilder.UNKNOWN_SOURCE; |
| 510 | Key<T> key; |
| 511 | InternalFactory<? extends T> factory; |
| 512 | Scope scope; |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 513 | String scopeName; |
crazyboblee | abc4dd0 | 2007-02-01 01:44:36 +0000 | [diff] [blame] | 514 | boolean preload = false; |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 515 | |
| 516 | BindingBuilder(Key<T> key) { |
| 517 | this.key = nonNull(key, "key"); |
| 518 | } |
| 519 | |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 520 | Object getSource() { |
| 521 | return source; |
| 522 | } |
| 523 | |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 524 | Key<T> getKey() { |
| 525 | return key; |
| 526 | } |
| 527 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 528 | BindingBuilder<T> from(Object source) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 529 | this.source = source; |
| 530 | return this; |
| 531 | } |
| 532 | |
| 533 | /** |
| 534 | * Sets the name of this binding. |
| 535 | */ |
| 536 | public BindingBuilder<T> named(String name) { |
| 537 | if (!this.key.hasDefaultName()) { |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 538 | addError(source, ErrorMessage.NAME_ALREADY_SET); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | this.key = this.key.named(name); |
| 542 | return this; |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Binds to instances of the given implementation class. The {@link |
| 547 | * Container} will inject the implementation instances as well. Sets the |
| 548 | * scope based on the @{@link Scoped} annotation on the implementation |
| 549 | * class if present. |
| 550 | */ |
| 551 | public <I extends T> BindingBuilder<T> to(Class<I> implementation) { |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 552 | return to(TypeLiteral.get(implementation)); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | /** |
| 556 | * Binds to instances of the given implementation type. The {@link |
| 557 | * Container} will inject the implementation instances as well. Sets the |
| 558 | * scope based on the @{@link Scoped} annotation on the implementation |
| 559 | * class if present. |
| 560 | */ |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 561 | public <I extends T> BindingBuilder<T> to(TypeLiteral<I> implementation) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 562 | ensureImplementationIsNotSet(); |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 563 | validate(source, implementation.getRawType()); |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 564 | this.factory = new DefaultFactory<I>(key, implementation); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 565 | setScopeFromType(implementation.getRawType()); |
| 566 | return this; |
| 567 | } |
| 568 | |
| 569 | private void setScopeFromType(Class<?> implementation) { |
| 570 | Scoped scoped = implementation.getAnnotation(Scoped.class); |
| 571 | if (scoped != null) { |
| 572 | in(scoped.value()); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * Binds to instances from the given factory. |
| 578 | */ |
| 579 | public BindingBuilder<T> to( |
| 580 | final ContextualFactory<? extends T> factory) { |
| 581 | ensureImplementationIsNotSet(); |
| 582 | |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 583 | this.factory = new InternalToContextualFactoryAdapter<T>(factory); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 584 | |
| 585 | return this; |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * Binds to instances from the given factory. |
| 590 | */ |
| 591 | public BindingBuilder<T> to(final Factory<? extends T> factory) { |
| 592 | ensureImplementationIsNotSet(); |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 593 | this.factory = new InternalFactoryToFactoryAdapter<T>(factory); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 594 | return this; |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * Binds to the given instance. |
| 599 | */ |
| 600 | BindingBuilder<T> to(T instance) { |
| 601 | ensureImplementationIsNotSet(); |
| 602 | this.factory = new ConstantFactory<T>(instance); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 603 | in(Scopes.CONTAINER_SCOPE); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 604 | return this; |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * Binds to instances from the given factory. |
| 609 | */ |
| 610 | BindingBuilder<T> to(final InternalFactory<? extends T> factory) { |
| 611 | ensureImplementationIsNotSet(); |
| 612 | this.factory = factory; |
| 613 | return this; |
| 614 | } |
| 615 | |
| 616 | /** |
| 617 | * Adds an error message if the implementation has already been bound. |
| 618 | */ |
| 619 | private void ensureImplementationIsNotSet() { |
| 620 | if (factory != null) { |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 621 | addError(source, ErrorMessage.IMPLEMENTATION_ALREADY_SET); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
| 625 | /** |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 626 | * Specifies the scope. References the name passed to {@link |
| 627 | * ContainerBuilder#put(String, Scope)}. |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 628 | */ |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 629 | public BindingBuilder<T> in(String scopeName) { |
| 630 | ensureScopeNotSet(); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 631 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 632 | // We could defer this lookup to when we create the container, but this |
| 633 | // is fine for now. |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 634 | this.scope = scopes.get(nonNull(scopeName, "scope name")); |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 635 | if (this.scope == null) { |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 636 | addError(source, ErrorMessage.SCOPE_NOT_FOUND, scopeName, |
| 637 | scopes.keySet()); |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 638 | } |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 639 | return this; |
| 640 | } |
| 641 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 642 | /** |
| 643 | * Specifies the scope. |
| 644 | */ |
| 645 | public BindingBuilder<T> in(Scope scope) { |
| 646 | ensureScopeNotSet(); |
| 647 | |
| 648 | this.scope = nonNull(scope, "scope"); |
| 649 | return this; |
| 650 | } |
| 651 | |
crazyboblee | 2af0637 | 2007-02-01 02:07:53 +0000 | [diff] [blame] | 652 | /** |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 653 | * Specifies container scope (i.e. one instance per container). |
crazyboblee | 2af0637 | 2007-02-01 02:07:53 +0000 | [diff] [blame] | 654 | */ |
| 655 | public BindingBuilder<T> inContainerScope() { |
| 656 | return in(Scopes.CONTAINER_SCOPE); |
| 657 | } |
| 658 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 659 | private void ensureScopeNotSet() { |
| 660 | if (this.scope != null) { |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 661 | addError(source, ErrorMessage.SCOPE_ALREADY_SET); |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
crazyboblee | abc4dd0 | 2007-02-01 01:44:36 +0000 | [diff] [blame] | 665 | /** |
| 666 | * Instructs the builder to eagerly load this binding when it creates |
| 667 | * the container. Useful for application initialization logic. Currently |
| 668 | * only supported for container-scoped bindings. |
| 669 | */ |
| 670 | public BindingBuilder<T> preload() { |
| 671 | this.preload = true; |
| 672 | return this; |
| 673 | } |
| 674 | |
| 675 | boolean shouldPreload() { |
| 676 | return preload; |
| 677 | } |
| 678 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 679 | InternalFactory<? extends T> getInternalFactory( |
| 680 | final ContainerImpl container) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 681 | // If an implementation wasn't specified, use the injection type. |
| 682 | if (this.factory == null) { |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 683 | to(key.getType()); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 684 | } |
| 685 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 686 | if (scope == null || scope == DEFAULT_SCOPE) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 687 | return this.factory; |
| 688 | } |
| 689 | |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 690 | Factory<T> scoped = scope.scope(this.key, |
| 691 | new FactoryToInternalFactoryAdapter<T>(container, this.factory)); |
| 692 | return new InternalFactoryToFactoryAdapter<T>(scoped); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 693 | } |
| 694 | |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 695 | boolean isInContainerScope() { |
| 696 | return this.scope == ContainerScope.INSTANCE; |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 697 | } |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 698 | } |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 699 | |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 700 | /** |
| 701 | * Injects new instances of the specified implementation class. |
| 702 | */ |
| 703 | private static class DefaultFactory<T> implements InternalFactory<T> { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 704 | |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame^] | 705 | volatile ConstructorInjector<T> constructor; |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 706 | |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 707 | private final TypeLiteral<T> implementation; |
| 708 | private final Key<? super T> key; |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 709 | |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 710 | public DefaultFactory(Key<? super T> key, TypeLiteral<T> implementation) { |
| 711 | this.key = key; |
| 712 | this.implementation = implementation; |
| 713 | } |
| 714 | |
| 715 | @SuppressWarnings("unchecked") |
| 716 | public T get(InternalContext context) { |
| 717 | if (constructor == null) { |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame^] | 718 | this.constructor = |
| 719 | context.getContainerImpl().getConstructor(implementation); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 720 | } |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 721 | return (T) constructor.construct(context, key.getRawType()); |
| 722 | } |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 723 | |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 724 | public String toString() { |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame^] | 725 | return new ToStringBuilder(Factory.class) |
| 726 | .add("implementation", implementation) |
| 727 | .toString(); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 728 | } |
| 729 | } |
| 730 | |
| 731 | /** |
| 732 | * Builds a constant binding. |
| 733 | */ |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 734 | public class ConstantBindingBuilder { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 735 | |
| 736 | final String name; |
| 737 | Class<?> type; |
| 738 | Object value; |
| 739 | Object source = ContainerBuilder.UNKNOWN_SOURCE; |
| 740 | |
| 741 | ConstantBindingBuilder(String name) { |
| 742 | this.name = name; |
| 743 | } |
| 744 | |
| 745 | Key<?> getKey() { |
| 746 | return Key.get(type, name); |
| 747 | } |
| 748 | |
| 749 | boolean hasValue() { |
| 750 | return type != null; |
| 751 | } |
| 752 | |
| 753 | Object getSource() { |
| 754 | return source; |
| 755 | } |
| 756 | |
| 757 | InternalFactory<?> getInternalFactory() { |
| 758 | return new ConstantFactory<Object>(value); |
| 759 | } |
| 760 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 761 | ConstantBindingBuilder from(Object source) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 762 | this.source = source; |
| 763 | return this; |
| 764 | } |
| 765 | |
| 766 | /** |
| 767 | * Binds constant to the given value. |
| 768 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 769 | public void to(String value) { |
| 770 | to(String.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | /** |
| 774 | * Binds constant to the given value. |
| 775 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 776 | public void to(int value) { |
| 777 | to(int.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | /** |
| 781 | * Binds constant to the given value. |
| 782 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 783 | public void to(long value) { |
| 784 | to(long.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /** |
| 788 | * Binds constant to the given value. |
| 789 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 790 | public void to(boolean value) { |
| 791 | to(boolean.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | /** |
| 795 | * Binds constant to the given value. |
| 796 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 797 | public void to(double value) { |
| 798 | to(double.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | /** |
| 802 | * Binds constant to the given value. |
| 803 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 804 | public void to(float value) { |
| 805 | to(float.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | /** |
| 809 | * Binds constant to the given value. |
| 810 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 811 | public void to(short value) { |
| 812 | to(short.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | /** |
| 816 | * Binds constant to the given value. |
| 817 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 818 | public void to(char value) { |
| 819 | to(char.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | /** |
| 823 | * Binds constant to the given value. |
| 824 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 825 | public void to(Class<?> value) { |
| 826 | to(Class.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | /** |
| 830 | * Binds constant to the given value. |
| 831 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 832 | public <E extends Enum<E>> void to(E value) { |
| 833 | to(value.getDeclaringClass(), value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | /** |
| 837 | * Maps a constant value to the given type and name. |
| 838 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 839 | <T> void to(final Class<T> type, final T value) { |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 840 | if (this.value != null) { |
| 841 | addError(source, ErrorMessage.CONSTANT_VALUE_ALREADY_SET); |
| 842 | } else { |
| 843 | this.type = type; |
| 844 | this.value = value; |
| 845 | } |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | |
| 849 | /** |
| 850 | * Links one binding to another. |
| 851 | */ |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 852 | public class LinkedBindingBuilder<T> { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 853 | |
| 854 | Key<T> key; |
| 855 | Key<? extends T> destination; |
| 856 | Object source = ContainerBuilder.UNKNOWN_SOURCE; |
| 857 | |
| 858 | LinkedBindingBuilder(Key<T> key) { |
| 859 | this.key = key; |
| 860 | } |
| 861 | |
| 862 | Object getSource() { |
| 863 | return source; |
| 864 | } |
| 865 | |
| 866 | Key<T> getKey() { |
| 867 | return key; |
| 868 | } |
| 869 | |
| 870 | Key<? extends T> getDestination() { |
| 871 | return destination; |
| 872 | } |
| 873 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 874 | LinkedBindingBuilder<T> from(Object source) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 875 | this.source = source; |
| 876 | return this; |
| 877 | } |
| 878 | |
| 879 | /** |
| 880 | * Links to another binding with the given key. |
| 881 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 882 | public void to(Key<? extends T> destination) { |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 883 | if (this.destination != null) { |
| 884 | addError(source, ErrorMessage.LINK_DESTINATION_ALREADY_SET); |
| 885 | } else { |
| 886 | this.destination = destination; |
| 887 | } |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 888 | } |
| 889 | } |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 890 | |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 891 | /** |
| 892 | * Handles errors up until we successfully create the container. |
| 893 | */ |
| 894 | class ConfigurationErrorHandler extends AbstractErrorHandler { |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 895 | |
| 896 | final Object source; |
| 897 | |
| 898 | ConfigurationErrorHandler(Object source) { |
| 899 | this.source = source; |
| 900 | } |
| 901 | |
| 902 | public void handle(String message) { |
| 903 | add(new ErrorMessage(source, message)); |
| 904 | } |
| 905 | |
| 906 | public void handle(Throwable t) { |
| 907 | add(new ErrorMessage(source, t.getMessage())); |
| 908 | } |
| 909 | } |
| 910 | |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 911 | /** |
| 912 | * Handles errors after the container is created. |
| 913 | */ |
| 914 | class RuntimeErrorHandler extends AbstractErrorHandler { |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 915 | |
| 916 | public void handle(String message) { |
| 917 | throw new ConfigurationException(message); |
| 918 | } |
| 919 | |
| 920 | public void handle(Throwable t) { |
| 921 | throw new ConfigurationException(t); |
| 922 | } |
| 923 | } |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 924 | |
| 925 | final Set<String> skippedClassNames = new HashSet<String>(Arrays.asList( |
| 926 | ContainerBuilder.class.getName(), |
| 927 | AbstractModule.class.getName() |
| 928 | )); |
| 929 | |
| 930 | /** |
| 931 | * Instructs the builder to skip the given class in the stack trace when |
| 932 | * determining the source of a binding. Use this to keep the container |
| 933 | * builder from logging utility methods as the sources of bindings (i.e. |
| 934 | * it will skip to the utility methods' callers instead). |
| 935 | * |
| 936 | * <p>Skipping only takes place after this method is called. |
| 937 | */ |
| 938 | void skipSource(Class<?> clazz) { |
| 939 | skippedClassNames.add(clazz.getName()); |
| 940 | } |
| 941 | |
| 942 | /** |
| 943 | * Creates an object pointing to the current location within the |
| 944 | * configuration. If we run into a problem later, we'll be able to trace it |
| 945 | * back to the original source. Useful for debugging. The default |
| 946 | * implementation returns {@code ContainerBuilder}'s caller's {@code |
| 947 | * StackTraceElement}. |
| 948 | */ |
| 949 | Object source() { |
| 950 | // Search up the stack until we find a class outside of this one. |
| 951 | for (final StackTraceElement element : new Throwable().getStackTrace()) { |
| 952 | String className = element.getClassName(); |
| 953 | if (!skippedClassNames.contains(className)) { |
| 954 | return element; |
| 955 | } |
| 956 | } |
| 957 | throw new AssertionError(); |
| 958 | } |
crazyboblee | ef83bd2 | 2007-02-01 00:52:57 +0000 | [diff] [blame] | 959 | |
| 960 | /** |
| 961 | * A requested static injection. |
| 962 | */ |
| 963 | class StaticInjection { |
| 964 | |
| 965 | final Object source; |
| 966 | final Class<?>[] types; |
| 967 | final List<ContainerImpl.Injector> injectors = |
| 968 | new ArrayList<ContainerImpl.Injector>(); |
| 969 | |
| 970 | public StaticInjection(Object source, Class<?>[] types) { |
| 971 | this.source = source; |
| 972 | this.types = types; |
| 973 | } |
| 974 | |
| 975 | void createInjectors(final ContainerImpl container) { |
| 976 | container.withErrorHandler(new ConfigurationErrorHandler(source), |
| 977 | new Runnable() { |
| 978 | public void run() { |
| 979 | for (Class<?> clazz : types) { |
| 980 | container.addInjectorsForFields( |
| 981 | clazz.getDeclaredFields(), true, injectors); |
| 982 | container.addInjectorsForMethods( |
| 983 | clazz.getDeclaredMethods(), true, injectors); |
| 984 | } |
| 985 | } |
| 986 | }); |
| 987 | } |
| 988 | |
| 989 | void runInjectors(ContainerImpl container) { |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 990 | container.callInContext(new ContextualCallable<Void>() { |
crazyboblee | ef83bd2 | 2007-02-01 00:52:57 +0000 | [diff] [blame] | 991 | public Void call(InternalContext context) { |
| 992 | for (ContainerImpl.Injector injector : injectors) { |
| 993 | injector.inject(context, null); |
| 994 | } |
| 995 | return null; |
| 996 | } |
| 997 | }); |
| 998 | } |
| 999 | } |
crazyboblee | abc4dd0 | 2007-02-01 01:44:36 +0000 | [diff] [blame] | 1000 | |
| 1001 | static class BindingPreloader |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 1002 | implements ContextualCallable<Void> { |
crazyboblee | abc4dd0 | 2007-02-01 01:44:36 +0000 | [diff] [blame] | 1003 | |
| 1004 | private final Key<?> key; |
| 1005 | private final InternalFactory<?> factory; |
| 1006 | |
| 1007 | public BindingPreloader(Key<?> key, InternalFactory<?> factory) { |
| 1008 | this.key = key; |
| 1009 | this.factory = factory; |
| 1010 | } |
| 1011 | |
| 1012 | public Void call(InternalContext context) { |
| 1013 | ExternalContext<?> externalContext = |
| 1014 | ExternalContext.newInstance(null, key, context.getContainerImpl()); |
| 1015 | context.setExternalContext(externalContext); |
| 1016 | try { |
| 1017 | factory.get(context); |
| 1018 | return null; |
| 1019 | } finally { |
| 1020 | context.setExternalContext(null); |
| 1021 | } |
| 1022 | } |
| 1023 | } |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1024 | } |