Author: sagara Date: Fri Jan 13 11:24:50 2012 New Revision: 1231009 URL: http://svn.apache.org/viewvc?rev=1231009&view=rev Log: Applied patch for AXIS2-5231 with few changes.
Added: axis/axis2/java/core/trunk/modules/spring/src/org/apache/axis2/extensions/spring/util/ axis/axis2/java/core/trunk/modules/spring/src/org/apache/axis2/extensions/spring/util/ApplicationContextUtil.java (with props) Modified: axis/axis2/java/core/trunk/modules/spring/src/org/apache/axis2/extensions/spring/receivers/SpringAppContextAwareObjectSupplier.java Modified: axis/axis2/java/core/trunk/modules/spring/src/org/apache/axis2/extensions/spring/receivers/SpringAppContextAwareObjectSupplier.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/spring/src/org/apache/axis2/extensions/spring/receivers/SpringAppContextAwareObjectSupplier.java?rev=1231009&r1=1231008&r2=1231009&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/spring/src/org/apache/axis2/extensions/spring/receivers/SpringAppContextAwareObjectSupplier.java (original) +++ axis/axis2/java/core/trunk/modules/spring/src/org/apache/axis2/extensions/spring/receivers/SpringAppContextAwareObjectSupplier.java Fri Jan 13 11:24:50 2012 @@ -23,41 +23,45 @@ import org.apache.axis2.AxisFault; import org.apache.axis2.ServiceObjectSupplier; import org.apache.axis2.description.AxisService; import org.apache.axis2.description.Parameter; +import org.apache.axis2.extensions.spring.util.ApplicationContextUtil; import org.apache.axis2.i18n.Messages; -import org.springframework.context.ApplicationContext; +import org.springframework.context.support.GenericApplicationContext; public class SpringAppContextAwareObjectSupplier implements ServiceObjectSupplier { public static final String SERVICE_SPRING_BEANNAME = "SpringBeanName"; /** - * Method getServiceObject that is Spring aware via Spring interface ApplicationContextAware. + * Method getServiceObject used to get the spring beans from the spring application context + * for the given spring service * - * @param axisService - * @return Returns Object. + * @param axisService - spring service + * @return Object * @throws AxisFault */ - public Object getServiceObject(AxisService axisService) throws AxisFault { try { + // Get the Spring Context based on service, the context is set as parameter to the + // spring service in SpringServiceDeployer when service deployed. + GenericApplicationContext aCtx = ApplicationContextUtil. + getSpringApplicationContext(axisService); + // Name of spring aware bean to be injected, taken from services.xml // via 'SERVICE_SPRING_BEANNAME ' . The Bean and its properties are pre-configured // as normally done in a spring type of way and subsequently loaded by Spring. // Axis2 just assumes that the bean is configured and is in the classloader. Parameter implBeanParam = axisService.getParameter(SERVICE_SPRING_BEANNAME); - String beanName = ((String)implBeanParam.getValue()).trim(); - if (beanName != null) { - // ApplicationContextHolder implements Spring interface ApplicationContextAware - ApplicationContext aCtx = ApplicationContextHolder.getContext(); + if (implBeanParam != null) { + String beanName = ((String) implBeanParam.getValue()).trim(); if (aCtx == null) { - throw new Exception("Axis2 Can't find Spring's ApplicationContext"); + throw new AxisFault("Axis2 Can't find Spring's ApplicationContext"); } else if (aCtx.getBean(beanName) == null) { - throw new Exception("Axis2 Can't find Spring Bean: " + beanName); + throw new AxisFault("Axis2 Can't find Spring Bean: " + beanName); } return aCtx.getBean(beanName); } else { throw new AxisFault( - Messages.getMessage("paramIsNotSpecified", "SERVICE_SPRING_BEANNAME")); + Messages.getMessage("paramIsNotSpecified", SERVICE_SPRING_BEANNAME)); } } catch (Exception e) { throw AxisFault.makeFault(e); Added: axis/axis2/java/core/trunk/modules/spring/src/org/apache/axis2/extensions/spring/util/ApplicationContextUtil.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/spring/src/org/apache/axis2/extensions/spring/util/ApplicationContextUtil.java?rev=1231009&view=auto ============================================================================== --- axis/axis2/java/core/trunk/modules/spring/src/org/apache/axis2/extensions/spring/util/ApplicationContextUtil.java (added) +++ axis/axis2/java/core/trunk/modules/spring/src/org/apache/axis2/extensions/spring/util/ApplicationContextUtil.java Fri Jan 13 11:24:50 2012 @@ -0,0 +1,129 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.axis2.extensions.spring.util; + +import org.apache.axis2.AxisFault; +import org.apache.axis2.deployment.DeploymentConstants; +import org.apache.axis2.description.AxisService; +import org.apache.axis2.description.AxisServiceGroup; +import org.apache.axis2.description.Parameter; +import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; +import org.springframework.context.support.GenericApplicationContext; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.InputStreamResource; + +import java.io.File; +import java.io.InputStream; + +/** + * Util class of the spring module. It contains a method which is used to get + * the spring application context for given spring web services + */ +public class ApplicationContextUtil { + public static final String SPRING_APPLICATION_CONTEXT = "SpringApplicationContext"; + public static final String SPRING_APPLICATION_CONTEXT_LOCATION = "SpringContextLocation"; + + /** + * Method to get the spring application context for a spring service. This + * method will first check the META-INF(or meta-inf) directory for the + * '<service-name>-application-context.xml file. If the file is not found + * then it will check whether file path is set as a parameter in + * service.xml. If the context file is set as a parameter for a service + * group, then the context will be add to the group or else it will be add + * to the service. + * + * @param axisService + * @return GenericApplicationContext + * @throws AxisFault + */ + + public static GenericApplicationContext getSpringApplicationContext(AxisService axisService) + throws AxisFault { + + GenericApplicationContext appContext; + Parameter appContextParameter = axisService.getParameter(SPRING_APPLICATION_CONTEXT); + Parameter contextLocationParam = axisService + .getParameter(SPRING_APPLICATION_CONTEXT_LOCATION); + + // return the application context + if (appContextParameter != null) { + appContext = (GenericApplicationContext) appContextParameter.getValue(); + // if the context is not found initialize a new one + } else { + appContext = new GenericApplicationContext(); + ClassLoader serviceCL = axisService.getClassLoader(); + appContext.setClassLoader(serviceCL); + ClassLoader currentCL = Thread.currentThread().getContextClassLoader(); + + try { + Thread.currentThread().setContextClassLoader(serviceCL); + XmlBeanDefinitionReader xbdr = new XmlBeanDefinitionReader(appContext); + + // load the bean context file from the parameter + if (contextLocationParam != null) { + xbdr.loadBeanDefinitions(new ClassPathResource((String) contextLocationParam + .getValue())); + appContext.refresh(); + AxisServiceGroup axisServiceGroup = axisService.getAxisServiceGroup(); + Parameter springGroupCtxLocation = axisServiceGroup + .getParameter(SPRING_APPLICATION_CONTEXT_LOCATION); + // add the context to the service group or add it to the + // service + if (springGroupCtxLocation != null) { + axisServiceGroup.addParameter(new Parameter(SPRING_APPLICATION_CONTEXT, + appContext)); + } else { + axisService.addParameter(new Parameter(SPRING_APPLICATION_CONTEXT, + appContext)); + } + return appContext; + } + + InputStream ctxFileInputStream = serviceCL + .getResourceAsStream(DeploymentConstants.META_INF + File.separator + + axisService.getName() + "-application-context.xml"); + // try for meta-inf + if (ctxFileInputStream == null) { + ctxFileInputStream = serviceCL.getResourceAsStream(DeploymentConstants.META_INF + .toLowerCase() + + File.separator + + axisService.getName() + + "-application-context.xml"); + } + // load the context file from meta-inf + if (ctxFileInputStream != null) { + xbdr.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE); + xbdr.loadBeanDefinitions(new InputStreamResource(ctxFileInputStream)); + appContext.refresh(); + axisService.addParameter(new Parameter(SPRING_APPLICATION_CONTEXT, appContext)); + return appContext; + } else { + throw new AxisFault("Spring context file cannot be located for AxisService"); + } + } catch (Exception e) { + throw AxisFault.makeFault(e); + } finally { + // restore the class loader + Thread.currentThread().setContextClassLoader(currentCL); + } + } + return appContext; + } +} Propchange: axis/axis2/java/core/trunk/modules/spring/src/org/apache/axis2/extensions/spring/util/ApplicationContextUtil.java ------------------------------------------------------------------------------ svn:eol-style = native