KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.axis2.context.ConfigurationContext;
20 import org.apache.axis2.context.ConfigurationContextFactory;
21 import org.apache.axis2.description.ServiceDescription;
22 import org.apache.axis2.engine.AxisFault;
23 import org.apache.axis2.transport.tcp.TCPServer;
24
25 import javax.xml.namespace.QName JavaDoc;
26 import java.io.File JavaDoc;
27
28 public class UtilsTCPServer {
29     private static int count = 0;
30     private static TCPServer reciver;
31
32     private static ConfigurationContext configurationContext;
33     public static final int TESTING_PORT = 5555;
34     public static final String JavaDoc FAILURE_MESSAGE = "Intentional Faliure";
35
36     public static synchronized void deployService(ServiceDescription service)
37         throws AxisFault {
38         configurationContext.getAxisConfiguration().addService(service);
39
40     }
41
42     public static synchronized void unDeployService(QName JavaDoc service)
43         throws AxisFault {
44         configurationContext.getAxisConfiguration().removeService(service);
45     }
46
47     public static synchronized void start() throws Exception JavaDoc {
48         if (count == 0) {
49
50             //start tcp server
51

52             ConfigurationContextFactory erfac =
53                 new ConfigurationContextFactory();
54             File JavaDoc file = new File JavaDoc(org.apache.axis2.Constants.TESTING_REPOSITORY);
55             System.out.println(file.getAbsoluteFile());
56             if (!file.exists()) {
57                 throw new Exception JavaDoc("repository directory does not exists");
58             }
59
60             configurationContext =
61                 erfac.buildConfigurationContext(file.getAbsolutePath());
62             try {
63                 Thread.sleep(2000);
64             } catch (InterruptedException JavaDoc e1) {
65                 throw new AxisFault("Thread interuptted", e1);
66             }
67             configurationContext.getAxisConfiguration().engageModule(
68                 new QName JavaDoc("addressing"));
69             reciver =
70                 new TCPServer(UtilServer.TESTING_PORT, configurationContext);
71             reciver.start();
72
73         }
74         count++;
75     }
76
77     public static synchronized void stop() {
78         try {
79             if (count == 1) {
80                 reciver.stop();
81                 count = 0;
82                 System.out.print("Server stopped .....");
83             } else {
84                 count--;
85             }
86         } catch (AxisFault e) {
87             // TODO Auto-generated catch block
88
e.printStackTrace();
89         }
90     }
91
92     public static ConfigurationContext getConfigurationContext() {
93         return configurationContext;
94     }
95 }
96
Popular Tags