001: /*
002: * The contents of this file are subject to the Sapient Public License
003: * Version 1.0 (the "License"); you may not use this file except in compliance
004: * with the License. You may obtain a copy of the License at
005: * http://carbon.sf.net/License.html.
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is The Carbon Component Framework.
012: *
013: * The Initial Developer of the Original Code is Sapient Corporation
014: *
015: * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
016: */
017: package org.sape.carbon.services.jndi;
018:
019: import java.util.Hashtable;
020: import java.util.Map;
021:
022: import javax.naming.InitialContext;
023: import javax.naming.NamingException;
024: import javax.naming.directory.InitialDirContext;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: import org.sape.carbon.core.component.Component;
030: import org.sape.carbon.core.component.ComponentConfiguration;
031: import org.sape.carbon.core.component.lifecycle.Configurable;
032: import org.sape.carbon.core.component.lifecycle.Initializable;
033: import org.sape.carbon.core.config.InvalidConfigurationException;
034:
035: /**
036: * <p>Default implementation of InitialContextFactory</p>
037: *
038: * Copyright 2003 Sapient
039: * @since carbon 2.1
040: * @author Douglas Voet, Jul 30, 2003
041: * @version $Revision: 1.2 $($Author: dvoet $ / $Date: 2003/10/30 19:28:56 $)
042: */
043: public class DefaultInitialContextFactoryImpl implements
044: InitialContextFactory, Configurable, Initializable {
045:
046: private Log log = LogFactory.getLog(this .getClass());
047:
048: protected Hashtable environment;
049:
050: public void configure(ComponentConfiguration configuration) {
051:
052: try {
053: InitialContextFactoryConfiguration factoryConfig = (InitialContextFactoryConfiguration) configuration;
054:
055: this .environment.clear();
056: if (factoryConfig.getEnvironment() != null) {
057: this .environment.putAll(factoryConfig.getEnvironment());
058: }
059:
060: } catch (ClassCastException cce) {
061: throw new InvalidConfigurationException(this .getClass(),
062: configuration.getConfigurationName(),
063: "ConfigurationInterface",
064: "Configuration was not of type "
065: + InitialContextFactoryConfiguration.class
066: .getName(), cce);
067: }
068:
069: }
070:
071: public void initialize(Component this Component) {
072: this .environment = new Hashtable();
073: }
074:
075: /**
076: * Gets a new InitialContext instance with evironment values that come
077: * from the service's configuration.
078: *
079: * @return a new InitalContext
080: * @throws NamingException this is also logged at the info level
081: */
082: public InitialContext getContext() throws NamingException {
083: try {
084: return new InitialContext(this .environment);
085: } catch (NamingException ne) {
086: this .log.info("Caught NamingException", ne);
087: throw ne;
088: }
089: }
090:
091: /**
092: * Gets a new InitialContext instance with evironment values that come
093: * from the service's configuration, but environment values can also
094: * be passed via the environment parameter to augment or override the
095: * values stored in configuration.
096: *
097: * @param environment values that augment and/or override the values
098: * stored in configuration
099: * @return a new InitialContext
100: * @throws NamingException this is also logged at the info level
101: */
102: public InitialContext getContext(Map givenEnvironment)
103: throws NamingException {
104:
105: try {
106: Hashtable localEnvironment = new Hashtable(this .environment);
107: if (givenEnvironment != null) {
108: localEnvironment.putAll(givenEnvironment);
109: }
110: return new InitialContext(localEnvironment);
111: } catch (NamingException ne) {
112: this .log.info("Caught NamingException", ne);
113: throw ne;
114: }
115: }
116:
117: /**
118: * Gets a new InitialContext with environment values given in the
119: * evironment parameter. The service's configuration is ignored.
120: *
121: * @param environment values used to construct the InitialContext
122: * @return a new InitialContext
123: * @throws NamingException this is also logged at the info level
124: */
125: public InitialContext getContextIgnoreConfig(Map givenEnvironment)
126: throws NamingException {
127:
128: try {
129: Hashtable localEnvironment = null;
130: if (givenEnvironment instanceof Hashtable) {
131: localEnvironment = (Hashtable) givenEnvironment;
132: } else if (givenEnvironment != null) {
133: localEnvironment = new Hashtable(givenEnvironment);
134: }
135:
136: return new InitialContext(localEnvironment);
137: } catch (NamingException ne) {
138: this .log.info("Caught NamingException", ne);
139: throw ne;
140: }
141: }
142:
143: /**
144: * Gets a new InitialDirContext instance with evironment values that come
145: * from the service's configuration.
146: *
147: * @return a new InitialDirContext
148: * @throws NamingException
149: */
150: public InitialDirContext getDirContext() throws NamingException {
151: try {
152: return new InitialDirContext(this .environment);
153: } catch (NamingException ne) {
154: this .log.info("Caught NamingException", ne);
155: throw ne;
156: }
157: }
158:
159: /**
160: * Gets a new InitialDirContext instance with evironment values that come
161: * from the service's configuration, but environment values can also
162: * be passed via the environment parameter to augment or override the
163: * values stored in configuration.
164: *
165: * @param environment values that augment and/or override the values
166: * stored in configuration
167: * @return a new InitialDirContext
168: * @throws NamingException
169: */
170: public InitialDirContext getDirContext(Map givenEnvironment)
171: throws NamingException {
172:
173: try {
174: Hashtable localEnvironment = new Hashtable(this .environment);
175: if (givenEnvironment != null) {
176: localEnvironment.putAll(givenEnvironment);
177: }
178: return new InitialDirContext(localEnvironment);
179: } catch (NamingException ne) {
180: this .log.info("Caught NamingException", ne);
181: throw ne;
182: }
183: }
184:
185: /**
186: * Gets a new InitialDirContext with environment values given in the
187: * evironment parameter. The service's configuration is ignored.
188: *
189: * @param environment values used to construct the InitialContext
190: * @return a new InitialDirContext
191: * @throws NamingException
192: */
193: public InitialDirContext getDirContextIgnoreConfig(
194: Map givenEnvironment) throws NamingException {
195:
196: try {
197: Hashtable localEnvironment = null;
198: if (givenEnvironment instanceof Hashtable) {
199: localEnvironment = (Hashtable) givenEnvironment;
200: } else if (givenEnvironment != null) {
201: localEnvironment = new Hashtable(givenEnvironment);
202: }
203:
204: return new InitialDirContext(localEnvironment);
205: } catch (NamingException ne) {
206: this .log.info("Caught NamingException", ne);
207: throw ne;
208: }
209: }
210:
211: }
|