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