KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Run > CCNS > ConnectorCacheNSImpl


1 /* $Id: ConnectorCacheNSImpl.java,v 1.3 2004/05/20 14:23:52 bures Exp $ */
2
3 /*
4  * Connector cache name server.
5  *
6  * Created on January 8, 2003, 4:34 PM
7  */

8
9 package SOFA.SOFAnode.Run.CCNS;
10
11 import java.io.File JavaDoc;
12 import java.io.FileInputStream JavaDoc;
13 import java.io.FileOutputStream JavaDoc;
14 import java.io.ObjectInputStream JavaDoc;
15 import java.io.ObjectOutputStream JavaDoc;
16 import java.net.MalformedURLException JavaDoc;
17 import java.rmi.Naming JavaDoc;
18 import java.rmi.RMISecurityManager JavaDoc;
19 import java.rmi.Remote JavaDoc;
20 import java.rmi.RemoteException JavaDoc;
21 import java.rmi.registry.LocateRegistry JavaDoc;
22 import java.rmi.registry.Registry JavaDoc;
23
24 /** Unicast remote object implementing remote interface.
25  *
26  * @author Tomas Bures
27  * @version 1.0
28  */

29 public class ConnectorCacheNSImpl extends java.rmi.server.UnicastRemoteObject JavaDoc implements ConnectorCacheNS {
30
31     private java.util.HashMap JavaDoc byHash;
32     private java.util.HashMap JavaDoc byName;
33     
34     private long counter=0;
35     
36     /** Constructs ConnectorCacheNSImpl object and exports it on default port.
37      */

38     public ConnectorCacheNSImpl() throws RemoteException JavaDoc {
39         super();
40         load();
41     }
42     
43     /** Constructs ConnectorCacheNSImpl object and exports it on specified port.
44      * @param port The port for exporting
45      */

46     public ConnectorCacheNSImpl(int port) throws RemoteException JavaDoc {
47         super(port);
48         load();
49     }
50
51     /** Register ConnectorCacheNSImpl object with the RMI registry.
52      * @param name - name identifying the service in the RMI registry
53      * @param create - create local registry if necessary
54      * @throw RemoteException if cannot be exported or bound to RMI registry
55      * @throw MalformedURLException if name cannot be used to construct a valid URL
56      * @throw IllegalArgumentException if null passed as name
57      */

58     public static void registerToRegistry(String JavaDoc name, Remote JavaDoc obj, boolean create) throws RemoteException JavaDoc, MalformedURLException JavaDoc{
59         
60         if (name == null) throw new IllegalArgumentException JavaDoc("registration name can not be null");
61         
62         System.out.print("Registering as: "+name);
63         try {
64             Naming.rebind(name, obj);
65         } catch (RemoteException JavaDoc ex){
66             if (create) {
67                 Registry JavaDoc r = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
68                 r.rebind(name, obj);
69             } else {
70               System.out.println();
71               throw ex;
72             }
73         }
74         System.out.println("\rCCNS is bound on '"+name+"' and running.");
75     }
76     
77     /** Main method.
78      */

79     public static void main(String JavaDoc[] args) {
80         System.setSecurityManager(new RMISecurityManager JavaDoc());
81         
82         try {
83             ConnectorCacheNSImpl obj = new ConnectorCacheNSImpl();
84
85             String JavaDoc rmiport = System.getProperty("SOFA.CCNS.rmiport","1099");
86             String JavaDoc rmihost = System.getProperty("SOFA.CCNS.rmihost","localhost");
87                   
88             registerToRegistry("//"+rmihost+":"+rmiport+"/CCNS", obj, false);
89             
90         } catch (RemoteException JavaDoc ex) {
91             ex.printStackTrace();
92         } catch (MalformedURLException JavaDoc ex) {
93             ex.printStackTrace();
94         }
95     }
96     
97     private String JavaDoc createHash(String JavaDoc name) {
98         counter++;
99         String JavaDoc cStr=Long.toHexString(counter);
100         return "H"+"0000000000000000".substring(0,16-cStr.length())+cStr;
101     }
102
103     private String JavaDoc getStorageFileName() {
104         return System.getProperty("sofa.connector.ccns","ccns.dat");
105     }
106
107     private void store() {
108         String JavaDoc fileName=getStorageFileName();
109         
110         try {
111             ObjectOutputStream JavaDoc os=new ObjectOutputStream JavaDoc(new FileOutputStream JavaDoc(fileName));
112             os.writeLong(counter);
113             os.writeObject(byName);
114             os.writeObject(byHash);
115         } catch (Exception JavaDoc e) {
116             System.err.println("Can't save the Connector Cache Name Server persistent storage.");
117             e.printStackTrace();
118         }
119     }
120     
121     private void load() {
122         String JavaDoc fileName=getStorageFileName();
123         
124         File JavaDoc inFile=new File JavaDoc(fileName);
125         if (!inFile.canRead()) {
126             byName=new java.util.HashMap JavaDoc();
127             byHash=new java.util.HashMap JavaDoc();
128             counter=0;
129             System.err.println("Connector Cache Name Server persistent storage not found. Initializing a new one.");
130             store();
131         } else {
132             try {
133                 ObjectInputStream JavaDoc is=new ObjectInputStream JavaDoc(new FileInputStream JavaDoc(inFile));
134                 counter=is.readLong();
135                 byName=(java.util.HashMap JavaDoc)is.readObject();
136                 byHash=(java.util.HashMap JavaDoc)is.readObject();
137             } catch (Exception JavaDoc e) {
138                 System.err.println("Can't read the Connector Cache Name Server persistent storage.");
139                 e.printStackTrace();
140             }
141         }
142     }
143     
144     /** Returns long connector name for the short unique tag. If the entry is not present in registry, it is automaticaly added and the unique tag is generated.
145      * @param name Unique short tag
146      * @throws RemoteException
147      * @return Long connector name
148      */

149     public synchronized String JavaDoc getHashByName(java.lang.String JavaDoc name) throws RemoteException JavaDoc {
150         String JavaDoc hash;
151         if (!byName.containsKey(name)) {
152             hash=createHash(name);
153             byName.put(name,hash);
154             byHash.put(hash,name);
155             store();
156         } else {
157             hash=(String JavaDoc)byName.get(name);
158         }
159         return hash;
160     }
161     
162     /** Returns long connector name for the short unique tag. If the entry is not present in registry, <code>null</code> is returned.
163      * @param name Unique short tag
164      * @throws RemoteException
165      * @return Long connector name
166      */

167     public synchronized String JavaDoc getNameByHash(java.lang.String JavaDoc hash) throws RemoteException JavaDoc {
168         String JavaDoc name;
169         if (!byHash.containsKey(hash)) {
170             name=null;
171         } else {
172             name=(String JavaDoc)byHash.get(hash);
173         }
174         return name;
175     }
176
177
178     /** Returns long connector name for the short unique tag. If the entry is not present in registry, <code>null</code> is returned.
179      * @param name Unique short tag
180      * @throws RemoteException
181      * @return Long connector name
182      *
183      * @author Petr Hnetynka
184      */

185     public synchronized String JavaDoc getHashByName2(java.lang.String JavaDoc name) throws RemoteException JavaDoc {
186         String JavaDoc hash;
187         if (!byName.containsKey(name)) {
188           hash = null;
189         } else {
190             hash=(String JavaDoc)byName.get(name);
191         }
192         return hash;
193     }
194 }
195
Popular Tags