KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > cmi > Naming


1 /*
2  * Copyright (C) 2002-2003, Simon Nieuviarts
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  */

19 package org.objectweb.carol.cmi;
20
21 import java.net.MalformedURLException JavaDoc;
22 import java.rmi.AlreadyBoundException JavaDoc;
23 import java.rmi.NotBoundException JavaDoc;
24 import java.rmi.Remote JavaDoc;
25 import java.rmi.RemoteException JavaDoc;
26
27 public final class Naming {
28     private Naming() {
29     }
30
31     private static ClusterRegistryInternal getRegistry(String JavaDoc host, int port)
32         throws RemoteException JavaDoc {
33         ClusterRegistryInternal r =
34             (ClusterRegistryInternal) LowerOrb.getRegistryStub(
35                 "org.objectweb.carol.cmi.ClusterRegistryImpl",
36                 host,
37                 port);
38         return r;
39     }
40
41     public static ClusterRegistry getRegistry(NamingContextHostPort[] hp)
42         throws RemoteException JavaDoc {
43         int n = hp.length;
44         if (n == 0) return null;
45         ClusterRegistryInternal r = getRegistry(hp[0].host, hp[0].port);
46         if (n == 1) return new ClusterRegistryClient(r);
47         ClusterStubData csd = new ClusterStubData(r);
48         for (int i = 1; i < n; i++) {
49             csd.setStub(getRegistry(hp[i].host, hp[i].port));
50         }
51         return new ClusterRegistryClient((ClusterRegistryInternal) csd.getClusterStub());
52     }
53
54     public static ClusterRegistry getLocalRegistry(NamingContextHostPort[] hp)
55         throws MalformedURLException JavaDoc, RemoteException JavaDoc {
56         if (hp.length > 1)
57             throw new MalformedURLException JavaDoc("Can not bind or unbind in multiple machines");
58         ClusterRegistry creg = getRegistry(hp);
59         return creg;
60     }
61
62     public static Object JavaDoc lookup(String JavaDoc name)
63         throws MalformedURLException JavaDoc, NotBoundException JavaDoc, RemoteException JavaDoc {
64         NamingContext nc = new NamingContext(name);
65         ClusterRegistry reg = getRegistry(nc.hp);
66         if (nc.name.length() == 0)
67             return reg;
68         return reg.lookup(nc.name);
69     }
70
71     public static void bind(String JavaDoc name, Remote JavaDoc obj)
72         throws MalformedURLException JavaDoc, AlreadyBoundException JavaDoc, RemoteException JavaDoc {
73         NamingContext nc = new NamingContext(name);
74         ClusterRegistry reg = getLocalRegistry(nc.hp);
75         if (obj == null)
76             throw new NullPointerException JavaDoc("cannot bind null object");
77         reg.bind(nc.name, obj);
78     }
79
80     public static void rebind(String JavaDoc name, Remote JavaDoc obj)
81         throws MalformedURLException JavaDoc, RemoteException JavaDoc {
82         NamingContext nc = new NamingContext(name);
83         ClusterRegistry reg = getLocalRegistry(nc.hp);
84         if (obj == null)
85             throw new NullPointerException JavaDoc("cannot bind null object");
86         reg.rebind(nc.name, obj);
87     }
88
89     public static void unbind(String JavaDoc name)
90         throws MalformedURLException JavaDoc, NotBoundException JavaDoc, RemoteException JavaDoc {
91         NamingContext nc = new NamingContext(name);
92         ClusterRegistry reg = getLocalRegistry(nc.hp);
93         reg.unbind(nc.name);
94     }
95
96     public static String JavaDoc[] list(String JavaDoc name)
97         throws MalformedURLException JavaDoc, RemoteException JavaDoc {
98         NamingContext nc = new NamingContext(name);
99         ClusterRegistry reg = getRegistry(nc.hp);
100
101         String JavaDoc prefix = nc.scheme.equals("") ? "" : nc.scheme + ":";
102         prefix += "//";
103         int i = 0;
104         while (i < nc.hp.length) {
105             prefix += nc.hp[i].host;
106             if (nc.hp[i].port != LowerOrb.DEFAULT_CREG_PORT)
107                 prefix += ":" + nc.hp[i].port;
108             i++;
109             if (i < nc.hp.length)
110                 prefix += ",";
111         }
112         prefix += "/";
113
114         String JavaDoc lst[] = reg.list();
115         for (i = 0; i < lst.length; i++)
116             lst[i] = prefix + lst[i];
117         return lst;
118     }
119 }
120
Popular Tags