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