KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.servlet.ServletContext JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25
26 import org.directwebremoting.WebContextFactory;
27 import org.directwebremoting.create.AbstractCreator;
28 import org.directwebremoting.extend.Creator;
29 import org.directwebremoting.util.LocalUtil;
30 import org.directwebremoting.util.Logger;
31 import org.directwebremoting.util.Messages;
32 import org.springframework.beans.factory.BeanFactory;
33 import org.springframework.context.support.ClassPathXmlApplicationContext;
34 import org.springframework.web.context.support.WebApplicationContextUtils;
35 import org.springframework.web.servlet.support.RequestContextUtils;
36
37 /**
38  * A creator that relies on a spring bean factory.
39  * @author Joe Walker [joe at getahead dot ltd dot uk]
40  */

41 public class SpringCreator extends AbstractCreator implements Creator
42 {
43     /**
44      * @return Returns the beanName.
45      */

46     public String JavaDoc getBeanName()
47     {
48         return beanName;
49     }
50
51     /**
52      * @param beanName The beanName to set.
53      */

54     public void setBeanName(String JavaDoc beanName)
55     {
56         this.beanName = beanName;
57     }
58
59     /**
60      * What sort of class do we create?
61      * @param classname The name of the class
62      */

63     public void setClass(String JavaDoc classname)
64     {
65         try
66         {
67             this.clazz = LocalUtil.classForName(classname);
68         }
69         catch (ClassNotFoundException JavaDoc ex)
70         {
71             throw new IllegalArgumentException JavaDoc(Messages.getString("Creator.ClassNotFound", classname));
72         }
73     }
74
75     /* (non-Javadoc)
76      * @see org.directwebremoting.create.Creator#init(org.w3c.dom.Element)
77      */

78     public void setProperties(Map JavaDoc params) throws IllegalArgumentException JavaDoc
79     {
80         List JavaDoc locValues = new ArrayList JavaDoc();
81
82         for (Iterator JavaDoc it = params.entrySet().iterator(); it.hasNext();)
83         {
84             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
85             String JavaDoc key = (String JavaDoc) entry.getKey();
86             String JavaDoc value = (String JavaDoc) entry.getValue();
87             if (key.startsWith("location"))
88             {
89                 log.debug("Adding configLocation: " + value + " from parameter: " + key);
90                 locValues.add(value);
91             }
92         }
93
94         configLocation = (String JavaDoc[]) locValues.toArray(new String JavaDoc[locValues.size()]);
95     }
96
97     /* (non-Javadoc)
98      * @see org.directwebremoting.Creator#getType()
99      */

100     public Class JavaDoc getType()
101     {
102         if (clazz == null)
103         {
104             try
105             {
106                 clazz = getInstance().getClass();
107             }
108             catch (InstantiationException JavaDoc ex)
109             {
110                 log.error("Failed to instansiate object to detect type.", ex);
111                 return Object JavaDoc.class;
112             }
113         }
114
115         return clazz;
116     }
117
118     /* (non-Javadoc)
119      * @see org.directwebremoting.Creator#getInstance()
120      */

121     public Object JavaDoc getInstance() throws InstantiationException JavaDoc
122     {
123         try
124         {
125             if (overrideFactory != null)
126             {
127                 return overrideFactory.getBean(beanName);
128             }
129
130             if (factory == null)
131             {
132                 factory = getBeanFactory();
133             }
134
135             if (factory == null)
136             {
137                 log.error("DWR can't find a spring config. See following info logs for solutions");
138                 log.info("- Option 1. In dwr.xml, <create creator='spring' ...> add <param name='location1' value='beans.xml'/> for each spring config file.");
139                 log.info("- Option 2. Use a spring org.springframework.web.context.ContextLoaderListener.");
140                 log.info("- Option 3. Call SpringCreator.setOverrideBeanFactory() from your web-app");
141                 throw new InstantiationException JavaDoc(Messages.getString("SpringCreator.MissingConfig"));
142             }
143
144             return factory.getBean(beanName);
145         }
146         catch (RuntimeException JavaDoc ex)
147         {
148             throw ex;
149         }
150         catch (Exception JavaDoc ex)
151         {
152             log.error("Error", ex);
153             throw new InstantiationException JavaDoc(ex.toString());
154         }
155     }
156
157     /**
158      * @return A found BeanFactory configuration
159      */

160     private BeanFactory getBeanFactory()
161     {
162         // If someone has set a resource name then we need to load that.
163
if (configLocation != null && configLocation.length > 0)
164         {
165             log.info("Spring BeanFactory via ClassPathXmlApplicationContext using " + configLocation.length + "configLocations.");
166             return new ClassPathXmlApplicationContext(configLocation);
167         }
168
169         ServletContext JavaDoc srvCtx = WebContextFactory.get().getServletContext();
170         HttpServletRequest JavaDoc request = WebContextFactory.get().getHttpServletRequest();
171
172         if (request != null)
173         {
174             return RequestContextUtils.getWebApplicationContext(request, srvCtx);
175         }
176         else
177         {
178             return WebApplicationContextUtils.getWebApplicationContext(srvCtx);
179         }
180     }
181
182     /**
183      * Set a web-app wide BeanFactory.
184      * This method is misnamed (The parameter is a BeanFactory and not a
185      * XmlBeanFactory)
186      * @param factory The factory to set.
187      * @deprecated This method is misnamed use setOverrideBeanFactory
188      */

189     public static void setXmlBeanFactory(BeanFactory factory)
190     {
191         SpringCreator.overrideFactory = factory;
192     }
193
194     /**
195      * Set a web-app wide BeanFactory.
196      * @param factory The factory to set.
197      */

198     public static void setOverrideBeanFactory(BeanFactory factory)
199     {
200         SpringCreator.overrideFactory = factory;
201     }
202
203     /**
204      * The name of the spring bean we want to create
205      */

206     private String JavaDoc beanName = null;
207
208     /**
209      * The log stream
210      */

211     private static final Logger log = Logger.getLogger(SpringCreator.class);
212
213     /**
214      * The Spring beans factory from which we get our beans.
215      */

216     private BeanFactory factory = null;
217
218     /**
219      * An web-app wide BeanFactory that we can use to override any calculated
220      * ones.
221      */

222     private static BeanFactory overrideFactory = null;
223
224     /**
225      * The cached type of bean that we are creating
226      */

227     private Class JavaDoc clazz = null;
228
229     /**
230      * An array of locations to search through for a beans.xml file
231      */

232     private String JavaDoc[] configLocation = null;
233 }
234
Popular Tags