crazyboblee | 66b415a | 2006-08-25 02:01:19 +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 | */ |
| 16 | |
| 17 | package com.google.inject; |
| 18 | |
| 19 | import java.util.HashMap; |
| 20 | import java.util.Map; |
| 21 | |
| 22 | /** |
| 23 | * Internal context. Used to coordinate injections and support circular |
| 24 | * dependencies. |
| 25 | * |
| 26 | * @author crazybob@google.com (Bob Lee) |
| 27 | */ |
| 28 | class InternalContext { |
| 29 | |
| 30 | final ContainerImpl container; |
| 31 | final Map<Object, ConstructionContext<?>> constructionContexts = |
| 32 | new HashMap<Object, ConstructionContext<?>>(); |
| 33 | Scope.Strategy scopeStrategy; |
| 34 | ExternalContext<?> externalContext; |
| 35 | |
| 36 | InternalContext(ContainerImpl container) { |
| 37 | this.container = container; |
| 38 | } |
| 39 | |
| 40 | public Container getContainer() { |
| 41 | return container; |
| 42 | } |
| 43 | |
| 44 | ContainerImpl getContainerImpl() { |
| 45 | return container; |
| 46 | } |
| 47 | |
| 48 | Scope.Strategy getScopeStrategy() { |
| 49 | if (scopeStrategy == null) { |
| 50 | scopeStrategy = container.localScopeStrategy.get(); |
| 51 | |
| 52 | if (scopeStrategy == null) { |
| 53 | throw new IllegalStateException("Scope strategy not set. " |
| 54 | + "Please call Container.setScopeStrategy()."); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return scopeStrategy; |
| 59 | } |
| 60 | |
| 61 | @SuppressWarnings("unchecked") |
| 62 | <T> ConstructionContext<T> getConstructionContext(Object key) { |
| 63 | ConstructionContext<T> constructionContext = |
| 64 | (ConstructionContext<T>) constructionContexts.get(key); |
| 65 | if (constructionContext == null) { |
| 66 | constructionContext = new ConstructionContext<T>(); |
| 67 | constructionContexts.put(key, constructionContext); |
| 68 | } |
| 69 | return constructionContext; |
| 70 | } |
| 71 | |
| 72 | @SuppressWarnings("unchecked") |
| 73 | <T> ExternalContext<T> getExternalContext() { |
| 74 | return (ExternalContext<T>) externalContext; |
| 75 | } |
| 76 | |
| 77 | void setExternalContext(ExternalContext<?> externalContext) { |
| 78 | this.externalContext = externalContext; |
| 79 | } |
| 80 | } |