KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > spring > SpringContainer


1 /*
2  * Copyright 2005 Joe Walker
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 package org.directwebremoting.spring;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Arrays JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.directwebremoting.Container;
26 import org.directwebremoting.impl.DefaultContainer;
27 import org.directwebremoting.util.Logger;
28 import org.springframework.beans.BeansException;
29 import org.springframework.beans.factory.BeanFactory;
30 import org.springframework.beans.factory.BeanFactoryAware;
31 import org.springframework.beans.factory.InitializingBean;
32 import org.springframework.beans.factory.ListableBeanFactory;
33 import org.springframework.util.ClassUtils;
34
35 /**
36  * A <code>Container</code> implementation that looks up all beans from the
37  * configuration specified in a Spring context.
38  * It loads the configuration from a Spring web application context.
39  * @author Bram Smeets
40  * @author Joe Walker [joe at getahead dot ltd dot uk]
41  */

42 public class SpringContainer extends DefaultContainer implements Container, BeanFactoryAware, InitializingBean
43 {
44     /* (non-Javadoc)
45      * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
46      */

47     public void setBeanFactory(BeanFactory beanFactory) throws BeansException
48     {
49         this.beanFactory = beanFactory;
50     }
51
52     public void addParameter(Object JavaDoc askFor, Object JavaDoc valueParam)
53     throws InstantiationException JavaDoc, IllegalAccessException JavaDoc {
54         try {
55             Class JavaDoc clz = ClassUtils.forName((String JavaDoc)askFor);
56             if (log.isDebugEnabled()) {
57                 log.debug("trying to resolve the following class from the Spring bean container: " + clz.getName());
58             }
59
60             Map JavaDoc beansOfType = ((ListableBeanFactory)beanFactory).getBeansOfType(clz);
61             if (log.isDebugEnabled()) {
62                 log.debug("beans: " + beansOfType + " - " + beansOfType.size());
63             }
64             if (beansOfType.size() == 0) {
65                 log.debug("adding parameter the normal way");
66                 super.addParameter(askFor, valueParam);
67             } else if (beansOfType.size() > 1) {
68                 // TODO: handle multiple declarations
69
throw new InstantiationException JavaDoc("multiple beans of type '" + clz.getName() + "' were found in the spring configuration");
70             } else {
71                 this.beans.put(askFor, beansOfType.values().iterator().next());
72             }
73         } catch (ClassNotFoundException JavaDoc e) {
74             super.addParameter(askFor, valueParam);
75         }
76     }
77
78     /* (non-Javadoc)
79      * @see org.directwebremoting.impl.DefaultContainer#getBean(java.lang.String)
80      */

81     public Object JavaDoc getBean(String JavaDoc id)
82     {
83         Object JavaDoc reply;
84         try {
85             reply = beanFactory.getBean(id);
86         }
87         catch (BeansException ex)
88         {
89             // Spring throws on not-found, we return null.
90
reply = super.getBean(id);
91         }
92
93         return reply;
94     }
95
96     /* (non-Javadoc)
97      * @see org.directwebremoting.impl.DefaultContainer#getBeanNames()
98      */

99     public Collection JavaDoc getBeanNames()
100     {
101         List JavaDoc names = new ArrayList JavaDoc();
102
103         // Snarf the beans from Spring
104
if (beanFactory instanceof ListableBeanFactory)
105         {
106             ListableBeanFactory listable = (ListableBeanFactory) beanFactory;
107             names.addAll(Arrays.asList(listable.getBeanDefinitionNames()));
108         }
109         else
110         {
111             log.warn("List of beanNames does not include Spring beans since your BeanFactory is not a ListableBeanFactory.");
112         }
113
114         // And append the DWR ones
115
names.addAll(super.getBeanNames());
116
117         return Collections.unmodifiableCollection(names);
118     }
119
120     /* (non-Javadoc)
121      * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
122      */

123     public void afterPropertiesSet() throws Exception JavaDoc
124     {
125         callInitializingBeans();
126     }
127
128     /**
129      * The Spring BeanFactory that we read from
130      */

131     protected BeanFactory beanFactory;
132
133     /**
134      * The log stream
135      */

136     private static final Logger log = Logger.getLogger(SpringContainer.class);
137 }
138
Popular Tags