KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > il > oil2 > OIL2ServerIL


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.mq.il.oil2;
8
9 import java.io.BufferedInputStream JavaDoc;
10 import java.io.BufferedOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.ObjectInputStream JavaDoc;
13 import java.io.ObjectOutputStream JavaDoc;
14 import java.net.InetAddress JavaDoc;
15 import java.net.Socket JavaDoc;
16
17 import javax.jms.Destination JavaDoc;
18 import javax.jms.JMSException JavaDoc;
19 import javax.jms.Queue JavaDoc;
20 import javax.jms.TemporaryQueue JavaDoc;
21 import javax.jms.TemporaryTopic JavaDoc;
22 import javax.jms.Topic JavaDoc;
23 import javax.net.SocketFactory;
24
25 import org.jboss.logging.Logger;
26 import org.jboss.mq.AcknowledgementRequest;
27 import org.jboss.mq.Connection;
28 import org.jboss.mq.ConnectionToken;
29 import org.jboss.mq.DurableSubscriptionID;
30 import org.jboss.mq.SpyDestination;
31 import org.jboss.mq.SpyMessage;
32 import org.jboss.mq.TransactionRequest;
33 import org.jboss.mq.il.ServerIL;
34
35 /**
36  * The JVM implementation of the ServerIL object
37  *
38  * @author <a HREF="mailto:hiram.chirino@jboss.org">Hiram Chirino</a>
39  * @version $Revision: $
40  * @created August 16, 2001
41  */

