KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > DefaultSettingsContext


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.loaders;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import javax.naming.*;
26 import org.openide.filesystems.FileObject;
27
28 /** JNDI context providing DataObject's settings. The context does not support
29  * subcontexts.
30  *
31  * @author Jan Pokorsky
32  */

33 final class DefaultSettingsContext implements Context, NameParser {
34
35     private final DataObject dobj;
36     private final Hashtable JavaDoc env;
37
38     /** Creates a new instance of DefaultSettingsContext */
39     public DefaultSettingsContext(DataObject dobj) {
40         this.dobj = dobj;
41         env = new Hashtable JavaDoc();
42     }
43     
44     public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal) throws NamingException {
45         return null;
46     }
47     
48     public void bind(Name name, Object JavaDoc obj) throws NamingException {
49         String JavaDoc attrName = getRelativeName(name);
50         FileObject fo = dobj.getPrimaryFile();
51         Object JavaDoc attrVal = fo.getAttribute(attrName);
52         if (attrVal != null) {
53             throw new NameAlreadyBoundException(attrName + " = " + attrVal); // NOI18N
54
}
55         
56         try {
57             fo.setAttribute(attrName, obj);
58         } catch (IOException JavaDoc ex) {
59             NamingException ne = new NamingException(attrName + " = " + obj); // NOI18N
60
ne.setRootCause(ex);
61         }
62     }
63     
64     public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException {
65         bind(parse(name), obj);
66     }
67     
68     public void close() throws NamingException {
69     }
70     
71     /** Unsupported. */
72     public Name composeName(Name name, Name prefix) throws NamingException {
73         throw new OperationNotSupportedException();
74     }
75     
76     /** Unsupported. */
77     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix) throws NamingException {
78         throw new OperationNotSupportedException();
79     }
80     
81     /** Unsupported. */
82     public Context createSubcontext(Name name) throws NamingException {
83         throw new OperationNotSupportedException();
84     }
85     
86     /** Unsupported. */
87     public Context createSubcontext(String JavaDoc name) throws NamingException {
88         throw new OperationNotSupportedException();
89     }
90     
91     /** Unsupported. */
92     public void destroySubcontext(Name name) throws NamingException {
93         throw new OperationNotSupportedException();
94     }
95     
96     /** Unsupported. */
97     public void destroySubcontext(String JavaDoc name) throws NamingException {
98         throw new OperationNotSupportedException();
99     }
100     
101     public Hashtable JavaDoc getEnvironment() throws NamingException {
102         return env;
103     }
104     
105     public String JavaDoc getNameInNamespace() throws NamingException {
106         // it does not seem a better name is necessary
107
return "."; //NOI18N
108
}
109     
110     public NameParser getNameParser(Name name) throws NamingException {
111         return this;
112     }
113     
114     public NameParser getNameParser(String JavaDoc name) throws NamingException {
115         return this;
116     }
117     
118     public NamingEnumeration/*<NameClassPair>*/ list(String JavaDoc name) throws NamingException {
119         return list(parse(name));
120     }
121     
122     public NamingEnumeration/*<NameClassPair>*/ list(Name name) throws NamingException {
123         if (name == null) throw new InvalidNameException("name cannot be null"); // NOI18N
124

125         int size = name.size();
126         if (size == 0) throw new InvalidNameException("name cannot be empty"); // NOI18N
127
if (size > 1 || !".".equals(name.get(0))) {
128             throw new InvalidNameException("subcontexts unsupported: " + name); // NOI18N
129
}
130         
131         return new BindingEnumeration(dobj.getPrimaryFile());
132     }
133     
134     public NamingEnumeration<Binding> listBindings(Name name) throws NamingException {
135         return list(name);
136     }
137     
138     public NamingEnumeration<Binding> listBindings(String JavaDoc name) throws NamingException {
139         return list(name);
140     }
141     
142     public Object JavaDoc lookup(String JavaDoc name) throws NamingException {
143         return lookup(parse(name));
144     }
145     
146     public Object JavaDoc lookup(Name name) throws NamingException {
147         String JavaDoc attrName = getRelativeName(name);
148         return dobj.getPrimaryFile().getAttribute(attrName);
149     }
150     
151     public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException {
152         return lookupLink(parse(name));
153     }
154     
155     public Object JavaDoc lookupLink(Name name) throws NamingException {
156         return lookup(name);
157     }
158     
159     public void rebind(Name name, Object JavaDoc obj) throws NamingException {
160         String JavaDoc attrName = getRelativeName(name);
161         FileObject fo = dobj.getPrimaryFile();
162         
163         try {
164             fo.setAttribute(attrName, obj);
165         } catch (IOException JavaDoc ex) {
166             NamingException ne = new NamingException(name + " = " + obj); // NOI18N
167
ne.setRootCause(ex);
168         }
169     }
170     
171     public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException {
172         rebind(parse(name), obj);
173     }
174     
175     public Object JavaDoc removeFromEnvironment(String JavaDoc propName) throws NamingException {
176         return null;
177     }
178     
179     public void rename(Name oldName, Name newName) throws NamingException {
180         String JavaDoc oldAttrName = getRelativeName(oldName);
181         String JavaDoc newAttrName = getRelativeName(newName);
182         FileObject fo = dobj.getPrimaryFile();
183         Object JavaDoc attrVal = fo.getAttribute(newAttrName);
184         
185         if (attrVal != null) {
186             throw new NameAlreadyBoundException(newAttrName + " = " + attrVal); // NOI18N
187
}
188         
189         try {
190             attrVal = fo.getAttribute(oldAttrName);
191             fo.setAttribute(newAttrName, attrVal);
192             fo.setAttribute(oldAttrName, null);
193         } catch (IOException JavaDoc ex) {
194             NamingException ne = new NamingException(oldName + "->" + newName); // NOI18N
195
ne.setRootCause(ex);
196         }
197     }
198     
199     public void rename(String JavaDoc oldName, String JavaDoc newName) throws NamingException {
200         rename(parse(oldName), parse(newName));
201     }
202     
203     public void unbind(String JavaDoc name) throws NamingException {
204         unbind(parse(name));
205     }
206     
207     public void unbind(Name name) throws NamingException {
208         String JavaDoc attrName = getRelativeName(name);
209         FileObject fo = dobj.getPrimaryFile();
210         
211         if (fo.getAttribute(attrName) == null) {
212             NamingException ne = new NameNotFoundException();
213             ne.setResolvedName(name);
214             throw ne;
215         }
216         
217         try {
218             fo.setAttribute(attrName, null);
219         } catch (IOException JavaDoc ex) {
220             NamingException ne = new NamingException(); // NOI18N
221
ne.setResolvedName(name);
222             ne.setRootCause(ex);
223         }
224     }
225     
226     // NameParser impl //////////////////////////////////////////////////////
227

