📄 objectservicefactory.java
字号:
package org.codehaus.xfire.service.binding;import java.io.IOException;import java.lang.reflect.Method;import java.net.URL;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import javax.wsdl.Definition;import javax.wsdl.WSDLException;import javax.wsdl.factory.WSDLFactory;import javax.xml.namespace.QName;import org.codehaus.xfire.MessageContext;import org.codehaus.xfire.XFireFactory;import org.codehaus.xfire.XFireRuntimeException;import org.codehaus.xfire.exchange.MessageSerializer;import org.codehaus.xfire.fault.FaultInfoException;import org.codehaus.xfire.fault.FaultSender;import org.codehaus.xfire.fault.SoapFaultSerializer;import org.codehaus.xfire.fault.XFireFault;import org.codehaus.xfire.handler.CustomFaultHandler;import org.codehaus.xfire.handler.OutMessageSender;import org.codehaus.xfire.service.Binding;import org.codehaus.xfire.service.Endpoint;import org.codehaus.xfire.service.FaultInfo;import org.codehaus.xfire.service.MessageInfo;import org.codehaus.xfire.service.MessagePartContainer;import org.codehaus.xfire.service.MessagePartInfo;import org.codehaus.xfire.service.OperationInfo;import org.codehaus.xfire.service.Service;import org.codehaus.xfire.service.ServiceFactory;import org.codehaus.xfire.service.ServiceInfo;import org.codehaus.xfire.service.documentation.DocumentationProvider;import org.codehaus.xfire.service.documentation.XMLDocumentationBuilder;import org.codehaus.xfire.service.invoker.ObjectInvoker;import org.codehaus.xfire.service.invoker.ScopePolicyEditor;import org.codehaus.xfire.soap.AbstractSoapBinding;import org.codehaus.xfire.soap.Soap11Binding;import org.codehaus.xfire.soap.Soap12Binding;import org.codehaus.xfire.soap.SoapConstants;import org.codehaus.xfire.soap.SoapTransport;import org.codehaus.xfire.transport.Transport;import org.codehaus.xfire.transport.TransportManager;import org.codehaus.xfire.transport.http.SoapHttpTransport;import org.codehaus.xfire.transport.local.LocalTransport;import org.codehaus.xfire.util.ClassLoaderUtils;import org.codehaus.xfire.util.MethodComparator;import org.codehaus.xfire.util.NamespaceHelper;import org.codehaus.xfire.util.ServiceUtils;import org.codehaus.xfire.wsdl.ResourceWSDL;import org.codehaus.xfire.wsdl.WSDLWriter;import org.codehaus.xfire.wsdl11.DefinitionWSDL;import org.codehaus.xfire.wsdl11.ResolverWSDLLocator;import org.codehaus.xfire.wsdl11.builder.DefaultWSDLBuilderFactory;import org.codehaus.xfire.wsdl11.builder.WSDLBuilderAdapter;import org.codehaus.xfire.wsdl11.builder.WSDLBuilderFactory;import org.codehaus.xfire.wsdl11.parser.WSDLServiceConfigurator;import org.xml.sax.InputSource;/** * Creates Services from java objects. This class is meant to be easily overridable * so you can customize how your services are created. * * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a> * @author <a href="mailto:poutsma@mac.com">Arjen Poutsma</a> */public class ObjectServiceFactory implements ServiceFactory{ public static final String PORT_TYPE = "objectServiceFactory.portType"; public static final String PORT_NAME = "objectServiceFactory.portName"; public static final String STYLE = "objectServiceFactory.style"; public static final String USE = "objectServiceFactory.use"; public static final String CREATE_DEFAULT_BINDINGS = "objectServiceFactory.createDefaultBindings"; public static final String SOAP11_TRANSPORTS = "objectServiceFactory.soap11Transports"; public static final String SOAP12_TRANSPORTS = "objectServiceFactory.soap12Transports"; public static final String SCOPE = "objectServiceFactory.scope"; public static final String SCHEMAS = "objectServiceFactory.schemas"; private BindingProvider bindingProvider; private TransportManager transportManager; private String style; private String use; private Set ignoredClasses = new HashSet(); private boolean voidOneWay; private WSDLBuilderFactory wsdlBuilderFactory = new DefaultWSDLBuilderFactory(); private boolean customFaultsEnabled = true; private boolean bindingCreationEnabled = true; private static final DocumentationProvider EMPTY_DOC_PROVIDER = new DocumentationProvider(); private DocumentationProvider documentationProvider = EMPTY_DOC_PROVIDER; private Map customTypesMapping = new HashMap(); private Set soap11Transports = new HashSet(); private Set soap12Transports = new HashSet(); private List serviceConfigurations = new ArrayList(); /** * Initializes a new instance of the <code>ObjectServiceFactory</code>. * Uses the XFireFactory to obtain an instance of the TransportManager. */ public ObjectServiceFactory() { this(XFireFactory.newInstance().getXFire().getTransportManager()); } public Map getCustomTypesMapping() { return customTypesMapping; } public void setCustomTypesMapping(Map customTypesMapping) { this.customTypesMapping = customTypesMapping; } /** * Initializes a new instance of the <code>ObjectServiceFactory</code> with the given transport manager and type * mapping registry. * * @param transportManager the transport manager * @param provider the binding provider */ public ObjectServiceFactory(TransportManager transportManager, BindingProvider provider) { this(transportManager); this.bindingProvider = provider; } /** * Initializes a new instance of the <code>ObjectServiceFactory</code>. */ public ObjectServiceFactory(TransportManager transportManager) { this.transportManager = transportManager; setStyle(SoapConstants.STYLE_WRAPPED); setUse(SoapConstants.USE_LITERAL); DefaultServiceConfiguration config = new DefaultServiceConfiguration(); config.setServiceFactory(this); serviceConfigurations.add(config); soap11Transports.add(SoapHttpTransport.SOAP11_HTTP_BINDING); soap11Transports.add(LocalTransport.BINDING_ID); soap12Transports.add(LocalTransport.BINDING_ID); ignoredClasses.add("java.lang.Object"); ignoredClasses.add("java.lang.Throwable"); ignoredClasses.add("org.omg.CORBA_2_3.portable.ObjectImpl"); ignoredClasses.add("org.omg.CORBA.portable.ObjectImpl"); ignoredClasses.add("javax.ejb.EJBObject"); ignoredClasses.add("javax.rmi.CORBA.Stub"); } public ObjectServiceFactory(BindingProvider bp) { this(XFireFactory.newInstance().getXFire().getTransportManager(), bp); } public BindingProvider getBindingProvider() { if (bindingProvider == null) { try { bindingProvider = (BindingProvider) ClassLoaderUtils .loadClass("org.codehaus.xfire.aegis.AegisBindingProvider", getClass()).newInstance(); } catch (Exception e) { throw new XFireRuntimeException("Couldn't find a binding provider!", e); } } return bindingProvider; } /** * Creates a service via <code>create(Class)</code>. It then configures * the bindings and endpoints on the service via the WSDL. */ public Service create(Class clazz, QName name, URL wsdlUrl, Map properties) { try { return create(clazz, name, WSDLFactory.newInstance().newWSDLReader().readWSDL(new ResolverWSDLLocator(null, new InputSource(wsdlUrl.openStream()))), properties, new ResourceWSDL(wsdlUrl)); } catch (WSDLException e) { throw new XFireRuntimeException("Could not load WSDL.", e); } catch (IOException e) { throw new XFireRuntimeException("Could not load WSDL.", e); } } public Service create(Class clazz, QName name, Definition def, Map properties) { return create(clazz, name, def, properties, new DefinitionWSDL(def)); } public Service create(Class clazz, QName name, Definition def, Map properties, WSDLWriter writer) { if (properties == null) properties = new HashMap(); properties.put(CREATE_DEFAULT_BINDINGS, Boolean.FALSE); if (name == null) { Map services = def.getServices(); javax.wsdl.Service service = (javax.wsdl.Service) getOnlyElem(services); if (service != null) { name = service.getQName(); } } Service service; if (name != null) service = create(clazz, name.getLocalPart(), name.getNamespaceURI(), properties); else service = create(clazz, properties); if (name != null) service.setName(name); service.setWSDLWriter(writer); try { WSDLServiceConfigurator config = new WSDLServiceConfigurator(service, def, transportManager); config.configure(); } catch (Exception e) { if (e instanceof XFireRuntimeException) throw (XFireRuntimeException) e; throw new XFireRuntimeException("Couldn't configure service.", e); } for (Iterator itr = service.getBindings().iterator(); itr.hasNext();) { Binding b = (Binding) itr.next(); for (Iterator oitr = service.getServiceInfo().getOperations().iterator(); oitr.hasNext();) { OperationInfo op = (OperationInfo) oitr.next(); configureHeaders(service, op, b); } if (b instanceof AbstractSoapBinding) { b.setSerializer(getSerializer((AbstractSoapBinding) b)); } service.getBindingProvider().initialize(service, b); } service.getBindingProvider().initialize(service); return service; } /** * Returns the only value in a Map * * @param map * @return the only value in the map, if it contained exactly 1 key/value * pair <br> * <code>null</code>, otherwise <br> */ private Object getOnlyElem(Map map) { if (map.size() == 1) { Set keySet = map.keySet(); Iterator i = keySet.iterator(); return map.get(i.next()); } else { return null; } } protected void configureHeaders(Service service, OperationInfo op, Binding b) { Method method = op.getMethod(); Class[] paramClasses = method.getParameterTypes(); MessagePartContainer inHeaders = b.getHeaders(op.getInputMessage()); MessagePartContainer outHeaders = null; if (op.hasOutput()) outHeaders = b.getHeaders(op.getOutputMessage()); for (int j = 0; j < paramClasses.length; j++) { if (!paramClasses[j].equals(MessageContext.class) && isHeader(method, j)) { final QName q = getInParameterName(service, op, method, j, false); if (isOutParam(method, j)) { MessagePartInfo part = outHeaders.getMessagePart(q); if (part == null) throw new XFireRuntimeException("Could not find header " + q + " in wsdl for operation " + op.getName()); part.setTypeClass(paramClasses[j]); part.setIndex(j); part.setSchemaType(null); } if (isInParam(method, j)) { MessagePartInfo part = inHeaders.getMessagePart(q); if (part == null) throw new XFireRuntimeException("Could not find header " + q + " in wsdl for operation " + op.getName()); part.setTypeClass(paramClasses[j]); part.setIndex(j); part.setSchemaType(null); } } } } /** * Creates a service from the specified class. The service name will be the * unqualified class name. The namespace will be based on the package. * The service will use soap version 1.1, wrapped style, and literal use. * * @param clazz * The service class used to populate the operations and * parameters. If the class is an interface, then the * implementation class that implements that interface must be * set via {@link Service#setProperty(String, Object)} with the * property key being * {@link org.codehaus.xfire.service.invoker.ObjectInvoker#SERVICE_IMPL_CLASS} * @return The service. */ public Service create(Class clazz) { return create(clazz, (Map) null); } /** * Creates a service from the specified class. The service name will be the * unqualified class name. The namespace will be based on the package. * The service will use soap version 1.1, wrapped style, and literal use. * * @param clazz * The service class used to populate the operations and * parameters. If the class is an interface, then the * implementation class that implements that interface must be * set via {@link Service#setProperty(String, Object)} with the * property key being * {@link org.codehaus.xfire.service.invoker.ObjectInvoker#SERVICE_IMPL_CLASS} * @return The service. */ public Service create(Class clazz, Map properties) { return create(clazz, (String) null, (String) null, properties); } protected String makeServiceNameFromClassName(Class clazz) { return ServiceUtils.makeServiceNameFromClassName(clazz); } /** * Creates a service from the specified class, soap version, style and use. The returned service will have a name * based on the class name, and a namespace based on the class package. * <p/> * Some parameters can be <code>null</code>, and will be replaced with sensible defaults if so. See the specific * parameters for more info. * * @param clazz The service class used to populate the operations and parameters. * @param name The name of the service. If <code>null</code>, a name will be generated from the class * name. * @param namespace The default namespace of the service. If <code>null</code>, a namespace will be generated * from the class package. * @return The service. */ public Service create(Class clazz, String name, String namespace, Map properties)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -