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.Collections;
021    import java.util.HashMap;
022    import java.util.Iterator;
023    import java.util.Map;
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.EntryFactory;
032    import org.apache.geronimo.naming.reference.KernelAwareReference;
033    import org.apache.xbean.naming.context.ImmutableContext;
034    
035    /**
036     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
037     */
038    public final class EnterpriseNamingContext {
039    
040        public static Context createEnterpriseNamingContext(Map<String, Object> componentContext, UserTransaction userTransaction, Kernel kernel, ClassLoader classLoader) throws NamingException {
041            Map<String, Object> map = new HashMap<String, Object>();
042            if (componentContext != null) {
043                map.putAll(componentContext);
044            }
045    
046            boolean containsEnv = false;
047            for (Map.Entry<String, Object> entry: map.entrySet()) {
048                String name = entry.getKey();
049                Object value = entry.getValue();
050    
051                if (name.startsWith("env/")) {
052                    containsEnv = true;
053                }
054                if (value instanceof EntryFactory) {
055                    value = ((EntryFactory)value).buildEntry(kernel, classLoader);
056                    entry.setValue(value);
057                }
058                if (value instanceof KernelAwareReference) {
059                    ((KernelAwareReference) value).setKernel(kernel);
060                }
061                if (value instanceof ClassLoaderAwareReference) {
062                    ((ClassLoaderAwareReference) value).setClassLoader(classLoader);
063                }
064            }
065    
066            if (!containsEnv) {
067                Context env = new ImmutableContext("java:comp/env", Collections.EMPTY_MAP, false);
068                map.put("env", env);
069            }
070    
071            if (userTransaction != null) {
072                map.put("UserTransaction", userTransaction);
073            }
074    
075            return createEnterpriseNamingContext(map);
076        }
077    
078        public static Context createEnterpriseNamingContext(Map context) throws NamingException {
079            return new ImmutableContext(context, false);
080        }
081    
082    }