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