001 /*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements. See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership. The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied. See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 *
020 */
021 package org.apache.qpid.management.messages;
022
023 import java.io.IOException;
024 import java.nio.ByteBuffer;
025
026 import org.apache.qpid.api.Message;
027 import org.apache.qpid.management.configuration.Configuration;
028 import org.apache.qpid.management.domain.services.SequenceNumberGenerator;
029 import org.apache.qpid.transport.DeliveryProperties;
030 import org.apache.qpid.transport.Header;
031 import org.apache.qpid.transport.MessageProperties;
032 import org.apache.qpid.transport.codec.BBEncoder;
033
034 /**
035 * Message implementation used for specific management purposes.
036 *
037 * @author Andrea Gazzarini
038 */
039 public abstract class ManagementMessage implements Message
040 {
041 /**
042 * Strategy interface for building / getting data.
043 *
044 * @author Andrea Gazzarini
045 */
046 private interface IDataBuilderStrategy
047 {
048 ByteBuffer getData();
049 };
050
051 /**
052 * Strategy used for retrieving raw data from this message when it has been already encoded.
053 */
054 IDataBuilderStrategy READING = new IDataBuilderStrategy()
055 {
056 public ByteBuffer getData() {
057 return _data;
058 };
059 };
060
061 /**
062 * Strategy used for retrieving raw data from this message when it hasn't been already encoded.
063 */
064 IDataBuilderStrategy ACCUMULATING = new IDataBuilderStrategy()
065 {
066 public ByteBuffer getData() {
067 _codec.writeInt8((byte)opcode());
068 _codec.writeSequenceNo(sequenceNumber());
069
070 specificMessageEncoding();
071
072 _data =_codec.segment();
073 _reader = READING;
074 return _data;
075 }
076 };
077
078 protected BBEncoder _codec;
079 protected ByteBuffer _data;
080 private int _messageTransferId;
081 private IDataBuilderStrategy _reader = ACCUMULATING;
082
083 /**
084 * Builds an empty management message.
085 */
086 ManagementMessage()
087 {
088 _codec = new BBEncoder(100);
089 _codec.writeMagicNumber();
090 }
091
092 /**
093 * Returns the sequence number that will be used for this message.
094 *
095 * @return the sequence number that will be used for this message.
096 */
097 protected int sequenceNumber ()
098 {
099 return SequenceNumberGenerator.getNextSequenceNumber();
100 }
101
102 /**
103 * Returns the opcode that will be used for this message.
104 *
105 * @return the opcode that will be used for this message.
106 */
107 abstract char opcode ();
108
109 /**
110 * Returns the delivery properties of this message.
111 *
112 * @return the delivery properties of this message.
113 */
114 public DeliveryProperties getDeliveryProperties ()
115 {
116 return Configuration.getInstance().getCommandDeliveryProperties();
117 }
118
119 /**
120 * Returns the header of this message.
121 *
122 * @return the header of this message.
123 */
124 public Header getHeader ()
125 {
126 return Configuration.getInstance().getCommandMessageHeader();
127 }
128
129 /**
130 * Returns the messages header properties of this message.
131 *
132 * @return the message header properties of this message.
133 */
134 public MessageProperties getMessageProperties ()
135 {
136 return Configuration.getInstance().getCommandMessageProperties();
137 }
138
139 /**
140 * Returns the transfer Id of this message.
141 *
142 * @return the transfer Id of this message.
143 */
144 public int getMessageTransferId ()
145 {
146 return _messageTransferId;
147 }
148
149 /**
150 * Returns the encoded data of this message.
151 *
152 * @return the encoded data of this message.
153 */
154 public ByteBuffer readData () throws IOException
155 {
156 return _reader.getData();
157 }
158
159 /**
160 * Sets the header for this message.
161 *
162 * @param header the new message header.
163 */
164 public void setHeader (Header header)
165 {
166 // N.A. at the moment.
167 }
168
169 public void appendData (byte[] src) throws IOException
170 {
171 }
172
173 public void appendData (ByteBuffer src) throws IOException
174 {
175 }
176
177 public void clearData ()
178 {
179 }
180
181 public void readData (byte[] target) throws IOException
182 {
183 }
184
185 /**
186 * Concrete subclasses (message implementations) must define here their specific data encoding.
187 */
188 abstract void specificMessageEncoding();
189 }
|