QpidMethod.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.model;
022 
023 import java.nio.ByteBuffer;
024 import java.util.HashMap;
025 import java.util.LinkedList;
026 import java.util.List;
027 import java.util.Map;
028 
029 import org.apache.qpid.transport.codec.BBDecoder;
030 import org.apache.qpid.transport.codec.Encoder;
031 
032 
033 /**
034  * Qpid method definition.
035  * An entity describing an invocation that can be made on a managed object instance.
036  
037  @author Andrea Gazzarini
038  */
039 public class QpidMethod extends QpidFeature
040 {
041     /** Argument list */
042     List<QpidArgument> arguments = new LinkedList<QpidArgument>();
043     
044     /**
045      * Builds a new qpid method definition with the given name and description.
046      
047      @param name the method name.
048      @param description the method description.
049      */
050     QpidMethod(String name, String description)
051     {
052         this._name = name;
053         this._description = description;
054     }
055     
056     /**
057      * Adds an argument to this method.
058      
059      @param argument the new argument to be added.
060      */
061     void addArgument(QpidArgument argument
062     {
063         arguments.add(argument);
064     }
065     
066     /**
067      * Returns a string representation of this method. 
068      * The result format is <method name>(argType1 argName1 (Direction), argType2 argName2 (Direction), etc...)
069      
070      @return a string representation of this method.
071      */
072     @Override
073     public String toString ()
074     {
075         StringBuilder builder = new StringBuilder()
076             .append(_name)
077             .append('(');
078         
079         for (QpidArgument argument : arguments)
080         {
081             builder.append(argument).append(',');
082         }
083         
084         builder.append(')');
085         return builder.toString();
086     }
087 
088     /**
089      * Encodes the given parameter values according to this method arguments definitions.
090      * Note that only Input/Output and Input parameters are encoded.
091      
092      @param parameters the parameters values.
093      @param encoder the encoder used for encoding.
094      */
095     public void encodeParameters (Object[] parameters, Encoder encoder
096     {
097         int index = 0;
098         for (QpidArgument argument : arguments)
099         {
100             if (argument.getDirection() != Direction.O)
101             {
102                 argument.encode(parameters[index++],encoder);
103             }
104         }
105     }
106 
107     /**
108      * Decodes the given input raw according to this method arguments definitions.
109      * Note that only Input/Output and Output parameters are encoded.
110      
111      @param parameters the parameters values.
112      @param encoder the encoder used for encoding.
113      */
114     public Map<String, Object> decodeParameters (byte [] values
115     {
116         BBDecoder decoder = new BBDecoder();
117         decoder.init(ByteBuffer.wrap(values));
118         Map<String, Object> result = new HashMap<String, Object>();
119         
120         for (QpidArgument argument : arguments)
121         {
122             if (argument.getDirection() != Direction.I)
123             {
124                 result.put(argument.getName(),argument.decode(decoder));
125             }
126         }
127         return result;
128     }    
129     
130     /**
131      * Validates the given array of parameters against the constraint defined on this method's arguments.
132      
133      @param parameters the parameters (values) to be validated.
134      @throws ValidationException when one of the supplied values is violating some constraint.
135      */
136     public void validate (Object[] parametersthrows ValidationException
137     {
138         int index = 0;
139         for (QpidArgument argument : arguments)
140         {
141             if (argument.getDirection() != Direction.O)
142             {
143                 argument.validate(parameters[index++]);
144             }
145         }        
146     }
147 }