1 //
2 // ========================================================================
3 // Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19 package org.eclipse.jetty.plus.jndi;
20
21 import javax.naming.Context;
22 import javax.naming.InitialContext;
23 import javax.naming.LinkRef;
24 import javax.naming.Name;
25 import javax.naming.NameParser;
26 import javax.naming.NamingException;
27
28 import org.eclipse.jetty.jndi.NamingUtil;
29 import org.eclipse.jetty.util.log.Logger;
30
31
32
33 /**
34 * NamingEntry
35 *
36 * Base class for all jndi related entities. Instances of
37 * subclasses of this class are declared in jetty.xml or in a
38 * webapp's WEB-INF/jetty-env.xml file.
39 *
40 * NOTE: that all NamingEntries will be bound in a single namespace.
41 * The "global" level is just in the top level context. The "local"
42 * level is a context specific to a webapp.
43 */
44 public abstract class NamingEntry
45 {
46 private static Logger __log = NamingUtil.__log;
47 public static final String __contextName = "__"; //all NamingEntries stored in context called "__"
48 protected final Object _scope;
49 protected final String _jndiName; //the name representing the object associated with the NamingEntry
50 protected String _namingEntryNameString; //the name of the NamingEntry relative to the context it is stored in
51 protected String _objectNameString; //the name of the object relative to the context it is stored in
52
53
54 public String toString()
55 {
56 return _jndiName;
57 }
58
59
60 protected NamingEntry (Object scope, String jndiName)
61 throws NamingException
62 {
63 this._scope=scope;
64 this._jndiName = jndiName;
65 }
66
67 /**
68 * Create a NamingEntry.
69 * A NamingEntry is a name associated with a value which can later
70 * be looked up in JNDI by a webapp.
71 *
72 * We create the NamingEntry and put it into JNDI where it can
73 * be linked to the webapp's env-entry, resource-ref etc entries.
74 *
75 * @param jndiName the name of the object which will eventually be in java:comp/env
76 * @throws NamingException
77 */
78 protected NamingEntry (String jndiName)
79 throws NamingException
80 {
81 this (null, jndiName);
82 }
83
84
85
86
87 /**
88 * Add a java:comp/env binding for the object represented by this NamingEntry,
89 * but bind it as the name supplied
90 * @throws NamingException
91 */
92 public void bindToENC(String localName)
93 throws NamingException
94 {
95 //TODO - check on the whole overriding/non-overriding thing
96 InitialContext ic = new InitialContext();
97 Context env = (Context)ic.lookup("java:comp/env");
98 __log.debug("Binding java:comp/env/"+localName+" to "+_objectNameString);
99 NamingUtil.bind(env, localName, new LinkRef(_objectNameString));
100 }
101
102 /**
103 * Unbind this NamingEntry from a java:comp/env
104 */
105 public void unbindENC ()
106 {
107 try
108 {
109 InitialContext ic = new InitialContext();
110 Context env = (Context)ic.lookup("java:comp/env");
111 __log.debug("Unbinding java:comp/env/"+getJndiName());
112 env.unbind(getJndiName());
113 }
114 catch (NamingException e)
115 {
116 __log.warn(e);
117 }
118 }
119
120 /**
121 * Unbind this NamingEntry entirely
122 */
123 public void release ()
124 {
125 try
126 {
127 InitialContext ic = new InitialContext();
128 ic.unbind(_objectNameString);
129 ic.unbind(_namingEntryNameString);
130 this._namingEntryNameString=null;
131 this._objectNameString=null;
132 }
133 catch (NamingException e)
134 {
135 __log.warn(e);
136 }
137 }
138
139 /**
140 * Get the unique name of the object
141 * relative to the scope
142 * @return the unique jndi name of the object
143 */
144 public String getJndiName ()
145 {
146 return _jndiName;
147 }
148
149 /**
150 * Get the name of the object, fully
151 * qualified with the scope
152 * @return the name of the object, fully qualified with the scope
153 */
154 public String getJndiNameInScope ()
155 {
156 return _objectNameString;
157 }
158
159
160
161 /**
162 * Save the NamingEntry for later use.
163 *
164 * Saving is done by binding the NamingEntry
165 * itself, and the value it represents into
166 * JNDI. In this way, we can link to the
167 * value it represents later, but also
168 * still retrieve the NamingEntry itself too.
169 *
170 * The object is bound at the jndiName passed in.
171 * This NamingEntry is bound at __/jndiName.
172 *
173 * eg
174 *
175 * jdbc/foo : DataSource
176 * __/jdbc/foo : NamingEntry
177 *
178 * @throws NamingException
179 */
180 protected void save (Object object)
181 throws NamingException
182 {
183 __log.debug("SAVE {} in {}",this,_scope);
184 InitialContext ic = new InitialContext();
185 NameParser parser = ic.getNameParser("");
186 Name prefix = NamingEntryUtil.getNameForScope(_scope);
187
188 //bind the NamingEntry into the context
189 Name namingEntryName = NamingEntryUtil.makeNamingEntryName(parser, getJndiName());
190 namingEntryName.addAll(0, prefix);
191 _namingEntryNameString = namingEntryName.toString();
192 NamingUtil.bind(ic, _namingEntryNameString, this);
193
194 //bind the object as well
195 Name objectName = parser.parse(getJndiName());
196 objectName.addAll(0, prefix);
197 _objectNameString = objectName.toString();
198 NamingUtil.bind(ic, _objectNameString, object);
199 }
200
201 }