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