KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > jms > ha > tcp > HATcpConnection


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2004 - Bull SA
4  * Copyright (C) 2004 - ScalAgent Distributed Technologies
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): David Feliot (ScalAgent DT)
22  * Contributor(s): Nicolas Tachker (ScalAgent DT)
23  */

24 package org.objectweb.joram.client.jms.ha.tcp;
25
26 import java.io.*;
27 import java.util.*;
28
29 import javax.jms.JMSException JavaDoc;
30
31 import org.objectweb.joram.client.jms.*;
32 import org.objectweb.joram.client.jms.tcp.*;
33 import org.objectweb.joram.shared.client.*;
34 import org.objectweb.joram.client.jms.connection.RequestChannel;
35
36 import org.objectweb.util.monolog.api.BasicLevel;
37
38 public class HATcpConnection
39     implements RequestChannel {
40
41   private ReliableTcpClient tcpClient;
42
43   /**
44    * Creates a <code>HATcpConnection</code> instance.
45    *
46    * @param params Factory parameters.
47    * @param name Name of user.
48    * @param password Password of user.
49    *
50    * @exception JMSSecurityException If the user identification is incorrrect.
51    * @exception IllegalStateException If the server is not reachable.
52    */

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

76   public HATcpConnection(String JavaDoc url,
77                          FactoryParameters params,
78                          String JavaDoc name,
79                          String JavaDoc password,
80                          String JavaDoc reliableClass)
81     throws JMSException JavaDoc {
82     try {
83       tcpClient =
84         (ReliableTcpClient) Class.forName(reliableClass).newInstance();
85     } catch (ClassNotFoundException JavaDoc exc) {
86       JMSException JavaDoc jmsExc =
87         new JMSException JavaDoc("HATcpConnection: ClassNotFoundException : " +
88                          reliableClass);
89       jmsExc.setLinkedException(exc);
90       throw jmsExc;
91     } catch (InstantiationException JavaDoc exc) {
92       JMSException JavaDoc jmsExc =
93         new JMSException JavaDoc("HATcpConnection: InstantiationException : " +
94                          reliableClass);
95       jmsExc.setLinkedException(exc);
96       throw jmsExc;
97     } catch (IllegalAccessException JavaDoc exc) {
98       JMSException JavaDoc jmsExc =
99         new JMSException JavaDoc("HATcpConnection: IllegalAccessException : " +
100                          reliableClass);
101       jmsExc.setLinkedException(exc);
102       throw jmsExc;
103     }
104     tcpClient.init(params,
105                    name,
106                    password,
107                    true);
108     StringTokenizer tokenizer = new StringTokenizer(url, "/:,");
109     if (! tokenizer.hasMoreElements())
110       throw new javax.jms.JMSException JavaDoc("URL not valid:" + url);
111     String JavaDoc protocol = tokenizer.nextToken();
112     if (protocol.equals("hajoram")) {
113       while (tokenizer.hasMoreElements()) {
114         tcpClient.addServerAddress(
115           tokenizer.nextToken(),
116           Integer.parseInt(tokenizer.nextToken()));
117       }
118     } else {
119       throw new javax.jms.JMSException JavaDoc("Unknown protocol:" + protocol);
120     }
121   }
122   
123   public void setTimer(Timer timer) {
124     tcpClient.setTimer(timer);
125   }
126   
127   public void connect() throws Exception JavaDoc {
128     tcpClient.connect();
129   }
130
131   /**
132    * Sending a JMS request through the TCP connection.
133    */

134   public void send(AbstractJmsRequest request)
135     throws Exception JavaDoc {
136     tcpClient.send(request);
137   }
138
139   public AbstractJmsReply receive()
140     throws Exception JavaDoc {
141     return (AbstractJmsReply)tcpClient.receive();
142   }
143
144   /** Closes the TCP connection. */
145   public void close() {
146     tcpClient.close();
147   }
148
149 }
150
Popular Tags