[jboss-cvs] JBossAS SVN: r111493 - in projects/jboss-jca/trunk/rhq/src: main/java/org/jboss/jca/rhq/ds and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Jun 1 04:51:29 EDT 2011


Author: gaol
Date: 2011-06-01 04:51:29 -0400 (Wed, 01 Jun 2011)
New Revision: 111493

Modified:
   projects/jboss-jca/trunk/rhq/src/main/java/org/jboss/jca/rhq/core/IronJacamarResourceComponent.java
   projects/jboss-jca/trunk/rhq/src/main/java/org/jboss/jca/rhq/ds/DsResourceComponent.java
   projects/jboss-jca/trunk/rhq/src/test/java/org/jboss/jca/rhq/test/EmbeddedJCAContainerTestCase.java
Log:
[JBJCA-578] adds support to undeploy DataSource resources.

Modified: projects/jboss-jca/trunk/rhq/src/main/java/org/jboss/jca/rhq/core/IronJacamarResourceComponent.java
===================================================================
--- projects/jboss-jca/trunk/rhq/src/main/java/org/jboss/jca/rhq/core/IronJacamarResourceComponent.java	2011-06-01 06:51:42 UTC (rev 111492)
+++ projects/jboss-jca/trunk/rhq/src/main/java/org/jboss/jca/rhq/core/IronJacamarResourceComponent.java	2011-06-01 08:51:29 UTC (rev 111493)
@@ -21,11 +21,18 @@
  */
 package org.jboss.jca.rhq.core;
 
+import org.jboss.jca.core.api.management.DataSource;
+import org.jboss.jca.core.api.management.ManagementRepository;
 import org.jboss.jca.rhq.util.ContainerHelper;
 
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.OutputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 import org.rhq.core.domain.configuration.Configuration;
 import org.rhq.core.domain.content.PackageDetailsKey;
@@ -45,6 +52,11 @@
  */
 public class IronJacamarResourceComponent extends AbstractResourceComponent implements CreateChildResourceFacet
 {
+   
+   /**
+    * Maps contains DataSource URL and associated jndis mappings.
+    */
+   private Map<URL, List<String>> dsURLJndiMap = new HashMap<URL, List<String>>();
 
    /**
     * loadResourceConfiguration
@@ -73,14 +85,22 @@
       ContentContext contentContext = getResourceContext().getContentContext();
       ContentServices contentServices = contentContext.getContentServices();
       String tmpDir = getUploadedDir();
+      PackageDetailsKey pkgKey = pkgDetail.getKey();
+      String pkgTypeName = pkgKey.getPackageTypeName();
       String fileName = pkgDetail.getFileName();
-      if (!fileName.toLowerCase().endsWith(".rar") && !fileName.toLowerCase().endsWith("-ds.xml"))
+      if (pkgTypeName.equals("rar-file") && !fileName.toLowerCase().endsWith(".rar"))
       {
-         report.setErrorMessage(fileName + " is not a valid RAR or Datasource file.");
+         report.setErrorMessage(fileName + " is not a valid RAR file.");
          report.setStatus(CreateResourceStatus.FAILURE);
          return report;
       }
-      File outFile = new File(tmpDir, pkgDetail.getFileName()); // change to plugin configuration ??
+      else if (pkgTypeName.equals("ds-file") && !fileName.toLowerCase().endsWith("-ds.xml"))
+      {
+         report.setErrorMessage(fileName + " is not a valid DataSource file.");
+         report.setStatus(CreateResourceStatus.FAILURE);
+         return report;
+      }
+      File outFile = new File(tmpDir, fileName); // change to plugin configuration ??
       OutputStream output;
       try
       {
@@ -94,8 +114,22 @@
             return report;
          }
          Deploy deployer = (Deploy)ContainerHelper.getEmbeddedDiscover();
-         deployer.deploy(outFile.toURI().toURL());
-         
+         URL url = outFile.toURI().toURL();
+         if (pkgTypeName.equals("ds-file"))
+         {
+            List<String> dsJndiNamesBeforeDeploy = this.getAllDataSourceJndiNames();
+            deployer.deploy(url);
+            List<String> dsJndiNamesAfterDeploy = this.getAllDataSourceJndiNames();
+            List<String> moreJndiNames = moreDeployedDsJndis(dsJndiNamesBeforeDeploy, dsJndiNamesAfterDeploy);
+            if (!moreJndiNames.isEmpty())
+            {
+               this.dsURLJndiMap.put(url, moreJndiNames);
+            }
+         }
+         else
+         {
+            deployer.deploy(url);
+         }
          String resKey = outFile.getName();
          
          // set resource key
@@ -115,4 +149,69 @@
       }
       return report;
    }
+
+   /**
+    * Gets more DataSource jndis after a -ds.xml is deployed.
+    * 
+    * @param dsJndiNamesBeforeDeploy DataSource Jndis before the -ds.xml is deployed
+    * @param dsJndiNamesAfterDeploy  DataSource Jndis after the -ds.xml is deployed
+    * @return more DataSource Jndis.
+    */
+   private List<String> moreDeployedDsJndis(List<String> dsJndiNamesBeforeDeploy, List<String> dsJndiNamesAfterDeploy)
+   {
+      List<String> moreDsJndis = new ArrayList<String>();
+      for (String ds : dsJndiNamesAfterDeploy)
+      {
+         if (!dsJndiNamesBeforeDeploy.contains(ds))
+         {
+            moreDsJndis.add(ds);
+         }
+      }
+      return moreDsJndis;
+   }
+
+   /**
+    * Gets all DataSource JndiNames in current ManagementRepository.
+    * 
+    * @return all DataSource JndiNames
+    */
+   private List<String> getAllDataSourceJndiNames()
+   {
+      List<String> dsJndiNames = new ArrayList<String>();
+      ManagementRepository mr = ManagementRepositoryManager.getManagementRepository();
+      for (DataSource ds : mr.getDataSources())
+      {
+         dsJndiNames.add(ds.getJndiName());
+      }
+      return dsJndiNames;
+   }
+   
+   /**
+    * Removes the DataSource entry from the dsURLJndiMap after the DataSource is undeployed.
+    * 
+    * @param dsJndiName DataSource JndiName
+    * @return true if remove succeeds, false otherwise.
+    * @throws Throwable the exception
+    */
+   public boolean unDeployDataSource(String dsJndiName) throws Throwable
+   {
+      URL dsURL = null;
+      for (Map.Entry<URL, List<String>> dsEntry : this.dsURLJndiMap.entrySet())
+      {
+         if (dsEntry.getValue().contains(dsJndiName))
+         {
+            dsURL = dsEntry.getKey();
+            break;
+         }
+      }
+      if (dsURL != null)
+      {
+         Deploy deployer = (Deploy)ContainerHelper.getEmbeddedDiscover();
+         deployer.undeploy(dsURL);
+         this.dsURLJndiMap.remove(dsURL);
+         return true;
+      }
+      return false;
+   }
+   
 }

