1
19
20 package com.liferay.portal.comm;
21
22 import com.liferay.portal.kernel.log.Log;
23 import com.liferay.portal.kernel.log.LogFactoryUtil;
24 import com.liferay.portal.kernel.util.MethodInvoker;
25 import com.liferay.portal.kernel.util.MethodWrapper;
26 import com.liferay.portal.kernel.util.Validator;
27 import com.liferay.portal.util.PropsKeys;
28 import com.liferay.portal.util.PropsUtil;
29
30 import java.io.Serializable;
31
32 import java.util.LinkedList;
33 import java.util.List;
34
35 import org.jgroups.Channel;
36 import org.jgroups.JChannel;
37 import org.jgroups.Message;
38 import org.jgroups.MessageListener;
39 import org.jgroups.blocks.PullPushAdapter;
40 import org.jgroups.util.Util;
41
42
48 public class CommLink implements MessageListener {
49
50 public static CommLink getInstance() {
51 return _instance;
52 }
53
54 public byte[] getState() {
55 try {
56 return Util.objectToByteBuffer(_history);
57 }
58 catch (Exception e) {
59 return null;
60 }
61 }
62
63 public void setState(byte[] state) {
64 try {
65 _history = (List<Object>)Util.objectFromByteBuffer(state);
66 }
67 catch (Exception e) {
68 _log.error("Error setting state", e);
69 }
70 }
71
72 public void receive(Message msg) {
73 try {
74 Object obj = msg.getObject();
75
76 if (_log.isDebugEnabled()) {
77 _log.debug("Receiving message " + obj);
78 }
79
80 if (obj instanceof MethodWrapper) {
81 MethodWrapper methodWrapper = (MethodWrapper)obj;
82
83 MethodInvoker.invoke(methodWrapper);
84 }
85
86 _history.add(obj);
87 }
88 catch (Exception e) {
89 _log.error("Error receiving message", e);
90 }
91 }
92
93 public void send(Serializable obj) {
94 try {
95 if (_channel == null) {
96 return;
97 }
98
99 Message msg = new Message(null, null, obj);
100
101 _channel.send(msg);
102 }
103 catch (Exception e) {
104 _log.error("Error sending message", e);
105 }
106 }
107
108 private CommLink() {
109 try {
110 String properties = PropsUtil.get(PropsKeys.COMM_LINK_PROPERTIES);
111
112 if (Validator.isNotNull(properties)) {
113 _channel = new JChannel(properties);
114
115 _channel.connect("PortalMessageListener");
116
117 new PullPushAdapter(_channel, this);
118 }
119 }
120 catch (Exception e) {
121 _log.error("Error initializing", e);
122 }
123 }
124
125 private static Log _log = LogFactoryUtil.getLog(CommLink.class);
126
127 private static CommLink _instance = new CommLink();
128
129 private Channel _channel;
130 private List<Object> _history = new LinkedList<Object>();
131
132 }