KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > integration > UtilServer


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.context.ConfigurationContext;
21 import org.apache.axis2.context.ConfigurationContextFactory;
22 import org.apache.axis2.context.ServiceContext;
23 import org.apache.axis2.deployment.DeploymentEngine;
24 import org.apache.axis2.description.ModuleDescription;
25 import org.apache.axis2.description.ServiceDescription;
26 import org.apache.axis2.engine.AxisFault;
27 import org.apache.axis2.transport.http.SimpleHTTPServer;
28 import org.apache.axis2.util.Utils;
29
30 import javax.xml.namespace.QName JavaDoc;
31 import java.io.File JavaDoc;
32 import java.net.ServerSocket JavaDoc;
33
34 public class UtilServer {
35     private static int count = 0;
36     private static SimpleHTTPServer reciver;
37     public static final int TESTING_PORT = 5555;
38     public static final String JavaDoc FAILURE_MESSAGE = "Intentional Faliure";
39
40     public static synchronized void deployService(ServiceDescription service) throws AxisFault {
41         reciver.getSystemContext().getAxisConfiguration().addService(service);
42         Utils.resolvePhases(reciver.getSystemContext().getAxisConfiguration(), service);
43     }
44
45     public static synchronized void unDeployService(QName JavaDoc service) throws AxisFault {
46         reciver.getSystemContext().getAxisConfiguration().removeService(service);
47     }
48     
49     public static synchronized void start() throws Exception JavaDoc {
50         start(org.apache.axis2.Constants.TESTING_REPOSITORY);
51     }
52
53     public static synchronized void start(String JavaDoc repositry) throws Exception JavaDoc {
54         if (count == 0) {
55             ConfigurationContextFactory erfac = new ConfigurationContextFactory();
56             File JavaDoc file = new File JavaDoc(repositry);
57             if (!file.exists()) {
58                 throw new Exception JavaDoc("repository directory "+ file.getAbsolutePath()+ " does not exists");
59             }
60             ConfigurationContext er = erfac.buildConfigurationContext(file.getAbsolutePath());
61             try {
62                 Thread.sleep(2000);
63             } catch (InterruptedException JavaDoc e1) {
64                 throw new AxisFault("Thread interuptted", e1);
65             }
66
67             ServerSocket JavaDoc serverSoc = null;
68             serverSoc = new ServerSocket JavaDoc(Constants.TESTING_PORT);
69             reciver = new SimpleHTTPServer(er, serverSoc);
70             Thread JavaDoc thread = new Thread JavaDoc(reciver);
71             thread.setDaemon(true);
72
73             try {
74                 thread.start();
75                 System.out.print("Server started on port " + Constants.TESTING_PORT + ".....");
76             } finally {
77
78             }
79         }
80         count++;
81     }
82
83     public static synchronized void stop() {
84         if (count == 1) {
85             reciver.stop();
86             count = 0;
87             System.out.print("Server stopped .....");
88         } else {
89             count--;
90         }
91     }
92
93     public static ConfigurationContext getConfigurationContext() {
94         return reciver.getSystemContext();
95     }
96
97     public static ServiceContext createAdressedEnabledClientSide(ServiceDescription service)
98         throws AxisFault {
99         DeploymentEngine deploymentEngine = new DeploymentEngine();
100         File JavaDoc file = new File JavaDoc(org.apache.axis2.Constants.TESTING_REPOSITORY + "/modules/addressing.mar");
101         TestCase.assertTrue(file.exists());
102         ModuleDescription moduleDesc = deploymentEngine.buildModule(file);
103
104         ConfigurationContextFactory efac = new ConfigurationContextFactory();
105         ConfigurationContext sysContext = efac.buildClientConfigurationContext(null);
106         
107         sysContext.getAxisConfiguration().addMdoule(moduleDesc);
108         //sysContext.getAxisConfiguration().engageModule(moduleDesc.getName());
109

110         sysContext.getAxisConfiguration().addService(service);
111         //Utils.resolvePhases(sysContext.getEngineConfig(), service);
112
ServiceContext serviceContext = sysContext.createServiceContext(service.getName());
113         return serviceContext;
114
115     }
116
117 }
118
Popular Tags