KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > command > WireFormatInfo


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.command;
19
20 import java.io.DataInputStream JavaDoc;
21 import java.io.DataOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
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 /**
36  *
37  * @openwire:marshaller code="1"
38  * @version $Revision$
39  */

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 JavaDoc 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     /**
67      * @openwire:property version=1 size=8 testSize=-1
68      */

69     public byte[] getMagic() {
70         return magic;
71     }
72     public void setMagic(byte[] magic) {
73         this.magic = magic;
74     }
75
76     /**
77      * @openwire:property version=1
78      */

79     public int getVersion() {
80         return version;
81     }
82     public void setVersion(int version) {
83         this.version = version;
84     }
85     
86     /**
87      * @openwire:property version=1
88      */

89     public ByteSequence getMarshalledProperties() {
90         return marshalledProperties;
91     }
92     public void setMarshalledProperties(ByteSequence marshalledProperties) {
93         this.marshalledProperties = marshalledProperties;
94     }
95
96     /**
97      * The endpoint within the transport where this message came from.
98      */

99     public Endpoint getFrom() {
100         return from;
101     }
102
103     public void setFrom(Endpoint from) {
104         this.from = from;
105     }
106
107     /**
108      * The endpoint within the transport where this message is going to - null means all endpoints.
109      */

110     public Endpoint getTo() {
111         return to;
112     }
113
114     public void setTo(Endpoint to) {
115         this.to = to;
116     }
117     
118     //////////////////////
119
//
120
// Implementation Methods.
121
//
122
//////////////////////
123

124     public Object JavaDoc getProperty(String JavaDoc name) throws IOException JavaDoc {
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 JavaDoc getProperties() throws IOException JavaDoc {
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 JavaDoc name, Object JavaDoc value) throws IOException JavaDoc {
148         lazyCreateProperties();
149         properties.put(name, value);
150     }
151
152     protected void lazyCreateProperties() throws IOException JavaDoc {
153         if( properties == null ) {
154             if( marshalledProperties == null ) {
155                 properties = new HashMap JavaDoc();
156             } else {
157                 properties = unmarsallProperties(marshalledProperties);
158                 marshalledProperties = null;
159             }
160         }
161     }
162     
163     private Map JavaDoc unmarsallProperties(ByteSequence marshalledProperties) throws IOException JavaDoc {
164         return MarshallingSupport.unmarshalPrimitiveMap(new DataInputStream JavaDoc(new ByteArrayInputStream(marshalledProperties)), MAX_PROPERTY_SIZE);
165     }
166
167     public void beforeMarshall(WireFormat wireFormat) throws IOException JavaDoc {
168         // Need to marshal the properties.
169
if( marshalledProperties==null && properties!=null ) {
170             ByteArrayOutputStream baos = new ByteArrayOutputStream();
171             DataOutputStream JavaDoc os = new DataOutputStream JavaDoc(baos);
172             MarshallingSupport.marshalPrimitiveMap(properties, os);
173             os.close();
174             marshalledProperties = baos.toByteSequence();
175         }
176     }
177
178     public void afterMarshall(WireFormat wireFormat) throws IOException JavaDoc {
179     }
180
181     public void beforeUnmarshall(WireFormat wireFormat) throws IOException JavaDoc {
182     }
183
184     public void afterUnmarshall(WireFormat wireFormat) throws IOException JavaDoc {
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     /**
196      * @throws IOException
197      */

198     public boolean isCacheEnabled() throws IOException JavaDoc {
199         return Boolean.TRUE == getProperty("CacheEnabled");
200     }
201     public void setCacheEnabled(boolean cacheEnabled) throws IOException JavaDoc {
202         setProperty("CacheEnabled", cacheEnabled ? Boolean.TRUE : Boolean.FALSE);
203     }
204
205     /**
206      * @throws IOException
207      */

208     public boolean isStackTraceEnabled() throws IOException JavaDoc {
209         return Boolean.TRUE == getProperty("StackTraceEnabled");
210     }
211     public void setStackTraceEnabled(boolean stackTraceEnabled) throws IOException JavaDoc {
212         setProperty("StackTraceEnabled", stackTraceEnabled ? Boolean.TRUE : Boolean.FALSE);
213     }
214
215     /**
216      * @throws IOException
217      */

218     public boolean isTcpNoDelayEnabled() throws IOException JavaDoc {
219         return Boolean.TRUE == getProperty("TcpNoDelayEnabled");
220     }
221     public void setTcpNoDelayEnabled(boolean tcpNoDelayEnabled) throws IOException JavaDoc {
222         setProperty("TcpNoDelayEnabled", tcpNoDelayEnabled ? Boolean.TRUE : Boolean.FALSE);
223     }
224
225     /**
226      * @throws IOException
227      */

228     public boolean isSizePrefixDisabled() throws IOException JavaDoc {
229         return Boolean.TRUE == getProperty("SizePrefixDisabled");
230     }
231     public void setSizePrefixDisabled(boolean prefixPacketSize) throws IOException JavaDoc {
232         setProperty("SizePrefixDisabled", prefixPacketSize ? Boolean.TRUE : Boolean.FALSE);
233     }
234
235     /**
236      * @throws IOException
237      */

238     public boolean isTightEncodingEnabled() throws IOException JavaDoc {
239         return Boolean.TRUE == getProperty("TightEncodingEnabled");
240     }
241     public void setTightEncodingEnabled(boolean tightEncodingEnabled) throws IOException JavaDoc {
242         setProperty("TightEncodingEnabled", tightEncodingEnabled ? Boolean.TRUE : Boolean.FALSE);
243     }
244     
245     /**
246      * @throws IOException
247      */

248     public long getMaxInactivityDuration() throws IOException JavaDoc {
249         Long JavaDoc l = (Long JavaDoc) getProperty("MaxInactivityDuration");
250         return l == null ? 0 : l.longValue();
251     }
252     public void seMaxInactivityDuration(long maxInactivityDuration) throws IOException JavaDoc {
253         setProperty("MaxInactivityDuration", new Long JavaDoc(maxInactivityDuration));
254     }
255
256     /**
257      * @throws IOException
258      */

259     public int getCacheSize() throws IOException JavaDoc {
260         Integer JavaDoc i = (Integer JavaDoc) getProperty("CacheSize");
261         return i == null ? 0 : i.intValue();
262     }
263     public void setCacheSize(int cacheSize) throws IOException JavaDoc {
264         setProperty("CacheSize", new Integer JavaDoc(cacheSize));
265     }
266     
267     
268     public Response visit(CommandVisitor visitor) throws Exception JavaDoc {
269         return visitor.processWireFormat(this);
270     }
271
272     public String JavaDoc toString() {
273         Map JavaDoc p=null;
274         try {
275             p = getProperties();
276         } catch (IOException JavaDoc e) {
277         }
278         return "WireFormatInfo { version="+version+", properties="+p+", magic="+toString(magic)+"}";
279     }
280     
281     private String JavaDoc toString(byte []data) {
282         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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     ///////////////////////////////////////////////////////////////
295
//
296
// This are not implemented.
297
//
298
///////////////////////////////////////////////////////////////
299

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