View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.naming.enc;
20  
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.Map;
24  import java.util.Collections;
25  
26  import javax.naming.Context;
27  import javax.naming.NamingException;
28  import javax.transaction.UserTransaction;
29  
30  import org.apache.geronimo.kernel.Kernel;
31  import org.apache.geronimo.naming.reference.ClassLoaderAwareReference;
32  import org.apache.geronimo.naming.reference.KernelAwareReference;
33  import org.apache.xbean.naming.context.ImmutableContext;
34  
35  /**
36   * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
37   */
38  public final class EnterpriseNamingContext {
39  
40      public static Context createEnterpriseNamingContext(Map componentContext, UserTransaction userTransaction, Kernel kernel, ClassLoader classLoader) throws NamingException {
41          Map map = new HashMap();
42          if (componentContext != null) {
43              map.putAll(componentContext);
44          }
45  
46          boolean containsEnv = false;
47          for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
48              Map.Entry entry = (Map.Entry) iterator.next();
49              String name = (String) entry.getKey();
50              Object value = entry.getValue();
51  
52              if (name.startsWith("env/")) {
53                  containsEnv = true;
54              }
55              if (value instanceof KernelAwareReference) {
56                  ((KernelAwareReference) value).setKernel(kernel);
57              }
58              if (value instanceof ClassLoaderAwareReference) {
59                  ((ClassLoaderAwareReference) value).setClassLoader(classLoader);
60              }
61          }
62  
63          if (!containsEnv) {
64              Context env = new ImmutableContext("java:comp/env", Collections.EMPTY_MAP, false);
65              map.put("env", env);
66          }
67  
68          if (userTransaction != null) {
69              map.put("UserTransaction", userTransaction);
70          }
71  
72          return createEnterpriseNamingContext(map);
73      }
74  
75      public static Context createEnterpriseNamingContext(Map context) throws NamingException {
76          return new ImmutableContext(context);
77      }
78  
79  }