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.MsgProvider;
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 message-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 NanoMsgProvider extends MsgProvider implements KeyConstants {
030    
031        protected Object makeNewServiceObject(MessageContext msgContext,
032                                              String clsName)
033                throws Exception {
034    
035            ClassLoader cl = msgContext.getClassLoader();
036            ClassCache cache = msgContext.getAxisEngine().getClassCache();
037            Class svcClass = cache.lookup(clsName, cl).getJavaClass();
038    
039            return instantiateService(svcClass, msgContext);
040    
041        }
042    
043        private Object instantiateService(Class svcClass, MessageContext msgContext) {
044    
045            HttpServletRequest request = (HttpServletRequest) msgContext.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
046            ObjectReference ref = new RequestScopeObjectReference(request, REQUEST_CONTAINER);
047            MutablePicoContainer requestContainer = (MutablePicoContainer) ref.get();
048    
049            MutablePicoContainer container = new DefaultPicoContainer(requestContainer);
050            container.registerComponentImplementation(svcClass);
051            return container.getComponentInstance(svcClass);
052        }
053    
054    }