001 /* 002 @license.text@ 003 */ 004 package biz.hammurapi.config; 005 006 import java.util.Hashtable; 007 008 import javax.naming.InitialContext; 009 import javax.naming.NamingException; 010 011 import org.w3c.dom.Element; 012 import org.w3c.dom.Node; 013 import org.w3c.dom.NodeList; 014 015 import biz.hammurapi.convert.ConvertingService; 016 import biz.hammurapi.xml.dom.DOMUtils; 017 018 /** 019 * Binds to jndi context and bridges pv-naming with jndi naming. 020 * 021 * @author Pavel Vlasov 022 * @revision $Revision$ 023 */ 024 public class JndiBridge extends ComponentBase implements DomConfigurable { 025 private Hashtable ctxProps = new Hashtable(); 026 027 private javax.naming.Context master; 028 029 private String name; 030 031 public JndiBridge() { 032 super(); 033 } 034 035 public void start() throws ConfigurationException { 036 try { 037 InitialContext jndiContext = new InitialContext(ctxProps); 038 master = (javax.naming.Context) (name == null ? jndiContext : jndiContext.lookup(name)); 039 } catch (NamingException e) { 040 throw new ConfigurationException(e); 041 } 042 } 043 044 public void stop() throws ConfigurationException { 045 if (master != null) { 046 try { 047 master.close(); 048 } catch (NamingException e) { 049 throw new ConfigurationException(e); 050 } 051 } 052 } 053 054 protected Object getChild(String name) { 055 try { 056 return master.lookup(name); 057 } catch (NamingException e) { 058 throw new RuntimeConfigurationException("Lookup failed for '" + name + "' " + e, e); 059 } 060 } 061 062 public void configure(Node configNode, Context context, ClassLoader classLoader) throws ConfigurationException { 063 try { 064 NodeList nl = DOMUtils.selectNodeList(configNode, "environment-property"); 065 for (int i=0, l=nl.getLength(); i<l; ++i) { 066 Element e = (Element) nl.item(i); 067 Object value = DOMUtils.getNonBlankElementText(e); 068 if (e.hasAttribute("type")) { 069 value = ConvertingService.convert(value, Class.forName(e.getAttribute("type"))); 070 } 071 ctxProps.put(e.getAttribute("name"), value); 072 } 073 Element ce = (Element) configNode; 074 if (ce.hasAttribute("jndi-name")) { 075 name = ce.getAttribute("jndi-name"); 076 } 077 } catch (Exception e) { 078 throw new ConfigurationException(e); 079 } 080 } 081 082 }