KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jsr181 > xfire > ServiceFactoryHelper


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jsr181.xfire;
18
19 import java.lang.reflect.Constructor JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
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 JavaDoc TM_DEFAULT = "default";
38     public static final String JavaDoc TM_XMLBEANS = "xmlbeans";
39     public static final String JavaDoc TM_JAXB2 = "jaxb2";
40     
41     public static final String JavaDoc AN_JSR181 = "jsr181";
42     public static final String JavaDoc AN_JAVA5 = "java5";
43     public static final String JavaDoc AN_COMMONS = "commons";
44     public static final String JavaDoc AN_NONE = "none";
45
46     private static final Map JavaDoc knownTypeMappings;
47     private static final Map JavaDoc knownAnnotations;
48     
49     static {
50         knownTypeMappings = new HashMap JavaDoc();
51         knownTypeMappings.put(TM_DEFAULT, new DefaultTypeMappingRegistry(true));
52         knownTypeMappings.put(TM_XMLBEANS, new XmlBeansTypeRegistry());
53         try {
54             Class JavaDoc cl = Class.forName("org.codehaus.xfire.jaxb2.JaxbTypeRegistry");
55             Object JavaDoc tr = cl.newInstance();
56             knownTypeMappings.put(TM_JAXB2, tr);
57         } catch (Throwable JavaDoc e) {
58             // we are in jdk 1.4, do nothing
59
}
60         
61         knownAnnotations = new HashMap JavaDoc();
62         knownAnnotations.put(AN_COMMONS, new CommonsWebAttributes());
63         try {
64             Class JavaDoc cl = Class.forName("org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations");
65             Object JavaDoc wa = cl.newInstance();
66             knownAnnotations.put(AN_JAVA5, wa);
67         } catch (Throwable JavaDoc e) {
68             // we are in jdk 1.4, do nothing
69
}
70     }
71     
72     public static ObjectServiceFactory findServiceFactory(
73                         XFire xfire,
74                         Class JavaDoc serviceClass,
75                         String JavaDoc annotations,
76                         String JavaDoc typeMapping) throws Exception JavaDoc {
77         // jsr181 is synonymous to java5
78
if (annotations != null && AN_JSR181.equals(annotations)) {
79             annotations = AN_JAVA5;
80         }
81         // Determine annotations
82
WebAnnotations wa = null;
83         String JavaDoc 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 JavaDoc("Unrecognized annotations: " + annotations);
90                 }
91             }
92         } else {
93             for (Iterator JavaDoc it = knownAnnotations.entrySet().iterator(); it.hasNext();) {
94                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
95                 WebAnnotations w = (WebAnnotations) entry.getValue();
96                 if (w.hasWebServiceAnnotation(serviceClass)) {
97                     selectedAnnotations = (String JavaDoc) entry.getKey();
98                     wa = w;
99                     break;
100                 }
101             }
102         }
103         // Determine TypeMappingRegistry
104
TypeMappingRegistry tm = null;
105         String JavaDoc 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 JavaDoc("Unrecognized typeMapping: " + typeMapping);
114         }
115         // Create factory
116
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 JavaDoc clazz = Class.forName("org.codehaus.xfire.jaxws.JAXWSServiceFactory");
124                 Constructor JavaDoc ct = clazz.getDeclaredConstructor(new Class JavaDoc[] { TransportManager.class });
125                 factory = (ObjectServiceFactory) ct.newInstance(new Object JavaDoc[] { xfire.getTransportManager() });
126             } catch (Exception JavaDoc 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         // Register only JBI transport in the factory
137
factory.getSoap11Transports().clear();
138         factory.getSoap12Transports().clear();
139         factory.getSoap11Transports().add(JbiTransport.JBI_BINDING);
140         return factory;
141     }
142     
143 }
144
Popular Tags