KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > SpyDestination


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mq;
23
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectInput JavaDoc;
26 import java.io.ObjectOutput JavaDoc;
27 import java.io.Serializable JavaDoc;
28
29 import javax.jms.Destination JavaDoc;
30
31 /**
32  * This class implements javax.jms.Destination
33  *
34  * @author Norbert Lataille (Norbert.Lataille@m4x.org)
35  * @author Hiram Chirino (Cojonudo14@hotmail.com)
36  * @author David Maplesden (David.Maplesden@orion.co.nz)
37  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
38  * @version $Revision: 37459 $
39  */

40 public class SpyDestination implements Destination JavaDoc, Serializable JavaDoc
41 {
42    // Constants -----------------------------------------------------
43

44    /** The serialVersionUID */
45    static final long serialVersionUID = -451227938651163471L;
46
47    /** Null object */
48    protected final static int NULL = 0;
49    /** An object */
50    protected final static int OBJECT = 1;
51    /** A queue */
52    protected final static int SPY_QUEUE = 2;
53    /** A topic */
54    protected final static int SPY_TOPIC = 3;
55    /** A temporary queue */
56    protected final static int SPY_TEMP_QUEUE = 4;
57    /** A temporary topic */
58    protected final static int SPY_TEMP_TOPIC = 5;
59
60    // Attributes ----------------------------------------------------
61

62    /** The name */
63    protected String JavaDoc name;
64
65    /** The hash code */
66    protected int hash;
67
68    // Static --------------------------------------------------------
69

70    /**
71     * Write the destination
72     *
73     * @param out the output
74     * @param dest the destination
75     * @throws IOException for any error
76     */

77    public static void writeDest(ObjectOutput JavaDoc out, Destination JavaDoc dest) throws IOException JavaDoc
78    {
79       if (dest == null)
80          out.writeByte(NULL);
81       else if (dest instanceof SpyTemporaryQueue)
82       {
83          out.writeByte(SPY_TEMP_QUEUE);
84          out.writeUTF(((SpyTemporaryQueue) dest).getName());
85       }
86       else if (dest instanceof SpyTemporaryTopic)
87       {
88          out.writeByte(SPY_TEMP_TOPIC);
89          out.writeUTF(((SpyTemporaryTopic) dest).getName());
90       }
91       else if (dest instanceof SpyQueue)
92       {
93          out.writeByte(SPY_QUEUE);
94          out.writeUTF(((SpyQueue) dest).getName());
95       }
96       else if (dest instanceof SpyTopic)
97       {
98          out.writeByte(SPY_TOPIC);
99          out.writeUTF(((SpyTopic) dest).getName());
100          DurableSubscriptionID id = ((SpyTopic) dest).durableSubscriptionID;
101          if (id == null)
102          {
103             out.writeByte(NULL);
104          }
105          else
106          {
107             out.writeByte(OBJECT);
108             writeString(out, id.getClientID());
109             writeString(out, id.getSubscriptionName());
110             writeString(out, id.getSelector());
111          }
112       }
113       else
114       {
115          out.writeByte(OBJECT);
116          out.writeObject(dest);
117       }
118    }
119
120    /**
121     * Read a destination
122     *
123     * @param in the input
124     * @return the destination
125     * @throws IOException for any error
126     */

127    public static Destination JavaDoc readDest(ObjectInput JavaDoc in) throws IOException JavaDoc
128    {
129       byte destType = in.readByte();
130       if (destType == NULL)
131          return null;
132       else if (destType == SPY_TEMP_QUEUE)
133          return new SpyTemporaryQueue(in.readUTF(), null);
134       else if (destType == SPY_TEMP_TOPIC)
135          return new SpyTemporaryTopic(in.readUTF(), null);
136       else if (destType == SPY_QUEUE)
137          return new SpyQueue(in.readUTF());
138       else if (destType == SPY_TOPIC)
139       {
140          String JavaDoc name = in.readUTF();
141          destType = in.readByte();
142          if (destType == NULL)
143             return new SpyTopic(name);
144          else
145          {
146             String JavaDoc clientId = readString(in);
147             String JavaDoc subName = readString(in);
148             String JavaDoc selector = readString(in);
149             return new SpyTopic(new SpyTopic(name), clientId, subName, selector);
150          }
151       }
152       else
153       {
154          try
155          {
156             return (Destination JavaDoc) in.readObject();
157          }
158          catch (ClassNotFoundException JavaDoc e)
159          {
160             throw new IOException JavaDoc("Class not found for unknown destination.");
161          }
162       }
163    }
164
165    // Constructors --------------------------------------------------
166

167    /**
168      * Create a new SpyDestination
169      *
170      * @param name the name
171      */

172    SpyDestination(String JavaDoc name)
173    {
174       this.name = name;
175       hash = name.hashCode();
176    }
177
178    // Public --------------------------------------------------------
179

180    /**
181      * Gets the name of the destination.
182      *
183      * @return the name
184      */

185    public String JavaDoc getName()
186    {
187       return name;
188    }
189
190    // Object overrides ----------------------------------------------
191

192    public int hashCode()
193    {
194       return hash;
195    }
196
197    // Package protected ---------------------------------------------
198

199    // Protected -----------------------------------------------------
200

201    // Private -------------------------------------------------------
202

203    /**
204     * Write a string
205     *
206     * @param out the output
207     * @param s the string
208     * @throws IOException for any error
209     */

210    private static void writeString(ObjectOutput JavaDoc out, String JavaDoc s) throws IOException JavaDoc
211    {
212       if (s == null)
213          out.writeByte(NULL);
214       else
215       {
216          out.writeByte(OBJECT);
217          //non-null
218
out.writeUTF(s);
219       }
220    }
221
222    /**
223     * Read a string
224     *
225     * @param in the input
226     * @return the string
227     * @throws IOException for any error
228     */

229    private static String JavaDoc readString(ObjectInput JavaDoc in) throws IOException JavaDoc
230    {
231       byte b = in.readByte();
232       if (b == NULL)
233          return null;
234       else
235          return in.readUTF();
236    }
237
238    // Inner classes -------------------------------------------------
239
}
Popular Tags