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