KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > vfs > JndiPath


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.vfs;
30
31 import com.caucho.util.L10N;
32 import com.caucho.util.Log;
33
34 import javax.naming.Context JavaDoc;
35 import javax.naming.InitialContext JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40
41 /**
42  * Adapts the JNDI to the Path API. The name separator is always '/'.
43  *
44  * @since Resin 1.2
45  */

46 public class JndiPath extends FilesystemPath {
47   protected static final Logger JavaDoc log = Log.open(JndiPath.class);
48   protected static final L10N L = new L10N(JndiPath.class);
49
50   private Context JavaDoc parent;
51
52   /**
53    * Creates a new JndiPath root.
54    */

55   public JndiPath()
56   {
57     super(null, "/", "/");
58
59     _root = this;
60   }
61
62   /**
63    * Create a new JndiPath with the given name.
64    */

65   protected JndiPath(FilesystemPath root, String JavaDoc userPath, String JavaDoc path)
66   {
67     super(root, userPath, path);
68   }
69
70   /**
71    * Walking down the path just stores the new name in the created Path.
72    *
73    * @param userPath the string used in the <code>lookup</code> call.
74    * @param attributes any inherited attributes.
75    * @param path the normalized slash-separated path.
76    * @return a new JndiPath representing the new path.
77    */

78   public Path fsWalk(String JavaDoc userPath,
79              Map JavaDoc<String JavaDoc,Object JavaDoc> attributes,
80              String JavaDoc path)
81   {
82     return new JndiPath(_root, userPath, path);
83   }
84
85   /**
86    * The scheme is always "jndi:".
87    */

88   public String JavaDoc getScheme()
89   {
90     return "jndi";
91   }
92
93   /**
94    * Create a new subcontext
95    */

96   public boolean mkdir()
97     throws IOException JavaDoc
98   {
99     try {
100       Context JavaDoc parent = getAllButLast(getPath());
101
102       parent.createSubcontext(getTail());
103
104       return true;
105     } catch (Exception JavaDoc e) {
106       throw new IOExceptionWrapper(e);
107     }
108   }
109   
110   /**
111    * Returns the object bound at this path.
112    */

113   public Object JavaDoc getObject()
114     throws IOException JavaDoc
115   {
116     try {
117       Context JavaDoc parent = getAllButLast(getPath());
118
119       return parent.lookup(getTail());
120     } catch (Exception JavaDoc e) {
121       throw new IOExceptionWrapper(e);
122     }
123   }
124   
125   /**
126    * Sets the object bound at this path.
127    *
128    * @param value the new value
129    */

130   public void setObject(Object JavaDoc value)
131     throws IOException JavaDoc
132   {
133     try {
134       Context JavaDoc parent = getAllButLast(getPath());
135
136       parent.rebind(getTail(), value);
137     } catch (Exception JavaDoc e) {
138       e.printStackTrace();
139       throw new IOExceptionWrapper(e);
140     }
141   }
142
143   /**
144    * Returns the context found by looking up all but the last segment
145    * of the path.
146    *
147    * @param path slash-separated path
148    * @return context of the parent path
149    */

150   private Context JavaDoc getAllButLast(String JavaDoc path)
151     throws NamingException JavaDoc
152   {
153     if (parent != null)
154       return parent;
155     
156     Context JavaDoc context = new InitialContext JavaDoc();
157
158     int head = 1;
159     int tail;
160
161     while ((tail = path.indexOf('/', head)) > 0) {
162       String JavaDoc section = path.substring(head, tail);
163
164       if (context == null)
165         throw new NamingException JavaDoc(L.l("null context for `{0}'", path));
166       
167       context = (Context JavaDoc) context.lookup(section);
168
169       head = tail + 1;
170     }
171
172     parent = context;
173
174     return parent;
175   }
176 }
177
Popular Tags