KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > il > oil > OILServerIL


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.oil;
8
9 import java.io.BufferedInputStream JavaDoc;
10 import java.io.BufferedOutputStream JavaDoc;
11
12 import java.io.ObjectInputStream JavaDoc;
13 import java.io.ObjectOutputStream JavaDoc;
14 import java.net.InetAddress JavaDoc;
15
16 import java.net.Socket JavaDoc;
17 import javax.jms.Destination JavaDoc;
18
19 import javax.jms.JMSException JavaDoc;
20 import javax.jms.Queue JavaDoc;
21 import javax.jms.TemporaryQueue JavaDoc;
22 import javax.jms.TemporaryTopic JavaDoc;
23 import javax.jms.Topic JavaDoc;
24 import javax.net.SocketFactory;
25
26 import org.jboss.logging.Logger;
27 import org.jboss.mq.AcknowledgementRequest;
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 Hiram Chirino (Cojonudo14@hotmail.com)
39  * @author Norbert Lataille (Norbert.Lataille@m4x.org)
40  * @version $Revision: 1.12.6.1 $
41  * @created August 16, 2001
42  */

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

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

58    private final static String JavaDoc LOCAL_PORT = "org.jboss.mq.il.oil.localPort";
59
60    /** The server host name/IP to connect to
61     */

62    private InetAddress JavaDoc addr;
63    /** The server port to connect to.
64     */

65    private int port;
66    /** The name of the class implementing the javax.net.SocketFactory to
67     * use for creating the client socket.
68     */

69    private String JavaDoc socketFactoryName;
70
71    /**
72     * If the TcpNoDelay option should be used on the socket.
73     */

74    private boolean enableTcpNoDelay = false;
75    /** The local interface name/IP to use for the client
76     */

77    private transient InetAddress JavaDoc localAddr;
78    /** The local port to use for the client
79     */

80    private transient int localPort;
81
82    /**
83     * Description of the Field
84     */

85    private transient ObjectInputStream JavaDoc in;
86
87    /**
88     * Description of the Field
89     */

90    private transient ObjectOutputStream JavaDoc out;
91    
92    /**
93     * Description of the Field
94     */

95    private transient Socket JavaDoc socket;
96
97    /**
98     * Constructor for the OILServerIL object
99     *
100     * @param a Description of Parameter
101     * @param port Description of Parameter
102     */

103    public OILServerIL(InetAddress JavaDoc addr, int port,
104       String JavaDoc socketFactoryName, boolean enableTcpNoDelay)
105    {
106       this.addr = addr;
107       this.port = port;
108       this.socketFactoryName = socketFactoryName;
109       this.enableTcpNoDelay = enableTcpNoDelay;
110    }
111
112    /**
113     * Sets the ConnectionToken attribute of the OILServerIL object
114     *
115     * @param dest The new ConnectionToken value
116     * @exception Exception Description of Exception
117     */

118    public synchronized void setConnectionToken(ConnectionToken dest)
119           throws Exception JavaDoc
120    {
121       checkConnection();
122       out.writeByte(OILConstants.SET_SPY_DISTRIBUTED_CONNECTION);
123       out.writeObject(dest);
124       waitAnswer();
125    }
126
127    /**
128     * Sets the Enabled attribute of the OILServerIL object
129     *
130     * @param dc The new Enabled value
131     * @param enabled The new Enabled value
132     * @exception JMSException Description of Exception
133     * @exception Exception Description of Exception
134     */

135    public synchronized void setEnabled(ConnectionToken dc, boolean enabled)
136           throws JMSException JavaDoc, Exception JavaDoc
137    {
138       checkConnection();
139       out.writeByte(OILConstants.SET_ENABLED);
140       out.writeBoolean(enabled);
141       waitAnswer();
142    }
143
144    /**
145     * Gets the ID attribute of the OILServerIL object
146     *
147     * @return The ID value
148     * @exception Exception Description of Exception
149     */

150    public synchronized String JavaDoc getID()
151           throws Exception JavaDoc
152    {
153       checkConnection();
154       out.writeByte(OILConstants.GET_ID);
155       return (String JavaDoc)waitAnswer();
156    }
157
158    /**
159     * Gets the TemporaryQueue attribute of the OILServerIL object
160     *
161     * @param dc Description of Parameter
162     * @return The TemporaryQueue value
163     * @exception JMSException Description of Exception
164     * @exception Exception Description of Exception
165     */

