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