KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > kjoram > ksoap > SoapConnection


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - Dyade
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): Nicolas Tachker (ScalAgent)
22  * Contributor(s):
23  */

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 JavaDoc;
31 import java.util.Vector JavaDoc;
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 /**
39  * A <code>SoapConnection</code> links a Joram client and a Joram platform
40  * with HTTP connections.
41  * <p>
42  * Requests and replies travel through the connections in SOAP (XML) format.
43  */

44 public class SoapConnection implements com.scalagent.kjoram.ConnectionItf {
45   /** String URL of the SOAP service this object communicates with. */
46   private String JavaDoc serviceUrl = null;
47   /** Identifier of the connection. */
48   private int cnxId;
49   /** */
50   private HttpConnection httpConnect = null;
51
52   private String JavaDoc name = null;
53
54   /**
55    * Creates a <code>SoapConnection</code> instance.
56    *
57    * @param params Factory parameters.
58    * @param name Name of user.
59    * @param password Password of user.
60    *
61    * @exception JMSSecurityException If the user identification is incorrrect.
62    * @exception IllegalStateException If the server is not reachable.
63    */

64   public SoapConnection(FactoryParameters factParams, String JavaDoc name,
65                         String JavaDoc password) throws JMSException {
66     connect(factParams, name, password);
67     this.name = name;
68   }
69
70   public String JavaDoc getUserName() {
71     return name;
72   }
73
74   /**
75    * Creates a driver for the connection.
76    *
77    * @param cnx The calling <code>Connection</code> instance.
78    */

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   /**
86    * Sending a JMS request through the SOAP protocol.
87    *
88    * @exception IllegalStateException If the SOAP service fails.
89    */

90   public synchronized void send(AbstractJmsRequest request)
91                            throws IllegalStateException JavaDoc {
92     try {
93       if (httpConnect == null)
94         httpConnect = new HttpConnection(serviceUrl);
95       httpConnect.call(request,name,cnxId);
96     } catch (Exception JavaDoc exc) {
97       httpConnect = null;
98       throw new IllegalStateException JavaDoc("The SOAP call failed: "
99                                       + exc.getMessage());
100     }
101   }
102
103   /** Closes the <code>SoapConnection</code>. */
104   public void close() {
105     try {
106       send(new CnxCloseRequest());
107     }
108     catch (Exception JavaDoc exc) {}
109   }
110
111   /**
112    * Actually tries to set a first SOAP connection with the server.
113    *
114    * @param params Factory parameters.
115    * @param name The user's name.
116    * @param password The user's password.
117    *
118    * @exception JMSSecurityException If the user identification is incorrrect.
119    * @exception IllegalStateException If the SOAP service fails.
120    */

121   private void connect(FactoryParameters factParams, String JavaDoc name,
122                        String JavaDoc password)
123                throws JMSException {
124     // Setting the timer values:
125
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 JavaDoc resp;
132     String JavaDoc 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 JavaDoc(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 JavaDoc exc) {
157         error = exc.getMessage();
158         tryAgain = true;
159       }
160       // Trying again to connect:
161
if (tryAgain) {
162         currentTime = System.currentTimeMillis();
163         // Keep on trying as long as timer is ok:
164
if (currentTime < endTime) {
165
166           if (currentTime + nextSleep > endTime)
167             nextSleep = endTime - currentTime;
168
169           // Sleeping for a while:
170
try {
171             Thread.sleep(nextSleep);
172           }
173           catch (InterruptedException JavaDoc intExc) {}
174
175           // Trying again!
176
nextSleep = nextSleep * 2;
177           continue;
178         }
179         // If timer is over, throwing an IllegalStateException:
180
else {
181           long attemptsT = (System.currentTimeMillis() - startTime) / 1000;
182           throw new IllegalStateException JavaDoc("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