166    public synchronized TemporaryQueue JavaDoc getTemporaryQueue(ConnectionToken dc)
167           throws JMSException JavaDoc, Exception JavaDoc
168    {
169       checkConnection();
170       out.writeByte(OILConstants.GET_TEMPORARY_QUEUE);
171       return (TemporaryQueue JavaDoc)waitAnswer();
172    }
173
174    /**
175     * Gets the TemporaryTopic attribute of the OILServerIL object
176     *
177     * @param dc Description of Parameter
178     * @return The TemporaryTopic value
179     * @exception JMSException Description of Exception
180     * @exception Exception Description of Exception
181     */

182    public synchronized TemporaryTopic JavaDoc getTemporaryTopic(ConnectionToken dc)
183           throws JMSException JavaDoc, Exception JavaDoc
184    {
185       checkConnection();
186       out.writeByte(OILConstants.GET_TEMPORARY_TOPIC);
187       return (TemporaryTopic JavaDoc)waitAnswer();
188    }
189
190    /**
191     * #Description of the Method
192     *
193     * @param dc Description of Parameter
194     * @param item Description of Parameter
195     * @exception JMSException Description of Exception
196     * @exception Exception Description of Exception
197     */

198    public synchronized void acknowledge(ConnectionToken dc, AcknowledgementRequest item)
199           throws JMSException JavaDoc, Exception JavaDoc
200    {
201       checkConnection();
202       out.writeByte(OILConstants.ACKNOWLEDGE);
203       item.writeExternal(out);
204       waitAnswer();
205    }
206
207    /**
208     * Adds a feature to the Message attribute of the OILServerIL object
209     *
210     * @param dc The feature to be added to the Message attribute
211     * @param val The feature to be added to the Message attribute
212     * @exception Exception Description of Exception
213     */

214    public synchronized void addMessage(ConnectionToken dc, SpyMessage val)
215           throws Exception JavaDoc
216    {
217       checkConnection();
218       out.writeByte(OILConstants.ADD_MESSAGE);
219       SpyMessage.writeMessage(val, out);
220       waitAnswer();
221    }
222
223    /**
224     * #Description of the Method
225     *
226     * @param dc Description of Parameter
227     * @param dest Description of Parameter
228     * @param selector Description of Parameter
229     * @return Description of the Returned Value
230     * @exception JMSException Description of Exception
231     * @exception Exception Description of Exception
232     */

233    public synchronized SpyMessage[] browse(ConnectionToken dc, Destination JavaDoc dest, String JavaDoc selector)
234           throws JMSException JavaDoc, Exception JavaDoc
235    {
236       checkConnection();
237       out.writeByte(OILConstants.BROWSE);
238       out.writeObject(dest);
239       out.writeObject(selector);
240       return (SpyMessage[])waitAnswer();
241    }
242
243    /**
244     * #Description of the Method
245     *
246     * @param ID Description of Parameter
247     * @exception JMSException Description of Exception
248     * @exception Exception Description of Exception
249     */

250    public synchronized void checkID(String JavaDoc ID)
251           throws JMSException JavaDoc, Exception JavaDoc
252    {
253       checkConnection();
254       out.writeByte(OILConstants.CHECK_ID);
255       out.writeObject(ID);
256       waitAnswer();
257    }
258
259    /**
260     * #Description of the Method
261     *
262     * @param userName Description of Parameter
263     * @param password Description of Parameter
264     * @return Description of the Returned Value
265     * @exception JMSException Description of Exception
266     * @exception Exception Description of Exception
267     */

268    public synchronized String JavaDoc checkUser(String JavaDoc userName, String JavaDoc password)
269           throws JMSException JavaDoc, Exception JavaDoc
270    {
271       checkConnection();
272       out.writeByte(OILConstants.CHECK_USER);
273       out.writeObject(userName);
274       out.writeObject(password);
275       return (String JavaDoc)waitAnswer();
276    }
277
278    /**
279     * #Description of the Method
280     *
281     * @param userName Description of Parameter
282     * @param password Description of Parameter
283     * @return Description of the Returned Value
284     * @exception JMSException Description of Exception
285     * @exception Exception Description of Exception
286     */

287    public synchronized String JavaDoc authenticate(String JavaDoc userName, String JavaDoc password)
288           throws JMSException JavaDoc, Exception JavaDoc
289    {
290       checkConnection();
291       out.writeByte(OILConstants.AUTHENTICATE);
292       out.writeObject(userName);
293       out.writeObject(password);
294       return (String JavaDoc)waitAnswer();
295    }
296    /**
297     * #Description of the Method
298     *
299     * @return Description of the Returned Value
300     * @exception CloneNotSupportedException Description of Exception
301     */

302    public Object JavaDoc clone()
303           throws CloneNotSupportedException JavaDoc
304    {
305       return super.clone();
306    }
307
308    /**
309     * Need to clone because there are instance variables tha can get clobbered.
310     * All Multiple connections can NOT share the same JVMServerIL object
311     *
312     * @return Description of the Returned Value
313     * @exception Exception Description of Exception
314     */

