KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > event > JavaGroupsBridge


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.event;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.Collection JavaDoc;
24
25 import org.jgroups.Channel;
26 import org.jgroups.JChannel;
27 import org.jgroups.Message;
28 import org.jgroups.MessageListener;
29 import org.jgroups.blocks.PullPushAdapter;
30
31 /**
32  * Implementation of EventBridge that passes and receives events via JavaGroups
33  * communication software.
34  *
35  * @author Andrus Adamchik
36  * @since 1.1
37  */

38 public class JavaGroupsBridge extends EventBridge implements MessageListener {
39
40     // TODO: Meaning of "state" in JGroups is not yet clear to me
41
protected byte[] state;
42
43     protected Channel channel;
44     protected PullPushAdapter adapter;
45     protected String JavaDoc multicastAddress;
46     protected String JavaDoc multicastPort;
47     protected String JavaDoc configURL;
48
49     /**
50      * Creates new instance of JavaGroupsBridge.
51      */

52     public JavaGroupsBridge(EventSubject localSubject, String JavaDoc externalSubject) {
53         super(localSubject, externalSubject);
54     }
55
56     /**
57      * @since 1.2
58      */

59     public JavaGroupsBridge(Collection JavaDoc localSubjects, String JavaDoc externalSubject) {
60         super(localSubjects, externalSubject);
61     }
62
63     public String JavaDoc getConfigURL() {
64         return configURL;
65     }
66
67     public void setConfigURL(String JavaDoc configURL) {
68         this.configURL = configURL;
69     }
70
71     public String JavaDoc getMulticastAddress() {
72         return multicastAddress;
73     }
74
75     public void setMulticastAddress(String JavaDoc multicastAddress) {
76         this.multicastAddress = multicastAddress;
77     }
78
79     public String JavaDoc getMulticastPort() {
80         return multicastPort;
81     }
82
83     public void setMulticastPort(String JavaDoc multicastPort) {
84         this.multicastPort = multicastPort;
85     }
86
87     public byte[] getState() {
88         return state;
89     }
90
91     public void setState(byte[] state) {
92         this.state = state;
93     }
94
95     /**
96      * Implementation of org.javagroups.MessageListener - a callback method to process
97      * incoming messages.
98      */

99     public void receive(Message message) {
100         try {
101             CayenneEvent event = messageObjectToEvent((Serializable JavaDoc) message.getObject());
102             if (event != null) {
103
104                 onExternalEvent(event);
105             }
106         }
107         catch (Exception JavaDoc ex) {
108             // TODO: Andrus, 2/8/2006 logging... Log4J was removed to make this usable on
109
// the client
110
}
111     }
112
113     protected void startupExternal() throws Exception JavaDoc {
114         // TODO: need to do more research to figure out the best default transport
115
// settings
116
// to avoid fragmentation, etc.
117

118         // if config file is set, use it, otherwise use a default
119
// set of properties, trying to configure multicast address and port
120
if (configURL != null) {
121             channel = new JChannel(configURL);
122         }
123         else {
124             String JavaDoc configString = buildConfigString();
125             channel = new JChannel(configString);
126         }
127
128         // Important - discard messages from self
129
channel.setOpt(Channel.LOCAL, Boolean.FALSE);
130         channel.connect(externalSubject);
131
132         if (receivesExternalEvents()) {
133             adapter = new PullPushAdapter(channel, this);
134         }
135     }
136
137     /**
138      * Creates JavaGroups configuration String, using preconfigured multicast port and
139      * address.
140      */

141     protected String JavaDoc buildConfigString() {
142         if (multicastAddress == null) {
143             throw new IllegalStateException JavaDoc("'multcastAddress' is not set");
144         }
145
146         if (multicastPort == null) {
147             throw new IllegalStateException JavaDoc("'multcastPort' is not set");
148         }
149
150         return "UDP(mcast_addr="
151                 + multicastAddress
152                 + ";mcast_port="
153                 + multicastPort
154                 + ";ip_ttl=32):"
155                 + "PING(timeout=3000;num_initial_members=6):"
156                 + "FD(timeout=3000):"
157                 + "VERIFY_SUSPECT(timeout=1500):"
158                 + "pbcast.NAKACK(gc_lag=10;retransmit_timeout=600,1200,2400,4800):"
159                 + "pbcast.STABLE(desired_avg_gossip=10000):"
160                 + "FRAG:"
161                 + "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;"
162                 + "shun=true;print_local_addr=false)";
163     }
164
165     protected void shutdownExternal() throws Exception JavaDoc {
166         try {
167             if (adapter != null) {
168                 adapter.stop();
169             }
170
171             channel.close();
172         }
173         finally {
174             adapter = null;
175             channel = null;
176         }
177     }
178
179     protected void sendExternalEvent(CayenneEvent localEvent) throws Exception JavaDoc {
180         Message message = new Message(null, null, eventToMessageObject(localEvent));
181         channel.send(message);
182     }
183
184     /**
185      * Converts CayenneEvent to a serializable object that will be sent via JMS. Default
186      * implementation simply returns the event, but subclasses can customize this
187      * behavior.
188      */

189     protected Serializable JavaDoc eventToMessageObject(CayenneEvent event) throws Exception JavaDoc {
190         return event;
191     }
192
193     /**
194      * Converts a Serializable instance to CayenneEvent. Returns null if the object is not
195      * supported. Default implementation simply tries to cast the object to CayenneEvent,
196      * but subclasses can customize this behavior.
197      */

198     protected CayenneEvent messageObjectToEvent(Serializable JavaDoc object) throws Exception JavaDoc {
199         return (object instanceof CayenneEvent) ? (CayenneEvent) object : null;
200     }
201 }
202
Popular Tags