KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > jndi > DefaultInitialContextFactoryImpl


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17 package org.sape.carbon.services.jndi;
18
19 import java.util.Hashtable JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.naming.InitialContext JavaDoc;
23 import javax.naming.NamingException JavaDoc;
24 import javax.naming.directory.InitialDirContext JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.sape.carbon.core.component.Component;
30 import org.sape.carbon.core.component.ComponentConfiguration;
31 import org.sape.carbon.core.component.lifecycle.Configurable;
32 import org.sape.carbon.core.component.lifecycle.Initializable;
33 import org.sape.carbon.core.config.InvalidConfigurationException;
34
35 /**
36  * <p>Default implementation of InitialContextFactory</p>
37  *
38  * Copyright 2003 Sapient
39  * @since carbon 2.1
40  * @author Douglas Voet, Jul 30, 2003
41  * @version $Revision: 1.2 $($Author: dvoet $ / $Date: 2003/10/30 19:28:56 $)
42  */

43 public class DefaultInitialContextFactoryImpl
44     implements InitialContextFactory, Configurable, Initializable {
45         
46     private Log log = LogFactory.getLog(this.getClass());
47     
48     protected Hashtable JavaDoc environment;
49
50     public void configure(ComponentConfiguration configuration) {
51         
52         try {
53             InitialContextFactoryConfiguration factoryConfig =
54                 (InitialContextFactoryConfiguration) configuration;
55                 
56             this.environment.clear();
57             if (factoryConfig.getEnvironment() != null) {
58                 this.environment.putAll(factoryConfig.getEnvironment());
59             }
60             
61         } catch (ClassCastException JavaDoc cce) {
62             throw new InvalidConfigurationException(
63                 this.getClass(),
64                 configuration.getConfigurationName(),
65                 "ConfigurationInterface",
66                 "Configuration was not of type " +
67                     InitialContextFactoryConfiguration.class.getName(),
68                 cce);
69         }
70         
71
72     }
73
74     public void initialize(Component thisComponent) {
75         this.environment = new Hashtable JavaDoc();
76     }
77
78     /**
79      * Gets a new InitialContext instance with evironment values that come
80      * from the service's configuration.
81      *
82      * @return a new InitalContext
83      * @throws NamingException this is also logged at the info level
84      */

85     public InitialContext JavaDoc getContext() throws NamingException JavaDoc {
86         try {
87             return new InitialContext JavaDoc(this.environment);
88         } catch (NamingException JavaDoc ne) {
89             this.log.info("Caught NamingException", ne);
90             throw ne;
91         }
92     }
93
94     /**
95      * Gets a new InitialContext instance with evironment values that come
96      * from the service's configuration, but environment values can also
97      * be passed via the environment parameter to augment or override the
98      * values stored in configuration.
99      *
100      * @param environment values that augment and/or override the values
101      * stored in configuration
102      * @return a new InitialContext
103      * @throws NamingException this is also logged at the info level
104      */

105     public InitialContext JavaDoc getContext(Map JavaDoc givenEnvironment)
106         throws NamingException JavaDoc {
107             
108         try {
109             Hashtable JavaDoc localEnvironment = new Hashtable JavaDoc(this.environment);
110             if (givenEnvironment != null) {
111                 localEnvironment.putAll(givenEnvironment);
112             }
113             return new InitialContext JavaDoc(localEnvironment);
114         } catch (NamingException JavaDoc ne) {
115             this.log.info("Caught NamingException", ne);
116             throw ne;
117         }
118     }
119
120     /**
121      * Gets a new InitialContext with environment values given in the
122      * evironment parameter. The service's configuration is ignored.
123      *
124      * @param environment values used to construct the InitialContext
125      * @return a new InitialContext
126      * @throws NamingException this is also logged at the info level
127      */

128     public InitialContext JavaDoc getContextIgnoreConfig(Map JavaDoc givenEnvironment)
129         throws NamingException JavaDoc {
130
131         try {
132             Hashtable JavaDoc localEnvironment = null;
133             if (givenEnvironment instanceof Hashtable JavaDoc) {
134                 localEnvironment = (Hashtable JavaDoc) givenEnvironment;
135             } else if (givenEnvironment != null) {
136                 localEnvironment = new Hashtable JavaDoc(givenEnvironment);
137             }
138             
139             return new InitialContext JavaDoc(localEnvironment);
140         } catch (NamingException JavaDoc ne) {
141             this.log.info("Caught NamingException", ne);
142             throw ne;
143         }
144     }
145
146     /**
147      * Gets a new InitialDirContext instance with evironment values that come
148      * from the service's configuration.
149      *
150      * @return a new InitialDirContext
151      * @throws NamingException
152      */

153     public InitialDirContext JavaDoc getDirContext() throws NamingException JavaDoc {
154         try {
155             return new InitialDirContext JavaDoc(this.environment);
156         } catch (NamingException JavaDoc ne) {
157             this.log.info("Caught NamingException", ne);
158             throw ne;
159         }
160     }
161
162     /**
163      * Gets a new InitialDirContext instance with evironment values that come
164      * from the service's configuration, but environment values can also
165      * be passed via the environment parameter to augment or override the
166      * values stored in configuration.
167      *
168      * @param environment values that augment and/or override the values
169      * stored in configuration
170      * @return a new InitialDirContext
171      * @throws NamingException
172      */

173     public InitialDirContext JavaDoc getDirContext(Map JavaDoc givenEnvironment)
174         throws NamingException JavaDoc {
175
176         try {
177             Hashtable JavaDoc localEnvironment = new Hashtable JavaDoc(this.environment);
178             if (givenEnvironment != null) {
179                 localEnvironment.putAll(givenEnvironment);
180             }
181             return new InitialDirContext JavaDoc(localEnvironment);
182         } catch (NamingException JavaDoc ne) {
183             this.log.info("Caught NamingException", ne);
184             throw ne;
185         }
186     }
187
188     /**
189      * Gets a new InitialDirContext with environment values given in the
190      * evironment parameter. The service's configuration is ignored.
191      *
192      * @param environment values used to construct the InitialContext
193      * @return a new InitialDirContext
194      * @throws NamingException
195      */

196     public InitialDirContext JavaDoc getDirContextIgnoreConfig(Map JavaDoc givenEnvironment)
197         throws NamingException JavaDoc {
198
199         try {
200             Hashtable JavaDoc localEnvironment = null;
201             if (givenEnvironment instanceof Hashtable JavaDoc) {
202                 localEnvironment = (Hashtable JavaDoc) givenEnvironment;
203             } else if (givenEnvironment != null) {
204                 localEnvironment = new Hashtable JavaDoc(givenEnvironment);
205             }
206         
207             return new InitialDirContext JavaDoc(localEnvironment);
208         } catch (NamingException JavaDoc ne) {
209             this.log.info("Caught NamingException", ne);
210             throw ne;
211         }
212     }
213
214 }
215
Popular Tags