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