KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > regis > remote > RemoteRegistry


1 package org.sapia.regis.remote;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.rmi.Remote JavaDoc;
5 import java.rmi.RemoteException JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.Map JavaDoc;
8 import java.util.Properties JavaDoc;
9
10 import org.sapia.regis.Configurable;
11 import org.sapia.regis.Node;
12 import org.sapia.regis.Path;
13 import org.sapia.regis.RWNode;
14 import org.sapia.regis.RWSession;
15 import org.sapia.regis.RegisSession;
16 import org.sapia.regis.Registry;
17 import org.sapia.regis.loader.RegistryConfigLoader;
18 import org.sapia.regis.util.CompositeProperties;
19
20 /**
21  * A registry of remote nodes. An instance of this class wraps
22  * a given <code>Registry</code> instance in order to make it
23  * "remotable".
24  *
25  * @see org.sapia.regis.remote.RemoteNode
26  * @see org.sapia.regis.remote.RegistryExporter
27  * @author yduchesne
28  *
29  */

30 public class RemoteRegistry implements Registry, Configurable, Remote JavaDoc, Authenticating{
31   
32   private Registry _delegate;
33   private Node _root;
34   private Map JavaDoc _peers;
35   private Authenticator _auth;
36   private Properties JavaDoc _boostrapProps;
37   RemoteRegistry(Authenticator auth, Registry deleg, Properties JavaDoc boostrapProps){
38     _delegate = deleg;
39     _auth = auth;
40     RegisSession s = deleg.open();
41     try{
42       _root = new RemoteNode(deleg.getRoot());
43     }finally{
44       s.close();
45     }
46     _boostrapProps = boostrapProps;
47   }
48   
49   public RegisSession open() {
50     RegisSession s = _delegate.open();
51     RemoteSessions.join(s);
52     return s;
53   }
54   
55   public Node getRoot() {
56     return _root;
57   }
58   
59   public void close() {
60     _delegate.close();
61   }
62   
63   public Registry internal(){
64     return _delegate;
65   }
66   
67   public void load(Path path, String JavaDoc username, String JavaDoc password, String JavaDoc xmlConf, Properties JavaDoc props) throws RemoteException JavaDoc, Exception JavaDoc {
68     doLoad(path, username, password, xmlConf, true, props);
69   }
70
71   public void syncLoad(Path path, String JavaDoc username, String JavaDoc password, String JavaDoc xmlConf, Properties JavaDoc props) throws RemoteException JavaDoc, Exception JavaDoc {
72     doLoad(path, username, password, xmlConf, false, props);
73   }
74   
75   public boolean authenticate(String JavaDoc username, String JavaDoc password) {
76     try{
77       _auth.authenticate(username, password);
78       return true;
79     }catch(IllegalStateException JavaDoc e){
80       e.printStackTrace();
81       return false;
82     }
83   }
84   
85   public void doLoad(Path path, String JavaDoc username, String JavaDoc password, String JavaDoc xmlConf, boolean sync, Properties JavaDoc props) throws RemoteException JavaDoc, Exception JavaDoc {
86     _auth.authenticate(username, password);
87     RegisSession sess = null;
88     try{
89       RegistryServerLockManager.lock().readLock().unlock();
90       RegistryServerLockManager.lock().writeLock().lock();
91       sess = _delegate.open();
92       Node node = _delegate.getRoot();
93       if(_delegate instanceof Configurable){
94         loadConfigurable((Configurable)_delegate, path, xmlConf, props);
95       }
96       else if(node instanceof Configurable){
97         loadConfigurable((Configurable)node, path, xmlConf, props);
98       }
99       else{
100         RWSession rw = (RWSession)sess;
101         rw.begin();
102   
103         try{
104           if(path != null && !path.isRoot()){
105             node = node.getChild(path);
106           }
107           
108           RegistryConfigLoader loader = new RegistryConfigLoader((RWNode)node);
109           CompositeProperties cProps = new CompositeProperties(_boostrapProps);
110           if(props != null){
111             cProps.addChild(props);
112           }
113           loader.load(new ByteArrayInputStream JavaDoc(xmlConf.getBytes()), cProps);
114           rw.commit();
115         }catch(Exception JavaDoc e){
116           rw.rollback();
117         }
118       }
119       if(sync){
120         dispatch(path, username, password, xmlConf);
121       }
122       else{
123         ServerDebug.debug(this, "Receiving configuration from peer");
124       }
125     }catch(ClassCastException JavaDoc e){
126       e.printStackTrace();
127       throw new IllegalStateException JavaDoc("Registry does not support remote operations");
128     }catch(RuntimeException JavaDoc e){
129       e.printStackTrace();
130       throw new IllegalStateException JavaDoc("Could not load configuration - " + e.getMessage());
131     }finally{
132       sess.close();
133       RegistryServerLockManager.lock().readLock().lock();
134       RegistryServerLockManager.lock().writeLock().unlock();
135     }
136   }
137   
138   void setPeers(Map JavaDoc peers){
139     _peers = peers;
140   }
141   
142   private void loadConfigurable(Configurable conf, Path path, String JavaDoc xmlConf, Properties JavaDoc props) throws Exception JavaDoc{
143     CompositeProperties cProps = new CompositeProperties(_boostrapProps);
144     if(props != null){
145       cProps.addChild(props);
146     }
147     conf.load(path, null, null, xmlConf, cProps);
148   }
149   
150   private void dispatch(Path path, String JavaDoc username, String JavaDoc password, String JavaDoc xmlConf) throws Exception JavaDoc{
151     if(_peers != null){
152       ServerDebug.debug(this, "Sending configuration to peers (got " + _peers.size() + " peers in list)");
153       synchronized(_peers){
154         Iterator JavaDoc itr = _peers.values().iterator();
155         while(itr.hasNext()){
156           Configurable cfg = (Configurable)itr.next();
157           try{
158             cfg.syncLoad(path, username, password, xmlConf, null);
159           }catch(RemoteException JavaDoc e){
160             ServerDebug.debug(this, "One peer is down; removing from list");
161             itr.remove();
162           }
163         }
164       }
165     }
166   }
167
168
169 }
170
Popular Tags