315    public ServerIL cloneServerIL()
316           throws Exception JavaDoc
317    {
318       return (ServerIL)clone();
319    }
320
321    /**
322     * #Description of the Method
323     *
324     * @param dc Description of Parameter
325     * @exception JMSException Description of Exception
326     * @exception Exception Description of Exception
327     */

328    public synchronized void connectionClosing(ConnectionToken dc)
329           throws JMSException JavaDoc, Exception JavaDoc
330    {
331       try
332       {
333          checkConnection();
334          out.writeByte(OILConstants.CONNECTION_CLOSING);
335          waitAnswer();
336       }
337       finally
338       {
339          destroyConnection();
340       }
341    }
342
343    /**
344     * #Description of the Method
345     *
346     * @param dc Description of Parameter
347     * @param dest Description of Parameter
348     * @return Description of the Returned Value
349     * @exception JMSException Description of Exception
350     * @exception Exception Description of Exception
351     */

352    public synchronized Queue JavaDoc createQueue(ConnectionToken dc, String JavaDoc dest)
353           throws JMSException JavaDoc, Exception JavaDoc
354    {
355       checkConnection();
356       out.writeByte(OILConstants.CREATE_QUEUE);
357       out.writeObject(dest);
358       return (Queue JavaDoc)waitAnswer();
359    }
360
361    /**
362     * #Description of the Method
363     *
364     * @param dc Description of Parameter
365     * @param dest Description of Parameter
366     * @return Description of the Returned Value
367     * @exception JMSException Description of Exception
368     * @exception Exception Description of Exception
369     */

370    public synchronized Topic JavaDoc createTopic(ConnectionToken dc, String JavaDoc dest)
371           throws JMSException JavaDoc, Exception JavaDoc
372    {
373       checkConnection();
374       out.writeByte(OILConstants.CREATE_TOPIC);
375       out.writeObject(dest);
376       return (Topic JavaDoc)waitAnswer();
377    }
378
379    /**
380     * #Description of the Method
381     *
382     * @param dc Description of Parameter
383     * @param dest Description of Parameter
384     * @exception JMSException Description of Exception
385     * @exception Exception Description of Exception
386     */

387    public synchronized void deleteTemporaryDestination(ConnectionToken dc, SpyDestination dest)
388           throws JMSException JavaDoc, Exception JavaDoc
389    {
390       checkConnection();
391       out.writeByte(OILConstants.DELETE_TEMPORARY_DESTINATION);
392       out.writeObject(dest);
393       waitAnswer();
394    }
395
396    /**
397     * #Description of the Method
398     *
399     * @param id Description of Parameter
400     * @exception JMSException Description of Exception
401     * @exception Exception Description of Exception
402     */

403    public synchronized void destroySubscription(ConnectionToken dc,DurableSubscriptionID id)
404           throws JMSException JavaDoc, Exception JavaDoc
405    {
406       checkConnection();
407       out.writeByte(OILConstants.DESTROY_SUBSCRIPTION);
408       out.writeObject(id);
409       waitAnswer();
410    }
411
412    /**
413     * #Description of the Method
414     *
415     * @param dc Description of Parameter
416     * @param clientTime Description of Parameter
417     * @exception Exception Description of Exception
418     */

419    public synchronized void ping(ConnectionToken dc, long clientTime)
420           throws Exception JavaDoc
421    {
422       checkConnection();
423       out.writeByte(OILConstants.PING);
424       out.writeLong(clientTime);
425       waitAnswer();
426    }
427
428    /**
429     * #Description of the Method
430     *
431     * @param dc Description of Parameter
432     * @param subscriberId Description of Parameter
433     * @param wait Description of Parameter
434     * @return Description of the Returned Value
435     * @exception Exception Description of Exception
436     */

437    public synchronized SpyMessage receive(ConnectionToken dc, int subscriberId, long wait)
438           throws Exception JavaDoc, Exception JavaDoc
439    {
440       checkConnection();
441       out.writeByte(OILConstants.RECEIVE);
442       out.writeInt(subscriberId);
443       out.writeLong(wait);
444       return (SpyMessage)waitAnswer();
445    }
446
447    /**
448     * #Description of the Method
449     *
450     * @param dc Description of Parameter
451     * @param s Description of Parameter
452     * @exception JMSException Description of Exception
453     * @exception Exception Description of Exception
454     */

