1 24 package com.scalagent.kjoram.ksoap; 25 26 import com.scalagent.kjoram.Driver; 27 import com.scalagent.kjoram.FactoryParameters; 28 import com.scalagent.kjoram.jms.*; 29 30 import java.io.IOException ; 31 import java.util.Vector ; 32 33 import com.scalagent.kjoram.excepts.IllegalStateException; 34 import com.scalagent.kjoram.excepts.JMSException; 35 import com.scalagent.kjoram.excepts.JMSSecurityException; 36 import com.scalagent.ksoap.*; 37 38 44 public class SoapConnection implements com.scalagent.kjoram.ConnectionItf { 45 46 private String serviceUrl = null; 47 48 private int cnxId; 49 50 private HttpConnection httpConnect = null; 51 52 private String name = null; 53 54 64 public SoapConnection(FactoryParameters factParams, String name, 65 String password) throws JMSException { 66 connect(factParams, name, password); 67 this.name = name; 68 } 69 70 public String getUserName() { 71 return name; 72 } 73 74 79 public Driver createDriver(com.scalagent.kjoram.Connection cnx) { 80 Driver driver = new SoapDriver(cnx,serviceUrl,cnxId); 81 driver.setDaemon(true); 82 return driver; 83 } 84 85 90 public synchronized void send(AbstractJmsRequest request) 91 throws IllegalStateException { 92 try { 93 if (httpConnect == null) 94 httpConnect = new HttpConnection(serviceUrl); 95 httpConnect.call(request,name,cnxId); 96 } catch (Exception exc) { 97 httpConnect = null; 98 throw new IllegalStateException ("The SOAP call failed: " 99 + exc.getMessage()); 100 } 101 } 102 103 104 public void close() { 105 try { 106 send(new CnxCloseRequest()); 107 } 108 catch (Exception exc) {} 109 } 110 111 121 private void connect(FactoryParameters factParams, String name, 122 String password) 123 throws JMSException { 124 long startTime = System.currentTimeMillis(); 126 long endTime = startTime + factParams.connectingTimer * 1000; 127 long currentTime; 128 long nextSleep = 2000; 129 boolean tryAgain; 130 int attemptsC = 0; 131 Object resp; 132 String error; 133 134 serviceUrl = "http://" + factParams.getHost() + ":" + 135 factParams.getPort() + "/soap/servlet/rpcrouter"; 136 HttpTransport httpTransport = new HttpTransport(serviceUrl,"ProxyService"); 137 138 while (true) { 139 attemptsC++; 140 141 try { 142 SoapObject sO = ConversionSoapHelper.getSoapObject( 143 new SetCnx(name,password,new Integer (factParams.soapCnxPendingTimer))); 144 resp = httpTransport.call(sO); 145 cnxId = ConversionSoapHelper.getSetCnx((SoapObject)resp); 146 147 if (cnxId == -1) { 148 throw new JMSSecurityException("Can't open the connection with" 149 + " the server on host " 150 + factParams.getHost() 151 + " and port " 152 + factParams.getPort() 153 + ": invalid user identification."); 154 } 155 break; 156 } catch (Exception exc) { 157 error = exc.getMessage(); 158 tryAgain = true; 159 } 160 if (tryAgain) { 162 currentTime = System.currentTimeMillis(); 163 if (currentTime < endTime) { 165 166 if (currentTime + nextSleep > endTime) 167 nextSleep = endTime - currentTime; 168 169 try { 171 Thread.sleep(nextSleep); 172 } 173 catch (InterruptedException intExc) {} 174 175 nextSleep = nextSleep * 2; 177 continue; 178 } 179 else { 181 long attemptsT = (System.currentTimeMillis() - startTime) / 1000; 182 throw new IllegalStateException ("Could not open the connection" 183 + " with server on host " 184 + factParams.getHost() 185 + " and port " 186 + factParams.getPort() 187 + " after " + attemptsC 188 + " attempts during " 189 + attemptsT + " secs: " 190 + error); 191 } 192 } 193 } 194 } 195 } 196 | Popular Tags |