1 6 7 package SOFA.SOFAnet.Search.RMI; 8 9 import java.net.InetAddress ; 10 import java.net.MalformedURLException ; 11 import java.rmi.Naming ; 12 import java.rmi.NotBoundException ; 13 import java.rmi.RemoteException ; 14 import SOFA.SOFAnet.Repository.NodeInfo; 15 import java.util.Date ; 16 import java.util.Calendar ; 17 18 26 public class RMISearchServerHolder 27 { 28 private final long tryPeriod = 30000; 30 private RMISearchInterface server; 31 private NodeInfo nodeInfo; 32 private Date lastError; 33 34 35 public RMISearchServerHolder() 36 { 37 lastError = new Date (0); 38 } 39 40 41 public void setNode(NodeInfo nodeInfo) 42 { 43 this.nodeInfo = nodeInfo; 44 } 45 46 50 public synchronized RMISearchInterface getServer(boolean justTry) throws RMISearchException 51 { 52 if (nodeInfo == null) throw new RMISearchException("Not specified node"); 53 54 if (server != null) return server; 55 56 if (justTry) 57 { 58 Date time = new Date (System.currentTimeMillis() - tryPeriod); 59 if (lastError.after(time)) return null; } 61 62 try 63 { 64 server = (RMISearchInterface)Naming.lookup("rmi://" + nodeInfo.getAddressAndPort() + "/SOFAnet/RMISearchServer"); 65 } 66 catch (NotBoundException e) 67 { 68 releaseServer(true); 69 if (justTry) return null; 70 else throw new RMISearchException(e.getMessage(), e); 71 } 72 catch (MalformedURLException e) 73 { 74 releaseServer(true); 75 if (justTry) return null; 76 else throw new RMISearchException(e.getMessage(), e); 77 } 78 catch (RemoteException e) 79 { 80 releaseServer(true); 81 if (justTry) return null; 82 else throw new RMISearchException(e.getMessage(), e); 83 } 84 85 return server; 86 } 87 88 92 public void releaseServer(boolean error) 93 { 94 server = null; 95 if (error) lastError = new Date (System.currentTimeMillis()); 96 } 97 98 101 public boolean isServer() 102 { 103 return server != null; 104 } 105 } 106 | Popular Tags |