| 1 package org.objectweb.celtix.bus.configuration.spring; 2 3 import java.lang.reflect.Method ; 4 import java.net.MalformedURLException ; 5 import java.util.ArrayList ; 6 import java.util.HashMap ; 7 import java.util.List ; 8 import java.util.Map ; 9 import java.util.logging.Level ; 10 import java.util.logging.Logger ; 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 CONFIG_FILE_PROPERTY_NAME = "celtix.config.file"; 27 28 29 private static final Logger LOG = LogUtils.getL7dLogger(ConfigurationProviderImpl.class); 30 private static Map <UrlResource, CeltixXmlBeanFactory> beanFactories; 31 32 private Object 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 <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 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 getObject(String name) { 77 if (null != bean) { 79 return invokeGetter(bean, name); 80 } 81 return null; 82 } 83 84 public boolean setObject(String name, Object 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 104 return false; 105 } 106 107 protected Object getBean() { 108 return bean; 109 } 110 111 protected static Map <UrlResource, CeltixXmlBeanFactory> getBeanFactories() { 112 return beanFactories; 113 } 114 115 private Object invokeGetter(Object beanObject, String name) { 116 117 String methodName = JAXBUtils.nameToIdentifier(name, JAXBUtils.IdentifierType.GETTER); 118 try { 119 Method m = beanObject.getClass().getMethod("isSet", new Class [] {String .class}); 120 Object o = m.invoke(beanObject, new Object [] {name}); 121 if (!((Boolean )o).booleanValue()) { 122 return null; 123 } 124 m = beanObject.getClass().getMethod(methodName, new Class [] {}); 125 return m.invoke(beanObject); 126 127 } catch (Exception ex) { 128 throw new ConfigurationException(new Message("BEAN_INCOVATION_EXC", LOG), ex); 129 } 130 } 131 132 private boolean invokeSetter(Object beanObject, Object value, String name) { 133 134 String methodName = JAXBUtils.nameToIdentifier(name, JAXBUtils.IdentifierType.SETTER); 135 try { 136 Class [] para = new Class [1]; 137 138 if (value.getClass() == Integer .class) { 139 para[0] = Integer.TYPE; 140 } else if (value.getClass() == Float .class) { 141 para[0] = Float.TYPE; 142 } else if (value.getClass() == Double .class) { 143 para[0] = Double.TYPE; 144 } else if (value.getClass() == Boolean .class) { 145 para[0] = Boolean.TYPE; 146 } else if (value.getClass() == Long .class) { 147 para[0] = Long.TYPE; 148 } else if (value.getClass() == Short .class) { 149 para[0] = Short.TYPE; 150 } else if (value.getClass() == Character .class) { 151 para[0] = Character.TYPE; 152 } else if (value.getClass() == Byte .class) { 153 para[0] = Byte.TYPE; 154 } else { 155 para[0] = value.getClass(); 156 } 157 158 Method m = beanObject.getClass().getMethod(methodName, para); 159 m.invoke(beanObject, value); 160 return true; 161 162 } catch (Exception ex) { 163 ex.printStackTrace(); 164 throw new ConfigurationException(new Message("BEAN_INCOVATION_EXC", LOG), ex); 165 } 166 } 167 168 private void initBean() { 169 String beanClassName = 170 SpringUtils.getBeanClassName(configuration.getModel().getNamespaceURI()); 171 Class beanClass = null; 172 try { 173 beanClass = Class.forName(beanClassName); 174 } catch (ClassCastException ex) { 175 LOG.log(Level.SEVERE, "Could not load bean class " + beanClassName, ex); 176 return; 177 } catch (ClassNotFoundException 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 e) { 185 LOG.log(Level.SEVERE, "Could not create bean instance " + beanClassName, e); 186 return; 187 } 188 } 189 190 197 198 protected UrlResource getBeanDefinitionsResource() { 199 200 UrlResource urlRes = null; 201 String url = System.getProperty(CONFIG_FILE_PROPERTY_NAME); 202 if (null != url) { 203 try { 204 urlRes = new UrlResource(url); 205 } catch (MalformedURLException ex) { 206 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 beanClassName = SpringUtils.getBeanClassName(configuration.getModel().getNamespaceURI()); 218 Class beanClass = null; 219 try { 220 beanClass = Class.forName(beanClassName); 221 } catch (ClassCastException ex) { 222 LOG.log(Level.SEVERE, "Could not load bean class " + beanClassName, ex); 223 return; 224 } catch (ClassNotFoundException ex) { 225 LOG.log(Level.SEVERE, "Could not load bean class " + beanClassName, ex); 226 return; 227 } 228 229 String [] 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 <BeanName> beanNames = new ArrayList <BeanName>(); 239 for (String 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 |