1 2 3 8 9 package SOFA.SOFAnode.Run.CCNS; 10 11 import java.io.File ; 12 import java.io.FileInputStream ; 13 import java.io.FileOutputStream ; 14 import java.io.ObjectInputStream ; 15 import java.io.ObjectOutputStream ; 16 import java.net.MalformedURLException ; 17 import java.rmi.Naming ; 18 import java.rmi.RMISecurityManager ; 19 import java.rmi.Remote ; 20 import java.rmi.RemoteException ; 21 import java.rmi.registry.LocateRegistry ; 22 import java.rmi.registry.Registry ; 23 24 29 public class ConnectorCacheNSImpl extends java.rmi.server.UnicastRemoteObject implements ConnectorCacheNS { 30 31 private java.util.HashMap byHash; 32 private java.util.HashMap byName; 33 34 private long counter=0; 35 36 38 public ConnectorCacheNSImpl() throws RemoteException { 39 super(); 40 load(); 41 } 42 43 46 public ConnectorCacheNSImpl(int port) throws RemoteException { 47 super(port); 48 load(); 49 } 50 51 58 public static void registerToRegistry(String name, Remote obj, boolean create) throws RemoteException , MalformedURLException { 59 60 if (name == null) throw new IllegalArgumentException ("registration name can not be null"); 61 62 System.out.print("Registering as: "+name); 63 try { 64 Naming.rebind(name, obj); 65 } catch (RemoteException ex){ 66 if (create) { 67 Registry 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 79 public static void main(String [] args) { 80 System.setSecurityManager(new RMISecurityManager ()); 81 82 try { 83 ConnectorCacheNSImpl obj = new ConnectorCacheNSImpl(); 84 85 String rmiport = System.getProperty("SOFA.CCNS.rmiport","1099"); 86 String rmihost = System.getProperty("SOFA.CCNS.rmihost","localhost"); 87 88 registerToRegistry("//"+rmihost+":"+rmiport+"/CCNS", obj, false); 89 90 } catch (RemoteException ex) { 91 ex.printStackTrace(); 92 } catch (MalformedURLException ex) { 93 ex.printStackTrace(); 94 } 95 } 96 97 private String createHash(String name) { 98 counter++; 99 String cStr=Long.toHexString(counter); 100 return "H"+"0000000000000000".substring(0,16-cStr.length())+cStr; 101 } 102 103 private String getStorageFileName() { 104 return System.getProperty("sofa.connector.ccns","ccns.dat"); 105 } 106 107 private void store() { 108 String fileName=getStorageFileName(); 109 110 try { 111 ObjectOutputStream os=new ObjectOutputStream (new FileOutputStream (fileName)); 112 os.writeLong(counter); 113 os.writeObject(byName); 114 os.writeObject(byHash); 115 } catch (Exception 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 fileName=getStorageFileName(); 123 124 File inFile=new File (fileName); 125 if (!inFile.canRead()) { 126 byName=new java.util.HashMap (); 127 byHash=new java.util.HashMap (); 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 is=new ObjectInputStream (new FileInputStream (inFile)); 134 counter=is.readLong(); 135 byName=(java.util.HashMap )is.readObject(); 136 byHash=(java.util.HashMap )is.readObject(); 137 } catch (Exception e) { 138 System.err.println("Can't read the Connector Cache Name Server persistent storage."); 139 e.printStackTrace(); 140 } 141 } 142 } 143 144 149 public synchronized String getHashByName(java.lang.String name) throws RemoteException { 150 String 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 )byName.get(name); 158 } 159 return hash; 160 } 161 162 167 public synchronized String getNameByHash(java.lang.String hash) throws RemoteException { 168 String name; 169 if (!byHash.containsKey(hash)) { 170 name=null; 171 } else { 172 name=(String )byHash.get(hash); 173 } 174 return name; 175 } 176 177 178 185 public synchronized String getHashByName2(java.lang.String name) throws RemoteException { 186 String hash; 187 if (!byName.containsKey(name)) { 188 hash = null; 189 } else { 190 hash=(String )byName.get(name); 191 } 192 return hash; 193 } 194 } 195 | Popular Tags |