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 | |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 19 | import com.google.inject.internal.Errors; |
| 20 | import com.google.inject.internal.InternalFactory; |
| 21 | import com.google.inject.internal.Scoping; |
| 22 | import java.lang.annotation.Annotation; |
| 23 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 24 | /** |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 25 | * Built-in scope implementations. |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 26 | * |
| 27 | * @author crazybob@google.com (Bob Lee) |
| 28 | */ |
crazyboblee | 68d2f4b | 2007-02-07 18:47:24 +0000 | [diff] [blame] | 29 | public class Scopes { |
| 30 | |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 31 | private Scopes() {} |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 32 | |
| 33 | /** |
crazyboblee | 61257a8 | 2007-03-03 00:23:40 +0000 | [diff] [blame] | 34 | * One instance per {@link Injector}. Also see {@code @}{@link Singleton}. |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 35 | */ |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 36 | public static final Scope SINGLETON = new Scope() { |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 37 | public <T> Provider<T> scope(Key<T> key, final Provider<T> creator) { |
| 38 | return new Provider<T>() { |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 39 | |
| 40 | private volatile T instance; |
| 41 | |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 42 | // 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] | 43 | @SuppressWarnings("DoubleCheckedLocking") |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 44 | public T get() { |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 45 | if (instance == null) { |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 46 | /* |
| 47 | * Use a pretty coarse lock. We don't want to run into deadlocks |
| 48 | * when two threads try to load circularly-dependent objects. |
| 49 | * Maybe one of these days we will identify independent graphs of |
| 50 | * objects and offer to load them in parallel. |
| 51 | */ |
limpbizkit | 7096280 | 2008-12-31 01:57:54 +0000 | [diff] [blame] | 52 | synchronized (InjectorImpl.class) { |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 53 | if (instance == null) { |
| 54 | instance = creator.get(); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | return instance; |
| 59 | } |
| 60 | |
| 61 | public String toString() { |
kevinb9n | da11d0d | 2007-04-20 15:58:38 +0000 | [diff] [blame] | 62 | return String.format("%s[%s]", creator, SINGLETON); |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 63 | } |
| 64 | }; |
| 65 | } |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 66 | |
limpbizkit | c9465f9 | 2008-05-31 19:01:54 +0000 | [diff] [blame] | 67 | @Override public String toString() { |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 68 | return "Scopes.SINGLETON"; |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 69 | } |
crazyboblee | 68d2f4b | 2007-02-07 18:47:24 +0000 | [diff] [blame] | 70 | }; |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 71 | |
| 72 | /** |
kevinb9n | b605482 | 2007-03-19 03:17:47 +0000 | [diff] [blame] | 73 | * No scope; the same as not applying any scope at all. Each time the |
| 74 | * Injector obtains an instance of an object with "no scope", it injects this |
| 75 | * instance then immediately forgets it. When the next request for the same |
| 76 | * binding arrives it will need to obtain the instance over again. |
| 77 | * |
| 78 | * <p>This exists only in case a class has been annotated with a scope |
| 79 | * annotation such as {@link Singleton @Singleton}, and you need to override |
| 80 | * this to "no scope" in your binding. |
limpbizkit | c489adf | 2008-11-18 07:01:33 +0000 | [diff] [blame] | 81 | * |
| 82 | * @since 2.0 |
kevinb9n | b605482 | 2007-03-19 03:17:47 +0000 | [diff] [blame] | 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 | } |
limpbizkit | c9465f9 | 2008-05-31 19:01:54 +0000 | [diff] [blame] | 88 | @Override public String toString() { |
kevinb9n | b605482 | 2007-03-19 03:17:47 +0000 | [diff] [blame] | 89 | return "Scopes.NO_SCOPE"; |
| 90 | } |
| 91 | }; |
| 92 | |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 93 | /** Scopes an internal factory. */ |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 94 | static <T> InternalFactory<? extends T> scope(Key<T> key, InjectorImpl injector, |
| 95 | InternalFactory<? extends T> creator, Scoping scoping) { |
| 96 | |
| 97 | if (scoping.isNoScope()) { |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 98 | return creator; |
| 99 | } |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 100 | |
| 101 | Scope scope = scoping.getScopeInstance(); |
| 102 | |
| 103 | Provider<T> scoped |
| 104 | = scope.scope(key, new ProviderToInternalFactoryAdapter<T>(injector, creator)); |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 105 | return new InternalFactoryToProviderAdapter<T>( |
| 106 | Initializables.<Provider<? extends T>>of(scoped)); |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 107 | } |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Replaces annotation scopes with instance scopes using the Injector's annotation-to-instance |
| 111 | * map. If the scope annotation has no corresponding instance, an error will be added and unscoped |
| 112 | * will be retuned. |
| 113 | */ |
| 114 | static Scoping makeInjectable(Scoping scoping, InjectorImpl injector, Errors errors) { |
| 115 | Class<? extends Annotation> scopeAnnotation = scoping.getScopeAnnotation(); |
| 116 | if (scopeAnnotation == null) { |
| 117 | return scoping; |
| 118 | } |
| 119 | |
| 120 | Scope scope = injector.state.getScope(scopeAnnotation); |
| 121 | if (scope != null) { |
| 122 | return Scoping.forInstance(scope); |
| 123 | } |
| 124 | |
| 125 | errors.scopeNotFound(scopeAnnotation); |
| 126 | return Scoping.UNSCOPED; |
| 127 | } |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 128 | } |