001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.config;
024:
025: import java.util.Hashtable;
026:
027: import javax.naming.InitialContext;
028: import javax.naming.NamingException;
029:
030: import org.apache.xpath.XPathAPI;
031: import org.w3c.dom.Element;
032: import org.w3c.dom.Node;
033: import org.w3c.dom.traversal.NodeIterator;
034:
035: import biz.hammurapi.convert.CompositeConverter;
036: import biz.hammurapi.xml.dom.DOMUtils;
037:
038: /**
039: * Binds to jndi context and bridges pv-naming with jndi naming.
040: *
041: * @author Pavel Vlasov
042: * @revision $Revision$
043: */
044: public class JndiBridge extends ComponentBase implements
045: DomConfigurable {
046: private Hashtable ctxProps = new Hashtable();
047:
048: private javax.naming.Context master;
049:
050: private String name;
051:
052: public JndiBridge() {
053: super ();
054: }
055:
056: public void start() throws ConfigurationException {
057: try {
058: InitialContext jndiContext = new InitialContext(ctxProps);
059: master = (javax.naming.Context) (name == null ? jndiContext
060: : jndiContext.lookup(name));
061: } catch (NamingException e) {
062: throw new ConfigurationException(e);
063: }
064: }
065:
066: public void stop() throws ConfigurationException {
067: if (master != null) {
068: try {
069: master.close();
070: } catch (NamingException e) {
071: throw new ConfigurationException(e);
072: }
073: }
074: }
075:
076: protected Object getChild(String name) {
077: try {
078: return master.lookup(name);
079: } catch (NamingException e) {
080: throw new RuntimeConfigurationException(
081: "Lookup failed for '" + name + "' " + e, e);
082: }
083: }
084:
085: public void configure(Node configNode, Context context)
086: throws ConfigurationException {
087: try {
088: NodeIterator nit = XPathAPI.selectNodeIterator(configNode,
089: "environment-property");
090: Element e;
091: while ((e = (Element) nit.nextNode()) != null) {
092: Object value = DOMUtils.getNonBlankElementText(e);
093: if (e.hasAttribute("type")) {
094: value = CompositeConverter.getDefaultConverter()
095: .convert(
096: value,
097: Class.forName(e
098: .getAttribute("type")),
099: false);
100: }
101: ctxProps.put(e.getAttribute("name"), value);
102: }
103: Element ce = (Element) configNode;
104: if (ce.hasAttribute("jndi-name")) {
105: name = ce.getAttribute("jndi-name");
106: }
107: } catch (Exception e) {
108: throw new ConfigurationException(e);
109: }
110: }
111:
112: }
|