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 object from jndi to pv.
040: * Configuration:<BR/>
041: * Attribute: <code>jndi-name</code><BR>
042: * Nested elements: <code>environment-property</code> with attribute <code>name</code>.
043: * Example:<PRE><topicConnectionFactory type="biz.hammurapi.config.JndiWrapper" jndi-name="TopicConnectionFactory">
044:
045: <environment-property name="java.naming.factory.initial"><I>factory class</I></environment-property>
046:
047: <environment-property name="java.naming.provider.url"><I>provider urle</I></environment-property>
048:
049: </topicConnectionFactory>
050: </PRE>
051: * Environment properties are passed to the constructor of initial JNDI context.
052: * If <code>jndi-name</code> attribute is not set then this class returns InitialContext as its master.
053: *
054: * If jndi-name is not set or master object is instance of javax.naming.Context then this component acts as a JNDI bridge.
055: * @author Pavel Vlasov
056: * @revision $Revision$
057: */
058: public class JndiWrapper extends ComponentBase implements
059: DomConfigurable, Wrapper {
060:
061: private Hashtable ctxProps = new Hashtable();
062:
063: private Object master;
064:
065: private String name;
066:
067: public JndiWrapper() {
068: super ();
069: }
070:
071: /**
072: * Looks up master in JNDI.
073: */
074: public void start() throws ConfigurationException {
075: try {
076: InitialContext jndiContext = new InitialContext(ctxProps);
077: master = name == null ? jndiContext : jndiContext
078: .lookup(name);
079: } catch (NamingException e) {
080: throw new ConfigurationException(e);
081: }
082: }
083:
084: public void stop() throws ConfigurationException {
085: // Nothing
086: }
087:
088: public void configure(Node configNode, Context context)
089: throws ConfigurationException {
090: try {
091: NodeIterator nit = XPathAPI.selectNodeIterator(configNode,
092: "environment-property");
093: Element e;
094: while ((e = (Element) nit.nextNode()) != null) {
095: Object value = DOMUtils.getNonBlankElementText(e);
096: if (e.hasAttribute("type")) {
097: value = CompositeConverter.getDefaultConverter()
098: .convert(
099: value,
100: Class.forName(e
101: .getAttribute("type")),
102: false);
103: }
104: ctxProps.put(e.getAttribute("name"), value);
105: }
106:
107: Element ce = (Element) configNode;
108: if (ce.hasAttribute("jndi-name")) {
109: name = ce.getAttribute("jndi-name");
110: }
111: } catch (Exception e) {
112: throw new ConfigurationException(e);
113: }
114: }
115:
116: public Object getMaster() {
117: return master;
118: }
119:
120: protected Object getChild(String name) {
121: if (master instanceof javax.naming.Context) {
122: try {
123: return ((javax.naming.Context) master).lookup(name);
124: } catch (NamingException e) {
125: throw new RuntimeConfigurationException(
126: "Lookup failed: " + e, e);
127: }
128: }
129: return super.getChild(name);
130: }
131: }
|