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.cxf.interceptor;
019:
020: import java.util.List;
021: import java.util.concurrent.Executor;
022:
023: import org.apache.cxf.endpoint.Endpoint;
024: import org.apache.cxf.message.Exchange;
025: import org.apache.cxf.message.Message;
026: import org.apache.cxf.message.MessageContentsList;
027: import org.apache.cxf.phase.AbstractPhaseInterceptor;
028: import org.apache.cxf.phase.Phase;
029: import org.apache.cxf.service.Service;
030: import org.apache.cxf.service.invoker.Invoker;
031:
032: /**
033: * Invokes a Binding's invoker with the <code>INVOCATION_INPUT</code> from
034: * the Exchange.
035: * @author Dan Diephouse
036: */
037: public class ServiceInvokerInterceptor extends
038: AbstractPhaseInterceptor<Message> {
039:
040: public ServiceInvokerInterceptor() {
041: super (Phase.INVOKE);
042: }
043:
044: public void handleMessage(final Message message) {
045: final Exchange exchange = message.getExchange();
046: final Endpoint endpoint = exchange.get(Endpoint.class);
047: final Service service = endpoint.getService();
048: final Invoker invoker = service.getInvoker();
049:
050: Runnable invocation = new Runnable() {
051:
052: public void run() {
053: Exchange runableEx = message.getExchange();
054: Object result = invoker.invoke(runableEx,
055: getInvokee(message));
056: if (!exchange.isOneWay()) {
057: Endpoint ep = exchange.get(Endpoint.class);
058:
059: Message outMessage = runableEx.getOutMessage();
060: if (outMessage == null) {
061: outMessage = ep.getBinding().createMessage();
062: exchange.setOutMessage(outMessage);
063: }
064: copyJaxwsProperties(message, outMessage);
065: if (result != null) {
066: MessageContentsList resList = null;
067: if (result instanceof MessageContentsList) {
068: resList = (MessageContentsList) result;
069: } else if (result instanceof List) {
070: resList = new MessageContentsList(
071: (List) result);
072: } else if (result.getClass().isArray()) {
073: resList = new MessageContentsList(
074: (Object[]) result);
075: } else {
076: outMessage.setContent(Object.class, result);
077: }
078: if (resList != null) {
079: outMessage.setContent(List.class, resList);
080: }
081: }
082: }
083: }
084:
085: };
086:
087: Executor executor = getExecutor(endpoint);
088: if (exchange.get(Executor.class) == executor) {
089: // already executing on the appropriate executor
090: invocation.run();
091: } else {
092: exchange.put(Executor.class, executor);
093: executor.execute(invocation);
094: }
095: }
096:
097: private Object getInvokee(Message message) {
098: Object invokee = message.getContent(List.class);
099: if (invokee == null) {
100: invokee = message.getContent(Object.class);
101: }
102: return invokee;
103: }
104:
105: /**
106: * Get the Executor for this invocation.
107: * @param endpoint
108: * @return
109: */
110: private Executor getExecutor(final Endpoint endpoint) {
111: return endpoint.getService().getExecutor();
112: }
113:
114: private void copyJaxwsProperties(Message inMsg, Message outMsg) {
115: outMsg.put(Message.WSDL_OPERATION, inMsg
116: .get(Message.WSDL_OPERATION));
117: outMsg.put(Message.WSDL_SERVICE, inMsg
118: .get(Message.WSDL_SERVICE));
119: outMsg.put(Message.WSDL_INTERFACE, inMsg
120: .get(Message.WSDL_INTERFACE));
121: outMsg.put(Message.WSDL_PORT, inMsg.get(Message.WSDL_PORT));
122: outMsg.put(Message.WSDL_DESCRIPTION, inMsg
123: .get(Message.WSDL_DESCRIPTION));
124: }
125: }
|