001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.naming;
030:
031: import com.caucho.util.L10N;
032: import com.caucho.vfs.Path;
033:
034: import javax.naming.*;
035: import java.io.IOException;
036: import java.util.Hashtable;
037:
038: public class PathJndiContext implements Context {
039: private static L10N L = new L10N(PathJndiContext.class);
040:
041: private PathJndiContext _root;
042: private Path _path;
043:
044: public PathJndiContext(Path path) {
045: _root = this ;
046: _path = path;
047: }
048:
049: PathJndiContext(Path path, PathJndiContext root) {
050: _root = root;
051: _path = path;
052: if (root == null)
053: _root = root;
054: }
055:
056: public Path getPath() {
057: return _path;
058: }
059:
060: public Object lookup(String name) throws NamingException {
061: if (name == null || name.equals(""))
062: return new PathJndiContext(_path.lookup(null), _root);
063:
064: Path subpath = _path.lookup(name);
065:
066: if (subpath == null) {
067: throw new NamingException(L.l("bad path {0}", name));
068: }
069:
070: if (subpath.isDirectory())
071: return new PathJndiContext(subpath, _root);
072:
073: else if (subpath.isObject()) {
074: try {
075: return subpath.getValue();
076: } catch (Exception e) {
077: throw new NamingException(e.toString());
078: }
079: }
080:
081: else if (!subpath.exists())
082: return null;
083:
084: else
085: throw new NamingException(L.l("lookup can't handle files"));
086: }
087:
088: public Object lookup(Name name) throws NamingException {
089: return lookup(name.toString());
090: }
091:
092: public void bind(String name, Object obj) throws NamingException {
093: Path subpath = _path.lookup(name);
094:
095: Path parent = subpath.getParent();
096: if (!parent.exists()) {
097: try {
098: parent.mkdirs();
099: } catch (IOException e) {
100: throw new NamingException(e.toString());
101: }
102: }
103:
104: if (!parent.isDirectory())
105: throw new NamingException(L.l(
106: "bind expects directory for `{0}'", subpath
107: .getParent()));
108: else if (subpath.exists())
109: throw new NameAlreadyBoundException(L.l(
110: "`{0}' already has a binding", subpath));
111:
112: try {
113: subpath.setValue(obj);
114: } catch (Exception e) {
115: throw new NamingException(e.toString());
116: }
117: }
118:
119: public void bind(Name name, Object obj) throws NamingException {
120: bind(name.toString(), obj);
121: }
122:
123: public void rebind(String name, Object obj) throws NamingException {
124: Path subpath = _path.lookup(name);
125:
126: try {
127: Path parent = subpath.getParent();
128: if (!parent.exists())
129: parent.mkdirs();
130: subpath.setValue(obj);
131: } catch (Exception e) {
132: throw new NamingException(e.toString());
133: }
134: }
135:
136: public void rebind(Name name, Object obj) throws NamingException {
137: rebind(name.toString(), obj);
138: }
139:
140: public void unbind(String name) throws NamingException {
141: Path subpath = _path.lookup(name);
142:
143: try {
144: subpath.remove();
145: } catch (IOException e) {
146: throw new NamingException(L.l("can't remove `{0}'"));
147: }
148: }
149:
150: public void unbind(Name name) throws NamingException {
151: unbind(name.toString());
152: }
153:
154: public void rename(String oldName, String newName)
155: throws NamingException {
156: Object obj = lookup(oldName);
157: bind(newName, obj);
158: unbind(oldName);
159: }
160:
161: public void rename(Name oldName, Name newName)
162: throws NamingException {
163: Object obj = lookup(oldName);
164: bind(newName, obj);
165: unbind(oldName);
166: }
167:
168: public NamingEnumeration list(String name) throws NamingException {
169: return null;
170: }
171:
172: public NamingEnumeration list(Name name) throws NamingException {
173: return list(name.toString());
174: }
175:
176: public NamingEnumeration listBindings(String name)
177: throws NamingException {
178: return null;
179: }
180:
181: public NamingEnumeration listBindings(Name name)
182: throws NamingException {
183: return list(name.toString());
184: }
185:
186: public void destroySubcontext(String name) throws NamingException {
187: Path subpath = _path.lookup(name);
188:
189: if (!subpath.exists())
190: throw new NameNotFoundException(name);
191: if (!subpath.isDirectory())
192: throw new NotContextException(name);
193:
194: try {
195: subpath.remove();
196: } catch (IOException e) {
197: throw new ContextNotEmptyException(name);
198: }
199: }
200:
201: public void destroySubcontext(Name name) throws NamingException {
202: destroySubcontext(name.toString());
203: }
204:
205: public Context createSubcontext(String name) throws NamingException {
206: Path subpath = _path.lookup(name);
207:
208: if (!subpath.getParent().isDirectory())
209: throw new NamingException(L.l(
210: "parent of `{0}' must be directory", name));
211:
212: try {
213: subpath.mkdir();
214: } catch (IOException e) {
215: throw new ContextNotEmptyException(name);
216: }
217:
218: return new PathJndiContext(subpath, _root);
219: }
220:
221: public Context createSubcontext(Name name) throws NamingException {
222: return createSubcontext(name.toString());
223: }
224:
225: public Object lookupLink(String name) throws NamingException {
226: throw new NamingException(L.l("links not supported"));
227: }
228:
229: public Object lookupLink(Name name) throws NamingException {
230: return lookupLink(name.toString());
231: }
232:
233: public NameParser getNameParser(String name) throws NamingException {
234: return new PathNameParser();
235: }
236:
237: public NameParser getNameParser(Name name) throws NamingException {
238: return getNameParser(name.toString());
239: }
240:
241: public String composeName(String suffix, String prefix)
242: throws NamingException {
243: return prefix + "/" + suffix;
244: }
245:
246: public Name composeName(Name suffix, Name prefix)
247: throws NamingException {
248: return null;
249: }
250:
251: public String getNameInNamespace() throws NamingException {
252: return _path.getPath();
253: }
254:
255: public Object addToEnvironment(String prop, Object value)
256: throws NamingException {
257: throw new UnsupportedOperationException();
258: /*
259: try {
260: Object old = _path.getAttribute(prop);
261: _path.setAttribute(prop, value);
262: return old;
263: } catch (IOException e) {
264: throw new NamingException(e.toString());
265: }
266: */
267: }
268:
269: public Object removeFromEnvironment(String prop)
270: throws NamingException {
271: throw new UnsupportedOperationException();
272: /*
273: try {
274: Object old = _path.getAttribute(prop);
275: _path.removeAttribute(prop);
276: return old;
277: } catch (IOException e) {
278: throw new NamingException(e.toString());
279: }
280: */
281: }
282:
283: public Hashtable getEnvironment() throws NamingException {
284: return null;
285: }
286:
287: public void close() throws NamingException {
288: }
289:
290: static class PathNameParser implements NameParser {
291: public Name parse(String name) throws NamingException {
292: return new CompositeName(name);
293: }
294: }
295: }
|