KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > jndi > spi > AbsInitialContextFactory


1 /**
2  * Copyright (C) 2005 - Bull S.A.
3  *
4  * CAROL: Common Architecture for RMI ObjectWeb Layer
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: AbsInitialContextFactory.java,v 1.1 2005/03/10 10:05:02 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.carol.jndi.spi;
26
27 import java.lang.reflect.Constructor JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Hashtable JavaDoc;
30
31 import javax.naming.Context JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34 import javax.naming.spi.InitialContextFactory JavaDoc;
35
36 import org.objectweb.carol.rmi.exception.NamingExceptionHelper;
37
38 /**
39  * Instantiate the class of the given SPI and Handle a cache mechanism for
40  * storing contexts. It avoids to build new Context.
41  * When wanting to integrate a new protocol, extends this class and define getReferencingFactory() and getWrapperClass() method.
42  * Also, there are some others methods that you could redefine like init() or addExtraConfInEnvironment() to your specific needs.
43  * @author Florent Benoit
44  */

45 public abstract class AbsInitialContextFactory implements InitialContextFactory JavaDoc {
46
47
48     /**
49      * Map containing context (used as cache mechanism)
50      */

51     private static HashMap JavaDoc contexts = new HashMap JavaDoc();
52
53
54     /**
55      * @return the real factory of this wrapper
56      */

57     protected abstract String JavaDoc getReferencingFactory();
58
59     /**
60      * @return class of the wrapper (to be instantiated + pool).
61      */

62     protected abstract Class JavaDoc getWrapperClass();
63
64     /**
65      * Creates an Initial Context for beginning name resolution. Special
66      * requirements of this context are supplied using <code>environment</code>.
67      * @param environment The possibly null environment specifying information
68      * to be used in the creation of the initial context.
69      * @return A non-null initial context object that implements the Context
70      * interface.
71      * @exception NamingException If cannot create an initial context.
72      */

73     public Context JavaDoc getInitialContext(Hashtable JavaDoc environment) throws NamingException JavaDoc {
74
75         // Look into cache if key is not null
76
Context JavaDoc ctx = null;
77         String JavaDoc key = getKey(environment);
78         if (key != null) {
79             ctx = (Context JavaDoc) contexts.get(key);
80         }
81
82         // value found, return it
83
if (ctx != null) {
84             return ctx;
85         }
86
87         // Factory need to initialize some specific settings ?
88
init();
89
90         // else, need to build a new one and add it in cache
91
environment.put(Context.INITIAL_CONTEXT_FACTORY, getReferencingFactory());
92
93         // Get class of the wrapper
94
Class JavaDoc clazz = getWrapperClass();
95
96         // Load the constructor
97
Constructor JavaDoc ctr = null;
98         try {
99             ctr = clazz.getConstructor(getClassConstructor());
100         } catch (Exception JavaDoc e) {
101             throw NamingExceptionHelper.create("Cannot find the constructor with Context class as argument in class '" + clazz.getName() + "' : " + e.getMessage(), e);
102         }
103
104         // The environment may be completed by extra values ?
105
addExtraConfInEnvironment(environment);
106
107         // Build a new context
108
try {
109             ctx = (Context JavaDoc) ctr.newInstance(getClassArgs(environment));
110         } catch (Exception JavaDoc e) {
111             throw NamingExceptionHelper.create("Cannot build an instance of the class '" + clazz.getName() + "' : " + e.getMessage(), e);
112         }
113
114         // add in cache if there is a key
115
if (key != null) {
116             contexts.put(key, ctx);
117         }
118
119         // return context
120
return ctx;
121     }
122
123     /**
124      * @param environment env to determine the key
125      * @return the key or null if we don't want to cache it
126      */

127     protected String JavaDoc getKey(Hashtable JavaDoc environment) {
128         String JavaDoc key = null;
129         if (environment != null) {
130             key = (String JavaDoc) environment.get(Context.PROVIDER_URL);
131         }
132         return key;
133     }
134
135     /**
136      * Gets the values to give to the constructor of the given class (provided by getWrapperClass())
137      * @param environment given environment
138      * @return array of objects for the constructor
139      * @throws NamingException if the new InitialContext fails
140      */

141     protected Object JavaDoc[] getClassArgs(Hashtable JavaDoc environment) throws NamingException JavaDoc {
142         return new Object JavaDoc[] {new InitialContext JavaDoc(environment)};
143     }
144
145     /**
146      * Gets the arguments of the constructor of the given class (provided by getWrapperClass())
147      * @return array of objects for the constructor
148      */

149     protected Class JavaDoc[] getClassConstructor() {
150         return new Class JavaDoc[] {Context JavaDoc.class};
151     }
152
153
154     /**
155      * This method should be redefined by subclasses
156      * For some protocols, there is a need to populate environment with some settings
157      * @param environment hashtable containing the environment
158      */

159     protected void addExtraConfInEnvironment(Hashtable JavaDoc environment) {
160
161     }
162
163     /**
164      * This method should be redefined by subclasses
165      * For some protocols, there are some initialization stuff to do
166      * @throws NamingException if there is an exception
167      */

168     protected void init() throws NamingException JavaDoc {
169     }
170
171 }
172
Popular Tags