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 ; 9 10 import javax.naming.Context ; 11 import javax.naming.InitialContext ; 12 import javax.naming.NamingException ; 13 14 15 25 public class Lookup implements ObjectCreationCallback { 26 private String _host; 27 private String _domain; 28 private String _name; 29 private int _port; 30 31 34 public Lookup() { 35 } 36 37 40 public void setJndiHost(String host) { 41 _host = host; 42 } 43 44 47 public void setJndiPort(int port) { 48 _port = port; 49 } 50 51 54 public void setDomain(String domain) { 55 _domain = domain; 56 } 57 58 61 public void setJndiName(String name) { 62 _name = name; 63 } 64 65 68 public Object 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 props = new Properties (); 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 ctx = null; 91 92 try { 93 ctx = new InitialContext (props); 94 95 return ctx.lookup(_name); 96 } catch (NamingException e) { 97 throw new ConfigurationException("Could not lookup " + _name, e); 98 } finally { 99 if (ctx != null) { 100 try { 101 ctx.close(); 102 } catch (NamingException e) { 103 } 105 } 106 } 107 } 108 } 109 | Popular Tags |