Home » rampart-dist-1.4-src » org.apache » axis2 » integration » [javadoc | source]

    1   /*
    2    * Copyright 2004,2005 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
   10    * Unless required by applicable law or agreed to in writing, software
   11    * distributed under the License is distributed on an "AS IS" BASIS,
   12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13    * See the License for the specific language governing permissions and
   14    * limitations under the License.
   15    */
   16   
   17   package org.apache.axis2.integration;
   18   
   19   import junit.framework.TestCase;
   20   import org.apache.axis2.AxisFault;
   21   import org.apache.axis2.Constants;
   22   import org.apache.axis2.context.ConfigurationContext;
   23   import org.apache.axis2.context.ConfigurationContextFactory;
   24   import org.apache.axis2.context.ServiceContext;
   25   import org.apache.axis2.context.ServiceGroupContext;
   26   import org.apache.axis2.deployment.DeploymentEngine;
   27   import org.apache.axis2.description.AxisModule;
   28   import org.apache.axis2.description.AxisService;
   29   import org.apache.axis2.description.AxisServiceGroup;
   30   import org.apache.axis2.description.TransportInDescription;
   31   import org.apache.axis2.engine.ListenerManager;
   32   import org.apache.axis2.transport.http.SimpleHTTPServer;
   33   
   34   import javax.xml.namespace.QName;
   35   import java.io.File;
   36   import java.io.FilenameFilter;
   37   
   38   public class UtilServer {
   39       private static int count = 0;
   40   
   41       private static SimpleHTTPServer receiver;
   42   
   43       public static final int TESTING_PORT = 5555;
   44   
   45       public static final String FAILURE_MESSAGE = "Intentional Failure";
   46   
   47   
   48       public static synchronized void deployService(AxisService service)
   49               throws AxisFault {
   50           receiver.getConfigurationContext().getAxisConfiguration().addService(
   51                   service);
   52       }
   53   
   54       public static synchronized void unDeployService(QName service)
   55               throws AxisFault {
   56           receiver.getConfigurationContext().getAxisConfiguration()
   57                   .removeService(service.getLocalPart());
   58       }
   59   
   60       public static synchronized void unDeployClientService() throws AxisFault {
   61           if (receiver.getConfigurationContext().getAxisConfiguration() != null) {
   62               receiver.getConfigurationContext().getAxisConfiguration()
   63                       .removeService("AnonymousService");
   64           }
   65       }
   66   
   67       public static synchronized void start() throws Exception {
   68           start(org.apache.axis2.Constants.TESTING_REPOSITORY);
   69       }
   70   
   71       public static synchronized void start(String repository) throws Exception {
   72           if (count == 0) {
   73               ConfigurationContext er = getNewConfigurationContext(repository);
   74   
   75               receiver = new SimpleHTTPServer(er, TESTING_PORT);
   76   
   77               try {
   78                   receiver.start();
   79                   ListenerManager listenerManager = er.getListenerManager();
   80                   TransportInDescription trsIn = new TransportInDescription(
   81                           Constants.TRANSPORT_HTTP);
   82                   trsIn.setReceiver(receiver);
   83                   if (listenerManager == null) {
   84                       listenerManager = new ListenerManager();
   85                       listenerManager.init(er);
   86                   }
   87                   listenerManager.addListener(trsIn, true);
   88                   System.out.print("Server started on port "
   89                           + TESTING_PORT + ".....");
   90               } catch (Exception e) {
   91                   e.printStackTrace();
   92               }
   93           }
   94   
   95           try {
   96               Thread.sleep(2000);
   97           } catch (InterruptedException e1) {
   98               throw new AxisFault("Thread interuptted", e1);
   99           }
  100   
  101   
  102           count++;
  103       }
  104   
  105       public static synchronized void start(String repository, String axis2xml) throws Exception {
  106           if (count == 0) {
  107               ConfigurationContext er = getNewConfigurationContext(repository, axis2xml);
  108   
  109               receiver = new SimpleHTTPServer(er, TESTING_PORT);
  110   
  111               try {
  112                   receiver.start();
  113                   System.out.print("Server started on port "
  114                           + TESTING_PORT + ".....");
  115               } catch (Exception e) {
  116                   throw new AxisFault(e.getMessage(), e);
  117               }
  118   
  119               try {
  120                   Thread.sleep(2000);
  121               } catch (InterruptedException e1) {
  122                   throw new AxisFault("Thread interuptted", e1);
  123               }
  124   
  125           }
  126           count++;
  127       }
  128   
  129       public static ConfigurationContext getNewConfigurationContext(
  130               String repository) throws Exception {
  131           File file = new File(repository);
  132           if (!file.exists()) {
  133               throw new Exception("repository directory "
  134                       + file.getAbsolutePath() + " does not exists");
  135           }
  136           return ConfigurationContextFactory.createConfigurationContextFromFileSystem(file.getAbsolutePath(),
  137                   file.getAbsolutePath() + "/conf/axis2.xml");
  138       }
  139   
  140       public static ConfigurationContext getNewConfigurationContext(
  141               String repository, String axis2xml) throws Exception {
  142           File file = new File(repository);
  143           if (!file.exists()) {
  144               throw new Exception("repository directory "
  145                       + file.getAbsolutePath() + " does not exists");
  146           }
  147           return ConfigurationContextFactory.createConfigurationContextFromFileSystem(file.getAbsolutePath(),
  148                   axis2xml);
  149       }
  150   
  151       public static synchronized void stop() throws AxisFault {
  152           if (count == 1) {
  153               receiver.stop();
  154               while (receiver.isRunning()) {
  155                   try {
  156                       Thread.sleep(1000);
  157                   } catch (InterruptedException e1) {
  158                       //nothing to do here
  159                   }
  160               }
  161               count = 0;
  162   // tp.doStop();
  163               System.out.print("Server stopped .....");
  164           } else {
  165               count--;
  166           }
  167           receiver.getConfigurationContext().terminate();
  168       }
  169   
  170       public static ConfigurationContext getConfigurationContext() {
  171           return receiver.getConfigurationContext();
  172       }
  173   
  174       public static ServiceContext createAdressedEnabledClientSide(
  175               AxisService service) throws AxisFault {
  176           File file = getAddressingMARFile();
  177           TestCase.assertTrue(file.exists());
  178           ConfigurationContext configContext = ConfigurationContextFactory
  179                   .createConfigurationContextFromFileSystem(
  180                           "target/test-resources/integrationRepo", null);
  181           AxisModule axisModule = DeploymentEngine.buildModule(file,
  182                   configContext.getAxisConfiguration());
  183           configContext.getAxisConfiguration().addModule(axisModule);
  184   
  185           configContext.getAxisConfiguration().addService(service);
  186   
  187           return new ServiceGroupContext(configContext, (AxisServiceGroup) service.getParent())
  188                   .getServiceContext(service);
  189       }
  190   
  191       static class AddressingFilter implements FilenameFilter {
  192           public boolean accept(File dir, String name) {
  193               return name.startsWith("addressing") && name.endsWith(".mar");
  194           }
  195       }
  196   
  197       private static File getAddressingMARFile() {
  198           File dir = new File(org.apache.axis2.Constants.TESTING_REPOSITORY + "/modules");
  199           File[] files = dir.listFiles(new AddressingFilter());
  200           TestCase.assertTrue(files.length == 1);
  201           File file = files[0];
  202           TestCase.assertTrue(file.exists());
  203           return file;
  204       }
  205   
  206       public static ConfigurationContext createClientConfigurationContext() throws AxisFault {
  207           File file = getAddressingMARFile();
  208           TestCase.assertTrue(file.exists());
  209   
  210           ConfigurationContext configContext = ConfigurationContextFactory .createConfigurationContextFromFileSystem(
  211                   "target/test-resources/integrationRepo",
  212                   "target/test-resources/integrationRepo/conf/axis2.xml");
  213           AxisModule axisModule = DeploymentEngine.buildModule(file,
  214                   configContext.getAxisConfiguration());
  215           configContext.getAxisConfiguration().addModule(axisModule);
  216           return configContext;
  217       }
  218   
  219       public static ConfigurationContext createClientConfigurationContext(String repo) throws AxisFault {
  220           return ConfigurationContextFactory .createConfigurationContextFromFileSystem(
  221                   repo,
  222                   repo + "/conf/axis2.xml");
  223       }
  224   
  225       public static ServiceContext createAdressedEnabledClientSide(
  226               AxisService service, String clientHome) throws AxisFault {
  227           File file = getAddressingMARFile();
  228           TestCase.assertTrue(file.exists());
  229   
  230           ConfigurationContext configContext = ConfigurationContextFactory
  231                   .createConfigurationContextFromFileSystem(clientHome, null);
  232           AxisModule axisModule = DeploymentEngine.buildModule(file,
  233                   configContext.getAxisConfiguration());
  234   
  235           configContext.getAxisConfiguration().addModule(axisModule);
  236   // sysContext.getAxisConfiguration().engageModule(moduleDesc.getName());
  237   
  238           configContext.getAxisConfiguration().addService(service);
  239   
  240           return new ServiceGroupContext(configContext, (AxisServiceGroup) service.getParent())
  241                   .getServiceContext(service);
  242       }
  243   
  244   }

Home » rampart-dist-1.4-src » org.apache » axis2 » integration » [javadoc | source]