1 17 package org.apache.servicemix.jsr181.xfire; 18 19 import java.lang.reflect.Constructor ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import org.codehaus.xfire.XFire; 25 import org.codehaus.xfire.aegis.AegisBindingProvider; 26 import org.codehaus.xfire.aegis.type.DefaultTypeMappingRegistry; 27 import org.codehaus.xfire.aegis.type.TypeMappingRegistry; 28 import org.codehaus.xfire.annotations.AnnotationServiceFactory; 29 import org.codehaus.xfire.annotations.WebAnnotations; 30 import org.codehaus.xfire.annotations.commons.CommonsWebAttributes; 31 import org.codehaus.xfire.service.binding.ObjectServiceFactory; 32 import org.codehaus.xfire.transport.TransportManager; 33 import org.codehaus.xfire.xmlbeans.XmlBeansTypeRegistry; 34 35 public class ServiceFactoryHelper { 36 37 public static final String TM_DEFAULT = "default"; 38 public static final String TM_XMLBEANS = "xmlbeans"; 39 public static final String TM_JAXB2 = "jaxb2"; 40 41 public static final String AN_JSR181 = "jsr181"; 42 public static final String AN_JAVA5 = "java5"; 43 public static final String AN_COMMONS = "commons"; 44 public static final String AN_NONE = "none"; 45 46 private static final Map knownTypeMappings; 47 private static final Map knownAnnotations; 48 49 static { 50 knownTypeMappings = new HashMap (); 51 knownTypeMappings.put(TM_DEFAULT, new DefaultTypeMappingRegistry(true)); 52 knownTypeMappings.put(TM_XMLBEANS, new XmlBeansTypeRegistry()); 53 try { 54 Class cl = Class.forName("org.codehaus.xfire.jaxb2.JaxbTypeRegistry"); 55 Object tr = cl.newInstance(); 56 knownTypeMappings.put(TM_JAXB2, tr); 57 } catch (Throwable e) { 58 } 60 61 knownAnnotations = new HashMap (); 62 knownAnnotations.put(AN_COMMONS, new CommonsWebAttributes()); 63 try { 64 Class cl = Class.forName("org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations"); 65 Object wa = cl.newInstance(); 66 knownAnnotations.put(AN_JAVA5, wa); 67 } catch (Throwable e) { 68 } 70 } 71 72 public static ObjectServiceFactory findServiceFactory( 73 XFire xfire, 74 Class serviceClass, 75 String annotations, 76 String typeMapping) throws Exception { 77 if (annotations != null && AN_JSR181.equals(annotations)) { 79 annotations = AN_JAVA5; 80 } 81 WebAnnotations wa = null; 83 String selectedAnnotations = null; 84 if (annotations != null) { 85 selectedAnnotations = annotations; 86 if (!annotations.equals(AN_NONE)) { 87 wa = (WebAnnotations) knownAnnotations.get(annotations); 88 if (wa == null) { 89 throw new Exception ("Unrecognized annotations: " + annotations); 90 } 91 } 92 } else { 93 for (Iterator it = knownAnnotations.entrySet().iterator(); it.hasNext();) { 94 Map.Entry entry = (Map.Entry ) it.next(); 95 WebAnnotations w = (WebAnnotations) entry.getValue(); 96 if (w.hasWebServiceAnnotation(serviceClass)) { 97 selectedAnnotations = (String ) entry.getKey(); 98 wa = w; 99 break; 100 } 101 } 102 } 103 TypeMappingRegistry tm = null; 105 String selectedTypeMapping = null; 106 if (typeMapping == null) { 107 selectedTypeMapping = (wa == null) ? TM_DEFAULT : TM_JAXB2; 108 } else { 109 selectedTypeMapping = typeMapping; 110 } 111 tm = (TypeMappingRegistry) knownTypeMappings.get(selectedTypeMapping); 112 if (tm == null) { 113 throw new Exception ("Unrecognized typeMapping: " + typeMapping); 114 } 115 ObjectServiceFactory factory = null; 117 if (wa == null) { 118 factory = new ObjectServiceFactory(xfire.getTransportManager(), 119 new AegisBindingProvider(tm)); 120 } else if (selectedAnnotations.equals(AN_JAVA5) && 121 selectedTypeMapping.equals(TM_JAXB2)) { 122 try { 123 Class clazz = Class.forName("org.codehaus.xfire.jaxws.JAXWSServiceFactory"); 124 Constructor ct = clazz.getDeclaredConstructor(new Class [] { TransportManager.class }); 125 factory = (ObjectServiceFactory) ct.newInstance(new Object [] { xfire.getTransportManager() }); 126 } catch (Exception e) { 127 factory = new AnnotationServiceFactory(wa, 128 xfire.getTransportManager(), 129 new AegisBindingProvider(tm)); 130 } 131 } else { 132 factory = new AnnotationServiceFactory(wa, 133 xfire.getTransportManager(), 134 new AegisBindingProvider(tm)); 135 } 136 factory.getSoap11Transports().clear(); 138 factory.getSoap12Transports().clear(); 139 factory.getSoap11Transports().add(JbiTransport.JBI_BINDING); 140 return factory; 141 } 142 143 } 144 | Popular Tags |