blob: 4f9b876854b6b6e7a3e183b6b4795f19b65de1d6 [file] [log] [blame]
crazybobleeabc4dd02007-02-01 01:44:36 +00001/**
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 */
crazyboblee63b592b2007-01-25 02:45:24 +000016
17package com.google.inject;
18
crazyboblee6b931bd2007-02-13 21:02:41 +000019import com.google.inject.util.SurrogateAnnotations;
20import com.google.inject.util.DuplicateAnnotationException;
21
crazybobleef33d23e2007-02-12 04:17:48 +000022import java.util.Map;
crazybobleeb1f2e682007-02-15 05:14:32 +000023import java.lang.annotation.Annotation;
crazybobleef33d23e2007-02-12 04:17:48 +000024
crazyboblee63b592b2007-01-25 02:45:24 +000025/**
crazybobleee5fbbb02007-02-05 07:00:27 +000026 * Built in scope implementations.
crazyboblee63b592b2007-01-25 02:45:24 +000027 *
28 * @author crazybob@google.com (Bob Lee)
29 */
crazyboblee68d2f4b2007-02-07 18:47:24 +000030public class Scopes {
31
crazybobleef33d23e2007-02-12 04:17:48 +000032 private Scopes() {}
crazyboblee63b592b2007-01-25 02:45:24 +000033
34 /**
crazyboblee68d2f4b2007-02-07 18:47:24 +000035 * The default scope, one instance per injection.
36 */
37 public static final Scope DEFAULT = new Scope() {
crazybobleee5fbbb02007-02-05 07:00:27 +000038 public <T> Factory<T> scope(Key<T> key, Factory<T> creator) {
39 return creator;
40 }
crazybobleef33d23e2007-02-12 04:17:48 +000041
42 public String toString() {
crazybobleeb1f2e682007-02-15 05:14:32 +000043 return "Scopes.DEFAULT";
crazybobleef33d23e2007-02-12 04:17:48 +000044 }
crazyboblee68d2f4b2007-02-07 18:47:24 +000045 };
46
47 /**
crazybobleebaaaf2d2007-02-07 03:35:09 +000048 * One instance per container.
crazyboblee63b592b2007-01-25 02:45:24 +000049 */
crazyboblee68d2f4b2007-02-07 18:47:24 +000050 public static final Scope CONTAINER = new Scope() {
crazybobleee5fbbb02007-02-05 07:00:27 +000051 public <T> Factory<T> scope(Key<T> key, final Factory<T> creator) {
52 return new Factory<T>() {
53
54 private volatile T instance;
55
crazybobleef33d23e2007-02-12 04:17:48 +000056 // DCL on a volatile is safe as of Java 5, which we obviously require.
kevinb9n6a565c72007-02-11 01:58:33 +000057 @SuppressWarnings("DoubleCheckedLocking")
crazybobleee5fbbb02007-02-05 07:00:27 +000058 public T get() {
crazybobleee5fbbb02007-02-05 07:00:27 +000059 if (instance == null) {
kevinb9na99dca72007-02-11 04:48:57 +000060 /*
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 */
crazybobleee5fbbb02007-02-05 07:00:27 +000066 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 }
crazybobleef33d23e2007-02-12 04:17:48 +000080
81 public String toString() {
crazybobleeb1f2e682007-02-15 05:14:32 +000082 return "Scopes.CONTAINER";
crazybobleef33d23e2007-02-12 04:17:48 +000083 }
crazyboblee68d2f4b2007-02-07 18:47:24 +000084 };
crazybobleef33d23e2007-02-12 04:17:48 +000085
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,
crazybobleeb1f2e682007-02-15 05:14:32 +000095 Map<Class<? extends Annotation>, Scope> scopes,
crazybobleef33d23e2007-02-12 04:17:48 +000096 ErrorHandler errorHandler) {
crazybobleeb1f2e682007-02-15 05:14:32 +000097 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 }
crazybobleef33d23e2007-02-12 04:17:48 +0000108 }
crazybobleeb1f2e682007-02-15 05:14:32 +0000109 return found;
crazybobleef33d23e2007-02-12 04:17:48 +0000110 }
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 }
crazybobleef33d23e2007-02-12 04:17:48 +0000122 Factory<T> scoped = scope.scope(key,
123 new FactoryToInternalFactoryAdapter<T>(container, creator));
124 return new InternalFactoryToFactoryAdapter<T>(scoped);
125 }
kevinb9na99dca72007-02-11 04:48:57 +0000126}