KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > naming > enc > EnterpriseNamingContext


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.naming.enc;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Collections JavaDoc;
24
25 import javax.naming.Context JavaDoc;
26 import javax.naming.NamingException JavaDoc;
27 import javax.transaction.UserTransaction JavaDoc;
28
29 import org.apache.geronimo.kernel.Kernel;
30 import org.apache.geronimo.naming.reference.ClassLoaderAwareReference;
31 import org.apache.geronimo.naming.reference.KernelAwareReference;
32 import org.apache.xbean.naming.context.ImmutableContext;
33
34 /**
35  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
36  */

37 public final class EnterpriseNamingContext {
38
39     public static Context JavaDoc createEnterpriseNamingContext(Map JavaDoc componentContext, UserTransaction JavaDoc userTransaction, Kernel kernel, ClassLoader JavaDoc classLoader) throws NamingException JavaDoc {
40         Map JavaDoc map = new HashMap JavaDoc();
41         if (componentContext != null) {
42             map.putAll(componentContext);
43         }
44
45         boolean containsEnv = false;
46         for (Iterator JavaDoc iterator = map.entrySet().iterator(); iterator.hasNext();) {
47             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
48             String JavaDoc name = (String JavaDoc) entry.getKey();
49             Object JavaDoc value = entry.getValue();
50
51             if (name.startsWith("env/")) {
52                 containsEnv = true;
53             }
54             if (value instanceof KernelAwareReference) {
55                 ((KernelAwareReference) value).setKernel(kernel);
56             }
57             if (value instanceof ClassLoaderAwareReference) {
58                 ((ClassLoaderAwareReference) value).setClassLoader(classLoader);
59             }
60         }
61
62         if (!containsEnv) {
63             Context JavaDoc env = new ImmutableContext("java:comp/env", Collections.EMPTY_MAP, false);
64             map.put("env", env);
65         }
66
67         if (userTransaction != null) {
68             map.put("UserTransaction", userTransaction);
69         }
70
71         return createEnterpriseNamingContext(map);
72     }
73
74     public static Context JavaDoc createEnterpriseNamingContext(Map JavaDoc context) throws NamingException JavaDoc {
75         return new ImmutableContext(context);
76     }
77
78 }
79
Popular Tags