| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.ksb.messaging; |
| 18 | |
|
| 19 | |
import org.apache.commons.codec.binary.Base64; |
| 20 | |
import org.apache.log4j.Logger; |
| 21 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
| 22 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
| 23 | |
import org.kuali.rice.ksb.messaging.resourceloader.KSBResourceLoaderFactory; |
| 24 | |
import org.kuali.rice.ksb.messaging.serviceproxies.AsynchronousServiceCallProxy; |
| 25 | |
import org.kuali.rice.ksb.messaging.serviceproxies.DelayedAsynchronousServiceCallProxy; |
| 26 | |
import org.kuali.rice.ksb.messaging.serviceproxies.SynchronousServiceCallProxy; |
| 27 | |
import org.kuali.rice.ksb.util.KSBConstants; |
| 28 | |
|
| 29 | |
import javax.xml.namespace.QName; |
| 30 | |
import java.io.ByteArrayInputStream; |
| 31 | |
import java.io.ByteArrayOutputStream; |
| 32 | |
import java.io.IOException; |
| 33 | |
import java.io.ObjectInputStream; |
| 34 | |
import java.io.ObjectOutput; |
| 35 | |
import java.io.ObjectOutputStream; |
| 36 | |
import java.io.Serializable; |
| 37 | |
import java.util.List; |
| 38 | |
|
| 39 | |
|
| 40 | 0 | public class MessageHelperImpl implements MessageHelper { |
| 41 | |
|
| 42 | 0 | private static final Logger LOG = Logger.getLogger(MessageHelperImpl.class); |
| 43 | |
|
| 44 | |
public String serializeObject(Serializable object) { |
| 45 | 0 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 46 | 0 | ObjectOutput out = null; |
| 47 | |
try { |
| 48 | 0 | out = new ObjectOutputStream(bos); |
| 49 | 0 | out.writeObject(object); |
| 50 | 0 | } catch (IOException e) { |
| 51 | 0 | throw new RiceRuntimeException(e); |
| 52 | |
} finally { |
| 53 | 0 | try { |
| 54 | 0 | out.close(); |
| 55 | 0 | } catch (IOException e) { |
| 56 | 0 | LOG.error("Failed to close ObjectOutputStream", e); |
| 57 | 0 | } |
| 58 | 0 | } |
| 59 | 0 | byte[] buf = bos.toByteArray(); |
| 60 | 0 | Base64 b64 = new Base64(); |
| 61 | 0 | byte[] encodedObj = b64.encode(buf); |
| 62 | 0 | return new String(encodedObj); |
| 63 | |
} |
| 64 | |
|
| 65 | |
public Object deserializeObject(String serializedObject) { |
| 66 | 0 | if (serializedObject == null) { |
| 67 | 0 | return serializedObject; |
| 68 | |
} |
| 69 | 0 | Base64 b64 = new Base64(); |
| 70 | 0 | byte[] result = b64.decode(serializedObject.getBytes()); |
| 71 | 0 | Object payload = null; |
| 72 | 0 | ObjectInputStream ois = null; |
| 73 | |
try { |
| 74 | 0 | ois = new ObjectInputStream(new ByteArrayInputStream(result)); |
| 75 | 0 | payload = ois.readObject(); |
| 76 | 0 | } catch (Exception e) { |
| 77 | |
|
| 78 | 0 | LOG.error("Caught Error de-serializing message payload", e); |
| 79 | |
|
| 80 | |
} finally { |
| 81 | 0 | try { |
| 82 | 0 | ois.close(); |
| 83 | 0 | } catch (IOException e) { |
| 84 | 0 | LOG.error("Failed to close de-serialization stream", e); |
| 85 | 0 | } |
| 86 | 0 | } |
| 87 | 0 | return payload; |
| 88 | |
} |
| 89 | |
|
| 90 | |
public Object getServiceAsynchronously(QName qname) { |
| 91 | 0 | return getAsynchronousServiceCallProxy(qname, null, null, null, null); |
| 92 | |
} |
| 93 | |
|
| 94 | |
public Object getServiceAsynchronously(QName qname, AsynchronousCallback callback) { |
| 95 | 0 | return getAsynchronousServiceCallProxy(qname, callback, null, null, null); |
| 96 | |
} |
| 97 | |
|
| 98 | |
public Object getServiceAsynchronously(QName qname, AsynchronousCallback callback, Serializable context) { |
| 99 | 0 | return getAsynchronousServiceCallProxy(qname, callback, context, null, null); |
| 100 | |
} |
| 101 | |
|
| 102 | |
public Object getServiceAsynchronously(QName qname, AsynchronousCallback callback, Serializable context, String value1, String value2) { |
| 103 | 0 | return getAsynchronousServiceCallProxy(qname, callback, context, value1, value2); |
| 104 | |
} |
| 105 | |
|
| 106 | |
public Object getAsynchronousServiceCallProxy(QName qname, AsynchronousCallback callback, Serializable context, String value1, String value2) { |
| 107 | |
|
| 108 | 0 | List<RemotedServiceHolder> servicesToProxy = KSBResourceLoaderFactory.getRemoteResourceLocator().getAllServices(qname); |
| 109 | 0 | if (KSBConstants.MESSAGING_SYNCHRONOUS.equals(ConfigContext.getCurrentContextConfig().getProperty(KSBConstants.Config.MESSAGE_DELIVERY))) { |
| 110 | 0 | return SynchronousServiceCallProxy.createInstance(servicesToProxy, callback, context, value1, value2); |
| 111 | |
} |
| 112 | |
|
| 113 | 0 | return AsynchronousServiceCallProxy.createInstance(servicesToProxy, callback, context, value1, value2); |
| 114 | |
|
| 115 | |
} |
| 116 | |
|
| 117 | |
public Object getDelayedAsynchronousServiceCallProxy(QName qname, Serializable context, String value1, String value2, long delayMilliseconds) { |
| 118 | 0 | List<RemotedServiceHolder> servicesToProxy = KSBResourceLoaderFactory.getRemoteResourceLocator().getAllServices(qname); |
| 119 | 0 | if (KSBConstants.MESSAGING_SYNCHRONOUS.equals(ConfigContext.getCurrentContextConfig().getProperty(KSBConstants.Config.MESSAGE_DELIVERY))) { |
| 120 | 0 | LOG.warn("Executing a delayed service call for " + qname + " with delay of " + delayMilliseconds + " in synchronous mode. Service will be invoked immediately."); |
| 121 | 0 | return SynchronousServiceCallProxy.createInstance(servicesToProxy, null, context, value1, value2); |
| 122 | |
} |
| 123 | 0 | return DelayedAsynchronousServiceCallProxy.createInstance(servicesToProxy, context, value1, value2, delayMilliseconds); |
| 124 | |
} |
| 125 | |
|
| 126 | |
public Object getServiceAsynchronously(QName qname, Serializable context, String value1, String value2, long delayMilliseconds) { |
| 127 | 0 | return getDelayedAsynchronousServiceCallProxy(qname, context, value1, value2, delayMilliseconds); |
| 128 | |
} |
| 129 | |
|
| 130 | |
} |