KickJava   Java API By Example, From Geeks To Geeks.

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


1 package jfun.yan.xfire;
2
3 import java.beans.IntrospectionException JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.List JavaDoc;
6
7 import org.codehaus.xfire.service.Service;
8 import org.codehaus.xfire.soap.SoapVersion;
9
10 import jfun.yan.Component;
11 import jfun.yan.Components;
12 import jfun.yan.Creator;
13 import jfun.yan.factory.Factory;
14 import jfun.yan.factory.GlobalScope;
15 import jfun.yan.factory.PooledFactory;
16 import jfun.yan.xml.NutsUtils;
17 import jfun.yan.xml.nuts.DelegatingNut;
18
19 /**
20  * The nut class that wraps up a component as a service.
21  * <p>
22  * @author Ben Yu
23  * Feb 2, 2006 6:40:08 PM
24  */

25 public class ServiceNut extends DelegatingNut {
26   private Component default_xfire;
27   public ServiceNut(){
28     this(Components.useKey("xfire"));
29   }
30   /**
31    * Create a ServiceNut object.
32    * @param default_xfire the default xfire component if not specified.
33    */

34   public ServiceNut(Component default_xfire){
35     this.default_xfire = default_xfire;
36   }
37   private /*ServiceFactory*/ Component serviceFactory;
38
39   protected /*XFire*/ Component xFire;
40
41   private String JavaDoc name;
42
43   private String JavaDoc namespace;
44
45   private Class JavaDoc serviceInterface;
46
47   private List JavaDoc inHandlers;
48
49   private List JavaDoc outHandlers;
50
51   private List JavaDoc faultHandlers;
52
53   private List JavaDoc schemas;
54   
55   protected Class JavaDoc implementationClass;
56
57   private List JavaDoc properties;
58
59   /** Some properties to make it easier to work with ObjectServiceFactory */
60
61   protected SoapVersion soapVersion;
62
63   protected String JavaDoc use;
64
65   protected String JavaDoc style;
66
67   private ScopePolicy scope;
68   
69   public List JavaDoc getFaultHandlers() {
70     return faultHandlers;
71   }
72
73   public void setFaultHandlers(List JavaDoc faultHandlers) {
74     this.faultHandlers = faultHandlers;
75   }
76
77   public Class JavaDoc getImplementationClass() {
78     return implementationClass;
79   }
80
81   public void setImplementationClass(Class JavaDoc implementationClass) {
82     this.implementationClass = implementationClass;
83   }
84
85   public List JavaDoc getInHandlers() {
86     return inHandlers;
87   }
88
89   public void setInHandlers(List JavaDoc inHandlers) {
90     this.inHandlers = inHandlers;
91   }
92
93   public String JavaDoc getName() {
94     return name;
95   }
96
97   public void setName(String JavaDoc name) {
98     this.name = name;
99   }
100
101   public String JavaDoc getNamespace() {
102     return namespace;
103   }
104
105   public void setNamespace(String JavaDoc namespace) {
106     this.namespace = namespace;
107   }
108
109   public List JavaDoc getOutHandlers() {
110     return outHandlers;
111   }
112
113   public void setOutHandlers(List JavaDoc outHandlers) {
114     this.outHandlers = outHandlers;
115   }
116
117   public List JavaDoc getProperties() {
118     return properties;
119   }
120
121   public void setProperties(List JavaDoc properties) {
122     this.properties = properties;
123   }
124
125   public List JavaDoc getSchemas() {
126     return schemas;
127   }
128
129   public void setSchemas(List JavaDoc schemas) {
130     this.schemas = schemas;
131   }
132
133   public ScopePolicy getScope() {
134     return scope;
135   }
136
137   public void setScope(ScopePolicy scope) {
138     this.scope = scope;
139   }
140
141   public Component getServiceFactory() {
142     return serviceFactory;
143   }
144
145   public void setServiceFactory(Component serviceFactory) {
146     this.serviceFactory = serviceFactory;
147   }
148
149   public Class JavaDoc getServiceClass() {
150     return serviceInterface;
151   }
152
153   public void setServiceClass(Class JavaDoc serviceInterface) {
154     this.serviceInterface = serviceInterface;
155   }
156
157   public SoapVersion getSoapVersion() {
158     return soapVersion;
159   }
160
161   public void setSoapVersion(SoapVersion soapVersion) {
162     this.soapVersion = soapVersion;
163   }
164
165   public String JavaDoc getStyle() {
166     return style;
167   }
168
169   public void setStyle(String JavaDoc style) {
170     this.style = style;
171   }
172
173   public String JavaDoc getUse() {
174     return use;
175   }
176
177   public void setUse(String JavaDoc use) {
178     this.use = use;
179   }
180
181   public Component getXFire() {
182     return xFire;
183   }
184
185   public void setXFire(Component fire) {
186     xFire = fire;
187   }
188
189
190   public Component eval() throws IntrospectionException JavaDoc {
191     /**
192      * Use the ServiceInterface if that is set, otherwise use the Class of
193      * the service object.
194      */

195     final Component cc = super.getComponent();
196     Class JavaDoc impltype = getImplementationClass();
197     if(impltype==null){
198       if(cc!=null){
199         impltype = cc.getType();
200       }
201     }
202     Class JavaDoc intf = getServiceClass();
203     if (intf == null)
204     {
205       if(impltype!=null){
206         intf = impltype;
207       }
208       else{
209             throw new RuntimeException JavaDoc("Error creating service " + name +
210                     ". The service class or the service bean must be set!");
211       }
212     }
213     
214     final ServiceBuilder builder = new ServiceBuilder();
215     builder.add("faultHandlers", this.faultHandlers)
216     .add("xfire", this.xFire==null?this.default_xfire:this.xFire)
217     .add("servant", toLazyFactory(this.getProxyForService()))
218     .add("implementationClass", impltype)
219     .add("inHandlers", this.inHandlers)
220     .add("name", this.name==null?this.getId():this.name)
221     .add("namespace", this.namespace)
222     .add("outHandlers", this.outHandlers)
223     .add("properties", this.properties)
224     .add("schemas", this.schemas)
225     .add("scope", this.scope)
226     .add("serviceClass", intf)
227     .add("serviceFactory", this.serviceFactory)
228     .add("soapVersion", this.soapVersion)
229     .add("style", this.style)
230     .add("use", this.use);
231     return builder.getResult();
232   }
233   private static final jfun.yan.Map laziness = new jfun.yan.Map(){
234     public Object JavaDoc map(Object JavaDoc f){
235       return new PooledFactory((Factory)f, new GlobalScope());
236     }
237   };
238   private static Component toLazyFactory(Component cc){
239     return cc.factory().map(laziness);
240   }
241   private class ServiceBuilder{
242     private final HashMap JavaDoc props = new HashMap JavaDoc();
243     ServiceBuilder add(String JavaDoc key, Object JavaDoc val){
244       props.put(key, toComponent(val));
245       return this;
246     }
247     Component getResult()throws IntrospectionException JavaDoc{
248       final Component c = Components.bean(ServiceBean.class)
249         .withProperties(props).map(new jfun.yan.Map(){
250           public Object JavaDoc map(Object JavaDoc bean){
251             return ((ServiceBean)bean).buildService();
252           }
253         });
254       if(getId()==null && isGloballyDefined()){
255         //instantiate it using "name".
256
registerEagerInstantiation(c);
257         return c;
258       }
259       else
260         return c.cast(Service.class);
261     }
262   }
263   private static Component toListComponent(List JavaDoc l){
264     final Component[] elems = new Component[l.size()];
265     for(int i=0; i<elems.length; i++){
266       elems[i] = NutsUtils.asComponent(l.get(i));
267     }
268     return Components.list(elems);
269   }
270   private static Component toListComponent(Object JavaDoc[] arr){
271     final Component[] elems = new Component[arr.length];
272     for(int i=0; i<elems.length; i++){
273       elems[i] = NutsUtils.asComponent(arr[i]);
274     }
275     return Components.list(elems);
276   }
277   private static Component toComponent(Object JavaDoc obj){
278     if(obj == null){
279       //for null, we use the default value.
280
return Components.useDefault();
281     }
282     else if(obj instanceof List JavaDoc){
283       return toListComponent((List JavaDoc)obj);
284     }
285     else if(obj instanceof Creator[]){
286       return Components.list((Creator[])obj);
287     }
288     else if(obj instanceof Object JavaDoc[]){
289       return toListComponent((Object JavaDoc[])obj);
290     }
291     else{
292       return NutsUtils.asComponent(obj);
293     }
294   }
295
296   protected Component getProxyForService(){
297       return getServant();
298   }
299   /**
300    * Get the Component for the actual servant.
301    */

302   public Component getServant(){
303     final Component cc = getComponent();
304     if(cc!=null) return cc;
305     if(this.implementationClass != null)
306       return Components.ctor(this.implementationClass);
307     if(this.serviceInterface != null)
308       return Components.ctor(this.serviceInterface);
309     throw raise(
310         "at least one of \"component\", \"serviceClass\" or \"implementationClass\" has to be specified.");
311   }
312 }
313
Popular Tags