228     public Name parse(String JavaDoc name) throws NamingException {
229         if (name == null) throw new InvalidNameException("name cannot be null"); //NOI18N
230

231         return new CompositeName(name);
232     }
233     
234     /** validates name and returns its relative value */
235     private String JavaDoc getRelativeName(Name name) throws NamingException {
236         if (name == null) throw new InvalidNameException("name cannot be null"); // NOI18N
237
if (name.isEmpty()) throw new InvalidNameException("name cannot be empty"); // NOI18N
238

239         String JavaDoc rel = null;
240         Enumeration JavaDoc en = name.getAll();
241         while (en.hasMoreElements()) {
242             if (rel == null) {
243                 String JavaDoc item = (String JavaDoc) en.nextElement();
244                 if (".".equals(item)) continue; // NOI18N
245
if ("..".equals(item)) { // NOI18N
246
throw new InvalidNameException("subcontexts unsupported: " + name); // NOI18N
247
}
248                 
249                 rel = item;
250             } else {
251                 throw new InvalidNameException("subcontexts unsupported: " + name); // NOI18N
252
}
253         }
254         return rel;
255     }
256     
257     public String JavaDoc toString() {
258         String JavaDoc retValue = super.toString();
259         return retValue + '[' + dobj + ']';
260     }
261     
262     /** Enumerates file attributes as jndi bindings.
263      */

264     private static final class BindingEnumeration implements NamingEnumeration<Binding> {
265         
266         private final Enumeration JavaDoc<String JavaDoc> en;
267         private final FileObject fo;
268         
269         public BindingEnumeration(FileObject fo) {
270             this.fo = fo;
271             this.en = fo.getAttributes();
272         }
273         
274         public void close() throws NamingException {
275         }
276         
277         public boolean hasMore() throws NamingException {
278             return hasMoreElements();
279         }
280         
281         public boolean hasMoreElements() {
282             return en.hasMoreElements();
283         }
284         
285         public Binding next() throws NamingException {
286             return nextElement();
287         }
288         
289         public Binding nextElement() {
290             String JavaDoc name = en.nextElement();
291             Object JavaDoc val = fo.getAttribute(name);
292             return new Binding(name, val);
293         }
294         
295     }
296     
297 }
298
Popular Tags