/**
*
* 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 java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import javax.jbi.JBIException;
import javax.xml.namespace.QName;
import javax.xml.transform.stream.StreamSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.servicemix.components.AbstractComponent;
import org.servicemix.components.xbean.EndpointSpec;
import org.servicemix.components.xbean.XBeanServiceUnit;
import org.servicemix.jbi.jaxp.SourceTransformer;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import com.sun.xml.ws.binding.BindingImpl;
import com.sun.xml.ws.server.DocInfo;
import com.sun.xml.ws.server.RuntimeEndpointInfo;
import com.sun.xml.ws.server.DocInfo.DOC_TYPE;
import com.sun.xml.ws.transport.http.server.WebServiceContextImpl;
public class JAXWSServiceUnit extends XBeanServiceUnit {
private Map<String, RuntimeEndpointInfo> rtEndpointInfos = new ConcurrentHashMap<String, RuntimeEndpointInfo>();
private static final Log logger = LogFactory.getLog(JAXWSServiceUnit.class);
public JAXWSServiceUnit(AbstractComponent component, String serviceUnitName, String serviceUnitRootPath) throws Exception {
super(component, serviceUnitName, serviceUnitRootPath);
for (Iterator iter = services.iterator(); iter.hasNext();) {
EndpointSpec es = (EndpointSpec) iter.next();
RuntimeEndpointInfo rtEndpointInfo = new RuntimeEndpointInfo();
rtEndpointInfo.setImplementor(es.getPojo());
rtEndpointInfo.setImplementorClass(es.getPojo().getClass());
rtEndpointInfo.setBinding(BindingImpl.getBinding(null, es.getPojo().getClass(), false));
rtEndpointInfo.init();
rtEndpointInfo.setWebServiceContext(new WebServiceContextImpl());
rtEndpointInfo.injectContext();
QName serviceName = rtEndpointInfo.getServiceName();
String endpointName = rtEndpointInfo.getPortName().getLocalPart();
if (es.getServiceName() == null) {
es.setServiceName(serviceName);
} else if (!es.getServiceName().equals(serviceName)) {
logger.warn("The service name defined in the wsdl (" + serviceName +
") does not match the service name defined in the endpoint spec (" + es.getServiceName() +
"). WSDL description may be unusable.");
}
if (es.getEndpointName() == null) {
es.setEndpointName(endpointName);
} else if (!es.getEndpointName().equals(endpointName)) {
logger.warn("The endpoint name defined in the wsdl (" + endpointName +
") does not match the endpoint name defined in the endpoint spec (" + es.getEndpointName() +
"). WSDL description may be unusable.");
}
if (rtEndpointInfo.needWSDLGeneration()) {
rtEndpointInfo.generateWSDL();
}
// put service description
try {
SourceTransformer st = new SourceTransformer();
Map<String, DocInfo> docs = rtEndpointInfo.getDocMetadata();
Set<Entry<String, DocInfo>> entries = docs.entrySet();
for (Entry<String, DocInfo> entry : entries) {
DocInfo docInfo = (DocInfo)entry.getValue();
DOC_TYPE docType = docInfo.getDocType();
if (docType == DOC_TYPE.WSDL) {
Node node = st.toDOMNode(new StreamSource(docInfo.getDoc()));
component.setServiceDescription(es.getServiceName(), es.getEndpointName(), (Document) node);
if (logger.isDebugEnabled()) {
logger.debug("WSDL: " + st.toString(node));
}
}
}
} catch (Exception e) {
logger.warn("Could not set endpoint description", e);
}
rtEndpointInfos.put(getKey(es), rtEndpointInfo);
}
}
public RuntimeEndpointInfo getRuntimeEndpointInfo(QName service, String endpoint) {
return rtEndpointInfos.get(getKey(service, endpoint));
}
@Override
public void start() throws JBIException {
for (Iterator iter = services.iterator(); iter.hasNext();) {
EndpointSpec es = (EndpointSpec) iter.next();
RuntimeEndpointInfo rtEndpointInfo = rtEndpointInfos.get(getKey(es));
// start service
rtEndpointInfo.beginService();
}
// activate endpoints
super.start();
}
@Override
public void stop() throws JBIException {
// deactivate endpoints
super.stop();
for (RuntimeEndpointInfo rtEndpointInfo : rtEndpointInfos.values()) {
// stop service
rtEndpointInfo.stop();
}
}
}