KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > providers > java > CORBAProvider


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

16
17 package org.apache.axis.providers.java;
18
19 import org.apache.axis.Constants;
20 import org.apache.axis.Handler;
21 import org.apache.axis.MessageContext;
22 import org.apache.axis.components.logger.LogFactory;
23 import org.apache.axis.utils.ClassUtils;
24 import org.apache.commons.logging.Log;
25 import org.omg.CORBA.ORB JavaDoc;
26 import org.omg.CosNaming.NameComponent JavaDoc;
27 import org.omg.CosNaming.NamingContext JavaDoc;
28 import org.omg.CosNaming.NamingContextHelper JavaDoc;
29
30 import java.lang.reflect.Method JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 /**
34  * A basic CORBA Provider
35  *
36  * @author Davanum Srinivas (dims@yahoo.com)
37  */

38 public class CORBAProvider extends RPCProvider
39 {
40     protected static Log log =
41         LogFactory.getLog(CORBAProvider.class.getName());
42
43     private static final String JavaDoc DEFAULT_ORB_INITIAL_HOST = "localhost";
44     private static final String JavaDoc DEFAULT_ORB_INITIAL_PORT = "900";
45     
46     // The enterprise category is for stuff that an enterprise product might
47
// want to track, but in a simple environment (like the AXIS build) would
48
// be nothing more than a nuisance.
49
protected static Log entLog =
50         LogFactory.getLog(Constants.ENTERPRISE_LOG_CATEGORY);
51
52     public static final String JavaDoc OPTION_ORB_INITIAL_HOST = "ORBInitialHost";
53     public static final String JavaDoc OPTION_ORB_INITIAL_PORT = "ORBInitialPort";
54     public static final String JavaDoc OPTION_NAME_ID = "NameID";
55     public static final String JavaDoc OPTION_NAME_KIND = "NameKind";
56     public static final String JavaDoc OPTION_INTERFACE_CLASSNAME = "InterfaceClassName";
57     public static final String JavaDoc OPTION_HELPER_CLASSNAME = "HelperClassName";
58
59     /**
60      * Return a object which implements the service.
61      *
62      * @param msgContext the message context
63      * @param clsName The JNDI name of the EJB home class
64      * @return an object that implements the service
65      */

66     protected Object JavaDoc makeNewServiceObject(MessageContext msgContext,
67                                           String JavaDoc clsName)
68         throws Exception JavaDoc
69     {
70         // Read deployment descriptor options
71
String JavaDoc orbInitialHost = getStrOption(OPTION_ORB_INITIAL_HOST,msgContext.getService());
72         if (orbInitialHost == null)
73           orbInitialHost = DEFAULT_ORB_INITIAL_HOST;
74         String JavaDoc orbInitialPort = getStrOption(OPTION_ORB_INITIAL_PORT,msgContext.getService());
75         if (orbInitialPort == null)
76           orbInitialPort = DEFAULT_ORB_INITIAL_PORT;
77         String JavaDoc nameId = getStrOption(OPTION_NAME_ID,msgContext.getService());
78         String JavaDoc nameKind = getStrOption(OPTION_NAME_KIND,msgContext.getService());
79         String JavaDoc helperClassName = getStrOption(OPTION_HELPER_CLASSNAME,msgContext.getService());
80
81         // Initialize ORB
82
Properties JavaDoc orbProps = new Properties JavaDoc();
83         orbProps.put("org.omg.CORBA.ORBInitialHost", orbInitialHost);
84         orbProps.put("org.omg.CORBA.ORBInitialPort", orbInitialPort);
85         ORB JavaDoc orb = ORB.init(new String JavaDoc[0], orbProps);
86
87         // Find the object
88
NamingContext JavaDoc root = NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));
89         NameComponent JavaDoc nc = new NameComponent JavaDoc(nameId, nameKind);
90         NameComponent JavaDoc[] ncs = {nc};
91         org.omg.CORBA.Object JavaDoc corbaObject = root.resolve(ncs);
92
93         Class JavaDoc helperClass = ClassUtils.forName(helperClassName);
94         // Narrow the object reference
95
Method JavaDoc narrowMethod = helperClass.getMethod("narrow", CORBA_OBJECT_CLASS);
96         Object JavaDoc targetObject = narrowMethod.invoke(null, new Object JavaDoc[] {corbaObject});
97
98         return targetObject;
99     }
100
101     private static final Class JavaDoc[] CORBA_OBJECT_CLASS = new Class JavaDoc[] {org.omg.CORBA.Object JavaDoc.class};
102
103     /**
104      * Return the option in the configuration that contains the service class
105      * name.
106      */

107     protected String JavaDoc getServiceClassNameOptionName()
108     {
109         return OPTION_INTERFACE_CLASSNAME;
110     }
111
112     /**
113      * Get a String option by looking first in the service options,
114      * and then at the Handler's options. This allows defaults to be
115      * specified at the provider level, and then overriden for particular
116      * services.
117      *
118      * @param optionName the option to retrieve
119      * @return String the value of the option or null if not found in
120      * either scope
121      */

122     protected String JavaDoc getStrOption(String JavaDoc optionName, Handler service)
123     {
124         String JavaDoc value = null;
125         if (service != null)
126             value = (String JavaDoc)service.getOption(optionName);
127         if (value == null)
128             value = (String JavaDoc)getOption(optionName);
129         return value;
130     }
131  }
132
Popular Tags