Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* PETALS - PETALS Services Platform.
* Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* -------------------------------------------------------------------------
* $Id: Pojo.java 676 2006-06-27 15:44:03Z alouis $
* -------------------------------------------------------------------------
*/
package org.objectweb.petals.engine.pojo;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Logger;
import javax.jbi.component.ComponentContext;
import javax.jbi.messaging.DeliveryChannel;
import javax.jbi.messaging.MessageExchange;
import org.objectweb.petals.component.common.HandlingException;
import org.objectweb.petals.component.common.PEtALSComponentSDKException;
public class Pojo {
private Object object;
private PojoSE pojoSE;
private Method onExchangeMethod;
private ClassLoader classLoader;
public Pojo(Object object, PojoSE pojoSE, ClassLoader classLoader) {
this.object = object;
this.pojoSE = pojoSE;
this.classLoader = classLoader;
}
public void init() throws PEtALSComponentSDKException {
if (object == null) {
throw new PEtALSComponentSDKException("POJO is null.");
}
setLoggerOnPojo();
setContextOnPojo();
setChannelOnPojo();
call("init");
setupOnExchangeMethod();
}
public void start() throws PEtALSComponentSDKException {
if (object == null) {
throw new PEtALSComponentSDKException("POJO is null.");
}
call("start");
}
public void stop() throws PEtALSComponentSDKException {
if (object == null) {
throw new PEtALSComponentSDKException("POJO is null.");
}
call("stop");
}
/**
* Call the messageExchange handler method of the POJO
* @param ex
* @return true if the OUT message has been set on the exchange and has to be send
* @throws HandlingException
*/
public boolean callOnExchangeMethod(MessageExchange ex)
throws HandlingException {
// First, we set the SU classloader
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(this.classLoader);
boolean result = false;
try {
Object o = onExchangeMethod.invoke(object, ex);
result = (Boolean) o;
} catch (IllegalArgumentException e) {
throw new HandlingException(
"java reflection exception during call on "
+ onExchangeMethod.getName() + "() on the POJO.", e);
} catch (IllegalAccessException e) {
throw new HandlingException(
"java reflection exception during call on "
+ onExchangeMethod.getName() + "() on the POJO.", e);
} catch (InvocationTargetException e) {
/* An exception is thrown during the processing of the POJO class.
* This exception will be convert in a fault
*/
throw new HandlingException(
"processing exception during call on "
+ onExchangeMethod.getName() + "() on the POJO.", e);
} catch (Throwable e) {
throw new HandlingException(e.getMessage(), e);
} finally {
// We restore the thread classloader
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
return result;
}
/**
* Find a "set...(ComponentContext context)" on the pojo and set the
* ComponentContext on it.
* The method is not mandatory.
* @throws PEtALSComponentSDKException
* invocation failed
*/
protected void setContextOnPojo() throws PEtALSComponentSDKException {
// First, we set the SU classloader
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(this.classLoader);
try {
Method setContextMethod = findMethod(object.getClass(), "set",
ComponentContext.class);
if (setContextMethod != null) {
try {
setContextMethod.invoke(object, pojoSE.getContext());
} catch (Throwable e) {
throw new PEtALSComponentSDKException(
"Can not set the ComponentContext on the POJO.", e);
}
}
} finally {
// We restore the thread classloader
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
}
/**
* Find a "set...(DeliveryChannel channel)" on the pojo and set the
* DeliveryChannel on it.
* The method is not mandatory.
* @throws PEtALSComponentSDKException
* invocation failed
*/
protected void setChannelOnPojo() throws PEtALSComponentSDKException {
// First, we set the SU classloader
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(this.classLoader);
try {
Method setChannelMethod = findMethod(object.getClass(), "set",
DeliveryChannel.class);
if (setChannelMethod != null) {
try {
setChannelMethod.invoke(object, pojoSE.getChannel());
} catch (Throwable e) {
throw new PEtALSComponentSDKException(
"Can not set the DeliveryChannel on the POJO.", e);
}
}
} finally {
// We restore the thread classloader
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
}
/**
* Find a "on...(MessageExchange exchange)" on the pojo to be used when
* message exchanges are accepted.
*
* @throws PEtALSComponentSDKException
* method not found or invocation failed
*/
protected void setupOnExchangeMethod() throws PEtALSComponentSDKException {
onExchangeMethod = findMethod(object.getClass(), "on",
MessageExchange.class);
if (onExchangeMethod == null) {
throw new PEtALSComponentSDKException(
"A handler method for MessageExchange is not found in "
+ object.getClass());
}
if (onExchangeMethod.getReturnType() != Boolean.TYPE) {
throw new PEtALSComponentSDKException(
"The handler method for MessageExchange must return a boolean.");
}
}
/**
* Call the method on the pojo. If the method is not found, nothing happens.
* The method can not have parameters
*
* @throws PEtALSComponentSDKException
* invocation failed or the method has thrown an exception
*/
protected void call(String methodName) throws PEtALSComponentSDKException {
// First, we set the SU classloader
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(this.classLoader);
try {
Method m = findMethod(object.getClass(), methodName, null);
if (m != null) {
try {
m.invoke(object);
} catch (IllegalArgumentException e) {
throw new PEtALSComponentSDKException(
"java reflection exception during call on " + methodName
+ "() on the POJO.", e);
} catch (IllegalAccessException e) {
throw new PEtALSComponentSDKException(
"java reflection exception during call on " + methodName
+ "() on the POJO.", e);
} catch (InvocationTargetException e) {
// An exception is thrown calling the POJO class method.
throw new PEtALSComponentSDKException(
"processing exception during call on "
+ methodName + "() on the POJO.", e);
} catch (Throwable e) {
throw new PEtALSComponentSDKException(e.getMessage(), e);
}
}
} finally {
// We restore the thread classloader
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
}
/**
* Find a "set...(Logger logger)" on the pojo and set the
* logger on it.
* The method is not mandatory.
* @throws PEtALSComponentSDKException
* invocation failed
*/
protected void setLoggerOnPojo() throws PEtALSComponentSDKException {
// First, we set the SU classloader
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(this.classLoader);
try {
Method setLoggerMethod = findMethod(object.getClass(), "set",
Logger.class);
if (setLoggerMethod != null) {
try {
setLoggerMethod.invoke(object, pojoSE.getLogger());
} catch (Throwable e) {
throw new PEtALSComponentSDKException(
"Can not set the Logger on the POJO.", e);
}
}
} finally {
// We restore the thread classloader
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
}
/**
* Find a method on the specified class (including those
* inherited from superclasses) starting with the specified
* prefix, and having the optionnal specified parameter.
*
* @param clazz
* class on which the method has to be found
* @param prefix
* prefix of the method name
* @param parameterClass
* parameter of the class (only one parameter is allow, but is
* not mandatory)
* @return the matching method, or null if no one match
*/
protected static Method findMethod(Class clazz, String prefix,
Class parameterClass) {
Method m = null;
Method[] ms = clazz.getMethods();
for (int i = 0; i < ms.length && m == null; i++) {
Method tmpMethod = ms[i];
if (tmpMethod.getName().startsWith(prefix)) {
if (parameterClass != null
&& tmpMethod.getParameterTypes().length == 1
&& tmpMethod.getParameterTypes()[0].equals(parameterClass)) {
m = tmpMethod;
} else if (parameterClass == null
&& tmpMethod.getParameterTypes().length == 0) {
m = tmpMethod;
}
}
}
return m;
}
}