1 56 57 package org.objectstyle.cayenne.event; 58 59 import java.util.EventListener ; 60 61 import org.objectstyle.cayenne.util.IDUtil; 62 63 80 public abstract class EventBridge implements EventListener { 81 public static final String VM_ID = new String (IDUtil.pseudoUniqueByteSequence16()); 82 public static final String VM_ID_PROPERRTY = "VM_ID"; 83 84 public static final int RECEIVE_LOCAL = 1; 85 public static final int RECEIVE_EXTERNAL = 2; 86 public static final int RECEIVE_LOCAL_EXTERNAL = 3; 87 88 protected String externalSubject; 89 protected EventSubject localSubject; 90 protected EventManager eventManager; 91 protected int mode; 92 93 98 public static String convertToExternalSubject(EventSubject localSubject) { 99 char[] chars = localSubject.getSubjectName().toCharArray(); 100 for (int i = 0; i < chars.length; i++) { 101 if (chars[i] == '/' || chars[i] == '.') { 102 chars[i] = '_'; 103 } 104 } 105 106 return new String (chars); 107 } 108 109 public EventBridge(EventSubject localSubject, String externalSubject) { 110 this.localSubject = localSubject; 111 this.externalSubject = externalSubject; 112 } 113 114 117 public String getExternalSubject() { 118 return externalSubject; 119 } 120 121 124 public EventSubject getLocalSubject() { 125 return localSubject; 126 } 127 128 public boolean receivesLocalEvents() { 129 return mode == RECEIVE_LOCAL_EXTERNAL || mode == RECEIVE_LOCAL; 130 } 131 132 public boolean receivesExternalEvents() { 133 return mode == RECEIVE_LOCAL_EXTERNAL || mode == RECEIVE_EXTERNAL; 134 } 135 136 140 public void startup(EventManager eventManager, int mode) throws Exception { 141 this.startup(eventManager, mode, null); 142 } 143 144 public void startup(EventManager eventManager, int mode, Object eventsSource) 145 throws Exception { 146 if (this.eventManager != null) { 148 shutdown(); 152 } 153 154 if (eventManager == null) { 155 throw new NullPointerException ("'eventManager' can't be null."); 156 } 157 158 this.eventManager = eventManager; 159 this.mode = mode; 160 161 if (receivesLocalEvents()) { 162 eventManager.addNonBlockingListener( 165 this, 166 "onLocalEvent", 167 CayenneEvent.class, 168 localSubject, 169 eventsSource); 170 } 171 172 startupExternal(); 173 } 174 175 178 protected abstract void startupExternal() throws Exception ; 179 180 183 public void shutdown() throws Exception { 184 this.eventManager.removeListener(this); 185 this.eventManager = null; 186 187 shutdownExternal(); 188 } 189 190 194 protected abstract void shutdownExternal() throws Exception ; 195 196 201 public void onExternalEvent(CayenneEvent event) { 202 if (eventManager != null) { 203 204 event.setPostedBy(this); 206 if (event.getSource() == null) { 207 event.setSource(this); 208 } 209 210 eventManager.postEvent(event, localSubject); 213 } 214 else { 215 throw new IllegalStateException ( 216 "Can't post events. EventBridge was not started properly. " 217 + "EventManager is null."); 218 } 219 } 220 221 225 public void onLocalEvent(CayenneEvent event) throws Exception { 226 if (event.getSource() != this) { 227 sendExternalEvent(event); 228 } 229 } 230 231 protected abstract void sendExternalEvent(CayenneEvent localEvent) throws Exception ; 232 } 233 | Popular Tags |