KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > dmf > headers > ChangeHeaders


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Coridan.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46
47 /*
48  * Author: Moti Tal
49  */

50
51 /*============================================================================
52   For instructions on how to run this sample please refer to the file
53   sample\jms\files\Readme.txt under the MantaRay installation directory.
54 ============================================================================*/

55
56
57 package sample.dmf.headers;
58
59 import org.mr.api.jms.MantaConnectionFactory;
60 import org.mr.api.jms.MantaMessage;
61 import org.mr.core.protocol.MantaBusMessage;
62 import org.mr.core.util.exceptions.CreationException;
63 import org.mr.kernel.dmf.DMFDeclaration;
64 import org.mr.kernel.dmf.DMFObject;
65 import org.w3c.dom.Element JavaDoc;
66
67 import javax.jms.*;
68 import java.util.Enumeration JavaDoc;
69 import java.util.Map JavaDoc;
70
71
72 public class ChangeHeaders implements DMFDeclaration{
73
74     //header name
75
private final static String JavaDoc ITEAM_PRICE_HEADER_NAME = "item-price";
76     private final static String JavaDoc TASK_HEADER_NAME = "task";
77
78     //////////////////////////////////////////////////////////////////////////////////////////
79
/////////// DMFDeclaration Impl
80
//////////////////////////////////////////////////////////////////////////////////////////
81

82     public String JavaDoc declarationType() {
83         return "ChangeHeaders";
84     }
85
86     public void configure(Element JavaDoc i_element) throws CreationException {
87     }
88
89     public Map JavaDoc execute(Map JavaDoc i_params) {
90
91         //cast the map to the dmf map object
92
DMFObject dmfObject = (DMFObject)i_params;
93
94         try {
95
96             MantaBusMessage busMessage = dmfObject.getMantaBusMessage();
97             //gets the JMS message
98
MantaMessage mantaMessage = (MantaMessage)busMessage.getPayload();
99
100             //If ACK message ignore
101
if(mantaMessage == null)
102                 return i_params;
103
104             String JavaDoc task = mantaMessage.getStringProperty(TASK_HEADER_NAME);
105             mantaMessage.setWriteableState(true);
106             if (task.equals("tax")) {
107                 //get the item price
108
double pbt = mantaMessage.getDoubleProperty(ITEAM_PRICE_HEADER_NAME);
109
110                 //add vat to that value
111
double pat = (pbt*117)/100;
112                 //set the new price to the message headers
113
mantaMessage.setDoubleProperty(ITEAM_PRICE_HEADER_NAME, pat);
114             }else if(task.equals("gift")){
115                 mantaMessage.setDoubleProperty(ITEAM_PRICE_HEADER_NAME,0);
116             }
117             mantaMessage.setWriteableState(false);
118             //set the payload
119
busMessage.setPayload(mantaMessage);
120
121             //set the bus message into the dmf object
122
dmfObject.setMantaBusMessage(busMessage);
123
124         } catch (JMSException e) {
125             e.printStackTrace();
126         }
127         return dmfObject;
128     }
129
130     //////////////////////////////////////////////////////////////////////////////////////////
131
/////////// Subscriber Listener
132
//////////////////////////////////////////////////////////////////////////////////////////
133

134     private static class MantaListener implements MessageListener{
135
136         public void onMessage(Message i_message) {
137             printMessageHeaders("MessageListener", i_message);
138         }
139
140     }
141
142     //////////////////////////////////////////////////////////////////////////////////////////
143
/////////// UTIL
144
//////////////////////////////////////////////////////////////////////////////////////////
145

146     private static void printMessageHeaders(String JavaDoc i_from, Message i_message){
147         try {
148             //gets the message
149
TextMessage message = (TextMessage)i_message;
150             System.out.println("["+i_from+"][MSG Headers]{");
151             Enumeration JavaDoc enumeration = message.getPropertyNames();
152             //print the properties
153
while(enumeration.hasMoreElements()){
154                 String JavaDoc propName = (String JavaDoc)enumeration.nextElement();
155                 String JavaDoc propValue = message.getStringProperty(propName);
156                 System.out.println(" Name=["+propName + "] Value=["+propValue+"] ");
157             }
158             System.out.println("}");
159         } catch (JMSException e) {
160             e.printStackTrace();
161         }
162     }
163     /** Prints the usage. */
164     private static void printHelp() {
165
166         StringBuffer JavaDoc use = new StringBuffer JavaDoc();
167         use.append("help: dmf, change header (options) (amount)\n\n");
168         use.append("options:\n");
169         use.append(" gift Set the value of the header item to 0.\n");
170         use.append(" tax Adds 17% vat for an header item.\n");
171         use.append("amount: any number\n");
172         System.err.println (use);
173     }
174
175
176     //////////////////////////////////////////////////////////////////////////////////////////
177
/////////// MAIN
178
//////////////////////////////////////////////////////////////////////////////////////////
179

180     public static void main(String JavaDoc[] args) {
181
182         //check parameters
183
if(args.length != 2){
184             System.err.println("Wrong number of arguments");
185             printHelp();
186             System.exit(1);
187         }
188         //options
189
String JavaDoc task = args[0];
190         if(!"gift".equals(task) && !"tax".equals(task)){
191             System.err.println("Unknown operation:"+task);
192             printHelp();
193             System.exit(1);
194         }
195         double amount = -1;
196         try {
197             amount = Double.parseDouble(args[1]);
198         } catch (NumberFormatException JavaDoc e) {
199             System.err.println("Unknown amount:"+amount);
200             printHelp();
201             System.exit(1);
202         }
203         try {
204             //creating the connection
205
MantaConnectionFactory mcf = new MantaConnectionFactory();
206             Connection connection = mcf.createConnection();
207             connection.start();
208
209             //create publisher and subscriber
210
Session publiserSession = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
211             Session subscriberSession = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
212
213             Topic topic = publiserSession.createTopic("helloWorld");
214
215             MantaListener mantaListener = new MantaListener();
216             MessageConsumer mc = subscriberSession.createDurableSubscriber(topic, "test");
217             mc.setMessageListener(mantaListener);
218
219             MessageProducer mp = publiserSession.createProducer(topic);
220
221             //set the message to be sent
222
Message message = publiserSession.createTextMessage();
223             message.setStringProperty(TASK_HEADER_NAME, task);
224             message.setDoubleProperty(ITEAM_PRICE_HEADER_NAME, amount);
225
226             //send the message
227
printMessageHeaders("Message To be Sent", message);
228             mp.send(message);
229
230         } catch (JMSException e) {
231             e.printStackTrace();
232         }
233     }
234
235 }
236
Popular Tags