crazyboblee | abc4dd0 | 2007-02-01 01:44:36 +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 | */ |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 16 | |
| 17 | package com.google.inject; |
| 18 | |
crazyboblee | b1f2e68 | 2007-02-15 05:14:32 +0000 | [diff] [blame] | 19 | import java.lang.annotation.Annotation; |
crazyboblee | 278ee4d | 2007-02-15 19:23:13 +0000 | [diff] [blame^] | 20 | import java.util.Map; |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 21 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 22 | /** |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 23 | * Built in scope implementations. |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 24 | * |
| 25 | * @author crazybob@google.com (Bob Lee) |
| 26 | */ |
crazyboblee | 68d2f4b | 2007-02-07 18:47:24 +0000 | [diff] [blame] | 27 | public class Scopes { |
| 28 | |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 29 | private Scopes() {} |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 30 | |
| 31 | /** |
crazyboblee | 68d2f4b | 2007-02-07 18:47:24 +0000 | [diff] [blame] | 32 | * The default scope, one instance per injection. |
| 33 | */ |
| 34 | public static final Scope DEFAULT = new Scope() { |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 35 | public <T> Factory<T> scope(Key<T> key, Factory<T> creator) { |
| 36 | return creator; |
| 37 | } |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 38 | |
| 39 | public String toString() { |
crazyboblee | b1f2e68 | 2007-02-15 05:14:32 +0000 | [diff] [blame] | 40 | return "Scopes.DEFAULT"; |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 41 | } |
crazyboblee | 68d2f4b | 2007-02-07 18:47:24 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | /** |
crazyboblee | 278ee4d | 2007-02-15 19:23:13 +0000 | [diff] [blame^] | 45 | * One instance per container. Also see {@code @}{@link ContainerScoped}. |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 46 | */ |
crazyboblee | 68d2f4b | 2007-02-07 18:47:24 +0000 | [diff] [blame] | 47 | public static final Scope CONTAINER = new Scope() { |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 48 | public <T> Factory<T> scope(Key<T> key, final Factory<T> creator) { |
| 49 | return new Factory<T>() { |
| 50 | |
| 51 | private volatile T instance; |
| 52 | |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 53 | // DCL on a volatile is safe as of Java 5, which we obviously require. |
kevinb9n | 6a565c7 | 2007-02-11 01:58:33 +0000 | [diff] [blame] | 54 | @SuppressWarnings("DoubleCheckedLocking") |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 55 | public T get() { |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 56 | if (instance == null) { |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 57 | /* |
| 58 | * Use a pretty coarse lock. We don't want to run into deadlocks |
| 59 | * when two threads try to load circularly-dependent objects. |
| 60 | * Maybe one of these days we will identify independent graphs of |
| 61 | * objects and offer to load them in parallel. |
| 62 | */ |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 63 | synchronized (Container.class) { |
| 64 | if (instance == null) { |
| 65 | instance = creator.get(); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | return instance; |
| 70 | } |
| 71 | |
| 72 | public String toString() { |
| 73 | return creator.toString(); |
| 74 | } |
| 75 | }; |
| 76 | } |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 77 | |
| 78 | public String toString() { |
crazyboblee | b1f2e68 | 2007-02-15 05:14:32 +0000 | [diff] [blame] | 79 | return "Scopes.CONTAINER"; |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 80 | } |
crazyboblee | 68d2f4b | 2007-02-07 18:47:24 +0000 | [diff] [blame] | 81 | }; |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * Gets the scope for a type based on its annotations. Returns {@code null} |
| 85 | * if none specified. |
| 86 | * |
| 87 | * @param implementation type |
| 88 | * @param scopes map of scope names to scopes |
| 89 | * @param errorHandler handles errors |
| 90 | */ |
| 91 | static Scope getScopeForType(Class<?> implementation, |
crazyboblee | b1f2e68 | 2007-02-15 05:14:32 +0000 | [diff] [blame] | 92 | Map<Class<? extends Annotation>, Scope> scopes, |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 93 | ErrorHandler errorHandler) { |
crazyboblee | b1f2e68 | 2007-02-15 05:14:32 +0000 | [diff] [blame] | 94 | Scope found = null; |
| 95 | for (Annotation annotation : implementation.getAnnotations()) { |
| 96 | Scope scope = scopes.get(annotation.annotationType()); |
| 97 | if (scope != null) { |
| 98 | if (found != null) { |
| 99 | errorHandler.handle(ErrorMessages.DUPLICATE_SCOPE_ANNOTATIONS, |
| 100 | implementation, found, scope); |
| 101 | } else { |
| 102 | found = scope; |
| 103 | } |
| 104 | } |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 105 | } |
crazyboblee | b1f2e68 | 2007-02-15 05:14:32 +0000 | [diff] [blame] | 106 | return found; |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Scopes an internal factory. |
| 111 | */ |
| 112 | static <T> InternalFactory<? extends T> scope(Key<T> key, |
| 113 | ContainerImpl container, InternalFactory<? extends T> creator, |
| 114 | Scope scope) { |
| 115 | // Default scope does nothing. |
| 116 | if (scope == null || scope == DEFAULT) { |
| 117 | return creator; |
| 118 | } |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 119 | Factory<T> scoped = scope.scope(key, |
| 120 | new FactoryToInternalFactoryAdapter<T>(container, creator)); |
| 121 | return new InternalFactoryToFactoryAdapter<T>(scoped); |
| 122 | } |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 123 | } |