Hi,

I have attached the new diff file for the data source deployer feature. I
have added the password encryption support so the datasource xml files can
have encrypted passwords. Sample car file is also attached here with.
Please review and commit.

Thanks,


On Mon, Jul 28, 2014 at 11:48 AM, Rajith Vitharana <raji...@wso2.com> wrote:

> Hi Jasintha,Sohani,
>
> Thanks for the quick response. The "type" defined in "artifact.xml" should
> be "datasource/datasource" for it to be picked up by the datasource
> deployment handler and get deployed. Below is a sample
>
> <?xml version="1.0" encoding="UTF-8"?>
> <artifact name="DataSourceA" version="1.0.0" type="datasource/datasource"
> serverRole="ApplicationServer">
>     <file>DataSource.xml</file>
> </artifact>
>
> Thanks,
>
>
> On Mon, Jul 28, 2014 at 11:37 AM, Sohani Weerasinghe <soh...@wso2.com>
> wrote:
>
>> Hi,
>>
>> I am working on implementing the Datasource editor in DevS.
>>
>> Thanks,
>> Sohani
>>
>> Sohani Weerasinghe
>> Software Engineer
>> WSO2, Inc: http://wso2.com
>>
>> Mobile  : +94 716439774
>> Blog     :http://christinetechtips.blogspot.com/
>> Twitter  : https://twitter.com/sohanichristine
>>
>>
>> On Mon, Jul 28, 2014 at 11:04 AM, Jasintha Dasanayake <jasin...@wso2.com>
>> wrote:
>>
>>> Hi
>>>
>>> We can provide a single page form editor in DevS to edit this config
>>>
>>> Thanks and regards
>>> /Jasintha
>>>
>>>
>>>
>>> On Mon, Jul 28, 2014 at 10:36 AM, Rajith Vitharana <raji...@wso2.com>
>>> wrote:
>>>
>>>> Hi Kasun,
>>>>
>>>> Yes these datasources only provides a read-only view. and they won't be
>>>> saved in the registry. other than that they are same as the datasources
>>>> which we can create using UI.
>>>>
>>>> Hi Nirmal,
>>>>
>>>> noted. I'll try out encryption of passwords and to get DevS support
>>>>
>>>> Thanks,
>>>>
>>>>
>>>> On Mon, Jul 28, 2014 at 10:28 AM, Nirmal Fernando <nir...@wso2.com>
>>>> wrote:
>>>>
>>>>> Thanks Rajith. Seems like for this feature, we need DevS support (data
>>>>> source resource type) before deployed in a production environment. Also,
>>>>> this feature should ideally support encryption of passwords.
>>>>>
>>>>>
>>>>> On Mon, Jul 28, 2014 at 10:21 AM, Rajith Vitharana <raji...@wso2.com>
>>>>> wrote:
>>>>>
>>>>>> Hi Nirmal,
>>>>>>
>>>>>> This is a Appdeployment handler which will pickup datasources
>>>>>> deployed as carbon application and register them as datasources.
>>>>>> since datasource xml file is already in car app this feature wont
>>>>>> save it to the registry to avoid duplication.
>>>>>> datasources deployed as car apps can't be edited using UI. Editing
>>>>>> them should be done by redeploying the car app.
>>>>>>
>>>>>> Thanks,
>>>>>>
>>>>>>
>>>>>> On Mon, Jul 28, 2014 at 9:49 AM, Nirmal Fernando <nir...@wso2.com>
>>>>>> wrote:
>>>>>>
>>>>>>> Hi Rajith,
>>>>>>>
>>>>>>> Can you please also mention what new improvements this patch brings?
>>>>>>>
>>>>>>>
>>>>>>> On Mon, Jul 28, 2014 at 9:39 AM, Rajith Vitharana <raji...@wso2.com>
>>>>>>> wrote:
>>>>>>>
>>>>>>>> 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/
>>>>>>>>
>>>>>>>> _______________________________________________
>>>>>>>> Dev mailing list
>>>>>>>> Dev@wso2.org
>>>>>>>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>>
>>>>>>> Thanks & regards,
>>>>>>> Nirmal
>>>>>>>
>>>>>>> Senior Software Engineer- Platform Technologies Team, WSO2 Inc.
>>>>>>> Mobile: +94715779733
>>>>>>> Blog: http://nirmalfdo.blogspot.com/
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Rajith Vitharana
>>>>>>
>>>>>> Software Engineer,
>>>>>> WSO2 Inc. : wso2.com
>>>>>> Mobile : +94715883223
>>>>>> Blog : http://lankavitharana.blogspot.com/
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>>
>>>>> Thanks & regards,
>>>>> Nirmal
>>>>>
>>>>> Senior Software Engineer- Platform Technologies Team, WSO2 Inc.
>>>>> Mobile: +94715779733
>>>>> Blog: http://nirmalfdo.blogspot.com/
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>> Rajith Vitharana
>>>>
>>>> Software Engineer,
>>>> WSO2 Inc. : wso2.com
>>>> Mobile : +94715883223
>>>> Blog : http://lankavitharana.blogspot.com/
>>>>
>>>
>>>
>>>
>>> --
>>>
>>> *Jasintha Dasanayake *
>>>
>>> *Senior Software EngineerWSO2 Inc. | http://wso2.com <http://wso2.com/>
>>> lean . enterprise . middleware*
>>>
>>>
>>> *mobile :- 0711368118 <0711368118> *
>>>
>>> _______________________________________________
>>> Dev mailing list
>>> Dev@wso2.org
>>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>>
>>>
>>
>
>
> --
> Rajith Vitharana
>
> Software Engineer,
> WSO2 Inc. : wso2.com
> Mobile : +94715883223
> Blog : http://lankavitharana.blogspot.com/
>



-- 
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,170 @@
+/*
+*  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.w3c.dom.Document;
+import org.w3c.dom.Element;
+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 org.wso2.carbon.ndatasource.core.utils.DataSourceUtils;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Unmarshaller;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+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.getMessage(), 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 {
+            InputStream in = new FileInputStream(file);
+
+            Document doc = DataSourceUtils.convertToDocument(in);
+                /* only super tenant will lookup secure vault information for 
system data sources,
+                 * others are not allowed to */
+            DataSourceUtils.secureResolveDocument(doc, false);
+
+            // 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(doc);
+            return dataSourceMetaInfo;
+        } catch (JAXBException e) {
+            throw new 
DeploymentException("DataSourceDeployer::readDataSourceFile --> Error in 
reading data source file: " + e.getMessage(), e);
+        } catch (FileNotFoundException e) {
+            throw new 
DeploymentException("DataSourceDeployer::readDataSourceFile --> Error data 
source file not found: " + e.getMessage(), e);
+        } catch (DataSourceException e) {
+            throw new 
DeploymentException("DataSourceDeployer::readDataSourceFile --> Error in 
decrypting data source file: " + e.getMessage(), 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: encrypt.car
Description: Binary data

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

Reply via email to