package org.apache.ode.axis2;

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.axis2.description.AxisOperation;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.WSDL11ToAxisServiceBuilder;
import org.apache.axis2.description.Parameter;
import org.apache.axis2.engine.AxisServer;
import org.apache.axis2.engine.MessageReceiver;
import org.apache.ode.axis2.hooks.ODEAxisService;
import org.apache.ode.axis2.util.Axis2UriResolver;
import org.apache.ode.axis2.util.Axis2WSDLLocator;
import org.apache.ode.tools.sendsoap.cline.HttpSoapSender;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

import javax.servlet.ServletException;
import javax.wsdl.WSDLException;
import javax.xml.namespace.QName;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collection;
import java.util.Iterator;

/**
 * @author Matthieu Riou <mriou@apache.org>
 */
public abstract class Axis2TestBase {

    public static final int DEFAULT_TEST_PORT = 8888;

    protected ODEAxis2Server server;

    public void startServer() throws Exception {
        String webappPath = getClass().getClassLoader().getResource("webapp").getFile();
        server = new ODEAxis2Server(webappPath);
        server.start();
    }

    public void stopServer() throws AxisFault {
        server.stop();
    }

  @BeforeMethod
    protected void setUp() throws Exception {
        startServer();
    }

  @AfterMethod
    protected void tearDown() throws Exception {
        stopServer();
    }

    protected class ODEAxis2Server extends AxisServer {
        ODEServer _ode = new ODEServer();
        String webappPath;

        protected ODEAxis2Server (String webappPath) throws Exception {
            super(false);
            this.webappPath = webappPath;
            String confLocation = webappPath + "/WEB-INF/conf/axis2.xml";
            String repoLocation = webappPath + "/WEB-INF";
            configContext = ConfigurationContextFactory
                    .createConfigurationContextFromFileSystem(repoLocation, confLocation);
            // do not use 8080 for tests
            configContext.getAxisConfiguration().getTransportIn("http").addParameter(new Parameter("port", ""+DEFAULT_TEST_PORT));
        }

        protected void start() throws AxisFault {
            super.start();
            _ode = new ODEServer();
            try {
                _ode.init(webappPath+"/WEB-INF", configContext.getAxisConfiguration());
            } catch (ServletException e) {
                e.printStackTrace();
            }
        }

        public void stop() throws AxisFault {
            _ode.shutDown();
            super.stop();
        }

        public Collection<QName> deployProcess(String bundleName) {
            return _ode.getProcessStore().deploy(new File(getBundleDir(bundleName)));
        }
        public void undeployProcess(String bundleName) {
            _ode.getProcessStore().undeploy(new File(getBundleDir(bundleName)));
        }
        public boolean isDeployed(String bundleName) {
            return _ode.getProcessStore().getPackages().contains(bundleName);
        }

        /**
         * Creates and deploys an Axis service based on a provided MessageReceiver. The receiver
         * will be invoked for all invocations of that service.
         */
        protected void deployService(String bundleName, String defFile, QName serviceName, String port,
                                     MessageReceiver receiver) throws WSDLException, IOException, URISyntaxException {
            URI wsdlUri = new File(getBundleDir(bundleName) + "/" + defFile).toURI();

            InputStream is = wsdlUri.toURL().openStream();
            WSDL11ToAxisServiceBuilder serviceBuilder = new ODEAxisService.WSDL11ToAxisPatchedBuilder(is, serviceName, port);
            serviceBuilder.setBaseUri(wsdlUri.toString());
            serviceBuilder.setCustomResolver(new Axis2UriResolver());
            serviceBuilder.setCustomWSLD4JResolver(new Axis2WSDLLocator(wsdlUri));
            serviceBuilder.setServerSide(true);

            AxisService axisService = serviceBuilder.populateService();
            axisService.setName(serviceName.getLocalPart());
            axisService.setWsdlFound(true);
            axisService.setCustomWsdl(true);
            axisService.setClassLoader(getConfigurationContext().getAxisConfiguration().getServiceClassLoader());

            Iterator operations = axisService.getOperations();
            while (operations.hasNext()) {
                AxisOperation operation = (AxisOperation) operations.next();
                if (operation.getMessageReceiver() == null) {
                    operation.setMessageReceiver(receiver);
                }
            }
            getConfigurationContext().getAxisConfiguration().addService(axisService);            
        }

        public String sendRequestFile(String endpoint, String bundleName, String filename) {
            try {
                return HttpSoapSender.doSend(new URL(endpoint),
                        new FileInputStream(getBundleDir(bundleName)+"/" + filename), null, 0, null, null, null);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        protected String getBundleDir(String bundleName) {
            return getClass().getClassLoader().getResource(bundleName).getFile();
        }

        /**
         * Convenient methods to generate a WSDL for an Axis2 service. Often nice, but also often
         * generates crappy WSDL that aren't even valid (especially when faults are involved) so
         * use with care.
         * @param serviceName
         * @param fileName
         * @throws AxisFault
         */
        protected void generateWSDL(String serviceName, String fileName) throws AxisFault {
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(fileName);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            configContext.getAxisConfiguration().getService(serviceName).printWSDL(fos);
        }

        public ODEServer getODEServer() {
            return _ode;
        }
    }

}

@@ -260,32 +260,15 @@
 ion.
-WSDL11ToAxisServiceBuild
+Paramet
 er;%0A
@@ -295,39 +295,56 @@
 is2.description.
-Paramet
+WSDL11ToAxisServiceBuild
 er;%0Aimport org.a
@@ -418,24 +418,109 @@
 geReceiver;%0A
+import org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A
 import org.a
@@ -1373,16 +1373,92 @@
  8888;%0A%0A
+    private static final Log log = LogFactory.getLog(Axis2TestBase.class);%0A%0A
     prot
@@ -2024,16 +2024,17 @@
 erver %7B%0A
+%0A
         
@@ -2309,16 +2309,16 @@
 2.xml%22;%0A
-
         
@@ -2368,16 +2368,240 @@
 B-INF%22;%0A
+            if(log.isInfoEnabled())%7B%0A                log.info(%22Webapp dir: %22+webappPath);%0A                log.info(%22Axis2 Conf file: %22+confLocation);%0A                log.info(%22Axis2 Repo dir: %22+repoLocation);%0A            %7D%0A%0A
         

add logging information