1 package org.sapia.regis.remote; 2 3 import java.io.ByteArrayInputStream ; 4 import java.rmi.Remote ; 5 import java.rmi.RemoteException ; 6 import java.util.Iterator ; 7 import java.util.Map ; 8 import java.util.Properties ; 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 30 public class RemoteRegistry implements Registry, Configurable, Remote , Authenticating{ 31 32 private Registry _delegate; 33 private Node _root; 34 private Map _peers; 35 private Authenticator _auth; 36 private Properties _boostrapProps; 37 RemoteRegistry(Authenticator auth, Registry deleg, Properties 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 username, String password, String xmlConf, Properties props) throws RemoteException , Exception { 68 doLoad(path, username, password, xmlConf, true, props); 69 } 70 71 public void syncLoad(Path path, String username, String password, String xmlConf, Properties props) throws RemoteException , Exception { 72 doLoad(path, username, password, xmlConf, false, props); 73 } 74 75 public boolean authenticate(String username, String password) { 76 try{ 77 _auth.authenticate(username, password); 78 return true; 79 }catch(IllegalStateException e){ 80 e.printStackTrace(); 81 return false; 82 } 83 } 84 85 public void doLoad(Path path, String username, String password, String xmlConf, boolean sync, Properties props) throws RemoteException , Exception { 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 (xmlConf.getBytes()), cProps); 114 rw.commit(); 115 }catch(Exception 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 e){ 126 e.printStackTrace(); 127 throw new IllegalStateException ("Registry does not support remote operations"); 128 }catch(RuntimeException e){ 129 e.printStackTrace(); 130 throw new IllegalStateException ("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 peers){ 139 _peers = peers; 140 } 141 142 private void loadConfigurable(Configurable conf, Path path, String xmlConf, Properties props) throws Exception { 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 username, String password, String xmlConf) throws Exception { 151 if(_peers != null){ 152 ServerDebug.debug(this, "Sending configuration to peers (got " + _peers.size() + " peers in list)"); 153 synchronized(_peers){ 154 Iterator 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 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 |