KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > naming > java > ComponentContextBuilder


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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.java;
19
20 import java.util.Map JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import javax.management.MalformedObjectNameException JavaDoc;
23 import javax.management.ObjectName JavaDoc;
24 import javax.naming.NamingException JavaDoc;
25 import javax.transaction.UserTransaction JavaDoc;
26
27 import org.apache.geronimo.kernel.ClassLoading;
28 import org.apache.geronimo.naming.reference.GBeanProxyReference;
29 import org.apache.geronimo.naming.reference.KernelReference;
30 import org.apache.geronimo.naming.reference.ORBReference;
31
32 /**
33  * TODO consider removing this class. The only purpose is to slightly hide the internalBind method.
34  *
35  * @version $Rev: 157487 $ $Date: 2005-03-14 16:06:01 -0800 (Mon, 14 Mar 2005) $
36  */

37 public class ComponentContextBuilder {
38     private static final String JavaDoc ENV = "env/";
39     private final Map JavaDoc context = new HashMap JavaDoc();
40
41     public Map JavaDoc getContext() {
42         return context;
43     }
44
45     public void addUserTransaction(UserTransaction JavaDoc userTransaction) {
46         context.put("UserTransaction", userTransaction);
47     }
48
49     public void addORB(ObjectName JavaDoc corbaGBeanObjectName) {
50         context.put("ORB", new ORBReference(corbaGBeanObjectName));
51     }
52
53     public void addHandleDelegateReference(Object JavaDoc handleDelegateReference) {
54         context.put("HandleDelegate", handleDelegateReference);
55     }
56
57     public void bind(String JavaDoc name, Object JavaDoc value) {
58         context.put(ENV + name, value);
59     }
60
61     public void addEnvEntry(String JavaDoc name, String JavaDoc type, String JavaDoc text, ClassLoader JavaDoc classLoader) throws NamingException JavaDoc, NumberFormatException JavaDoc {
62         Object JavaDoc value;
63         if (text == null) {
64             if ("org.apache.geronimo.kernel.Kernel".equals(type)) {
65                 value = new KernelReference();
66             } else {
67                 value = null;
68             }
69         } else if ("java.lang.String".equals(type)) {
70             value = text;
71         } else if ("java.lang.Character".equals(type)) {
72             value = new Character JavaDoc(text.charAt(0));
73         } else if ("java.lang.Boolean".equals(type)) {
74             value = Boolean.valueOf(text);
75         } else if ("java.lang.Byte".equals(type)) {
76             value = Byte.valueOf(text);
77         } else if ("java.lang.Short".equals(type)) {
78             value = Short.valueOf(text);
79         } else if ("java.lang.Integer".equals(type)) {
80             value = Integer.valueOf(text);
81         } else if ("java.lang.Long".equals(type)) {
82             value = Long.valueOf(text);
83         } else if ("java.lang.Float".equals(type)) {
84             value = Float.valueOf(text);
85         } else if ("java.lang.Double".equals(type)) {
86             value = Double.valueOf(text);
87         } else {
88             Class JavaDoc clazz = null;
89             try {
90                 clazz = ClassLoading.loadClass(type, classLoader);
91             } catch (ClassNotFoundException JavaDoc e) {
92                 throw new IllegalArgumentException JavaDoc("Could not load class for env-entry " + name + ", " + type);
93             }
94             ObjectName JavaDoc objectName = null;
95             try {
96                 objectName = ObjectName.getInstance(text);
97             } catch (MalformedObjectNameException JavaDoc e) {
98                 throw new IllegalArgumentException JavaDoc("If env-entry type is not String, Character, Byte, Short, Integer, Long, " +
99                         "Boolean, Double, or Float, the text value must be a valid ObjectName for use in a GBeanProxy:" +
100                         " name= " + name +
101                         ", value=" + type +
102                         ", text=" + text);
103             }
104             value = new GBeanProxyReference(objectName, clazz);
105
106         }
107         context.put(ENV + name, value);
108     }
109
110 }
111
Popular Tags