KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > clientapi > ListenerManager


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  * Runtime state of the engine
17  */

18 package org.apache.axis2.clientapi;
19
20 import org.apache.axis2.addressing.EndpointReference;
21 import org.apache.axis2.context.ConfigurationContext;
22 import org.apache.axis2.description.TransportInDescription;
23 import org.apache.axis2.engine.AxisFault;
24 import org.apache.axis2.transport.TransportListener;
25
26 import javax.xml.namespace.QName JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.net.ServerSocket JavaDoc;
29 import java.util.HashMap JavaDoc;
30
31 public class ListenerManager {
32
33     public static int port = 6059;
34     public static HashMap JavaDoc listeners = new HashMap JavaDoc();
35     public static ConfigurationContext configurationContext;
36
37     public static final void makeSureStarted(
38         String JavaDoc transport,
39         ConfigurationContext configurationContext)
40         throws AxisFault {
41         if (ListenerManager.configurationContext != null && configurationContext != ListenerManager.configurationContext) {
42             throw new AxisFault("Only One ConfigurationContext Instance we support at the Client Side");
43         }
44
45         ListenerManager.configurationContext = configurationContext;
46         TransportListnerState tsState = (TransportListnerState) listeners.get(transport);
47         if (tsState == null) {
48             TransportInDescription tranportIn =
49                 configurationContext.getAxisConfiguration().getTransportIn(new QName JavaDoc(transport));
50             TransportListener listener = tranportIn.getReciever();
51 // listener.init(configurationContext, tranportIn);
52
listener.start();
53             tsState = new TransportListnerState(listener);
54             listeners.put(transport,tsState);
55         }
56         tsState.waitingCalls++;
57     }
58
59     public static final void stop(String JavaDoc transport) throws AxisFault {
60         TransportListnerState tsState = (TransportListnerState) listeners.get(transport);
61         if (tsState != null) {
62             tsState.waitingCalls--;
63             if (tsState.waitingCalls == 0) {
64                 tsState.listener.stop();
65             }
66         }
67     }
68
69     public static EndpointReference replyToEPR(String JavaDoc serviceName, String JavaDoc transport)
70         throws AxisFault {
71         TransportListnerState tsState = (TransportListnerState) listeners.get(transport);
72         if (tsState != null) {
73             return tsState.listener.replyToEPR(serviceName);
74         } else {
75             throw new AxisFault(
76                 "Calling method before starting the with makeSureStarted(..) Listener transport = "
77                     + transport);
78
79         }
80
81     }
82
83     public int getPort() {
84         port++;
85         return port;
86     }
87
88     public static class TransportListnerState {
89         public TransportListnerState(TransportListener listener) {
90             this.listener = listener;
91         }
92         public int waitingCalls = 0;
93         public TransportListener listener;
94     }
95
96     public static ServerSocket JavaDoc openSocket(int port) throws AxisFault {
97         for (int i = 0; i < 5; i++) {
98             try {
99                 return new ServerSocket JavaDoc(port + i);
100             } catch (IOException JavaDoc e) {
101             }
102         }
103         throw new AxisFault("failed to open the scoket");
104     }
105
106 }
107
Popular Tags