AMQConstant.java
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.protocol;
022 
023 import java.util.HashMap;
024 import java.util.Map;
025 
026 import org.apache.qpid.framing.AMQShortString;
027 
028 /**
029  * Defines constants for AMQP codes and also acts as a factory for creating such constants from the raw codes. Each
030  * constant also defines a short human readable description of the constant.
031  *
032  * @todo Why would a constant be defined that is not in the map? Seems more natural that getConstant should raise an
033  *       exception for an unknown constant. Or else provide an explanation of why this is so. Also, there is no way for
034  *       callers to determine the unknown status of a code except by comparing its name to "unknown code", which would
035  *       seem to render this scheme a little bit pointless?
036  *
037  * @todo Java has a nice enum construct for doing this sort of thing. Maybe this is done in the old style for Java 1.4
038  *       backward compatability? Now that is handled through retrotranslater it may be time to use enum.
039  *
040  <p/><tabld id="crc"><caption>CRC Card</caption>
041  <tr><th> Responsibilities <th> Collaborations
042  <tr><td> Define the set of AMQP status codes.
043  <tr><td> Provide a factory to lookup constants by their code.
044  <tr><td>
045  */
046 public final class AMQConstant
047 {
048     /** Defines a map from codes to constants. */
049     private static Map _codeMap = new HashMap();
050 
051     /** Indicates that the method completed successfully. */
052     public static final AMQConstant REPLY_SUCCESS = new AMQConstant(200"reply success"true);
053 
054     public static final AMQConstant FRAME_END = new AMQConstant(206"frame end"true);
055 
056     /**
057      * The client asked for a specific message that is no longer available. The message was delivered to another
058      * client, or was purged from the queue for some other reason.
059      */
060     public static final AMQConstant NOT_DELIVERED = new AMQConstant(310"not delivered"true);
061 
062     /**
063      * The client attempted to transfer content larger than the server could accept at the present time.  The client
064      * may retry at a later time.
065      */
066     public static final AMQConstant MESSAGE_TOO_LARGE = new AMQConstant(311"message too large"true);
067 
068     /**
069      * When the exchange cannot route the result of a .Publish, most likely due to an invalid routing key. Only when
070      * the mandatory flag is set.
071      */
072     public static final AMQConstant NO_ROUTE = new AMQConstant(312"no route"true);
073 
074     /**
075      * When the exchange cannot deliver to a consumer when the immediate flag is set. As a result of pending data on
076      * the queue or the absence of any consumers of the queue.
077      */
078     public static final AMQConstant NO_CONSUMERS = new AMQConstant(313"no consumers"true);
079 
080     /**
081      * An operator intervened to close the connection for some reason. The client may retry at some later date.
082      */
083     public static final AMQConstant CONTEXT_IN_USE = new AMQConstant(320"context in use"true);
084 
085     /** The client tried to work with an unknown virtual host or cluster. */
086     public static final AMQConstant INVALID_PATH = new AMQConstant(402"invalid path"true);
087 
088     /** The client attempted to work with a server entity to which it has no access due to security settings. */
089     public static final AMQConstant ACCESS_REFUSED = new AMQConstant(403"access refused"true);
090 
091     /** The client attempted to work with a server entity that does not exist. */
092     public static final AMQConstant NOT_FOUND = new AMQConstant(404"not found"true);
093 
094     /**
095      * The client attempted to work with a server entity to which it has no access because another client is
096      * working with it.
097      */
098     public static final AMQConstant ALREADY_EXISTS = new AMQConstant(405"Already exists"true);
099 
100     /** The client requested a method that was not allowed because some precondition failed. */
101     public static final AMQConstant IN_USE = new AMQConstant(406"In use"true);
102 
103     public static final AMQConstant INVALID_ROUTING_KEY = new AMQConstant(407"routing key invalid"true);
104 
105     public static final AMQConstant REQUEST_TIMEOUT = new AMQConstant(408"Request Timeout"true);
106 
107     public static final AMQConstant INVALID_ARGUMENT = new AMQConstant(409"argument invalid"true);
108 
109     /**
110      * The client sent a malformed frame that the server could not decode. This strongly implies a programming error
111      * in the client.
112      */
113     public static final AMQConstant FRAME_ERROR = new AMQConstant(501"frame error"true);
114 
115     /**
116      * The client sent a frame that contained illegal values for one or more fields. This strongly implies a
117      * programming error in the client.
118      */
119     public static final AMQConstant SYNTAX_ERROR = new AMQConstant(502"syntax error"true);
120 
121     /**
122      * The client sent an invalid sequence of frames, attempting to perform an operation that was considered invalid
123      * by the server. This usually implies a programming error in the client.
124      */
125     public static final AMQConstant COMMAND_INVALID = new AMQConstant(503"command invalid"true);
126 
127     /**
128      * The client attempted to work with a channel that had not been correctly opened. This most likely indicates a
129      * fault in the client layer.
130      */
131     public static final AMQConstant CHANNEL_ERROR = new AMQConstant(504"channel error"true);
132 
133     /**
134      * The server could not complete the method because it lacked sufficient resources. This may be due to the client
135      * creating too many of some type of entity.
136      */
137     public static final AMQConstant RESOURCE_ERROR = new AMQConstant(506"resource error"true);
138 
139     /**
140      * The client tried to work with some entity in a manner that is prohibited by the server, due to security settings
141      * or by some other criteria.
142      */
143     public static final AMQConstant NOT_ALLOWED = new AMQConstant(530"not allowed"true);
144 
145     /** The client tried to use functionality that is not implemented in the server. */
146     public static final AMQConstant NOT_IMPLEMENTED = new AMQConstant(540"not implemented"true);
147 
148     /**
149      * The server could not complete the method because of an internal error. The server may require intervention by
150      * an operator in order to resume normal operations.
151      */
152     public static final AMQConstant INTERNAL_ERROR = new AMQConstant(541"internal error"true);
153 
154     public static final AMQConstant FRAME_MIN_SIZE = new AMQConstant(4096"frame min size"true);
155 
156     /**
157      * The server does not support the protocol version
158      */
159     public static final AMQConstant UNSUPPORTED_BROKER_PROTOCOL_ERROR = new AMQConstant(542"broker unsupported protocol"true);
160     /**
161      * The client imp does not support the protocol version
162      */
163     public static final AMQConstant UNSUPPORTED_CLIENT_PROTOCOL_ERROR = new AMQConstant(543"client unsupported protocol"true);
164 
165     /** The AMQP status code. */
166     private int _code;
167 
168     /** A short description of the status code. */
169     private AMQShortString _name;
170 
171     /**
172      * Creates a new AMQP status code.
173      *
174      @param code The code.
175      @param name A short description of the code.
176      @param map  <tt>true</tt> to register the code as a known code, <tt>false</tt> otherwise.
177      */
178     private AMQConstant(int code, String name, boolean map)
179     {
180         _code = code;
181         _name = new AMQShortString(name);
182         if (map)
183         {
184             _codeMap.put(new Integer(code)this);
185         }
186     }
187 
188     /**
189      * Creates a constant for a status code by looking up the code in the map of known codes. If the code is not known
190      * a constant is still created for it, but it is marked as unknown.
191      *
192      @param code The AMQP status code.
193      *
194      @return The AMQP status code encapsulated as a constant.
195      */
196     public static AMQConstant getConstant(int code)
197     {
198         AMQConstant c = (AMQConstant_codeMap.get(new Integer(code));
199         if (c == null)
200         {
201             c = new AMQConstant(code, "unknown code"false);
202         }
203 
204         return c;
205     }
206 
207     /**
208      * Gets the underlying AMQP status code.
209      *
210      @return The AMQP status code.
211      */
212     public int getCode()
213     {
214         return _code;
215     }
216 
217     /**
218      * Gets a short description of the status code.
219      *
220      @return A short description of the status code.
221      */
222     public AMQShortString getName()
223     {
224         return _name;
225     }
226 
227     /**
228      * Renders the constant as a string, mainly for debugging purposes.
229      *
230      @return The status code and its description.
231      */
232     public String toString()
233     {
234         return _code + ": " + _name;
235     }
236 }