0001: /**
0002: * JOnAS: Java(TM) Open Application Server
0003: * Copyright (C) 1999-2005 Bull S.A.
0004: * Contact: jonas-team@objectweb.org
0005: *
0006: * This library is free software; you can redistribute it and/or
0007: * modify it under the terms of the GNU Lesser General Public
0008: * License as published by the Free Software Foundation; either
0009: * version 2.1 of the License, or any later version.
0010: *
0011: * This library is distributed in the hope that it will be useful,
0012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0014: * Lesser General Public License for more details.
0015: *
0016: * You should have received a copy of the GNU Lesser General Public
0017: * License along with this library; if not, write to the Free Software
0018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
0019: * USA
0020: *
0021: * --------------------------------------------------------------------------
0022: * $Id: Rar.java 9932 2007-01-18 00:03:16Z ehardesty $
0023: * --------------------------------------------------------------------------
0024: */package org.objectweb.jonas.resource;
0025:
0026: import java.io.File;
0027: import java.net.URL;
0028: import java.util.Enumeration;
0029: import java.util.Hashtable;
0030: import java.util.Iterator;
0031: import java.util.List;
0032: import java.util.Properties;
0033: import java.util.Vector;
0034: import java.util.jar.JarEntry;
0035:
0036: import javax.management.InstanceNotFoundException;
0037: import javax.management.MBeanRegistrationException;
0038: import javax.management.MBeanServer;
0039: import javax.management.MalformedObjectNameException;
0040: import javax.management.ObjectName;
0041: import javax.management.modelmbean.ModelMBean;
0042: import javax.naming.Context;
0043: import javax.naming.InitialContext;
0044: import javax.naming.NamingException;
0045: import javax.resource.Referenceable;
0046: import javax.resource.spi.ActivationSpec;
0047: import javax.resource.spi.ConnectionManager;
0048: import javax.resource.spi.ManagedConnectionFactory;
0049: import javax.resource.spi.ResourceAdapter;
0050: import javax.resource.spi.ResourceAdapterAssociation;
0051: import javax.resource.spi.ResourceAllocationException;
0052: import javax.resource.spi.work.WorkManager;
0053:
0054: import org.apache.commons.modeler.ManagedBean;
0055: import org.apache.commons.modeler.Registry;
0056:
0057: import org.objectweb.transaction.jta.TransactionManager;
0058:
0059: import org.objectweb.jonas_ejb.deployment.api.ActivationConfigPropertyDesc;
0060:
0061: import org.objectweb.jonas_rar.deployment.api.AdminobjectDesc;
0062: import org.objectweb.jonas_rar.deployment.api.AuthenticationMechanismDesc;
0063: import org.objectweb.jonas_rar.deployment.api.ConfigPropertyDesc;
0064: import org.objectweb.jonas_rar.deployment.api.ConnectionDefinitionDesc;
0065: import org.objectweb.jonas_rar.deployment.api.ConnectorDesc;
0066: import org.objectweb.jonas_rar.deployment.api.InboundResourceadapterDesc;
0067: import org.objectweb.jonas_rar.deployment.api.JonasActivationspecDesc;
0068: import org.objectweb.jonas_rar.deployment.api.JonasAdminobjectDesc;
0069: import org.objectweb.jonas_rar.deployment.api.JonasConnectionDefinitionDesc;
0070: import org.objectweb.jonas_rar.deployment.api.JonasConnectorDesc;
0071: import org.objectweb.jonas_rar.deployment.api.MessageadapterDesc;
0072: import org.objectweb.jonas_rar.deployment.api.MessagelistenerDesc;
0073: import org.objectweb.jonas_rar.deployment.api.OutboundResourceadapterDesc;
0074: import org.objectweb.jonas_rar.deployment.api.RarDeploymentDesc;
0075: import org.objectweb.jonas_rar.deployment.api.RequiredConfigPropertyDesc;
0076: import org.objectweb.jonas_rar.deployment.api.ResourceadapterDesc;
0077: import org.objectweb.jonas_rar.deployment.api.TmConfigPropertyDesc;
0078: import org.objectweb.jonas_rar.deployment.lib.wrapper.RarManagerWrapper;
0079:
0080: import org.objectweb.jonas.common.JJarFile;
0081: import org.objectweb.jonas.common.Log;
0082: import org.objectweb.jonas.ear.EarServiceImpl;
0083: import org.objectweb.jonas.jmx.J2eeObjectName;
0084: import org.objectweb.jonas.jmx.JmxService;
0085: import org.objectweb.jonas.jmx.JonasObjectName;
0086: import org.objectweb.jonas.jtm.TransactionService;
0087: import org.objectweb.jonas.management.JonasMBeanTools;
0088: import org.objectweb.jonas.service.ServiceException;
0089: import org.objectweb.jonas.service.ServiceManager;
0090:
0091: import org.objectweb.util.monolog.api.BasicLevel;
0092: import org.objectweb.util.monolog.api.Logger;
0093:
0094: /**
0095: * Rar object
0096: * @author Eric Hardesty
0097: */
0098: public class Rar {
0099:
0100: /**
0101: * Main logger
0102: */
0103: private static Logger logger = Log.getLogger(Log.JONAS_JCA_PREFIX
0104: + ".process");
0105:
0106: /**
0107: * Pool infomation logger
0108: */
0109: private static Logger poolLogger = Log
0110: .getLogger(Log.JONAS_JCA_PREFIX + ".pool");
0111:
0112: /**
0113: * Config property setter logger
0114: */
0115: private static Logger setterLogger = Log
0116: .getLogger(Log.JONAS_JCA_PREFIX + ".setters");
0117:
0118: /**
0119: * Management logger
0120: */
0121: private static Logger manageLogger = Log
0122: .getLogger(Log.JONAS_JCA_PREFIX + ".management");
0123:
0124: String rarFileName = null;
0125: ClassLoader curLoader = null;
0126: boolean isInEar;
0127: URL earUrl = null;
0128: Context rCtx = null;
0129: String jDomain = null;
0130: String jServer = null;
0131: Vector jndinames = new Vector();
0132:
0133: String lnkJndiName = "";
0134: String lnkRarFilename = "";
0135:
0136: JCAResource jcaResourceMBean = null;
0137: String JCAResourceName = null;
0138: Registry oRegistry = null;
0139:
0140: // J2EE CA 1.5 objects
0141: private WorkManager workMgr = null;
0142: private ResourceBootstrapContext bootCtx = null;
0143:
0144: // Properties for inits
0145:
0146: // JCA resource service configuration parameters
0147: public static final String CLASS = "jonas.service.resource.class";
0148: public static final String JNDI_NAME = "jndiname";
0149: public static final String RAR_FILENAME = "rarfilename";
0150: public static final String LNK_JNDI_NAME = "lnkjndiname";
0151: public static final String LNK_RAR_FILENAME = "lnkrarfilename";
0152: public static final String OBJ_TYPE = "objtype";
0153: public static final String RESOURCE_LIST = "jonas.service.resource.resources";
0154:
0155: public static final int DEF_WRK_THREADS = 5;
0156: public static final int DEF_EXEC_TIME = 0;
0157:
0158: public static final String JCD = "JCD";
0159: public static final String JAS = "JAS";
0160: public static final String JAO = "JAO";
0161:
0162: public String objectName = null;
0163: public String pathName = null;
0164: public ResourceAdapter resAdp = null;
0165: public ConnectorDesc raConn = null;
0166: public JonasConnectorDesc lnkJonasConn = null;
0167: public JonasConnectorDesc jonasConn = null;
0168:
0169: public String xmlContent = null;
0170: public String jonasXmlContent = null;
0171:
0172: private class ConfigObj {
0173: public String type;
0174: public int offset;
0175: public String jndiName;
0176: public String rarFilename;
0177: public String lnkJndiName;
0178: public String lnkRarFilename;
0179: public String interfaceStr;
0180: public String classStr;
0181: public Object factory;
0182: public ConnectionManager cm;
0183: public boolean basicPass;
0184: public boolean defaultAS;
0185: public List reqConfigProps;
0186:
0187: public ConfigObj(String fType, int off, String jndi,
0188: String fName, String intStr, String clsStr, Object fact) {
0189: type = fType;
0190: offset = off;
0191: jndiName = jndi;
0192: rarFilename = fName;
0193: interfaceStr = intStr;
0194: classStr = clsStr;
0195: factory = fact;
0196: cm = null;
0197: reqConfigProps = null;
0198: lnkJndiName = "";
0199: lnkRarFilename = "";
0200: defaultAS = false;
0201: }
0202: }
0203:
0204: /**
0205: * Hold list of default ActivationSpecs
0206: */
0207: private static Vector defaultAS = new Vector();
0208:
0209: /**
0210: * Hashtable mapping a filename to an RAR object
0211: */
0212: public static Hashtable fileName2RA = new Hashtable();
0213: /**
0214: * These hashtables associate an RAR with other objects
0215: */
0216: public static Hashtable jndiName2RA = new Hashtable();
0217:
0218: private Hashtable cfgObjs = new Hashtable();
0219: /**
0220: * Hashtable mapping a jndiname to an "external" factory
0221: */
0222: private static Hashtable jndiName2Factory = new Hashtable();
0223:
0224: /**
0225: * The transaction manager in the server
0226: */
0227: private TransactionManager tm = null;
0228:
0229: /**
0230: * Reference to a MBean server.
0231: */
0232: private MBeanServer mbeanServer = null;
0233:
0234: /**
0235: * ObjectName for the jcaResource
0236: */
0237: private String jcaResourceObjectName = null;
0238:
0239: /**
0240: * The ConnectionManager manages connections of resource adapters
0241: * and more exactly the association between logical connections,
0242: * physical connections, local transactions and global transaction(XA)
0243: */
0244: private ConnectionManagerImpl cm = null;
0245:
0246: /**
0247: * JNDI Context
0248: */
0249: private Context ictx = null;
0250:
0251: /**
0252: * Resource Utility factory
0253: */
0254: ResourceUtility ru = null;
0255:
0256: /**
0257: * - Get the loggers
0258: * - Get the global jndi context
0259: * - Get the list of the resource adapters. The list is reachable in the
0260: * - context parameter under the name RESOURCE_LIST.
0261: * - Get the transaction manager into the jndi
0262: * - Set the XML validation property
0263: */
0264: public Rar() {
0265: curLoader = Thread.currentThread().getContextClassLoader();
0266: ru = new ResourceUtility(null, null, logger, setterLogger,
0267: manageLogger);
0268: }
0269:
0270: /**
0271: * - Get the loggers
0272: * - Get the global jndi context
0273: * - Get the list of the resource adapters. The list is reachable in the
0274: * - context parameter under the name RESOURCE_LIST.
0275: * - Get the transaction manager into the jndi
0276: * - Set the XML validation property
0277: */
0278: public Rar(Context ctx, String jDom, String jServ,
0279: WorkManager wrkMgr, ResourceBootstrapContext btCtx) {
0280:
0281: try {
0282: rarFileName = (String) ctx.lookup("rarFileName");
0283: isInEar = ((Boolean) ctx.lookup("isInEar")).booleanValue();
0284: if (isInEar) {
0285: earUrl = (URL) ctx.lookup("earUrl");
0286: }
0287: curLoader = (ClassLoader) ctx.lookup("classloader");
0288: } catch (NamingException e) {
0289: String err = "Error while getting parameter from context param.";
0290: logger.log(BasicLevel.ERROR, err + e.getMessage());
0291: throw new ResourceServiceException(err, e);
0292: } catch (Exception ex) {
0293: String err = "Error while getting parameter from context param.";
0294: logger.log(BasicLevel.ERROR, err + ex.getMessage());
0295: throw new ResourceServiceException(err, ex);
0296: }
0297:
0298: workMgr = wrkMgr;
0299: bootCtx = btCtx;
0300: rCtx = ctx;
0301: jDomain = jDom;
0302: jServer = jServ;
0303:
0304: // Avoid using NamingManager here: performance is not a goal here.
0305: try {
0306: ictx = new InitialContext();
0307: } catch (NamingException e) {
0308: logger
0309: .log(BasicLevel.ERROR,
0310: "Cannot create initial context when Resource service initializing");
0311: throw new ServiceException(
0312: "Cannot create initial context when Resource service initializing",
0313: e);
0314: }
0315:
0316: // Get a reference to the Transaction service
0317: try {
0318: ServiceManager sm = ServiceManager.getInstance();
0319: TransactionService ts = (TransactionService) sm
0320: .getTransactionService();
0321: tm = ts.getTransactionManager();
0322: } catch (Exception e) {
0323: logger.log(BasicLevel.ERROR,
0324: "Cannot get the Transaction service: " + e);
0325: throw new ServiceException(
0326: "Cannot get the Transaction service: ", e);
0327: }
0328:
0329: // Get the JMX Server via JMX Service
0330: try {
0331: mbeanServer = ((JmxService) ServiceManager.getInstance()
0332: .getJmxService()).getJmxServer();
0333: } catch (Exception e) {
0334: // the JMX service may not be started
0335: mbeanServer = null;
0336: }
0337:
0338: // Use Jakarta Common Modeler API
0339: oRegistry = JonasMBeanTools.getRegistry();
0340:
0341: ru = new ResourceUtility(mbeanServer, oRegistry, logger,
0342: setterLogger, manageLogger);
0343: }
0344:
0345: /**
0346: * Process the resource adapter. This Resource Adapter is configured via
0347: * xml files in the rar file
0348: */
0349: public Context processRar() throws Exception {
0350:
0351: String onRar = null;
0352: // Get the ra.xml and jonas-ra.xml descriptions
0353: RarDeploymentDesc radesc = null;
0354: ConnectorDesc conn = null;
0355: JonasConnectorDesc jConn = null;
0356: xmlContent = null;
0357: jonasXmlContent = null;
0358: ConnectionManager cm = null;
0359: ManagedConnectionFactory mcf = null;
0360:
0361: radesc = RarManagerWrapper.getInstance(rCtx);
0362: conn = radesc.getConnectorDesc();
0363: jConn = radesc.getJonasConnectorDesc();
0364: xmlContent = radesc.getXmlContent();
0365: jonasXmlContent = radesc.getJOnASXmlContent();
0366:
0367: if (conn == null && jConn == null) {
0368: logger.log(BasicLevel.ERROR, "Rar.processRar: Resource ("
0369: + rarFileName + ") must be a valid RAR file.");
0370: throw new Exception("resource input file incorrect");
0371: }
0372:
0373: // Need to check if rarlink is specified. If yes and has been processed, then get the correct
0374: // Connector object and continue, if it has not been processed, then add this rar to the
0375: // deferred list and it will be processed at end of the rar files. If no rarlink, then
0376: // make sure that the connector object is valid.
0377: String rarVal = null;
0378: if (jConn != null) {
0379: rarVal = (jConn.getRarlink() == null ? null : jConn
0380: .getRarlink());
0381: if (rarVal != null && rarVal.length() > 0) {
0382: lnkJndiName = rarVal;
0383: conn = getConnectorDesc(rarVal);
0384: if (conn == null) {
0385: return rCtx;
0386: }
0387: ConfigObj co = (ConfigObj) getConfigObject(rarVal);
0388: lnkRarFilename = co.rarFilename;
0389: xmlContent = getXmlContent(rarVal);
0390: // Get the config parameters
0391: lnkJonasConn = getJonasConnectorDesc(rarVal);
0392: } else if (conn == null) {
0393: logger.log(BasicLevel.ERROR,
0394: "Rar.processRar: Resource (" + rarFileName
0395: + ") is not valid.");
0396: throw new Exception(
0397: "resource input file incorrect: no ra.xml file");
0398: }
0399: bldSecurityTable(lnkJonasConn, jConn);
0400: raConn = conn;
0401: jonasConn = jConn;
0402: } else {
0403: logger
0404: .log(
0405: BasicLevel.ERROR,
0406: "Rar.processRar: Resource ("
0407: + rarFileName
0408: + ") must be a valid RAR file, there must be jonas-ra.xml file.");
0409: throw new Exception(
0410: "resource input file incorrect: no jonas-ra.xml file");
0411: }
0412:
0413: // Setup the global pool parameters
0414: ConnectionManagerPoolParams pParams = ru.configurePoolParams(
0415: jConn.getPoolParamsDesc(), jConn
0416: .getJdbcConnParamsDesc(), null);
0417:
0418: // Process the RAR file adding each of the jar files into the classloader and if the
0419: // native-lib is defined then place all other files into that directory.
0420:
0421: extractJars(rarFileName, jConn);
0422:
0423: // Process the config values
0424: ResourceadapterDesc ra = conn.getResourceadapterDesc();
0425:
0426: // Check Authentication Mechanisms, must if some exist then BasicPassword
0427: // must be present
0428: boolean basicPass = true;
0429: List authList = ra.getAuthenticationMechanismList();
0430: if (authList != null && authList.size() > 0) {
0431: basicPass = false;
0432: for (Iterator i = authList.iterator(); i.hasNext();) {
0433: AuthenticationMechanismDesc am = (AuthenticationMechanismDesc) i
0434: .next();
0435: if (am.getAuthenticationMechanismType()
0436: .equalsIgnoreCase("BasicPassword")) {
0437: basicPass = true;
0438: break;
0439: }
0440: }
0441: if (!basicPass) {
0442: logger
0443: .log(
0444: BasicLevel.ERROR,
0445: "Rar.processRar: Resource ("
0446: + rarFileName
0447: + ") doesn't contain an AuthenticationMechanismType that is supported by JOnAS(BasicPassword).");
0448: throw new Exception(
0449: "No AuthenticationMechanismType that is supported by JOnAS(BasicPassword).");
0450: }
0451: }
0452:
0453: ConfigPropertyDesc[] cfgRaJonas = null;
0454:
0455: String logEnabled = null;
0456: if (jConn.getLogEnabled() != null) {
0457: logEnabled = jConn.getLogEnabled().trim();
0458: }
0459: String logTopic = null;
0460: if (jConn.getLogTopic() != null) {
0461: logTopic = jConn.getLogTopic().trim();
0462: }
0463:
0464: Referenceable cf = null;
0465: String jndiName = null;
0466: String mcfc = null;
0467: ConfigObj cObj = null;
0468:
0469: if (lnkJonasConn != null) {
0470: cfgRaJonas = ru.buildConfigProperty(ra
0471: .getConfigPropertyList(), jConn
0472: .getJonasConfigPropertyList(), lnkJonasConn
0473: .getJonasConfigPropertyList());
0474: } else {
0475: cfgRaJonas = ru.buildConfigProperty(ra
0476: .getConfigPropertyList(), jConn
0477: .getJonasConfigPropertyList(), null);
0478: }
0479:
0480: Properties tmProp = new Properties();
0481: if (jConn.getTmParamsDesc() != null) {
0482: List tmParams = jConn.getTmParamsDesc()
0483: .getTmConfigPropertyList();
0484: if (tmParams != null) {
0485: for (Iterator i = tmParams.iterator(); i.hasNext();) {
0486: TmConfigPropertyDesc tpd = (TmConfigPropertyDesc) i
0487: .next();
0488: String tpdVal = tpd.getTmConfigPropertyValue();
0489: if (tpdVal == null) {
0490: tpdVal = "";
0491: }
0492: tmProp.setProperty(tpd.getTmConfigPropertyName(),
0493: tpdVal);
0494: }
0495: if (logger.isLoggable(BasicLevel.DEBUG)) {
0496: logger.log(BasicLevel.DEBUG, "TM Properties: "
0497: + tmProp);
0498: }
0499: }
0500: }
0501:
0502: String specVersion = conn.getSpecVersion();
0503: if (specVersion.equals("1.0")) {
0504: if (!jConn.isSetup()) {
0505: logger
0506: .log(
0507: BasicLevel.ERROR,
0508: "Rar.processRar: Resource ("
0509: + rarFileName
0510: + ") must be a valid RAR file, there must be jonas-ra.xml file.");
0511: throw new Exception(
0512: "resource input file incorrect: no jonas-ra.xml file");
0513: }
0514: jndiName = jConn.getJndiName().trim();
0515: ConnectionManagerPoolParams cmpp = ru.configurePoolParams(
0516: jConn.getPoolParamsDesc(), jConn
0517: .getJdbcConnParamsDesc(), null);
0518: onRar = registerRarMBean(cfgRaJonas, specVersion, jndiName);
0519: cm = ru.createConnectionManager(ra.getTransactionSupport(),
0520: tm, logger, poolLogger, jndiName);
0521: // Only need to build an MCF for a 1.0 resource adapter
0522: mcfc = ra.getManagedconnectionfactoryClass();
0523: mcf = (ManagedConnectionFactory) ru.processMCF(conn, jConn,
0524: cm, curLoader, rarFileName, mcfc, jndiName,
0525: logEnabled, logTopic, cfgRaJonas, resAdp, 0);
0526: ((ConnectionManagerImpl) cm).setResourceAdapter(mcf, cmpp);
0527:
0528: cf = (Referenceable) mcf.createConnectionFactory(cm);
0529:
0530: // Add entry to list of configured objects
0531: cObj = new ConfigObj(JCD, 0, jndiName, rarFileName, null,
0532: mcfc, cf);
0533: cObj.cm = cm;
0534: cObj.basicPass = basicPass;
0535:
0536: cfgObjs.put(jndiName, cObj);
0537:
0538: jndinames.add(jndiName);
0539:
0540: ru.registerMBean(cf, jndiName, onRar, rarFileName, conn,
0541: jonasConn, JCD, 0, jcaResourceMBean,
0542: JCAResourceName, jDomain, jServer, ictx,
0543: buildProp(cfgRaJonas), "",
0544: (ConnectionManagerImpl) cm);
0545: ((ConnectionManagerImpl) cm).setXAName(ru
0546: .getJcaMcfName(jndiName));
0547: ((ConnectionManagerImpl) cm).registerXAResource(tmProp);
0548:
0549: } else if (conn.getSpecVersion().equals("1.5")) {
0550: String raStr = conn.getResourceadapterDesc()
0551: .getResourceadapterClass().trim();
0552: boolean isResAdapt = false;
0553: logger.log(BasicLevel.INFO, "Starting deployment of "
0554: + rarFileName);
0555: if (raStr != null && raStr.length() > 0) {
0556: isResAdapt = true;
0557:
0558: // Instantiate the resource adapter class
0559: Class raClass = curLoader.loadClass(raStr);
0560: resAdp = (ResourceAdapter) raClass.newInstance();
0561:
0562: ru.processSetters(raClass, resAdp, rarFileName,
0563: cfgRaJonas);
0564:
0565: // Call Resourceadapter start method
0566: try {
0567: resAdp.start(bootCtx);
0568: } catch (Exception ex) {
0569: logger.log(BasicLevel.ERROR,
0570: "Rar: Error from resource (" + rarFileName
0571: + ") start method.");
0572: throw new Exception("Error from start method. ", ex);
0573: } catch (Throwable th) {
0574: logger.log(BasicLevel.ERROR,
0575: "Rar: Error from resource (" + rarFileName
0576: + ") start method.");
0577: throw new Exception("Error from start method. ", th);
0578: }
0579: }
0580:
0581: onRar = registerRarMBean(cfgRaJonas, specVersion, null);
0582:
0583: // Loop thru ConnectionDefinition(outbound) factories
0584: OutboundResourceadapterDesc outRa = ra
0585: .getOutboundResourceadapterDesc();
0586: List cdList = null;
0587: if (outRa != null) {
0588: cdList = outRa.getConnectionDefinitionList();
0589: }
0590: ConnectionDefinitionDesc conDef = null;
0591: JonasConnectionDefinitionDesc jConDef = null;
0592: String id = null;
0593: int idOffset = -1;
0594: if (cdList != null) {
0595: if (cdList.size() > 0 && !jConn.isSetup()) {
0596: logger
0597: .log(
0598: BasicLevel.ERROR,
0599: "Rar.processRar: Resource ("
0600: + rarFileName
0601: + ") must be a valid RAR file, there must be jonas-ra.xml file.");
0602: throw new Exception(
0603: "resource input file incorrect: no jonas-ra.xml file");
0604: }
0605: for (int cd = 0; cd < cdList.size(); cd++) {
0606: conDef = (ConnectionDefinitionDesc) cdList.get(cd);
0607: id = conDef.getId();
0608: idOffset++;
0609: jConDef = (JonasConnectionDefinitionDesc) ru
0610: .getJonasXML(jConn, id, idOffset, JCD);
0611:
0612: if (jConDef.getLogEnabled() != null) {
0613: logEnabled = jConDef.getLogEnabled().trim();
0614: }
0615: if (jConDef.getLogTopic() != null) {
0616: logTopic = jConDef.getLogTopic().trim();
0617: }
0618: ConfigPropertyDesc[] cfgCdDesc = ru
0619: .buildConfigProperty(conDef
0620: .getConfigPropertyList(), jConDef
0621: .getJonasConfigPropertyList(), null);
0622: ConnectionManagerPoolParams pool = ru
0623: .configurePoolParams(jConDef
0624: .getPoolParamsDesc(), jConDef
0625: .getJdbcConnParamsDesc(), pParams);
0626: jndiName = jConDef.getJndiName().trim();
0627: cm = ru.createConnectionManager(outRa
0628: .getTransactionSupport(), tm, logger,
0629: poolLogger, jndiName);
0630:
0631: mcfc = conDef.getManagedconnectionfactoryClass();
0632: mcf = (ManagedConnectionFactory) ru.processMCF(
0633: conn, jConn, cm, curLoader, rarFileName,
0634: mcfc, jndiName, logEnabled, logTopic,
0635: cfgCdDesc, resAdp, idOffset);
0636: ((ConnectionManagerImpl) cm).setResourceAdapter(
0637: mcf, pool);
0638:
0639: cf = (Referenceable) mcf
0640: .createConnectionFactory(cm);
0641:
0642: // Add entry to list of configured objects
0643: cObj = new ConfigObj(JCD, idOffset, jndiName,
0644: rarFileName, null, mcfc, cf);
0645: cObj.cm = cm;
0646: cObj.basicPass = basicPass;
0647:
0648: cfgObjs.put(jndiName, cObj);
0649:
0650: jndinames.add(jndiName);
0651:
0652: String desc = "";
0653: List descList = jConDef.getDescriptionList();
0654: if (descList != null) {
0655: for (int i = 0; i < descList.size(); i++) {
0656: String tmp = (String) descList.get(i);
0657: desc = desc + tmp;
0658: }
0659: }
0660: ru.registerMBean(cf, jndiName, onRar, rarFileName,
0661: conn, jonasConn, JCD, idOffset,
0662: jcaResourceMBean, JCAResourceName, jDomain,
0663: jServer, ictx, buildProp(cfgCdDesc), desc,
0664: (ConnectionManagerImpl) cm);
0665:
0666: ((ConnectionManagerImpl) cm).setXAName(ru
0667: .getJcaMcfName(jndiName));
0668: ((ConnectionManagerImpl) cm)
0669: .registerXAResource(tmProp);
0670: }
0671: }
0672:
0673: // We only want to do anything with Inbound or Adminobjects if the Resourceadapter
0674: // class was specified, otherwise it is a 1.0 RAR with a 1.5 ra.xml
0675: if (isResAdapt) {
0676: // Loop thru Messagelisteners (inbound) factories
0677: InboundResourceadapterDesc inAdapt = ra
0678: .getInboundResourceadapterDesc();
0679: MessageadapterDesc msgAdapt = null;
0680: List mlList = null;
0681: if (inAdapt != null) {
0682: msgAdapt = inAdapt.getMessageadapterDesc();
0683: mlList = null;
0684: if (msgAdapt != null) {
0685: mlList = msgAdapt.getMessagelistenerList();
0686: }
0687: }
0688: MessagelistenerDesc msgList = null;
0689: JonasActivationspecDesc jAct = null;
0690: id = null;
0691: idOffset = -1;
0692: if (mlList != null) {
0693: if (mlList.size() > 0 && !jConn.isSetup()) {
0694: logger
0695: .log(
0696: BasicLevel.ERROR,
0697: "Rar.processRar: Resource ("
0698: + rarFileName
0699: + ") must be a valid RAR file, there must be jonas-ra.xml file.");
0700: throw new Exception(
0701: "resource input file incorrect: no jonas-ra.xml file");
0702: }
0703: for (int ml = 0; ml < mlList.size(); ml++) {
0704: msgList = (MessagelistenerDesc) mlList.get(ml);
0705: id = msgList.getId();
0706: idOffset++;
0707: jAct = (JonasActivationspecDesc) ru
0708: .getJonasXML(jConn, id, idOffset, JAS);
0709:
0710: processJAS(rarFileName, msgList, jAct, idOffset);
0711: }
0712: }
0713:
0714: // Loop thru Adminobjects
0715: List aoList = ra.getAdminobjectList();
0716: AdminobjectDesc admObj = null;
0717: JonasAdminobjectDesc jAObj = null;
0718: id = null;
0719: idOffset = -1;
0720: if (aoList != null) {
0721: if (aoList.size() > 0 && !jConn.isSetup()) {
0722: logger.log(BasicLevel.ERROR, "aoList = "
0723: + aoList);
0724: logger
0725: .log(
0726: BasicLevel.ERROR,
0727: "Rar.processRar: Resource ("
0728: + rarFileName
0729: + ") must be a valid RAR file, there must be jonas-ra.xml file.");
0730: throw new Exception(
0731: "resource input file incorrect: no jonas-ra.xml file");
0732: }
0733: for (int ao = 0; ao < aoList.size(); ao++) {
0734: admObj = (AdminobjectDesc) aoList.get(ao);
0735: id = admObj.getId();
0736: idOffset++;
0737: jAObj = (JonasAdminobjectDesc) ru.getJonasXML(
0738: jConn, id, idOffset, JAO);
0739:
0740: ConfigPropertyDesc[] cfgCdDesc = ru
0741: .buildConfigProperty(admObj
0742: .getConfigPropertyList(), jAObj
0743: .getJonasConfigPropertyList(),
0744: null);
0745:
0746: processJAO(rarFileName, admObj, jAObj,
0747: cfgCdDesc, idOffset);
0748: }
0749: }
0750: }
0751: } else {
0752: logger
0753: .log(
0754: BasicLevel.ERROR,
0755: "ResourceService.createRA: Resource ("
0756: + rarFileName
0757: + ") must be specify a valid specification of 1.0 or 1.5.");
0758: throw new Exception(
0759: "resource input file incorrect: invalid specification support(only 1.0 & 1.5 are valid)");
0760: }
0761:
0762: logger.log(BasicLevel.INFO, rarFileName + " available");
0763: try {
0764: rCtx.rebind("onRar", onRar);
0765: rCtx.rebind("deployed", new Boolean(true));
0766: } catch (Exception ex) {
0767: String err = "Error while getting parameter from context param.";
0768: logger.log(BasicLevel.ERROR, err + ex.getMessage());
0769: throw new ResourceServiceException(err, ex);
0770: }
0771: return rCtx;
0772: }
0773:
0774: /**
0775: * Register the Rar.
0776: * @param cfgRaJonas ConfigPropertyDesc []
0777: * @param specVersion String
0778: * @param pooProp Properties pool params
0779: * @return String object Name of the Rar
0780: * @throws Exception thrown
0781: */
0782: private String registerRarMBean(ConfigPropertyDesc[] cfgRaJonas,
0783: String specVersion, String jndiName) throws Exception {
0784:
0785: // --------------------------
0786: // Register MBeans cf. JSR 77
0787: // --------------------------
0788: if (mbeanServer != null) {
0789:
0790: // ------------------------------------------------
0791: // Register this ResourceAdapter with JMX - old way
0792: // ------------------------------------------------
0793: String raName = null; // ResourceAdapter name
0794: Properties props = buildProp(cfgRaJonas);
0795:
0796: // MBean resourcesource registering
0797: String jmxRaName = (new File(rarFileName)).toURL()
0798: .getFile().replace(':', '|');
0799: mbeanServer.registerMBean(new JmxResourceAdapter(props,
0800: jndiName, rarFileName, isInEar, earUrl),
0801: JonasObjectName.resourceAdapter(jmxRaName));
0802:
0803: // ResourceAdapterModule MBean
0804: // ---------------------------
0805: String appName = null;
0806: if (isInEar) {
0807: appName = EarServiceImpl
0808: .buildJ2eeApplicationName(earUrl);
0809: }
0810:
0811: // ResourceAdapter Module name
0812: String resourceAdapaterModuleName = buildModuleName(
0813: jmxRaName, appName);
0814:
0815: ObjectName onResourceAdapterModule = J2eeObjectName
0816: .getResourceAdapterModule(jDomain, jServer,
0817: appName, resourceAdapaterModuleName);
0818:
0819: if (manageLogger.isLoggable(BasicLevel.DEBUG)) {
0820: manageLogger.log(BasicLevel.DEBUG,
0821: "ObjectName created for ResourceAdapterModule: "
0822: + onResourceAdapterModule.toString());
0823: }
0824:
0825: ResourceAdapterModule raModuleMBean = new ResourceAdapterModule(
0826: onResourceAdapterModule, isInEar, earUrl);
0827: raModuleMBean.setFileName(jmxRaName);
0828: raModuleMBean.setDeploymentDescriptor(xmlContent);
0829: raModuleMBean.setJonasDeploymentDescriptor(jonasXmlContent);
0830: ManagedBean oManaged = oRegistry
0831: .findManagedBean("ResourceAdapterModule");
0832:
0833: ModelMBean oMBean = oManaged.createMBean(raModuleMBean);
0834:
0835: mbeanServer.registerMBean(oMBean, onResourceAdapterModule);
0836: if (manageLogger.isLoggable(BasicLevel.DEBUG)) {
0837: manageLogger.log(BasicLevel.DEBUG,
0838: "ResourceAdapterModule MBean created");
0839: }
0840:
0841: // ResourceAdapter MBean
0842: // ---------------------
0843: raName = resourceAdapaterModuleName;
0844: ObjectName onResourceAdapter = J2eeObjectName
0845: .getResourceAdapter(jDomain,
0846: resourceAdapaterModuleName, appName,
0847: jServer, raName);
0848: org.objectweb.jonas.resource.ResourceAdapter raMBean = new org.objectweb.jonas.resource.ResourceAdapter(
0849: onResourceAdapter.toString(), props, jndiName,
0850: jmxRaName, isInEar, earUrl, specVersion);
0851: oManaged = oRegistry.findManagedBean("ResourceAdapter");
0852: oMBean = oManaged.createMBean(raMBean);
0853: if (manageLogger.isLoggable(BasicLevel.DEBUG)) {
0854: manageLogger.log(BasicLevel.DEBUG,
0855: "ResourceAdapter ManagedBean created");
0856: }
0857: mbeanServer.registerMBean(oMBean, onResourceAdapter);
0858:
0859: // Update the ResourceAdapterModule MBean with the ResourceAdapter MBean's OBJECT_NAME
0860: raModuleMBean.setResourceAdapter(onResourceAdapter
0861: .toString());
0862: if (manageLogger.isLoggable(BasicLevel.DEBUG)) {
0863: manageLogger.log(BasicLevel.DEBUG,
0864: "ResourceAdapterModule ManagedBean updated");
0865: }
0866:
0867: // JCA Resource implemented by this ResourceAdapter
0868: // ------------------------------------------------
0869: JCAResourceName = raName; // TO BE IMPLEMENTED
0870: ObjectName onJCAResource = J2eeObjectName.getJCAResource(
0871: jDomain, jServer, raName, JCAResourceName);
0872: jcaResourceMBean = new JCAResource(onJCAResource.toString());
0873: oManaged = oRegistry.findManagedBean("JCAResource");
0874: oMBean = oManaged.createMBean(jcaResourceMBean);
0875: if (manageLogger.isLoggable(BasicLevel.DEBUG)) {
0876: manageLogger.log(BasicLevel.DEBUG,
0877: "JCAResource J2EEResource created");
0878: }
0879: mbeanServer.registerMBean(oMBean, onJCAResource);
0880:
0881: // Update the ResourceAdapter MBean with the JCAResource MBean's OBJECT_NAME
0882: raMBean.setJcaResource(onJCAResource.toString());
0883: if (manageLogger.isLoggable(BasicLevel.DEBUG)) {
0884: manageLogger.log(BasicLevel.DEBUG,
0885: "JCAResource J2EEResource updated");
0886: }
0887:
0888: return onResourceAdapterModule.toString();
0889: } else {
0890: String jmxRaName = (new File(rarFileName)).toURL()
0891: .getFile().replace(':', '|');
0892: return jmxRaName;
0893: }
0894: }
0895:
0896: /**
0897: * Unregister the resource adapter.
0898: *
0899: * @throws Exception thrown
0900: */
0901: public void unRegister() throws Exception {
0902:
0903: for (int i = 0; i < jndinames.size(); i++) {
0904: String jName = (String) jndinames.get(i);
0905: ConfigObj co = (ConfigObj) cfgObjs.get(jName);
0906: try {
0907: // Unbind from jndi
0908: ictx.unbind(jName);
0909: } catch (NamingException ex) {
0910: if (logger.isLoggable(BasicLevel.DEBUG)) {
0911: logger.log(BasicLevel.DEBUG,
0912: "ResourceService: cannot unbind jndiname of "
0913: + jName + " for ResourceAdapter "
0914: + rarFileName, ex);
0915: }
0916: throw new ServiceException("Cannot unregister", ex);
0917: }
0918:
0919: if (co.defaultAS) {
0920: removeDefaultAS(jName);
0921: }
0922: if (co != null) { // Add for now, take out for final
0923: if (co.cm != null) {
0924: // The connection manger must stop managing the RA
0925: ((ConnectionManagerImpl) co.cm)
0926: .cleanResourceAdapter();
0927: }
0928: }
0929:
0930: }
0931: cfgObjs.clear();
0932:
0933: // --------------------------
0934: // unregister MBeans cf. JSR 77
0935: // --------------------------
0936: if (mbeanServer != null) {
0937: // MBean resourcesource registering
0938: String jmxRaName = (new File(rarFileName)).toURL()
0939: .getFile().replace(':', '|');
0940: mbeanServer.unregisterMBean(JonasObjectName
0941: .resourceAdapter(jmxRaName));
0942:
0943: // Construct ResourceAdapterModule MBean name
0944: // ------------------------------------------
0945: String appName = null;
0946: if (isInEar) {
0947: appName = EarServiceImpl
0948: .buildJ2eeApplicationName(earUrl);
0949: }
0950: // ResourceAdapter Module name
0951: String resourceAdapaterModuleName = buildModuleName(
0952: jmxRaName, appName);
0953: ObjectName onResourceAdapterModule = J2eeObjectName
0954: .getResourceAdapterModule(jDomain, jServer,
0955: appName, resourceAdapaterModuleName);
0956: try {
0957: String[] resourceAdapaterNames = (String[]) mbeanServer
0958: .getAttribute(onResourceAdapterModule,
0959: "resourceAdapters");
0960:
0961: // Unregister ResourceAdapterModule MBean
0962: mbeanServer.unregisterMBean(onResourceAdapterModule);
0963:
0964: // Unregister ResourceAdapters
0965: for (int i = 0; i < resourceAdapaterNames.length; i++) {
0966: String resourceAdapaterName = resourceAdapaterNames[i];
0967: ObjectName onResourceAdapater = new ObjectName(
0968: resourceAdapaterName);
0969: // Determine this ResourceAdapter's JCAResource
0970: String jcaResourceName = (String) mbeanServer
0971: .getAttribute(onResourceAdapater,
0972: "jcaResource");
0973: // Update the list of resources in the J2EEServer MBean with the JCAResource MBean's OBJECT_NAME
0974: mbeanServer.unregisterMBean(onResourceAdapater);
0975: ObjectName onJCAResource = new ObjectName(
0976: jcaResourceName);
0977: String[] connectionFactoriesNames = (String[]) mbeanServer
0978: .getAttribute(onJCAResource,
0979: "connectionFactories");
0980: // Unregister the JCA ConnectionFactories of this JCAResource
0981: for (int j = 0; j < connectionFactoriesNames.length; j++) {
0982: String connectionFactoryName = (String) connectionFactoriesNames[j];
0983: ObjectName onJCAConnectionFactory = new ObjectName(
0984: connectionFactoryName);
0985: String managedConnectionFactoryName = (String) mbeanServer
0986: .getAttribute(onJCAConnectionFactory,
0987: "managedConnectionFactory");
0988: mbeanServer
0989: .unregisterMBean(onJCAConnectionFactory);
0990: mbeanServer.unregisterMBean(new ObjectName(
0991: managedConnectionFactoryName));
0992: }
0993: String[] adminObjectNames = (String[]) mbeanServer
0994: .getAttribute(onJCAResource, "adminObjects");
0995: // Unregister the JCA AdminObjects of this JCAResource
0996: for (int j = 0; j < adminObjectNames.length; j++) {
0997: String adminObjectName = (String) adminObjectNames[j];
0998: ObjectName onJCAAdminObject = new ObjectName(
0999: adminObjectName);
1000: mbeanServer.unregisterMBean(onJCAAdminObject);
1001: }
1002: String[] activationSpecNames = (String[]) mbeanServer
1003: .getAttribute(onJCAResource,
1004: "activationSpecs");
1005: // Unregister the JCA ActivationSpecs of this JCAResource
1006: for (int j = 0; j < activationSpecNames.length; j++) {
1007: String activationSpecName = (String) activationSpecNames[j];
1008: ObjectName onJCAActivationSpec = new ObjectName(
1009: activationSpecName);
1010: mbeanServer
1011: .unregisterMBean(onJCAActivationSpec);
1012: }
1013: mbeanServer.unregisterMBean(onJCAResource);
1014: }
1015: } catch (MalformedObjectNameException ma) {
1016: logger.log(BasicLevel.ERROR,
1017: "Cannot cleanly unregister RAR: ", ma);
1018: } catch (MBeanRegistrationException mr) {
1019: logger.log(BasicLevel.ERROR,
1020: "Cannot cleanly unregister RAR: ", mr);
1021: } catch (InstanceNotFoundException infe) {
1022: logger.log(BasicLevel.ERROR,
1023: "Cannot cleanly unregister RAR: ", infe);
1024: } catch (Exception e) {
1025: logger.log(BasicLevel.ERROR,
1026: "Cannot cleanly unregister RAR: ", e);
1027: }
1028: }
1029:
1030: // Call Resourceadapter stop
1031: if (resAdp != null) {
1032: resAdp.stop();
1033: }
1034:
1035: }
1036:
1037: //--------------------------------------------------------------
1038: // Other Method
1039: //--------------------------------------------------------------
1040:
1041: /**
1042: * get the ConfigObj matching the jndiname specified
1043: *
1044: * @param jndiName String of jndi name
1045: * @return Object ConfigObj associated with the jndi name
1046: */
1047: public Object getConfigObj(String jndiName) {
1048: synchronized (cfgObjs) {
1049: ConfigObj co = (ConfigObj) cfgObjs.get(jndiName);
1050: return co;
1051: }
1052: }
1053:
1054: /**
1055: * get the ResourceAdapter Connector object for this Rar
1056: *
1057: * @return ConnectorDesc associated resource adapter connector object
1058: */
1059: public ConnectorDesc getConnectorDesc() {
1060: return raConn;
1061: }
1062:
1063: /**
1064: * set the ResourceAdapter Connector object for this Rar
1065: *
1066: * @param cd ConnectorDesc to associate with
1067: */
1068: public void setConnectorDesc(ConnectorDesc cd) {
1069: raConn = cd;
1070: }
1071:
1072: /**
1073: * get the Xml Content for this Rar
1074: *
1075: * @return String Xml content of this RAR
1076: */
1077: public String getXmlContent() {
1078: return xmlContent;
1079: }
1080:
1081: /**
1082: * get the JonasConnector object for this Rar
1083: *
1084: * @return JonasConnectorDesc associated JonasConnector
1085: */
1086: public JonasConnectorDesc getJonasConnectorDesc() {
1087: return jonasConn;
1088: }
1089:
1090: /**
1091: * set the JonasConnector object for this Rar
1092: *
1093: * @param jcd JonasConnectorDesc to associate with
1094: */
1095: public void setJonasConnectorDesc(JonasConnectorDesc jcd) {
1096: jonasConn = jcd;
1097: }
1098:
1099: public Object getFactory(String jndiname) {
1100: ConfigObj co = (ConfigObj) cfgObjs.get(jndiname);
1101: return (co == null ? null : co.factory);
1102: }
1103:
1104: public String getInterface(String jndiname) {
1105: ConfigObj co = (ConfigObj) cfgObjs.get(jndiname);
1106: return (co == null ? null : co.interfaceStr);
1107: }
1108:
1109: public Vector getJndinames() {
1110: return jndinames;
1111: }
1112:
1113: public ResourceAdapter getResourceAdapter() {
1114: return resAdp;
1115: }
1116:
1117: public void configureAS(ActivationSpec as, List acp, List jacp,
1118: String jndiname, String ejbName) throws Exception {
1119: ConfigPropertyDesc[] asCp = ru.buildConfigProperty(null, acp,
1120: jacp);
1121:
1122: // set resource adapter if not already done
1123: if (((ResourceAdapterAssociation) as).getResourceAdapter() == null) {
1124: ((ResourceAdapterAssociation) as)
1125: .setResourceAdapter(resAdp);
1126: }
1127: ru.processSetters(as.getClass(), as, ejbName, asCp);
1128: validateAS(as, jndiname, acp, jacp);
1129: }
1130:
1131: //--------------------------------------------------------------
1132: // Private Methods
1133: //--------------------------------------------------------------
1134:
1135: private void validateAS(ActivationSpec as, String jndiname,
1136: List acp, List jacp) throws Exception {
1137: boolean found = false;
1138: String reqName = null;
1139: RequiredConfigPropertyDesc rcProp = null;
1140: ActivationConfigPropertyDesc acProp = null;
1141: // Check that the req properties have been satisfied
1142: ConfigObj co = (ConfigObj) cfgObjs.get(jndiname);
1143: for (Iterator r = co.reqConfigProps.iterator(); r.hasNext();) {
1144: rcProp = (RequiredConfigPropertyDesc) r.next();
1145: found = false;
1146: reqName = rcProp.getConfigPropertyName();
1147: if (acp != null) {
1148: for (Iterator i = acp.iterator(); i.hasNext();) {
1149: acProp = (ActivationConfigPropertyDesc) i.next();
1150: if (reqName.equals(acProp
1151: .getActivationConfigPropertyName())) {
1152: found = true;
1153: break;
1154: }
1155: }
1156: }
1157: if (!found && jacp != null) {
1158: for (Iterator i = jacp.iterator(); i.hasNext();) {
1159: acProp = (ActivationConfigPropertyDesc) i.next();
1160: if (reqName.equals(acProp
1161: .getActivationConfigPropertyName())) {
1162: found = true;
1163: break;
1164: }
1165: }
1166: }
1167: if (!found) {
1168: logger.log(BasicLevel.ERROR, "Required property "
1169: + reqName + " not specified ");
1170: throw new ResourceAllocationException(
1171: "Required property " + reqName
1172: + " not specified ");
1173: }
1174: }
1175:
1176: // Call validate
1177: try {
1178: as.validate();
1179: } catch (UnsupportedOperationException uoe) {
1180: // treat this as no error from validate
1181: } catch (Exception ex) {
1182: logger.log(BasicLevel.ERROR,
1183: "Error from ActivationSpec.validate(). " + ex);
1184: throw new ResourceAllocationException(
1185: "Error from ActivationSpec.validate(). ", ex);
1186: }
1187:
1188: }
1189:
1190: // Needs to be implemented
1191: /**
1192: *
1193: */
1194: private void bldSecurityTable(JonasConnectorDesc lnkJCon,
1195: JonasConnectorDesc jCon) {
1196: }
1197:
1198: /**
1199: *
1200: * @param rarFileName String of rar file
1201: * @param appName the name of the application
1202: * @return String of module name
1203: */
1204: public static String buildModuleName(String rarFileName,
1205: String appName) {
1206: String sName = null;
1207: try {
1208: sName = new File(rarFileName).getName();
1209: int iPos = sName.lastIndexOf('.');
1210: if (iPos > -1) {
1211: sName = sName.substring(0, iPos);
1212: }
1213: } catch (NullPointerException e) {
1214: // none action
1215: }
1216: if (appName != null) {
1217: StringBuffer sbName = new StringBuffer(appName);
1218: sbName.append(".");
1219: sbName.append(sName);
1220: sName = sbName.toString();
1221: }
1222:
1223: return sName;
1224: }
1225:
1226: /* Static methods
1227: *
1228: */
1229:
1230: /**
1231: * Create the factory specified
1232: *
1233: * @param jndiName name to bind the factory
1234: * @param rarObjectName unique name to match rar
1235: * @param factoryOffset factory offset in the to create
1236: * @param factoryType factory type to create
1237: * @param conn ConnectionDesc object to use for creation
1238: * @param jConn JonasConnectionDesc object to use for creation
1239: * @return Object factory created
1240: * @throws Exception if an error occurs
1241: */
1242: public Object createFactory(String jndiName, String rarObjectName,
1243: int factoryOffset, String factoryType, ConnectorDesc conn,
1244: JonasConnectorDesc jConn) throws Exception {
1245:
1246: ResourceUtility ru = new ResourceUtility(null, null, logger,
1247: setterLogger, manageLogger);
1248: Object factory = null;
1249: ManagedConnectionFactory mcf = null;
1250: ResourceadapterDesc ra = conn.getResourceadapterDesc();
1251: /**
1252: * Can currently assume Basic Password support since the RAR must have already been
1253: * deployed correctly
1254: */
1255: boolean basicPass = true;
1256: /**
1257: * Needed if a 1.5 rar and ResourceAdapterAssociation is implemented
1258: */
1259: ResourceAdapter resAdp = null;
1260:
1261: String logEnabled = null;
1262: if (jConn.getLogEnabled() != null) {
1263: logEnabled = jConn.getLogEnabled().trim();
1264: }
1265: String logTopic = null;
1266: if (jConn.getLogTopic() != null) {
1267: logTopic = jConn.getLogTopic().trim();
1268: }
1269: String specVersion = conn.getSpecVersion();
1270:
1271: ConnectionManager cm = null;
1272: String jBase = null;
1273: try {
1274: jBase = System.getProperty("jonas.base");
1275: if (jBase != null) {
1276: // Get a reference to the Transaction service
1277: try {
1278: ServiceManager sm = ServiceManager.getInstance();
1279: TransactionService ts = (TransactionService) sm
1280: .getTransactionService();
1281: tm = ts.getTransactionManager();
1282: } catch (Exception e) {
1283: logger.log(BasicLevel.ERROR,
1284: "Cannot get the Transaction service: " + e);
1285: throw new Exception(
1286: "Cannot get the Transaction service: ", e);
1287: }
1288:
1289: cm = ru.createConnectionManager(ra
1290: .getTransactionSupport(), tm, logger,
1291: poolLogger, jndiName);
1292: }
1293: } catch (Exception ex) {
1294: logger.log(BasicLevel.ERROR,
1295: "Use default ConnectionManager: " + rarObjectName);
1296: }
1297:
1298: // Build the global config and pool values
1299: ConfigPropertyDesc[] cfgRaJonas = ru.buildConfigProperty(ra
1300: .getConfigPropertyList(), jConn
1301: .getJonasConfigPropertyList(), null);
1302: ConnectionManagerPoolParams cmpp = ru.configurePoolParams(jConn
1303: .getPoolParamsDesc(), jConn.getJdbcConnParamsDesc(),
1304: null);
1305: Properties tmProp = new Properties();
1306: if (jConn.getTmParamsDesc() != null) {
1307: List tmParams = jConn.getTmParamsDesc()
1308: .getTmConfigPropertyList();
1309: if (tmParams != null) {
1310: for (Iterator i = tmParams.iterator(); i.hasNext();) {
1311: TmConfigPropertyDesc tpd = (TmConfigPropertyDesc) i
1312: .next();
1313: String tpdVal = tpd.getTmConfigPropertyValue();
1314: if (tpdVal == null) {
1315: tpdVal = "";
1316: }
1317: tmProp.setProperty(tpd.getTmConfigPropertyName(),
1318: tpdVal);
1319: }
1320: if (logger.isLoggable(BasicLevel.DEBUG)) {
1321: logger.log(BasicLevel.DEBUG, "TM Properties: "
1322: + tmProp);
1323: }
1324: }
1325: }
1326:
1327: if (specVersion.equals("1.0")) {
1328: // Only need to build an MCF for a 1.0 resource adapter
1329: mcf = (ManagedConnectionFactory) ru.processMCF(conn, jConn,
1330: cm, curLoader, rarObjectName, ra
1331: .getManagedconnectionfactoryClass(),
1332: jndiName, logEnabled, logTopic, cfgRaJonas, resAdp,
1333: 0);
1334: if (cm != null) {
1335: ((ConnectionManagerImpl) cm).setResourceAdapter(mcf,
1336: cmpp);
1337: factory = mcf.createConnectionFactory(cm);
1338: } else {
1339: factory = mcf.createConnectionFactory();
1340: }
1341:
1342: } else if (specVersion.equals("1.5")) {
1343: // Need to figure out how to call RA.start() under some circumstances
1344: boolean callStart = false;
1345:
1346: String raStr = conn.getResourceadapterDesc()
1347: .getResourceadapterClass().trim();
1348: if (raStr != null && raStr.length() > 0) {
1349:
1350: // Instantiate the resource adapter class
1351: Class raClass = curLoader.loadClass(raStr);
1352: resAdp = (ResourceAdapter) raClass.newInstance();
1353:
1354: ru.processSetters(raClass, resAdp, rarObjectName,
1355: cfgRaJonas);
1356:
1357: // Call Resourceadapter start method
1358: try {
1359: if (callStart) {
1360: resAdp.start(bootCtx);
1361: }
1362: } catch (Exception ex) {
1363: logger
1364: .log(BasicLevel.ERROR,
1365: "Rar: Error from resource ("
1366: + rarObjectName
1367: + ") start method.");
1368: throw new Exception("Error from start method. "
1369: + ex);
1370: } catch (Throwable th) {
1371: logger
1372: .log(BasicLevel.ERROR,
1373: "Rar: Error from resource ("
1374: + rarObjectName
1375: + ") start method.");
1376: throw new Exception("Error from start method. ", th);
1377: }
1378: }
1379:
1380: // Determine type
1381: if (factoryType.equals(ru.JCD)) {
1382: OutboundResourceadapterDesc outRa = ra
1383: .getOutboundResourceadapterDesc();
1384: List cdList = null;
1385: if (outRa != null) {
1386: cdList = outRa.getConnectionDefinitionList();
1387: }
1388: ConnectionDefinitionDesc conDef = null;
1389: JonasConnectionDefinitionDesc jConDef = null;
1390: String id = null;
1391: if (cdList != null) {
1392: conDef = (ConnectionDefinitionDesc) cdList
1393: .get(factoryOffset);
1394: id = conDef.getId();
1395: jConDef = (JonasConnectionDefinitionDesc) ru
1396: .getJonasXML(jConn, id, factoryOffset,
1397: ru.JCD);
1398:
1399: if (jConDef.getLogEnabled() != null) {
1400: logEnabled = jConDef.getLogEnabled().trim();
1401: }
1402: if (jConDef.getLogTopic() != null) {
1403: logTopic = jConDef.getLogTopic().trim();
1404: }
1405:
1406: // Create Config Property List
1407: ConfigPropertyDesc[] cfgCdDesc = ru
1408: .buildConfigProperty(conDef
1409: .getConfigPropertyList(), jConDef
1410: .getJonasConfigPropertyList(), null);
1411:
1412: // Only need to build an MCF for a 1.0 resource adapter
1413: mcf = (ManagedConnectionFactory) ru.processMCF(
1414: conn, jConn, cm, curLoader, rarObjectName,
1415: conDef.getManagedconnectionfactoryClass(),
1416: jndiName, logEnabled, logTopic, cfgCdDesc,
1417: resAdp, factoryOffset);
1418: if (cm != null) {
1419: ConnectionManagerPoolParams pool = ru
1420: .configurePoolParams(jConDef
1421: .getPoolParamsDesc(), jConDef
1422: .getJdbcConnParamsDesc(), cmpp);
1423: ((ConnectionManagerImpl) cm)
1424: .setResourceAdapter(mcf, pool);
1425: factory = mcf.createConnectionFactory(cm);
1426: } else {
1427: factory = mcf.createConnectionFactory();
1428: }
1429:
1430: }
1431: } else if (factoryType.equals(ru.JAO)) {
1432: if (logger.isLoggable(BasicLevel.DEBUG)) {
1433: logger.log(BasicLevel.DEBUG,
1434: "Cannot lookup remote admin object of "
1435: + jndiName);
1436: }
1437: return null;
1438: } else if (factoryType.equals(ru.JAS)) {
1439: if (logger.isLoggable(BasicLevel.DEBUG)) {
1440: logger.log(BasicLevel.DEBUG,
1441: "Cannot lookup remote activationspec of "
1442: + jndiName);
1443: }
1444: return null;
1445: }
1446: }
1447:
1448: if (logger.isLoggable(BasicLevel.DEBUG)) {
1449: logger.log(BasicLevel.DEBUG, "cf = " + factory);
1450: }
1451: // Need to register the RM with JOTM
1452: if (factoryType.equals(ru.JCD) && cm != null) {
1453: // Call CM to register with JOTM
1454: ((ConnectionManagerImpl) cm).setXAName(ru
1455: .getJcaMcfName(jndiName));
1456: ((ConnectionManagerImpl) cm).registerXAResource(tmProp);
1457: }
1458: return factory;
1459: }
1460:
1461: /**
1462: * add a default AS to the list, the first one deployed is the default
1463: * if others are added then they will become default if this one is undeployed
1464: * @param jndiName jndi name to add
1465: */
1466: public static void addDefaultAS(String jndiName) {
1467: defaultAS.add(jndiName);
1468: }
1469:
1470: /**
1471: * return the current default ActivationSpec
1472: * @return String default ActivationSpec name
1473: */
1474: public static String getDefaultAS() {
1475: String ret = null;
1476: try {
1477: ret = (String) defaultAS.firstElement();
1478: } catch (Exception ex) {
1479: if (logger.isLoggable(BasicLevel.DEBUG)) {
1480: logger.log(BasicLevel.DEBUG,
1481: "No default activationspec");
1482: }
1483: }
1484: return ret;
1485: }
1486:
1487: /**
1488: * remove the specified jndiName from the default AS list
1489: * @param jndiName jndi name to remove
1490: */
1491: public static void removeDefaultAS(String jndiName) {
1492: try {
1493: defaultAS.remove(jndiName);
1494: } catch (Exception ex) {
1495: if (logger.isLoggable(BasicLevel.DEBUG)) {
1496: logger.log(BasicLevel.DEBUG,
1497: "Unable to remove default activationspec");
1498: }
1499: }
1500: }
1501:
1502: /**
1503: * get the RAR ConnectorDesc object matching the jndiname
1504: * @param jndiName the jndiname to lookup
1505: * @return ConnectorDesc matching ConnectorDesc
1506: */
1507: public static ConnectorDesc getConnectorDesc(String jndiName) {
1508: synchronized (jndiName2RA) {
1509: Rar rar = (Rar) jndiName2RA.get(jndiName);
1510: if (rar != null) {
1511: return (rar.getConnectorDesc());
1512: }
1513: if (logger.isLoggable(BasicLevel.DEBUG)) {
1514: logger.log(BasicLevel.DEBUG, "Can't find " + jndiName
1515: + " in " + jndiName2RA);
1516: }
1517: return null;
1518: }
1519: }
1520:
1521: /**
1522: * get the RAR JonasConnectorDesc object matching the jndiname
1523: * @param jndiName the jndiname to lookup
1524: * @return JonasConnectorDesc matching JonasConnectorDesc
1525: */
1526: public static JonasConnectorDesc getJonasConnectorDesc(
1527: String jndiName) {
1528: synchronized (jndiName2RA) {
1529: Rar rar = (Rar) jndiName2RA.get(jndiName);
1530: if (rar != null) {
1531: return (rar.getJonasConnectorDesc());
1532: }
1533: return null;
1534: }
1535: }
1536:
1537: /**
1538: * get the Rar matching the jndiNname
1539: * will be used in EJB container to deploy an MDB
1540: * @param jndiName jndi name to lookup
1541: * @return Rar Rar object for the specified jndi name
1542: */
1543: public static Rar getRar(String jndiName) {
1544: synchronized (jndiName2RA) {
1545: Rar ra = (Rar) jndiName2RA.get(jndiName);
1546: return (ra);
1547: }
1548: }
1549:
1550: /**
1551: * get the ResourceAdapter matching the jndiName
1552: * will be used in ResourceObjectJNDIHandler class at lookup time
1553: * @param jndiName to lookup ResourceObject
1554: * @return Object resourceObject corresponding to jndiName
1555: */
1556: public static Object getResourceObject(String jndiName) {
1557: Rar ra = null;
1558: synchronized (jndiName2RA) {
1559: ra = (Rar) jndiName2RA.get(jndiName);
1560: }
1561: if (ra != null) {
1562: return (ra.getFactory(jndiName));
1563: }
1564: Object fact = null;
1565: synchronized (jndiName2Factory) {
1566: fact = jndiName2Factory.get(jndiName);
1567: }
1568: return fact;
1569: }
1570:
1571: /* Private Methods
1572: *
1573: */
1574:
1575: /**
1576: *
1577: */
1578: private Properties buildProp(ConfigPropertyDesc[] cfgRaJonas) {
1579: Properties ret = new Properties();
1580: if (cfgRaJonas != null) {
1581: for (int i = 0; i < cfgRaJonas.length; i++) {
1582: String val = cfgRaJonas[i].getConfigPropertyValue() != null ? cfgRaJonas[i]
1583: .getConfigPropertyValue()
1584: : "";
1585: ret.setProperty(cfgRaJonas[i].getConfigPropertyName(),
1586: val);
1587: }
1588: }
1589: return ret;
1590: }
1591:
1592: /**
1593: * Process the specified rar, extracting all the jars files to be included
1594: * in the classloader and all the native files needed by the rar.
1595: * @param resFileName rar file to process
1596: * @param jonasConn the jonas-ra.xml properties
1597: * @throws ResourceServiceException if an error occurs during
1598: * the extracting.
1599: */
1600: private void extractJars(String resFileName,
1601: JonasConnectorDesc jonasConn)
1602: throws ResourceServiceException {
1603:
1604: String jarPath = ResourceServiceImpl.WORK_RARS_DIR
1605: + File.separator + "jonas" + File.separator;
1606: String binPath = null;
1607: if (jonasConn.getNativeLib() != null
1608: && jonasConn.getNativeLib().trim().length() > 0) {
1609: binPath = jonasConn.getNativeLib().trim() + File.separator;
1610: if (binPath.startsWith("/") || binPath.startsWith("\\")
1611: || binPath.charAt(1) == ':') {
1612: ;
1613: } else {
1614: binPath = ResourceServiceImpl.JONAS_BASE
1615: + File.separator + binPath;
1616: }
1617:
1618: }
1619:
1620: //Get RAR name
1621: String rarName = resFileName;
1622: if (rarName.endsWith(".rar")) {
1623: rarName = resFileName.substring(0, resFileName
1624: .lastIndexOf('.'));
1625: }
1626:
1627: int off = rarName.lastIndexOf(File.separator);
1628: rarName = rarName.substring(++off);
1629: if (File.separatorChar == '\\') {
1630: off = rarName.lastIndexOf("/");
1631: rarName = rarName.substring(++off);
1632: }
1633:
1634: JJarFile jjar = null;
1635:
1636: try {
1637: jjar = new JJarFile(resFileName);
1638: for (Enumeration ent = jjar.entries(); ent
1639: .hasMoreElements();) {
1640: JarEntry je = (JarEntry) ent.nextElement();
1641: if (!je.isDirectory() && je.getName().endsWith(".jar")) {
1642: File newd = new File(jarPath);
1643: if (!newd.exists()) {
1644: newd.mkdirs();
1645: }
1646:
1647: String fileName = jarPath + "_" + rarName + "_"
1648: + je.getName();
1649: jjar.extract(je, fileName);
1650: ((org.objectweb.jonas.server.JClassLoader) curLoader)
1651: .addURL((new File(fileName)).toURL());
1652: } else if (!je.isDirectory()
1653: && !je.getName().startsWith("META-INF")
1654: && binPath != null) {
1655: File newf = new File(binPath);
1656: if (!newf.exists()) {
1657: newf.mkdirs();
1658: }
1659: String fileName = binPath + je.getName();
1660: jjar.extract(je, fileName);
1661: }
1662: }
1663: } catch (Exception ex) {
1664: String err = "Error while extracting the files from "
1665: + resFileName;
1666: logger.log(BasicLevel.ERROR, err + ex.getMessage());
1667: throw new ResourceServiceException(err, ex);
1668: }
1669:
1670: }
1671:
1672: /**
1673: * get the ConfigObj object matching the jndiName
1674: *
1675: * @param jndiName String of jndi name
1676: * @return Object for the jndiName
1677: */
1678: private Object getConfigObject(String jndiName) {
1679: Rar ra = null;
1680: synchronized (jndiName2RA) {
1681: ra = (Rar) jndiName2RA.get(jndiName);
1682: }
1683: if (ra != null) {
1684: return (ra.getConfigObj(jndiName));
1685: }
1686: return null;
1687: }
1688:
1689: /**
1690: * get the ResourceAdapter Connector object matching the jndiName
1691: *
1692: * @param jndiName String of jndi name
1693: * @return String for the jndiName
1694: */
1695: private String getXmlContent(String jndiName) {
1696: synchronized (jndiName2RA) {
1697: Rar rar = (Rar) jndiName2RA.get(jndiName);
1698: if (rar != null) {
1699: return (rar.getXmlContent());
1700: }
1701: return null;
1702: }
1703: }
1704:
1705: private void processJAO(String rarFilename, AdminobjectDesc aObj,
1706: JonasAdminobjectDesc jao, ConfigPropertyDesc[] cfgRaJonas,
1707: int idOff) throws Exception {
1708:
1709: ConfigObj cObj = null;
1710: String jndiName = jao.getJndiName();
1711: String aoDesc = "";
1712: for (Iterator r = jao.getDescriptionList().iterator(); r
1713: .hasNext();) {
1714: String desc = (String) r.next();
1715: aoDesc += desc + " ";
1716: }
1717: if (jndiName == null || jndiName.length() == 0) {
1718: logger.log(BasicLevel.ERROR,
1719: "Rar: jndi-name not set in jonas-ra.xml for Adminobject: "
1720: + idOff);
1721: throw new Exception("configuration file incorrect");
1722: }
1723:
1724: // Instantiate into the currect loader
1725: String admObj = aObj.getAdminobjectClass();
1726: Class aoClass = curLoader.loadClass(admObj);
1727: Object ao = aoClass.newInstance();
1728:
1729: ru.processSetters(aoClass, ao, rarFileName, cfgRaJonas);
1730:
1731: jndinames.add(jndiName);
1732: if (logger.isLoggable(BasicLevel.DEBUG)) {
1733: logger.log(BasicLevel.DEBUG, "jndiName=" + jndiName);
1734: }
1735:
1736: // Add entry to list of configured objects
1737: cObj = new ConfigObj(JAO, idOff, jndiName, rarFileName, aObj
1738: .getAdminobjectInterface(), admObj, ao);
1739:
1740: cfgObjs.put(jndiName, cObj);
1741: //
1742: // Register into Jndi
1743: try {
1744: ictx.rebind(jndiName, ao);
1745:
1746: } catch (Exception e) {
1747: logger.log(BasicLevel.ERROR,
1748: "Rar: Cannot register ResourceAdapter with the name "
1749: + jndiName);
1750: logger
1751: .log(BasicLevel.ERROR, "Rar: Exception caught : "
1752: + e);
1753: throw new Exception("Error binding jndiName: " + jndiName,
1754: e.getCause());
1755: }
1756:
1757: // --------------------------
1758: // Register MBeans cf. JSR 77
1759: // --------------------------
1760: if (mbeanServer != null) {
1761:
1762: // Available Adminobjects
1763: // ------------------------------------------------------------
1764: // Adminobject MBean
1765: // -----------------------
1766: String jcaAdminobjectName = jndiName;
1767: Properties p = buildProp(cfgRaJonas);
1768: ObjectName onJCAAdminobject = J2eeObjectName
1769: .getJCAAdminObject(jDomain, JCAResourceName,
1770: jServer, jcaAdminobjectName);
1771: JCAAdminObject jcaAdminobjectMBean = new JCAAdminObject(
1772: onJCAAdminobject.toString(), jndiName, aoDesc, p);
1773: ManagedBean oManaged = oRegistry
1774: .findManagedBean("JCAAdminObject");
1775: ModelMBean oMBean = oManaged
1776: .createMBean(jcaAdminobjectMBean);
1777: if (manageLogger.isLoggable(BasicLevel.DEBUG)) {
1778: manageLogger.log(BasicLevel.DEBUG,
1779: "JCAAdminObject created");
1780: }
1781: mbeanServer.registerMBean(oMBean, onJCAAdminobject);
1782:
1783: // Update the list of connection factories in the JCAResource MBean with the JCAConnectionFactory
1784: // MBean's OBJECT_NAME
1785: jcaResourceMBean.setAdminObjects(onJCAAdminobject
1786: .toString());
1787:
1788: } // end JMX registration
1789:
1790: }
1791:
1792: private void processJAS(String rarFilename,
1793: MessagelistenerDesc mlst, JonasActivationspecDesc jas,
1794: int idOff) throws Exception {
1795:
1796: ConfigObj cObj = null;
1797: String jndiName = jas.getJndiName();
1798: String asDesc = "";
1799: for (Iterator r = jas.getDescriptionList().iterator(); r
1800: .hasNext();) {
1801: String desc = (String) r.next();
1802: asDesc += desc + " ";
1803: }
1804: if (jndiName == null || jndiName.length() == 0) {
1805: logger.log(BasicLevel.ERROR,
1806: "Rar: jndi-name not set in jonas-ra.xml for Activationspec: "
1807: + idOff);
1808: throw new Exception("configuration file incorrect");
1809: }
1810:
1811: // Instantiate into the currect loader
1812: String mlIntf = mlst.getMessagelistenerType();
1813: String actSpec = mlst.getActivationspecDesc()
1814: .getActivationspecClass();
1815: Class asClass = curLoader.loadClass(actSpec);
1816: ActivationSpec as = (ActivationSpec) asClass.newInstance();
1817:
1818: jndinames.add(jndiName);
1819: if (logger.isLoggable(BasicLevel.DEBUG)) {
1820: logger.log(BasicLevel.DEBUG, "jndiName=" + jndiName);
1821: }
1822:
1823: // Add entry to list of configured objects
1824: cObj = new ConfigObj(JAS, idOff, jndiName, rarFilename, mlIntf,
1825: actSpec, as);
1826: cObj.reqConfigProps = mlst.getActivationspecDesc()
1827: .getRequiredConfigPropertyList();
1828: if (jas.getDefaultAS() != null
1829: && jas.getDefaultAS().equals("true")) {
1830: cObj.defaultAS = true;
1831: addDefaultAS(jndiName);
1832: }
1833:
1834: cfgObjs.put(jndiName, cObj);
1835: //
1836: // Register into Jndi
1837: try {
1838: ictx.rebind(jndiName, as);
1839:
1840: } catch (Exception e) {
1841: logger.log(BasicLevel.ERROR,
1842: "Rar.processJAS: Cannot register ResourceAdapter with the name "
1843: + jndiName);
1844: logger.log(BasicLevel.ERROR,
1845: "Rar.processJAS: Exception caught : " + e);
1846: throw new Exception("Error binding jndiName: " + jndiName,
1847: e.getCause());
1848: }
1849:
1850: // --------------------------
1851: // Register MBeans cf. JSR 77
1852: // --------------------------
1853: if (mbeanServer != null) {
1854:
1855: // Available ActivationSpecs
1856: // ------------------------------------------------------------
1857: // ActivationSpec MBean
1858: // -----------------------
1859: String jcaActivationSpecName = jndiName;
1860: ObjectName onJCAActivationSpec = J2eeObjectName
1861: .getJCAActivationSpec(jDomain, JCAResourceName,
1862: jServer, jcaActivationSpecName);
1863: JCAActivationSpec jcaActivationSpecMBean = new JCAActivationSpec(
1864: onJCAActivationSpec.toString(), jndiName, asDesc,
1865: cObj.reqConfigProps);
1866: ManagedBean oManaged = oRegistry
1867: .findManagedBean("JCAActivationSpec");
1868: ModelMBean oMBean = oManaged
1869: .createMBean(jcaActivationSpecMBean);
1870: if (manageLogger.isLoggable(BasicLevel.DEBUG)) {
1871: manageLogger.log(BasicLevel.DEBUG,
1872: "JCAActivationSpec created");
1873: }
1874: mbeanServer.registerMBean(oMBean, onJCAActivationSpec);
1875:
1876: // Update the list of connection factories in the JCAResource MBean with the JCAConnectionFactory
1877: // MBean's OBJECT_NAME
1878: jcaResourceMBean.setActivationSpecs(onJCAActivationSpec
1879: .toString());
1880:
1881: } // end JMX registration
1882:
1883: }
1884:
1885: }
|