JndiWrapper.java

biz/hammurapi/config/JndiWrapper.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 089 Undocumented constructor 2 65:9
Java Inspector 089 Undocumented exception ConfigurationException 2 72:9
Java Inspector 089 Undocumented method 2 81:9
Java Inspector 089 Undocumented method 2 85:9
Java Inspector 089 Undocumented method 2 106:9
Java Inspector 089 Undocumented method 2 110:9
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 87:75
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 91:52
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 92:111
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 94:61
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 98:45
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 99:56
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 115:73
Java Inspector 040 Parameter name name clashes with field name in JndiWrapper 3 110:35

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.config;
24
25import java.util.Hashtable;
26
27import javax.naming.InitialContext;
28import javax.naming.NamingException;
29
30import org.w3c.dom.Element;
31import org.w3c.dom.Node;
32import org.w3c.dom.NodeList;
33
34import biz.hammurapi.convert.ConvertingService;
35import biz.hammurapi.xml.dom.DOMUtils;
36
37/**
38 * Binds object from jndi to pv.
39 * Configuration:<BR/>
40 * Attribute: <code>jndi-name</code><BR>
41 * Nested elements: <code>environment-property</code> with attribute <code>name</code>.
42 * Example:<PRE>&lt;topicConnectionFactory type="biz.hammurapi.config.JndiWrapper" jndi-name="TopicConnectionFactory"&gt;
43
44 &nbsp;&nbsp;&nbsp;&nbsp;&lt;environment-property name="java.naming.factory.initial"&gt;<I>factory class</I>&lt;/environment-property&gt;
45
46 &nbsp;&nbsp;&nbsp;&nbsp;&lt;environment-property name="java.naming.provider.url"&gt;<I>provider urle</I>&lt;/environment-property&gt;
47
48 &lt;/topicConnectionFactory&gt;
49 </PRE>
50 * Environment properties are passed to the constructor of initial JNDI context.
51 * If <code>jndi-name</code> attribute is not set then this class returns InitialContext as its master.
52 *
53 * If jndi-name is not set or master object is instance of javax.naming.Context then this component acts as a JNDI bridge.
54 * @author Pavel Vlasov
55 * @revision $Revision$
56 */
57public class JndiWrapper extends ComponentBase implements DomConfigurable, Wrapper {
58
59 private Hashtable ctxProps = new Hashtable();
60
61 private Object master;
62
63 private String name;
64
65 public JndiWrapper() {
66 super();
67 }
68
69 /**
70 * Looks up master in JNDI.
71 */
72 public void start() throws ConfigurationException {
73 try {
74 InitialContext jndiContext = new InitialContext(ctxProps);
75 master = name == null ? jndiContext : jndiContext.lookup(name);
76 } catch (NamingException e) {
77 throw new ConfigurationException(e);
78 }
79 }
80
81 public void stop() throws ConfigurationException {
82 // Nothing
83 }
84
85 public void configure(Node configNode, Context context, ClassLoader classLoader) throws ConfigurationException {
86 try {
87 NodeList nl = DOMUtils.selectNodeList(configNode, "environment-property");
88 for (int i=0, l=nl.getLength(); i<l; ++i) {
89 Element e = (Element) nl.item(i);
90 Object value = DOMUtils.getNonBlankElementText(e);
91 if (e.hasAttribute("type")) {
92 value = ConvertingService.convert(value, Class.forName(e.getAttribute("type")));
93 }
94 ctxProps.put(e.getAttribute("name"), value);
95 }
96
97 Element ce = (Element) configNode;
98 if (ce.hasAttribute("jndi-name")) {
99 name = ce.getAttribute("jndi-name");
100 }
101 } catch (Exception e) {
102 throw new ConfigurationException(e);
103 }
104 }
105
106 public Object getMaster() {
107 return master;
108 }
109
110 protected Object getChild(String name) {
111 if (master instanceof javax.naming.Context) {
112 try {
113 return ((javax.naming.Context) master).lookup(name);
114 } catch (NamingException e) {
115 throw new RuntimeConfigurationException("Lookup failed: "+e, e);
116 }
117 }
118 return super.getChild(name);
119 }
120}
121