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