KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > sandesha > client > ClientListener


1 /*
2 * Copyright 1999-2004 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
18 package org.apache.sandesha.client;
19
20
21 import org.apache.axis.SimpleChain;
22 import org.apache.axis.configuration.SimpleProvider;
23 import org.apache.axis.description.JavaServiceDesc;
24 import org.apache.axis.handlers.soap.SOAPService;
25 import org.apache.axis.transport.http.SimpleAxisServer;
26 import org.apache.sandesha.Constants;
27 import org.apache.sandesha.util.PropertyLoader;
28 import org.apache.sandesha.ws.rm.providers.RMProvider;
29
30 import java.io.IOException JavaDoc;
31 import java.net.ServerSocket JavaDoc;
32 import java.util.ArrayList JavaDoc;
33
34
35 /**
36  * This is the client side listener for Apache Sandesha.
37  * <p/>
38  * This will start with the RMService deployed to accept asynchronous responses or
39  * <p/>
40  * RM related messages such as Acknowledgements etc..
41  *
42  * @author Jaliya Ekanayake
43  * @author Patrick Collins
44  */

45
46 public class ClientListener {
47
48
49     public static final int NULL_PORT = -1;
50     private SimpleAxisServer sas;
51     private int listenerPort = NULL_PORT;
52     private boolean started;
53
54     public ClientListener(int aPort) {
55         listenerPort = aPort;
56     }
57
58
59     public synchronized void start() throws IOException JavaDoc {
60
61         if (!isStarted()) {
62             initSimpleAxisServer();
63             configureClientService();
64             configureServerSocket();
65             startSimpleAxisServer();
66         }
67     }
68
69
70     public void stop() {
71         getSas().stop();
72     }
73
74
75     protected void initSimpleAxisServer() {
76         setSas(new SimpleAxisServer());
77         getSas().setMyConfig(new SimpleProvider());
78     }
79
80
81     protected void configureClientService() {
82         SimpleChain reqHandlers = getListenerRequestChain();
83         SimpleChain resHandlers = getListenerResponseChain();
84         RMProvider rmp = new RMProvider();
85         rmp.setClient(true);
86         SOAPService rmService = new SOAPService(reqHandlers, rmp, resHandlers);
87         JavaServiceDesc desc = new JavaServiceDesc();
88         rmService.setOption(Constants.ClientProperties.CLASS_NAME, Constants.ClientProperties.RMSERVICE_CLASS);
89         rmService.setOption(Constants.ClientProperties.ALLOWED_METHODS, Constants.ASTERISK);
90         desc.setName(Constants.ClientProperties.RMSERVICE);
91         rmService.setServiceDescription(desc);
92         ((SimpleProvider) getSas().getMyConfig()).deployService(Constants.ClientProperties.RMSERVICE, rmService);
93     }
94
95     protected void configureServerSocket() throws IOException JavaDoc {
96         ServerSocket JavaDoc socket = new ServerSocket JavaDoc(getListenerPort());
97         getSas().setServerSocket(socket);
98     }
99
100     protected void startSimpleAxisServer() {
101         Thread JavaDoc serverThread = new Thread JavaDoc(getSas());
102         serverThread.start();
103     }
104
105     protected SimpleChain getListenerRequestChain() {
106         ArrayList JavaDoc arr = PropertyLoader.getListenerRequestHandlerNames();
107         return ClientHandlerUtil.getHandlerChain(arr);
108     }
109
110     protected SimpleChain getListenerResponseChain() {
111         ArrayList JavaDoc arr = PropertyLoader.getListenerResponseHandlerNames();
112         return ClientHandlerUtil.getHandlerChain(arr);
113     }
114
115     protected SimpleAxisServer getSas() {
116         return sas;
117     }
118
119     protected void setSas(SimpleAxisServer aSas) {
120         sas = aSas;
121     }
122
123     protected int getListenerPort() {
124         if (listenerPort == NULL_PORT) {
125             listenerPort = PropertyLoader.getClientSideListenerPort();
126         }
127
128         return listenerPort;
129     }
130
131
132     protected boolean isStarted() {
133         return started;
134     }
135 }
136
137
Popular Tags