View Javadoc

1    /****************************************************************************
2    * Copyright (c) 2005, 2006, 2007, 2008, 2009 Imola Informatica.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the LGPL License v2.1
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/licenses/lgpl.html
7    ****************************************************************************/
8   package it.imolinfo.jbi4corba.jbi.cxf;
9   
10  import it.imolinfo.jbi4corba.Logger;
11  import it.imolinfo.jbi4corba.LoggerFactory;
12  import it.imolinfo.jbi4corba.webservice.runtime.ProviderServiceInvoker;
13  
14  import java.util.List;
15  
16  import org.apache.cxf.endpoint.Endpoint;
17  import org.apache.cxf.message.Exchange;
18  import org.apache.cxf.message.Message;
19  import org.apache.cxf.message.MessageContentsList;
20  import org.apache.cxf.phase.AbstractPhaseInterceptor;
21  import org.apache.cxf.phase.Phase;
22  import org.apache.cxf.service.Service;
23  import org.apache.cxf.service.invoker.Invoker;
24  
25  /**
26   * Always single-threaded CXF Service invoker. 
27   */
28  public class Jbi4CorbaServiceInvokerInterceptor extends
29  		AbstractPhaseInterceptor<Message> {
30  
31  	/**
32  	 * Logger.
33  	 */
34  	private static final Logger LOG = LoggerFactory
35  			.getLogger(ProviderServiceInvoker.class);
36  
37  	public Jbi4CorbaServiceInvokerInterceptor() {
38  		super(Phase.INVOKE);
39  	}
40  
41  	@SuppressWarnings("unchecked")
42  	public void handleMessage(final Message message) {
43  		final Exchange exchange = message.getExchange();
44  		final Endpoint endpoint = exchange.get(Endpoint.class);
45  		final Service service = endpoint.getService();
46  		final Invoker invoker = service.getInvoker();
47  
48  		LOG.debug("Executing the invocation in the SAME thread ["
49  				+ Thread.currentThread().getId() + "]");
50  
51  		Exchange runableEx = message.getExchange();
52  
53  		Object result = invoker.invoke(runableEx, getInvokee(message));
54  		if (!exchange.isOneWay()) {
55  			Endpoint ep = exchange.get(Endpoint.class);
56  
57  			Message outMessage = runableEx.getOutMessage();
58  			if (outMessage == null) {
59  				outMessage = ep.getBinding().createMessage();
60  				exchange.setOutMessage(outMessage);
61  			}
62  			copyJaxwsProperties(message, outMessage);
63  			if (result != null) {
64  				MessageContentsList resList = null;
65  				if (result instanceof MessageContentsList) {
66  					resList = (MessageContentsList) result;
67  				} else if (result instanceof List) {
68  					resList = new MessageContentsList((List) result);
69  				} else if (result.getClass().isArray()) {
70  					resList = new MessageContentsList((Object[]) result);
71  				} else {
72  					outMessage.setContent(Object.class, result);
73  				}
74  				if (resList != null) {
75  					outMessage.setContent(List.class, resList);
76  				}
77  			}
78  		}
79  
80  	}
81  
82  	private Object getInvokee(Message message) {
83  		Object invokee = message.getContent(List.class);
84  		if (invokee == null) {
85  			invokee = message.getContent(Object.class);
86  		}
87  		return invokee;
88  	}
89  	
90  	private void copyJaxwsProperties(Message inMsg, Message outMsg) {
91  		outMsg.put(Message.WSDL_OPERATION, inMsg.get(Message.WSDL_OPERATION));
92  		outMsg.put(Message.WSDL_SERVICE, inMsg.get(Message.WSDL_SERVICE));
93  		outMsg.put(Message.WSDL_INTERFACE, inMsg.get(Message.WSDL_INTERFACE));
94  		outMsg.put(Message.WSDL_PORT, inMsg.get(Message.WSDL_PORT));
95  		outMsg.put(Message.WSDL_DESCRIPTION, inMsg
96  				.get(Message.WSDL_DESCRIPTION));
97  	}	
98  }