42 public final class OIL2ServerIL
43    implements java.io.Serializable JavaDoc, java.lang.Cloneable JavaDoc, org.jboss.mq.il.ServerIL, OIL2Constants
44 {
45    static final long serialVersionUID = 1841984837999477932L;
46    
47    private final static Logger log = Logger.getLogger(OIL2ServerIL.class);
48    /** The org.jboss.mq.il.oil2.localAddr system property allows a client to
49     *define the local interface to which its sockets should be bound
50     */

51    private final static String JavaDoc LOCAL_ADDR = "org.jboss.mq.il.oil2.localAddr";
52    /** The org.jboss.mq.il.oil2.localPort system property allows a client to
53     *define the local port to which its sockets should be bound
54     */

55    private final static String JavaDoc LOCAL_PORT = "org.jboss.mq.il.oil2.localPort";
56
57    /** The server host name/IP to connect to
58     */

59    private String JavaDoc addr;
60    /** The server port to connect to.
61     */

62    private int port;
63    /** The name of the class implementing the javax.net.SocketFactory to
64     * use for creating the client socket.
65     */

66    private String JavaDoc socketFactoryName;
67
68    /**
69     * If the TcpNoDelay option should be used on the socket.
70     */

71    private boolean enableTcpNoDelay = false;
72    /** The local interface name/IP to use for the client
73     */

74    private transient InetAddress JavaDoc localAddr;
75    /** The local port to use for the client
76     */

77    private transient int localPort;
78    /**
79     * Description of the Field
80     */

81    private transient ObjectInputStream JavaDoc in;
82
83    /**
84     * Description of the Field
85     */

86    private transient ObjectOutputStream JavaDoc out;
87
88    /**
89     * Description of the Field
90     */

91    private transient Socket JavaDoc socket;
92
93    OIL2SocketHandler socketHandler;
94
95    class RequestListner implements OIL2RequestListner
96    {
97       public void handleConnectionException(Exception JavaDoc e)
98       {
99       }
100
101       public void handleRequest(OIL2Request request)
102       {
103       }
104
105    }
106
107    /**
108     * Constructor for the OILServerIL object
109     *
110     * @param addr, the server host or ip
111     * @param port, the server port
112     * @param socketFactoryName, the name of the javax.net.SocketFactory to use
113     * @param enableTcpNoDelay,
114     */

115    public OIL2ServerIL(String JavaDoc addr, int port,
116       String JavaDoc socketFactoryName, boolean enableTcpNoDelay)
117    {
118       this.addr = addr;
119       this.port = port;
120       this.socketFactoryName = socketFactoryName;
121       this.enableTcpNoDelay = enableTcpNoDelay;
122    }
123
124    synchronized public void connect() throws IOException JavaDoc
125    {
126       if (socket == null)
127       {
128          boolean tracing = log.isTraceEnabled();
129          if( tracing )
130             log.trace("Connecting to : "+addr+":"+port);
131
132          /** Attempt to load the socket factory and if this fails, use the
133           * default socket factory impl.
134           */

135          SocketFactory socketFactory = null;
136          if( socketFactoryName != null )
137          {
138             try
139             {
140                ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
141                Class JavaDoc factoryClass = loader.loadClass(socketFactoryName);
142                socketFactory = (SocketFactory) factoryClass.newInstance();
143             }
144             catch(Exception JavaDoc e)
145             {
146                log.debug("Failed to load socket factory: "+socketFactoryName, e);
147             }
148          }
149          // Use the default socket factory
150
if( socketFactory == null )
151          {
152             socketFactory = SocketFactory.getDefault();
153          }
154
155          // Look for a local address and port as properties
156
String JavaDoc tmp = System.getProperty(LOCAL_ADDR);
157          if( tmp != null )
158             this.localAddr = InetAddress.getByName(tmp);
159          tmp = System.getProperty(LOCAL_PORT);
160          if( tmp != null )
161             this.localPort = Integer.parseInt(tmp);
162          if( tracing )
163          {
164             log.trace("Connecting with addr="+addr+", port="+port
165                + ", localAddr="+localAddr+", localPort="+localPort
166                + ", socketFactory="+socketFactory);
167          }
168
169          if( localAddr != null )
170             socket = socketFactory.createSocket(addr, port, localAddr, localPort);
171          else
172             socket = socketFactory.createSocket(addr, port);
173
174          if( tracing )
175             log.trace("Connection established.");
176          
177          socket.setTcpNoDelay(enableTcpNoDelay);
178          out = new ObjectOutputStream JavaDoc(new BufferedOutputStream JavaDoc(socket.getOutputStream()));
179          out.flush();
180          in = new ObjectInputStream JavaDoc(new BufferedInputStream JavaDoc(socket.getInputStream()));
181
182          if( tracing )
183             log.trace("Streams initialized.");
184
185          socketHandler = new OIL2SocketHandler(in, out, Connection.getThreadGroup());
186          socketHandler.setRequestListner(new RequestListner());
187          socketHandler.start();
188       }
189    }
190
191    /**
192     * Sets the ConnectionToken attribute of the OILServerIL object
193     *
194     * @param dest The new ConnectionToken value
195     * @exception Exception Description of Exception
196     */

197    public void setConnectionToken(ConnectionToken dest) throws Exception JavaDoc
198    {
199       connect();
200       OIL2Request request = new OIL2Request(OIL2Constants.SERVER_SET_SPY_DISTRIBUTED_CONNECTION, new Object JavaDoc[] { dest });
201       OIL2Response response = socketHandler.synchRequest(request);
202       response.evalThrowsJMSException();
203    }
204
205    /**
206     * Sets the Enabled attribute of the OILServerIL object
207     *
208     * @param dc The new Enabled value
209     * @param enabled The new Enabled value
210     * @exception JMSException Description of Exception
211     * @exception Exception Description of Exception
212     */

213    public void setEnabled(ConnectionToken dc, boolean enabled) throws JMSException JavaDoc, Exception JavaDoc
214    {
215       connect();
216       OIL2Request request = new OIL2Request(OIL2Constants.SERVER_SET_ENABLED, new Object JavaDoc[] { new Boolean JavaDoc(enabled)});
217       OIL2Response response = socketHandler.synchRequest(request);
218       response.evalThrowsJMSException();
219    }
220
221    /**
222     * Gets the ID attribute of the OILServerIL object
223     *
224     * @return The ID value
225     * @exception Exception Description of Exception
226     */

227    public String JavaDoc getID() throws Exception JavaDoc
228    {
229       connect();
230       OIL2Request request = new OIL2Request(OIL2Constants.SERVER_GET_ID, null);
231       OIL2Response response = socketHandler.synchRequest(request);
232       return (String JavaDoc) response.evalThrowsJMSException();
233    }
234
235    /**
236     * Gets the TemporaryQueue attribute of the OILServerIL object
237     *
238     * @param dc Description of Parameter
239     * @return The TemporaryQueue value
240     * @exception JMSException Description of Exception
241     * @exception Exception Description of Exception
242     */

243    public TemporaryQueue JavaDoc getTemporaryQueue(ConnectionToken dc) throws JMSException JavaDoc, Exception JavaDoc
244    {
245       connect();
246       OIL2Request request = new OIL2Request(OIL2Constants.SERVER_GET_TEMPORARY_QUEUE, null);
247       OIL2Response response = socketHandler.synchRequest(request);
248       return (TemporaryQueue JavaDoc) response.evalThrowsJMSException();
249    }
250
251    /**
252     * Gets the TemporaryTopic attribute of the OILServerIL object
253     *
254     * @param dc Description of Parameter
255     * @return The TemporaryTopic value
256     * @exception JMSException Description of Exception
257     * @exception Exception Description of Exception
258     */

259    public TemporaryTopic JavaDoc getTemporaryTopic(ConnectionToken dc) throws JMSException JavaDoc, Exception JavaDoc
260    {
261       connect();
262       OIL2Request request = new OIL2Request(OIL2Constants.SERVER_GET_TEMPORARY_TOPIC, null);
263       OIL2Response response = socketHandler.synchRequest(request);
264       return (TemporaryTopic JavaDoc) response.evalThrowsJMSException();
265    }
266
267    /**
268     * #Description of the Method
269     *
270     * @param dc Description of Parameter
271     * @param item Description of Parameter
272     * @exception JMSException Description of Exception
273     * @exception Exception Description of Exception
274     */

275    public void acknowledge(ConnectionToken dc, AcknowledgementRequest item) throws JMSException JavaDoc, Exception JavaDoc
276    {
277       connect();
278       OIL2Request request = new OIL2Request(OIL2Constants.SERVER_ACKNOWLEDGE, new Object JavaDoc[] { item });
279       OIL2Response response = socketHandler.synchRequest(request);
280       response.evalThrowsJMSException();
281
282    }
283
284    /**
285     * Adds a feature to the Message attribute of the OILServerIL object
286     *
287     * @param dc The feature to be added to the Message attribute
288     * @param val The feature to be added to the Message attribute
289     * @exception Exception Description of Exception
290     */

291    public void addMessage(ConnectionToken dc, SpyMessage val) throws Exception JavaDoc
292    {
293
294       connect();
295       OIL2Request request = new OIL2Request(OIL2Constants.SERVER_ADD_MESSAGE, new Object JavaDoc[] { val });
296       OIL2Response response = socketHandler.synchRequest(request);
297       response.evalThrowsJMSException();
298    }
299
300    /**
301     * #Description of the Method
302     *
303     * @param dc Description of Parameter
304     * @param dest Description of Parameter
305     * @param selector Description of Parameter
306     * @return Description of the Returned Value
307     * @exception JMSException Description of Exception
308     * @exception Exception Description of Exception
309     */

310    public SpyMessage[] browse(ConnectionToken dc, Destination JavaDoc dest, String JavaDoc selector)
311       throws JMSException JavaDoc, Exception JavaDoc
312    {
313       connect();
314       OIL2Request request = new OIL2Request(OIL2Constants.SERVER_BROWSE, new Object JavaDoc[] { dest, selector });
315       OIL2Response response = socketHandler.synchRequest(request);
316       return (SpyMessage[]) response.evalThrowsJMSException();
317    }
318
319    /**
320     * #Description of the Method
321     *
322     * @param ID Description of Parameter
323     * @exception JMSException Description of Exception
324     * @exception Exception Description of Exception
325     */

326    public void checkID(String JavaDoc ID) throws JMSException JavaDoc, Exception JavaDoc
327    {
328       connect();
329       OIL2Request request = new OIL2Request(OIL2Constants.SERVER_CHECK_ID, new Object JavaDoc[] { ID });
330       OIL2Response response = socketHandler.synchRequest(request);
331       response.evalThrowsJMSException();
332    }
333
334    /**
335     * #Description of the Method
336     *
337     * @param userName Description of Parameter
338     * @param password Description of Parameter
339     * @return Description of the Returned Value
340     * @exception JMSException Description of Exception
341     * @exception Exception Description of Exception
342     */

343    public String JavaDoc checkUser(String JavaDoc userName, String JavaDoc password) throws JMSException JavaDoc, Exception JavaDoc
344    {
345       connect();
346       OIL2Request request = new OIL2Request(OIL2Constants.SERVER_CHECK_USER, new Object JavaDoc[] { userName, password });
347       OIL2Response response = socketHandler.synchRequest(request);
348       return (String JavaDoc) response.evalThrowsJMSException();
349    }
350
351    /**
352     * #Description of the Method
353     *
354     * @param userName Description of Parameter
355     * @param password Description of Parameter
356     * @return Description of the Returned Value
357     * @exception JMSException Description of Exception
358     * @exception Exception Description of Exception
359     */

360    public String JavaDoc authenticate(String JavaDoc userName, String JavaDoc password) throws JMSException JavaDoc, Exception JavaDoc
361    {
362       connect();
363       OIL2Request request = new OIL2Request(OIL2Constants.SERVER_AUTHENTICATE, new Object JavaDoc[] { userName, password });
364       OIL2Response response = socketHandler.synchRequest(request);
365       return (String JavaDoc) response.evalThrowsJMSException();
366
367    }
368    /**
369     * #Description of the Method
370     *
371     * @return Description of the Returned Value
372     * @exception CloneNotSupportedException Description of Exception
373     */

374    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
375    {
376       return super.clone();
377    }
378
379    /**
380     * Need to clone because there are instance variables tha can get clobbered.
381     * All Multiple connections can NOT share the same JVMServerIL object
382     *
383     * @return Description of the Returned Value
384     * @exception Exception Description of Exception
385     */

386    public ServerIL cloneServerIL() throws Exception JavaDoc
387    {
388       return (ServerIL) clone();
389    }
390
391    /**
392     * #Description of the Method
393     *
394     * @param dc Description of Parameter
395     * @exception JMSException Description of Exception
396     * @exception Exception Description of Exception
397     */

398    public void connectionClosing(ConnectionToken dc) throws JMSException JavaDoc, Exception JavaDoc
399    {
400       try
401       {
402          connect();
403          OIL2Request request = new OIL2Request(OIL2Constants.SERVER_CONNECTION_CLOSING, null);
404          OIL2Response response = socketHandler.synchRequest(request);
405          response.evalThrowsJMSException();
406       }
407       finally
408       {
409          close();
410       }
411    }
412
413    /**
414     * #Description of the Method
415     *
416     * @param dc Description of Parameter
417     * @param dest Description of Parameter
418     * @return Description of the Returned Value
419     * @exception JMSException Description of Exception
420     * @exception Exception Description of Exception
421     */

422    public Queue JavaDoc createQueue(ConnectionToken dc, String JavaDoc dest) throws JMSException JavaDoc, Exception JavaDoc
423    {
424       connect();
425       OIL2Request request = new OIL2Request(OIL2Constants.SERVER_CREATE_QUEUE, new Object JavaDoc[] { dest });
426       OIL2Response response = socketHandler.synchRequest(request);
427       return (Queue JavaDoc) response.evalThrowsJMSException();
428
429    }
430
431    /**
432     * #Description of the Method
433     *
434     * @param dc Description of Parameter
435     * @param dest Description of Parameter
436     * @return Description of the Returned Value
437     * @exception JMSException Description of Exception
438     * @exception Exception Description of Exception
439     */

440    public Topic JavaDoc createTopic(ConnectionToken dc, String JavaDoc dest) throws JMSException JavaDoc, Exception JavaDoc
441    {
442       connect();
443       OIL2Request request = new OIL2Request(OIL2Constants.SERVER_CREATE_TOPIC, new Object JavaDoc[] { dest });
444       OIL2Response response = socketHandler.synchRequest(request);
445       return (Topic JavaDoc) response.evalThrowsJMSException();
446
447    }
448
449    /**
450     * #Description of the Method
451     *
452     * @param dc Description of Parameter
453     * @param dest Description of Parameter
454     * @exception JMSException Description of Exception
455     * @exception Exception Description of Exception
456     */

457    public void deleteTemporaryDestination(ConnectionToken dc, SpyDestination dest)
458       throws JMSException JavaDoc, Exception JavaDoc
459    {
460       connect();
461       OIL2Request request = new OIL2Request(OIL2Constants.SERVER_DELETE_TEMPORARY_DESTINATION, new Object JavaDoc[] { dest });
462       OIL2Response response = socketHandler.synchRequest(request);
463       response.evalThrowsJMSException();
464
465    }
466
467    /**
468     * #Description of the Method
469     *
470     * @param id Description of Parameter
471     * @exception JMSException Description of Exception
472     * @exception Exception Description of Exception
473     */

474    public void destroySubscription(ConnectionToken dc, DurableSubscriptionID id)
475       throws JMSException JavaDoc, Exception JavaDoc
476    {
477       connect();
478       OIL2Request request = new OIL2Request(OIL2Constants.SERVER_DESTROY_SUBSCRIPTION, new Object JavaDoc[] { id });
479       OIL2Response response = socketHandler.synchRequest(request);
480       response.evalThrowsJMSException();
481
482    }
483
484    /**
485     * #Description of the Method
486     *
487     * @param dc Description of Parameter
488     * @param clientTime Description of Parameter
489     * @exception Exception Description of Exception
490     */

491    public void ping(ConnectionToken dc, long clientTime) throws Exception JavaDoc
492    {
493       connect();
494       OIL2Request request = new OIL2Request(OIL2Constants.SERVER_PING, new Object JavaDoc[] { new Long JavaDoc(clientTime)});
495       OIL2Response response = socketHandler.synchRequest(request);
496       response.evalThrowsJMSException();
497    }
498
499    /**
500     * #Description of the Method
501     *
502     * @param dc Description of Parameter
503     * @param subscriberId Description of Parameter
504     * @param wait Description of Parameter
505     * @return Description of the Returned Value
506     * @exception Exception Description of Exception
507     */

508    public SpyMessage receive(ConnectionToken dc, int subscriberId, long wait) throws Exception JavaDoc, Exception JavaDoc
509    {
510       connect();
511       OIL2Request request =
512          new OIL2Request(OIL2Constants.SERVER_RECEIVE, new Object JavaDoc[] { new Integer JavaDoc(subscriberId), new Long JavaDoc(wait)});
513       OIL2Response response = socketHandler.synchRequest(request);
514       return (SpyMessage) response.evalThrowsJMSException();
515
516    }
517
518    /**
519     * #Description of the Method
520     *
521     * @param dc Description of Parameter
522     * @param s Description of Parameter
523     * @exception JMSException Description of Exception
524     * @exception Exception Description of Exception
525     */

526    public void subscribe(ConnectionToken dc, org.jboss.mq.Subscription s) throws JMSException JavaDoc, Exception JavaDoc
527    {
528       connect();
529       OIL2Request request = new OIL2Request(OIL2Constants.SERVER_SUBSCRIBE, new Object JavaDoc[] { s });
530       OIL2Response response = socketHandler.synchRequest(request);
531       response.evalThrowsJMSException();
532
533    }
534
535    /**
536     * #Description of the Method
537     *
538     * @param dc Description of Parameter
539     * @param t Description of Parameter
540     * @exception JMSException Description of Exception
541     * @exception Exception Description of Exception
542     */

543    public void transact(org.jboss.mq.ConnectionToken dc, TransactionRequest t)
544       throws JMSException JavaDoc, Exception JavaDoc
545    {
546       connect();
547       OIL2Request request = new OIL2Request(OIL2Constants.SERVER_TRANSACT, new Object JavaDoc[] { t });
548       OIL2Response response = socketHandler.synchRequest(request);
549       response.evalThrowsJMSException();
550    }
551
552    /**
553     * #Description of the Method
554     *
555     * @param dc Description of Parameter
556     * @param subscriptionId Description of Parameter
557     * @exception JMSException Description of Exception
558     * @exception Exception Description of Exception
559     */

560    public void unsubscribe(ConnectionToken dc, int subscriptionId) throws JMSException JavaDoc, Exception JavaDoc
561    {
562       connect();
563       OIL2Request request =
564          new OIL2Request(OIL2Constants.SERVER_UNSUBSCRIBE, new Object JavaDoc[] { new Integer JavaDoc(subscriptionId)});
565       OIL2Response response = socketHandler.synchRequest(request);
566       response.evalThrowsJMSException();
567
568    }
569
570    /**
571     * Used to close the current connection with the server
572     *
573     * @exception Exception Description of Exception
574     */

575    synchronized public void close()
576    {
577       try
578       {
579          if (socket != null)
580          {
581             socketHandler.stop();
582             in.close();
583             out.close();
584             socket.close();
585             socket = null;
586          }
587       }
588       catch (IOException JavaDoc e)
589       {
590          log.debug("Exception occured while closing opened resources: ", e);
591       }
592    }
593 }
594
Popular Tags