1 package jfun.yan.xfire; 2 3 import java.beans.IntrospectionException ; 4 import java.util.HashMap ; 5 import java.util.List ; 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 25 public class ServiceNut extends DelegatingNut { 26 private Component default_xfire; 27 public ServiceNut(){ 28 this(Components.useKey("xfire")); 29 } 30 34 public ServiceNut(Component default_xfire){ 35 this.default_xfire = default_xfire; 36 } 37 private Component serviceFactory; 38 39 protected Component xFire; 40 41 private String name; 42 43 private String namespace; 44 45 private Class serviceInterface; 46 47 private List inHandlers; 48 49 private List outHandlers; 50 51 private List faultHandlers; 52 53 private List schemas; 54 55 protected Class implementationClass; 56 57 private List properties; 58 59 60 61 protected SoapVersion soapVersion; 62 63 protected String use; 64 65 protected String style; 66 67 private ScopePolicy scope; 68 69 public List getFaultHandlers() { 70 return faultHandlers; 71 } 72 73 public void setFaultHandlers(List faultHandlers) { 74 this.faultHandlers = faultHandlers; 75 } 76 77 public Class getImplementationClass() { 78 return implementationClass; 79 } 80 81 public void setImplementationClass(Class implementationClass) { 82 this.implementationClass = implementationClass; 83 } 84 85 public List getInHandlers() { 86 return inHandlers; 87 } 88 89 public void setInHandlers(List inHandlers) { 90 this.inHandlers = inHandlers; 91 } 92 93 public String getName() { 94 return name; 95 } 96 97 public void setName(String name) { 98 this.name = name; 99 } 100 101 public String getNamespace() { 102 return namespace; 103 } 104 105 public void setNamespace(String namespace) { 106 this.namespace = namespace; 107 } 108 109 public List getOutHandlers() { 110 return outHandlers; 111 } 112 113 public void setOutHandlers(List outHandlers) { 114 this.outHandlers = outHandlers; 115 } 116 117 public List getProperties() { 118 return properties; 119 } 120 121 public void setProperties(List properties) { 122 this.properties = properties; 123 } 124 125 public List getSchemas() { 126 return schemas; 127 } 128 129 public void setSchemas(List 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 getServiceClass() { 150 return serviceInterface; 151 } 152 153 public void setServiceClass(Class 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 getStyle() { 166 return style; 167 } 168 169 public void setStyle(String style) { 170 this.style = style; 171 } 172 173 public String getUse() { 174 return use; 175 } 176 177 public void setUse(String 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 { 191 195 final Component cc = super.getComponent(); 196 Class impltype = getImplementationClass(); 197 if(impltype==null){ 198 if(cc!=null){ 199 impltype = cc.getType(); 200 } 201 } 202 Class intf = getServiceClass(); 203 if (intf == null) 204 { 205 if(impltype!=null){ 206 intf = impltype; 207 } 208 else{ 209 throw new RuntimeException ("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 map(Object 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 props = new HashMap (); 243 ServiceBuilder add(String key, Object val){ 244 props.put(key, toComponent(val)); 245 return this; 246 } 247 Component getResult()throws IntrospectionException { 248 final Component c = Components.bean(ServiceBean.class) 249 .withProperties(props).map(new jfun.yan.Map(){ 250 public Object map(Object bean){ 251 return ((ServiceBean)bean).buildService(); 252 } 253 }); 254 if(getId()==null && isGloballyDefined()){ 255 registerEagerInstantiation(c); 257 return c; 258 } 259 else 260 return c.cast(Service.class); 261 } 262 } 263 private static Component toListComponent(List 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 [] 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 obj){ 278 if(obj == null){ 279 return Components.useDefault(); 281 } 282 else if(obj instanceof List ){ 283 return toListComponent((List )obj); 284 } 285 else if(obj instanceof Creator[]){ 286 return Components.list((Creator[])obj); 287 } 288 else if(obj instanceof Object []){ 289 return toListComponent((Object [])obj); 290 } 291 else{ 292 return NutsUtils.asComponent(obj); 293 } 294 } 295 296 protected Component getProxyForService(){ 297 return getServant(); 298 } 299 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 |