[jopr-commits] JOPR SVN: r1160 - in branches/JOPR-167/modules/plugins/jboss-as/src/main: java/org/rhq/plugins/jbossas/util and 1 other directories.

jopr-commits at lists.jboss.org jopr-commits at lists.jboss.org
Thu Sep 3 12:29:29 EDT 2009


Author: lkrejci
Date: 2009-09-03 12:29:28 -0400 (Thu, 03 Sep 2009)
New Revision: 1160

Modified:
   branches/JOPR-167/modules/plugins/jboss-as/src/main/java/org/rhq/plugins/jbossas/JBossASServerComponent.java
   branches/JOPR-167/modules/plugins/jboss-as/src/main/java/org/rhq/plugins/jbossas/util/DatasourceConfigurationEditor.java
   branches/JOPR-167/modules/plugins/jboss-as/src/main/resources/META-INF/rhq-plugin.xml
Log:
[JOPR-167] xa datasources should now be created properly.

Modified: branches/JOPR-167/modules/plugins/jboss-as/src/main/java/org/rhq/plugins/jbossas/JBossASServerComponent.java
===================================================================
--- branches/JOPR-167/modules/plugins/jboss-as/src/main/java/org/rhq/plugins/jbossas/JBossASServerComponent.java	2009-09-03 16:19:03 UTC (rev 1159)
+++ branches/JOPR-167/modules/plugins/jboss-as/src/main/java/org/rhq/plugins/jbossas/JBossASServerComponent.java	2009-09-03 16:29:28 UTC (rev 1160)
@@ -582,15 +582,16 @@
         }
         File deployDir = new File(getConfigurationPath(), "deploy");
         File dsFile = new File(deployDir, FileNameUtility.formatFileName(name) + "-ds.xml");
-        DatasourceConfigurationEditor.updateDatasource(dsFile, name, report);
-
-        deployFile(dsFile);
-
-        String objectName = String.format("jboss.jca:name=%s,service=DataSourceBinding", name);
-        // IMPORTANT: The object name must be canonicalized so it matches the resource key that
-        //            MBeanResourceDiscoveryComponent uses, which is the canonical object name.
-        report.setResourceKey(getCanonicalName(objectName));
-        setResourceName(report, name);
+        
+        if (DatasourceConfigurationEditor.updateDatasource(dsFile, name, report)) {
+            deployFile(dsFile);
+    
+            String objectName = String.format("jboss.jca:name=%s,service=DataSourceBinding", name);
+            // IMPORTANT: The object name must be canonicalized so it matches the resource key that
+            //            MBeanResourceDiscoveryComponent uses, which is the canonical object name.
+            report.setResourceKey(getCanonicalName(objectName));
+            setResourceName(report, name);
+        }
     }
 
     private void connectionFactoryCreate(CreateResourceReport report) throws MainDeployer.DeployerException {

Modified: branches/JOPR-167/modules/plugins/jboss-as/src/main/java/org/rhq/plugins/jbossas/util/DatasourceConfigurationEditor.java
===================================================================
--- branches/JOPR-167/modules/plugins/jboss-as/src/main/java/org/rhq/plugins/jbossas/util/DatasourceConfigurationEditor.java	2009-09-03 16:19:03 UTC (rev 1159)
+++ branches/JOPR-167/modules/plugins/jboss-as/src/main/java/org/rhq/plugins/jbossas/util/DatasourceConfigurationEditor.java	2009-09-03 16:29:28 UTC (rev 1160)
@@ -65,9 +65,14 @@
         "new-connection-sql", "exception-sorter-class-name", "check-valid-connection-sql", "track-statements",
         "no-tx-separate-pools", "application-managed-security", "security-domain-and-application" };
 
