From 0b4a6196e0ef3342280735ebd0271438256170e7 Mon Sep 17 00:00:00 2001 From: manoj Date: Mon, 8 Dec 2014 15:12:07 +0530 Subject: [PATCH] DAO Layer Implementation --- .../dao/exception/DeviceMgtDAOFactory.java | 85 ++++++++++++ .../mgt/core/util/DeviceManagerUtil.java | 125 ++++++++++++++++++ .../mgt/core/util/DeviceMgtDbCreator.java | 49 +++++++ .../main/resources/conf/deviceMgt-config.xml | 29 ++++ 4 files changed, 288 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/exception/DeviceMgtDAOFactory.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceMgtDbCreator.java create mode 100644 features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/deviceMgt-config.xml diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/exception/DeviceMgtDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/exception/DeviceMgtDAOFactory.java new file mode 100644 index 00000000000..94d518e8085 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/exception/DeviceMgtDAOFactory.java @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2014, 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. + * 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.device.mgt.core.dao.exception; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; +import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition; +import org.wso2.carbon.device.mgt.core.config.datasource.RDBMSConfig; +import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; + +import javax.sql.DataSource; +import java.util.Hashtable; +import java.util.List; + +/** + * DAO factory class for creating RSS DAO objects. + */ +public class DeviceMgtDAOFactory { + + private static final Log log = LogFactory.getLog(DeviceMgtDAOFactory.class); + + /** + * Resolve data source from the data source definition + * + * @param dataSourceDef data source configuration + * @return data source resolved from the data source definition + */ + public static DataSource resolveDataSource(DataSourceConfig dataSourceDef) { + DataSource dataSource; + if (dataSourceDef == null) { + throw new RuntimeException("RSS Management Repository data source configuration " + + "is null and thus, is not initialized"); + } + JNDILookupDefinition jndiConfig = dataSourceDef.getJndiLookupDefintion(); + if (jndiConfig != null) { + if (log.isDebugEnabled()) { + log.debug("Initializing RSS Management Repository data source using the JNDI " + + "Lookup Definition"); + } + List jndiPropertyList = + jndiConfig.getJndiProperties(); + if (jndiPropertyList != null) { + Hashtable jndiProperties = new Hashtable(); + for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) { + jndiProperties.put(prop.getName(), prop.getValue()); + } + dataSource = + DeviceManagerUtil.lookupDataSource(jndiConfig.getJndiName(), jndiProperties); + } else { + dataSource = DeviceManagerUtil.lookupDataSource(jndiConfig.getJndiName(), null); + } + } else { + if (log.isDebugEnabled()) { + log.debug("No JNDI Lookup Definition found in the RSS Management Repository " + + "data source configuration. Thus, continuing with in-line data source " + + "configuration processing."); + } + RDBMSConfig rdbmsConfig = dataSourceDef.getRdbmsConfiguration(); + if (rdbmsConfig == null) { + throw new RuntimeException( + "No JNDI/In-line data source configuration found. " + + "Thus, RSS Management Repository DAO is not initialized" + ); + } + dataSource = + DeviceManagerUtil.createDataSource(DeviceManagerUtil.loadDataSourceProperties( + rdbmsConfig), rdbmsConfig.getDataSourceClassName()); + } + return dataSource; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java new file mode 100644 index 00000000000..7c9376bab33 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2014, 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. + * 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.device.mgt.core.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.w3c.dom.Document; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.core.config.datasource.RDBMSConfig; +import org.wso2.carbon.ndatasource.common.DataSourceException; +import org.wso2.carbon.ndatasource.rdbms.RDBMSConfiguration; +import org.wso2.carbon.ndatasource.rdbms.RDBMSDataSource; +import org.wso2.securevault.SecretResolver; + +import javax.naming.InitialContext; +import javax.sql.DataSource; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.File; +import java.util.*; + +public class DeviceManagerUtil { + + private static final Log log = LogFactory.getLog(DeviceManagerUtil.class); + private static SecretResolver secretResolver; + private static String jndiDataSourceName; + private static DataSource dataSource; + private static final String DEFAULT_PRIVILEGE_TEMPLATE_NAME = "CRUD_PRIVILEGES_DEFAULT"; + + /** + * Construct document from file resource + * + * @param file object + * @return Document + * @throws org.wso2.carbon.device.mgt.common.DeviceManagementException if error occurred constructing document from file resource + */ + public static Document convertToDocument(File file) throws DeviceManagementException { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + try { + DocumentBuilder docBuilder = factory.newDocumentBuilder(); + return docBuilder.parse(file); + } catch (Exception e) { + throw new DeviceManagementException("Error occurred while parsing file, while converting " + + "to a org.w3c.dom.Document : " + e.getMessage(), e); + } + } + + public static DataSource lookupDataSource(String dataSourceName, final Hashtable jndiProperties) { + try { + if (jndiProperties == null || jndiProperties.isEmpty()) { + return (DataSource) InitialContext.doLookup(dataSourceName); + } + final InitialContext context = new InitialContext(jndiProperties); + return (DataSource) context.doLookup(dataSourceName); + } catch (Exception e) { + throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e); + } + } + + public static void setJndiDataSourceName(String jndiDataSourceName) { + DeviceManagerUtil.jndiDataSourceName = jndiDataSourceName; + } + + public static void setDataSource(DataSource dataSource) { + DeviceManagerUtil.dataSource = dataSource; + } + + public static DataSource getDataSource() { + return dataSource; + } + + public static Properties loadDataSourceProperties(RDBMSConfig config) { + Properties props = new Properties(); + List dsProps = config.getDataSourceProps(); + for (RDBMSConfig.DataSourceProperty dsProp : dsProps) { + props.setProperty(dsProp.getName(), dsProp.getValue()); + } + return props; + } + + /** + * Create data source from properties + * + * @param properties set of data source properties + * @param dataSourceClassName data source class name + * @return DataSource + */ + public static DataSource createDataSource(Properties properties, String dataSourceClassName) { + RDBMSConfiguration config = new RDBMSConfiguration(); + config.setDataSourceClassName(dataSourceClassName); + List dsProps = new ArrayList(); + for (Map.Entry entry : properties.entrySet()) { + RDBMSConfiguration.DataSourceProperty property = + new RDBMSConfiguration.DataSourceProperty(); + property.setName((String) entry.getKey()); + property.setValue((String) entry.getValue()); + dsProps.add(property); + } + config.setDataSourceProps(dsProps); + return createDataSource(config); + } + + public static DataSource createDataSource(RDBMSConfiguration config) { + try { + RDBMSDataSource dataSource = new RDBMSDataSource(config); + return dataSource.getDataSource(); + } catch (DataSourceException e) { + throw new RuntimeException("Error in creating data source: " + e.getMessage(), e); + } + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceMgtDbCreator.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceMgtDbCreator.java new file mode 100644 index 00000000000..b23adcb0583 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceMgtDbCreator.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2014, 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. + * 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.device.mgt.core.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.utils.dbcreator.DatabaseCreator; + +import javax.sql.DataSource; + +public class DeviceMgtDbCreator extends DatabaseCreator { + + private static final Log log = LogFactory.getLog(DeviceMgtDbCreator.class); + private String rssDBScriptDirectory;// stores the the location of the database script that is run according to the databse type + + public DeviceMgtDbCreator(DataSource dataSource) { + super(dataSource); + + } + + protected String getDbScriptLocation(String databaseType) { + String scriptName = "wso2_rss_" + databaseType + ".sql"; + if (log.isDebugEnabled()) { + log.debug("Loading database script from :" + scriptName); + } + return rssDBScriptDirectory.replaceFirst("DBTYPE", databaseType) + scriptName; + } + + /** + * Sets database scripts directory + * @param rssDBScriptDirectory database scripts location + */ + public void setRssDBScriptDirectory(String rssDBScriptDirectory) { + this.rssDBScriptDirectory = rssDBScriptDirectory; + } +} diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/deviceMgt-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/deviceMgt-config.xml new file mode 100644 index 00000000000..7cb6db7a274 --- /dev/null +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/deviceMgt-config.xml @@ -0,0 +1,29 @@ + + + + + + + + + WSO2DEVICE_DB + + + + + + +