001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.geronimo.naming.enc;
019
020 import java.util.HashMap;
021 import java.util.Iterator;
022 import java.util.Map;
023 import java.util.Collections;
024
025 import javax.naming.Context;
026 import javax.naming.NamingException;
027 import javax.transaction.UserTransaction;
028
029 import org.apache.geronimo.kernel.Kernel;
030 import org.apache.geronimo.naming.reference.ClassLoaderAwareReference;
031 import org.apache.geronimo.naming.reference.KernelAwareReference;
032 import org.apache.xbean.naming.context.ImmutableContext;
033
034 /**
035 * @version $Rev: 525633 $ $Date: 2007-04-04 18:30:13 -0400 (Wed, 04 Apr 2007) $
036 */
037 public final class EnterpriseNamingContext {
038
039 public static Context createEnterpriseNamingContext(Map componentContext, UserTransaction userTransaction, Kernel kernel, ClassLoader classLoader) throws NamingException {
040 Map map = new HashMap();
041 if (componentContext != null) {
042 map.putAll(componentContext);
043 }
044
045 boolean containsEnv = false;
046 for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
047 Map.Entry entry = (Map.Entry) iterator.next();
048 String name = (String) entry.getKey();
049 Object value = entry.getValue();
050
051 if (name.startsWith("env/")) {
052 containsEnv = true;
053 }
054 if (value instanceof KernelAwareReference) {
055 ((KernelAwareReference) value).setKernel(kernel);
056 }
057 if (value instanceof ClassLoaderAwareReference) {
058 ((ClassLoaderAwareReference) value).setClassLoader(classLoader);
059 }
060 }
061
062 if (!containsEnv) {
063 Context env = new ImmutableContext("java:comp/env", Collections.EMPTY_MAP, false);
064 map.put("env", env);
065 }
066
067 if (userTransaction != null) {
068 map.put("UserTransaction", userTransaction);
069 }
070
071 return createEnterpriseNamingContext(map);
072 }
073
074 public static Context createEnterpriseNamingContext(Map context) throws NamingException {
075 return new ImmutableContext(context, false);
076 }
077
078 }