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