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 | cad2c2b | 2007-05-15 17:28:03 +0000 | [diff] [blame] | 19 | import com.google.inject.internal.StackTraceElements; |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame^] | 20 | import com.google.inject.internal.ErrorHandler; |
| 21 | import com.google.inject.internal.ErrorMessages; |
| 22 | |
crazyboblee | b1f2e68 | 2007-02-15 05:14:32 +0000 | [diff] [blame] | 23 | import java.lang.annotation.Annotation; |
crazyboblee | 278ee4d | 2007-02-15 19:23:13 +0000 | [diff] [blame] | 24 | import java.util.Map; |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 25 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 26 | /** |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 27 | * Built in scope implementations. |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 28 | * |
| 29 | * @author crazybob@google.com (Bob Lee) |
| 30 | */ |
crazyboblee | 68d2f4b | 2007-02-07 18:47:24 +0000 | [diff] [blame] | 31 | public class Scopes { |
| 32 | |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 33 | private Scopes() {} |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 34 | |
| 35 | /** |
crazyboblee | 61257a8 | 2007-03-03 00:23:40 +0000 | [diff] [blame] | 36 | * One instance per {@link Injector}. Also see {@code @}{@link Singleton}. |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 37 | */ |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 38 | public static final Scope SINGLETON = new Scope() { |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 39 | public <T> Provider<T> scope(Key<T> key, final Provider<T> creator) { |
| 40 | return new Provider<T>() { |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 41 | |
| 42 | private volatile T instance; |
| 43 | |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 44 | // 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] | 45 | @SuppressWarnings("DoubleCheckedLocking") |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 46 | public T get() { |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 47 | if (instance == null) { |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 48 | /* |
| 49 | * Use a pretty coarse lock. We don't want to run into deadlocks |
| 50 | * when two threads try to load circularly-dependent objects. |
| 51 | * Maybe one of these days we will identify independent graphs of |
| 52 | * objects and offer to load them in parallel. |
| 53 | */ |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 54 | synchronized (Injector.class) { |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 55 | if (instance == null) { |
| 56 | instance = creator.get(); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | return instance; |
| 61 | } |
| 62 | |
| 63 | public String toString() { |
kevinb9n | da11d0d | 2007-04-20 15:58:38 +0000 | [diff] [blame] | 64 | return String.format("%s[%s]", creator, SINGLETON); |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 65 | } |
| 66 | }; |
| 67 | } |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 68 | |
| 69 | public String toString() { |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 70 | return "Scopes.SINGLETON"; |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 71 | } |
crazyboblee | 68d2f4b | 2007-02-07 18:47:24 +0000 | [diff] [blame] | 72 | }; |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 73 | |
| 74 | /** |
kevinb9n | b605482 | 2007-03-19 03:17:47 +0000 | [diff] [blame] | 75 | * No scope; the same as not applying any scope at all. Each time the |
| 76 | * Injector obtains an instance of an object with "no scope", it injects this |
| 77 | * instance then immediately forgets it. When the next request for the same |
| 78 | * binding arrives it will need to obtain the instance over again. |
| 79 | * |
| 80 | * <p>This exists only in case a class has been annotated with a scope |
| 81 | * annotation such as {@link Singleton @Singleton}, and you need to override |
| 82 | * this to "no scope" in your binding. |
| 83 | */ |
| 84 | public static final Scope NO_SCOPE = new Scope() { |
| 85 | public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) { |
| 86 | return unscoped; |
| 87 | } |
| 88 | public String toString() { |
| 89 | return "Scopes.NO_SCOPE"; |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | /** |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 94 | * Gets the scope for a type based on its annotations. Returns {@code null} |
| 95 | * if none specified. |
| 96 | * |
| 97 | * @param implementation type |
| 98 | * @param scopes map of scope names to scopes |
| 99 | * @param errorHandler handles errors |
| 100 | */ |
| 101 | static Scope getScopeForType(Class<?> implementation, |
crazyboblee | b1f2e68 | 2007-02-15 05:14:32 +0000 | [diff] [blame] | 102 | Map<Class<? extends Annotation>, Scope> scopes, |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 103 | ErrorHandler errorHandler) { |
crazyboblee | 97b2cac | 2007-02-23 22:31:30 +0000 | [diff] [blame] | 104 | Class<? extends Annotation> found = null; |
crazyboblee | b1f2e68 | 2007-02-15 05:14:32 +0000 | [diff] [blame] | 105 | for (Annotation annotation : implementation.getAnnotations()) { |
crazyboblee | 97b2cac | 2007-02-23 22:31:30 +0000 | [diff] [blame] | 106 | if (isScopeAnnotation(annotation)) { |
crazyboblee | b1f2e68 | 2007-02-15 05:14:32 +0000 | [diff] [blame] | 107 | if (found != null) { |
crazyboblee | 97b2cac | 2007-02-23 22:31:30 +0000 | [diff] [blame] | 108 | errorHandler.handle( |
| 109 | StackTraceElements.forType(implementation), |
| 110 | ErrorMessages.DUPLICATE_SCOPE_ANNOTATIONS, |
| 111 | "@" + found.getSimpleName(), |
| 112 | "@" + annotation.annotationType().getSimpleName() |
| 113 | ); |
crazyboblee | b1f2e68 | 2007-02-15 05:14:32 +0000 | [diff] [blame] | 114 | } else { |
crazyboblee | 97b2cac | 2007-02-23 22:31:30 +0000 | [diff] [blame] | 115 | found = annotation.annotationType(); |
crazyboblee | b1f2e68 | 2007-02-15 05:14:32 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 118 | } |
crazyboblee | 97b2cac | 2007-02-23 22:31:30 +0000 | [diff] [blame] | 119 | |
| 120 | return scopes.get(found); |
| 121 | } |
| 122 | |
| 123 | static boolean isScopeAnnotation(Annotation annotation) { |
| 124 | return isScopeAnnotation(annotation.annotationType()); |
| 125 | } |
| 126 | |
| 127 | static boolean isScopeAnnotation(Class<? extends Annotation> annotationType) { |
| 128 | return annotationType.isAnnotationPresent(ScopeAnnotation.class); |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Scopes an internal factory. |
| 133 | */ |
| 134 | static <T> InternalFactory<? extends T> scope(Key<T> key, |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 135 | InjectorImpl injector, InternalFactory<? extends T> creator, |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 136 | Scope scope) { |
crazyboblee | 97b2cac | 2007-02-23 22:31:30 +0000 | [diff] [blame] | 137 | // No scope does nothing. |
crazyboblee | d0c4b8b | 2007-09-06 02:47:04 +0000 | [diff] [blame] | 138 | if (scope == null || scope == Scopes.NO_SCOPE) { |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 139 | return creator; |
| 140 | } |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 141 | Provider<T> scoped = scope.scope(key, |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 142 | new ProviderToInternalFactoryAdapter<T>(injector, creator)); |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 143 | return new InternalFactoryToProviderAdapter<T>(scoped); |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 144 | } |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 145 | } |