KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.soto.ubik;
2
3 import org.sapia.ubik.rmi.naming.remote.RemoteInitialContextFactory;
4
5 import org.sapia.util.xml.confix.ConfigurationException;
6 import org.sapia.util.xml.confix.ObjectCreationCallback;
7
8 import java.util.Properties JavaDoc;
9
10 import javax.naming.Context JavaDoc;
11 import javax.naming.InitialContext JavaDoc;
12 import javax.naming.NamingException JavaDoc;
13
14
15 /**
16  * An instance of this class is used to lookup remote Ubik services.
17  *
18  * @author Yanick Duchesne
19  * <dl>
20  * <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>
21  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
22  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
23  * </dl>
24  */

25 public class Lookup implements ObjectCreationCallback {
26   private String JavaDoc _host;
27   private String JavaDoc _domain;
28   private String JavaDoc _name;
29   private int _port;
30
31   /**
32    * Constructor for Lookup.
33    */

34   public Lookup() {
35   }
36
37   /**
38    * @param host the host of the remote Ubik JNDI server.
39    */

40   public void setJndiHost(String JavaDoc host) {
41     _host = host;
42   }
43
44   /**
45    * @param port the port of the remote Ubik JNDI server.
46    */

47   public void setJndiPort(int port) {
48     _port = port;
49   }
50
51   /**
52    * @param domain the domain of the remote Ubik JNDI server.
53    */

54   public void setDomain(String JavaDoc domain) {
55     _domain = domain;
56   }
57
58   /**
59    * @param the name of the object to lookup.
60    */

61   public void setJndiName(String JavaDoc name) {
62     _name = name;
63   }
64
65   /**
66    * @see org.sapia.util.xml.confix.ObjectCreationCallback#onCreate()
67    */

68   public Object JavaDoc onCreate() throws ConfigurationException {
69     if (_port == 0) {
70       throw new ConfigurationException(
71         "'jndiPort' not set for Ubik naming service");
72     }
73
74     if (_host == null) {
75       throw new ConfigurationException(
76         "'jndiHost' not set for Ubik naming service");
77     }
78
79     if (_domain == null) {
80       throw new ConfigurationException(
81         "'domain' not set for Ubik naming service");
82     }
83
84     Properties JavaDoc props = new Properties JavaDoc();
85     props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
86       RemoteInitialContextFactory.class.getName());
87     props.setProperty(RemoteInitialContextFactory.UBIK_DOMAIN_NAME, _domain);
88     props.setProperty(Context.PROVIDER_URL, "ubik://" + _host + ":" + _port);
89
90     InitialContext JavaDoc ctx = null;
91
92     try {
93       ctx = new InitialContext JavaDoc(props);
94
95       return ctx.lookup(_name);
96     } catch (NamingException JavaDoc e) {
97       throw new ConfigurationException("Could not lookup " + _name, e);
98     } finally {
99       if (ctx != null) {
100         try {
101           ctx.close();
102         } catch (NamingException JavaDoc e) {
103           //noop;
104
}
105       }
106     }
107   }
108 }
109
Popular Tags