KickJava   Java API By Example, From Geeks To Geeks.

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


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: JonasHandler.java,v 1.10 2005/01/26 08:51:05 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.ws.axis;
26
27
28 import javax.naming.Context JavaDoc;
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.handlers.BasicHandler;
36
37 import org.objectweb.jonas_ejb.container.JServiceEndpointHome;
38
39 import org.objectweb.jonas.common.Log;
40 import org.objectweb.jonas.security.ws.SecurityContextHelper;
41
42 import org.objectweb.util.monolog.api.BasicLevel;
43 import org.objectweb.util.monolog.api.Logger;
44
45
46 /**
47  * This handler is called before all application specific handlers
48  * It allows to process some jonas specific stuff like setting
49  * the bean environment for example.
50  * @author Philippe Durieux
51  */

52 public class JonasHandler extends BasicHandler {
53
54     /**
55      * logger
56      */

57     private static Logger logger = Log.getLogger(Log.JONAS_WS_EJBPROVIDER_PREFIX);
58
59     /**
60      * cached initial context
61      */

62     private static InitialContext JavaDoc cachedContext;
63
64     /**
65      * saved component context
66      */

67     private Context JavaDoc savedCtx = null;
68
69     /**
70      * ws home
71      */

72     private JServiceEndpointHome sehome = null;
73
74     /**
75      * Called on reply if a fault occured.
76      * @param msgContext MessageContext
77      */

78     public void onFault(MessageContext msgContext) {
79         logger.log(BasicLevel.DEBUG, "*------* Fault");
80         if (sehome != null) {
81             logger.log(BasicLevel.DEBUG, "*--* Fault");
82             sehome.resetCompCtx(savedCtx);
83             if (savedCtx != null) {
84                 logger.log(BasicLevel.ERROR, "Setting a non null context:" + savedCtx);
85             }
86             sehome = null;
87         }
88     }
89
90     /**
91      * Called on request and then on reply if no fault occured.
92      * @param msgContext MessageContext
93      * @throws AxisFault if the handler cannot get the service endpoint home
94      */

95     public void invoke(MessageContext msgContext) throws AxisFault {
96
97         if (msgContext.getPastPivot()) {
98             logger.log(BasicLevel.DEBUG, "*------* Response");
99             // Response
100
if (sehome != null) {
101                 logger.log(BasicLevel.DEBUG, "*--* Response");
102                 sehome.resetCompCtx(savedCtx);
103                 if (savedCtx != null) {
104                     logger.log(BasicLevel.ERROR, "Setting a non null context:" + savedCtx);
105                 }
106                 sehome = null;
107             }
108         } else {
109             // Request
110
logger.log(BasicLevel.DEBUG, "*------* Request");
111
112             // Get the service class name
113
Handler service = msgContext.getService();
114             String JavaDoc clsName = (String JavaDoc) service.getOption(JOnASEJBProvider.OPTION_SEINTERFACENAME);
115             if (clsName == null) {
116                 // We are not in the case of jonas ejb call: do nothing.
117
return;
118             }
119             logger.log(BasicLevel.DEBUG, "*--* Request");
120
121             // Get ServiceEndpointHome in JNDI
122
String JavaDoc jndiName = (String JavaDoc) service.getOption(JOnASEJBProvider.OPTION_SEJNDINAME);
123             if (jndiName == null) {
124                 logger.log(BasicLevel.ERROR, "Service Endpoint JNDI name is null");
125                 throw new AxisFault("Missing parameter OPTION_SEJNDINAME in service");
126             }
127             try {
128                 InitialContext JavaDoc ic = getCachedContext();
129                 sehome = (JServiceEndpointHome) ic.lookup(jndiName);
130             } catch (NamingException JavaDoc ne) {
131                 logger.log(BasicLevel.ERROR, "Cannot lookup ServiceEndpointHome");
132                 throw new AxisFault("Cannot lookup ServiceEndpointHome: " + jndiName);
133             }
134             
135             // add the security context
136
String JavaDoc username = msgContext.getUsername();
137             if (username != null) {
138                 // Do not forget to initialize the security context
139
SecurityContextHelper.getInstance().login(username, msgContext.getPassword());
140                 // Check the security and throw exception if user is not authorized
141
// to access the EJB before lauching handlers
142
sehome.checkSecurity(msgContext);
143             }
144             
145             // Switch Context on the context of the target bean (java:comp/env)
146
savedCtx = sehome.setCompCtx();
147             if (savedCtx != null) {
148                 logger.log(BasicLevel.ERROR, "Saving a non null context:" + savedCtx);
149             }
150
151         }
152     }
153
154     /**
155      * @return Returns the cached InitialContext (or created a new one)
156      * @throws javax.naming.NamingException when InitialContext creation fails
157      */

158     private InitialContext JavaDoc getCachedContext() throws javax.naming.NamingException JavaDoc {
159         if (cachedContext == null) {
160             cachedContext = new InitialContext JavaDoc();
161         }
162         return cachedContext;
163     }
164 }
165
Popular Tags