1 7 8 10 package org.jboss.net.jmx.server; 11 12 14 import org.jboss.axis.AxisFault; 15 import org.jboss.axis.Message; 16 import org.jboss.axis.MessageContext; 17 import org.jboss.axis.description.OperationDesc; 18 import org.jboss.axis.description.ParameterDesc; 19 import org.jboss.axis.description.ServiceDesc; 20 import org.jboss.axis.encoding.TypeMapping; 21 import org.jboss.axis.handlers.soap.SOAPService; 22 import org.jboss.axis.message.RPCElement; 23 import org.jboss.axis.message.RPCParam; 24 import org.jboss.axis.message.SOAPEnvelopeAxisImpl; 25 import org.jboss.axis.providers.BasicProvider; 26 import org.jboss.axis.providers.java.JavaProvider; 27 import org.jboss.axis.utils.JavaUtils; 28 import org.jboss.axis.utils.Messages; 29 import org.jboss.axis.wsdl.fromJava.Emitter; 30 import org.jboss.axis.wsdl.fromJava.Types; 31 import org.jboss.logging.Logger; 32 import org.jboss.mx.util.MBeanServerLocator; 33 import org.jboss.util.Classes; 34 import org.w3c.dom.Document ; 35 import org.xml.sax.SAXException ; 36 37 import javax.management.Attribute ; 38 import javax.management.AttributeNotFoundException ; 39 import javax.management.InstanceNotFoundException ; 40 import javax.management.IntrospectionException ; 41 import javax.management.InvalidAttributeValueException ; 42 import javax.management.MBeanAttributeInfo ; 43 import javax.management.MBeanException ; 44 import javax.management.MBeanInfo ; 45 import javax.management.MBeanOperationInfo ; 46 import javax.management.MBeanParameterInfo ; 47 import javax.management.MBeanServer ; 48 import javax.management.MalformedObjectNameException ; 49 import javax.management.ObjectName ; 50 import javax.management.ReflectionException ; 51 import javax.wsdl.Definition; 52 import javax.wsdl.factory.WSDLFactory; 53 import javax.xml.namespace.QName ; 54 import javax.xml.soap.SOAPException ; 55 import java.util.Iterator ; 56 import java.util.List ; 57 import java.util.Map ; 58 59 82 83 public class MBeanProvider extends BasicProvider 84 { 85 86 private static Logger log = Logger.getLogger(MBeanProvider.class); 87 88 92 93 protected MBeanServer server; 94 95 protected ObjectName name; 96 97 protected Map attributeData = new java.util.HashMap (); 98 protected Map methodData = new java.util.HashMap (); 99 100 protected String allowedMethodsOption = "allowedMethods"; 101 102 protected String allowedReadAttributesOption = "allowedReadAttributes"; 103 104 protected String allowedWriteAttributesOption = "allowedWriteAttributes"; 105 106 110 113 public MBeanProvider() 114 { 115 } 116 117 121 125 public void initServiceDesc(SOAPService service, MessageContext msgCtx) 126 throws AxisFault 127 { 128 try 130 { 131 String allowedMethods = 133 (String )service.getOption(allowedMethodsOption); 134 String allowedReadAttributes = 135 (String )service.getOption(allowedReadAttributesOption); 136 String allowedWriteAttributes = 137 (String )service.getOption(allowedWriteAttributesOption); 138 String objectName = 139 (String )service.getOption(Constants.OBJECTNAME_PROPERTY); 140 String serverId = 141 (String )service.getOption(Constants.MBEAN_SERVER_ID_PROPERTY); 142 try 144 { 145 server = MBeanServerLocator.locateJBoss(); 146 } 147 catch (IllegalStateException e) 148 { 149 throw new AxisFault(Constants.NO_MBEAN_SERVER_FOUND); 150 } 151 name = new ObjectName (objectName); 153 MBeanInfo info = server.getMBeanInfo(name); 154 ServiceDesc serviceDesc = service.getServiceDescription(); 155 java.lang.reflect.Field completeField = 156 ServiceDesc.class.getDeclaredField("introspectionComplete"); 157 completeField.setAccessible(true); 158 completeField.set(serviceDesc, Boolean.TRUE); 159 160 Class implClass = Classes.loadClass(info.getClassName(), msgCtx.getClassLoader()); 161 serviceDesc.setImplClass(implClass); 162 163 serviceDesc.setTypeMapping((TypeMapping)service.getTypeMappingRegistry().getTypeMapping(org.jboss.axis.Constants.URI_DEFAULT_SOAP_ENC)); 164 MBeanOperationInfo [] operations = info.getOperations(); 166 for (int count = 0; count < operations.length; count++) 167 { 168 String operationName = operations[count].getName(); 169 if (allowedMethods != null 171 && allowedMethods.equals("*") 172 || allowedMethods.indexOf(operationName + " ") != -1 173 || allowedMethods.indexOf(" " + operationName) != -1 174 || allowedMethods.equals(operationName)) 175 { 176 OperationDesc opDesc = new OperationDesc(); 178 if (methodData.containsKey(operationName)) 180 { 181 operationName = operationName + count; 182 } 183 opDesc.setName(operationName); 184 opDesc.setElementQName(new QName ("", operationName)); 185 methodData.put(operationName, operations[count]); 187 MBeanParameterInfo [] parameters = 189 operations[count].getSignature(); 190 Class [] parameterTypes = new Class [parameters.length]; 191 for (int count2 = 0; count2 < parameters.length; count2++) 192 { 193 ParameterDesc param = new ParameterDesc(); 194 param.setName("arg" + count2); 195 parameterTypes[count2] = 196 forName(parameters[count2].getType(), msgCtx.getClassLoader()); 197 param.setJavaType(parameterTypes[count2]); 198 param.setTypeQName(forName(parameterTypes[count2], 199 serviceDesc.getTypeMapping())); 200 opDesc.addParameter(param); 201 } 202 opDesc.setReturnClass(forName(operations[count].getReturnType(), msgCtx.getClassLoader())); 203 opDesc.setReturnType(forName(opDesc.getReturnClass(), 204 serviceDesc.getTypeMapping())); 205 serviceDesc.addOperationDesc(opDesc); 207 } } MBeanAttributeInfo [] attributes = info.getAttributes(); 211 for (int count = 0; count < attributes.length; count++) 212 { 213 String attributeName = attributes[count].getName(); 214 if (attributes[count].isReadable() 216 && allowedReadAttributes != null 217 && (allowedReadAttributes.equals("*") 218 || allowedReadAttributes.indexOf(attributeName + " ") != -1 219 || allowedReadAttributes.indexOf(" " + attributeName) != -1 220 || allowedReadAttributes.equals(attributeName))) 221 { 222 OperationDesc opDesc = new OperationDesc(); 223 if (attributes[count].getType().equals("boolean")) 224 { 225 opDesc.setName("is" + attributeName); 226 } 227 else 228 { 229 opDesc.setName("get" + attributeName); 230 } 231 if (methodData.containsKey(opDesc.getName())) 232 { 233 opDesc.setName(opDesc.getName() + count + "A"); 234 } 235 opDesc.setElementQName(new QName ("", opDesc.getName())); 236 attributeData.put(opDesc.getName(), attributes[count]); 237 opDesc.setReturnClass(forName(attributes[count].getType(), msgCtx.getClassLoader())); 238 opDesc.setReturnType(forName(opDesc.getReturnClass(), 239 serviceDesc.getTypeMapping())); 240 serviceDesc.addOperationDesc(opDesc); 242 } if (attributes[count].isWritable() 244 && allowedWriteAttributes != null 245 && (allowedWriteAttributes.equals("*") 246 || allowedWriteAttributes.indexOf(attributeName + " ") != -1 247 || allowedWriteAttributes.indexOf(" " + attributeName) != -1 248 || allowedWriteAttributes.equals(attributeName))) 249 { 250 OperationDesc opDesc = new OperationDesc(); 251 opDesc.setName("set" + attributeName); 252 if (methodData.containsKey(opDesc.getName())) 253 { 254 opDesc.setName(opDesc.getName() + count + "A"); 255 } 256 opDesc.setElementQName(new QName ("", opDesc.getName())); 257 attributeData.put(opDesc.getName(), attributes[count]); 258 ParameterDesc p = new ParameterDesc(); 259 p.setName("arg0"); 260 p.setJavaType(forName(attributes[count].getType(), msgCtx.getClassLoader())); 261 p.setTypeQName(forName(p.getJavaType(), serviceDesc.getTypeMapping())); 262 opDesc.addParameter(p); 263 opDesc.setReturnType(null); 264 serviceDesc.addOperationDesc(opDesc); 266 } } } 269 catch (InstanceNotFoundException e) 270 { 271 throw new AxisFault(Constants.NO_MBEAN_INSTANCE, e); 272 } 273 catch (IntrospectionException e) 274 { 275 throw new AxisFault(Constants.INTROSPECTION_EXCEPTION, e); 276 } 277 catch (ReflectionException e) 278 { 279 throw new AxisFault(Constants.INTROSPECTION_EXCEPTION, e); 280 } 281 catch (ClassNotFoundException e) 282 { 283 throw new AxisFault(Constants.INTROSPECTION_EXCEPTION, e); 284 } 285 catch (MalformedObjectNameException e) 286 { 287 throw new AxisFault(Constants.WRONG_OBJECT_NAME, e); 288 } 289 catch (IllegalAccessException e) 290 { 291 throw new AxisFault(Constants.INTROSPECTION_EXCEPTION, e); 292 } 293 catch (NoSuchFieldException e) 294 { 295 throw new AxisFault(Constants.INTROSPECTION_EXCEPTION, e); 296 } 297 } 298 299 300 protected Class forName(String string, ClassLoader loader) throws ClassNotFoundException 301 { 302 if ("void".equals(string)) 303 { 304 return void.class; 305 } 306 else if ("boolean".equals(string)) 307 { 308 return boolean.class; 309 } 310 else if ("float".equals(string)) 311 { 312 return float.class; 313 } 314 else if ("double".equals(string)) 315 { 316 return double.class; 317 } 318 else if ("int".equals(string)) 319 { 320 return int.class; 321 } 322 else if ("long".equals(string)) 323 { 324 return long.class; 325 } 326 else if ("short".equals(string)) 327 { 328 return short.class; 329 } 330 else if ("byte".equals(string)) 331 { 332 return byte.class; 333 } 334 else if ("char".equals(string)) 335 { 336 return char.class; 337 } 338 else 339 { 340 return org.jboss.util.Classes.loadClass(string, loader); 341 } 342 } 343 344 345 protected QName forName(Class clazz, TypeMapping tm) 346 throws ClassNotFoundException 347 { 348 if (void.class.equals(clazz)) 349 { 350 return null; 351 } 352 else 353 { 354 return tm.getTypeQName(clazz); 355 } 356 } 357 358 361 public void invoke(MessageContext msgContext) throws AxisFault 362 { 363 String serviceName = msgContext.getTargetService(); 365 Message reqMsg = msgContext.getRequestMessage(); 367 SOAPEnvelopeAxisImpl reqEnv = (SOAPEnvelopeAxisImpl)reqMsg.getSOAPEnvelope(); 368 Message resMsg = msgContext.getResponseMessage(); 369 SOAPEnvelopeAxisImpl resEnv = 370 (resMsg == null) 371 ? new SOAPEnvelopeAxisImpl() 372 : (SOAPEnvelopeAxisImpl)resMsg.getSOAPEnvelope(); 373 if (msgContext.getResponseMessage() == null) 375 { 376 resMsg = new Message(resEnv); 377 msgContext.setResponseMessage(resMsg); 378 } 379 Iterator allBodies = reqEnv.getBodyElements().iterator(); 381 while (allBodies.hasNext()) 382 { 383 Object nextBody = allBodies.next(); 384 if (nextBody instanceof RPCElement) 385 { 386 RPCElement body = (RPCElement)nextBody; 387 String mName = body.getMethodName(); 388 List args = null; 389 try 390 { 391 args = body.getParams(); 392 } 393 catch (SAXException e) 394 { 395 throw new AxisFault(Constants.EXCEPTION_OCCURED, e); 396 } 397 Object result = null; 398 try 399 { 400 MBeanAttributeInfo attr = 401 (MBeanAttributeInfo )attributeData.get(mName); 402 if (attr != null) 403 { 404 if (mName.startsWith("get") || mName.startsWith("is")) 405 { 406 result = server.getAttribute(name, attr.getName()); 407 } 408 else 409 { 410 RPCParam p = (RPCParam)args.get(0); 411 Object arg = 412 JavaUtils.convert(p.getValue(), 413 forName(attr.getType(), msgContext.getClassLoader())); 414 server.setAttribute(name, 415 new Attribute (attr.getName(), arg)); 416 result = null; 417 } 418 } 419 else 420 { 421 MBeanOperationInfo meth = 422 (MBeanOperationInfo )methodData.get(mName); 423 MBeanParameterInfo [] params = meth.getSignature(); 424 Object [] arguments = new Object [params.length]; 425 String [] classNames = new String [params.length]; 426 for (int count2 = 0; count2 < params.length; count2++) 427 { 428 classNames[count2] = params[count2].getType(); 429 if (args.size() > count2) 430 { 431 RPCParam param = (RPCParam)args.get(count2); 432 arguments[count2] = 433 JavaUtils.convert(param.getValue(), 434 forName(classNames[count2], msgContext.getClassLoader())); 435 } 436 else 437 { 438 arguments[count2] = null; 439 } 440 } 441 result = 443 server.invoke(name, meth.getName(), arguments, classNames); 444 } 445 RPCElement resBody = new RPCElement(mName + "Response"); 447 resBody.setPrefix(body.getPrefix()); 448 resBody.setNamespaceURI(body.getNamespaceURI()); 449 RPCParam param = new RPCParam(mName + "Result", result); 450 resBody.addParam(param); 451 resEnv.addBodyElement(resBody); 452 resEnv.setEncodingStyle(org.jboss.axis.Constants.URI_DEFAULT_SOAP_ENC); 453 } 454 catch (InstanceNotFoundException e) 455 { 456 throw new AxisFault(Constants.NO_MBEAN_INSTANCE, e); 457 } 458 catch (AttributeNotFoundException e) 459 { 460 throw new AxisFault(Constants.NO_SUCH_ATTRIBUTE, e); 461 } 462 catch (InvalidAttributeValueException e) 463 { 464 throw new AxisFault(Constants.INVALID_ARGUMENT, e); 465 } 466 catch (MBeanException e) 467 { 468 throw new AxisFault(Constants.MBEAN_EXCEPTION, e); 469 } 470 catch (ClassNotFoundException e) 471 { 472 throw new AxisFault(Constants.CLASS_NOT_FOUND, e); 473 } 474 catch (ReflectionException e) 475 { 476 throw new AxisFault(Constants.EXCEPTION_OCCURED, 477 e.getTargetException()); 478 } 479 catch (SOAPException e) 480 { 481 throw new AxisFault(Constants.EXCEPTION_OCCURED, e); 482 } 483 } } } 486 487 488 public void generateWSDL(MessageContext msgCtx) throws AxisFault 489 { 490 SOAPService service = msgCtx.getService(); 491 ServiceDesc serviceDesc = service.getInitializedServiceDesc(msgCtx); 493 if (msgCtx != null) 495 { 496 boolean isSoapAction = 497 msgCtx.getProperty(Constants.ACTION_HANDLER_PRESENT_PROPERTY) 498 == Boolean.TRUE; 499 for (Iterator alloperations = serviceDesc.getOperations().iterator(); 501 alloperations.hasNext(); 502 ) 503 { 504 OperationDesc opDesc = (OperationDesc)alloperations.next(); 505 opDesc.setSoapAction(isSoapAction ? service.getName() : null); 507 } 508 } 509 try 510 { 511 String locationUrl = 513 msgCtx.getStrProp(MessageContext.WSDLGEN_SERV_LOC_URL); 514 if (locationUrl == null) 515 { 516 locationUrl = serviceDesc.getEndpointURL(); 518 } 519 if (locationUrl == null) 520 { 521 locationUrl = msgCtx.getStrProp(MessageContext.TRANS_URL); 523 } 524 String interfaceNamespace = 526 msgCtx.getStrProp(MessageContext.WSDLGEN_INTFNAMESPACE); 527 if (interfaceNamespace == null) 528 { 529 interfaceNamespace = serviceDesc.getDefaultNamespace(); 531 } 532 if (interfaceNamespace == null) 533 { 534 interfaceNamespace = locationUrl; 536 } 537 Emitter emitter = new Emitter(); 538 String alias = (String )service.getOption("alias"); 539 if (alias != null) 540 emitter.setServiceElementName(alias); 541 542 emitter.setCls(serviceDesc.getImplClass()); 544 545 String targetNamespace = 548 (String )service.getOption(JavaProvider.OPTION_WSDL_TARGETNAMESPACE); 549 if (targetNamespace == null || targetNamespace.length() == 0) 550 { 551 targetNamespace = interfaceNamespace; 552 } 553 emitter.setIntfNamespace(targetNamespace); 554 emitter.setLocationUrl(locationUrl); 555 emitter.setServiceDesc(serviceDesc); 556 emitter.setTypeMapping((TypeMapping)service.getTypeMappingRegistry().getTypeMapping(org.jboss.axis.Constants.URI_DEFAULT_SOAP_ENC)); 557 emitter.setDefaultTypeMapping((TypeMapping)msgCtx 558 .getTypeMappingRegistry() 559 .getDefaultTypeMapping()); 560 String wsdlPortType = 561 (String )service.getOption(JavaProvider.OPTION_WSDL_PORTTYPE); 562 String wsdlServiceElement = 563 (String )service.getOption(JavaProvider.OPTION_WSDL_SERVICEELEMENT); 564 String wsdlServicePort = 565 (String )service.getOption(JavaProvider.OPTION_WSDL_SERVICEPORT); 566 if (wsdlPortType != null && wsdlPortType.length() > 0) 567 { 568 emitter.setPortTypeName(wsdlPortType); 569 } 570 if (wsdlServiceElement != null && wsdlServiceElement.length() > 0) 571 { 572 emitter.setServiceElementName(wsdlServiceElement); 573 } 574 if (wsdlServicePort != null && wsdlServicePort.length() > 0) 575 { 576 emitter.setServicePortName(wsdlServicePort); 577 } 578 Definition def = emitter.getWSDL(); 579 def.addNamespace("xsd99", 580 org.jboss.axis.Constants.URI_1999_SCHEMA_XSD); 581 def.addNamespace("xsd00", 582 org.jboss.axis.Constants.URI_2000_SCHEMA_XSD); 583 def.addNamespace("axis", 584 org.jboss.axis.Constants.NS_URI_AXIS); 585 Document doc = 586 WSDLFactory.newInstance().newWSDLWriter().getDocument(def); 587 java.lang.reflect.Field field = 588 Emitter.class.getDeclaredField("types"); 589 field.setAccessible(true); 590 ((Types)field.get(emitter)).insertTypesFragment(doc); 591 msgCtx.setProperty("WSDL", doc); 592 } 593 catch (NoClassDefFoundError e) 594 { 595 log.info(Messages.getMessage("toAxisFault00"), e); 596 throw new AxisFault(e.toString(), e); 597 } 598 catch (Exception e) 599 { 600 log.info(Messages.getMessage("toAxisFault00"), e); 601 throw AxisFault.makeFault(e); 602 } 603 } 604 605 608 public void undo(MessageContext msgContext) 609 { 610 } 612 } 613 | Popular Tags |