View Javadoc

1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2000 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: CorrelationId.java,v 1.1 2004/11/26 01:50:43 tanderson Exp $
44   *
45   * Date         Author  Changes
46   * 02/26/2000   jimm    Created
47   */
48  
49  
50  package org.exolab.jms.message;
51  
52  // java io
53  
54  import java.io.Externalizable;
55  import java.io.IOException;
56  import java.io.ObjectInput;
57  import java.io.ObjectOutput;
58  
59  import javax.jms.JMSException;
60  
61  
62  /***
63   * This class implements the JMSCorrelationID message header property
64   *
65   * @version     $Revision: 1.1 $ $Date: 2004/11/26 01:50:43 $
66   * @author      <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
67   * @see         javax.jms.Message
68   */
69  class CorrelationId implements Externalizable {
70  
71      /***
72       * Object version no. for serialization
73       */
74      static final long serialVersionUID = 1;
75  
76      /***
77       * Possible usages
78       */
79      static final int APPLICATION_USE = 1;
80      static final int PROVIDER_USE = 2;
81      static final int PROVIDER_NATIVE = 3;
82  
83      /***
84       * What is Correlation Id is used for
85       */
86      private int _usage = 0;
87  
88      /***
89       * Link to another message
90       */
91      private MessageId _id = null;
92  
93      /***
94       * Application specific link
95       */
96      private String _clientId = null;
97  
98      /***
99       * Default constructor for externalization support
100      */
101     public CorrelationId() {
102     }
103 
104     public CorrelationId(String id) throws JMSException {
105         // Get our own copy
106         if (id.startsWith(MessageId.PREFIX)) {
107             // Linked with another message
108             _usage = PROVIDER_USE;
109             _id = new MessageId(id);
110         } else {
111             // Client application specific
112             _usage = APPLICATION_USE;
113             _clientId = id;
114         }
115     }
116 
117     // Provider Native not supported
118     public CorrelationId(byte[] id) throws JMSException {
119         throw new UnsupportedOperationException(
120             "Provider native correlation identifier not supported");
121     }
122 
123     // Marshall out
124     public void writeExternal(ObjectOutput out) throws IOException {
125         out.writeLong(serialVersionUID);
126         out.writeInt(_usage);
127         if (_usage == APPLICATION_USE) {
128             out.writeInt(_clientId.length());
129             out.writeChars(_clientId);
130         } else if (_usage == PROVIDER_USE) {
131             _id.writeExternal(out);
132         }
133     }
134 
135     // Marshall in
136     public void readExternal(ObjectInput in)
137         throws IOException, ClassNotFoundException {
138         long version = in.readLong();
139         if (version == serialVersionUID) {
140             _usage = in.readInt();
141             if (_usage == APPLICATION_USE) {
142                 int len = in.readInt();
143                 int i;
144                 StringBuffer buf = new StringBuffer(len);
145                 for (i = 0; i < len; i++) {
146                     buf.append(in.readChar());
147                 }
148                 _clientId = buf.toString();
149             } else if (_usage == PROVIDER_USE) {
150                 _id = new MessageId();
151                 _id.readExternal(in);
152             }
153         } else {
154             throw new IOException("Incorrect version enountered: " + version +
155                 ". This version = " + serialVersionUID);
156         }
157     }
158 
159     public String getString() throws JMSException {
160         String result = null;
161         if (_usage == APPLICATION_USE) {
162             result = _clientId;
163         } else if (_usage == PROVIDER_USE) {
164             result = _id.toString();
165         } else {
166             throw new JMSException("Unknown correlation");
167         }
168         return result;
169     }
170 
171     public byte[] getBytes() throws JMSException {
172         throw new UnsupportedOperationException(
173             "Provider native correlation identifier not supported");
174     }
175 
176 } // End CorrelationId