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