/**
*
* Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
package org.servicemix.components.jaxws;
import javax.jbi.JBIException;
import javax.jbi.component.ComponentLifeCycle;
import javax.jbi.messaging.ExchangeStatus;
import javax.jbi.messaging.MessageExchange;
import javax.jbi.messaging.MessagingException;
import javax.jbi.messaging.NormalizedMessage;
import javax.xml.ws.handler.MessageContext;
import org.servicemix.MessageExchangeListener;
import org.servicemix.components.util.ComponentSupport;
import org.servicemix.jbi.NoInMessageAvailableException;
import org.springframework.beans.factory.InitializingBean;
import com.sun.xml.ws.binding.BindingImpl;
import com.sun.xml.ws.handler.MessageContextImpl;
import com.sun.xml.ws.server.RuntimeEndpointInfo;
import com.sun.xml.ws.server.Tie;
import com.sun.xml.ws.spi.runtime.WSConnection;
import com.sun.xml.ws.spi.runtime.WebServiceContext;
import com.sun.xml.ws.transport.http.server.WebServiceContextImpl;
/**
* JBI Component used to expose a service with JAX-WS,
* using jbi transports.
*
*/
public class JAXWSInBinding extends ComponentSupport
implements InitializingBean, MessageExchangeListener {
private Object implementor;
private String binding;
private RuntimeEndpointInfo rtEndpointInfo;
public Object getImplementor() {
return implementor;
}
public void setImplementor(Object implementor) {
this.implementor = implementor;
}
public String getBinding() {
return binding;
}
public void setBinding(String binding) {
this.binding = binding;
}
public void onMessageExchange(MessageExchange exchange) throws MessagingException {
NormalizedMessage in = exchange.getMessage("in");
if (in == null) {
throw new NoInMessageAvailableException(exchange);
}
NormalizedMessage out = exchange.createMessage();
try {
Tie tie = new Tie();
WSConnection con = new WSConnectionDelegate(in, out);
MessageContext msgCtxt = new MessageContextImpl();
WebServiceContext wsContext = rtEndpointInfo.getWebServiceContext();
wsContext.setMessageContext(msgCtxt);
tie.handle(con, rtEndpointInfo);
if (isInAndOut(exchange)) {
answer(exchange, out);
} else {
done(exchange);
}
}
catch (Exception e) {
fail(exchange, e);
}
}
public void afterPropertiesSet() throws Exception {
if (implementor == null) {
throw new IllegalArgumentException("You must configure the 'implementor' property");
}
rtEndpointInfo = new RuntimeEndpointInfo();
rtEndpointInfo.setImplementor(implementor);
rtEndpointInfo.setImplementorClass(implementor.getClass());
rtEndpointInfo.setBinding(BindingImpl.getBinding(binding, implementor.getClass(), false));
rtEndpointInfo.init();
rtEndpointInfo.setWebServiceContext(new WebServiceContextImpl());
rtEndpointInfo.injectContext();
rtEndpointInfo.beginService();
}
protected void init() throws JBIException {
if (implementor instanceof ComponentLifeCycle) {
((ComponentLifeCycle) implementor).init(getContext());
}
}
}