1 16 package org.directwebremoting.spring; 17 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import javax.servlet.ServletContext ; 24 import javax.servlet.http.HttpServletRequest ; 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 41 public class SpringCreator extends AbstractCreator implements Creator 42 { 43 46 public String getBeanName() 47 { 48 return beanName; 49 } 50 51 54 public void setBeanName(String beanName) 55 { 56 this.beanName = beanName; 57 } 58 59 63 public void setClass(String classname) 64 { 65 try 66 { 67 this.clazz = LocalUtil.classForName(classname); 68 } 69 catch (ClassNotFoundException ex) 70 { 71 throw new IllegalArgumentException (Messages.getString("Creator.ClassNotFound", classname)); 72 } 73 } 74 75 78 public void setProperties(Map params) throws IllegalArgumentException  79 { 80 List locValues = new ArrayList (); 81 82 for (Iterator it = params.entrySet().iterator(); it.hasNext();) 83 { 84 Map.Entry entry = (Map.Entry ) it.next(); 85 String key = (String ) entry.getKey(); 86 String value = (String ) 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 []) locValues.toArray(new String [locValues.size()]); 95 } 96 97 100 public Class getType() 101 { 102 if (clazz == null) 103 { 104 try 105 { 106 clazz = getInstance().getClass(); 107 } 108 catch (InstantiationException ex) 109 { 110 log.error("Failed to instansiate object to detect type.", ex); 111 return Object .class; 112 } 113 } 114 115 return clazz; 116 } 117 118 121 public Object getInstance() throws InstantiationException  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 (Messages.getString("SpringCreator.MissingConfig")); 142 } 143 144 return factory.getBean(beanName); 145 } 146 catch (RuntimeException ex) 147 { 148 throw ex; 149 } 150 catch (Exception ex) 151 { 152 log.error("Error", ex); 153 throw new InstantiationException (ex.toString()); 154 } 155 } 156 157 160 private BeanFactory getBeanFactory() 161 { 162 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 srvCtx = WebContextFactory.get().getServletContext(); 170 HttpServletRequest 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 189 public static void setXmlBeanFactory(BeanFactory factory) 190 { 191 SpringCreator.overrideFactory = factory; 192 } 193 194 198 public static void setOverrideBeanFactory(BeanFactory factory) 199 { 200 SpringCreator.overrideFactory = factory; 201 } 202 203 206 private String beanName = null; 207 208 211 private static final Logger log = Logger.getLogger(SpringCreator.class); 212 213 216 private BeanFactory factory = null; 217 218 222 private static BeanFactory overrideFactory = null; 223 224 227 private Class clazz = null; 228 229 232 private String [] configLocation = null; 233 } 234
| Popular Tags
|