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