| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kns.web.servlet.dwr; |
| 17 | |
|
| 18 | |
import javax.servlet.ServletContext; |
| 19 | |
import javax.servlet.http.HttpServletRequest; |
| 20 | |
|
| 21 | |
import org.apache.log4j.Logger; |
| 22 | |
import org.kuali.rice.core.resourceloader.GlobalResourceLoader; |
| 23 | |
import org.springframework.beans.factory.BeanFactory; |
| 24 | |
import org.springframework.web.context.support.WebApplicationContextUtils; |
| 25 | |
import org.springframework.web.servlet.support.RequestContextUtils; |
| 26 | |
|
| 27 | |
import uk.ltd.getahead.dwr.Messages; |
| 28 | |
import uk.ltd.getahead.dwr.WebContextFactory; |
| 29 | |
import uk.ltd.getahead.dwr.create.SpringCreator; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | 0 | public class GlobalResourceDelegatingSpringCreator extends SpringCreator { |
| 40 | |
|
| 41 | 0 | private static final Logger LOG = Logger.getLogger(GlobalResourceDelegatingSpringCreator.class); |
| 42 | |
|
| 43 | |
private BeanFactory factory; |
| 44 | |
|
| 45 | |
public static BeanFactory APPLICATION_BEAN_FACTORY; |
| 46 | |
|
| 47 | |
@Override |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public Object getInstance() throws InstantiationException { |
| 57 | 0 | if (factory == null) { |
| 58 | 0 | factory = getBeanFactory(); |
| 59 | |
} |
| 60 | |
|
| 61 | 0 | if (factory == null) { |
| 62 | 0 | factory = APPLICATION_BEAN_FACTORY; |
| 63 | |
} |
| 64 | |
|
| 65 | |
|
| 66 | 0 | if (factory == null) { |
| 67 | 0 | LOG.error("DWR can't find a spring config. See following info logs for solutions"); |
| 68 | 0 | LOG.info("- Option 1. In dwr.xml, <create creator='spring' ...> add <param name='location1' value='beans.xml'/> for each spring config file."); |
| 69 | 0 | LOG.info("- Option 2. Use a spring org.springframework.web.context.ContextLoaderListener."); |
| 70 | 0 | LOG.info("- Option 3. Call SpringCreator.setOverrideBeanFactory() from your web-app"); |
| 71 | 0 | throw new InstantiationException(Messages.getString("SpringCreator.MissingConfig")); |
| 72 | |
} |
| 73 | |
|
| 74 | 0 | if (factory.containsBean(this.getBeanName())) { |
| 75 | 0 | return factory.getBean(this.getBeanName()); |
| 76 | |
} else { |
| 77 | 0 | Object bean = GlobalResourceLoader.getService(this.getBeanName()); |
| 78 | 0 | if (bean == null) { |
| 79 | 0 | throw new InstantiationException("Unable to find bean " + this.getBeanName() + " in Rice Global Resource Loader"); |
| 80 | |
} |
| 81 | 0 | return bean; |
| 82 | |
} |
| 83 | |
} |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
private BeanFactory getBeanFactory() { |
| 89 | 0 | ServletContext srvCtx = WebContextFactory.get().getServletContext(); |
| 90 | 0 | HttpServletRequest request = WebContextFactory.get().getHttpServletRequest(); |
| 91 | |
|
| 92 | 0 | if (request != null) { |
| 93 | 0 | return RequestContextUtils.getWebApplicationContext(request, srvCtx); |
| 94 | |
} else { |
| 95 | 0 | return WebApplicationContextUtils.getWebApplicationContext(srvCtx); |
| 96 | |
} |
| 97 | |
} |
| 98 | |
|
| 99 | |
} |