KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Constructor JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.cayenne.CayenneRuntimeException;
28 import org.apache.cayenne.reflect.PropertyUtils;
29
30 /**
31  * Factory to create JavaGroupsBridge instances. If JavaGroups library is not installed
32  * this factory will return a noop EventBridge as a failover mechanism.
33  *
34  * @since 1.1
35  * @author Andrus Adamchik
36  */

37 public class JavaGroupsBridgeFactory implements EventBridgeFactory {
38
39     public static final String JavaDoc MCAST_ADDRESS_DEFAULT = "228.0.0.5";
40     public static final String JavaDoc MCAST_PORT_DEFAULT = "22222";
41
42     public static final String JavaDoc MCAST_ADDRESS_PROPERTY = "cayenne.JavaGroupsBridge.mcast.address";
43     public static final String JavaDoc MCAST_PORT_PROPERTY = "cayenne.JavaGroupsBridge.mcast.port";
44
45     /**
46      * Defines a property for JavaGroups XML configuration file. Example file can be found
47      * at <a
48      * HREF="http://www.filip.net/javagroups/javagroups-protocol.xml">http://www.filip.net/javagroups/javagroups-protocol.xml</a>.
49      */

50     public static final String JavaDoc JGROUPS_CONFIG_URL_PROPERTY = "javagroupsbridge.config.url";
51
52     /**
53      * Creates a JavaGroupsBridge instance. Since JavaGroups is not shipped with Cayenne
54      * and should be installed separately, a common misconfiguration problem may be the
55      * absense of JavaGroups jar file. This factory returns a dummy noop EventBridge, if
56      * this is the case. This would allow the application to continue to run, but without
57      * remote notifications.
58      */

59     public EventBridge createEventBridge(
60             Collection JavaDoc localSubjects,
61             String JavaDoc externalSubject,
62             Map JavaDoc properties) {
63
64         try {
65             // sniff JavaGroups presence
66
Class.forName("org.jgroups.Channel");
67             return createJavaGroupsBridge(localSubjects, externalSubject, properties);
68         }
69         catch (Exception JavaDoc ex) {
70             // recover from no JavaGroups
71
return createNoopBridge();
72         }
73     }
74
75     private EventBridge createNoopBridge() {
76         return new NoopEventBridge();
77     }
78
79     private EventBridge createJavaGroupsBridge(
80             Collection JavaDoc localSubjects,
81             String JavaDoc externalSubject,
82             Map JavaDoc properties) {
83
84         // create JavaGroupsBridge using reflection to avoid triggering
85
// ClassNotFound exceptions due to JavaGroups absence.
86

87         try {
88             Constructor JavaDoc c = Class
89                     .forName("org.apache.cayenne.event.JavaGroupsBridge")
90                     .getConstructor(new Class JavaDoc[] {
91                             Collection JavaDoc.class, String JavaDoc.class
92                     });
93
94             Object JavaDoc bridge = c.newInstance(new Object JavaDoc[] {
95                     localSubjects, externalSubject
96             });
97
98             // configure properties
99
String JavaDoc multicastAddress = (String JavaDoc) properties.get(MCAST_ADDRESS_PROPERTY);
100             String JavaDoc multicastPort = (String JavaDoc) properties.get(MCAST_PORT_PROPERTY);
101             String JavaDoc configURL = (String JavaDoc) properties.get(JGROUPS_CONFIG_URL_PROPERTY);
102
103             PropertyUtils.setProperty(bridge, "configURL", configURL);
104             PropertyUtils.setProperty(
105                     bridge,
106                     "multicastAddress",
107                     multicastAddress != null ? multicastAddress : MCAST_ADDRESS_DEFAULT);
108             PropertyUtils.setProperty(bridge, "multicastPort", multicastPort != null
109                     ? multicastPort
110                     : MCAST_PORT_DEFAULT);
111
112             return (EventBridge) bridge;
113         }
114         catch (Exception JavaDoc ex) {
115             throw new CayenneRuntimeException("Error creating JavaGroupsBridge", ex);
116         }
117     }
118
119     // mockup EventBridge
120
class NoopEventBridge extends EventBridge {
121
122         public NoopEventBridge() {
123             super(Collections.EMPTY_SET, null);
124         }
125
126         public boolean receivesExternalEvents() {
127             return false;
128         }
129
130         public boolean receivesLocalEvents() {
131             return false;
132         }
133
134         protected void startupExternal() {
135         }
136
137         protected void shutdownExternal() {
138         }
139
140         protected void sendExternalEvent(CayenneEvent localEvent) {
141         }
142
143         public void startup(EventManager eventManager, int mode, Object JavaDoc eventsSource) {
144             this.eventManager = eventManager;
145         }
146
147         public void shutdown() {
148         }
149     }
150 }
151
Popular Tags