crazyboblee | 6c7720d | 2007-03-01 21:49: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 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame^] | 19 | import com.google.inject.BindCommandProcessor.CreationListener; |
crazyboblee | 6c7720d | 2007-03-01 21:49:19 +0000 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * Delegates to a custom factory which is also bound in the injector. |
| 23 | */ |
| 24 | class BoundProviderFactory<T> |
| 25 | implements InternalFactory<T>, CreationListener { |
| 26 | |
| 27 | final Key<? extends Provider<? extends T>> providerKey; |
| 28 | final Object source; |
| 29 | private InternalFactory<? extends Provider<? extends T>> providerFactory; |
| 30 | |
| 31 | BoundProviderFactory( |
| 32 | Key<? extends Provider<? extends T>> providerKey, |
| 33 | Object source) { |
| 34 | this.providerKey = providerKey; |
| 35 | this.source = source; |
| 36 | } |
| 37 | |
| 38 | BoundProviderFactory( |
| 39 | Key<? extends Provider<? extends T>> providerKey, |
| 40 | InternalFactory<? extends Provider<? extends T>> providerFactory, |
| 41 | Object source) { |
| 42 | this.providerKey = providerKey; |
| 43 | this.providerFactory = providerFactory; |
| 44 | this.source = source; |
| 45 | } |
| 46 | |
| 47 | public void notify(final InjectorImpl injector) { |
| 48 | injector.withDefaultSource(source, new Runnable() { |
| 49 | public void run() { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 50 | providerFactory = injector.getInternalFactory(providerKey); |
crazyboblee | 6c7720d | 2007-03-01 21:49:19 +0000 | [diff] [blame] | 51 | } |
| 52 | }); |
| 53 | } |
| 54 | |
| 55 | public String toString() { |
| 56 | return providerKey.toString(); |
| 57 | } |
| 58 | |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 59 | public T get(InternalContext context, InjectionPoint<?> injectionPoint) { |
| 60 | Provider<? extends T> provider = providerFactory.get(context, injectionPoint); |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 61 | try { |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 62 | return injectionPoint.checkForNull(provider.get(), source); |
limpbizkit | b946ca2 | 2007-08-25 15:56:50 +0000 | [diff] [blame] | 63 | } catch(ProvisionException e) { |
| 64 | throw e; |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 65 | } catch(RuntimeException e) { |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 66 | throw new ProvisionException(e, ErrorMessages.ERROR_IN_PROVIDER); |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 67 | } |
crazyboblee | 6c7720d | 2007-03-01 21:49:19 +0000 | [diff] [blame] | 68 | } |
| 69 | } |