KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Run > DeplDockRegistry > DeplDockRegistryServer


1 /* $Id: DeplDockRegistryServer.java,v 1.2 2004/05/20 14:23:52 bures Exp $ */
2 package SOFA.SOFAnode.Run.DeplDockRegistry;
3
4 import java.net.InetAddress JavaDoc;
5 import java.net.UnknownHostException JavaDoc;
6 import java.util.Hashtable JavaDoc;
7
8 import org.omg.CORBA.ORB JavaDoc;
9 import org.omg.CosNaming.NameComponent JavaDoc;
10 import org.omg.CosNaming.NamingContext JavaDoc;
11 import org.omg.CosNaming.NamingContextHelper JavaDoc;
12 import org.omg.PortableServer.POA JavaDoc;
13 import org.omg.PortableServer.POAHelper JavaDoc;
14
15 /** Deployment dock registry servant
16   *
17   * @author Petr Hnetynka
18   */

19 class DeplDockRegistryServant implements DeplDockRegistryOperations {
20   
21   Hashtable JavaDoc store;
22   long id;
23   String JavaDoc nodeName;
24   SOFA.Util.Lock lock;
25   
26   public DeplDockRegistryServant (String JavaDoc nodeName) {
27     store = new Hashtable JavaDoc();
28     id = 0;
29     this.nodeName = nodeName;
30     lock = new SOFA.Util.Lock(false);
31   }
32   
33   public short register (byte[] sofaReference, String JavaDoc name) {
34     if (store.containsKey(name)) {
35       return 1;
36     }
37     store.put(new String JavaDoc(name), new Item(sofaReference));
38     return 0;
39   }
40   
41   public byte[] lookup (String JavaDoc name) throws SOFA.SOFAnode.Run.DeplDockRegistry.NotFoundException {
42     Item a = (Item) store.get(name);
43     if (a == null)
44       throw new NotFoundException();
45     return a.obj;
46   }
47
48   public String JavaDoc[] getAllNames () {
49     String JavaDoc[] ret = new String JavaDoc [store.size()] ;
50     int i = 0;
51     for (java.util.Enumeration JavaDoc e = store.keys() ; e.hasMoreElements() ;) {
52       ret[i] = new String JavaDoc ((String JavaDoc) e.nextElement());
53       i++;
54     }
55     return ret;
56   }
57
58   public String JavaDoc getUniqueID() {
59     String JavaDoc ret;
60     lock.lock();
61     ret = nodeName + Long.toString(id++);
62     lock.unlock();
63     return ret;
64   }
65 }
66
67 class Item {
68   byte[] obj;
69   public Item(byte[] o) { obj = o; }
70 }
71
72 /** Implementation.
73   *
74   * @author Petr Hnetynka
75   */

76 public class DeplDockRegistryServer {
77   public static void main(String JavaDoc[] argv) {
78     try{
79       String JavaDoc sofaNodeName = System.getProperty("sofa.nodename","");
80       
81       if (sofaNodeName.compareTo("")==0) {
82         try {
83           sofaNodeName = InetAddress.getLocalHost().getHostName();
84         } catch (UnknownHostException JavaDoc e) {
85           System.err.println("Can't get SOFAnode name. Set name manually.");
86           System.exit(1);
87         }
88       }
89
90 /* for (int i=0; i<argv.length; i++) {
91         System.out.println(argv[i]);
92       }*/

93      
94       ORB JavaDoc orb = ORB.init(argv, null);
95  
96       DeplDockRegistryServant servant = new DeplDockRegistryServant(sofaNodeName);
97
98       POA JavaDoc rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
99       rootpoa.the_POAManager().activate();
100      
101       DeplDockRegistryPOATie tie = new DeplDockRegistryPOATie(servant, rootpoa);
102       DeplDockRegistry ref = tie._this(orb);
103  
104       org.omg.CORBA.Object JavaDoc objRef = orb.resolve_initial_references("NameService");
105       NamingContext JavaDoc ncRef = NamingContextHelper.narrow(objRef);
106  
107       NameComponent JavaDoc nc = new NameComponent JavaDoc("DeplDockRegistry", "");
108       NameComponent JavaDoc path[] = { nc };
109       ncRef.rebind(path, ref);
110
111       
112       if (System.getSecurityManager() == null) {
113         System.setSecurityManager(new java.rmi.RMISecurityManager JavaDoc());
114       }
115       String JavaDoc rmiport = System.getProperty("sofa.rmiport","1099");
116       String JavaDoc rmihost = System.getProperty("sofa.rmihost","localhost");
117       java.rmi.Naming.rebind("//"+rmihost+":"+rmiport+"/RgRMIDock", new RgRMIDockImpl() );
118       
119
120       System.out.println("DeplDockRegistry is running.");
121
122       java.lang.Object JavaDoc sync = new java.lang.Object JavaDoc();
123       synchronized (sync) {
124         sync.wait();
125       }
126  
127     } catch (Exception JavaDoc e) {
128       System.err.println("Exception: " + e);
129       e.printStackTrace(System.out);
130     }
131   }
132 }
133
Popular Tags