001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.comm;
022:
023: import com.liferay.portal.kernel.util.MethodInvoker;
024: import com.liferay.portal.kernel.util.MethodWrapper;
025: import com.liferay.portal.kernel.util.Validator;
026: import com.liferay.portal.util.PropsUtil;
027:
028: import java.io.Serializable;
029:
030: import java.util.LinkedList;
031: import java.util.List;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035:
036: import org.jgroups.Channel;
037: import org.jgroups.JChannel;
038: import org.jgroups.Message;
039: import org.jgroups.MessageListener;
040: import org.jgroups.blocks.PullPushAdapter;
041: import org.jgroups.util.Util;
042:
043: /**
044: * <a href="CommLink.java.html"><b><i>View Source</i></b></a>
045: *
046: * @author Brian Wing Shun Chan
047: *
048: */
049: public class CommLink implements MessageListener {
050:
051: public static CommLink getInstance() {
052: return _instance;
053: }
054:
055: public byte[] getState() {
056: try {
057: return Util.objectToByteBuffer(_history);
058: } catch (Exception e) {
059: return null;
060: }
061: }
062:
063: public void setState(byte[] state) {
064: try {
065: _history = (List) Util.objectFromByteBuffer(state);
066: } catch (Exception e) {
067: _log.error("Error setting state", e);
068: }
069: }
070:
071: public void receive(Message msg) {
072: try {
073: Object obj = msg.getObject();
074:
075: if (_log.isDebugEnabled()) {
076: _log.debug("Receiving message " + obj);
077: }
078:
079: if (obj instanceof MethodWrapper) {
080: MethodWrapper methodWrapper = (MethodWrapper) obj;
081:
082: MethodInvoker.invoke(methodWrapper);
083: }
084:
085: _history.add(obj);
086: } catch (Exception e) {
087: _log.error("Error receiving message", e);
088: }
089: }
090:
091: public void send(Serializable obj) {
092: try {
093: if (_channel == null) {
094: return;
095: }
096:
097: Message msg = new Message(null, null, obj);
098:
099: _channel.send(msg);
100: } catch (Exception e) {
101: _log.error("Error sending message", e);
102: }
103: }
104:
105: private CommLink() {
106: try {
107: String properties = PropsUtil
108: .get(PropsUtil.COMM_LINK_PROPERTIES);
109:
110: if (Validator.isNotNull(properties)) {
111: _channel = new JChannel(properties);
112:
113: _channel.connect("PortalMessageListener");
114:
115: new PullPushAdapter(_channel, this );
116: }
117: } catch (Exception e) {
118: _log.error("Error initializing", e);
119: }
120: }
121:
122: private static Log _log = LogFactory.getLog(CommLink.class);
123:
124: private static CommLink _instance = new CommLink();
125:
126: private Channel _channel;
127: private List _history = new LinkedList();
128:
129: }
|