001    /*****************************************************************************
002     * Copyright (C) NanoContainer Organization. All rights reserved.            *
003     * ------------------------------------------------------------------------- *
004     * The software in this package is published under the terms of the BSD      *
005     * style license a copy of which has been included with this distribution in *
006     * the LICENSE.txt file.                                                     *
007     *                                                                           *
008     *****************************************************************************/
009    package org.nanocontainer.nanowar.axis;
010    
011    import org.apache.axis.MessageContext;
012    import org.apache.axis.providers.java.RPCProvider;
013    import org.apache.axis.transport.http.HTTPConstants;
014    import org.apache.axis.utils.cache.ClassCache;
015    import org.nanocontainer.nanowar.KeyConstants;
016    import org.nanocontainer.nanowar.RequestScopeObjectReference;
017    import org.picocontainer.MutablePicoContainer;
018    import org.picocontainer.defaults.DefaultPicoContainer;
019    import org.picocontainer.defaults.ObjectReference;
020    
021    import javax.servlet.http.HttpServletRequest;
022    
023    /**
024     * Axis provider for RPC-style services that uses the servlet container
025     * hierarchy to instantiate service classes and resolve their dependencies.
026     *
027     * @author <a href="mailto:evan@bottch.com">Evan Bottcher</a>
028     */
029    public class NanoRPCProvider extends RPCProvider implements KeyConstants {
030    
031        protected Object makeNewServiceObject(
032            MessageContext msgContext,
033            String clsName)
034            throws Exception {
035    
036            ClassLoader cl = msgContext.getClassLoader();
037            ClassCache cache = msgContext.getAxisEngine().getClassCache();
038            Class svcClass = cache.lookup(clsName, cl).getJavaClass();
039    
040            return instantiateService(svcClass, msgContext);
041    
042        }
043    
044        private Object instantiateService(Class svcClass, MessageContext msgContext) {
045    
046            HttpServletRequest request = (HttpServletRequest)msgContext.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
047            ObjectReference ref = new RequestScopeObjectReference(request, REQUEST_CONTAINER);
048            MutablePicoContainer requestContainer = (MutablePicoContainer) ref.get();
049    
050            MutablePicoContainer container = new DefaultPicoContainer(requestContainer);
051            container.registerComponentImplementation(svcClass);
052            return container.getComponentInstance(svcClass);
053        }
054    
055    }