1 56 package org.objectstyle.cayenne.event; 57 58 import java.lang.reflect.Constructor ; 59 import java.util.Map ; 60 61 import org.apache.commons.beanutils.BeanUtils; 62 import org.apache.log4j.Logger; 63 import org.objectstyle.cayenne.CayenneRuntimeException; 64 65 72 public class JavaGroupsBridgeFactory implements EventBridgeFactory { 73 private static Logger logObj = Logger.getLogger(JavaGroupsBridgeFactory.class); 74 75 public static final String MCAST_ADDRESS_DEFAULT = "228.0.0.5"; 76 public static final String MCAST_PORT_DEFAULT = "22222"; 77 78 public static final String MCAST_ADDRESS_PROPERTY = 79 "cayenne.JavaGroupsBridge.mcast.address"; 80 public static final String MCAST_PORT_PROPERTY = 81 "cayenne.JavaGroupsBridge.mcast.port"; 82 83 87 public static final String JGROUPS_CONFIG_URL_PROPERTY = 88 "javagroupsbridge.config.url"; 89 90 96 public EventBridge createEventBridge(EventSubject localSubject, Map properties) { 97 try { 98 Class.forName("org.jgroups.Channel"); 100 return createJavaGroupsBridge(localSubject, properties); 101 } catch (Exception ex) { 102 return createNoopBridge(); 104 } 105 } 106 107 private EventBridge createNoopBridge() { 108 logObj.warn( 109 "*** Remote events disabled. Reason: JGroups is not available. Download JGroups from http://www.jgroups.org/"); 110 return new NoopEventBridge(); 111 } 112 113 private EventBridge createJavaGroupsBridge( 114 EventSubject localSubject, 115 Map properties) { 116 117 120 try { 121 Constructor c = 122 Class.forName( 123 "org.objectstyle.cayenne.event.JavaGroupsBridge").getConstructor( 124 new Class [] { EventSubject.class, String .class }); 125 126 Object bridge = 127 c.newInstance( 128 new Object [] { 129 localSubject, 130 EventBridge.convertToExternalSubject(localSubject)}); 131 132 String multicastAddress = (String ) properties.get(MCAST_ADDRESS_PROPERTY); 134 String multicastPort = (String ) properties.get(MCAST_PORT_PROPERTY); 135 String configURL = (String ) properties.get(JGROUPS_CONFIG_URL_PROPERTY); 136 137 BeanUtils.setProperty(bridge, "configURL", configURL); 138 BeanUtils.setProperty( 139 bridge, 140 "multicastAddress", 141 multicastAddress != null ? multicastAddress : MCAST_ADDRESS_DEFAULT); 142 BeanUtils.setProperty( 143 bridge, 144 "multicastPort", 145 multicastPort != null ? multicastPort : MCAST_PORT_DEFAULT); 146 147 return (EventBridge) bridge; 148 } catch (Exception ex) { 149 throw new CayenneRuntimeException("Error creating JavaGroupsBridge", ex); 150 } 151 } 152 153 class NoopEventBridge extends EventBridge { 155 156 public NoopEventBridge() { 157 super(null, null); 158 } 159 160 public boolean receivesExternalEvents() { 161 return false; 162 } 163 164 public boolean receivesLocalEvents() { 165 return false; 166 } 167 168 protected void startupExternal() { 169 } 170 171 protected void shutdownExternal() { 172 } 173 174 protected void sendExternalEvent(CayenneEvent localEvent) { 175 } 176 177 public void startup(EventManager eventManager, int mode, Object eventsSource) { 178 this.eventManager = eventManager; 179 } 180 181 public void shutdown() { 182 } 183 } 184 } 185 | Popular Tags |