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