KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > util > patterns > flow > MessageCondition


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

61 public class MessageCondition extends AbstractCondition{
62
63     protected final static String JavaDoc HEADER = "header";
64     protected final static String JavaDoc PROPERTY = "property";
65     protected final static String JavaDoc JMS_HEADER = "jms-header";
66     protected final static String JavaDoc RECIPIENT = "recipient";
67
68     Method JavaDoc m_method = null;
69
70     public void configure(Element JavaDoc i_object) throws CreationException {
71         super.configure(i_object);
72         try {
73             if(getCondition().equals(JMS_HEADER))
74                 m_method = MantaMessage.class.getMethod("get"+getParam(),null);
75         } catch (NoSuchMethodException JavaDoc e) {
76             e.printStackTrace();
77             throw new CreationException("JMS MESSAGE has no header called: " +getParam(), e);
78         }
79     }
80
81     public boolean metCondition(Object JavaDoc i_object) {
82         MantaBusMessage mantaBusMessage = null;
83         if(i_object instanceof DMFObject)
84             mantaBusMessage = ((DMFObject)i_object).getMantaBusMessage();
85         else
86             mantaBusMessage = (MantaBusMessage)i_object;
87
88         String JavaDoc value = getValue(mantaBusMessage);
89
90        return getRequiresValue().equals(value);
91     }
92
93     protected String JavaDoc getValue(MantaBusMessage mantaBusMessage){
94         MantaMessage mantaMessage = (MantaMessage)mantaBusMessage.getPayload();
95         //TODO: The condition should be resolve at configure(Element)
96
String JavaDoc condition = getCondition().intern();
97         String JavaDoc value = null;
98         if(JMS_HEADER == condition){
99             //TODO: should create a new class for method call
100
try {
101                 value = String.valueOf(m_method.invoke(mantaMessage,null));
102             } catch (IllegalAccessException JavaDoc e) {
103             } catch (InvocationTargetException JavaDoc e) {
104             } catch (Exception JavaDoc e) {
105             }
106         }else if(PROPERTY == condition){
107             try {
108                 value = mantaMessage.getStringProperty(getParam());
109             } catch (JMSException JavaDoc e) {
110                 e.printStackTrace();
111             }
112         }else if(HEADER == condition){
113             value = mantaBusMessage.getHeader(getParam());
114         }else if(RECIPIENT == condition){
115             RecipientAddress address = mantaBusMessage.getRecipient();
116             value = address.getId();
117         }
118         return value;
119     }
120 }
Popular Tags