001: /*
002: * Copyright 2007 The Kuali Foundation
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package edu.iu.uis.eden.messaging;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.IOException;
021: import java.io.ObjectInputStream;
022: import java.io.ObjectOutput;
023: import java.io.ObjectOutputStream;
024: import java.io.Serializable;
025: import java.util.List;
026:
027: import javax.xml.namespace.QName;
028:
029: import org.apache.commons.codec.binary.Base64;
030: import org.apache.log4j.Logger;
031: import org.kuali.rice.RiceConstants;
032: import org.kuali.rice.core.Core;
033: import org.kuali.rice.exceptions.RiceRuntimeException;
034:
035: import edu.iu.uis.eden.messaging.resourceloading.KSBResourceLoaderFactory;
036: import edu.iu.uis.eden.messaging.serviceproxies.AsynchronousServiceCallProxy;
037: import edu.iu.uis.eden.messaging.serviceproxies.DelayedAsynchronousServiceCallProxy;
038: import edu.iu.uis.eden.messaging.serviceproxies.SynchronousServiceCallProxy;
039:
040: public class MessageHelperImpl implements MessageHelper {
041:
042: private static final Logger LOG = Logger
043: .getLogger(MessageHelperImpl.class);
044:
045: public String serializeObject(Serializable object) {
046: ByteArrayOutputStream bos = new ByteArrayOutputStream();
047: ObjectOutput out = null;
048: try {
049: out = new ObjectOutputStream(bos);
050: out.writeObject(object);
051: } catch (IOException e) {
052: throw new RiceRuntimeException(e);
053: } finally {
054: try {
055: out.close();
056: } catch (IOException e) {
057: LOG.error("Failed to close ObjectOutputStream", e);
058: }
059: }
060: byte[] buf = bos.toByteArray();
061: Base64 b64 = new Base64();
062: byte[] encodedObj = b64.encode(buf);
063: return new String(encodedObj);
064: }
065:
066: public Object deserializeObject(String serializedObject) {
067: if (serializedObject == null) {
068: return serializedObject;
069: }
070: Base64 b64 = new Base64();
071: byte[] result = b64.decode(serializedObject.getBytes());
072: Object payload = null;
073: ObjectInputStream ois = null;
074: try {
075: ois = new ObjectInputStream(
076: new ByteArrayInputStream(result));
077: payload = ois.readObject();
078: } catch (Exception e) {
079: // may want to move this loggging up
080: LOG.error("Caught Error de-serializing message payload", e);
081: // throw new RiceRuntimeException(e);
082: } finally {
083: try {
084: ois.close();
085: } catch (IOException e) {
086: LOG.error("Failed to close de-serialization stream", e);
087: }
088: }
089: return payload;
090: }
091:
092: public Object getServiceAsynchronously(QName qname) {
093: return getAsynchronousServiceCallProxy(qname, null, null, null,
094: null);
095: }
096:
097: public Object getServiceAsynchronously(QName qname,
098: AsynchronousCallback callback) {
099: return getAsynchronousServiceCallProxy(qname, callback, null,
100: null, null);
101: }
102:
103: public Object getServiceAsynchronously(QName qname,
104: AsynchronousCallback callback, Serializable context) {
105: return getAsynchronousServiceCallProxy(qname, callback,
106: context, null, null);
107: }
108:
109: public Object getServiceAsynchronously(QName qname,
110: AsynchronousCallback callback, Serializable context,
111: String value1, String value2) {
112: return getAsynchronousServiceCallProxy(qname, callback,
113: context, value1, value2);
114: }
115:
116: public Object getServiceAsynchronously(QName qname,
117: Serializable context, String value1, String value2,
118: long delayMilliseconds) {
119: return getDelayedAsynchronousServiceCallProxy(qname, context,
120: value1, value2, delayMilliseconds);
121: }
122:
123: public Object getAsynchronousServiceCallProxy(QName qname,
124: AsynchronousCallback callback, Serializable context,
125: String value1, String value2) {
126: List<RemotedServiceHolder> servicesToProxy = KSBResourceLoaderFactory
127: .getRemoteResourceLocator().getAllServices(qname);
128: if (RiceConstants.MESSAGING_SYNCHRONOUS.equals(Core
129: .getCurrentContextConfig().getProperty(
130: RiceConstants.MESSAGE_DELIVERY))) {
131: return SynchronousServiceCallProxy.createInstance(
132: servicesToProxy, callback, context, value1, value2);
133: }
134:
135: return AsynchronousServiceCallProxy.createInstance(
136: servicesToProxy, callback, context, value1, value2);
137: }
138:
139: public Object getDelayedAsynchronousServiceCallProxy(QName qname,
140: Serializable context, String value1, String value2,
141: long delayMilliseconds) {
142: List<RemotedServiceHolder> servicesToProxy = KSBResourceLoaderFactory
143: .getRemoteResourceLocator().getAllServices(qname);
144: if (RiceConstants.MESSAGING_SYNCHRONOUS.equals(Core
145: .getCurrentContextConfig().getProperty(
146: RiceConstants.MESSAGE_DELIVERY))) {
147: LOG
148: .warn("Executing a delayed service call for "
149: + qname
150: + " with delay of "
151: + delayMilliseconds
152: + " in synchronous mode. Service will be invoked immediately.");
153: return SynchronousServiceCallProxy.createInstance(
154: servicesToProxy, null, context, value1, value2);
155: }
156: return DelayedAsynchronousServiceCallProxy.createInstance(
157: servicesToProxy, context, value1, value2,
158: delayMilliseconds);
159: }
160:
161: }
|