1 package org.sapia.ubik.rmi.naming.remote.archie; 2 3 import org.sapia.archie.Archie; 4 import org.sapia.archie.Name; 5 import org.sapia.archie.NamePart; 6 import org.sapia.archie.NotFoundException; 7 import org.sapia.archie.sync.SynchronizedNode; 8 import org.sapia.archie.sync.Synchronizer; 9 10 import org.sapia.ubik.mcast.AsyncEventListener; 11 import org.sapia.ubik.mcast.EventChannel; 12 import org.sapia.ubik.mcast.ListenerAlreadyRegisteredException; 13 import org.sapia.ubik.mcast.RemoteEvent; 14 import org.sapia.ubik.mcast.RespList; 15 import org.sapia.ubik.mcast.Response; 16 import org.sapia.ubik.mcast.SyncEventListener; 17 18 import java.io.IOException ; 19 20 import javax.naming.NamingException ; 21 22 23 31 public class UbikSynchronizer implements Synchronizer, AsyncEventListener, 32 SyncEventListener { 33 private EventChannel _channel; 34 private Archie _root; 35 36 UbikSynchronizer(EventChannel channel) throws NamingException { 37 _channel = channel; 38 _channel = channel; 39 _channel.registerAsyncListener(SyncPutEvent.class.getName(), this); 40 41 try { 42 _channel.registerSyncListener(SyncGetEvent.class.getName(), this); 43 } catch (ListenerAlreadyRegisteredException e) { 44 NamingException ne = new NamingException ("Could not start event channel"); 45 ne.setRootCause(e); 46 throw ne; 47 } 48 49 if (!_channel.isStarted()) { 50 if (_channel.isClosed()) { 51 throw new IllegalStateException ("Event channel is closed!"); 52 } 53 54 try { 55 _channel.start(); 56 } catch (java.io.IOException e) { 57 NamingException ne = new NamingException ( 58 "Could not start event channel"); 59 ne.setRootCause(e); 60 throw ne; 61 } 62 } 63 } 64 65 68 public EventChannel getEventChannel() { 69 return _channel; 70 } 71 72 75 public void setRoot(SynchronizedNode root) { 76 _root = new Archie(root); 77 } 78 79 82 public Object onGetValue(Name nodeAbsolutePath, NamePart valueName) { 83 Response toReturn = null; 84 RespList results; 85 86 try { 87 results = _channel.send(SyncGetEvent.class.getName(), 88 new SyncGetEvent(nodeAbsolutePath, valueName)); 89 } catch (java.io.IOException ioe) { 90 ioe.printStackTrace(); 91 92 return null; 93 } 94 95 for (int i = 0; i < results.count(); i++) { 96 toReturn = results.get(i); 97 98 if ((toReturn.getData() != null) && !(toReturn.isError())) { 99 return toReturn.getData(); 100 } 101 } 102 103 return null; 104 } 105 106 109 public void onPutValue(Name nodeAbsolutePath, NamePart valueName, 110 Object value, boolean overwrite) { 111 SyncPutEvent evt = new SyncPutEvent(nodeAbsolutePath, valueName, value, 112 overwrite); 113 114 try { 115 _channel.dispatch(SyncPutEvent.class.getName(), evt); 116 } catch (IOException e) { 117 e.printStackTrace(); 118 } 119 } 120 121 124 public void onRemoveValue(Name nodeAbsolutePath, NamePart name) { 125 SyncRemoveEvent evt = new SyncRemoveEvent(nodeAbsolutePath, name); 126 127 try { 128 _channel.dispatch(SyncRemoveEvent.class.getName(), evt); 129 } catch (IOException e) { 130 e.printStackTrace(); 131 } 132 } 133 134 137 public void onAsyncEvent(RemoteEvent evt) { 138 if (evt.getType().equals(SyncPutEvent.class.getName())) { 139 try { 140 SyncPutEvent put = (SyncPutEvent) evt.getData(); 141 SynchronizedNode node = (SynchronizedNode) root().lookupNode(put.getNodePath(), 142 true); 143 node.synchronizePut(put.getName(), put.getValue(), true); 144 } catch (NotFoundException e) { 145 } catch (Exception e) { 147 e.printStackTrace(); 148 } 149 } 150 } 151 152 155 public Object onSyncEvent(RemoteEvent evt) { 156 if (evt.getType().equals(SyncGetEvent.class.getName())) { 157 try { 158 SyncGetEvent get = (SyncGetEvent) evt.getData(); 159 SynchronizedNode node = (SynchronizedNode) root().lookupNode(get.getNodePath(), 160 true); 161 162 return node.synchronizeGet(get.getName()); 163 } catch (Exception e) { 164 e.printStackTrace(); 165 166 return e; 167 } 168 } 169 170 return null; 171 } 172 173 private Archie root() { 174 if (_root == null) { 175 throw new IllegalStateException ("Root node was not set"); 176 } 177 178 return _root; 179 } 180 } 181 | Popular Tags |