001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JOnASEJBProvider.java 6100 2005-01-17 08:33:56Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.ws.axis;
025:
026: import java.lang.reflect.Method;
027:
028: import javax.naming.InitialContext;
029: import javax.naming.NamingException;
030:
031: import org.apache.axis.AxisFault;
032: import org.apache.axis.Handler;
033: import org.apache.axis.MessageContext;
034: import org.apache.axis.i18n.Messages;
035: import org.apache.axis.providers.java.RPCProvider;
036:
037: import org.objectweb.jonas_ejb.container.JServiceEndpoint;
038: import org.objectweb.jonas_ejb.container.JServiceEndpointHome;
039:
040: import org.objectweb.jonas.common.Log;
041: import org.objectweb.jonas.security.ws.SecurityContextHelper;
042:
043: import org.objectweb.util.monolog.api.BasicLevel;
044: import org.objectweb.util.monolog.api.Logger;
045:
046: /**
047: * Expose the service-endpoint interface of the Ejb.
048: *
049: * @author Guillaume Sauthier
050: */
051: public class JOnASEJBProvider extends RPCProvider {
052:
053: /**
054: * Logger
055: */
056: private static Logger logger = null;
057:
058: /**
059: * parameter service-endpoint class name
060: */
061: public static final String OPTION_SEINTERFACENAME = "serviceEndpointInterfaceName";
062:
063: /**
064: * parameter service-endpoint JNDI name
065: */
066: public static final String OPTION_SEJNDINAME = "serviceEndpointJndiName";
067:
068: /**
069: * cached initial context
070: */
071: private static InitialContext cachedContext;
072:
073: /**
074: * Create a new JOnASEJBProvider
075: */
076: public JOnASEJBProvider() {
077: super ();
078: logger = Log.getLogger(Log.JONAS_WS_EJBPROVIDER_PREFIX);
079: logger.log(BasicLevel.DEBUG, "");
080: }
081:
082: /**
083: * Override the default implementation
084: * Return a object which implements the service.
085: * @param msgContext the message context
086: * @param seiName The Service Endpoint Interface classname
087: * @return an object that implements the service
088: * @throws Exception when trying to create a Serviceobject without serviceendpoint name aprameter
089: */
090: protected Object makeNewServiceObject(MessageContext msgContext,
091: String seiName) throws Exception {
092: logger.log(BasicLevel.DEBUG, seiName);
093: if (seiName == null) {
094: logger.log(BasicLevel.ERROR,
095: "Service Endpoint Interface classname is null");
096: // cannot find service-endpoint
097: throw new AxisFault(Messages.getMessage("noOption00",
098: OPTION_SEINTERFACENAME, msgContext.getService()
099: .getName()));
100: }
101:
102: // Get ServiceEndpointHome in JNDI
103: String jndiName = getStrOption(OPTION_SEJNDINAME, msgContext
104: .getService());
105: if (jndiName == null) {
106: logger.log(BasicLevel.ERROR,
107: "Service Endpoint JNDI name is null");
108: throw new AxisFault("Missing parameter in service : "
109: + OPTION_SEJNDINAME);
110: }
111: JServiceEndpointHome sehome = null;
112: try {
113: InitialContext ic = getCachedContext();
114: sehome = (JServiceEndpointHome) ic.lookup(jndiName);
115: } catch (NamingException ne) {
116: logger.log(BasicLevel.ERROR,
117: "Cannot lookup ServiceEndpointHome");
118: throw new AxisFault("Cannot lookup ServiceEndpointHome: "
119: + jndiName);
120: }
121: // Check that the object implements the SEI
122: // TODO
123:
124: // Create a new ServiceEndpoint object
125: JServiceEndpoint se = sehome.create();
126:
127: // Set the MessageContext that can be retrived by the bean
128: se.setMessageContext(msgContext);
129: return se;
130: }
131:
132: /**
133: * Override the default implementation : create a SecurityContext from username and password
134: * @throws Exception if method invokation fail or produce an Exception
135: */
136: protected Object invokeMethod(MessageContext msgContext,
137: Method method, Object obj, Object[] argValues)
138: throws Exception {
139: logger.log(BasicLevel.DEBUG, "");
140: String username = msgContext.getUsername();
141: if (username != null) {
142: // Do not forget to initialize the security context
143: SecurityContextHelper.getInstance().login(username,
144: msgContext.getPassword());
145: }
146: return super .invokeMethod(msgContext, method, obj, argValues);
147: }
148:
149: /**
150: * Override the default implementation
151: * @return Return the option in the configuration that contains the service
152: * class name. In the EJB case, it is the JNDI name of the bean.
153: */
154: protected String getServiceClassNameOptionName() {
155: logger.log(BasicLevel.DEBUG, "");
156: return OPTION_SEINTERFACENAME;
157: }
158:
159: /**
160: * Get a String option by looking first in the service options, and then at
161: * the Handler's options. This allows defaults to be specified at the
162: * provider level, and then overriden for particular services.
163: *
164: * @param optionName the option to retrieve
165: * @param service Option holder
166: *
167: * @return String the value of the option or null if not found in either
168: * scope
169: */
170: private String getStrOption(String optionName, Handler service) {
171: String value = null;
172: if (service != null) {
173: value = (String) service.getOption(optionName);
174: }
175: if (value == null) {
176: value = (String) getOption(optionName);
177: }
178: return value;
179: }
180:
181: /**
182: * @return Returns the cached InitialContext (or created a new one)
183: * @throws javax.naming.NamingException when InitialContext creation fails
184: */
185: private InitialContext getCachedContext()
186: throws javax.naming.NamingException {
187: if (cachedContext == null) {
188: cachedContext = new InitialContext();
189: }
190: return cachedContext;
191: }
192:
193: }
|