1 25 26 27 package org.objectweb.jonas.resource; 28 29 import java.io.PrintWriter ; 30 import java.lang.reflect.Method ; 31 import java.text.SimpleDateFormat ; 32 import java.util.Date ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.Properties ; 36 import java.util.Vector ; 37 38 import javax.management.MBeanServer ; 39 import javax.management.ObjectName ; 40 import javax.management.modelmbean.ModelMBean ; 41 import javax.naming.BinaryRefAddr ; 42 import javax.naming.Context ; 43 import javax.naming.Reference ; 44 import javax.naming.StringRefAddr ; 45 import javax.resource.Referenceable ; 46 import javax.resource.spi.ConnectionManager ; 47 import javax.resource.spi.ManagedConnectionFactory ; 48 import javax.resource.spi.ResourceAdapter ; 49 import javax.resource.spi.ResourceAdapterAssociation ; 50 51 import org.apache.commons.modeler.ManagedBean; 52 import org.apache.commons.modeler.Registry; 53 import org.objectweb.jonas.common.JNDIUtils; 54 import org.objectweb.jonas.common.Log; 55 import org.objectweb.jonas.jmx.J2eeObjectName; 56 import org.objectweb.jonas.naming.CompNamingContext; 57 import org.objectweb.jonas_ejb.deployment.api.ActivationConfigPropertyDesc; 58 import org.objectweb.jonas_rar.deployment.api.ConfigPropertyDesc; 59 import org.objectweb.jonas_rar.deployment.api.ConnectorDesc; 60 import org.objectweb.jonas_rar.deployment.api.JdbcConnParamsDesc; 61 import org.objectweb.jonas_rar.deployment.api.JonasConfigPropertyDesc; 62 import org.objectweb.jonas_rar.deployment.api.JonasConnectorDesc; 63 import org.objectweb.jonas_rar.deployment.api.PoolParamsDesc; 64 import org.objectweb.transaction.jta.TransactionManager; 65 import org.objectweb.util.monolog.api.BasicLevel; 66 import org.objectweb.util.monolog.api.Logger; 67 68 74 75 public class ResourceUtility { 76 77 80 private static Logger logger = null; 81 84 private static Logger setterLogger = null; 85 88 private static Logger manageLogger = null; 89 90 93 private Registry oRegistry = null; 94 95 98 private MBeanServer mbeanServer = null; 99 100 102 105 public static final String JCD = "JCD"; 106 109 public static final String JAS = "JAS"; 110 113 public static final String JAO = "JAO"; 114 115 119 public ResourceUtility() { 120 } 121 122 131 public ResourceUtility(MBeanServer mbeanServer, Registry oRegistry, 132 Logger log, Logger sLog, Logger mLog) { 133 this.mbeanServer = mbeanServer; 134 this.oRegistry = oRegistry; 135 logger = log; 136 setterLogger = sLog; 137 manageLogger = mLog; 138 } 139 140 147 public ConfigPropertyDesc [] buildConfigProperty(List raCfg, 148 List jRaCfg1, 149 List jRaCfg2) { 150 Vector cfVec = null; 151 ConfigPropertyDesc[] configs = null; 152 153 if (raCfg != null) { 155 for (Iterator i = raCfg.iterator(); i.hasNext();) { 156 if (cfVec == null) { 157 cfVec = new Vector (); 158 } 159 cfVec.add(new ConfigPropertyDesc((ConfigPropertyDesc) i.next())); 160 } 161 } else { ActivationConfigPropertyDesc acp = null; 163 ConfigPropertyDesc cp = null; 164 if (jRaCfg1 != null) { 165 for (Iterator i = jRaCfg1.iterator(); i.hasNext();) { 166 if (cfVec == null) { 167 cfVec = new Vector (); 168 } 169 acp = (ActivationConfigPropertyDesc) i.next(); 170 cp = new ConfigPropertyDesc(); 171 cp.setConfigPropertyName(acp.getActivationConfigPropertyName()); 172 cp.setConfigPropertyValue(acp.getActivationConfigPropertyValue()); 173 cfVec.add(cp); 174 } 175 } 176 177 if (cfVec == null) { 178 if (jRaCfg2 != null) { 179 for (Iterator i = jRaCfg2.iterator(); i.hasNext();) { 180 if (cfVec == null) { 181 cfVec = new Vector (); 182 } 183 acp = (ActivationConfigPropertyDesc) i.next(); 184 cp = new ConfigPropertyDesc(); 185 cp.setConfigPropertyName(acp.getActivationConfigPropertyName()); 186 cp.setConfigPropertyValue(acp.getActivationConfigPropertyValue()); 187 cfVec.add(cp); 188 } 189 configs = new ConfigPropertyDesc[cfVec.size()]; 190 cfVec.copyInto(configs); 191 } 192 } else { 193 if (jRaCfg2 != null) { 194 boolean found = false; 195 for (Iterator i = jRaCfg2.iterator(); i.hasNext();) { 196 found = false; 197 acp = (ActivationConfigPropertyDesc) i.next(); 198 String name = acp.getActivationConfigPropertyName(); 199 String val = acp.getActivationConfigPropertyValue(); 200 if (val != null && val.length() > 0) { 201 for (int j = 0; j < cfVec.size(); j++) { 202 cp = (ConfigPropertyDesc) cfVec.get(j); 203 if (name.equalsIgnoreCase(cp.getConfigPropertyName())) { 204 cp.setConfigPropertyValue(val); 205 cfVec.set(j, cp); 206 found = true; 207 break; 208 } 209 } 210 if (!found) { 213 cp = new ConfigPropertyDesc(); 214 cp.setConfigPropertyName(name); 215 cp.setConfigPropertyValue(val); 216 cfVec.add(cp); 217 } 218 } 219 } 220 } 221 configs = new ConfigPropertyDesc[cfVec.size()]; 222 cfVec.copyInto(configs); 223 } 224 225 return configs; 226 } 227 228 if (cfVec == null) { 229 return null; 230 } 231 232 configs = new ConfigPropertyDesc[cfVec.size()]; 233 cfVec.copyInto(configs); 234 235 JonasConfigPropertyDesc jcpNext = null; 236 if (jRaCfg2 != null) { 237 for (Iterator i = jRaCfg2.iterator(); i.hasNext();) { 238 jcpNext = (JonasConfigPropertyDesc) i.next(); 239 String name = jcpNext.getJonasConfigPropertyName(); 240 String val = jcpNext.getJonasConfigPropertyValue(); 241 if (val != null && val.length() > 0) { 242 for (int j = 0; j < configs.length; j++) { 243 if (name.equalsIgnoreCase(configs[j].getConfigPropertyName())) { 244 configs[j].setConfigPropertyValue(val); 245 break; 246 } 247 } 248 } 249 } 250 } 251 252 if (jRaCfg1 != null) { 253 for (Iterator i = jRaCfg1.iterator(); i.hasNext();) { 254 jcpNext = (JonasConfigPropertyDesc) i.next(); 255 String name = jcpNext.getJonasConfigPropertyName(); 256 String val = jcpNext.getJonasConfigPropertyValue(); 257 if (val != null && val.length() > 0) { 258 for (int j = 0; j < configs.length; j++) { 259 if (name.equalsIgnoreCase(configs[j].getConfigPropertyName())) { 260 configs[j].setConfigPropertyValue(val); 261 break; 262 } 263 } 264 } 265 } 266 } 267 return configs; 268 } 269 270 275 private boolean checkLogEnabled(String inp) { 276 if (inp.equals("1") || inp.equalsIgnoreCase("on") 277 || inp.equalsIgnoreCase("t") || inp.equalsIgnoreCase("true") 278 || inp.equalsIgnoreCase("y") || inp.equalsIgnoreCase("yes")) { 279 return true; 280 } 281 return false; 282 } 283 284 293 public ConnectionManager createConnectionManager(String trans, 294 TransactionManager tm, 295 Logger logger, 296 Logger poolLogger) 297 throws Exception { 298 299 ConnectionManagerImpl cm = new ConnectionManagerImpl(trans); 300 301 Context c = new CompNamingContext(""); 302 c.rebind(ConnectionManagerImpl.TRANSACTION_MANAGER, tm); 303 c.rebind(ConnectionManagerImpl.RESOURCE_MANAGER_EVENT_LISTENER, tm); 304 305 c.rebind(ConnectionManagerImpl.LOGGER, logger); 307 c.rebind(ConnectionManagerImpl.POOL_LOGGER, poolLogger); 308 309 cm.init(c); 311 return cm; 312 } 313 314 332 public Object processMCF(ConnectorDesc conn, JonasConnectorDesc jonasConn, ConnectionManager cm, 333 ClassLoader curLoader, String rarName, String mcfc, 334 String jndiName, String logEnabled, String logTopic, 335 ConfigPropertyDesc [] cfgRaJonas, ResourceAdapter resAdp, 336 int idOff) throws Exception { 337 338 if (mcfc == null) { 339 logger.log(BasicLevel.ERROR, "ResourceService.createRA:" 340 + " managedconnectionfactoryclass property not found"); 341 throw new Exception ("configuration file incorrect"); 342 } 343 344 if (jndiName == null || jndiName.length() == 0) { 345 logger.log(BasicLevel.ERROR, "ResourceService.createRA: jndi-name not set in jonas-ra.xml"); 346 throw new Exception ("configuration file incorrect"); 347 } 348 349 Class mcfClass = curLoader.loadClass(mcfc); 351 ManagedConnectionFactory mcf = 352 (ManagedConnectionFactory ) mcfClass.newInstance(); 353 354 if (resAdp != null) { 355 try { 356 ((ResourceAdapterAssociation ) mcf).setResourceAdapter(resAdp); 357 } catch (ClassCastException ce) { 358 } catch (Exception ex) { 360 logger.log(BasicLevel.ERROR, "ResourceService: Error setting ResourceAdapter class to ManagedConnectionFactory (" 361 + mcfc + ") for " + jndiName); 362 throw ex; 363 } 364 } 365 366 if (logger.isLoggable(BasicLevel.DEBUG)) { 367 logger.log(BasicLevel.DEBUG, "jndiName=" + jndiName); 368 } 369 370 if (logEnabled != null) { 372 if (logger.isLoggable(BasicLevel.DEBUG)) { 373 logger.log(BasicLevel.DEBUG, "log-enabled=" + logEnabled); 374 } 375 if (checkLogEnabled(logEnabled)) { 376 if (logTopic != null && logTopic.length() > 0) { 377 if (logger.isLoggable(BasicLevel.DEBUG)) { 378 logger.log(BasicLevel.DEBUG, "log-topic=" + logTopic); 379 } 380 mcf.setLogWriter(Log.getLogWriter(logTopic)); 381 } else { 382 if (logger.isLoggable(BasicLevel.DEBUG)) { 383 logger.log(BasicLevel.DEBUG, "default log-topic=" + Log.JONAS_JCA_PREFIX); 384 } 385 mcf.setLogWriter(Log.getLogWriter(Log.JONAS_JCA_PREFIX)); } 387 } 388 } 389 390 processSetters(mcfClass, mcf, rarName, cfgRaJonas); 391 392 PrintWriter pw = mcf.getLogWriter(); 393 if (pw != null) { 394 SimpleDateFormat sdf = new SimpleDateFormat ("yyyy.MM.dd HH:mm:ss z"); 396 String date = sdf.format(new Date ()); 397 pw.println("MCF: output starting at " + date); 398 pw.flush(); 399 } 400 401 mcf.hashCode(); 403 404 return mcf; 405 } 406 407 408 425 public void registerMBean(Referenceable cf, String jndiName, String rarName, 426 ConnectorDesc conn, JonasConnectorDesc jonasConn, 427 String factType, int factOffset, 428 JCAResource jcaResourceMBean, String jcaResourceName, 429 String jDomain, String jServer, Context ictx, Properties prop, 430 String description, ConnectionManagerImpl cm) throws Exception { 431 432 try { 433 Reference ref = 434 new Reference (cf.getClass().getName(), 435 ResourceObjectJNDIHandler.class.getName(), 436 null); 437 438 ref.add(new StringRefAddr (ResourceServiceImpl.JNDI_NAME, jndiName)); 439 ref.add(new StringRefAddr (ResourceServiceImpl.RAR_OBJNAME, rarName)); 440 ref.add(new StringRefAddr (ResourceServiceImpl.FACTORY_TYPE, factType)); 441 ref.add(new StringRefAddr (ResourceServiceImpl.FACTORY_OFFSET, "" + factOffset)); 442 byte[] bytes = JNDIUtils.getBytesFromObject(conn); 444 if (bytes != null) { 445 ref.add(new BinaryRefAddr (ResourceServiceImpl.RA_XML, bytes)); 446 } 447 448 bytes = JNDIUtils.getBytesFromObject(jonasConn); 449 if (bytes != null) { 450 ref.add(new BinaryRefAddr (ResourceServiceImpl.JONAS_RA_XML, bytes)); 451 } 452 453 cf.setReference(ref); 454 ictx.rebind(jndiName, cf); 455 456 } catch (Exception e) { 457 logger.log(BasicLevel.ERROR, "ResourceService: Cannot register ResourceAdapter in naming with the name " + jndiName); 458 logger.log(BasicLevel.ERROR, "ResourceService: Exception caught : " + e); 459 } 460 461 if (mbeanServer != null) { 465 466 String jcaConnectionFactoryName = jndiName; 471 ObjectName onJCAConnectionFactory = 472 J2eeObjectName.getJCAConnectionFactory(jDomain, 473 jcaResourceName, 474 jServer, 475 jcaConnectionFactoryName); 476 JCAConnectionFactory jcaConnectionFactoryMBean = new JCAConnectionFactory(onJCAConnectionFactory.toString(), jndiName, prop, description, cm); 477 ManagedBean oManaged = oRegistry.findManagedBean("JCAConnectionFactory"); 478 ModelMBean oMBean = oManaged.createMBean(jcaConnectionFactoryMBean); 479 if (manageLogger.isLoggable(BasicLevel.DEBUG)) { 480 manageLogger.log(BasicLevel.DEBUG, "JCAConnectionFactory created"); 481 } 482 mbeanServer.registerMBean(oMBean, onJCAConnectionFactory); 483 484 jcaResourceMBean.setConnectionFactory(onJCAConnectionFactory.toString()); 487 488 String jcaManagedConnectionFactoryName = getJcaMcfName(jcaConnectionFactoryName); 491 ObjectName onJCAManagedConnectionFactory = 492 J2eeObjectName.getJCAManagedConnectionFactory(jDomain, 493 jServer, 494 jcaManagedConnectionFactoryName); 495 JCAManagedConnectionFactory jcaManagedConnectionFactoryMBean = 496 new JCAManagedConnectionFactory(onJCAManagedConnectionFactory.toString()); 497 oManaged = oRegistry.findManagedBean("JCAManagedConnectionFactory"); 498 oMBean = oManaged.createMBean(jcaManagedConnectionFactoryMBean); 499 if (manageLogger.isLoggable(BasicLevel.DEBUG)) { 500 manageLogger.log(BasicLevel.DEBUG, "JCAManagedConnectionFactory created"); 501 } 502 mbeanServer.registerMBean(oMBean, onJCAManagedConnectionFactory); 503 504 jcaConnectionFactoryMBean.setManagedConnectionFactory(onJCAManagedConnectionFactory.toString()); 506 if (manageLogger.isLoggable(BasicLevel.DEBUG)) { 507 manageLogger.log(BasicLevel.DEBUG, "JCAConnectionFactory updated"); 508 } 509 } } 511 512 513 521 public void processSetters(Class clsClass, Object clsObj, String rarFileName, 522 ConfigPropertyDesc [] cProp) throws Exception { 523 524 int curParam = 0; 525 while (cProp != null && curParam < cProp.length) { 526 String fieldName = cProp[curParam].getConfigPropertyName(); 527 String methodName = "set" + fieldName.substring(0, 1).toUpperCase() 528 + fieldName.substring(1); 529 String fieldType = cProp[curParam].getConfigPropertyType(); 530 Method [] m = clsClass.getMethods(); 531 532 int i = 0; 533 while (i < m.length 534 && (!m[i].getName().equals(methodName) 535 || m[i].getParameterTypes().length != 1 536 || (fieldType != null 537 && !(m[i].getParameterTypes())[0].getName().equals(fieldType))) 538 ) { 539 i++; 540 } 541 if (i < m.length) { 542 Class [] paramtype = m[i].getParameterTypes(); 543 Object [] param = new Object [1]; 544 545 String curValue = cProp[curParam].getConfigPropertyValue(); 546 if (setterLogger.isLoggable(BasicLevel.DEBUG)) { 547 setterLogger.log(BasicLevel.DEBUG, "Processing Field Name: " + fieldName 548 + " Type: " + fieldType + " Value: " + curValue); 549 } 550 551 if (paramtype[0].equals(java.lang.Integer.TYPE) 552 || paramtype[0].equals(java.lang.Integer .class)) { 553 param[0] = new Integer (curValue); 554 555 } else if (paramtype[0].equals(java.lang.Boolean.TYPE) 556 || paramtype[0].equals(java.lang.Boolean .class)) { 557 param[0] = new Boolean (curValue); 558 559 } else if (paramtype[0].equals(java.lang.Double.TYPE) 560 || paramtype[0].equals(java.lang.Double .class)) { 561 param[0] = new Double (curValue); 562 563 } else if (paramtype[0].equals(java.lang.Byte.TYPE) 564 || paramtype[0].equals(java.lang.Byte .class)) { 565 param[0] = new Byte (curValue); 566 567 } else if (paramtype[0].equals(java.lang.Short.TYPE) 568 || paramtype[0].equals(java.lang.Short .class)) { 569 param[0] = new Short (curValue); 570 571 } else if (paramtype[0].equals(java.lang.Long.TYPE) 572 || paramtype[0].equals(java.lang.Long .class)) { 573 param[0] = new Long (curValue); 574 575 } else if (paramtype[0].equals(java.lang.Float.TYPE) 576 || paramtype[0].equals(java.lang.Float .class)) { 577 param[0] = new Float (curValue); 578 579 } else if (paramtype[0].equals(java.lang.Character.TYPE) 580 || paramtype[0].equals(java.lang.Character .class)) { 581 param[0] = new Character (curValue.charAt(0)); 582 583 } else if (paramtype[0].equals(java.lang.String .class)) { 584 param[0] = curValue; 585 if (setterLogger.isLoggable(BasicLevel.DEBUG)) { 586 setterLogger.log(BasicLevel.DEBUG, "Calling String method with " + curValue); 587 } 588 589 } else { 590 logger.log(BasicLevel.ERROR, "Type unsupported for setter method:" 591 + methodName); 592 throw new Exception ("incorrect type for setter method "); 593 } 594 try { 597 m[i].invoke(clsObj, param); 598 } catch (Exception e) { 599 if (logger.isLoggable(BasicLevel.DEBUG)) { 600 logger.log(BasicLevel.DEBUG, "method " 601 + methodName 602 + " not found for " + rarFileName + "check its configuration "); 603 } 604 } 605 curParam++; 606 } else { 607 logger.log(BasicLevel.ERROR, "Method not found in class '" + clsClass + "': " + methodName); 608 throw new Exception ("method name '" + methodName + "' not found in class '" + clsClass + "'"); 609 } 610 } 611 } 612 613 621 public ConnectionManagerPoolParams configurePoolParams (PoolParamsDesc pParams, 622 JdbcConnParamsDesc jConnParams, 623 ConnectionManagerPoolParams pool) 624 throws Exception { 625 626 ConnectionManagerPoolParams cmpp = null; 627 if (pool == null) { 628 cmpp = new ConnectionManagerPoolParams(); 629 } else { 630 cmpp = new ConnectionManagerPoolParams(pool); 631 } 632 String tmpPool = null; 633 try { 634 if (pParams != null) { 635 if (pParams.getPoolInit() != null) { 636 tmpPool = pParams.getPoolInit(); 637 if (tmpPool != null && tmpPool.length() > 0) { 638 cmpp.setPoolInit(Integer.parseInt(tmpPool)); 639 } 640 } 641 if (pParams.getPoolMin() != null) { 642 tmpPool = pParams.getPoolMin(); 643 if (tmpPool != null && tmpPool.length() > 0) { 644 cmpp.setPoolMin(Integer.parseInt(tmpPool)); 645 } 646 } 647 if (pParams.getPoolMax() != null) { 648 tmpPool = pParams.getPoolMax(); 649 if (tmpPool != null && tmpPool.length() > 0) { 650 cmpp.setPoolMax(Integer.parseInt(tmpPool)); 651 } 652 } 653 if (pParams.getPoolMaxAge() != null) { 654 tmpPool = pParams.getPoolMaxAge(); 655 if (tmpPool != null && tmpPool.length() > 0) { 656 cmpp.setPoolMaxAge(Long.parseLong(tmpPool)); 657 } 658 } 659 if (pParams.getPoolMaxAgeMinutes() != null) { 660 tmpPool = pParams.getPoolMaxAgeMinutes(); 661 if (tmpPool != null && tmpPool.length() > 0) { 662 cmpp.setPoolMaxAgeMinutes(Integer.parseInt(tmpPool)); 663 } 664 } 665 if (pParams.getPoolMaxOpentime() != null) { 666 tmpPool = pParams.getPoolMaxOpentime(); 667 if (tmpPool != null && tmpPool.length() > 0) { 668 cmpp.setPoolMaxOpentime(Integer.parseInt(tmpPool)); 669 } 670 } 671 if (pParams.getPoolMaxWaiters() != null) { 672 tmpPool = pParams.getPoolMaxWaiters(); 673 if (tmpPool != null && tmpPool.length() > 0) { 674 cmpp.setPoolMaxWaiters(Integer.parseInt(tmpPool)); 675 } 676 } 677 if (pParams.getPoolMaxWaittime() != null) { 678 tmpPool = pParams.getPoolMaxWaittime(); 679 if (tmpPool != null && tmpPool.length() > 0) { 680 cmpp.setPoolMaxWaittime(Integer.parseInt(tmpPool)); 681 } 682 } 683 if (pParams.getPoolSamplingPeriod() != null) { 684 tmpPool = pParams.getPoolSamplingPeriod(); 685 if (tmpPool != null && tmpPool.length() > 0) { 686 cmpp.setPoolSamplingPeriod(Integer.parseInt(tmpPool)); 687 } 688 } 689 if (pParams.getPstmtMax() != null) { 690 tmpPool = pParams.getPstmtMax(); 691 if (tmpPool != null && tmpPool.length() > 0) { 692 cmpp.setPstmtMax(Integer.parseInt(tmpPool)); 693 } 694 } 695 } 696 if (jConnParams != null) { 697 if (jConnParams.getJdbcCheckLevel() != null) { 698 tmpPool = jConnParams.getJdbcCheckLevel(); 699 if (tmpPool != null && tmpPool.length() > 0) { 700 cmpp.setJdbcConnLevel(Integer.parseInt(tmpPool)); 701 } 702 } 703 if (jConnParams.getJdbcTestStatement() != null) { 704 cmpp.setJdbcConnTestStmt(jConnParams.getJdbcTestStatement()); 705 } 706 } 707 } catch (Exception ex) { 708 ex.printStackTrace(); 709 logger.log(BasicLevel.ERROR, "Invalid Pool parameter from jonas-ra.xml: Ensure that numeric values are used. " + tmpPool); 710 throw new Exception ("incorrect pool parameter "); 711 } 712 return cmpp; 713 } 714 715 724 public Object getJonasXML(JonasConnectorDesc jonasConn, 725 String id, int idOffset, String oType) { 726 Object obj = null; 727 if (oType == JCD) { 728 obj = jonasConn.getJonasConnectionDefinitionList().get(idOffset); 729 } else if (oType == JAS) { 730 obj = jonasConn.getJonasActivationspecList().get(idOffset); 731 } else if (oType == JAO) { 732 obj = jonasConn.getJonasAdminobjectList().get(idOffset); 733 } 734 return obj; 735 } 736 737 742 public String getJcaMcfName(String jndiName) { 743 return jndiName; 744 } 745 } 746 | Popular Tags |