| 1 25 26 27 package org.objectweb.jonas.resource; 28 29 import java.io.File ; 30 import java.net.URL ; 31 import java.util.Enumeration ; 32 import java.util.Hashtable ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.Properties ; 36 import java.util.Vector ; 37 import java.util.jar.JarEntry ; 38 39 import javax.management.InstanceNotFoundException ; 40 import javax.management.MBeanRegistrationException ; 41 import javax.management.MBeanServer ; 42 import javax.management.MalformedObjectNameException ; 43 import javax.management.ObjectName ; 44 import javax.management.modelmbean.ModelMBean ; 45 import javax.naming.Context ; 46 import javax.naming.InitialContext ; 47 import javax.naming.NamingException ; 48 import javax.resource.Referenceable ; 49 import javax.resource.spi.ActivationSpec ; 50 import javax.resource.spi.ConnectionManager ; 51 import javax.resource.spi.ManagedConnectionFactory ; 52 import javax.resource.spi.ResourceAdapter ; 53 import javax.resource.spi.ResourceAdapterAssociation ; 54 import javax.resource.spi.ResourceAllocationException ; 55 import javax.resource.spi.work.WorkManager ; 56 57 import org.apache.commons.modeler.ManagedBean; 58 import org.apache.commons.modeler.Registry; 59 60 import org.objectweb.transaction.jta.TransactionManager; 61 62 import org.objectweb.jonas_ejb.deployment.api.ActivationConfigPropertyDesc; 63 64 import org.objectweb.jonas_rar.deployment.api.AdminobjectDesc; 65 import org.objectweb.jonas_rar.deployment.api.AuthenticationMechanismDesc; 66 import org.objectweb.jonas_rar.deployment.api.ConfigPropertyDesc; 67 import org.objectweb.jonas_rar.deployment.api.ConnectionDefinitionDesc; 68 import org.objectweb.jonas_rar.deployment.api.ConnectorDesc; 69 import org.objectweb.jonas_rar.deployment.api.InboundResourceadapterDesc; 70 import org.objectweb.jonas_rar.deployment.api.JonasActivationspecDesc; 71 import org.objectweb.jonas_rar.deployment.api.JonasAdminobjectDesc; 72 import org.objectweb.jonas_rar.deployment.api.JonasConnectionDefinitionDesc; 73 import org.objectweb.jonas_rar.deployment.api.JonasConnectorDesc; 74 import org.objectweb.jonas_rar.deployment.api.MessageadapterDesc; 75 import org.objectweb.jonas_rar.deployment.api.MessagelistenerDesc; 76 import org.objectweb.jonas_rar.deployment.api.OutboundResourceadapterDesc; 77 import org.objectweb.jonas_rar.deployment.api.RarDeploymentDesc; 78 import org.objectweb.jonas_rar.deployment.api.RequiredConfigPropertyDesc; 79 import org.objectweb.jonas_rar.deployment.api.ResourceadapterDesc; 80 import org.objectweb.jonas_rar.deployment.api.TmConfigPropertyDesc; 81 import org.objectweb.jonas_rar.deployment.lib.wrapper.RarManagerWrapper; 82 83 import org.objectweb.jonas.common.JJarFile; 84 import org.objectweb.jonas.common.Log; 85 import org.objectweb.jonas.ear.EarServiceImpl; 86 import org.objectweb.jonas.jmx.J2eeObjectName; 87 import org.objectweb.jonas.jmx.JmxService; 88 import org.objectweb.jonas.jmx.JonasObjectName; 89 import org.objectweb.jonas.jtm.TransactionService; 90 import org.objectweb.jonas.management.JonasMBeanTools; 91 import org.objectweb.jonas.service.ServiceException; 92 import org.objectweb.jonas.service.ServiceManager; 93 94 import org.objectweb.util.monolog.api.BasicLevel; 95 import org.objectweb.util.monolog.api.Logger; 96 97 101 102 public class Rar { 103 104 107 private static Logger logger = null; 108 111 private static Logger poolLogger = null; 112 115 private static Logger setterLogger = null; 116 119 private static Logger manageLogger = null; 120 121 String rarFileName = null; 122 ClassLoader curLoader = null; 123 boolean isInEar; 124 URL earUrl = null; 125 Context rCtx = null; 126 String jDomain = null; 127 String jServer = null; 128 Vector jndinames = new Vector (); 129 130 String lnkJndiName = ""; 131 String lnkRarFilename = ""; 132 133 JCAResource jcaResourceMBean = null; 134 String JCAResourceName = null; 135 Registry oRegistry = null; 136 137 private WorkManager workMgr = null; 139 private ResourceBootstrapContext bootCtx = null; 140 141 143 public static final String CLASS = "jonas.service.resource.class"; 145 public static final String JNDI_NAME = "jndiname"; 146 public static final String RAR_FILENAME = "rarfilename"; 147 public static final String LNK_JNDI_NAME = "lnkjndiname"; 148 public static final String LNK_RAR_FILENAME = "lnkrarfilename"; 149 public static final String OBJ_TYPE = "objtype"; 150 public static final String RESOURCE_LIST = "jonas.service.resource.resources"; 151 152 public static final int DEF_WRK_THREADS = 5; 153 public static final int DEF_EXEC_TIME = 0; 154 155 public static final String JCD = "JCD"; 156 public static final String JAS = "JAS"; 157 public static final String JAO = "JAO"; 158 159 public String objectName = null; 160 public String pathName = null; 161 public ResourceAdapter resAdp = null; 162 public ConnectorDesc raConn = null; 163 public JonasConnectorDesc lnkJonasConn = null; 164 public JonasConnectorDesc jonasConn = null; 165 166 public String xmlContent = null; 167 public String jonasXmlContent = null; 168 169 170 private class ConfigObj { 171 public String type; 172 public int offset; 173 public String jndiName; 174 public String rarFilename; 175 public String lnkJndiName; 176 public String lnkRarFilename; 177 public String interfaceStr; 178 public String classStr; 179 public Object factory; 180 public ConnectionManager cm; 181 public boolean basicPass; 182 public boolean defaultAS; 183 public List reqConfigProps; 184 public ConfigObj(String fType, int off, String jndi, String fName, 185 String intStr, String clsStr, Object fact) { 186 type = fType; 187 offset = off; 188 jndiName = jndi; 189 rarFilename = fName; 190 interfaceStr = intStr; 191 classStr = clsStr; 192 factory = fact; 193 cm = null; 194 reqConfigProps = null; 195 lnkJndiName = ""; 196 lnkRarFilename = ""; 197 defaultAS = false; 198 } 199 } 200 201 204 private static Vector defaultAS = new Vector (); 205 206 209 public static Hashtable fileName2RA = new Hashtable (); 210 213 public static Hashtable jndiName2RA = new Hashtable (); 214 215 private Hashtable cfgObjs = new Hashtable (); 216 219 private static Hashtable jndiName2Factory = new Hashtable (); 220 221 224 private TransactionManager tm = null; 225 226 229 private MBeanServer mbeanServer = null; 230 231 234 private String jcaResourceObjectName = null; 235 236 241 private ConnectionManagerImpl cm = null; 242 243 246 private Context ictx = null; 247 248 251 ResourceUtility ru = null; 252 253 261 public Rar() { 262 if (logger == null) { 263 logger = Log.getLogger(Log.JONAS_JCA_PREFIX+".process"); 264 } 265 if (poolLogger == null) { 266 poolLogger = Log.getLogger(Log.JONAS_JCA_PREFIX+".pool"); 267 } 268 if (setterLogger == null) { 269 setterLogger = Log.getLogger(Log.JONAS_JCA_PREFIX+".setters"); 270 } 271 if (manageLogger == null) { 272 manageLogger = Log.getLogger(Log.JONAS_JCA_PREFIX+".management"); 273 } 274 275 curLoader = Thread.currentThread().getContextClassLoader(); 276 277 ru = new ResourceUtility(null, null, logger, setterLogger, manageLogger); 278 } 279 280 288 public Rar(Context ctx, String jDom, String jServ, 289 WorkManager wrkMgr, ResourceBootstrapContext btCtx) { 290 if (logger == null) { 291 logger = Log.getLogger(Log.JONAS_JCA_PREFIX + ".process"); 292 } 293 if (poolLogger == null) { 294 poolLogger = Log.getLogger(Log.JONAS_JCA_PREFIX + ".pool"); 295 } 296 if (setterLogger == null) { 297 setterLogger = Log.getLogger(Log.JONAS_JCA_PREFIX + ".setters"); 298 } 299 if (manageLogger == null) { 300 manageLogger = Log.getLogger(Log.JONAS_JCA_PREFIX + ".management"); 301 } 302 303 try { 304 rarFileName = (String ) ctx.lookup("rarFileName"); 305 isInEar = ((Boolean ) ctx.lookup("isInEar")).booleanValue(); 306 if (isInEar) { 307 earUrl = (URL ) ctx.lookup("earUrl"); 308 } 309 curLoader = (ClassLoader ) ctx.lookup("classloader"); 310 } catch (NamingException e) { 311 String err = "Error while getting parameter from context param."; 312 logger.log(BasicLevel.ERROR, err + e.getMessage()); 313 throw new ResourceServiceException(err, e); 314 } catch (Exception ex) { 315 String err = "Error while getting parameter from context param."; 316 logger.log(BasicLevel.ERROR, err + ex.getMessage()); 317 throw new ResourceServiceException(err, ex); 318 } 319 320 workMgr = wrkMgr; 321 bootCtx = btCtx; 322 rCtx = ctx; 323 jDomain = jDom; 324 jServer = jServ; 325 326 try { 328 ictx = new InitialContext (); 329 } catch (NamingException e) { 330 logger.log(BasicLevel.ERROR, "Cannot create initial context when Resource service initializing"); 331 throw new ServiceException("Cannot create initial context when Resource service initializing", e); 332 } 333 334 try { 336 ServiceManager sm = ServiceManager.getInstance(); 337 TransactionService ts = (TransactionService) sm.getTransactionService(); 338 tm = ts.getTransactionManager(); 339 } catch (Exception e) { 340 logger.log(BasicLevel.ERROR, "Cannot get the Transaction service: " + e); 341 throw new ServiceException("Cannot get the Transaction service: ", e); 342 } 343 344 try { 346 mbeanServer = 347 ((JmxService) ServiceManager.getInstance().getJmxService()).getJmxServer(); 348 } catch (Exception e) { 349 mbeanServer = null; 351 } 352 353 oRegistry = JonasMBeanTools.getRegistry(); 355 356 ru = new ResourceUtility(mbeanServer, oRegistry, logger, setterLogger, manageLogger); 357 } 358 359 363 public Context processRar() throws Exception { 364 365 String onRar = null; 366 RarDeploymentDesc radesc = null; 368 ConnectorDesc conn = null; 369 JonasConnectorDesc jConn = null; 370 xmlContent = null; 371 jonasXmlContent = null; 372 ConnectionManager cm = null; 373 ManagedConnectionFactory mcf = null; 374 375 radesc = RarManagerWrapper.getInstance(rCtx); 376 conn = radesc.getConnectorDesc(); 377 jConn = radesc.getJonasConnectorDesc(); 378 xmlContent = radesc.getXmlContent(); 379 jonasXmlContent = radesc.getJOnASXmlContent(); 380 381 if(conn == null && jConn == null) { 382 logger.log(BasicLevel.ERROR, "Rar.processRar: Resource (" + rarFileName 383 + ") must be a valid RAR file."); 384 throw new Exception ("resource input file incorrect"); 385 } 386 387 String rarVal = null; 392 if (jConn != null) { 393 rarVal = (jConn.getRarlink() == null 394 ? null 395 : jConn.getRarlink()); 396 if (rarVal != null && rarVal.length() > 0) { 397 lnkJndiName = rarVal; 398 conn = getConnectorDesc(rarVal); 399 if (conn == null) { 400 return rCtx; 401 } 402 ConfigObj co = (ConfigObj) getConfigObject(rarVal); 403 lnkRarFilename = co.rarFilename; 404 xmlContent = getXmlContent(rarVal); 405 lnkJonasConn = getJonasConnectorDesc(rarVal); 407 } else if (conn == null) { 408 logger.log(BasicLevel.ERROR, "Rar.processRar: Resource (" + rarFileName 409 + ") is not valid."); 410 throw new Exception ("resource input file incorrect: no ra.xml file"); 411 } 412 bldSecurityTable(lnkJonasConn, jConn); 413 raConn = conn; 414 jonasConn = jConn; 415 } else { 416 logger.log(BasicLevel.ERROR, "Rar.processRar: Resource (" + rarFileName 417 + ") must be a valid RAR file, there must be jonas-ra.xml file."); 418 throw new Exception ("resource input file incorrect: no jonas-ra.xml file"); 419 } 420 421 ConnectionManagerPoolParams pParams = 423 ru.configurePoolParams(jConn.getPoolParamsDesc(), 424 jConn.getJdbcConnParamsDesc(), 425 null); 426 427 430 extractJars(rarFileName, jConn); 431 432 ResourceadapterDesc ra = conn.getResourceadapterDesc(); 434 435 boolean basicPass = true; 438 List authList = ra.getAuthenticationMechanismList(); 439 if (authList != null && authList.size() > 0) { 440 basicPass = false; 441 for (Iterator i = authList.iterator(); i.hasNext();) { 442 AuthenticationMechanismDesc am = (AuthenticationMechanismDesc) i.next(); 443 if (am.getAuthenticationMechanismType().equalsIgnoreCase("BasicPassword")) { 444 basicPass = true; 445 break; 446 } 447 } 448 if (!basicPass) { 449 logger.log(BasicLevel.ERROR, "Rar.processRar: Resource (" + rarFileName + ") doesn't contain an AuthenticationMechanismType that is supported by JOnAS(BasicPassword)."); 450 throw new Exception ("No AuthenticationMechanismType that is supported by JOnAS(BasicPassword)."); 451 } 452 } 453 454 ConfigPropertyDesc [] cfgRaJonas = null; 455 456 String logEnabled = null; 457 if (jConn.getLogEnabled() != null) { 458 logEnabled = jConn.getLogEnabled().trim(); 459 } 460 String logTopic = null; 461 if (jConn.getLogTopic() != null) { 462 logTopic = jConn.getLogTopic().trim(); 463 } 464 465 Referenceable cf = null; 466 String jndiName = null; 467 String mcfc = null; 468 ConfigObj cObj = null; 469 470 if (lnkJonasConn != null) { 471 cfgRaJonas = ru.buildConfigProperty(ra.getConfigPropertyList(), 472 jConn.getJonasConfigPropertyList(), 473 lnkJonasConn.getJonasConfigPropertyList()); 474 } else { 475 cfgRaJonas = ru.buildConfigProperty(ra.getConfigPropertyList(), 476 jConn.getJonasConfigPropertyList(), 477 null); 478 } 479 480 Properties tmProp = new Properties (); 481 if (jConn.getTmParamsDesc() != null) { 482 List tmParams = jConn.getTmParamsDesc().getTmConfigPropertyList(); 483 if (tmParams != null) { 484 for (Iterator i = tmParams.iterator(); i.hasNext();) { 485 TmConfigPropertyDesc tpd = (TmConfigPropertyDesc) i.next(); 486 String tpdVal = tpd.getTmConfigPropertyValue(); 487 if (tpdVal == null) { 488 tpdVal = ""; 489 } 490 tmProp.setProperty(tpd.getTmConfigPropertyName(), tpdVal); 491 } 492 if (logger.isLoggable(BasicLevel.DEBUG)) { 493 logger.log(BasicLevel.DEBUG, "TM Properties: " + tmProp); 494 } 495 } 496 } 497 498 String specVersion = conn.getSpecVersion(); 499 if (specVersion.equals("1.0")) { 500 jndiName = jConn.getJndiName().trim(); 501 ConnectionManagerPoolParams cmpp = ru.configurePoolParams(jConn.getPoolParamsDesc(), 502 jConn.getJdbcConnParamsDesc(), null); 503 onRar = registerRarMBean(cfgRaJonas, specVersion, jndiName); 504 cm = ru.createConnectionManager(ra.getTransactionSupport(), tm, 505 logger, poolLogger); 506 mcfc = ra.getManagedconnectionfactoryClass(); 508 mcf = (ManagedConnectionFactory ) ru.processMCF(conn, jConn, cm, curLoader, rarFileName, 509 mcfc, 510 jndiName, logEnabled, logTopic, 511 cfgRaJonas, resAdp, 0); 512 ((ConnectionManagerImpl) cm).setResourceAdapter(mcf, cmpp); 513 514 cf = (Referenceable ) mcf.createConnectionFactory(cm); 515 516 cObj = new ConfigObj(JCD, 0, jndiName, rarFileName, null, mcfc, cf); 518 cObj.cm = cm; 519 cObj.basicPass = basicPass; 520 521 cfgObjs.put(jndiName, cObj); 522 523 jndinames.add(jndiName); 524 525 ru.registerMBean(cf, jndiName, onRar, conn, jonasConn, JCD, 0, 526 jcaResourceMBean, JCAResourceName, jDomain, jServer, ictx, 527 buildProp(cfgRaJonas), "", (ConnectionManagerImpl)cm); 528 ((ConnectionManagerImpl) cm).setXAName(ru.getJcaMcfName(jndiName)); 529 ((ConnectionManagerImpl) cm).registerXAResource(tmProp); 530 531 } else if (conn.getSpecVersion().equals("1.5")) { 532 String raStr = conn.getResourceadapterDesc().getResourceadapterClass().trim(); 533 boolean isResAdapt = false; 534 logger.log(BasicLevel.INFO, "Starting deployment of " + rarFileName); 535 if (raStr != null && raStr.length() > 0) { 536 isResAdapt = true; 537 538 Class raClass = curLoader.loadClass(raStr); 540 resAdp = (ResourceAdapter) raClass.newInstance(); 541 542 ru.processSetters(raClass, resAdp, rarFileName, cfgRaJonas); 543 544 try { 546 resAdp.start(bootCtx); 547 } catch (Exception ex) { 548 logger.log(BasicLevel.ERROR, "Rar: Error from resource (" 549 + rarFileName + ") start method."); 550 throw new Exception ("Error from start method. " + ex); 551 } catch (Throwable th) { 552 logger.log(BasicLevel.ERROR, "Rar: Error from resource (" 553 + rarFileName + ") start method."); 554 throw new Exception ("Error from start method. ", th); 555 } 556 } 557 558 onRar = registerRarMBean(cfgRaJonas, specVersion, null); 559 560 OutboundResourceadapterDesc outRa = ra.getOutboundResourceadapterDesc(); 562 List cdList = null; 563 if (outRa != null) { 564 cdList = outRa.getConnectionDefinitionList(); 565 } 566 ConnectionDefinitionDesc conDef = null; 567 JonasConnectionDefinitionDesc jConDef = null; 568 String id = null; 569 int idOffset = -1; 570 if (cdList != null) { 571 for (int cd = 0; cd < cdList.size(); cd++) { 572 conDef = (ConnectionDefinitionDesc) cdList.get(cd); 573 id = conDef.getId(); 574 idOffset++; 575 jConDef = (JonasConnectionDefinitionDesc) 576 ru.getJonasXML(jConn, id, idOffset, JCD); 577 578 579 if (jConDef.getLogEnabled() != null) { 580 logEnabled = jConDef.getLogEnabled().trim(); 581 } 582 if (jConDef.getLogTopic() != null) { 583 logTopic = jConDef.getLogTopic().trim(); 584 } 585 ConfigPropertyDesc [] cfgCdDesc = 586 ru.buildConfigProperty(conDef.getConfigPropertyList(), 587 jConDef.getJonasConfigPropertyList(), 588 null); 589 ConnectionManagerPoolParams pool = 590 ru.configurePoolParams(jConDef.getPoolParamsDesc(), 591 jConDef.getJdbcConnParamsDesc(), 592 pParams); 593 cm = ru.createConnectionManager(outRa.getTransactionSupport(), tm, 594 logger, poolLogger); 595 596 jndiName = jConDef.getJndiName().trim(); 597 mcfc = conDef.getManagedconnectionfactoryClass(); 598 mcf = (ManagedConnectionFactory ) 599 ru.processMCF(conn, jConn, cm, curLoader, rarFileName, 600 mcfc, jndiName, logEnabled, logTopic, 601 cfgCdDesc, resAdp, idOffset); 602 ((ConnectionManagerImpl) cm).setResourceAdapter(mcf, pool); 603 604 cf = (Referenceable ) mcf.createConnectionFactory(cm); 605 606 cObj = new ConfigObj(JCD, idOffset, jndiName, rarFileName, null, mcfc, cf); 608 cObj.cm = cm; 609 cObj.basicPass = basicPass; 610 611 cfgObjs.put(jndiName, cObj); 612 613 jndinames.add(jndiName); 614 615 String desc = ""; 616 List descList = jConDef.getDescriptionList(); 617 if (descList != null) { 618 for (int i = 0; i < descList.size(); i++) { 619 String tmp = (String ) descList.get(i); 620 desc = desc + tmp; 621 } 622 } 623 ru.registerMBean(cf, jndiName, onRar, conn, jonasConn, JCD, idOffset, 624 jcaResourceMBean, JCAResourceName, jDomain, jServer, ictx, 625 buildProp(cfgCdDesc), desc, (ConnectionManagerImpl)cm); 626 627 ((ConnectionManagerImpl) cm).setXAName(ru.getJcaMcfName(jndiName)); 628 ((ConnectionManagerImpl) cm).registerXAResource(tmProp); 629 } 630 } 631 632 if (isResAdapt) { 635 InboundResourceadapterDesc inAdapt = ra.getInboundResourceadapterDesc(); 637 MessageadapterDesc msgAdapt = null; 638 List mlList = null; 639 if (inAdapt != null) { 640 msgAdapt = inAdapt.getMessageadapterDesc(); 641 mlList = null; 642 if (msgAdapt != null) { 643 mlList = msgAdapt.getMessagelistenerList(); 644 } 645 } 646 MessagelistenerDesc msgList = null; 647 JonasActivationspecDesc jAct = null; 648 id = null; 649 idOffset = -1; 650 if (mlList != null) { 651 for (int ml = 0; ml < mlList.size(); ml++) { 652 &nbs
|