KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > ubik > NamingService


1 package org.sapia.soto.ubik;
2
3 import org.sapia.soto.ConfigurationException;
4 import org.sapia.soto.Service;
5
6 import org.sapia.ubik.rmi.naming.remote.RemoteInitialContextFactory;
7
8 import java.util.Properties JavaDoc;
9
10 import javax.naming.Context JavaDoc;
11 import javax.naming.InitialContext JavaDoc;
12 import javax.naming.NameNotFoundException JavaDoc;
13 import javax.naming.NamingException JavaDoc;
14
15
16 /**
17  * This service binds objects to a remote JNDI server. It is used by the Ubik layer.
18  *
19  * @author Yanick Duchesne
20  * <dl>
21  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
22  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
23  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
24  * </dl>
25  */

26 public class NamingService implements Service {
27   private String JavaDoc _domain;
28   private String JavaDoc _host;
29   private int _port;
30   private InitialContext JavaDoc _ctx;
31
32   /**
33    * @param host the host of the remote Ubik JNDI server.
34    */

35   public void setJndiHost(String JavaDoc host) {
36     _host = host;
37   }
38
39   /**
40    * @param port the port of the remote Ubik JNDI server.
41    */

42   public void setJndiPort(int port) {
43     _port = port;
44   }
45
46   /**
47    * @param domain the domain of the remote Ubik JNDI server.
48    */

49   public void setDomain(String JavaDoc domain) {
50     _domain = domain;
51   }
52
53   /**
54    * Binds the given object to the remote JNDI server to which this instance
55    * corresponds.
56    *
57    * @param name the name under which to bind the object.
58    * @param o the object to bind.
59    */

60   public synchronized void bind(String JavaDoc name, Object JavaDoc o)
61     throws NamingException JavaDoc {
62     _ctx.bind(name, o);
63   }
64
65   /**
66    * Looks up the object under the given name.
67    *
68    * @param name the name of the remote object to lookup.
69    */

70   public Object JavaDoc lookup(String JavaDoc name)
71     throws NamingException JavaDoc, NameNotFoundException JavaDoc {
72     return _ctx.lookup(name);
73   }
74
75   /**
76    * @see org.sapia.soto.Service#dispose()
77    */

78   public void dispose() {
79     try {
80       _ctx.close();
81     } catch (NamingException JavaDoc e) {
82       //noop
83
}
84   }
85
86   /**
87    * @see org.sapia.soto.Service#init()
88    */

89   public void init() throws Exception JavaDoc {
90     if (_port == 0) {
91       throw new ConfigurationException(
92         "'jndiPort' not set for Ubik naming service");
93     }
94
95     if (_host == null) {
96       throw new ConfigurationException(
97         "'jndiHost' not set for Ubik naming service");
98     }
99
100     if (_domain == null) {
101       throw new ConfigurationException(
102         "'domain' not set for Ubik naming service");
103     }
104
105     Properties JavaDoc props = new Properties JavaDoc();
106     props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
107       RemoteInitialContextFactory.class.getName());
108     props.setProperty(RemoteInitialContextFactory.UBIK_DOMAIN_NAME, _domain);
109     props.setProperty(Context.PROVIDER_URL, "ubik://" + _host + ":" + _port);
110     _ctx = new InitialContext JavaDoc(props);
111   }
112
113   /**
114    * @see org.sapia.soto.Service#start()
115    */

116   public void start() throws Exception JavaDoc {
117   }
118 }
119
Popular Tags