KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > repository > RepositoryContext


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.repository;
24
25 import java.util.*;
26 import java.io.*;
27 import javax.naming.*;
28 import java.security.*;
29 import com.sun.enterprise.util.FileUtil;
30 // IASRI 4660742 START
31
import java.util.logging.*;
32 import com.sun.logging.*;
33 // IASRI 4660742 END
34

35 /**
36  * Repository Context
37  * @author Harish Prabandham
38  */

39 class RepositoryContext implements Context {
40     /** This environment property specifies the path of the file
41      * that stores all the contents of the repository.
42      */

43 // IASRI 4660742 START
44
private static Logger _logger=null;
45     static{
46        _logger=LogDomains.getLogger(LogDomains.ROOT_LOGGER);
47         }
48 // IASRI 4660742 END
49
public static final String JavaDoc
50     REPOSITORY_NAME = "com.sun.enterprise.repository.name";
51     public static final String JavaDoc
52     REPOSITORY_DIR = "com.sun.enterprise.repository.dir";
53
54     Hashtable myEnv;
55     private Properties bindings;
56     private static final boolean debug = false;
57     static NameParser myParser = new RepositoryNameParser();
58     
59     RepositoryContext(Hashtable environment) {
60         myEnv = (environment != null)
61         ? (Hashtable)(environment.clone())
62         : null;
63         resurrectTable();
64     }
65
66     // New API for JNDI 1.2
67
public String JavaDoc getNameInNamespace() throws NamingException
68     {
69     throw new OperationNotSupportedException("Context.getNameInNamespace() not implemented");
70     }
71
72     public Object JavaDoc lookup(String JavaDoc name) throws NamingException {
73     if (name.equals("")) {
74             // Asking to look up this context itself. Create and return
75
// a new instance with its own independent environment.
76
return (new RepositoryContext(myEnv));
77         }
78         // System.out.println("BINDINGS :" + bindings);
79
// START OF IASRI 4660742
80
//_logger.log(Level.FINE,"BINDINGS :" + bindings);
81
// END OF IASRI 4660742
82
Object JavaDoc answer = bindings.get(name);
83         if (answer == null) {
84             throw new NameNotFoundException(name + " not found");
85         }
86         return answer;
87     }
88
89     public Object JavaDoc lookup(Name name) throws NamingException {
90         // Flat namespace; no federation; just call string version
91
return lookup(name.toString());
92     }
93
94     public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException {
95         if (name.equals("")) {
96             throw new InvalidNameException("Cannot bind empty name");
97         }
98         if (bindings.get(name) != null) {
99             throw new NameAlreadyBoundException(
100                     "Use rebind to override");
101         }
102         bindings.put(name, obj);
103         store();
104     }
105
106     public void bind(Name name, Object JavaDoc obj) throws NamingException {
107         // Flat namespace; no federation; just call string version
108
bind(name.toString(), obj);
109     }
110
111     public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException {
112         if (name.equals("")) {
113             throw new InvalidNameException("Cannot bind empty name");
114         }
115         bindings.put(name, obj);
116         store();
117     }
118
119     public void rebind(Name name, Object JavaDoc obj) throws NamingException {
120         // Flat namespace; no federation; just call string version
121
rebind(name.toString(), obj);
122     }
123
124     public void unbind(String JavaDoc name) throws NamingException {
125         if (name.equals("")) {
126             throw new InvalidNameException("Cannot unbind empty name");
127         }
128         bindings.remove(name);
129         store();
130     }
131
132     public void unbind(Name name) throws NamingException {
133         // Flat namespace; no federation; just call string version
134
unbind(name.toString());
135     }
136
137     public void rename(String JavaDoc oldname, String JavaDoc newname)
138             throws NamingException {
139         if (oldname.equals("") || newname.equals("")) {
140             throw new InvalidNameException("Cannot rename empty name");
141         }
142
143         // Check if new name exists
144
if (bindings.get(newname) != null) {
145             throw new NameAlreadyBoundException(newname +
146                                                 " is already bound");
147         }
148
149         // Check if old name is bound
150
Object JavaDoc oldBinding = bindings.remove(oldname);
151         if (oldBinding == null) {
152             throw new NameNotFoundException(oldname + " not bound");
153         }
154
155         bindings.put(newname, oldBinding);
156         store();
157     }
158
159     public void rename(Name oldname, Name newname)
160             throws NamingException {
161         // Flat namespace; no federation; just call string version
162
rename(oldname.toString(), newname.toString());
163     }
164
165     public NamingEnumeration list(String JavaDoc name)
166             throws NamingException {
167         if (name.equals("")) {
168             // listing this context
169
return new RepNames(bindings.keys());
170         }
171
172         // Perhaps 'name' names a context
173
Object JavaDoc target = lookup(name);
174         if (target instanceof Context) {
175             return ((Context)target).list("");
176         }
177         throw new NotContextException(name + " cannot be listed");
178     }
179
180     public NamingEnumeration list(Name name)
181             throws NamingException {
182         // Flat namespace; no federation; just call string version
183
return list(name.toString());
184     }
185
186     public NamingEnumeration listBindings(String JavaDoc name)
187             throws NamingException {
188         if (name.equals("")) {
189             // listing this context
190
return new RepBindings(bindings.keys());
191         }
192
193         // Perhaps 'name' names a context
194
Object JavaDoc target = lookup(name);
195         if (target instanceof Context) {
196             return ((Context)target).listBindings("");
197         }
198         throw new NotContextException(name + " cannot be listed");
199     }
200
201     public NamingEnumeration listBindings(Name name)
202             throws NamingException {
203         // Flat namespace; no federation; just call string version
204
return listBindings(name.toString());
205     }
206
207     public void destroySubcontext(String JavaDoc name) throws NamingException {
208         throw new OperationNotSupportedException(
209                 "RepositoryContext does not support subcontexts");
210     }
211
212     public void destroySubcontext(Name name) throws NamingException {
213         // Flat namespace; no federation; just call string version
214
destroySubcontext(name.toString());
215     }
216
217     public Context createSubcontext(String JavaDoc name)
218             throws NamingException {
219         throw new OperationNotSupportedException(
220                 "RepositoryContext does not support subcontexts");
221     }
222
223     public Context createSubcontext(Name name) throws NamingException {
224         // Flat namespace; no federation; just call string version
225
return createSubcontext(name.toString());
226     }
227
228     public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException {
229         // This flat context does not treat links specially
230
return lookup(name);
231     }
232
233     public Object JavaDoc lookupLink(Name name) throws NamingException {
234         // Flat namespace; no federation; just call string version
235
return lookupLink(name.toString());
236     }
237
238     public NameParser getNameParser(String JavaDoc name)
239             throws NamingException {
240         return myParser;
241     }
242
243     public NameParser getNameParser(Name name) throws NamingException {
244         // Flat namespace; no federation; just call string version
245
return getNameParser(name.toString());
246     }
247
248     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix)
249             throws NamingException {
250         Name result = composeName(new CompositeName(name),
251                                   new CompositeName(prefix));
252         return result.toString();
253     }
254
255     public Name composeName(Name name, Name prefix)
256             throws NamingException {
257         Name result = (Name)(prefix.clone());
258         result.addAll(name);
259         return result;
260     }
261
262     public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal)
263             throws NamingException {
264         if (myEnv == null) {
265             myEnv = new Hashtable(5, 0.75f);
266     }
267     return myEnv.put(propName, propVal);
268     }
269
270     public Object JavaDoc removeFromEnvironment(String JavaDoc propName)
271             throws NamingException {
272         if (myEnv == null)
273             return null;
274
275     return myEnv.remove(propName);
276     }
277
278     public Hashtable getEnvironment() throws NamingException {
279     if (myEnv == null) {
280         // Must return non-null
281
myEnv = new Hashtable(3, 0.75f);
282     }
283         return myEnv;
284     }
285
286     public void close() throws NamingException {
287     store();
288     myEnv = null;
289     bindings = null;
290     }
291
292     private void store()
293     {
294         try {
295         if (bindings != null) {
296             FileOutputStream fos = new FileOutputStream(getStoragePath());
297             bindings.store(fos, "Repository resource mapping");
298             fos.close();
299         }
300         } catch (Exception JavaDoc e) {
301 // IASRI 4660742 e.printStackTrace();
302
// START OF IASRI 4660742
303
_logger.log(Level.SEVERE,"enterprise.store_exception" ,e);
304 // END OF IASRI 4660742
305
}
306     }
307
308     static String JavaDoc getFilePath(final String JavaDoc dirName, final String JavaDoc filename) {
309
310         return (String JavaDoc)
311         AccessController.doPrivileged(new PrivilegedAction() {
312         public Object JavaDoc run() {
313             String JavaDoc path = filename + FILE_EXT;
314     
315             if(path != null) {
316                 // Check if this file exists...
317
String JavaDoc fname = FileUtil.getAbsolutePath(dirName + path);
318                 File f = new File(fname);
319                 
320                 if(!f.exists())
321                     path = DEFAULT_NAME + FILE_EXT;
322             }
323             else
324                 path = DEFAULT_NAME + FILE_EXT;
325             
326             return FileUtil.getAbsolutePath(dirName + path);
327         }});
328     }
329       
330     private String JavaDoc getStoragePath() {
331     String JavaDoc dirName = (String JavaDoc) myEnv.get(REPOSITORY_DIR);
332     if (dirName == null) {
333         dirName = DEFAULT_NAMETABLE_DIR;
334     }
335
336     // System.out.println("REPOSITORY: " + dirName);
337
// START OF IASRI 4660742
338
//_logger.log(Level.FINE,"REPOSITORY: " + dirName);
339
// END OF IASRI 4660742
340
return getFilePath(dirName, (String JavaDoc) myEnv.get(REPOSITORY_NAME));
341     }
342
343     public static String JavaDoc getRepositoryName(String JavaDoc fname) {
344     String JavaDoc filepath = getFilePath(DEFAULT_NAMETABLE_DIR, fname);
345     int fileIndex = filepath.lastIndexOf(File.separator);
346     String JavaDoc filename = null;
347     
348     if(fileIndex > 0)
349         filename = filepath.substring(fileIndex + 1);
350     else
351         filename = filepath;
352     
353     int extIndex = filename.lastIndexOf(FILE_EXT);
354     String JavaDoc fwithoutext;
355
356     if(extIndex > 0)
357         fwithoutext = filename.substring(0, extIndex);
358     else
359         fwithoutext = filename;
360     
361     return fwithoutext;
362     }
363
364     private void resurrectTable() {
365         AccessController.doPrivileged(new PrivilegedAction() {
366         public Object JavaDoc run() {
367             bindings = new Properties();
368             try {
369                 File f = new File(getStoragePath());
370                 if(debug)
371 // IASRI 4660742 System.out.println("Loaded File: " + f.getAbsolutePath());
372
// START OF IASRI 4660742
373
_logger.log(Level.FINE,"Loaded File: " + f.getAbsolutePath());
374 // END OF IASRI 4660742
375
if(f.exists())
376                     bindings.load(new FileInputStream(f));
377             } catch (Exception JavaDoc e) {
378 // IASRI 4660742 e.printStackTrace();
379
// START OF IASRI 4660742
380
_logger.log(Level.SEVERE,"enterprise.load_exception" ,e);
381 // END OF IASRI 4660742
382
}
383             return null;
384         }});
385     }
386
387     private static void print(Hashtable ht) {
388         for (Enumeration en = ht.keys(); en.hasMoreElements(); ) {
389             Object JavaDoc key = en.nextElement();
390             Object JavaDoc value = ht.get(key);
391         if (debug)
392 // IASRI 4660742 System.out.println("[" + key + ":" + key.getClass().getName() +
393
// IASRI 4660742 ", " + value + ":" + value.getClass().getName()
394
// IASRI 4660742 + "]");
395
// START OF IASRI 4660742
396
{
397             if(_logger.isLoggable(Level.FINE))
398                 _logger.log(Level.FINE,"[" + key + ":" + key.getClass().getName() +", " + value + ":" + value.getClass().getName()+ "]");
399         }
400 // END OF IASRI 4660742
401
}
402     }
403     
404     // Class for enumerating name/class pairs
405
class RepNames implements NamingEnumeration {
406         Enumeration names;
407
408         RepNames (Enumeration names) {
409             this.names = names;
410         }
411
412         public boolean hasMoreElements() {
413             return names.hasMoreElements();
414         }
415
416         public boolean hasMore() throws NamingException {
417             return hasMoreElements();
418         }
419
420         public Object JavaDoc nextElement() {
421             if(names.hasMoreElements())
422             {
423                 String JavaDoc name = (String JavaDoc)names.nextElement();
424                 String JavaDoc className = bindings.get(name).getClass().getName();
425                 return new NameClassPair(name, className);
426             }
427             else
428                 return null;
429         }
430
431         public Object JavaDoc next() throws NamingException {
432             return nextElement();
433         }
434
435     // New API for JNDI 1.2
436
public void close() throws NamingException {
437         throw new OperationNotSupportedException("NamingEnumeration.close() not implemented");
438     }
439     }
440
441     // Class for enumerating bindings
442
class RepBindings implements NamingEnumeration {
443         Enumeration names;
444
445         RepBindings (Enumeration names) {
446             this.names = names;
447         }
448
449         public boolean hasMoreElements() {
450             return names.hasMoreElements();
451         }
452
453         public boolean hasMore() throws NamingException {
454             return hasMoreElements();
455         }
456
457         public Object JavaDoc nextElement() {
458             if(hasMoreElements())
459             {
460                 String JavaDoc name = (String JavaDoc)names.nextElement();
461                 return new Binding(name, bindings.get(name));
462             }
463             else
464                 return null;
465         }
466
467         public Object JavaDoc next() throws NamingException {
468             return nextElement();
469         }
470
471     public void close() throws NamingException {
472         throw new OperationNotSupportedException("NamingEnumeration.close() not implemented");
473     }
474     }
475     
476     private static final String JavaDoc
477         DEFAULT_NAMETABLE_DIR = "config/";
478     private static final String JavaDoc DEFAULT_NAME = "default";
479     private static final String JavaDoc FILE_EXT = ".properties";
480 };
481
482
483
Popular Tags