-    public static final String[] NON_XA_PROPS = { "driver-class" };
-
-    public static final String[] XA_PROPS = { "xa-datasource-class", "track-connection-by-tx",
+    private static final String DRIVER_CLASS_PROP = "driver-class";
+    private static final String XA_DATASOURCE_CLASS_PROP = "xa-datasource-class";
+    
+    public static final String[] NON_XA_PROPS = { DRIVER_CLASS_PROP };
+    
+    public static final Map<String, String> NON_XA_TO_XA_PROPS_MAP;
+    
+    public static final String[] XA_PROPS = { XA_DATASOURCE_CLASS_PROP, "track-connection-by-tx",
         "isSameRM-override-value" };
     private static final String[][] XA_SPECIALS = { { "connection-url", "URL" } , { "user-name", "User" }, {"password", "Password"}};
 
@@ -81,10 +86,15 @@
      */
 
     private static Log log = LogFactory.getLog(DatasourceConfigurationEditor.class);
-    private static final String XA_DATASOURCE_PROPERTIES = "xa-datasource-properties";
     private static final String CONNECTION_PROPERTY = "connection-property";
     private static final String XA_DATASOURCE_PROPERTY = "xa-datasource-property";
 
+    static {
+        NON_XA_TO_XA_PROPS_MAP = new HashMap<String, String>();
+        NON_XA_TO_XA_PROPS_MAP.put(DRIVER_CLASS_PROP, XA_DATASOURCE_CLASS_PROP);
+        NON_XA_TO_XA_PROPS_MAP.put(CONNECTION_PROPERTY, XA_DATASOURCE_PROPERTY);
+    }
+        
     public static Configuration loadDatasource(File file, String name) {
         /*
          *    <local-tx-datasource>       <jndi-name>RHQDS</jndi-name>
@@ -132,8 +142,9 @@
             bindElements(datasourceElement, config, COMMON_PROPS);
 
             if (type.equals(XA_TX_TYPE)) {
+                defineSharedNonXaProperties(datasourceElement, config);
                 bindElements(datasourceElement, config, XA_PROPS);
-                bindMap(datasourceElement, config, XA_DATASOURCE_PROPERTIES);
+                bindMap(datasourceElement, config, XA_DATASOURCE_PROPERTY);
                 bindXASpecialElements(datasourceElement,config);
             } else {
                 bindElements(datasourceElement, config, NON_XA_PROPS);
@@ -158,10 +169,13 @@
      * @param name
      * @param report
      */
-    public static void updateDatasource(File deploymentFile, String name, ConfigurationUpdateReport report) {
+    public static boolean updateDatasource(File deploymentFile, String name, ConfigurationUpdateReport report) {
         try {
-            updateDatasource(deploymentFile, name, report.getConfiguration());
-            report.setStatus(ConfigurationUpdateStatus.SUCCESS);
+            if (validateForSelectedType(report)) {
+                updateDatasource(deploymentFile, name, report.getConfiguration());
+                report.setStatus(ConfigurationUpdateStatus.SUCCESS);
+                return true;
+            }
         } catch (IOException e) {
             report.setErrorMessageFromThrowable(e);
             log.error("IO error occurred while updating datasource at file: " + deploymentFile, e);
@@ -169,6 +183,7 @@
             report.setErrorMessageFromThrowable(e);
             log.error("Parsing error occurred while updating datasource at file: " + deploymentFile, e);
         }
+        return false;
     }
 
     /**
@@ -177,10 +192,13 @@
      * @param name
      * @param report
      */
-    public static void updateDatasource(File deploymentFile, String name, CreateResourceReport report) {
+    public static boolean updateDatasource(File deploymentFile, String name, CreateResourceReport report) {
         try {
-            updateDatasource(deploymentFile, name, report.getResourceConfiguration());
-            report.setStatus(CreateResourceStatus.SUCCESS);
+            if (validateForSelectedType(report)) {
+                updateDatasource(deploymentFile, name, report.getResourceConfiguration());
+                report.setStatus(CreateResourceStatus.SUCCESS);
+                return true;
+            }
         } catch (IOException e) {
             report.setException(e);
             log.error("IO error occurred while updating datasource at file: " + deploymentFile, e);
@@ -188,6 +206,7 @@
             report.setException(e);
             log.error("Parsing error occurred while updating datasource at file: " + deploymentFile, e);
         }
+        return false;
     }
 
     /**
@@ -232,8 +251,9 @@
         updateElements(datasourceElement, config, COMMON_PROPS);
 
         if (type.equals(XA_TX_TYPE)) {
+            defineSharedXaProperties(config);
             updateElements(datasourceElement, config, XA_PROPS);
-            updateMap(datasourceElement, config, XA_DATASOURCE_PROPERTIES);
+            updateMap(datasourceElement, config, XA_DATASOURCE_PROPERTY);
             updateXAElements(datasourceElement, config);
         } else {
             updateElements(datasourceElement, config, NON_XA_PROPS);
@@ -458,4 +478,90 @@
         fos.flush();
         fos.close();
     }
+    
+    /**
+     * Copies the properties that are defined for nonXa DSes to the corresponding XA specific
+     * properties.
+     * 
+     * @param config
+     */
+    private static void defineSharedXaProperties(Configuration config) {
+        for (Map.Entry<String, String> entry : NON_XA_TO_XA_PROPS_MAP.entrySet()) {
+            Property nonXaProp = config.get(entry.getKey());
+            Property xaProp = nonXaProp.deepCopy();
+            xaProp.setName(entry.getValue());
+            config.put(xaProp);
+        }
+    } 
+
+    /**
+     * Copies the properties that are defined for nonXa DSes to the corresponding XA specific
+     * properties.
+     * 
+     * @param config
+     */
+    private static void defineSharedNonXaProperties(Element datasourceElement, Configuration config) {
+        for (Map.Entry<String, String> entry : NON_XA_TO_XA_PROPS_MAP.entrySet()) {
+            String xaPropName = entry.getValue();
+            String nonXaPropName = entry.getKey();
+            
+            @SuppressWarnings("unchecked")
+            List<Element> xaProps = datasourceElement.getChildren(xaPropName);
+            
+            if (xaProps.size() > 0) {
+                if (xaProps.size() == 1) {
+                    Element child = xaProps.get(0);
+                    config.put(new PropertySimple(nonXaPropName, child.getText()));
+                } else {
+                    PropertyMap map = new PropertyMap(nonXaPropName);
+                    for (Element child : xaProps) {
+                        String name = child.getAttributeValue("name");
+                        map.put(new PropertySimple(name, child.getText()));
+                    }
+                    config.put(map);
+                }
+            }
+        }
+    } 
+
+    private static boolean validateForSelectedType(CreateResourceReport report) {
+        String errorMessage = null;
+        if ((errorMessage = validateForSelectedType(report.getResourceConfiguration())) != null) {
+            report.setStatus(CreateResourceStatus.FAILURE);
+            report.setErrorMessage(errorMessage);
+        }
+        return errorMessage == null;
+    }
+
+    private static boolean validateForSelectedType(ConfigurationUpdateReport report) {
+        String errorMessage = null;
+        if ((errorMessage = validateForSelectedType(report.getConfiguration())) != null) {
+            report.setStatus(ConfigurationUpdateStatus.FAILURE);
+            report.setErrorMessage(errorMessage);
+        }
+        return errorMessage == null;
+    }
+    
+    private static String validateForSelectedType(Configuration conf) {
+        String dsType = conf.getSimpleValue("type", null);
+        
+        if (dsType == null || !(NO_TX_TYPE.equals(dsType) || LOCAL_TX_TYPE.equals(dsType) || 
+            XA_TX_TYPE.equals(dsType))) {
+            
+            return "Invalid Datasource type specified.";
+        }
+
+        //there is only one condition for non-xa DSes.
+        //Those must have the connection-url specified, whereas
+        //xa DSes might not have to.
+        if (!XA_TX_TYPE.equals(dsType)) {
+            String connUrl = conf.getSimpleValue("connection-url", null);
+            
+            if (connUrl == null || connUrl.trim().length() == 0) {
+                return "Non XA datasources must have the Connection URL specified.";
+            }
+        }
+        
+        return null;
+    }
 }
\ No newline at end of file

Modified: branches/JOPR-167/modules/plugins/jboss-as/src/main/resources/META-INF/rhq-plugin.xml
===================================================================
--- branches/JOPR-167/modules/plugins/jboss-as/src/main/resources/META-INF/rhq-plugin.xml	2009-09-03 16:19:03 UTC (rev 1159)
+++ branches/JOPR-167/modules/plugins/jboss-as/src/main/resources/META-INF/rhq-plugin.xml	2009-09-03 16:29:28 UTC (rev 1160)
@@ -443,10 +443,14 @@
                <c:simple-property name="jndi-name" readOnly="true" displayName="JNDI Name"
                                   description="The JNDI name under which the DataSource wrapper will be bound."/>
                <c:simple-property name="driver-class"
-                                  description="The fully qualified name of the JDBC driver or datasource class"/>
-               <!-- xa-datasource-class -->
-               <c:simple-property name="connection-url" displayName="Connection Url"
-                                  description="The JDBC driver connection URL string"/>
+                                  description="The fully qualified name of the JDBC driver or an XA datasource class"/>
+               <c:simple-property name="connection-url" displayName="Connection Url" required="false" default="jdbc:connection-url">
+                    <c:description>
+                        The JDBC driver connection URL string. 
+                        For Oracle XA datasource, this is automatically mapped to the "URL" XA datasource property.
+                        For other XA datasource types this might not have any impact. 
+                    </c:description>
+               </c:simple-property>
 
                <c:simple-property name="user-name" required="false"/>
                <c:simple-property name="password" type="password" required="false"/>
@@ -491,21 +495,26 @@
                <c:simple-property name="track-statements" type="boolean" required="false"
                                   description="Whether to check for unclosed statements when a connection is returned to the pool and result sets are closed when a statement is closed/returned to the prepared statement cache. The default is Yes, but no warnings."/>
 
-               <c:map-property name="connection-property" required="false"/>
-               <!-- xa-datasource-property -->
+               <c:map-property name="connection-property" required="false" description="Connection Properties of non-XA datasource or XA datasource properties of an XA datasource." />
 
-               <c:simple-property name="no-tx-separate-pools" required="false"
-                                  description="Whether to use separate pools for connections retrieved in a transaction and those retrieved outside a transaction. The default is Yes."/>
                <c:simple-property name="application-managed-security" required="false"/>
                <c:simple-property name="security-domain-and-application" required="false"/>
                <c:simple-property name="track-connection-by-tx" required="false"
                                   description="Whether the connection should be &quot;locked&quot; to the transaction, returning it to the pool at the end of the transaction. The default is No."/>
-               <!-- XA only -->
+            </c:group>
+
+            <c:group name="xaSpecific" displayName="XA specific">
+               <!-- Based on http://www.jboss.org/community/wiki/ConfigDataSources 
+                    The other xa specific props are mapped onto existing non-xa as follows:
+                    driver-class = xa-datasource-class
+                    connection-propery = xa-datasource-property 
+               -->
                <c:simple-property name="isSameRM-override-value" required="false"
                                   description="Allows one to unconditionally set whether the javax.transaction.xa.XAResource.isSameRM(XAResource) returns true or false. The default is not to override the return value."/>
-               <!-- XA only -->
+               <c:simple-property name="no-tx-separate-pools" required="false"
+                                  description="Whether to use separate pools for connections retrieved in a transaction and those retrieved outside a transaction. The default is Yes."/>
             </c:group>
-
+            
             <c:template name="Oracle Local TX" description="Local Transaction Template for Oracle">
                <c:simple-property name="type" default="local-tx-datasource"/>
                <c:simple-property name="driver-class" default="oracle.jdbc.driver.OracleDriver"/>
@@ -518,7 +527,8 @@
                <c:simple-property name="type" default="xa-datasource"/>
                <c:simple-property name="track-connection-by-tx" default="true"/>
                <c:simple-property name="isSameRM-override-value" default="false"/>
-               <c:simple-property name="xa-datasource-class" default="oracle.jdbc.xa.client.OracleXADataSource"/>
+               <c:simple-property name="driver-class" default="oracle.jdbc.xa.client.OracleXADataSource"/>
+               <c:simple-property name="connection-url" default="jdbc:oracle:oci8:@tc" />
                <!--xa-datasource-property name="URL">jdbc:oracle:oci8:@tc</xa-datasource-property>-->
                <!--xa-datasource-property name="User">scott</xa-datasource-property>-->
                <!--<xa-datasource-property name="Password">tiger</xa-datasource-property>-->



More information about the jopr-commits mailing list