1 // ========================================================================
2 // $Id: NamingEntry.java 3680 2008-09-21 10:37:13Z janb $
3 // Copyright 2006 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 // ========================================================================
15
16 package org.mortbay.jetty.plus.naming;
17
18 import javax.naming.Context;
19 import javax.naming.InitialContext;
20 import javax.naming.LinkRef;
21 import javax.naming.Name;
22 import javax.naming.NameParser;
23 import javax.naming.NamingException;
24
25 import org.mortbay.log.Log;
26 import org.mortbay.naming.NamingUtil;
27
28
29
30 /**
31 * NamingEntry
32 *
33 * Base class for all jndi related entities. Instances of
34 * subclasses of this class are declared in jetty.xml or in a
35 * webapp's WEB-INF/jetty-env.xml file.
36 *
37 * NOTE: that all NamingEntries will be bound in a single namespace.
38 * The "global" level is just in the top level context. The "local"
39 * level is a context specific to a webapp.
40 */
41 public abstract class NamingEntry
42 {
43 public static final String __contextName = "__"; //all NamingEntries stored in context called "__"
44 protected String jndiName; //the name representing the object associated with the NamingEntry
45 protected Object objectToBind; //the object associated with the NamingEntry
46 protected String namingEntryNameString; //the name of the NamingEntry relative to the context it is stored in
47 protected String objectNameString; //the name of the object relative to the context it is stored in
48
49
50
51
52
53 public NamingEntry (Object scope, String jndiName, Object object)
54 throws NamingException
55 {
56 this.jndiName = jndiName;
57 this.objectToBind = object;
58 save(scope);
59 }
60
61 /**
62 * Create a NamingEntry.
63 * A NamingEntry is a name associated with a value which can later
64 * be looked up in JNDI by a webapp.
65 *
66 * We create the NamingEntry and put it into JNDI where it can
67 * be linked to the webapp's env-entry, resource-ref etc entries.
68 *
69 * @param jndiName the name of the object which will eventually be in java:comp/env
70 * @param object the object to be bound
71 * @throws NamingException
72 */
73 public NamingEntry (String jndiName, Object object)
74 throws NamingException
75 {
76 this (null, jndiName, object);
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.jndiName=null;
126 this.namingEntryNameString=null;
127 this.objectNameString=null;
128 this.objectToBind=null;
129 }
130 catch (NamingException e)
131 {
132 Log.warn(e);
133 }
134 }
135
136 /**
137 * Get the unique name of the object
138 * @return
139 */
140 public String getJndiName ()
141 {
142 return this.jndiName;
143 }
144
145 /**
146 * Get the object that is to be bound
147 * @return
148 */
149 public Object getObjectToBind()
150 throws NamingException
151 {
152 return this.objectToBind;
153 }
154
155
156
157
158
159 /**
160 * Save the NamingEntry for later use.
161 *
162 * Saving is done by binding the NamingEntry
163 * itself, and the value it represents into
164 * JNDI. In this way, we can link to the
165 * value it represents later, but also
166 * still retrieve the NamingEntry itself too.
167 *
168 * The object is bound at the jndiName passed in.
169 * This NamingEntry is bound at __/jndiName.
170 *
171 * eg
172 *
173 * jdbc/foo : DataSource
174 * __/jdbc/foo : NamingEntry
175 *
176 * @throws NamingException
177 */
178 protected void save (Object scope)
179 throws NamingException
180 {
181 InitialContext ic = new InitialContext();
182 NameParser parser = ic.getNameParser("");
183 Name prefix = NamingEntryUtil.getNameForScope(scope);
184
185 //bind the NamingEntry into the context
186 Name namingEntryName = NamingEntryUtil.makeNamingEntryName(parser, getJndiName());
187 namingEntryName.addAll(0, prefix);
188 namingEntryNameString = namingEntryName.toString();
189 NamingUtil.bind(ic, namingEntryNameString, this);
190
191 //bind the object as well
192 Name objectName = parser.parse(getJndiName());
193 objectName.addAll(0, prefix);
194 objectNameString = objectName.toString();
195 NamingUtil.bind(ic, objectNameString, objectToBind);
196 }
197
198 }