package org.codehaus.xfire.transport.http; import java.io.InputStream; import javax.servlet.ServletException; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamReader; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.codehaus.xfire.XFire; import org.codehaus.xfire.XFireFactory; import org.codehaus.xfire.aegis.type.DefaultTypeMappingRegistry; import org.codehaus.xfire.aegis.type.TypeMappingRegistry; import org.codehaus.xfire.service.ServiceEndpoint; import org.codehaus.xfire.service.ServiceRegistry; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import org.codehaus.xfire.soap.Soap11; import org.codehaus.xfire.soap.Soap12; import org.codehaus.xfire.soap.SoapVersion; import org.codehaus.yom.Document; import org.codehaus.yom.Element; import org.codehaus.yom.Elements; import org.codehaus.yom.stax.StaxBuilder; /** * XFire Servlet as Dispatcher including a configuration * of XFire from services.xml in classpath */ public class XFireConfigurableServlet extends XFireServlet { private static final String SOAP_12 = "1.2"; private Log log = LogFactory.getLog(XFireConfigurableServlet.class); private final static String CONFIG_FILE = "services.xml"; private final static String ENCODING_STYLE_URI = "http://schemas.xmlsoap.org/soap/encoding/"; /** * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig) */ public void init() throws ServletException { super.init(); try { XFire xfire = XFireFactory.newInstance().getXFire(); configureXFire(xfire); } catch(Exception e){ log.error("Can't configure XFire",e); } } private void configureXFire(XFire xfire) throws Exception { // get services.xml InputStream in = getClass().getClassLoader().getResourceAsStream(CONFIG_FILE); if(in!=null){ // get xfire service registry ServiceRegistry registry = xfire.getServiceEndpointRegistry(); TypeMappingRegistry typeMappingRegistry = new DefaultTypeMappingRegistry(true); ObjectServiceFactory objSvrFactory = new ObjectServiceFactory(xfire.getTransportManager(),null); // parse xml XMLInputFactory ifactory = XMLInputFactory.newInstance(); XMLStreamReader reader = ifactory.createXMLStreamReader(in); StaxBuilder builder = new StaxBuilder(); Document doc = builder.build(reader); Element root = doc.getRootElement(); Elements contents = root.getChildElements(); for(int i=0;i0) { name = value.get(0).getValue(); } // namespace String namespace = null; value = service.getChildElements("namespace"); if(value.size()>0) { namespace = value.get(0).getValue(); } // soapVersion String soapVersionValue = null; value = service.getChildElements("soapVersion"); if(value.size()>0) { soapVersionValue = value.get(0).getValue(); } // style String style = null; value = service.getChildElements("style"); if(value.size()>0) { style = value.get(0).getValue(); } // use String use = null; value = service.getChildElements("use"); if(value.size()>0) { use = value.get(0).getValue(); } // serviceClass String serviceClass = null; value = service.getChildElements("serviceClass"); if(value.size()>0) { serviceClass = value.get(0).getValue(); } // autoTyped String autoTyped = null; value = service.getChildElements("autoTyped"); if(value.size()>0) { autoTyped = value.get(0).getValue(); } // define Service SoapVersion soapVersion = null; if(SOAP_12.equals(soapVersionValue)) { soapVersion = Soap12.getInstance(); } else { soapVersion = Soap11.getInstance(); } Class clazz = getClass().getClassLoader().loadClass(serviceClass); if(log.isInfoEnabled()) { log.info("Create Service "+name+" with Impl "+serviceClass+" Soap "+soapVersionValue+" style "+style+" use "+use+" in Namespace "+namespace); } ServiceEndpoint svr = objSvrFactory.create(clazz,name,namespace,soapVersion,style,use,ENCODING_STYLE_URI); registry.register(svr); } } } else { log.error("Could't not find "+CONFIG_FILE+" in classpath!"); } } }