1 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 ; 27 import java.io.IOException ; 28 import java.net.ServerSocket ; 29 import java.util.HashMap ; 30 31 public class ListenerManager { 32 33 public static int port = 6059; 34 public static HashMap listeners = new HashMap (); 35 public static ConfigurationContext configurationContext; 36 37 public static final void makeSureStarted( 38 String 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 (transport)); 50 TransportListener listener = tranportIn.getReciever(); 51 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 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 serviceName, String 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 openSocket(int port) throws AxisFault { 97 for (int i = 0; i < 5; i++) { 98 try { 99 return new ServerSocket (port + i); 100 } catch (IOException e) { 101 } 102 } 103 throw new AxisFault("failed to open the scoket"); 104 } 105 106 } 107 | Popular Tags |