001: /*
002: * Copyright 2007 The Kuali Foundation
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package org.kuali.core.web.servlet.dwr;
016:
017: import javax.servlet.ServletContext;
018: import javax.servlet.http.HttpServletRequest;
019:
020: import org.apache.log4j.Logger;
021: import org.kuali.rice.resourceloader.GlobalResourceLoader;
022: import org.springframework.beans.factory.BeanFactory;
023: import org.springframework.web.context.support.WebApplicationContextUtils;
024: import org.springframework.web.servlet.support.RequestContextUtils;
025:
026: import uk.ltd.getahead.dwr.Messages;
027: import uk.ltd.getahead.dwr.WebContextFactory;
028: import uk.ltd.getahead.dwr.create.SpringCreator;
029:
030: /**
031: * A {@link SpringCreator} that checks the {@link GlobalResourceLoader} for the
032: * bean name in question if the default {@link BeanFactory} (the applications)
033: * does not have the bean in question.
034: *
035: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
036: *
037: */
038: public class GlobalResourceDelegatingSpringCreator extends
039: SpringCreator {
040:
041: private static final Logger LOG = Logger
042: .getLogger(GlobalResourceDelegatingSpringCreator.class);
043:
044: private BeanFactory factory;
045:
046: public static BeanFactory APPLICATION_BEAN_FACTORY;
047:
048: @Override
049: /*
050: * This is largely ripped from the super class. Implementation details
051: * prevent calling the super class and then going to the
052: * GlobalResourceLoader ( the BeanFactory wll throw an exception if the bean
053: * isn't there). The defalt SpringCreator's static BeanFactory override
054: * could not be used - what a pity.
055: *
056: */
057: public Object getInstance() throws InstantiationException {
058: if (factory == null) {
059: factory = getBeanFactory();
060: }
061:
062: if (factory == null) {
063: factory = APPLICATION_BEAN_FACTORY;
064: }
065:
066: //we could just delegation to the GRL here no need for a resource factory
067: if (factory == null) {
068: LOG
069: .error("DWR can't find a spring config. See following info logs for solutions"); //$NON-NLS-1$
070: LOG
071: .info("- Option 1. In dwr.xml, <create creator='spring' ...> add <param name='location1' value='beans.xml'/> for each spring config file."); //$NON-NLS-1$
072: LOG
073: .info("- Option 2. Use a spring org.springframework.web.context.ContextLoaderListener."); //$NON-NLS-1$
074: LOG
075: .info("- Option 3. Call SpringCreator.setOverrideBeanFactory() from your web-app"); //$NON-NLS-1$
076: throw new InstantiationException(Messages
077: .getString("SpringCreator.MissingConfig")); //$NON-NLS-1$
078: }
079:
080: if (factory.containsBean(this .getBeanName())) {
081: return factory.getBean(this .getBeanName());
082: } else {
083: Object bean = GlobalResourceLoader.getService(this
084: .getBeanName());
085: if (bean == null) {
086: throw new InstantiationException("Unable to find bean "
087: + this .getBeanName()
088: + " in Rice Global Resource Loader");
089: }
090: return bean;
091: }
092: }
093:
094: /**
095: * @return A found BeanFactory configuration
096: */
097: private BeanFactory getBeanFactory() {
098: ServletContext srvCtx = WebContextFactory.get()
099: .getServletContext();
100: HttpServletRequest request = WebContextFactory.get()
101: .getHttpServletRequest();
102:
103: if (request != null) {
104: return RequestContextUtils.getWebApplicationContext(
105: request, srvCtx);
106: } else {
107: return WebApplicationContextUtils
108: .getWebApplicationContext(srvCtx);
109: }
110: }
111:
112: }
|