KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > naming > remote > archie > UbikSynchronizer


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 JavaDoc;
19
20 import javax.naming.NamingException JavaDoc;
21
22
23 /**
24  * @author Yanick Duchesne
25  * <dl>
26  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
27  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
28  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
29  * </dl>
30  */

31 public class UbikSynchronizer implements Synchronizer, AsyncEventListener,
32   SyncEventListener {
33   private EventChannel _channel;
34   private Archie _root;
35
36   UbikSynchronizer(EventChannel channel) throws NamingException JavaDoc {
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 JavaDoc ne = new NamingException JavaDoc("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 JavaDoc("Event channel is closed!");
52       }
53
54       try {
55         _channel.start();
56       } catch (java.io.IOException JavaDoc e) {
57         NamingException JavaDoc ne = new NamingException JavaDoc(
58             "Could not start event channel");
59         ne.setRootCause(e);
60         throw ne;
61       }
62     }
63   }
64
65   /**
66    * @return the <code>EventChannel</code> that this instance uses.
67    */

68   public EventChannel getEventChannel() {
69     return _channel;
70   }
71
72   /**
73    * @param root the <code>SynchronizedNode</code> that acts as the root node.
74    */

75   public void setRoot(SynchronizedNode root) {
76     _root = new Archie(root);
77   }
78
79   /**
80    * @see org.sapia.archie.sync.Synchronizer#onGetValue(org.sapia.archie.Name, org.sapia.archie.NamePart)
81    */

82   public Object JavaDoc 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 JavaDoc 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   /**
107    * @see org.sapia.archie.sync.Synchronizer#onPutValue(org.sapia.archie.Name, org.sapia.archie.NamePart, java.lang.Object, boolean)
108    */

109   public void onPutValue(Name nodeAbsolutePath, NamePart valueName,
110     Object JavaDoc 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 JavaDoc e) {
117       e.printStackTrace();
118     }
119   }
120
121   /**
122    * @see org.sapia.archie.sync.Synchronizer#onRemoveValue(org.sapia.archie.Name, org.sapia.archie.NamePart)
123    */

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 JavaDoc e) {
130       e.printStackTrace();
131     }
132   }
133
134   /**
135    * @see org.sapia.ubik.mcast.AsyncEventListener#onAsyncEvent(org.sapia.ubik.mcast.RemoteEvent)
136    */

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         // noop
146
} catch (Exception JavaDoc e) {
147         e.printStackTrace();
148       }
149     }
150   }
151
152   /**
153    * @see org.sapia.ubik.mcast.SyncEventListener#onSyncEvent(org.sapia.ubik.mcast.RemoteEvent)
154    */

155   public Object JavaDoc 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 JavaDoc 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 JavaDoc("Root node was not set");
176     }
177
178     return _root;
179   }
180 }
181
Popular Tags