001    /** 
002     * 
003     * Copyright 2005 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * You may obtain a copy of the License at 
008     * 
009     * http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS, 
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
014     * See the License for the specific language governing permissions and 
015     * limitations under the License. 
016     * 
017     **/
018    package org.codehaus.activesoap.handler.xmlbeans;
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import org.apache.wsif.WSIFException;
023    import org.apache.wsif.providers.WSIFDynamicTypeMap;
024    import org.codehaus.activesoap.RestService;
025    import org.codehaus.activesoap.SoapClient;
026    import org.codehaus.activesoap.SoapService;
027    import org.codehaus.activesoap.handler.EchoHandler;
028    import org.codehaus.activesoap.handler.stax.AnyElementMarshaler;
029    import org.codehaus.activesoap.wsif.ASProviderSupport;
030    
031    import javax.wsdl.Definition;
032    import javax.wsdl.Port;
033    import javax.wsdl.Service;
034    
035    /**
036     * A <a href="http://ws.apache.org/wsif/">WSIF</a> provider for <a href="http://activesoap.codehaus.org/">ActiveSOAP</a>
037     * using <a href="http://xml.apache.org/xmlbeans/">XMLBeans</a> as the mershalling layer
038     *
039     * @version $Revision: 1.2 $
040     */
041    public class XMLBeansWSIFProvider extends ASProviderSupport {
042        private static final transient Log log = LogFactory.getLog(XMLBeansWSIFProvider.class);
043    
044        public String[] getAddressNamespaceURIs() {
045            return new String[]{"http://activesoap.codehaus.org/wsif/xmlbeans/1.0/"};
046        }
047    
048        public String[] getBindingNamespaceURIs() {
049            return new String[]{"http://activesoap.codehaus.org/wsif/xmlbeans/1.0/"};
050        }
051    
052    
053        protected void applyClientPolicies(SoapClient client) {
054            XMLBeansHelper.useXMLBeans(client);
055        }
056    
057        protected RestService createService(Definition definition, Service service, Port port, WSIFDynamicTypeMap wsifDynamicTypeMap) throws WSIFException {
058            // lets make an echo service
059            XMLBeansRegistry registry = new XMLBeansRegistry();
060    
061            String className = getServiceClassName(definition, service, port, wsifDynamicTypeMap);
062            registerService(className, registry);
063    
064            //registry.setBodyHandler(new EchoHandler());
065            SoapService soapService = new SoapService(registry);
066            //soapService.setRepairingNamespace(true);
067            return soapService;
068        }
069    
070    
071        protected void registerService(String className, XMLBeansRegistry registry) throws WSIFException {
072            log.debug("RegisterService with className: " + className);
073            if (className != null && className.length() > 0) {
074                try {
075                    Class serviceType = loadClass(className);
076                    registry.registerService(serviceType);
077                }
078                catch (Exception e) {
079                    throw new WSIFException("Failed to find class: " + className + ". Reason: " + e, e);
080                }
081            }
082        }
083    
084        protected AnyElementMarshaler createMarshaler(Definition definition, Service service, Port port, WSIFDynamicTypeMap wsifDynamicTypeMap) {
085            return new XMLBeansAnyElementMarshaler();
086        }
087    }