1 19 20 package org.apache.cayenne.event; 21 22 import java.lang.reflect.Constructor ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.Map ; 26 27 import org.apache.cayenne.CayenneRuntimeException; 28 import org.apache.cayenne.reflect.PropertyUtils; 29 30 37 public class JavaGroupsBridgeFactory implements EventBridgeFactory { 38 39 public static final String MCAST_ADDRESS_DEFAULT = "228.0.0.5"; 40 public static final String MCAST_PORT_DEFAULT = "22222"; 41 42 public static final String MCAST_ADDRESS_PROPERTY = "cayenne.JavaGroupsBridge.mcast.address"; 43 public static final String MCAST_PORT_PROPERTY = "cayenne.JavaGroupsBridge.mcast.port"; 44 45 50 public static final String JGROUPS_CONFIG_URL_PROPERTY = "javagroupsbridge.config.url"; 51 52 59 public EventBridge createEventBridge( 60 Collection localSubjects, 61 String externalSubject, 62 Map properties) { 63 64 try { 65 Class.forName("org.jgroups.Channel"); 67 return createJavaGroupsBridge(localSubjects, externalSubject, properties); 68 } 69 catch (Exception ex) { 70 return createNoopBridge(); 72 } 73 } 74 75 private EventBridge createNoopBridge() { 76 return new NoopEventBridge(); 77 } 78 79 private EventBridge createJavaGroupsBridge( 80 Collection localSubjects, 81 String externalSubject, 82 Map properties) { 83 84 87 try { 88 Constructor c = Class 89 .forName("org.apache.cayenne.event.JavaGroupsBridge") 90 .getConstructor(new Class [] { 91 Collection .class, String .class 92 }); 93 94 Object bridge = c.newInstance(new Object [] { 95 localSubjects, externalSubject 96 }); 97 98 String multicastAddress = (String ) properties.get(MCAST_ADDRESS_PROPERTY); 100 String multicastPort = (String ) properties.get(MCAST_PORT_PROPERTY); 101 String configURL = (String ) 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 ex) { 115 throw new CayenneRuntimeException("Error creating JavaGroupsBridge", ex); 116 } 117 } 118 119 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 eventsSource) { 144 this.eventManager = eventManager; 145 } 146 147 public void shutdown() { 148 } 149 } 150 } 151 | Popular Tags |