KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnet > Search > RMI > RMISearchServerHolder


1 /*
2  * RMISearchServerHolder.java
3  *
4  * Created on 29. duben 2004, 13:26
5  */

6
7 package SOFA.SOFAnet.Search.RMI;
8
9 import java.net.InetAddress JavaDoc;
10 import java.net.MalformedURLException JavaDoc;
11 import java.rmi.Naming JavaDoc;
12 import java.rmi.NotBoundException JavaDoc;
13 import java.rmi.RemoteException JavaDoc;
14 import SOFA.SOFAnet.Repository.NodeInfo;
15 import java.util.Date JavaDoc;
16 import java.util.Calendar JavaDoc;
17
18 /**
19  * The object of this class is responsible for connecting to remote RMI server and holding its interface.
20  * <p>
21  * It can be configured not to connect to the remote server for specific time if the previous attempt
22  * was not successfull.
23  *
24  * @author Ladislav Sobr
25  */

26 public class RMISearchServerHolder
27 {
28   private final long tryPeriod = 30000; //how often we try to connect to the server (in milliseconds)
29

30   private RMISearchInterface server;
31   private NodeInfo nodeInfo;
32   private Date JavaDoc lastError;
33   
34   /** Creates a new instance of RMISearchServerHolder */
35   public RMISearchServerHolder()
36   {
37     lastError = new Date JavaDoc(0);
38   }
39   
40   /** Setups the node to connect to */
41   public void setNode(NodeInfo nodeInfo)
42   {
43     this.nodeInfo = nodeInfo;
44   }
45   
46   /**
47    * Returns interface to the RMI server on the remote node.
48    * @param justTry if true, the function can return null if the previous attempt recently failed
49    */

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 JavaDoc time = new Date JavaDoc(System.currentTimeMillis() - tryPeriod);
59       if (lastError.after(time)) return null; //it is to early to look for another lookup
60
}
61
62     try
63     {
64       server = (RMISearchInterface)Naming.lookup("rmi://" + nodeInfo.getAddressAndPort() + "/SOFAnet/RMISearchServer");
65     }
66     catch (NotBoundException JavaDoc e)
67     {
68       releaseServer(true);
69       if (justTry) return null;
70       else throw new RMISearchException(e.getMessage(), e);
71     }
72     catch (MalformedURLException JavaDoc e)
73     {
74       releaseServer(true);
75       if (justTry) return null;
76       else throw new RMISearchException(e.getMessage(), e);
77     }
78     catch (RemoteException JavaDoc 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   /**
89    * Releases interface of the remote RMI server
90    * @param error do we release the server because an error occured ?
91    */

92   public void releaseServer(boolean error)
93   {
94     server = null;
95     if (error) lastError = new Date JavaDoc(System.currentTimeMillis());
96   }
97   
98   /**
99    * @return true if the object holds active server
100    */

101   public boolean isServer()
102   {
103     return server != null;
104   }
105 }
106
Popular Tags