Hi,

Attached here with the diff for datasource deployer feature for
org.wso2.carbon.ndatasource.core and a sample car file. please review and
commit

Thanks,

-- 
Rajith Vitharana

Software Engineer,
WSO2 Inc. : wso2.com
Mobile : +94715883223
Blog : http://lankavitharana.blogspot.com/
Index: pom.xml
===================================================================
--- pom.xml     (revision 204943)
+++ pom.xml     (working copy)
@@ -33,6 +33,11 @@
     <dependencies>
         <dependency>
             <groupId>org.wso2.carbon</groupId>
+            <artifactId>org.wso2.carbon.application.deployer</artifactId>
+            <version>${carbon.platform.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.wso2.carbon</groupId>
             <artifactId>org.wso2.carbon.utils</artifactId>
             <version>4.2.0</version>
         </dependency>
@@ -74,6 +79,7 @@
                         </Export-Package>
                         <DynamicImport-Package>*</DynamicImport-Package>
                         <DeployBefore>UserCore</DeployBefore>
+                        <WSO2-Application-Deployer>Data Source Capp 
Deployer</WSO2-Application-Deployer>
                     </instructions>
                 </configuration>
             </plugin>
Index: src/main/java/org/wso2/carbon/ndatasource/core/DataSourceMetaInfo.java
===================================================================
--- src/main/java/org/wso2/carbon/ndatasource/core/DataSourceMetaInfo.java      
(revision 204943)
+++ src/main/java/org/wso2/carbon/ndatasource/core/DataSourceMetaInfo.java      
(working copy)
@@ -43,6 +43,9 @@
        @XmlTransient
        private boolean system;
 
+    @XmlTransient
+    private boolean carbonApplicationDeployed;
+
        public void setName(String name) {
                this.name = name;
        }
@@ -59,6 +62,10 @@
                this.system = system;
        }
 
+    public void setCarbonApplicationDeployed(boolean 
carbonApplicationDeployed) {
+        this.carbonApplicationDeployed = carbonApplicationDeployed;
+    }
+
        @XmlElement (name = "name", required = true, nillable = false)
        public String getName() {
                return name;
@@ -79,6 +86,11 @@
                return system;
        }
 
+    @XmlTransient
+    public boolean isCarbonApplicationDeployed() {
+        return  carbonApplicationDeployed;
+    }
+
        @XmlElement (name = "definition", required = true, nillable = false)
        public DataSourceDefinition getDefinition() {
                return definition;
@@ -88,7 +100,12 @@
                this.definition = definition;
        }
 
