KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > xfire > ServiceBean


1 package jfun.yan.xfire;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import jfun.yan.factory.Factory;
10
11 import org.codehaus.xfire.XFire;
12 import org.codehaus.xfire.aegis.AegisBindingProvider;
13 import org.codehaus.xfire.service.Service;
14 import org.codehaus.xfire.service.ServiceFactory;
15 import org.codehaus.xfire.service.binding.ObjectInvoker;
16 import org.codehaus.xfire.service.binding.ObjectServiceFactory;
17 import org.codehaus.xfire.soap.Soap11;
18 import org.codehaus.xfire.soap.SoapVersion;
19 import org.codehaus.xfire.wsdl11.builder.DefaultWSDLBuilderFactory;
20
21
22 /**
23  * A convenience bean which creates a Service from a ServiceFactory instance. If there is no
24  * ServiceFactory set, ServiceBean will create one from the ObjectServiceFactory.
25  */

26 public class ServiceBean {
27     private ServiceFactory serviceFactory;
28
29     private XFire xFire;
30
31     private String JavaDoc name;
32
33     private String JavaDoc namespace;
34
35     private Class JavaDoc serviceInterface;
36
37     private Factory servant;
38     private List JavaDoc inHandlers;
39
40     private List JavaDoc outHandlers;
41
42     private List JavaDoc faultHandlers;
43
44     private List JavaDoc schemas;
45     
46     private Class JavaDoc implementationClass;
47
48     private List JavaDoc properties = new ArrayList JavaDoc();
49
50     /** Some properties to make it easier to work with ObjectServiceFactory */
51
52     private SoapVersion soapVersion = Soap11.getInstance();
53
54     private String JavaDoc use;
55
56     private String JavaDoc style;
57
58     private ScopePolicy scope;
59     
60     public Service buildService(){
61
62         if (serviceFactory == null)
63         {
64             serviceFactory = new ObjectServiceFactory(xFire.getTransportManager(),
65                                                       new AegisBindingProvider());
66         }
67
68         /**
69          * Use the ServiceInterface if that is set, otherwise use the Class of
70          * the service object.
71          */

72         final Class JavaDoc intf = getServiceClass();
73         // Lets set up some properties for the service
74
final Map JavaDoc properties = new HashMap JavaDoc();
75         properties.put(ObjectServiceFactory.SOAP_VERSION, soapVersion);
76         
77         if (style != null)
78             properties.put(ObjectServiceFactory.STYLE, style);
79         if (use != null)
80             properties.put(ObjectServiceFactory.USE, use);
81         
82         if (implementationClass != null)
83         {
84             properties.put(ObjectInvoker.SERVICE_IMPL_CLASS, implementationClass);
85         }
86         
87         // Set the properties
88
copyProperties(properties);
89         
90         final Service xfireService = serviceFactory.create(intf, name, namespace, properties);
91
92         // Register the service
93
xFire.getServiceRegistry().register(xfireService);
94         
95         // If we're referencing a proxy via a Factory, which may be lazy,
96
// set up our invoker using the factory.
97
final Factory servant = getServant();
98         if (servant != null) {
99             xfireService.setInvoker(new FactoryInvoker(servant, scope));
100         }
101
102         if (schemas != null)
103         {
104             ObjectServiceFactory osf = (ObjectServiceFactory) serviceFactory;
105             
106             DefaultWSDLBuilderFactory wbf =
107                 (DefaultWSDLBuilderFactory) osf.getWsdlBuilderFactory();
108             wbf.setSchemaLocations(schemas);
109         }
110
111         // set up in handlers
112
if (xfireService.getInHandlers() == null)
113             xfireService.setInHandlers(getInHandlers());
114         else if (getInHandlers() != null)
115             xfireService.getInHandlers().addAll(getInHandlers());
116
117         // set up out handlers
118
if (xfireService.getOutHandlers() == null)
119             xfireService.setOutHandlers(getOutHandlers());
120         else if (getOutHandlers() != null)
121             xfireService.getOutHandlers().addAll(getOutHandlers());
122
123         // set up fault handlers.
124
if (xfireService.getFaultHandlers() == null)
125             xfireService.setFaultHandlers(getFaultHandlers());
126         else if (getFaultHandlers() != null)
127             xfireService.getFaultHandlers().addAll(getFaultHandlers());
128         return xfireService;
129     }
130
131     /**
132      * Gets the Factory that creates the servant object who backs this service.
133      * @return the Factory that creates the servant object.
134      */

135     public Factory getServant(){
136         return servant;
137     }
138
139     /**
140      * Sets the Factory object that creates the servant object who backs up the service.
141      */

142     public void setServant(Factory servant)
143     {
144         this.servant = servant;
145     }
146
147     /**
148      * Set the service class. The service class is passed to the ServiceFactory's
149      * create method and is used to determine the operations on the service.
150      * @return the service class.
151      */

152     public Class JavaDoc getServiceClass()
153     {
154         return serviceInterface;
155     }
156
157     public void setServiceClass(Class JavaDoc serviceInterface)
158     {
159         this.serviceInterface = serviceInterface;
160     }
161
162     public void setServiceFactory(ServiceFactory serviceFactory)
163     {
164         this.serviceFactory = serviceFactory;
165     }
166
167     public ServiceFactory getServiceFactory()
168     {
169         return this.serviceFactory;
170     }
171
172     /**
173      * Sets the service name. Default is the bean name of this exporter.
174      */

175     public void setName(String JavaDoc name)
176     {
177         this.name = name;
178     }
179
180     /**
181      * Sets the service default namespace. Default is a namespace based on the
182      * package of the {@link #getServiceClass() service interface}.
183      */

184     public void setNamespace(String JavaDoc namespace)
185     {
186         this.namespace = namespace;
187     }
188
189     public List JavaDoc getFaultHandlers()
190     {
191         return faultHandlers;
192     }
193
194     public void setFaultHandlers(List JavaDoc faultHandlers)
195     {
196         this.faultHandlers = faultHandlers;
197     }
198
199     public List JavaDoc getInHandlers()
200     {
201         return inHandlers;
202     }
203
204     public void setInHandlers(List JavaDoc inHandlers)
205     {
206         this.inHandlers = inHandlers;
207     }
208
209     public List JavaDoc getOutHandlers()
210     {
211         return outHandlers;
212     }
213
214     public void setOutHandlers(List JavaDoc outHandlers)
215     {
216         this.outHandlers = outHandlers;
217     }
218
219     public void setXfire(XFire xFire)
220     {
221         this.xFire = xFire;
222     }
223
224     public XFire getXfire()
225     {
226         return xFire;
227     }
228
229     public Class JavaDoc getImplementationClass()
230     {
231         return implementationClass;
232     }
233
234     public void setImplementationClass(Class JavaDoc implementationClass)
235     {
236         this.implementationClass = implementationClass;
237     }
238
239     public List JavaDoc getProperties()
240     {
241         return properties;
242     }
243
244     public void setProperties(List JavaDoc properties)
245     {
246         this.properties = properties;
247     }
248
249
250
251     public ScopePolicy getScope() {
252       return scope;
253     }
254
255     public void setScope(ScopePolicy scope) {
256       this.scope = scope;
257     }
258
259     public String JavaDoc getStyle()
260     {
261         return style;
262     }
263
264     public void setStyle(String JavaDoc style)
265     {
266         this.style = style;
267     }
268
269     public String JavaDoc getUse()
270     {
271         return use;
272     }
273
274     public void setUse(String JavaDoc use)
275     {
276         this.use = use;
277     }
278
279     public SoapVersion getSoapVersion()
280     {
281         return soapVersion;
282     }
283
284     public void setSoapVersion(SoapVersion soapVersion)
285     {
286         this.soapVersion = soapVersion;
287     }
288
289     public List JavaDoc getSchemas()
290     {
291         return schemas;
292     }
293
294     public void setSchemas(List JavaDoc schemas)
295     {
296         this.schemas = schemas;
297     }
298
299     protected void copyProperties(Map JavaDoc properties){
300         for (Iterator JavaDoc iter = getProperties().iterator(); iter.hasNext();) {
301             Object JavaDoc[] keyval = (Object JavaDoc[]) iter.next();
302             String JavaDoc key = (String JavaDoc) keyval[0];
303             Object JavaDoc value = keyval[1];
304             properties.put(key, value);
305         }
306     }
307
308 }
Popular Tags