KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > engine > pojo > Pojo


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: Pojo.java 676 2006-06-27 15:44:03Z alouis $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.engine.pojo;
23
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27
28 import javax.jbi.component.ComponentContext;
29 import javax.jbi.messaging.DeliveryChannel;
30 import javax.jbi.messaging.MessageExchange;
31
32 import org.objectweb.petals.component.common.HandlingException;
33 import org.objectweb.petals.component.common.PEtALSComponentSDKException;
34
35 public class Pojo {
36
37     private Object JavaDoc object;
38
39     private PojoSE pojoSE;
40
41     private Method JavaDoc onExchangeMethod;
42
43     public Pojo(Object JavaDoc object, PojoSE pojoSE) {
44         this.object = object;
45         this.pojoSE = pojoSE;
46     }
47
48     public void init() throws PEtALSComponentSDKException {
49         if (object == null) {
50             throw new PEtALSComponentSDKException("POJO is null.");
51         }
52         setLoggerOnPojo();
53         setContextOnPojo();
54         setChannelOnPojo();
55
56         call("init");
57
58         setupOnExchangeMethod();
59     }
60
61     public void start() throws PEtALSComponentSDKException {
62         if (object == null) {
63             throw new PEtALSComponentSDKException("POJO is null.");
64         }
65         call("start");
66     }
67
68     public void stop() throws PEtALSComponentSDKException {
69         if (object == null) {
70             throw new PEtALSComponentSDKException("POJO is null.");
71         }
72         call("stop");
73     }
74
75     /**
76      * Call the messageExchange handler method of the POJO
77      * @param ex
78      * @return true if the OUT message has been set on the exchange and has to be send
79      * @throws HandlingException
80      */

81     public boolean callOnExchangeMethod(MessageExchange ex)
82     throws HandlingException {
83
84         boolean result = false;
85         try {
86             Object JavaDoc o = onExchangeMethod.invoke(object, ex);
87
88             result = (Boolean JavaDoc) o;
89
90         } catch (IllegalArgumentException JavaDoc e) {
91             throw new HandlingException(
92                 "java reflection exception during call on "
93                 + onExchangeMethod.getName() + "() on the POJO.", e);
94         } catch (IllegalAccessException JavaDoc e) {
95             throw new HandlingException(
96                 "java reflection exception during call on "
97                 + onExchangeMethod.getName() + "() on the POJO.", e);
98         } catch (InvocationTargetException JavaDoc e) {
99             /* An exception is thrown during the processing of the POJO class.
100              * This exception will be convert in a fault
101              */

102             throw new HandlingException(
103                 "processing exception during call on "
104                 + onExchangeMethod.getName() + "() on the POJO.", e);
105         }catch (Throwable JavaDoc e) {
106             throw new HandlingException(e.getMessage(), e);
107         }
108         return result;
109     }
110
111     /**
112      * Find a "set...(ComponentContext context)" on the pojo and set the
113      * ComponentContext on it.
114      * The method is not mandatory.
115      * @throws PEtALSComponentSDKException
116      * invocation failed
117      */

118     protected void setContextOnPojo() throws PEtALSComponentSDKException {
119
120         Method JavaDoc setContextMethod = findMethod(object.getClass(), "set",
121             ComponentContext.class);
122
123         if (setContextMethod != null) {
124             try {
125                 setContextMethod.invoke(object, pojoSE.getContext());
126
127             } catch (Throwable JavaDoc e) {
128                 throw new PEtALSComponentSDKException(
129                     "Can not set the ComponentContext on the POJO.", e);
130             }
131         }
132     }
133
134     /**
135      * Find a "set...(DeliveryChannel channel)" on the pojo and set the
136      * DeliveryChannel on it.
137      * The method is not mandatory.
138      * @throws PEtALSComponentSDKException
139      * invocation failed
140      */

141     protected void setChannelOnPojo() throws PEtALSComponentSDKException {
142
143         Method JavaDoc setChannelMethod = findMethod(object.getClass(), "set",
144             DeliveryChannel.class);
145
146         if (setChannelMethod != null) {
147             try {
148                 setChannelMethod.invoke(object, pojoSE.getChannel());
149
150             } catch (Throwable JavaDoc e) {
151                 throw new PEtALSComponentSDKException(
152                     "Can not set the DeliveryChannel on the POJO.", e);
153             }
154         }
155     }
156
157     /**
158      * Find a "on...(MessageExchange exchange)" on the pojo to be used when
159      * message exchanges are accepted.
160      *
161      * @throws PEtALSComponentSDKException
162      * method not found or invocation failed
163      */

164     protected void setupOnExchangeMethod() throws PEtALSComponentSDKException {
165
166         onExchangeMethod = findMethod(object.getClass(), "on",
167             MessageExchange.class);
168
169         if (onExchangeMethod == null) {
170             throw new PEtALSComponentSDKException(
171                 "A handler method for MessageExchange is not found in "
172                 + object.getClass());
173
174         }
175         if (onExchangeMethod.getReturnType() != Boolean.TYPE) {
176             throw new PEtALSComponentSDKException(
177             "The handler method for MessageExchange must return a boolean.");
178         }
179
180     }
181
182     /**
183      * Call the method on the pojo. If the method is not found, nothing happens.
184      * The method can not have parameters
185      *
186      * @throws PEtALSComponentSDKException
187      * invocation failed or the method has thrown an exception
188      */

189     protected void call(String JavaDoc methodName) throws PEtALSComponentSDKException {
190
191         Method JavaDoc m = findMethod(object.getClass(), methodName, null);
192
193         if (m != null) {
194             try {
195                 m.invoke(object);
196             } catch (IllegalArgumentException JavaDoc e) {
197                 throw new PEtALSComponentSDKException(
198                     "java reflection exception during call on " + methodName
199                     + "() on the POJO.", e);
200             } catch (IllegalAccessException JavaDoc e) {
201                 throw new PEtALSComponentSDKException(
202                     "java reflection exception during call on " + methodName
203                     + "() on the POJO.", e);
204             } catch (InvocationTargetException JavaDoc e) {
205                 throw new PEtALSComponentSDKException(
206                     "java reflection exception during call on " + methodName
207                     + "() on the POJO.", e);
208             } catch (Throwable JavaDoc e) {
209                 throw new PEtALSComponentSDKException(e.getMessage(), e);
210             }
211
212         }
213     }
214
215     /**
216      * Find a "set...(Logger logger)" on the pojo and set the
217      * logger on it.
218      * The method is not mandatory.
219      * @throws PEtALSComponentSDKException
220      * invocation failed
221      */

222     protected void setLoggerOnPojo() throws PEtALSComponentSDKException {
223
224         Method JavaDoc setLoggerMethod = findMethod(object.getClass(), "set",
225             Logger JavaDoc.class);
226
227         if (setLoggerMethod != null) {
228             try {
229                 setLoggerMethod.invoke(object, pojoSE.getLogger());
230
231             } catch (Throwable JavaDoc e) {
232                 throw new PEtALSComponentSDKException(
233                     "Can not set the Logger on the POJO.", e);
234             }
235         }
236     }
237
238     /**
239      * Find a declared method on the specified class, starting with the specfied
240      * prefix, and having the optionnal specified parameter.
241      *
242      * @param clazz
243      * class on which the method has to be found
244      * @param prefix
245      * prefix of the method name
246      * @param parameterClass
247      * parameter of the class (only one parameter is allow, but is
248      * not mandatory)
249      * @return the matching method, or null if no one match
250      */

251     protected static Method JavaDoc findMethod(Class JavaDoc clazz, String JavaDoc prefix,
252         Class JavaDoc parameterClass) {
253         Method JavaDoc m = null;
254         Method JavaDoc[] ms = clazz.getDeclaredMethods();
255
256         for (int i = 0; i < ms.length && m == null; i++) {
257
258             Method JavaDoc tmpMethod = ms[i];
259
260             if (tmpMethod.getName().startsWith(prefix)) {
261
262                 if (parameterClass != null
263                         && tmpMethod.getParameterTypes().length == 1
264                         && tmpMethod.getParameterTypes()[0].equals(parameterClass)) {
265
266                     m = tmpMethod;
267                 } else if (parameterClass == null
268                         && tmpMethod.getParameterTypes().length == 0) {
269
270                     m = tmpMethod;
271                 }
272             }
273         }
274         return m;
275     }
276 }
277
Popular Tags