KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > jms > tcp > TcpConnection


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2003 - 2006 ScalAgent Distributed Technologies
4  * Copyright (C) 2004 France Telecom R&D
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): Frederic Maistre (INRIA)
22  * Contributor(s): ScalAgent Distributed Technologies
23  */

24 package org.objectweb.joram.client.jms.tcp;
25
26 import java.util.Timer JavaDoc;
27
28 import javax.jms.IllegalStateException JavaDoc;
29 import javax.jms.JMSException JavaDoc;
30 import javax.jms.JMSSecurityException JavaDoc;
31
32 import org.objectweb.joram.client.jms.FactoryParameters;
33 import org.objectweb.joram.client.jms.connection.RequestChannel;
34 import org.objectweb.joram.shared.client.AbstractJmsReply;
35 import org.objectweb.joram.shared.client.AbstractJmsRequest;
36
37 import org.objectweb.joram.shared.JoramTracing;
38 import org.objectweb.util.monolog.api.BasicLevel;
39
40 /**
41  * A <code>TcpConnection</code> links a Joram client and a Joram platform
42  * with a TCP socket.
43  * <p>
44  * Requests and replies travel through the socket after serialization.
45  */

46 public class TcpConnection implements RequestChannel {
47   
48   private ReliableTcpClient tcpClient = null;
49
50   /**
51    * Creates a <code>TcpConnection</code> instance.
52    *
53    * @param params Factory parameters.
54    * @param name Name of user.
55    * @param password Password of user.
56    *
57    * @exception JMSSecurityException If the user identification is incorrrect.
58    * @exception IllegalStateException If the server is not reachable.
59    */

60   public TcpConnection(FactoryParameters params,
61                        String JavaDoc name,
62                        String JavaDoc password)
63     throws JMSException JavaDoc {
64     this(params,
65          name,
66          password,
67          "org.objectweb.joram.client.jms.tcp.ReliableTcpClient");
68   }
69
70   /**
71    * Creates a <code>TcpConnection</code> instance.
72    *
73    * @param params Factory parameters.
74    * @param name Name of user.
75    * @param password Password of user.
76    * @param reliableClass reliable class name.
77    *
78    * @exception JMSSecurityException If the user identification is incorrrect.
79    * @exception IllegalStateException If the server is not reachable.
80    */

81   public TcpConnection(FactoryParameters params,
82                        String JavaDoc name,
83                        String JavaDoc password,
84                        String JavaDoc reliableClass)
85     throws JMSException JavaDoc {
86
87     if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
88       JoramTracing.dbgClient.log(
89         BasicLevel.DEBUG,
90         "TcpConnection.<init>(" + params + ',' +
91         name + ',' + password + ',' + reliableClass +')');
92
93     if (reliableClass == null ||
94         reliableClass.equals("") ||
95         reliableClass.length() < 1) {
96       reliableClass = "org.objectweb.joram.client.jms.tcp.ReliableTcpClient";
97     }
98     try {
99       tcpClient =
100         (ReliableTcpClient) Class.forName(reliableClass).newInstance();
101     } catch (ClassNotFoundException JavaDoc exc) {
102       JMSException JavaDoc jmsExc =
103         new JMSException JavaDoc("TcpConnection: ClassNotFoundException : " +
104                          reliableClass);
105       jmsExc.setLinkedException(exc);
106       throw jmsExc;
107     } catch (InstantiationException JavaDoc exc) {
108       JMSException JavaDoc jmsExc =
109         new JMSException JavaDoc("TcpConnection: InstantiationException : " +
110                          reliableClass);
111       jmsExc.setLinkedException(exc);
112       throw jmsExc;
113     } catch (IllegalAccessException JavaDoc exc) {
114       JMSException JavaDoc jmsExc =
115         new JMSException JavaDoc("TcpConnection: IllegalAccessException : " +
116                          reliableClass);
117       jmsExc.setLinkedException(exc);
118       throw jmsExc;
119     }
120     tcpClient.init(params,
121                    name,
122                    password,
123                    params.cnxPendingTimer > 0);
124     tcpClient.addServerAddress(
125       params.getHost(),
126       params.getPort());
127   }
128   
129   public void setTimer(Timer JavaDoc timer) {
130     tcpClient.setTimer(timer);
131   }
132   
133   public void connect() throws Exception JavaDoc {
134     tcpClient.connect();
135   }
136   
137   /**
138    * Sending a JMS request through the TCP connection.
139    *
140    * @exception IllegalStateException If the connection is broken.
141    */

142   public synchronized void send(AbstractJmsRequest request)
143     throws Exception JavaDoc {
144     tcpClient.send(request);
145   }
146
147   public AbstractJmsReply receive()
148     throws Exception JavaDoc {
149     return (AbstractJmsReply)tcpClient.receive();
150   }
151
152   /** Closes the TCP connection. */
153   public void close() {
154     tcpClient.close();
155   }
156
157   public String JavaDoc toString() {
158     return '(' + super.toString() +
159       ",tcpClient=" + tcpClient + ')';
160   }
161 }
162
Popular Tags