Modified: projects/jboss-jca/trunk/rhq/src/main/java/org/jboss/jca/rhq/ds/DsResourceComponent.java
===================================================================
--- projects/jboss-jca/trunk/rhq/src/main/java/org/jboss/jca/rhq/ds/DsResourceComponent.java	2011-06-01 06:51:42 UTC (rev 111492)
+++ projects/jboss-jca/trunk/rhq/src/main/java/org/jboss/jca/rhq/ds/DsResourceComponent.java	2011-06-01 08:51:29 UTC (rev 111493)
@@ -27,6 +27,7 @@
 import org.jboss.jca.core.api.management.ManagementRepository;
 import org.jboss.jca.core.spi.statistics.StatisticsPlugin;
 
+import org.jboss.jca.rhq.core.IronJacamarResourceComponent;
 import org.jboss.jca.rhq.core.ManagementRepositoryManager;
 import org.jboss.jca.rhq.core.PoolResourceComponent;
 
@@ -44,13 +45,14 @@
 import org.rhq.core.domain.measurement.MeasurementReport;
 import org.rhq.core.domain.measurement.MeasurementScheduleRequest;
 import org.rhq.core.pluginapi.configuration.ConfigurationUpdateReport;
+import org.rhq.core.pluginapi.inventory.DeleteResourceFacet;
 
 /**
  * Represent <b>XA Datasource</b> in JCA container
  * 
  * @author <a href="mailto:lgao at redhat.com">Lin Gao</a>
  */
-public class DsResourceComponent extends PoolResourceComponent
+public class DsResourceComponent extends PoolResourceComponent implements DeleteResourceFacet
 {
    
    /** log */
@@ -186,5 +188,30 @@
          }
       }
    }
+
+
+   /**
+    * {@inheritDoc}}
+    */
+   @Override
+   public void deleteResource() throws Exception
+   {
+      DataSource ds = getDataSource();
+      String jndiName = ds.getJndiName();
+      IronJacamarResourceComponent ironJacamarResCompo = 
+         (IronJacamarResourceComponent)this.getResourceContext().getParentResourceComponent();
+      try
+      {
+         if (!ironJacamarResCompo.unDeployDataSource(jndiName))
+         {
+            throw new IllegalStateException("Can not delete the DataSource with JndiName: " + jndiName);
+         }
+         logger.debug("Finished undeploy DataSource: " + jndiName);
+      }
+      catch (Throwable e)
+      {
+         throw new Exception(e);
+      }
+   }
    
 }

Modified: projects/jboss-jca/trunk/rhq/src/test/java/org/jboss/jca/rhq/test/EmbeddedJCAContainerTestCase.java
===================================================================
--- projects/jboss-jca/trunk/rhq/src/test/java/org/jboss/jca/rhq/test/EmbeddedJCAContainerTestCase.java	2011-06-01 06:51:42 UTC (rev 111492)
+++ projects/jboss-jca/trunk/rhq/src/test/java/org/jboss/jca/rhq/test/EmbeddedJCAContainerTestCase.java	2011-06-01 08:51:29 UTC (rev 111493)
@@ -95,6 +95,36 @@
       }
       
    }
+   
+   /**
+    * Tests UnDeploy DataSource
+    * 
+    * @throws Throwable the exception
+    */
+   @Test
+   public void testUnDeployDataSource() throws Throwable
+   {
+      URL jdbcURL = DsTestCase.class.getResource("/jdbc-local.rar");
+      embedded.deploy(jdbcURL);
+      
+      URL dsURL = DsTestCase.class.getResource("/h2-ds.xml");
+      embedded.deploy(dsURL);
+      
+      InitialContext context = new InitialContext();
+      assertNotNull(context.lookup("java:/H2DS"));
+      
+      embedded.undeploy(dsURL);
+      try
+      {
+         context.lookup("java:/H2DS");
+         fail("DataSource of: java:/H2DS should be unboundded.");
+      }
+      catch (Exception e)
+      {
+         assertEquals(javax.naming.NameNotFoundException.class, e.getClass());
+      }
+      embedded.undeploy(jdbcURL);
+   }
 
    /**
     * Lifecycle start, before the suite is executed



More information about the jboss-cvs-commits mailing list