blob: 1e68b741f5efe867082c499482d51f5201e2aea9 [file] [log] [blame]
crazyboblee6c7720d2007-03-01 21:49:19 +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 */
16
limpbizkit5ae41eb2009-06-06 17:51:27 +000017package com.google.inject.internal;
crazyboblee6c7720d2007-03-01 21:49:19 +000018
limpbizkit5ae41eb2009-06-06 17:51:27 +000019import com.google.inject.Key;
20import com.google.inject.Provider;
21import com.google.inject.internal.BindingProcessor.CreationListener;
limpbizkita98bc7a2008-08-29 16:52:44 +000022import com.google.inject.spi.Dependency;
crazyboblee6c7720d2007-03-01 21:49:19 +000023
24/**
25 * Delegates to a custom factory which is also bound in the injector.
26 */
limpbizkit5ae41eb2009-06-06 17:51:27 +000027final class BoundProviderFactory<T> implements InternalFactory<T>, CreationListener {
crazyboblee6c7720d2007-03-01 21:49:19 +000028
limpbizkitfcbdf992008-11-26 02:37:35 +000029 private final InjectorImpl injector;
crazyboblee6c7720d2007-03-01 21:49:19 +000030 final Key<? extends Provider<? extends T>> providerKey;
31 final Object source;
32 private InternalFactory<? extends Provider<? extends T>> providerFactory;
33
34 BoundProviderFactory(
limpbizkitfcbdf992008-11-26 02:37:35 +000035 InjectorImpl injector,
crazyboblee6c7720d2007-03-01 21:49:19 +000036 Key<? extends Provider<? extends T>> providerKey,
37 Object source) {
limpbizkitfcbdf992008-11-26 02:37:35 +000038 this.injector = injector;
crazyboblee6c7720d2007-03-01 21:49:19 +000039 this.providerKey = providerKey;
40 this.source = source;
41 }
42
limpbizkitc3f92842008-12-30 19:43:47 +000043 public void notify(Errors errors) {
limpbizkit163c48a2008-06-16 02:58:08 +000044 try {
limpbizkit2c2c6102008-06-20 13:34:36 +000045 providerFactory = injector.getInternalFactory(providerKey, errors.withSource(source));
limpbizkit163c48a2008-06-16 02:58:08 +000046 } catch (ErrorsException e) {
47 errors.merge(e.getErrors());
limpbizkit163c48a2008-06-16 02:58:08 +000048 }
crazyboblee6c7720d2007-03-01 21:49:19 +000049 }
50
limpbizkita98bc7a2008-08-29 16:52:44 +000051 public T get(Errors errors, InternalContext context, Dependency<?> dependency)
limpbizkit163c48a2008-06-16 02:58:08 +000052 throws ErrorsException {
limpbizkit06898062008-11-02 05:14:55 +000053 errors = errors.withSource(providerKey);
limpbizkita98bc7a2008-08-29 16:52:44 +000054 Provider<? extends T> provider = providerFactory.get(errors, context, dependency);
limpbizkit1dabcfd2007-08-25 08:06:30 +000055 try {
limpbizkita98bc7a2008-08-29 16:52:44 +000056 return errors.checkForNull(provider.get(), source, dependency);
limpbizkit163c48a2008-06-16 02:58:08 +000057 } catch(RuntimeException userException) {
limpbizkit06898062008-11-02 05:14:55 +000058 throw errors.errorInProvider(userException).toException();
limpbizkit1dabcfd2007-08-25 08:06:30 +000059 }
crazyboblee6c7720d2007-03-01 21:49:19 +000060 }
limpbizkitc9465f92008-05-31 19:01:54 +000061
62 @Override public String toString() {
63 return providerKey.toString();
64 }
crazyboblee6c7720d2007-03-01 21:49:19 +000065}