KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > configuration > spring > ConfigurationProviderImpl


1 package org.objectweb.celtix.bus.configuration.spring;
2
3 import java.lang.reflect.Method JavaDoc;
4 import java.net.MalformedURLException JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.logging.Level JavaDoc;
10 import java.util.logging.Logger JavaDoc;
11
12 import org.objectweb.celtix.common.i18n.Message;
13 import org.objectweb.celtix.common.logging.LogUtils;
14 import org.objectweb.celtix.configuration.Configuration;
15 import org.objectweb.celtix.configuration.ConfigurationException;
16 import org.objectweb.celtix.configuration.ConfigurationProvider;
17 import org.objectweb.celtix.jaxb.JAXBUtils;
18 import org.objectweb.celtix.tools.generators.spring.SpringUtils;
19 import org.springframework.beans.BeansException;
20 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
21 import org.springframework.core.io.UrlResource;
22
23
24 public class ConfigurationProviderImpl implements ConfigurationProvider {
25
26     public static final String JavaDoc CONFIG_FILE_PROPERTY_NAME = "celtix.config.file";
27
28
29     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(ConfigurationProviderImpl.class);
30     private static Map JavaDoc<UrlResource, CeltixXmlBeanFactory> beanFactories;
31
32     private Object JavaDoc bean;
33     private Configuration configuration;
34
35
36     public static void clearBeanFactoriesMap() {
37         beanFactories = null;
38     }
39
40     public void init(Configuration c) {
41         configuration = c;
42
43         if (null == beanFactories) {
44             beanFactories = new HashMap JavaDoc<UrlResource, CeltixXmlBeanFactory>();
45         }
46
47         CeltixXmlBeanFactory beanFactory = null;
48         UrlResource urlRes = getBeanDefinitionsResource();
49         if (null != urlRes) {
50             if (!beanFactories.containsKey(urlRes)) {
51
52                 if (null != urlRes) {
53                     try {
54                         beanFactory = new CeltixXmlBeanFactory(urlRes);
55                     } catch (BeansException ex) {
56                         // continue without using configuration from the bean definitions
57
LOG.log(Level.WARNING, new Message("BEAN_FACTORY_CREATION_MSG", LOG, urlRes
58                                                            .toString()).toString(), ex);
59                     }
60                     beanFactories.put(urlRes, beanFactory);
61                 }
62             } else {
63                 beanFactory = beanFactories.get(urlRes);
64             }
65         }
66
67         if (null != beanFactory) {
68             beanFactory.registerCustomEditors(configuration);
69             findBean(beanFactory);
70         } else {
71             LOG.fine("Not using a bean definitions file.");
72         }
73
74     }
75
76     public Object JavaDoc getObject(String JavaDoc name) {
77         // TODO use BeanWrapper instead
78
if (null != bean) {
79             return invokeGetter(bean, name);
80         }
81         return null;
82     }
83
84     public boolean setObject(String JavaDoc name, Object JavaDoc value) {
85         if (null == bean) {
86             initBean();
87         }
88
89         if (null != bean) {
90             return invokeSetter(bean, value, name);
91         }
92
93         return false;
94     }
95
96     public boolean save() {
97         //TODO, two situations:
98
//1. the bean is created by spring XMLBeanFactory. As the spring XMLBeanFactory
99
//knows BeanDefinition, so it knows how to persist the bean to config file properly.
100
//2. The bean is created by ourself. In this case, we can not use spring XMLBeanFactory
101
//to do the persistence. We may need to find config schemas and using jaxb to persist
102
//bean by ourself
103

104         return false;
105     }
106
107     protected Object JavaDoc getBean() {
108         return bean;
109     }
110
111     protected static Map JavaDoc<UrlResource, CeltixXmlBeanFactory> getBeanFactories() {
112         return beanFactories;
113     }
114
115     private Object JavaDoc invokeGetter(Object JavaDoc beanObject, String JavaDoc name) {
116
117         String JavaDoc methodName = JAXBUtils.nameToIdentifier(name, JAXBUtils.IdentifierType.GETTER);
118         try {
119             Method JavaDoc m = beanObject.getClass().getMethod("isSet", new Class JavaDoc[] {String JavaDoc.class});
120             Object JavaDoc o = m.invoke(beanObject, new Object JavaDoc[] {name});
121             if (!((Boolean JavaDoc)o).booleanValue()) {
122                 return null;
123             }
124             m = beanObject.getClass().getMethod(methodName, new Class JavaDoc[] {});
125             return m.invoke(beanObject);
126
127         } catch (Exception JavaDoc ex) {
128             throw new ConfigurationException(new Message("BEAN_INCOVATION_EXC", LOG), ex);
129         }
130     }
131
132     private boolean invokeSetter(Object JavaDoc beanObject, Object JavaDoc value, String JavaDoc name) {
133
134         String JavaDoc methodName = JAXBUtils.nameToIdentifier(name, JAXBUtils.IdentifierType.SETTER);
135         try {
136             Class JavaDoc[] para = new Class JavaDoc[1];
137
138             if (value.getClass() == Integer JavaDoc.class) {
139                 para[0] = Integer.TYPE;
140             } else if (value.getClass() == Float JavaDoc.class) {
141                 para[0] = Float.TYPE;
142             } else if (value.getClass() == Double JavaDoc.class) {
143                 para[0] = Double.TYPE;
144             } else if (value.getClass() == Boolean JavaDoc.class) {
145                 para[0] = Boolean.TYPE;
146             } else if (value.getClass() == Long JavaDoc.class) {
147                 para[0] = Long.TYPE;
148             } else if (value.getClass() == Short JavaDoc.class) {
149                 para[0] = Short.TYPE;
150             } else if (value.getClass() == Character JavaDoc.class) {
151                 para[0] = Character.TYPE;
152             } else if (value.getClass() == Byte JavaDoc.class) {
153                 para[0] = Byte.TYPE;
154             } else {
155                 para[0] = value.getClass();
156             }
157
158             Method JavaDoc m = beanObject.getClass().getMethod(methodName, para);
159             m.invoke(beanObject, value);
160             return true;
161
162         } catch (Exception JavaDoc ex) {
163             ex.printStackTrace();
164             throw new ConfigurationException(new Message("BEAN_INCOVATION_EXC", LOG), ex);
165         }
166     }
167
168     private void initBean() {
169         String JavaDoc beanClassName =
170             SpringUtils.getBeanClassName(configuration.getModel().getNamespaceURI());
171         Class JavaDoc beanClass = null;
172         try {
173             beanClass = Class.forName(beanClassName);
174         } catch (ClassCastException JavaDoc ex) {
175             LOG.log(Level.SEVERE, "Could not load bean class " + beanClassName, ex);
176             return;
177         } catch (ClassNotFoundException JavaDoc ex) {
178             LOG.log(Level.SEVERE, "Could not load bean class " + beanClassName, ex);
179             return;
180         }
181
182         try {
183             bean = beanClass.newInstance();
184         } catch (Exception JavaDoc e) {
185             LOG.log(Level.SEVERE, "Could not create bean instance " + beanClassName, e);
186             return;
187         }
188     }
189
190     /**
191      * get the id of the ancestor configuration and look for a correspondingly named file
192      * with extension .xml in the directory pointed to by system property
193      * celtix.config.dir
194      * @param id
195      * @return
196      */

197
198     protected UrlResource getBeanDefinitionsResource() {
199
200         UrlResource urlRes = null;
201         String JavaDoc url = System.getProperty(CONFIG_FILE_PROPERTY_NAME);
202         if (null != url) {
203             try {
204                 urlRes = new UrlResource(url);
205             } catch (MalformedURLException JavaDoc ex) {
206                 // continue using default configuration
207
LOG.log(Level.WARNING, new Message("MALFORMED_URL_MSG", LOG, url).toString(), ex);
208             }
209
210             return urlRes;
211         }
212         return null;
213     }
214
215     private void findBean(CeltixXmlBeanFactory beanFactory) {
216
217         String JavaDoc beanClassName = SpringUtils.getBeanClassName(configuration.getModel().getNamespaceURI());
218         Class JavaDoc beanClass = null;
219         try {
220             beanClass = Class.forName(beanClassName);
221         } catch (ClassCastException JavaDoc ex) {
222             LOG.log(Level.SEVERE, "Could not load bean class " + beanClassName, ex);
223             return;
224         } catch (ClassNotFoundException JavaDoc ex) {
225             LOG.log(Level.SEVERE, "Could not load bean class " + beanClassName, ex);
226             return;
227         }
228
229         String JavaDoc[] candidates = beanFactory.getBeanNamesForType(beanClass);
230         if (null == candidates || candidates.length == 0) {
231             bean = null;
232             if (LOG.isLoggable(Level.FINE)) {
233                 LOG.fine("No definitions for beans of type " + beanClass.getName());
234             }
235             return;
236         }
237
238         List JavaDoc<BeanName> beanNames = new ArrayList JavaDoc<BeanName>();
239         for (String JavaDoc n : candidates) {
240             BeanName bn = new BeanName(n);
241             bn.normalise();
242             beanNames.add(bn);
243         }
244
245         BeanName ref = new BeanName(configuration);
246         BeanName beanName = ref.findBestMatch(beanNames);
247
248         if (null != beanName) {
249             try {
250                 bean = beanFactory.getBean(beanName.getName(), beanClass);
251             } catch (NoSuchBeanDefinitionException ex) {
252                 if (LOG.isLoggable(Level.FINE)) {
253                     LOG.fine("Could not find definition for bean with id " + beanName);
254                 }
255             } catch (BeansException ex) {
256                 throw new ConfigurationException(new Message("BEAN_CREATION_EXC", LOG, beanName), ex);
257             }
258         }
259
260         if (null == bean && LOG.isLoggable(Level.INFO)) {
261             LOG.info("Could not find matching bean definition for component " + ref.getName());
262         }
263     }
264 }
265
Popular Tags