1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.kandula.utility;
18
19 import java.io.IOException;
20 import java.net.InetAddress;
21 import java.net.UnknownHostException;
22 import java.util.HashMap;
23
24 import org.apache.axis2.AxisFault;
25 import org.apache.axis2.context.ConfigurationContext;
26 import org.apache.axis2.context.ConfigurationContextFactory;
27 import org.apache.axis2.description.AxisOperation;
28 import org.apache.axis2.description.AxisService;
29 import org.apache.axis2.transport.http.SimpleHTTPServer;
30
31 /***
32 * @author <a href="mailto:thilina@opensource.lk"> Thilina Gunarathne </a>
33 */
34 public class KandulaListener {
35
36 private static KandulaListener instance = null;
37
38 private ConfigurationContext responseConfigurationContext;
39
40 protected static AxisOperation[] operations;
41
42 private SimpleHTTPServer receiver = null;
43
44 private boolean serverStarted = false;
45
46 public int serverPort;
47
48 private KandulaListener() throws IOException {
49 KandulaConfiguration configuration = KandulaConfiguration.getInstance();
50 responseConfigurationContext = ConfigurationContextFactory
51 .createConfigurationContextFromFileSystem(configuration
52 .getKandulaListenerRepository(), configuration
53 .getKandulaListenerAxis2Xml());
54 try {
55 serverPort = Integer.parseInt(KandulaConfiguration.getInstance()
56 .getKadulaListenerPort());
57 } catch (Exception e) {
58 serverPort = 5059;
59 }
60 while (receiver == null) {
61
62 receiver = new SimpleHTTPServer(responseConfigurationContext,
63 serverPort);
64
65 }
66 }
67
68 public static KandulaListener getInstance() throws IOException {
69 if (instance == null) {
70 instance = new KandulaListener();
71 }
72 return instance;
73 }
74
75 public void start() throws IOException {
76 if (!serverStarted) {
77
78 receiver.start();
79 serverStarted = true;
80 System.out.print("Server started on port " + serverPort + ".....");
81 }
82
83 }
84
85 public void stop() {
86 receiver.stop();
87 serverStarted = false;
88 }
89
90 /***
91 * @param service
92 * @throws AxisFault
93 * To add services with only one operation, which is the
94 * frequent case in reponses
95 */
96 public void addService(AxisService service) throws AxisFault {
97
98 service.setClassLoader(Thread.currentThread().getContextClassLoader());
99 HashMap allServices = responseConfigurationContext
100 .getAxisConfiguration().getServices();
101
102 if (allServices.get(service.getName()) == null) {
103
104 responseConfigurationContext.getAxisConfiguration().addService(
105 service);
106
107
108
109 }
110
111 }
112
113 public String getHost() throws UnknownHostException {
114 return "http://"
115 + InetAddress.getLocalHost().getHostAddress()
116 + ":"
117 + KandulaConfiguration.getInstance()
118 .getKadulaListenerPortForEPR() + "/axis2/services/";
119 }
120 }