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 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 17 | package com.google.inject.internal; |
crazyboblee | 6c7720d | 2007-03-01 21:49:19 +0000 | [diff] [blame] | 18 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 19 | import com.google.inject.Key; |
| 20 | import com.google.inject.Provider; |
| 21 | import com.google.inject.internal.BindingProcessor.CreationListener; |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 22 | import com.google.inject.spi.Dependency; |
crazyboblee | 6c7720d | 2007-03-01 21:49:19 +0000 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Delegates to a custom factory which is also bound in the injector. |
| 26 | */ |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 27 | final class BoundProviderFactory<T> implements InternalFactory<T>, CreationListener { |
crazyboblee | 6c7720d | 2007-03-01 21:49:19 +0000 | [diff] [blame] | 28 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 29 | private final InjectorImpl injector; |
crazyboblee | 6c7720d | 2007-03-01 21:49:19 +0000 | [diff] [blame] | 30 | final Key<? extends Provider<? extends T>> providerKey; |
| 31 | final Object source; |
| 32 | private InternalFactory<? extends Provider<? extends T>> providerFactory; |
| 33 | |
| 34 | BoundProviderFactory( |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 35 | InjectorImpl injector, |
crazyboblee | 6c7720d | 2007-03-01 21:49:19 +0000 | [diff] [blame] | 36 | Key<? extends Provider<? extends T>> providerKey, |
| 37 | Object source) { |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 38 | this.injector = injector; |
crazyboblee | 6c7720d | 2007-03-01 21:49:19 +0000 | [diff] [blame] | 39 | this.providerKey = providerKey; |
| 40 | this.source = source; |
| 41 | } |
| 42 | |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 43 | public void notify(Errors errors) { |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 44 | try { |
limpbizkit | 2c2c610 | 2008-06-20 13:34:36 +0000 | [diff] [blame] | 45 | providerFactory = injector.getInternalFactory(providerKey, errors.withSource(source)); |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 46 | } catch (ErrorsException e) { |
| 47 | errors.merge(e.getErrors()); |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 48 | } |
crazyboblee | 6c7720d | 2007-03-01 21:49:19 +0000 | [diff] [blame] | 49 | } |
| 50 | |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 51 | public T get(Errors errors, InternalContext context, Dependency<?> dependency) |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 52 | throws ErrorsException { |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 53 | errors = errors.withSource(providerKey); |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 54 | Provider<? extends T> provider = providerFactory.get(errors, context, dependency); |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 55 | try { |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 56 | return errors.checkForNull(provider.get(), source, dependency); |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 57 | } catch(RuntimeException userException) { |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 58 | throw errors.errorInProvider(userException).toException(); |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 59 | } |
crazyboblee | 6c7720d | 2007-03-01 21:49:19 +0000 | [diff] [blame] | 60 | } |
limpbizkit | c9465f9 | 2008-05-31 19:01:54 +0000 | [diff] [blame] | 61 | |
| 62 | @Override public String toString() { |
| 63 | return providerKey.toString(); |
| 64 | } |
crazyboblee | 6c7720d | 2007-03-01 21:49:19 +0000 | [diff] [blame] | 65 | } |