1 18 package org.apache.activemq.command; 19 20 import java.io.DataInputStream ; 21 import java.io.DataOutputStream ; 22 import java.io.IOException ; 23 import java.util.Arrays ; 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 28 import org.apache.activemq.state.CommandVisitor; 29 import org.apache.activemq.util.ByteArrayInputStream; 30 import org.apache.activemq.util.ByteArrayOutputStream; 31 import org.apache.activemq.util.ByteSequence; 32 import org.apache.activemq.util.MarshallingSupport; 33 import org.apache.activemq.wireformat.WireFormat; 34 35 40 public class WireFormatInfo implements Command, MarshallAware { 41 42 private static final int MAX_PROPERTY_SIZE = 1024*4; 43 public static final byte DATA_STRUCTURE_TYPE = CommandTypes.WIREFORMAT_INFO; 44 static final private byte MAGIC[] = new byte[] { 'A', 'c', 't', 'i', 'v', 'e', 'M', 'Q' }; 45 46 protected byte magic[] = MAGIC; 47 protected int version; 48 protected ByteSequence marshalledProperties; 49 50 protected transient Map properties; 51 private transient Endpoint from; 52 private transient Endpoint to; 53 54 public byte getDataStructureType() { 55 return DATA_STRUCTURE_TYPE; 56 } 57 58 public boolean isWireFormatInfo() { 59 return true; 60 } 61 62 public boolean isMarshallAware() { 63 return true; 64 } 65 66 69 public byte[] getMagic() { 70 return magic; 71 } 72 public void setMagic(byte[] magic) { 73 this.magic = magic; 74 } 75 76 79 public int getVersion() { 80 return version; 81 } 82 public void setVersion(int version) { 83 this.version = version; 84 } 85 86 89 public ByteSequence getMarshalledProperties() { 90 return marshalledProperties; 91 } 92 public void setMarshalledProperties(ByteSequence marshalledProperties) { 93 this.marshalledProperties = marshalledProperties; 94 } 95 96 99 public Endpoint getFrom() { 100 return from; 101 } 102 103 public void setFrom(Endpoint from) { 104 this.from = from; 105 } 106 107 110 public Endpoint getTo() { 111 return to; 112 } 113 114 public void setTo(Endpoint to) { 115 this.to = to; 116 } 117 118 124 public Object getProperty(String name) throws IOException { 125 if( properties == null ) { 126 if( marshalledProperties ==null ) 127 return null; 128 properties = unmarsallProperties(marshalledProperties); 129 } 130 return properties.get(name); 131 } 132 133 public Map getProperties() throws IOException { 134 if( properties == null ) { 135 if( marshalledProperties==null ) 136 return Collections.EMPTY_MAP; 137 properties = unmarsallProperties(marshalledProperties); 138 } 139 return Collections.unmodifiableMap(properties); 140 } 141 142 public void clearProperties() { 143 marshalledProperties = null; 144 properties=null; 145 } 146 147 public void setProperty(String name, Object value) throws IOException { 148 lazyCreateProperties(); 149 properties.put(name, value); 150 } 151 152 protected void lazyCreateProperties() throws IOException { 153 if( properties == null ) { 154 if( marshalledProperties == null ) { 155 properties = new HashMap (); 156 } else { 157 properties = unmarsallProperties(marshalledProperties); 158 marshalledProperties = null; 159 } 160 } 161 } 162 163 private Map unmarsallProperties(ByteSequence marshalledProperties) throws IOException { 164 return MarshallingSupport.unmarshalPrimitiveMap(new DataInputStream (new ByteArrayInputStream(marshalledProperties)), MAX_PROPERTY_SIZE); 165 } 166 167 public void beforeMarshall(WireFormat wireFormat) throws IOException { 168 if( marshalledProperties==null && properties!=null ) { 170 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 171 DataOutputStream os = new DataOutputStream (baos); 172 MarshallingSupport.marshalPrimitiveMap(properties, os); 173 os.close(); 174 marshalledProperties = baos.toByteSequence(); 175 } 176 } 177 178 public void afterMarshall(WireFormat wireFormat) throws IOException { 179 } 180 181 public void beforeUnmarshall(WireFormat wireFormat) throws IOException { 182 } 183 184 public void afterUnmarshall(WireFormat wireFormat) throws IOException { 185 } 186 187 188 public boolean isValid() { 189 return magic != null && Arrays.equals(magic, MAGIC); 190 } 191 192 public void setResponseRequired(boolean responseRequired) { 193 } 194 195 198 public boolean isCacheEnabled() throws IOException { 199 return Boolean.TRUE == getProperty("CacheEnabled"); 200 } 201 public void setCacheEnabled(boolean cacheEnabled) throws IOException { 202 setProperty("CacheEnabled", cacheEnabled ? Boolean.TRUE : Boolean.FALSE); 203 } 204 205 208 public boolean isStackTraceEnabled() throws IOException { 209 return Boolean.TRUE == getProperty("StackTraceEnabled"); 210 } 211 public void setStackTraceEnabled(boolean stackTraceEnabled) throws IOException { 212 setProperty("StackTraceEnabled", stackTraceEnabled ? Boolean.TRUE : Boolean.FALSE); 213 } 214 215 218 public boolean isTcpNoDelayEnabled() throws IOException { 219 return Boolean.TRUE == getProperty("TcpNoDelayEnabled"); 220 } 221 public void setTcpNoDelayEnabled(boolean tcpNoDelayEnabled) throws IOException { 222 setProperty("TcpNoDelayEnabled", tcpNoDelayEnabled ? Boolean.TRUE : Boolean.FALSE); 223 } 224 225 228 public boolean isSizePrefixDisabled() throws IOException { 229 return Boolean.TRUE == getProperty("SizePrefixDisabled"); 230 } 231 public void setSizePrefixDisabled(boolean prefixPacketSize) throws IOException { 232 setProperty("SizePrefixDisabled", prefixPacketSize ? Boolean.TRUE : Boolean.FALSE); 233 } 234 235 238 public boolean isTightEncodingEnabled() throws IOException { 239 return Boolean.TRUE == getProperty("TightEncodingEnabled"); 240 } 241 public void setTightEncodingEnabled(boolean tightEncodingEnabled) throws IOException { 242 setProperty("TightEncodingEnabled", tightEncodingEnabled ? Boolean.TRUE : Boolean.FALSE); 243 } 244 245 248 public long getMaxInactivityDuration() throws IOException { 249 Long l = (Long ) getProperty("MaxInactivityDuration"); 250 return l == null ? 0 : l.longValue(); 251 } 252 public void seMaxInactivityDuration(long maxInactivityDuration) throws IOException { 253 setProperty("MaxInactivityDuration", new Long (maxInactivityDuration)); 254 } 255 256 259 public int getCacheSize() throws IOException { 260 Integer i = (Integer ) getProperty("CacheSize"); 261 return i == null ? 0 : i.intValue(); 262 } 263 public void setCacheSize(int cacheSize) throws IOException { 264 setProperty("CacheSize", new Integer (cacheSize)); 265 } 266 267 268 public Response visit(CommandVisitor visitor) throws Exception { 269 return visitor.processWireFormat(this); 270 } 271 272 public String toString() { 273 Map p=null; 274 try { 275 p = getProperties(); 276 } catch (IOException e) { 277 } 278 return "WireFormatInfo { version="+version+", properties="+p+", magic="+toString(magic)+"}"; 279 } 280 281 private String toString(byte []data) { 282 StringBuffer sb = new StringBuffer (); 283 sb.append('['); 284 for (int i = 0; i < data.length; i++) { 285 if( i != 0 ) { 286 sb.append(','); 287 } 288 sb.append((char)data[i]); 289 } 290 sb.append(']'); 291 return sb.toString(); 292 } 293 294 300 public void setCommandId(int value) { 301 } 302 public int getCommandId() { 303 return 0; 304 } 305 public boolean isResponseRequired() { 306 return false; 307 } 308 public boolean isResponse() { 309 return false; 310 } 311 public boolean isBrokerInfo() { 312 return false; 313 } 314 public boolean isMessageDispatch() { 315 return false; 316 } 317 public boolean isMessage() { 318 return false; 319 } 320 public boolean isMessageAck() { 321 return false; 322 } 323 public boolean isMessageDispatchNotification(){ 324 return false; 325 } 326 public boolean isShutdownInfo(){ 327 return false; 328 } 329 public void setCachedMarshalledForm(WireFormat wireFormat, ByteSequence data) { 330 } 331 public ByteSequence getCachedMarshalledForm(WireFormat wireFormat) { 332 return null; 333 } 334 335 } 336 | Popular Tags |