1 57 58 package org.apache.wsif.base; 59 60 import java.util.HashMap ; 61 import java.util.Iterator ; 62 import java.util.List ; 63 import java.util.Map ; 64 65 import javax.wsdl.Operation; 66 import javax.xml.namespace.QName ; 67 68 import org.apache.wsif.WSIFCorrelationId; 69 import org.apache.wsif.WSIFException; 70 import org.apache.wsif.WSIFMessage; 71 import org.apache.wsif.WSIFOperation; 72 import org.apache.wsif.WSIFPort; 73 import org.apache.wsif.WSIFResponseHandler; 74 import org.apache.wsif.compiler.util.TypeMapping; 75 import org.apache.wsif.logging.Trc; 76 import org.apache.wsif.util.WSIFUtils; 77 import org.apache.wsif.wsdl.extensions.jms.JMSProperty; 78 import org.apache.wsif.wsdl.extensions.jms.JMSPropertyValue; 79 80 public abstract class WSIFDefaultOperation implements WSIFOperation { 81 private static final long serialVersionUID = 1L; 82 transient protected HashMap inJmsProps = new HashMap (); 83 transient protected HashMap outJmsProps = new HashMap (); 84 transient protected HashMap inJmsPropVals = new HashMap (); 85 protected WSIFMessage context; 86 protected boolean closed = false; 87 88 91 public abstract boolean executeRequestResponseOperation( 92 WSIFMessage input, 93 WSIFMessage output, 94 WSIFMessage fault) 95 throws WSIFException; 96 97 100 public abstract void executeInputOnlyOperation(WSIFMessage input) 101 throws WSIFException; 102 103 109 public WSIFCorrelationId executeRequestResponseAsync( 110 WSIFMessage input, 111 WSIFResponseHandler handler) 112 throws WSIFException { 113 throw new WSIFException("asynchronous operations not supportted"); 114 } 115 116 122 public WSIFCorrelationId executeRequestResponseAsync(WSIFMessage input) 123 throws WSIFException { 124 throw new WSIFException("asynchronous operations not supportted"); 125 } 126 127 134 public void fireAsyncResponse(Object response) throws WSIFException { 135 throw new WSIFException("asynchronous operations not supportted"); 136 } 137 138 144 public boolean processAsyncResponse( 145 Object response, 146 WSIFMessage output, 147 WSIFMessage fault) 148 throws WSIFException { 149 throw new WSIFException("asynchronous operations not supportted"); 150 } 151 152 155 public WSIFMessage createInputMessage() { 156 Trc.entry(this); 157 WSIFMessage msg = new WSIFDefaultMessage(); 158 if (msg != null) { 159 try { 161 msg.setMessageDefinition( 162 getOperation().getInput().getMessage()); 163 } catch (Exception e) { 164 Trc.ignoredException(e); 165 } 166 } 167 Trc.exit(msg); 168 return msg; 169 } 170 171 174 public WSIFMessage createInputMessage(String name) { 175 Trc.entry(this, name); 176 WSIFMessage msg = new WSIFDefaultMessage(); 177 if (msg != null) { 178 msg.setName(name); 179 try { 181 msg.setMessageDefinition( 182 getOperation().getInput().getMessage()); 183 } catch (Exception e) { 184 Trc.ignoredException(e); 185 } 186 } 187 Trc.exit(msg); 188 return msg; 189 } 190 191 194 public WSIFMessage createOutputMessage() { 195 Trc.entry(this); 196 WSIFMessage msg = new WSIFDefaultMessage(); 197 if (msg != null) { 198 try { 200 msg.setMessageDefinition( 201 getOperation().getOutput().getMessage()); 202 } catch (Exception e) { 203 Trc.ignoredException(e); 204 } 205 } 206 Trc.exit(msg); 207 return msg; 208 } 209 210 213 public WSIFMessage createOutputMessage(String name) { 214 Trc.entry(this, name); 215 WSIFMessage msg = new WSIFDefaultMessage(); 216 if (msg != null) { 217 msg.setName(name); 218 try { 220 msg.setMessageDefinition( 221 getOperation().getOutput().getMessage()); 222 } catch (Exception e) { 223 Trc.ignoredException(e); 224 } 225 } 226 Trc.exit(msg); 227 return msg; 228 } 229 230 233 public WSIFMessage createFaultMessage() { 234 Trc.entry(this); 235 WSIFMessage wm = new WSIFDefaultMessage(); 236 Trc.exit(wm); 237 return wm; 238 } 239 240 243 public WSIFMessage createFaultMessage(String name) { 244 Trc.entry(this, name); 245 WSIFMessage msg = new WSIFDefaultMessage(); 246 if (msg != null) { 247 msg.setName(name); 248 try { 250 msg.setMessageDefinition( 251 getOperation().getFault(name).getMessage()); 252 } catch (Exception e) { 253 Trc.ignoredException(e); 254 } 255 } 256 Trc.exit(msg); 257 return msg; 258 } 259 260 263 public void setInputJmsProperties(List list) throws WSIFException { 264 Trc.entry(this, list); 265 inJmsProps = makeSomeKindOfJmsMap(list); 266 Trc.exit(); 267 } 268 269 272 public void setOutputJmsProperties(List list) throws WSIFException { 273 Trc.entry(this, list); 274 outJmsProps = makeSomeKindOfJmsMap(list); 275 Trc.exit(); 276 } 277 278 public void setInputJmsProperties(HashMap hm) { 279 Trc.entry(this, hm); 280 inJmsProps = hm; 281 Trc.exit(); 282 } 283 284 public void setOutputJmsProperties(HashMap hm) { 285 Trc.entry(this, hm); 286 outJmsProps = hm; 287 Trc.exit(); 288 } 289 290 public HashMap getInputJmsProperties() { 291 Trc.entry(this); 292 Trc.exit(inJmsProps); 293 return inJmsProps; 294 } 295 296 public HashMap getOutputJmsProperties() { 297 Trc.entry(this); 298 Trc.exit(outJmsProps); 299 return outJmsProps; 300 } 301 302 public abstract WSIFPort getWSIFPort(); 303 304 309 public void addInputJmsPropertyValues(List list) throws WSIFException { 310 Trc.entry(this, list); 311 if (list != null && !list.isEmpty()) { 312 HashMap newPvs = makeSomeKindOfJmsMap(list); 313 newPvs.putAll(inJmsPropVals); 314 inJmsPropVals = newPvs; 315 } 316 Trc.exit(); 317 } 318 319 public void setInputJmsPropertyValues(HashMap hm) { 320 Trc.entry(this, hm); 321 inJmsPropVals = hm; 322 Trc.exit(); 323 } 324 325 public HashMap getInputJmsPropertyValues() { 326 Trc.entry(this); 327 Trc.exit(inJmsPropVals); 328 return inJmsPropVals; 329 } 330 331 334 protected HashMap makeSomeKindOfJmsMap(List list) throws WSIFException { 335 Trc.entry(this, list); 336 Map simpleTypeReg = WSIFUtils.getSimpleTypesMap(); 337 HashMap props = new HashMap (list.size()); 338 for (Iterator it = list.iterator(); it.hasNext();) { 339 Object ee = it.next(); 340 if (ee instanceof JMSProperty) { 341 JMSProperty prop = (JMSProperty) ee; 342 props.put(prop.getPart(), prop.getName()); 343 } else if (ee instanceof JMSPropertyValue) { 344 JMSPropertyValue propVal = (JMSPropertyValue) ee; 345 346 String name = propVal.getName(); 347 if (name == null || name.length() == 0) 348 throw new WSIFException("jms:propertyValue found without a name"); 349 350 QName type = propVal.getType(); 351 if (type == null) 352 throw new WSIFException( 353 "jms:propertyValue " + name + " did not have a type"); 354 if (type.getNamespaceURI() == null || type.getLocalPart() == null) 355 throw new WSIFException( 356 "jms:propertyValue " + name + " has a badly formed type"); 357 358 String value = propVal.getValue(); 359 if (value == null || value.length() == 0) 360 throw new WSIFException( 361 "jms:propertyValue " + name + " did not have a value"); 362 363 TypeMapping tm = (TypeMapping) (simpleTypeReg.get(type)); 364 if (tm == null || tm.javaType == null) 365 throw new WSIFException( 366 "jms:propertyValue " 367 + name 368 + " had a type that was " 369 + "unknown or was not a simple type"); 370 371 Class javaClass = null; 372 try { 373 javaClass = 374 Class.forName( 375 tm.javaType, 376 true, 377 Thread.currentThread().getContextClassLoader()); 378 } catch (ClassNotFoundException cce) { 379 Trc.exception(cce); 380 throw new WSIFException( 381 "Unexpected ClassNotFoundException when processing " 382 + "jms:propertyValue " 383 + name 384 + ". Could not convert the type to a java class. " 385 + cce); 386 } 387 388 Object obj = null; 389 try { 390 if (javaClass.equals(String .class)) 391 obj = value; 392 else if (javaClass.equals(Integer .class)) 393 obj = new Integer (value); 394 else if (javaClass.equals(Boolean .class)) 395 obj = new Boolean (value); 396 else if (javaClass.equals(Byte .class)) 397 obj = new Byte (value); 398 else if (javaClass.equals(Double .class)) 399 obj = new Double (value); 400 else if (javaClass.equals(Float .class)) 401 obj = new Float (value); 402 else if (javaClass.equals(Long .class)) 403 obj = new Long (value); 404 else if (javaClass.equals(Short .class)) 405 obj = new Short (value); 406 else 407 throw new WSIFException( 408 "jms:propertyValue " + name + " had an invalid type"); 409 } catch (NumberFormatException nfe) { 410 Trc.exception(nfe); 411 throw new WSIFException( 412 "jms:propertyValue " 413 + name 414 + " a value that could not " 415 + "be converted into the specified type. Caught NumberFormatException. " 416 + nfe); 417 } 418 419 props.put(name, obj); 420 } 421 } 422 Trc.exit(props); 423 return props; 424 } 425 426 432 public void setContext(WSIFMessage context) { 433 Trc.entry(this, context); 434 if (context == null) { 435 throw new IllegalArgumentException ("context must not be null"); 436 } 437 this.context = context; 438 Trc.exit(); 439 } 440 441 444 public WSIFMessage getContext() throws WSIFException { 445 Trc.entry(this); 446 WSIFMessage contextCopy; 447 try { 448 if (this.context == null) { 449 contextCopy = (WSIFMessage) getWSIFPort().getContext().clone(); 450 } else { 451 contextCopy = (WSIFMessage) this.context.clone(); 452 } 453 } catch (CloneNotSupportedException e) { 454 throw new WSIFException( 455 "CloneNotSupportedException cloning context", e); 456 } 457 Trc.exit(contextCopy); 458 return contextCopy; 459 } 460 461 abstract protected Operation getOperation() throws Exception ; 462 463 protected void close() throws WSIFException { 464 Trc.entry(this); 465 if (closed) 466 throw new WSIFException("Cannot reuse a WSIFOperation to invoke multiple operations"); 467 closed = true; 468 Trc.exit(); 469 } 470 } 471 | Popular Tags |