InvocationResult.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.management.domain.handler.impl;
022 
023 import java.io.Serializable;
024 import java.util.Map;
025 import java.util.Map.Entry;
026 
027 import org.apache.qpid.management.domain.services.MethodInvocationException;
028 
029 /**
030  * Value object used for storing an invocation method result.
031  * This is done in order to accomplish multiple return value requirement. 
032  * As we know, it's not possible to do that only with method signature and therefore this value object / struct is used.
033  
034  @author Andrea Gazzarini
035  */
036 public class InvocationResult implements Serializable
037 {
038     private static final long serialVersionUID = 2062662997326399693L;
039     
040     private final long _returnCode;
041     private final String _statusText;
042     private final byte [] _outputAndBidirectionalArgumentValues;
043     private Map<String, Object> _outputSection;
044     
045     /**
046      * Builds an invocation result with the given status code and status text.
047      
048      @param statusCode the status code.
049      @param statusText the status text.
050      */
051     InvocationResult(long statusCode, String statusText,byte [] outputAndBidirectionalArgumentValues)
052     {
053         this._returnCode = statusCode;
054         this._statusText = statusText;
055         this._outputAndBidirectionalArgumentValues = outputAndBidirectionalArgumentValues;
056     }
057         
058     /**
059      * Checks if this result contains an error return code.
060      *  
061      @return true if this result object contains an error return code.
062      */
063     public boolean isException () 
064     {
065         return _returnCode != 0;
066     }
067     
068     /**
069      * Simply throws a new MethodInvocationException.
070      * Usually this method is called in conjunction with the isException() method in order to raise an exception if 
071      * the wrapped return code means that there was an error.
072      
073      @throws MethodInvocationException always.
074      */
075     public void createAndThrowException() throws MethodInvocationException
076     {
077         throw new MethodInvocationException(_returnCode, _statusText);
078     }
079     
080     @Override
081     public String toString ()
082     {
083         StringBuilder builder = new StringBuilder()
084             .append("Status code : ")
085             .append(_returnCode)
086             .append(",")
087             .append("Status Text : ")
088             .append(_statusText);
089         if (_outputSection != null && !_outputSection.isEmpty())
090         {
091             builder.append(". Parameters : ");
092             for (Entry<String, Object> outputEntry : _outputSection.entrySet())
093             {
094                 builder.append(outputEntry.getKey()).append('=').append(outputEntry.getValue());
095                 builder.append(',');
096             }
097         }            
098         return builder.toString();
099     }
100 
101     /**
102      * Returns the return code of this invocation result.
103      
104      @return the return code of this invocation result.
105      */
106     public long getReturnCode ()
107     {
108         return _returnCode;
109     }
110 
111     /**
112      * Contains the status text of this invocation result.
113      
114      @return the status text of this invocation result.
115      */
116     public String getStatusText ()
117     {
118         return _statusText;
119     }
120     
121     /**
122      * Returns the output and bidirectional argument values in raw format (byte [])
123      
124      @return the output and bidirectional argument values in raw format (byte [])
125      */
126     public byte [] getOutputAndBidirectionalArgumentValues()
127     {
128         return _outputAndBidirectionalArgumentValues;
129     }
130 
131     /**
132      * Sets the output section (decoded) of this invocation result.
133      * When an incoming message arrives, the output section (output and bidirectional argument values) are 
134      * initially stored in raw format.
135      * After that, their values need to be converted. 
136      * The final result is a map containing (for each Output or Input/Output parameter) the name of the argument as key 
137      * and its value as value. 
138      
139      @param output a map containing outptu and bidirectional values (not in schema order). 
140      */
141     public void setOutputSection (Map<String, Object> outputSection)
142     {
143         this._outputSection = outputSection;
144     }
145     
146     /**
147      * Returns the output section of this invocation result. 
148      * The output section consists in output and bidirectional argument values.
149      * Note that the order of the arguments is not guaranteed.
150      
151      @param outputSection the output section of this invocation result;
152      */
153     public Map<String, Object> getOutputSection ()
154     {
155         return _outputSection;
156     }    
157 }