455    public synchronized void subscribe(ConnectionToken dc, org.jboss.mq.Subscription s)
456           throws JMSException JavaDoc, Exception JavaDoc
457    {
458       checkConnection();
459       out.writeByte(OILConstants.SUBSCRIBE);
460       out.writeObject(s);
461       waitAnswer();
462    }
463
464    /**
465     * #Description of the Method
466     *
467     * @param dc Description of Parameter
468     * @param t Description of Parameter
469     * @exception JMSException Description of Exception
470     * @exception Exception Description of Exception
471     */

472    public synchronized void transact(org.jboss.mq.ConnectionToken dc, TransactionRequest t)
473           throws JMSException JavaDoc, Exception JavaDoc
474    {
475       checkConnection();
476       out.writeByte(OILConstants.TRANSACT);
477       t.writeExternal(out);
478       waitAnswer();
479    }
480
481    /**
482     * #Description of the Method
483     *
484     * @param dc Description of Parameter
485     * @param subscriptionId Description of Parameter
486     * @exception JMSException Description of Exception
487     * @exception Exception Description of Exception
488     */

489    public synchronized void unsubscribe(ConnectionToken dc, int subscriptionId)
490           throws JMSException JavaDoc, Exception JavaDoc
491    {
492       checkConnection();
493       out.writeByte(OILConstants.UNSUBSCRIBE);
494       out.writeInt(subscriptionId);
495       waitAnswer();
496    }
497
498    /**
499     * #Description of the Method
500     *
501     * @exception Exception Description of Exception
502     */

503    private void checkConnection()
504           throws Exception JavaDoc
505    {
506       if (socket == null)
507       {
508          createConnection();
509       }
510    }
511
512    /**
513     * Used to establish a new connection to the server
514     *
515     * @exception Exception Description of Exception
516     */

517    private void createConnection()
518           throws Exception JavaDoc
519    {
520       boolean tracing = log.isTraceEnabled();
521       if( tracing )
522          log.trace("Connecting to : "+addr+":"+port);
523
524       /** Attempt to load the socket factory and if this fails, use the
525        * default socket factory impl.
526        */

527       SocketFactory socketFactory = null;
528       if( socketFactoryName != null )
529       {
530          try
531          {
532             ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
533             Class JavaDoc factoryClass = loader.loadClass(socketFactoryName);
534             socketFactory = (SocketFactory) factoryClass.newInstance();
535          }
536          catch(Exception JavaDoc e)
537          {
538             log.debug("Failed to load socket factory: "+socketFactoryName, e);
539          }
540       }
541       // Use the default socket factory
542
if( socketFactory == null )
543       {
544          socketFactory = SocketFactory.getDefault();
545       }
546
547       // Look for a local address and port as properties
548
String JavaDoc tmp = System.getProperty(LOCAL_ADDR);
549       if( tmp != null )
550          this.localAddr = InetAddress.getByName(tmp);
551       tmp = System.getProperty(LOCAL_PORT);
552       if( tmp != null )
553          this.localPort = Integer.parseInt(tmp);
554       if( tracing )
555       {
556          log.trace("Connecting with addr="+addr+", port="+port
557             + ", localAddr="+localAddr+", localPort="+localPort
558             + ", socketFactory="+socketFactory);
559       }
560
561       if( localAddr != null )
562          socket = socketFactory.createSocket(addr, port, localAddr, localPort);
563       else
564          socket = socketFactory.createSocket(addr, port);
565
566       socket.setTcpNoDelay(enableTcpNoDelay);
567       in = new ObjectInputStream JavaDoc(new BufferedInputStream JavaDoc(socket.getInputStream()));
568       out = new ObjectOutputStream JavaDoc(new BufferedOutputStream JavaDoc(socket.getOutputStream()));
569       out.flush();
570    }
571    
572    /**
573     * Used to close the current connection with the server
574     *
575     * @exception Exception Description of Exception
576     */

577    private void destroyConnection()
578           throws Exception JavaDoc
579    {
580       try
581       {
582          out.close();
583          in.close();
584       }
585       finally
586       {
587          socket.close();
588       }
589    }
590
591    /**
592     * #Description of the Method
593     *
594     * @return Description of the Returned Value
595     * @exception Exception Description of Exception
596     */

597    private Object JavaDoc waitAnswer()
598           throws Exception JavaDoc
599    {
600       out.reset();
601       out.flush();
602       int val = in.readByte();
603       if (val == OILConstants.SUCCESS)
604       {
605          return null;
606       }
607       if (val == OILConstants.SUCCESS_OBJECT)
608       {
609          return in.readObject();
610       }
611       else
612       {
613          Exception JavaDoc e = (Exception JavaDoc)in.readObject();
614          throw e;
615       }
616    }
617 }
618
Popular Tags