blob: 6528a9b7de6611b73506e5bea8801ccefef1c6a0 [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
17package com.google.inject;
18
limpbizkit00ca9f72008-08-02 17:56:17 +000019import com.google.inject.BindingProcessor.CreationListener;
limpbizkit9dc32d42008-06-15 11:29:10 +000020import com.google.inject.internal.Errors;
limpbizkit163c48a2008-06-16 02:58:08 +000021import com.google.inject.internal.ErrorsException;
limpbizkit76c24b12008-12-25 04:32:41 +000022import com.google.inject.internal.InternalContext;
23import com.google.inject.internal.InternalFactory;
limpbizkita98bc7a2008-08-29 16:52:44 +000024import com.google.inject.spi.Dependency;
crazyboblee6c7720d2007-03-01 21:49:19 +000025
26/**
27 * Delegates to a custom factory which is also bound in the injector.
28 */
limpbizkit9dc32d42008-06-15 11:29:10 +000029class BoundProviderFactory<T> implements InternalFactory<T>, CreationListener {
crazyboblee6c7720d2007-03-01 21:49:19 +000030
limpbizkitfcbdf992008-11-26 02:37:35 +000031 private final InjectorImpl injector;
crazyboblee6c7720d2007-03-01 21:49:19 +000032 final Key<? extends Provider<? extends T>> providerKey;
33 final Object source;
34 private InternalFactory<? extends Provider<? extends T>> providerFactory;
35
36 BoundProviderFactory(
limpbizkitfcbdf992008-11-26 02:37:35 +000037 InjectorImpl injector,
crazyboblee6c7720d2007-03-01 21:49:19 +000038 Key<? extends Provider<? extends T>> providerKey,
39 Object source) {
limpbizkitfcbdf992008-11-26 02:37:35 +000040 this.injector = injector;
crazyboblee6c7720d2007-03-01 21:49:19 +000041 this.providerKey = providerKey;
42 this.source = source;
43 }
44
limpbizkitc3f92842008-12-30 19:43:47 +000045 public void notify(Errors errors) {
limpbizkit163c48a2008-06-16 02:58:08 +000046 try {
limpbizkit2c2c6102008-06-20 13:34:36 +000047 providerFactory = injector.getInternalFactory(providerKey, errors.withSource(source));
limpbizkit163c48a2008-06-16 02:58:08 +000048 } catch (ErrorsException e) {
49 errors.merge(e.getErrors());
limpbizkit163c48a2008-06-16 02:58:08 +000050 }
crazyboblee6c7720d2007-03-01 21:49:19 +000051 }
52
limpbizkita98bc7a2008-08-29 16:52:44 +000053 public T get(Errors errors, InternalContext context, Dependency<?> dependency)
limpbizkit163c48a2008-06-16 02:58:08 +000054 throws ErrorsException {
limpbizkit06898062008-11-02 05:14:55 +000055 errors = errors.withSource(providerKey);
limpbizkita98bc7a2008-08-29 16:52:44 +000056 Provider<? extends T> provider = providerFactory.get(errors, context, dependency);
limpbizkit1dabcfd2007-08-25 08:06:30 +000057 try {
limpbizkita98bc7a2008-08-29 16:52:44 +000058 return errors.checkForNull(provider.get(), source, dependency);
limpbizkit163c48a2008-06-16 02:58:08 +000059 } catch(RuntimeException userException) {
limpbizkit06898062008-11-02 05:14:55 +000060 throw errors.errorInProvider(userException).toException();
limpbizkit1dabcfd2007-08-25 08:06:30 +000061 }
crazyboblee6c7720d2007-03-01 21:49:19 +000062 }
limpbizkitc9465f92008-05-31 19:01:54 +000063
64 @Override public String toString() {
65 return providerKey.toString();
66 }
crazyboblee6c7720d2007-03-01 21:49:19 +000067}