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