KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > inject > spring > SpringIntegration


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.spring;
18
19 import static com.google.inject.util.Objects.nonNull;
20 import com.google.inject.Provider;
21 import com.google.inject.Inject;
22 import com.google.inject.Binder;
23 import com.google.inject.name.Names;
24 import com.google.inject.spi.SourceProviders;
25 import com.google.inject.util.Objects;
26 import org.springframework.beans.factory.BeanFactory;
27 import org.springframework.beans.factory.ListableBeanFactory;
28
29 /**
30  * Integrates Guice with Spring.
31  *
32  * @author crazybob@google.com (Bob Lee)
33  */

34 public class SpringIntegration {
35
36   static {
37     SourceProviders.skip(SpringIntegration.class);
38   }
39
40   private SpringIntegration() {}
41
42   /**
43    * Creates a provider which looks up objects from Spring using the given name.
44    * Expects a binding to {@link
45    * org.springframework.beans.factory.BeanFactory}. Example usage:
46    *
47    * <pre>
48    * bind(DataSource.class)
49    * .toProvider(fromSpring(DataSource.class, "dataSource"));
50    * </pre>
51    */

52   public static <T> Provider<T> fromSpring(Class JavaDoc<T> type, String JavaDoc name) {
53     return new InjectableSpringProvider<T>(type, name);
54   }
55
56   /**
57    * Binds all Spring beans from the given factory by name. For a Spring bean
58    * named "foo", this method creates a binding to the bean's type and
59    * {@code @Named("foo")}.
60    *
61    * @see com.google.inject.name.Named
62    * @see com.google.inject.name.Names#named(String)
63    */

64   public static void bindAll(Binder binder, ListableBeanFactory beanFactory) {
65     for (String JavaDoc name : beanFactory.getBeanDefinitionNames()) {
66       Class JavaDoc<?> type = beanFactory.getType(name);
67       bindBean(binder, beanFactory, name, type);
68     }
69   }
70
71   static <T> void bindBean(Binder binder, ListableBeanFactory beanFactory,
72       String JavaDoc name, Class JavaDoc<T> type) {
73     SpringProvider<T> provider
74         = SpringProvider.newInstance(type, name);
75     try {
76       provider.initialize(beanFactory);
77     }
78     catch (Exception JavaDoc e) {
79       binder.addError(e);
80       return;
81     }
82
83     binder.bind(type)
84         .annotatedWith(Names.named(name))
85         .toProvider(provider);
86   }
87
88   static class SpringProvider<T> implements Provider<T> {
89
90     BeanFactory beanFactory;
91     boolean singleton;
92     final Class JavaDoc<T> type;
93     final String JavaDoc name;
94
95     public SpringProvider(Class JavaDoc<T> type, String JavaDoc name) {
96       this.type = nonNull(type, "type");
97       this.name = nonNull(name, "name");
98     }
99
100     static <T> SpringProvider<T> newInstance(Class JavaDoc<T> type, String JavaDoc name) {
101       return new SpringProvider<T>(type, name);
102     }
103
104     void initialize(BeanFactory beanFactory) {
105       this.beanFactory = beanFactory;
106       if (!beanFactory.isTypeMatch(name, type)) {
107         throw new ClassCastException JavaDoc("Spring bean named '" + name
108             + "' does not implement " + type.getName() + ".");
109       }
110       singleton = beanFactory.isSingleton(name);
111     }
112
113     public T get() {
114       return singleton ? getSingleton() : type.cast(beanFactory.getBean(name));
115     }
116
117     volatile T instance;
118
119     private T getSingleton() {
120       if (instance == null) {
121         instance = type.cast(beanFactory.getBean(name));
122       }
123       return instance;
124     }
125   }
126
127   static class InjectableSpringProvider<T> extends SpringProvider<T> {
128
129     InjectableSpringProvider(Class JavaDoc<T> type, String JavaDoc name) {
130       super(type, name);
131     }
132
133     @Inject
134     @Override JavaDoc
135     void initialize(BeanFactory beanFactory) {
136       super.initialize(beanFactory);
137     }
138   }
139 }
140
Popular Tags