MethodInvocationRequestMessage.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.messages;
022 
023 import org.apache.qpid.management.Messages;
024 import org.apache.qpid.management.Names;
025 import org.apache.qpid.management.Protocol;
026 import org.apache.qpid.management.configuration.Configuration;
027 import org.apache.qpid.management.domain.model.QpidMethod;
028 import org.apache.qpid.management.domain.model.type.Binary;
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.ReplyTo;
033 import org.apache.qpid.transport.util.Logger;
034 
035 /**
036  * Abstract representation of a method invocation request message.
037  * Concrete subclasses must supply the values needed to build & encode the message.
038  
039  @author Andrea Gazzarini
040  */
041 public abstract class MethodInvocationRequestMessage extends ManagementMessage
042 {
043   private final static Logger LOGGER = Logger.get(MethodInvocationRequestMessage.class);
044   
045   private DeliveryProperties _deliveryProperties;
046   private MessageProperties _messageProperties;
047   private Header _header;  
048   
049   /**
050    * Builds a new method invocation request message with the given target identifiers.
051    
052    @param bankId the bank identifier.
053    @param brokerId the broker identifier.
054    */
055   public MethodInvocationRequestMessage(long bankId, long brokerId
056   {
057      ReplyTo replyTo=new ReplyTo();
058        replyTo.setRoutingKey(Configuration.getInstance().getMethodReplyQueueName());
059        _messageProperties = new MessageProperties();
060        _messageProperties.setReplyTo(replyTo);
061 
062        String routingKey = String.format(Names.AGENT_ROUTING_KEY_PREFIX+"%s.%s", brokerId,bankId);
063        
064        LOGGER.debug(Messages.QMAN_200032_COMMAND_MESSAGE_ROUTING_KEY, routingKey);
065        
066        _deliveryProperties = new DeliveryProperties();       
067        _deliveryProperties.setRoutingKey(routingKey);        
068        _header = new Header(_deliveryProperties, _messageProperties);
069   }
070   
071     @Override
072     char opcode ()
073     {
074         return Protocol.OPERATION_INVOCATION_REQUEST_OPCODE;
075     }
076 
077     /**
078      * Returns the package name.
079      
080      @return the package name.
081      */
082     protected abstract String packageName();
083     
084     /**
085      * Returns the class name.
086      
087      @return the class name.
088      */
089     protected abstract String className();
090     
091     /**
092      * Returns the schema hash.
093      
094      @return the schema hash.
095      */
096     protected abstract Binary schemaHash();
097     
098     /**
099      * Returns the object identifier.
100      
101      @return the object identifier.
102      */
103     protected abstract Binary objectId();
104     
105     /**
106      * Returns the method to be invoked.
107      
108      @return the method to be invoked.
109      */
110     protected abstract QpidMethod method();
111     
112     /**
113      * Returns the parameters used for method invocation.
114      
115      @return the parameters used for method invocation.
116      */
117     protected abstract Object[] parameters();
118 
119     /**
120      * Returns the delivery properties of this message.
121      
122      @return the delivery properties of this message.
123      */
124     public DeliveryProperties getDeliveryProperties ()
125     {
126         return _deliveryProperties;
127     }
128 
129     /**
130      * Returns the header of this message.
131      
132      @return the header of this message.
133      */
134     public Header getHeader ()
135     {
136         return _header;
137     }
138 
139     /**
140      * Returns the messages header properties of this message.
141      
142      @return the message header properties of this message.
143      */
144     public MessageProperties getMessageProperties ()
145     {
146         return _messageProperties;
147     }
148     
149     @Override
150     void specificMessageEncoding ()
151     {
152         objectId().encode(_codec);
153         _codec.writeStr8(packageName());
154         _codec.writeStr8(className());        
155         schemaHash().encode(_codec);
156         
157         QpidMethod method = method();
158         _codec.writeStr8(method.getName());
159        method.encodeParameters(parameters(), _codec);
160     }
161 }