KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > ws > axis > JOnASEJBProvider


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JOnASEJBProvider.java,v 1.9 2005/01/17 08:33:56 sauthieg Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.ws.axis;
26
27 import java.lang.reflect.Method JavaDoc;
28
29 import javax.naming.InitialContext JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31
32 import org.apache.axis.AxisFault;
33 import org.apache.axis.Handler;
34 import org.apache.axis.MessageContext;
35 import org.apache.axis.i18n.Messages;
36 import org.apache.axis.providers.java.RPCProvider;
37
38 import org.objectweb.jonas_ejb.container.JServiceEndpoint;
39 import org.objectweb.jonas_ejb.container.JServiceEndpointHome;
40
41 import org.objectweb.jonas.common.Log;
42 import org.objectweb.jonas.security.ws.SecurityContextHelper;
43
44 import org.objectweb.util.monolog.api.BasicLevel;
45 import org.objectweb.util.monolog.api.Logger;
46
47 /**
48  * Expose the service-endpoint interface of the Ejb.
49  *
50  * @author Guillaume Sauthier
51  */

52 public class JOnASEJBProvider extends RPCProvider {
53
54     /**
55      * Logger
56      */

57     private static Logger logger = null;
58
59     /**
60      * parameter service-endpoint class name
61      */

62     public static final String JavaDoc OPTION_SEINTERFACENAME = "serviceEndpointInterfaceName";
63
64     /**
65      * parameter service-endpoint JNDI name
66      */

67     public static final String JavaDoc OPTION_SEJNDINAME = "serviceEndpointJndiName";
68
69     /**
70      * cached initial context
71      */

72     private static InitialContext JavaDoc cachedContext;
73
74     /**
75      * Create a new JOnASEJBProvider
76      */

77     public JOnASEJBProvider() {
78         super();
79         logger = Log.getLogger(Log.JONAS_WS_EJBPROVIDER_PREFIX);
80         logger.log(BasicLevel.DEBUG, "");
81     }
82
83     /**
84      * Override the default implementation
85      * Return a object which implements the service.
86      * @param msgContext the message context
87      * @param seiName The Service Endpoint Interface classname
88      * @return an object that implements the service
89      * @throws Exception when trying to create a Serviceobject without serviceendpoint name aprameter
90      */

91     protected Object JavaDoc makeNewServiceObject(MessageContext msgContext, String JavaDoc seiName) throws Exception JavaDoc {
92         logger.log(BasicLevel.DEBUG, seiName);
93         if (seiName == null) {
94             logger.log(BasicLevel.ERROR, "Service Endpoint Interface classname is null");
95             // cannot find service-endpoint
96
throw new AxisFault(Messages.getMessage("noOption00", OPTION_SEINTERFACENAME, msgContext.getService()
97                     .getName()));
98         }
99
100         // Get ServiceEndpointHome in JNDI
101
String JavaDoc jndiName = getStrOption(OPTION_SEJNDINAME, msgContext.getService());
102         if (jndiName == null) {
103             logger.log(BasicLevel.ERROR, "Service Endpoint JNDI name is null");
104             throw new AxisFault("Missing parameter in service : " + OPTION_SEJNDINAME);
105         }
106         JServiceEndpointHome sehome = null;
107         try {
108             InitialContext JavaDoc ic = getCachedContext();
109             sehome = (JServiceEndpointHome) ic.lookup(jndiName);
110         } catch (NamingException JavaDoc ne) {
111             logger.log(BasicLevel.ERROR, "Cannot lookup ServiceEndpointHome");
112             throw new AxisFault("Cannot lookup ServiceEndpointHome: " + jndiName);
113         }
114         // Check that the object implements the SEI
115
// TODO
116

117         // Create a new ServiceEndpoint object
118
JServiceEndpoint se = sehome.create();
119
120         // Set the MessageContext that can be retrived by the bean
121
se.setMessageContext(msgContext);
122         return se;
123     }
124
125     /**
126      * Override the default implementation : create a SecurityContext from username and password
127      * @throws Exception if method invokation fail or produce an Exception
128      */

129     protected Object JavaDoc invokeMethod(MessageContext msgContext, Method JavaDoc method, Object JavaDoc obj, Object JavaDoc[] argValues)
130             throws Exception JavaDoc {
131         logger.log(BasicLevel.DEBUG, "");
132         String JavaDoc username = msgContext.getUsername();
133         if (username != null) {
134             // Do not forget to initialize the security context
135
SecurityContextHelper.getInstance().login(username, msgContext.getPassword());
136         }
137         return super.invokeMethod(msgContext, method, obj, argValues);
138     }
139
140     /**
141      * Override the default implementation
142      * @return Return the option in the configuration that contains the service
143      * class name. In the EJB case, it is the JNDI name of the bean.
144      */

145     protected String JavaDoc getServiceClassNameOptionName() {
146         logger.log(BasicLevel.DEBUG, "");
147         return OPTION_SEINTERFACENAME;
148     }
149
150     /**
151      * Get a String option by looking first in the service options, and then at
152      * the Handler's options. This allows defaults to be specified at the
153      * provider level, and then overriden for particular services.
154      *
155      * @param optionName the option to retrieve
156      * @param service Option holder
157      *
158      * @return String the value of the option or null if not found in either
159      * scope
160      */

161     private String JavaDoc getStrOption(String JavaDoc optionName, Handler service) {
162         String JavaDoc value = null;
163         if (service != null) {
164             value = (String JavaDoc) service.getOption(optionName);
165         }
166         if (value == null) {
167             value = (String JavaDoc) getOption(optionName);
168         }
169         return value;
170     }
171
172     /**
173      * @return Returns the cached InitialContext (or created a new one)
174      * @throws javax.naming.NamingException when InitialContext creation fails
175      */

176     private InitialContext JavaDoc getCachedContext() throws javax.naming.NamingException JavaDoc {
177         if (cachedContext == null) {
178             cachedContext = new InitialContext JavaDoc();
179         }
180         return cachedContext;
181     }
182
183 }
Popular Tags