1 48 49 50 package org.exolab.jms.message; 51 52 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 69 class CorrelationId implements Externalizable { 70 71 74 static final long serialVersionUID = 1; 75 76 79 static final int APPLICATION_USE = 1; 80 static final int PROVIDER_USE = 2; 81 static final int PROVIDER_NATIVE = 3; 82 83 86 private int _usage = 0; 87 88 91 private MessageId _id = null; 92 93 96 private String _clientId = null; 97 98 101 public CorrelationId() { 102 } 103 104 public CorrelationId(String id) throws JMSException { 105 if (id.startsWith(MessageId.PREFIX)) { 107 _usage = PROVIDER_USE; 109 _id = new MessageId(id); 110 } else { 111 _usage = APPLICATION_USE; 113 _clientId = id; 114 } 115 } 116 117 public CorrelationId(byte[] id) throws JMSException { 119 throw new UnsupportedOperationException ( 120 "Provider native correlation identifier not supported"); 121 } 122 123 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 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 } | Popular Tags |