-       @XmlRootElement (name = "definition")
+    @XmlTransient
+    public boolean isPersistable() {
+        return (!system && !carbonApplicationDeployed);
+    }
+
+    @XmlRootElement (name = "definition")
        public static class DataSourceDefinition {
                
                private String type;
@@ -112,7 +129,7 @@
                public void setDsXMLConfiguration(Object dsXMLConfiguration) {
                        this.dsXMLConfiguration = dsXMLConfiguration;
                }
-               
+
                public boolean equals(Object rhs) {
                        if (!(rhs instanceof DataSourceDefinition)) {
                                return false;
Index: src/main/java/org/wso2/carbon/ndatasource/core/DataSourceRepository.java
===================================================================
--- src/main/java/org/wso2/carbon/ndatasource/core/DataSourceRepository.java    
(revision 204943)
+++ src/main/java/org/wso2/carbon/ndatasource/core/DataSourceRepository.java    
(working copy)
@@ -467,11 +467,11 @@
                if (log.isDebugEnabled()) {
                        log.debug("Adding data source: " + dsmInfo.getName());
                }
-               if (!dsmInfo.isSystem()) {
+               if (dsmInfo.isPersistable()) {
                    this.persistDataSource(dsmInfo);
                }
                this.registerDataSource(dsmInfo);
-               if (!dsmInfo.isSystem()) {
+               if (dsmInfo.isPersistable()) {
                    this.notifyClusterDSChange(dsmInfo.getName());
                }
        }
@@ -491,10 +491,12 @@
                if (cds.getDSMInfo().isSystem()) {
                        throw new DataSourceException("System data sources 
cannot be deleted: " + dsName);
                }
-               this.removePersistedDataSource(dsName);
-               this.unregisterDataSource(dsName);
-               this.notifyClusterDSChange(dsName);
-       }
+        this.unregisterDataSource(dsName);
+        if (cds.getDSMInfo().isPersistable()) {
+            this.removePersistedDataSource(dsName);
+            this.notifyClusterDSChange(dsName);
+        }
+    }
        
        /**
         * Tests Connection of the data source
Index: 
src/main/java/org/wso2/carbon/ndatasource/core/deployer/DataSourceDeployer.java
===================================================================
--- 
src/main/java/org/wso2/carbon/ndatasource/core/deployer/DataSourceDeployer.java 
    (revision 0)
+++ 
src/main/java/org/wso2/carbon/ndatasource/core/deployer/DataSourceDeployer.java 
    (working copy)
@@ -0,0 +1,153 @@
+/*
+*  Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights 
Reserved.
+*
+*  WSO2 Inc. licenses this file to you under the Apache License,
+*  Version 2.0 (the "License"); you may not use this file except
+*  in compliance with the License.
+*  You may obtain a copy of the License at
+*
+*    http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing,
+* software distributed under the License is distributed on an
+* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+* KIND, either express or implied.  See the License for the
+* specific language governing permissions and limitations
+* under the License.
+*/
+package org.wso2.carbon.ndatasource.core.deployer;
+
+import org.apache.axis2.deployment.DeploymentException;
+import org.apache.axis2.engine.AxisConfiguration;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.wso2.carbon.application.deployer.CarbonApplication;
+import org.wso2.carbon.application.deployer.config.ApplicationConfiguration;
+import org.wso2.carbon.application.deployer.config.Artifact;
+import org.wso2.carbon.application.deployer.config.CappFile;
+import org.wso2.carbon.application.deployer.handler.AppDeploymentHandler;
+import org.wso2.carbon.ndatasource.common.DataSourceException;
+import org.wso2.carbon.ndatasource.core.DataSourceManager;
+import org.wso2.carbon.ndatasource.core.DataSourceMetaInfo;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Unmarshaller;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * This class is the implementation of the data source deployer which will 
deploy data sources to the server.
+ */
+public class DataSourceDeployer implements AppDeploymentHandler {
+
+    private static final Log log = LogFactory.getLog(DataSourceDeployer.class);
+
+    public static final String DATA_SOURCE_TYPE = "datasource/datasource";
+
+    /**
+     * Deploy the data source artifacts and add them to datasources.
+     *
+     * @param carbonApp  - store info in this object after deploying
+     * @param axisConfig - AxisConfiguration of the current tenant
+     */
+    @Override
+    public void deployArtifacts(CarbonApplication carbonApp, AxisConfiguration 
axisConfig)throws DeploymentException {
+        ApplicationConfiguration appConfig = carbonApp.getAppConfig();
+        List<Artifact.Dependency> deps = 
appConfig.getApplicationArtifact().getDependencies();
+
+        List<Artifact> artifacts = new ArrayList<Artifact>();
+        for (Artifact.Dependency dep : deps) {
+            if (dep.getArtifact() != null) {
+                artifacts.add(dep.getArtifact());
+            }
+        }
+        deployUnDeployDataSources(true, artifacts);
+    }
+
+    /**
+     * undeploy the data sources and remove them from datasources
+     *
+     * @param carbonApp  - all information about the existing artifacts are in 
this instance
+     * @param axisConfig - AxisConfiguration of the current tenant
+     */
+    @Override
+    public void undeployArtifacts(CarbonApplication carbonApp, 
AxisConfiguration axisConfig)throws DeploymentException {
+        ApplicationConfiguration appConfig = carbonApp.getAppConfig();
+        List<Artifact.Dependency> deps = 
appConfig.getApplicationArtifact().getDependencies();
+
+        List<Artifact> artifacts = new ArrayList<Artifact>();
+        for (Artifact.Dependency dep : deps) {
+            if (dep.getArtifact() != null) {
+                artifacts.add(dep.getArtifact());
+            }
+        }
+        deployUnDeployDataSources(false, artifacts);
+    }
+
+    /**
+     * deploy or undeploy data sources. if deploying, adding the data source 
to the data sources and if undeploying,
+     * removing the data source from data sources. there can be multiple data 
sources as separate xml files.
+     *
+     * @param deploy identify whether deployment process or undeployment 
process.
+     * @param artifacts     - list of artifacts to be deployed.
+     */
+    private void deployUnDeployDataSources(boolean deploy, List<Artifact> 
artifacts) throws DeploymentException {
+        for (Artifact artifact : artifacts) {
+            if (DATA_SOURCE_TYPE.equals(artifact.getType())) {
+                List<CappFile> files = artifact.getFiles();
+                if (files == null || files.isEmpty()) {
+                    throw new 
DeploymentException("DataSourceDeployer::deployUnDeployDataSources --> Error No 
data sources found in the artifact to deploy");
+                }
+                for (CappFile cappFile : files) {
+                    String fileName = cappFile.getName();
+                    String dataSourceConfigPath = artifact.getExtractedPath() +
+                            File.separator + fileName;
+
+                    File file = new File(dataSourceConfigPath);
+                    if (!file.exists()) {
+                        throw new 
DeploymentException("DataSourceDeployer::deployUnDeployDataSources --> Error 
Data source file cannot be found in artifact, file name - " + fileName);
+                    }
+                    DataSourceMetaInfo dataSourceMetaInfo = 
readDataSourceFile(file);
+                    if (deploy) {
+                        try {
+                            
dataSourceMetaInfo.setCarbonApplicationDeployed(true);
+                            
DataSourceManager.getInstance().getDataSourceRepository().addDataSource(dataSourceMetaInfo);
+                        } catch (DataSourceException e) {
+                            throw new 
DeploymentException("DataSourceDeployer::deployUnDeployDataSources --> Error in 
deploying data source: " + e.getMessage(), e);
+                        }
+                    } else {
+                        try {
+                            
DataSourceManager.getInstance().getDataSourceRepository().deleteDataSource(dataSourceMetaInfo.getName());
+                        } catch (DataSourceException e) {
+                            throw new 
DeploymentException("DataSourceDeployer::deployUnDeployDataSources --> Error in 
undeploying data source ", e);
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * method to read data source file and create the object from it.
+     *
+     * @param file - xml file
+     * @return dataSourceMetaInfo object which is created using the xml file.
+     * @throws org.apache.axis2.deployment.DeploymentException
+     */
+    private DataSourceMetaInfo readDataSourceFile(File file) throws 
DeploymentException {
+        try {
+            // create JAXB context and initializing Marshaller
+            JAXBContext jaxbContext = 
JAXBContext.newInstance(DataSourceMetaInfo.class);
+
+            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
+
+            // this will create Java object - data source from xml file
+            DataSourceMetaInfo dataSourceMetaInfo = (DataSourceMetaInfo) 
jaxbUnmarshaller.unmarshal(file);
+            return dataSourceMetaInfo;
+        } catch (JAXBException e) {
+            throw new 
DeploymentException("DataSourceDeployer::readDataSourceFile --> Error in 
reading data source file ", e);
+        }
+    }
+}
Index: 
src/main/java/org/wso2/carbon/ndatasource/core/internal/DataSourceServiceComponent.java
===================================================================
--- 
src/main/java/org/wso2/carbon/ndatasource/core/internal/DataSourceServiceComponent.java
     (revision 204943)
+++ 
src/main/java/org/wso2/carbon/ndatasource/core/internal/DataSourceServiceComponent.java
     (working copy)
@@ -15,10 +15,17 @@
  */
 package org.wso2.carbon.ndatasource.core.internal;
 
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.component.ComponentContext;
+import org.wso2.carbon.application.deployer.AppDeployerConstants;
+import org.wso2.carbon.application.deployer.AppDeployerUtils;
+import org.wso2.carbon.application.deployer.Feature;
+import org.wso2.carbon.application.deployer.handler.AppDeploymentHandler;
+import org.wso2.carbon.ndatasource.core.deployer.DataSourceDeployer;
 import org.wso2.carbon.base.MultitenantConstants;
 import org.wso2.carbon.base.api.ServerConfigurationService;
 import org.wso2.carbon.ndatasource.common.DataSourceException;
@@ -33,7 +40,11 @@
 import org.wso2.carbon.utils.CarbonUtils;
 import org.wso2.carbon.utils.ConfigurationContextService;
 
+import java.io.InputStream;
 import java.lang.reflect.InvocationTargetException;
+import java.net.URL;
+import java.util.List;
+import java.util.Map;
 
 /**
 * @scr.component name="org.wso2.carbon.ndatasource" immediate="true"
@@ -73,6 +84,11 @@
                
        private static ConfigurationContextService configContextService;
 
+
+    private static Map<String, List<Feature>> requiredFeatures;
+
+    private static ServiceRegistration appHandlerRegistration;
+
     private static Class<DataSourceRepository>  
carbonDataSourceRepositoryClass;
 
        protected synchronized void activate(ComponentContext ctx) {
@@ -80,6 +96,25 @@
                if (log.isDebugEnabled()) {
                        log.debug("DataSourceServiceComponent activated");
                }
+
+        try {
+            //register data source deployer as an OSGi service
+            DataSourceDeployer dataSourceDeployer = new DataSourceDeployer();
+            appHandlerRegistration = 
this.ctx.getBundleContext().registerService(
+                    AppDeploymentHandler.class.getName(), dataSourceDeployer, 
null);
+
+            // read required-features.xml
+            URL reqFeaturesResource = this.ctx.getBundleContext().getBundle()
+                    .getResource(AppDeployerConstants.REQ_FEATURES_XML);
+            if (reqFeaturesResource != null) {
+                InputStream xmlStream = reqFeaturesResource.openStream();
+                requiredFeatures = AppDeployerUtils
+                        .readRequiredFeaturs(new 
StAXOMBuilder(xmlStream).getDocumentElement());
+            }
+        } catch (Throwable e) {
+            log.error("Failed to activate Data Source Capp Deployer", e);
+        }
+
         /** Attempting to load the DatasourceRepository implementation class 
defined in carbon.xml
         .*If it is not there it will use default implementation
          */
Index: 
src/main/java/org/wso2/carbon/ndatasource/core/services/WSDataSourceMetaInfo.java
===================================================================
--- 
src/main/java/org/wso2/carbon/ndatasource/core/services/WSDataSourceMetaInfo.java
   (revision 204943)
+++ 
src/main/java/org/wso2/carbon/ndatasource/core/services/WSDataSourceMetaInfo.java
   (working copy)
@@ -43,7 +43,7 @@
                this.name = metaInfo.getName();
                this.description = metaInfo.getDescription();
                this.jndiConfig = metaInfo.getJndiConfig();
-               this.system = metaInfo.isSystem();
+               this.system = !metaInfo.isPersistable();
                this.definition = new 
WSDataSourceDefinition(metaInfo.getDefinition());
        }
        
Index: src/main/resources/META-INF/required-features.xml
===================================================================
--- src/main/resources/META-INF/required-features.xml   (revision 0)
+++ src/main/resources/META-INF/required-features.xml   (working copy)
@@ -0,0 +1,5 @@
+<featureSets>
+    <featureSet artifactType="datasource/datasource">
+        <feature id="org.wso2.carbon.ndatasource" version="[4.2.0, 4.3.0)"/>
+    </featureSet>
+</featureSets>
\ No newline at end of file

Attachment: sample4.car
Description: Binary data

_______________________________________________
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to