001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.geronimo.cxf.ejb;
019:
020: import java.lang.reflect.InvocationTargetException;
021: import java.lang.reflect.Method;
022: import java.util.ArrayList;
023: import java.util.List;
024:
025: import javax.interceptor.InvocationContext;
026: import javax.xml.ws.handler.MessageContext;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.cxf.Bus;
031: import org.apache.cxf.interceptor.Fault;
032: import org.apache.cxf.jaxws.context.WebServiceContextImpl;
033: import org.apache.cxf.jaxws.support.ContextPropertiesMapping;
034: import org.apache.cxf.message.Exchange;
035: import org.apache.cxf.message.FaultMode;
036: import org.apache.cxf.service.invoker.AbstractInvoker;
037: import org.apache.openejb.DeploymentInfo;
038: import org.apache.openejb.RpcContainer;
039:
040: public class EJBMethodInvoker extends AbstractInvoker {
041:
042: private static final Log LOG = LogFactory
043: .getLog(EJBMethodInvoker.class);
044:
045: private DeploymentInfo deploymentInfo;
046: private Bus bus;
047: private EJBEndpoint endpoint;
048:
049: public EJBMethodInvoker(EJBEndpoint endpoint, Bus bus,
050: DeploymentInfo deploymentInfo) {
051: this .endpoint = endpoint;
052: this .bus = bus;
053: this .deploymentInfo = deploymentInfo;
054: }
055:
056: public Object getServiceObject(Exchange context) {
057: return null;
058: }
059:
060: protected Object invoke(Exchange exchange, Object serviceObject,
061: Method m, List<Object> params) {
062: Object result = null;
063:
064: InvocationContext invContext = exchange
065: .get(InvocationContext.class);
066: if (invContext == null) {
067: LOG.debug("PreEJBInvoke");
068: result = preEjbInvoke(exchange, serviceObject, m, params);
069: } else {
070: LOG.debug("EJBInvoke");
071: result = ejbInvoke(exchange, serviceObject, m, params);
072: }
073:
074: return result;
075: }
076:
077: private Object preEjbInvoke(Exchange exchange,
078: Object serviceObject, Method method, List<Object> params) {
079:
080: MessageContext ctx = ContextPropertiesMapping
081: .createWebServiceContext(exchange);
082: WebServiceContextImpl.setMessageContext(ctx);
083:
084: try {
085: EJBInterceptor interceptor = new EJBInterceptor(params,
086: method, this .endpoint, this .bus, exchange);
087: Object[] arguments = { ctx, interceptor };
088:
089: RpcContainer container = (RpcContainer) this .deploymentInfo
090: .getContainer();
091:
092: Class callInterface = this .deploymentInfo
093: .getServiceEndpointInterface();
094: method = getMostSpecificMethod(method, callInterface);
095: Object res = container.invoke(this .deploymentInfo
096: .getDeploymentID(), callInterface, method,
097: arguments, null);
098:
099: if (exchange.isOneWay()) {
100: return null;
101: }
102:
103: List<Object> retList = new ArrayList<Object>(1);
104: if (!((Class) method.getReturnType()).getName().equals(
105: "void")) {
106: retList.add(res);
107: }
108:
109: return retList;
110: } catch (Exception e) {
111: exchange.getInMessage().put(FaultMode.class,
112: FaultMode.UNCHECKED_APPLICATION_FAULT);
113: throw new Fault(e);
114: } finally {
115: WebServiceContextImpl.clear();
116: }
117: }
118:
119: private Object ejbInvoke(Exchange exchange, Object serviceObject,
120: Method m, List<Object> params) {
121: try {
122: Object res = directEjbInvoke(exchange, m, params);
123:
124: if (exchange.isOneWay()) {
125: return null;
126: }
127:
128: List<Object> retList = new ArrayList<Object>(1);
129: if (!((Class) m.getReturnType()).getName().equals("void")) {
130: retList.add(res);
131: }
132:
133: return retList;
134: } catch (InvocationTargetException e) {
135: Throwable t = e.getCause();
136: if (t == null) {
137: t = e;
138: }
139: exchange.getInMessage().put(FaultMode.class,
140: FaultMode.CHECKED_APPLICATION_FAULT);
141: throw new Fault(t);
142: } catch (Exception e) {
143: exchange.getInMessage().put(FaultMode.class,
144: FaultMode.UNCHECKED_APPLICATION_FAULT);
145: throw new Fault(e);
146: }
147: }
148:
149: public Object directEjbInvoke(Exchange exchange, Method m,
150: List<Object> params) throws Exception {
151: InvocationContext invContext = exchange
152: .get(InvocationContext.class);
153: Object[] paramArray;
154: if (params != null) {
155: paramArray = params.toArray();
156: } else {
157: paramArray = new Object[] {};
158: }
159:
160: invContext.setParameters(paramArray);
161: Object res = invContext.proceed();
162:
163: ContextPropertiesMapping.updateWebServiceContext(exchange,
164: (MessageContext) invContext.getContextData());
165:
166: return res;
167: }
168:
169: }
|