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 | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 318 | // Blow up. |
| 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. |
| 334 | if (preload) { |
| 335 | container.callInContext(new ContainerImpl.ContextualCallable<Void>() { |
| 336 | public Void call(InternalContext context) { |
| 337 | for (ContainerImpl.ContextualCallable<Void> preloader |
| 338 | : preloaders) { |
| 339 | preloader.call(context); |
| 340 | } |
| 341 | return null; |
| 342 | } |
| 343 | }); |
| 344 | } |
| 345 | |
| 346 | stopwatch.resetAndLog(logger, "Preloading"); |
| 347 | |
| 348 | return container; |
| 349 | } |
| 350 | |
| 351 | private String createErrorMessage() { |
| 352 | StringBuilder error = new StringBuilder(); |
| 353 | error.append("Guice configuration errors:\n\n"); |
| 354 | int index = 1; |
| 355 | for (ErrorMessage errorMessage : errorMessages) { |
| 356 | error.append(index++) |
| 357 | .append(") ") |
| 358 | .append("Error at ") |
| 359 | .append(errorMessage.getSource()) |
| 360 | .append(':') |
| 361 | .append('\n') |
| 362 | .append(" ") |
| 363 | .append(errorMessage.getMessage()) |
| 364 | .append("\n\n"); |
| 365 | } |
| 366 | error.append(errorMessages.size()).append(" error[s]\n"); |
| 367 | return error.toString(); |
| 368 | } |
| 369 | |
| 370 | private void createLinkedBindings( |
| 371 | HashMap<Key<?>, InternalFactory<?>> factories) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 372 | for (LinkedBindingBuilder<?> builder : linkedBindingBuilders) { |
| 373 | // TODO: Support alias to a later-declared alias. |
| 374 | Key<?> destination = builder.getDestination(); |
| 375 | if (destination == null) { |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 376 | addError(builder.getSource(), ErrorMessage.MISSING_LINK_DESTINATION); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 377 | continue; |
| 378 | } |
| 379 | |
| 380 | InternalFactory<?> factory = factories.get(destination); |
| 381 | if (factory == null) { |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 382 | addError(builder.getSource(), ErrorMessage.LINK_DESTINATION_NOT_FOUND, |
| 383 | destination); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 384 | continue; |
| 385 | } |
| 386 | |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 387 | putFactory(builder.getSource(), factories, builder.getKey(), factory); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 388 | } |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 389 | } |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 390 | |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 391 | private void createBindings(ContainerImpl container, |
| 392 | HashMap<Key<?>, InternalFactory<?>> factories, boolean preload, |
| 393 | List<ContainerImpl.ContextualCallable<Void>> preloaders) { |
| 394 | for (BindingBuilder<?> builder : bindingBuilders) { |
| 395 | final Key<?> key = builder.getKey(); |
| 396 | final InternalFactory<?> factory = builder.getInternalFactory(container); |
| 397 | putFactory(builder.getSource(), factories, key, factory); |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 398 | |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 399 | if (preload && builder.isInContainerScope()) { |
| 400 | preloaders.add(new ContainerImpl.ContextualCallable<Void>() { |
| 401 | public Void call(InternalContext context) { |
| 402 | context.setExternalContext( |
| 403 | ExternalContext.newInstance(null, key, |
| 404 | context.getContainerImpl())); |
| 405 | try { |
| 406 | factory.get(context); |
| 407 | return null; |
| 408 | } finally { |
| 409 | context.setExternalContext(null); |
| 410 | } |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 411 | } |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 412 | }); |
| 413 | } |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 414 | } |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 415 | } |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 416 | |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 417 | private void createConstantBindings( |
| 418 | HashMap<Key<?>, InternalFactory<?>> factories) { |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 419 | for (ConstantBindingBuilder builder : constantBindingBuilders) { |
| 420 | if (builder.hasValue()) { |
| 421 | Key<?> key = builder.getKey(); |
| 422 | InternalFactory<?> factory = builder.getInternalFactory(); |
| 423 | putFactory(builder.getSource(), factories, key, factory); |
| 424 | } else { |
| 425 | addError(builder.getSource(), ErrorMessage.MISSING_CONSTANT_VALUE); |
| 426 | } |
| 427 | } |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 428 | } |
| 429 | |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 430 | void putFactory(Object source, Map<Key<?>, InternalFactory<?>> factories, |
| 431 | Key<?> key, InternalFactory<?> factory) { |
| 432 | if (factories.containsKey(key)) { |
| 433 | addError(source, ErrorMessage.BINDING_ALREADY_SET, key); |
| 434 | } else { |
| 435 | factories.put(key, factory); |
| 436 | } |
| 437 | } |
| 438 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 439 | /** |
| 440 | * Currently we only support creating one Container instance per builder. |
| 441 | * If we want to support creating more than one container per builder, |
| 442 | * we should move to a "factory factory" model where we create a factory |
| 443 | * instance per Container. Right now, one factory instance would be |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 444 | * shared across all the containers, which means container-scoped objects |
| 445 | * would be shared, etc. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 446 | */ |
| 447 | private void ensureNotCreated() { |
| 448 | if (created) { |
| 449 | throw new IllegalStateException("Container already created."); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /** |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 454 | * Binds a {@link Key} to an implementation in a given scope. |
| 455 | */ |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 456 | public class BindingBuilder<T> { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 457 | |
| 458 | Object source = ContainerBuilder.UNKNOWN_SOURCE; |
| 459 | Key<T> key; |
| 460 | InternalFactory<? extends T> factory; |
| 461 | Scope scope; |
| 462 | |
| 463 | BindingBuilder(Key<T> key) { |
| 464 | this.key = nonNull(key, "key"); |
| 465 | } |
| 466 | |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 467 | Object getSource() { |
| 468 | return source; |
| 469 | } |
| 470 | |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 471 | Key<T> getKey() { |
| 472 | return key; |
| 473 | } |
| 474 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 475 | BindingBuilder<T> from(Object source) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 476 | this.source = source; |
| 477 | return this; |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * Sets the name of this binding. |
| 482 | */ |
| 483 | public BindingBuilder<T> named(String name) { |
| 484 | if (!this.key.hasDefaultName()) { |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 485 | addError(source, ErrorMessage.NAME_ALREADY_SET); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | this.key = this.key.named(name); |
| 489 | return this; |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Binds to instances of the given implementation class. The {@link |
| 494 | * Container} will inject the implementation instances as well. Sets the |
| 495 | * scope based on the @{@link Scoped} annotation on the implementation |
| 496 | * class if present. |
| 497 | */ |
| 498 | public <I extends T> BindingBuilder<T> to(Class<I> implementation) { |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 499 | return to(TypeLiteral.get(implementation)); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Binds to instances of the given implementation type. The {@link |
| 504 | * Container} will inject the implementation instances as well. Sets the |
| 505 | * scope based on the @{@link Scoped} annotation on the implementation |
| 506 | * class if present. |
| 507 | */ |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 508 | public <I extends T> BindingBuilder<T> to(TypeLiteral<I> implementation) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 509 | ensureImplementationIsNotSet(); |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 510 | validate(source, implementation.getRawType()); |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 511 | this.factory = new DefaultFactory<I>(key, implementation); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 512 | setScopeFromType(implementation.getRawType()); |
| 513 | return this; |
| 514 | } |
| 515 | |
| 516 | private void setScopeFromType(Class<?> implementation) { |
| 517 | Scoped scoped = implementation.getAnnotation(Scoped.class); |
| 518 | if (scoped != null) { |
| 519 | in(scoped.value()); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Binds to instances from the given factory. |
| 525 | */ |
| 526 | public BindingBuilder<T> to( |
| 527 | final ContextualFactory<? extends T> factory) { |
| 528 | ensureImplementationIsNotSet(); |
| 529 | |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 530 | this.factory = new InternalToContextualFactoryAdapter<T>(factory); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 531 | |
| 532 | return this; |
| 533 | } |
| 534 | |
| 535 | /** |
| 536 | * Binds to instances from the given factory. |
| 537 | */ |
| 538 | public BindingBuilder<T> to(final Factory<? extends T> factory) { |
| 539 | ensureImplementationIsNotSet(); |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 540 | this.factory = new InternalFactoryToFactoryAdapter<T>(factory); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 541 | return this; |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * Binds to the given instance. |
| 546 | */ |
| 547 | BindingBuilder<T> to(T instance) { |
| 548 | ensureImplementationIsNotSet(); |
| 549 | this.factory = new ConstantFactory<T>(instance); |
| 550 | return this; |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Binds to instances from the given factory. |
| 555 | */ |
| 556 | BindingBuilder<T> to(final InternalFactory<? extends T> factory) { |
| 557 | ensureImplementationIsNotSet(); |
| 558 | this.factory = factory; |
| 559 | return this; |
| 560 | } |
| 561 | |
| 562 | /** |
| 563 | * Adds an error message if the implementation has already been bound. |
| 564 | */ |
| 565 | private void ensureImplementationIsNotSet() { |
| 566 | if (factory != null) { |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 567 | addError(source, ErrorMessage.IMPLEMENTATION_ALREADY_SET); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
| 571 | /** |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 572 | * Specifies the scope. References the name passed to {@link |
| 573 | * ContainerBuilder#put(String, Scope)}. |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 574 | */ |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 575 | public BindingBuilder<T> in(String scopeName) { |
| 576 | ensureScopeNotSet(); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 577 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 578 | // We could defer this lookup to when we create the container, but this |
| 579 | // is fine for now. |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 580 | this.scope = scopes.get(nonNull(scopeName, "scope name")); |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 581 | if (this.scope == null) { |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 582 | addError(source, ErrorMessage.SCOPE_NOT_FOUND, scopeName, |
| 583 | scopes.keySet()); |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 584 | } |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 585 | return this; |
| 586 | } |
| 587 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 588 | /** |
| 589 | * Specifies the scope. |
| 590 | */ |
| 591 | public BindingBuilder<T> in(Scope scope) { |
| 592 | ensureScopeNotSet(); |
| 593 | |
| 594 | this.scope = nonNull(scope, "scope"); |
| 595 | return this; |
| 596 | } |
| 597 | |
| 598 | private void ensureScopeNotSet() { |
| 599 | if (this.scope != null) { |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 600 | addError(source, ErrorMessage.SCOPE_ALREADY_SET); |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
| 604 | InternalFactory<? extends T> getInternalFactory( |
| 605 | final ContainerImpl container) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 606 | // If an implementation wasn't specified, use the injection type. |
| 607 | if (this.factory == null) { |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 608 | to(key.getType()); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 609 | } |
| 610 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 611 | if (scope == null || scope == DEFAULT_SCOPE) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 612 | return this.factory; |
| 613 | } |
| 614 | |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 615 | Factory<T> scoped = scope.scope(this.key, |
| 616 | new FactoryToInternalFactoryAdapter<T>(container, this.factory)); |
| 617 | return new InternalFactoryToFactoryAdapter<T>(scoped); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 618 | } |
| 619 | |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 620 | boolean isInContainerScope() { |
| 621 | return this.scope == ContainerScope.INSTANCE; |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 622 | } |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 623 | } |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 624 | |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 625 | /** |
| 626 | * Injects new instances of the specified implementation class. |
| 627 | */ |
| 628 | private static class DefaultFactory<T> implements InternalFactory<T> { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 629 | |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 630 | volatile ContainerImpl.ConstructorInjector<T> constructor; |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 631 | |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 632 | private final TypeLiteral<T> implementation; |
| 633 | private final Key<? super T> key; |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 634 | |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 635 | public DefaultFactory(Key<? super T> key, TypeLiteral<T> implementation) { |
| 636 | this.key = key; |
| 637 | this.implementation = implementation; |
| 638 | } |
| 639 | |
| 640 | @SuppressWarnings("unchecked") |
| 641 | public T get(InternalContext context) { |
| 642 | if (constructor == null) { |
| 643 | // This unnecessary cast is a workaround for an annoying compiler |
| 644 | // bug I keep running into. |
| 645 | Object c = context.getContainerImpl().getConstructor(implementation); |
| 646 | this.constructor = (ContainerImpl.ConstructorInjector<T>) c; |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 647 | } |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 648 | return (T) constructor.construct(context, key.getRawType()); |
| 649 | } |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 650 | |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 651 | public String toString() { |
| 652 | return implementation.toString(); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * Builds a constant binding. |
| 658 | */ |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 659 | public class ConstantBindingBuilder { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 660 | |
| 661 | final String name; |
| 662 | Class<?> type; |
| 663 | Object value; |
| 664 | Object source = ContainerBuilder.UNKNOWN_SOURCE; |
| 665 | |
| 666 | ConstantBindingBuilder(String name) { |
| 667 | this.name = name; |
| 668 | } |
| 669 | |
| 670 | Key<?> getKey() { |
| 671 | return Key.get(type, name); |
| 672 | } |
| 673 | |
| 674 | boolean hasValue() { |
| 675 | return type != null; |
| 676 | } |
| 677 | |
| 678 | Object getSource() { |
| 679 | return source; |
| 680 | } |
| 681 | |
| 682 | InternalFactory<?> getInternalFactory() { |
| 683 | return new ConstantFactory<Object>(value); |
| 684 | } |
| 685 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 686 | ConstantBindingBuilder from(Object source) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 687 | this.source = source; |
| 688 | return this; |
| 689 | } |
| 690 | |
| 691 | /** |
| 692 | * Binds constant to the given value. |
| 693 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 694 | public void to(String value) { |
| 695 | to(String.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /** |
| 699 | * Binds constant to the given value. |
| 700 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 701 | public void to(int value) { |
| 702 | to(int.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | /** |
| 706 | * Binds constant to the given value. |
| 707 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 708 | public void to(long value) { |
| 709 | to(long.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | /** |
| 713 | * Binds constant to the given value. |
| 714 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 715 | public void to(boolean value) { |
| 716 | to(boolean.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | /** |
| 720 | * Binds constant to the given value. |
| 721 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 722 | public void to(double value) { |
| 723 | to(double.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | /** |
| 727 | * Binds constant to the given value. |
| 728 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 729 | public void to(float value) { |
| 730 | to(float.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | /** |
| 734 | * Binds constant to the given value. |
| 735 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 736 | public void to(short value) { |
| 737 | to(short.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | /** |
| 741 | * Binds constant to the given value. |
| 742 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 743 | public void to(char value) { |
| 744 | to(char.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | /** |
| 748 | * Binds constant to the given value. |
| 749 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 750 | public void to(Class<?> value) { |
| 751 | to(Class.class, value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | /** |
| 755 | * Binds constant to the given value. |
| 756 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 757 | public <E extends Enum<E>> void to(E value) { |
| 758 | to(value.getDeclaringClass(), value); |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | /** |
| 762 | * Maps a constant value to the given type and name. |
| 763 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 764 | <T> void to(final Class<T> type, final T value) { |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 765 | if (this.value != null) { |
| 766 | addError(source, ErrorMessage.CONSTANT_VALUE_ALREADY_SET); |
| 767 | } else { |
| 768 | this.type = type; |
| 769 | this.value = value; |
| 770 | } |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | |
| 774 | /** |
| 775 | * Links one binding to another. |
| 776 | */ |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 777 | public class LinkedBindingBuilder<T> { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 778 | |
| 779 | Key<T> key; |
| 780 | Key<? extends T> destination; |
| 781 | Object source = ContainerBuilder.UNKNOWN_SOURCE; |
| 782 | |
| 783 | LinkedBindingBuilder(Key<T> key) { |
| 784 | this.key = key; |
| 785 | } |
| 786 | |
| 787 | Object getSource() { |
| 788 | return source; |
| 789 | } |
| 790 | |
| 791 | Key<T> getKey() { |
| 792 | return key; |
| 793 | } |
| 794 | |
| 795 | Key<? extends T> getDestination() { |
| 796 | return destination; |
| 797 | } |
| 798 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 799 | LinkedBindingBuilder<T> from(Object source) { |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 800 | this.source = source; |
| 801 | return this; |
| 802 | } |
| 803 | |
| 804 | /** |
| 805 | * Links to another binding with the given key. |
| 806 | */ |
crazyboblee | 78e1cc1 | 2007-01-26 02:18:06 +0000 | [diff] [blame] | 807 | public void to(Key<? extends T> destination) { |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 808 | if (this.destination != null) { |
| 809 | addError(source, ErrorMessage.LINK_DESTINATION_ALREADY_SET); |
| 810 | } else { |
| 811 | this.destination = destination; |
| 812 | } |
crazyboblee | 7c5b2c4 | 2007-01-20 02:05:20 +0000 | [diff] [blame] | 813 | } |
| 814 | } |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 815 | |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 816 | /** |
| 817 | * Handles errors up until we successfully create the container. |
| 818 | */ |
| 819 | class ConfigurationErrorHandler extends AbstractErrorHandler { |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 820 | |
| 821 | final Object source; |
| 822 | |
| 823 | ConfigurationErrorHandler(Object source) { |
| 824 | this.source = source; |
| 825 | } |
| 826 | |
| 827 | public void handle(String message) { |
| 828 | add(new ErrorMessage(source, message)); |
| 829 | } |
| 830 | |
| 831 | public void handle(Throwable t) { |
| 832 | add(new ErrorMessage(source, t.getMessage())); |
| 833 | } |
| 834 | } |
| 835 | |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 836 | /** |
| 837 | * Handles errors after the container is created. |
| 838 | */ |
| 839 | class RuntimeErrorHandler extends AbstractErrorHandler { |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 840 | |
| 841 | public void handle(String message) { |
| 842 | throw new ConfigurationException(message); |
| 843 | } |
| 844 | |
| 845 | public void handle(Throwable t) { |
| 846 | throw new ConfigurationException(t); |
| 847 | } |
| 848 | } |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 849 | |
| 850 | final Set<String> skippedClassNames = new HashSet<String>(Arrays.asList( |
| 851 | ContainerBuilder.class.getName(), |
| 852 | AbstractModule.class.getName() |
| 853 | )); |
| 854 | |
| 855 | /** |
| 856 | * Instructs the builder to skip the given class in the stack trace when |
| 857 | * determining the source of a binding. Use this to keep the container |
| 858 | * builder from logging utility methods as the sources of bindings (i.e. |
| 859 | * it will skip to the utility methods' callers instead). |
| 860 | * |
| 861 | * <p>Skipping only takes place after this method is called. |
| 862 | */ |
| 863 | void skipSource(Class<?> clazz) { |
| 864 | skippedClassNames.add(clazz.getName()); |
| 865 | } |
| 866 | |
| 867 | /** |
| 868 | * Creates an object pointing to the current location within the |
| 869 | * configuration. If we run into a problem later, we'll be able to trace it |
| 870 | * back to the original source. Useful for debugging. The default |
| 871 | * implementation returns {@code ContainerBuilder}'s caller's {@code |
| 872 | * StackTraceElement}. |
| 873 | */ |
| 874 | Object source() { |
| 875 | // Search up the stack until we find a class outside of this one. |
| 876 | for (final StackTraceElement element : new Throwable().getStackTrace()) { |
| 877 | String className = element.getClassName(); |
| 878 | if (!skippedClassNames.contains(className)) { |
| 879 | return element; |
| 880 | } |
| 881 | } |
| 882 | throw new AssertionError(); |
| 883 | } |
crazyboblee | ef83bd2 | 2007-02-01 00:52:57 +0000 | [diff] [blame^] | 884 | |
| 885 | /** |
| 886 | * A requested static injection. |
| 887 | */ |
| 888 | class StaticInjection { |
| 889 | |
| 890 | final Object source; |
| 891 | final Class<?>[] types; |
| 892 | final List<ContainerImpl.Injector> injectors = |
| 893 | new ArrayList<ContainerImpl.Injector>(); |
| 894 | |
| 895 | public StaticInjection(Object source, Class<?>[] types) { |
| 896 | this.source = source; |
| 897 | this.types = types; |
| 898 | } |
| 899 | |
| 900 | void createInjectors(final ContainerImpl container) { |
| 901 | container.withErrorHandler(new ConfigurationErrorHandler(source), |
| 902 | new Runnable() { |
| 903 | public void run() { |
| 904 | for (Class<?> clazz : types) { |
| 905 | container.addInjectorsForFields( |
| 906 | clazz.getDeclaredFields(), true, injectors); |
| 907 | container.addInjectorsForMethods( |
| 908 | clazz.getDeclaredMethods(), true, injectors); |
| 909 | } |
| 910 | } |
| 911 | }); |
| 912 | } |
| 913 | |
| 914 | void runInjectors(ContainerImpl container) { |
| 915 | container.callInContext(new ContainerImpl.ContextualCallable<Void>() { |
| 916 | public Void call(InternalContext context) { |
| 917 | for (ContainerImpl.Injector injector : injectors) { |
| 918 | injector.inject(context, null); |
| 919 | } |
| 920 | return null; |
| 921 | } |
| 922 | }); |
| 923 | } |
| 924 | } |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 925 | } |