001: // ========================================================================
002: // $Id: javaRootURLContext.java 887 2006-09-05 13:46:42Z janb $
003: // Copyright 1999-2006 Mort Bay Consulting Pty. Ltd.
004: // ------------------------------------------------------------------------
005: // Licensed under the Apache License, Version 2.0 (the "License");
006: // you may not use this file except in compliance with the License.
007: // You may obtain a copy of the License at
008: // http://www.apache.org/licenses/LICENSE-2.0
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014: // ========================================================================
015:
016: package org.mortbay.naming.java;
017:
018: import java.util.Hashtable;
019:
020: import javax.naming.Context;
021: import javax.naming.Name;
022: import javax.naming.NameParser;
023: import javax.naming.NamingEnumeration;
024: import javax.naming.NamingException;
025: import javax.naming.Reference;
026: import javax.naming.StringRefAddr;
027:
028: import org.mortbay.log.Log;
029: import org.mortbay.naming.ContextFactory;
030: import org.mortbay.naming.NamingContext;
031:
032: /** javaRootURLContext
033: * <p>This is the root of the java: url namespace
034: *
035: * <p><h4>Notes</h4>
036: * <p>Thanks to Rickard Oberg for the idea of binding an ObjectFactory at "comp".
037: *
038: * <p><h4>Usage</h4>
039: * <pre>
040: */
041: /*
042: * </pre>
043: *
044: * @see
045: *
046: * @author <a href="mailto:janb@mortbay.com">Jan Bartel</a>
047: * @version 1.0
048: */
049: public class javaRootURLContext implements Context {
050: public static final String URL_PREFIX = "java:";
051:
052: protected Hashtable _env;
053:
054: protected static NamingContext _nameRoot;
055:
056: protected static NameParser _javaNameParser;
057:
058: static {
059: try {
060: _javaNameParser = new javaNameParser();
061: _nameRoot = new NamingContext();
062: _nameRoot.setNameParser(_javaNameParser);
063:
064: StringRefAddr parserAddr = new StringRefAddr("parser",
065: _javaNameParser.getClass().getName());
066:
067: Reference ref = new Reference("javax.naming.Context",
068: parserAddr, ContextFactory.class.getName(),
069: (String) null);
070:
071: //bind special object factory at comp
072: _nameRoot.bind("comp", ref);
073: } catch (Exception e) {
074: Log.warn(e);
075: }
076: }
077:
078: /*------------------------------------------------*/
079: /**
080: * Creates a new <code>javaRootURLContext</code> instance.
081: *
082: * @param env a <code>Hashtable</code> value
083: */
084: public javaRootURLContext(Hashtable env) {
085: _env = env;
086: }
087:
088: public Object lookup(Name name) throws NamingException {
089: return getRoot().lookup(stripProtocol(name));
090: }
091:
092: public Object lookup(String name) throws NamingException {
093: return getRoot().lookup(stripProtocol(name));
094: }
095:
096: public void bind(Name name, Object obj) throws NamingException {
097: getRoot().bind(stripProtocol(name), obj);
098: }
099:
100: public void bind(String name, Object obj) throws NamingException {
101: getRoot().bind(stripProtocol(name), obj);
102: }
103:
104: public void unbind(String name) throws NamingException {
105: getRoot().unbind(stripProtocol(name));
106: }
107:
108: public void unbind(Name name) throws NamingException {
109: getRoot().unbind(stripProtocol(name));
110: }
111:
112: public void rename(String oldStr, String newStr)
113: throws NamingException {
114: getRoot().rename(stripProtocol(oldStr), stripProtocol(newStr));
115: }
116:
117: public void rename(Name oldName, Name newName)
118: throws NamingException {
119: getRoot()
120: .rename(stripProtocol(oldName), stripProtocol(newName));
121: }
122:
123: public void rebind(Name name, Object obj) throws NamingException {
124: getRoot().rebind(stripProtocol(name), obj);
125: }
126:
127: public void rebind(String name, Object obj) throws NamingException {
128: getRoot().rebind(stripProtocol(name), obj);
129: }
130:
131: public Object lookupLink(Name name) throws NamingException {
132: return getRoot().lookupLink(stripProtocol(name));
133: }
134:
135: public Object lookupLink(String name) throws NamingException {
136: return getRoot().lookupLink(stripProtocol(name));
137: }
138:
139: public Context createSubcontext(Name name) throws NamingException {
140: return getRoot().createSubcontext(stripProtocol(name));
141: }
142:
143: public Context createSubcontext(String name) throws NamingException {
144: return getRoot().createSubcontext(stripProtocol(name));
145: }
146:
147: public void destroySubcontext(Name name) throws NamingException {
148: getRoot().destroySubcontext(stripProtocol(name));
149: }
150:
151: public void destroySubcontext(String name) throws NamingException {
152: getRoot().destroySubcontext(stripProtocol(name));
153: }
154:
155: public NamingEnumeration list(Name name) throws NamingException {
156: return getRoot().list(stripProtocol(name));
157: }
158:
159: public NamingEnumeration list(String name) throws NamingException {
160: return getRoot().list(stripProtocol(name));
161: }
162:
163: public NamingEnumeration listBindings(Name name)
164: throws NamingException {
165: return getRoot().listBindings(stripProtocol(name));
166: }
167:
168: public NamingEnumeration listBindings(String name)
169: throws NamingException {
170: return getRoot().listBindings(stripProtocol(name));
171: }
172:
173: public Name composeName(Name name, Name prefix)
174: throws NamingException {
175: return getRoot().composeName(name, prefix);
176: }
177:
178: public String composeName(String name, String prefix)
179: throws NamingException {
180: return getRoot().composeName(name, prefix);
181: }
182:
183: public void close() throws NamingException {
184: }
185:
186: public String getNameInNamespace() throws NamingException {
187: return URL_PREFIX;
188: }
189:
190: public NameParser getNameParser(Name name) throws NamingException {
191: return _javaNameParser;
192: }
193:
194: public NameParser getNameParser(String name) throws NamingException {
195: return _javaNameParser;
196: }
197:
198: public Object addToEnvironment(String propName, Object propVal)
199: throws NamingException {
200: return _env.put(propName, propVal);
201: }
202:
203: public Object removeFromEnvironment(String propName)
204: throws NamingException {
205: return _env.remove(propName);
206: }
207:
208: public Hashtable getEnvironment() {
209: return _env;
210: }
211:
212: protected NamingContext getRoot() {
213: return _nameRoot;
214: }
215:
216: protected Name stripProtocol(Name name) throws NamingException {
217: if ((name != null) && (name.size() > 0)) {
218: String head = name.get(0);
219:
220: if (Log.isDebugEnabled())
221: Log.debug("Head element of name is: " + head);
222:
223: if (head.startsWith(URL_PREFIX)) {
224: head = head.substring(URL_PREFIX.length());
225: name.remove(0);
226: if (head.length() > 0)
227: name.add(0, head);
228:
229: if (Log.isDebugEnabled())
230: Log.debug("name modified to " + name.toString());
231: }
232: }
233:
234: return name;
235: }
236:
237: protected String stripProtocol(String name) {
238: String newName = name;
239:
240: if ((name != null) && (!name.equals(""))) {
241: if (name.startsWith(URL_PREFIX))
242: newName = name.substring(URL_PREFIX.length());
243: }
244:
245: return newName;
246: }
247:
248: }
|