1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.apache.cxf.interceptor;
21
22 import java.util.List;
23 import java.util.concurrent.Executor;
24
25 import org.apache.cxf.endpoint.Endpoint;
26 import org.apache.cxf.message.Exchange;
27 import org.apache.cxf.message.Message;
28 import org.apache.cxf.message.MessageContentsList;
29 import org.apache.cxf.phase.AbstractPhaseInterceptor;
30 import org.apache.cxf.phase.Phase;
31 import org.apache.cxf.service.Service;
32 import org.apache.cxf.service.invoker.Invoker;
33
34 /**
35 * Invokes a Binding's invoker with the <code>INVOCATION_INPUT</code> from
36 * the Exchange.
37 * @author Dan Diephouse
38 */
39 public class ServiceInvokerInterceptor extends AbstractPhaseInterceptor<Message> {
40
41
42 public ServiceInvokerInterceptor() {
43 super(Phase.INVOKE);
44 }
45
46 public void handleMessage(final Message message) {
47 final Exchange exchange = message.getExchange();
48 final Endpoint endpoint = exchange.get(Endpoint.class);
49 final Service service = endpoint.getService();
50 final Invoker invoker = service.getInvoker();
51
52 Runnable invocation = new Runnable() {
53
54 public void run() {
55 Exchange runableEx = message.getExchange();
56 Object result = invoker.invoke(runableEx, getInvokee(message));
57 if (!exchange.isOneWay()) {
58 Endpoint ep = exchange.get(Endpoint.class);
59
60 Message outMessage = runableEx.getOutMessage();
61 if (outMessage == null) {
62 outMessage = ep.getBinding().createMessage();
63 exchange.setOutMessage(outMessage);
64 }
65 copyJaxwsProperties(message, outMessage);
66 if (result != null) {
67 MessageContentsList resList = null;
68 if (result instanceof MessageContentsList) {
69 resList = (MessageContentsList)result;
70 } else if (result instanceof List) {
71 resList = new MessageContentsList((List)result);
72 } else if (result.getClass().isArray()) {
73 resList = new MessageContentsList((Object[])result);
74 } else {
75 outMessage.setContent(Object.class, result);
76 }
77 if (resList != null) {
78 outMessage.setContent(List.class, resList);
79 }
80 }
81 }
82 }
83
84 };
85
86 Executor executor = getExecutor(endpoint);
87 if (exchange.get(Executor.class) == executor) {
88 // already executing on the appropriate executor
89 invocation.run();
90 } else {
91 exchange.put(Executor.class, executor);
92 executor.execute(invocation);
93 }
94 }
95
96 private Object getInvokee(Message message) {
97 Object invokee = message.getContent(List.class);
98 if (invokee == null) {
99 invokee = message.getContent(Object.class);
100 }
101 return invokee;
102 }
103
104 /**
105 * Get the Executor for this invocation.
106 * @param endpoint
107 * @return
108 */
109 private Executor getExecutor(final Endpoint endpoint) {
110 return endpoint.getService().getExecutor();
111 }
112
113 private void copyJaxwsProperties(Message inMsg, Message outMsg) {
114 outMsg.put(Message.WSDL_OPERATION, inMsg.get(Message.WSDL_OPERATION));
115 outMsg.put(Message.WSDL_SERVICE, inMsg.get(Message.WSDL_SERVICE));
116 outMsg.put(Message.WSDL_INTERFACE, inMsg.get(Message.WSDL_INTERFACE));
117 outMsg.put(Message.WSDL_PORT, inMsg.get(Message.WSDL_PORT));
118 outMsg.put(Message.WSDL_DESCRIPTION, inMsg.get(Message.WSDL_DESCRIPTION));
119 }
120 }