KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > modules > fs > fsURLContextFactory


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.naming.modules.fs;
18
19 import java.util.Hashtable JavaDoc;
20
21 import javax.naming.Context JavaDoc;
22 import javax.naming.Name JavaDoc;
23 import javax.naming.NamingException JavaDoc;
24 import javax.naming.spi.InitialContextFactory JavaDoc;
25 import javax.naming.spi.ObjectFactory JavaDoc;
26 //import org.apache.naming.ContextBindings;
27

28 /**
29  * Context factory for the "file:" namespace.
30  * <p>
31  * <b>Important note</b> : This factory MUST be associated with the "java" URL
32  * prefix, which can be done by either :
33  * <ul>
34  * <li>Adding a
35  * java.naming.factory.url.pkgs=org.apache.naming property to the JNDI properties file</li>
36  * <li>Setting an environment variable named Context.URL_PKG_PREFIXES with
37  * its value including the org.apache.naming package name.
38  * More detail about this can be found in the JNDI documentation :
39  * {@link javax.naming.spi.NamingManager#getURLContext(java.lang.String, java.util.Hashtable)}.</li>
40  * </ul>
41  *
42  * @author Remy Maucherat
43  */

44
45 public class fsURLContextFactory implements ObjectFactory JavaDoc,InitialContextFactory JavaDoc
46 {
47     private static org.apache.commons.logging.Log log=
48         org.apache.commons.logging.LogFactory.getLog( fsURLContextFactory.class );
49     /**
50      * Initial context.
51      */

52     protected static Context JavaDoc initialContext = null;
53
54
55     // --------------------------------------------------------- Public Methods
56

57     /**
58      * Crete a new Context's instance.
59      */

60     public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx,
61                                     Hashtable JavaDoc environment)
62         throws NamingException JavaDoc {
63         // Called with null/null/null if fs:/tmp/test
64
if( log.isDebugEnabled() ) log.debug( "getObjectInstance " + obj + " " + name + " " + nameCtx + " " + environment);
65
66         FileDirContext fc= new FileDirContext(environment);
67         fc.setDocBase( "/" );
68         fc.setURLPrefix("fs:");
69         return fc;
70     }
71
72
73     /**
74      * Get a new (writable) initial context.
75      */

76     public Context JavaDoc getInitialContext(Hashtable JavaDoc environment)
77         throws NamingException JavaDoc
78     {
79         // If the thread is not bound, return a shared writable context
80
if (initialContext == null) {
81             FileDirContext fc= new FileDirContext(environment);
82             fc.setDocBase( "/" );
83             fc.setURLPrefix("fs:");
84             initialContext=fc;
85             if( log.isDebugEnabled() )
86                 log.debug("Create initial fs context "+ environment);
87         }
88         return initialContext;
89     }
90 }
91
92
Popular Tags