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