001: /**
002: * JOnAS: Java(TM) Open Application Server Copyright (C) 1999 Bull S.A. Contact:
003: * jonas-team@objectweb.org This library is free software; you can redistribute
004: * it and/or modify it under the terms of the GNU Lesser General Public License
005: * as published by the Free Software Foundation; either version 2.1 of the
006: * License, or any later version. This library is distributed in the hope that
007: * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
008: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
009: * Lesser General Public License for more details. You should have received a
010: * copy of the GNU Lesser General Public License along with this library; if
011: * not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
012: * Boston, MA 02111-1307 USA
013: * --------------------------------------------------------------------------
014: * $Id: JServiceEndpointHome.java 9584 2006-09-12 15:10:41Z sauthieg $
015: * --------------------------------------------------------------------------
016: */package org.objectweb.jonas_ejb.container;
017:
018: import java.rmi.RemoteException;
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022:
023: import javax.ejb.EJBException;
024: import javax.naming.Context;
025: import javax.naming.NamingException;
026: import javax.naming.Reference;
027: import javax.naming.StringRefAddr;
028: import javax.xml.namespace.QName;
029: import javax.xml.soap.SOAPBody;
030: import javax.xml.soap.SOAPElement;
031: import javax.xml.soap.SOAPEnvelope;
032: import javax.xml.soap.SOAPException;
033:
034: import org.apache.axis.AxisFault;
035: import org.apache.axis.MessageContext;
036:
037: import org.objectweb.jonas_ejb.deployment.api.MethodDesc;
038: import org.objectweb.jonas_ejb.deployment.api.SessionStatelessDesc;
039: import org.objectweb.jonas_ejb.lib.BeanNaming;
040: import org.objectweb.jonas_ejb.lib.EJBInvocation;
041:
042: import org.objectweb.jonas.common.Log;
043:
044: import org.objectweb.util.monolog.api.BasicLevel;
045: import org.objectweb.util.monolog.api.Logger;
046:
047: /**
048: * This class is a factory for JServiceEndpoint objects.
049: * @author Philippe Durieux
050: */
051: public abstract class JServiceEndpointHome {
052:
053: /**
054: * Logger, used also in the generated part.
055: */
056: protected static Logger logger = Log
057: .getLogger(Log.JONAS_WS_EJBPROVIDER_PREFIX);
058: protected static Logger lognaming = Log
059: .getLogger(Log.JONAS_NAMING_PREFIX);
060:
061: protected SessionStatelessDesc dd;
062:
063: protected JFactory bf;
064:
065: // The static table of all JServiceEndpointHome objects
066: protected static Map sehomeList = new HashMap();
067:
068: /**
069: * constructor
070: * @param dd The Session Deployment Decriptor
071: * @param bf The Session Factory
072: */
073: public JServiceEndpointHome(SessionStatelessDesc dd,
074: JStatelessFactory bf) {
075: if (logger.isLoggable(BasicLevel.DEBUG)) {
076: logger.log(BasicLevel.DEBUG, "");
077: }
078: this .dd = dd;
079: this .bf = bf;
080: }
081:
082: /**
083: * register this bean to JNDI (rebind) We register actually a Reference
084: * object.
085: */
086: protected void register() throws NamingException {
087: if (logger.isLoggable(BasicLevel.DEBUG)) {
088: logger.log(BasicLevel.DEBUG, "");
089: }
090: String name = dd.getJndiServiceEndpointName();
091:
092: // Keep it in the static list for later retrieving.
093: sehomeList.put(name, this );
094:
095: Reference ref = new Reference(
096: "org.objectweb.jonas_ejb.container.JServiceEndpointHome",
097: "org.objectweb.jonas_ejb.container.HomeFactory", null);
098: ref.add(new StringRefAddr("bean.name", name));
099: bf.getInitialContext().rebind(name, ref);
100: }
101:
102: /**
103: * unregister this bean in JNDI (unbind)
104: */
105: protected void unregister() throws NamingException {
106: if (logger.isLoggable(BasicLevel.DEBUG)) {
107: logger.log(BasicLevel.DEBUG, "");
108: }
109: String name = dd.getJndiServiceEndpointName();
110: // unregister in default InitialContext
111: bf.getInitialContext().unbind(name);
112: // remove from the static list
113: sehomeList.remove(name);
114: }
115:
116: /**
117: * Get JServiceEndpointHome by its name
118: * used by HomeFactory to retrieve object from its reference.
119: * @param beanName The Bean JNDI local Name
120: * @return The Bean ServiceEndpointHome
121: */
122: public static JServiceEndpointHome getSEHome(String beanName) {
123: if (logger.isLoggable(BasicLevel.DEBUG)) {
124: logger.log(BasicLevel.DEBUG, "");
125: }
126: JServiceEndpointHome seh = (JServiceEndpointHome) sehomeList
127: .get(beanName);
128: return seh;
129: }
130:
131: /**
132: * unique create method
133: */
134: public JServiceEndpoint create() throws RemoteException {
135: if (TraceEjb.isDebugIc()) {
136: TraceEjb.interp.log(BasicLevel.DEBUG, "");
137: }
138: RequestCtx rctx = bf.preInvoke(MethodDesc.TX_NOT_SET);
139: bf.checkSecurity(null);
140: JStatelessSwitch bs = null;
141: try {
142: bs = (JStatelessSwitch) ((JStatelessFactory) bf)
143: .createEJB();
144: } catch (javax.ejb.AccessLocalException e) {
145: throw new EJBException(
146: "Security Exception thrown by an enterprise Bean",
147: e);
148: } catch (EJBException e) {
149: rctx.sysExc = e;
150: throw e;
151: } catch (RuntimeException e) {
152: rctx.sysExc = e;
153: throw new EJBException(
154: "RuntimeException thrown by an enterprise Bean", e);
155: } catch (Error e) {
156: rctx.sysExc = e;
157: throw new EJBException("Error thrown by an enterprise Bean"
158: + e);
159: } catch (RemoteException e) {
160: rctx.sysExc = e;
161: throw new EJBException("Remote Exception raised:", e);
162: } finally {
163: bf.postInvoke(rctx);
164: }
165: return bs.getServiceEndpoint();
166: }
167:
168: /**
169: * Set the bean environment in current context
170: * @return previous Context
171: */
172: public Context setCompCtx() {
173: if (lognaming.isLoggable(BasicLevel.DEBUG)) {
174: lognaming.log(BasicLevel.DEBUG, "");
175: }
176: return bf.setComponentContext();
177: }
178:
179: /**
180: * Restore old value
181: * @return previous Context
182: */
183: public void resetCompCtx(Context ctx) {
184: if (lognaming.isLoggable(BasicLevel.DEBUG)) {
185: lognaming.log(BasicLevel.DEBUG, "reset ctx:" + ctx);
186: }
187: bf.resetComponentContext(ctx);
188: }
189:
190: /**
191: * Check security with the Meesage Context
192: */
193: public void checkSecurity(MessageContext msgContext) {
194:
195: EJBInvocation ejb = new EJBInvocation();
196: QName q = null;
197: try {
198: SOAPEnvelope env = msgContext.getMessage().getSOAPPart()
199: .getEnvelope();
200: SOAPBody body = env.getBody();
201: Iterator it = body.getChildElements();
202: SOAPElement operation = (SOAPElement) it.next();
203:
204: q = new QName(operation.getNamespaceURI(), operation
205: .getLocalName());
206: ejb.methodPermissionSignature = BeanNaming.getSignature(bf
207: .getEJBName(), msgContext.getOperationByQName(q)
208: .getMethod());
209: } catch (AxisFault e) {
210: // error during getting the operation from the message
211: // not possible to check the security
212: if (lognaming.isLoggable(BasicLevel.WARN))
213: lognaming
214: .log(BasicLevel.WARN,
215: "can't retreive the operation from message...can not check the security");
216: return;
217: } catch (SOAPException e) {
218: // error during getting the operation from the message
219: // not possible to check the security
220: if (lognaming.isLoggable(BasicLevel.WARN))
221: lognaming
222: .log(BasicLevel.WARN,
223: "can't retreive the operation from message...can not check the security");
224: return;
225: }
226:
227: bf.checkSecurity(ejb);
228: }
229:
230: /**
231: * @return A JServiceEndpoint that can be used by the JonasEJBProvider
232: */
233: public abstract JServiceEndpoint createServiceEndpointObject()
234: throws RemoteException;
235:
236: }
|