From 630c065d4ee98d134e48106ac5bd38b6c412db00 Mon Sep 17 00:00:00 2001 From: manoj Date: Mon, 8 Dec 2014 15:09:30 +0530 Subject: [PATCH] DAO Layer Implementation --- .../mgt/core/config/DeviceMgtConfig.java | 44 +++--------- .../core/config/datasource/DSXMLConfig.java | 28 ++++---- .../config/datasource/DataSourceConfig.java | 28 ++++---- .../datasource/JNDILookupDefinition.java | 34 ++++------ .../core/config/datasource/RDBMSConfig.java | 34 ++++------ .../carbon/device/mgt/core/dao/DeviceDAO.java | 2 +- .../device/mgt/core/dao/DeviceMgtDAO.java | 11 +-- .../mgt/core/dao/impl/DeviceMgtDAOImpl.java | 24 ++++--- .../internal/DeviceMgtServiceComponent.java | 68 +++++++++++++++++++ .../conf/datasources/master-datasources.xml | 6 +- 10 files changed, 154 insertions(+), 125 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceMgtConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceMgtConfig.java index 1625749c40d..0a10dfc8a93 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceMgtConfig.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceMgtConfig.java @@ -14,48 +14,26 @@ */ package org.wso2.carbon.device.mgt.core.config; -import org.wso2.carbon.rssmanager.core.environment.Environment; - import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; /** - * Represents RSS configuration. + * Represents Device Mgt configuration. */ -@XmlRootElement(name = "RSSConfiguration") +@XmlRootElement(name = "DeviceMgtConfiguration") public final class DeviceMgtConfig { - private String rssProvider; - private Environment[] environments; - private RSSManagementRepository rssMgtRepository; - - @XmlElement(name = "ManagementRepository", nillable = false) - public RSSManagementRepository getRSSManagementRepository() { - return rssMgtRepository; - } - - public void setRSSManagementRepository(RSSManagementRepository rssMgtRepository) { - this.rssMgtRepository = rssMgtRepository; - } - - @XmlElementWrapper(name = "Environments", nillable = false) - @XmlElement(name = "Environment", nillable = false) - public Environment[] getRSSEnvironments() { - return environments; - } + private String deviceMgtProvider; - public void setRSSEnvironments(Environment[] environments) { - this.environments = environments; - } + @XmlElement(name = "ManagementRepository", nillable = false) + public DeviceManagementRepository getDeviceMgtRepository() { + return deviceMgtRepository; + } - @XmlElement(name = "Provider", nillable = false) - public String getRSSProvider() { - return rssProvider; - } + public void setDeviceMgtRepository(DeviceManagementRepository deviceMgtRepository) { + this.deviceMgtRepository = deviceMgtRepository; + } - public void setRSSProvider(String rssProvider) { - this.rssProvider = rssProvider; - } + private DeviceManagementRepository deviceMgtRepository; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/DSXMLConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/DSXMLConfig.java index c8205aea608..61806962996 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/DSXMLConfig.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/DSXMLConfig.java @@ -1,20 +1,16 @@ /* - * Copyright (c) 2013, 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. - * + * 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.config.datasource; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/DataSourceConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/DataSourceConfig.java index 5525f9f8968..43da64f852f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/DataSourceConfig.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/DataSourceConfig.java @@ -1,20 +1,16 @@ /* - * Copyright (c) 2005-2013, 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. - * + * 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.config.datasource; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/JNDILookupDefinition.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/JNDILookupDefinition.java index 91b4d5228b4..2d0004f52cb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/JNDILookupDefinition.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/JNDILookupDefinition.java @@ -1,29 +1,21 @@ /* - * Copyright (c) 2005-2013, 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. - * + * 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.config.datasource; -import javax.xml.bind.annotation.XmlAttribute; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlElementWrapper; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlValue; +import javax.xml.bind.annotation.*; import java.util.List; /** diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/RDBMSConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/RDBMSConfig.java index 3f7bc080a37..c67c6640c98 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/RDBMSConfig.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/RDBMSConfig.java @@ -1,29 +1,21 @@ /* - * Copyright (c) 2013, 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. - * + * 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.config.datasource; import javax.xml.bind.Marshaller; -import javax.xml.bind.annotation.XmlAttribute; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlElementWrapper; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlValue; +import javax.xml.bind.annotation.*; import java.util.List; /** diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index b3dae2e43bf..d7de35a6baa 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -24,7 +24,7 @@ public class DeviceDAO { private DataSource dataSource; public DeviceDAO(DataSource dataSource) { - this.dataSource = ; + this.dataSource = dataSource; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceMgtDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceMgtDAO.java index d3369b61779..c2e50159de3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceMgtDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceMgtDAO.java @@ -18,6 +18,7 @@ package org.wso2.carbon.device.mgt.core.dao; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.core.dao.exception.DeviceDAOException; import org.wso2.carbon.device.mgt.core.dao.exception.DeviceDatabaseConnectionException; import org.wso2.carbon.device.mgt.core.dto.Device; @@ -27,14 +28,14 @@ import java.util.List; public interface DeviceMgtDAO { - void addDevice(Device device) throws DeviceDAOException, DeviceDatabaseConnectionException; + void addDevice(Device device) throws DeviceDAOException, DeviceManagementException; - void updateDevice(Device device) throws DeviceDAOException, DeviceDatabaseConnectionException; + void updateDevice(Device device) throws DeviceDAOException, DeviceManagementException; - void updateDeviceStatus(Long deviceId, Status status) throws DeviceDAOException, DeviceDatabaseConnectionException; + void updateDeviceStatus(Long deviceId, Status status) throws DeviceDAOException, DeviceManagementException; - void deleteDevice(Long deviceId) throws DeviceDAOException, DeviceDatabaseConnectionException; + void deleteDevice(Long deviceId) throws DeviceDAOException, DeviceManagementException; - List getDeviceByDeviceId(Long deviceId) throws DeviceDAOException, DeviceDatabaseConnectionException; + List getDeviceByDeviceId(Long deviceId) throws DeviceDAOException, DeviceManagementException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceMgtDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceMgtDAOImpl.java index 024c5515400..bf1cae06eb3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceMgtDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceMgtDAOImpl.java @@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.core.dao.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceMgtDAO; import org.wso2.carbon.device.mgt.core.dao.exception.DeviceDAOException; @@ -29,6 +30,7 @@ import org.wso2.carbon.device.mgt.core.dao.util.DeviceDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.ErrorHandler; import org.wso2.carbon.device.mgt.core.dto.Device; import org.wso2.carbon.device.mgt.core.dto.Status; +import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import javax.sql.DataSource; import java.sql.Connection; @@ -37,13 +39,17 @@ import java.sql.SQLException; import java.util.Date; import java.util.List; -public class DeviceMgtDAOImpl extends DeviceDAO implements DeviceMgtDAO { +public class DeviceMgtDAOImpl implements DeviceMgtDAO { private static final Log log = LogFactory.getLog(DeviceMgtDAOImpl.class); private DataSource dataSource; + public DeviceMgtDAOImpl() { + this.dataSource = DeviceManagerUtil.getDataSource(); + } + @Override - public void addDevice(Device device) throws DeviceDAOException, DeviceDatabaseConnectionException { + public void addDevice(Device device) throws DeviceDAOException, DeviceManagementException { Connection conn = null; PreparedStatement addDeviceDBStatement = null; @@ -78,33 +84,33 @@ public class DeviceMgtDAOImpl extends DeviceDAO implements DeviceMgtDAO { } @Override - public void updateDevice(Device device) throws DeviceDAOException, DeviceDatabaseConnectionException { + public void updateDevice(Device device) throws DeviceDAOException, DeviceManagementException { } @Override public void updateDeviceStatus(Long deviceId, Status status) - throws DeviceDAOException, DeviceDatabaseConnectionException { + throws DeviceDAOException, DeviceManagementException { } @Override - public void deleteDevice(Long deviceId) throws DeviceDAOException, DeviceDatabaseConnectionException { + public void deleteDevice(Long deviceId) throws DeviceDAOException, DeviceManagementException { } @Override public List getDeviceByDeviceId(Long deviceId) - throws DeviceDAOException, DeviceDatabaseConnectionException { + throws DeviceDAOException, DeviceManagementException { return null; } - private Connection getDataSourceConnection() throws DeviceDatabaseConnectionException { + private Connection getDataSourceConnection() throws DeviceManagementException { try { return dataSource.getConnection(); } catch (SQLException e) { - String msg = "Error while acquiring the database connection. Meta Repository Database server may down"; - throw new DeviceDatabaseConnectionException(msg, e); + String msg = "Error while acquiring the database connection"; + throw new DeviceManagementException(msg, e); } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceMgtServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceMgtServiceComponent.java index 557de2cf94b..52bd0d2b8ca 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceMgtServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceMgtServiceComponent.java @@ -17,7 +17,18 @@ package org.wso2.carbon.device.mgt.core.internal; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.osgi.framework.BundleContext; +import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService; +import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; +import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; +import org.wso2.carbon.device.mgt.core.util.DeviceMgtDbCreator; +import org.wso2.carbon.utils.CarbonUtils; + +import javax.naming.InitialContext; +import javax.transaction.TransactionManager; +import java.io.File; /** * @scr.component name="org.wso2.carbon.device.manager" immediate="true" @@ -28,6 +39,7 @@ import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService; public class DeviceMgtServiceComponent { private static Log log = LogFactory.getLog(DeviceMgtServiceComponent.class); + private final String deviceMgtSetupSql = CarbonUtils.getCarbonHome() + File.separator + "dbscripts" ; protected void setDeviceManagerService(DeviceManagerService deviceManager) { if (log.isDebugEnabled()) { @@ -41,5 +53,61 @@ public class DeviceMgtServiceComponent { } } + protected void activate(ComponentContext componentContext) { + BundleContext bundleContext = componentContext.getBundleContext(); + + + try { + + /* Looks up for the JNDI registered transaction manager */ + DeviceManagerDataHolder.getInstance().setTransactionManager(this.lookupTransactionManager()); + /* Initializing RSS Configuration */ + DeviceConfigurationManager.getInstance().initConfig(); + + String setupOption = System.getProperty("setup"); + //if -Dsetup option specified then create rss manager tables + if (setupOption != null) { + log.info("Setup option specified"); + DeviceMgtDbCreator dbCreator = new DeviceMgtDbCreator(DeviceManagerUtil.getDataSource()); + dbCreator.setRssDBScriptDirectory(deviceMgtSetupSql); + log.info("Creating Meta Data tables"); + dbCreator.createRegistryDatabase(); + } + + } catch (Throwable e) { + String msg = "Error occurred while initializing RSS Manager core bundle"; + log.error(msg, e); + } + } + + private TransactionManager lookupTransactionManager() { + TransactionManager transactionManager = null; + try { + Object txObj = InitialContext.doLookup( + DeviceManagementConstants.STANDARD_USER_TRANSACTION_JNDI_NAME); + if (txObj instanceof TransactionManager) { + transactionManager = (TransactionManager) txObj; + } + } catch (Exception e) { + if (log.isDebugEnabled()) { + log.debug("Cannot find transaction manager at: " + + DeviceManagementConstants.STANDARD_USER_TRANSACTION_JNDI_NAME, e); + } + /* ignore, move onto next step */ + } + if (transactionManager == null) { + try { + transactionManager = InitialContext.doLookup( + DeviceManagementConstants.STANDARD_TRANSACTION_MANAGER_JNDI_NAME); + } catch (Exception e) { + if (log.isDebugEnabled()) { + log.debug("Cannot find transaction manager at: " + + DeviceManagementConstants.STANDARD_TRANSACTION_MANAGER_JNDI_NAME, e); + } + /* we'll do the lookup later, maybe user provided a custom JNDI name */ + } + } + return transactionManager; + } } diff --git a/product/modules/distribution/src/repository/conf/datasources/master-datasources.xml b/product/modules/distribution/src/repository/conf/datasources/master-datasources.xml index b1535beb62a..a16e93a69cf 100755 --- a/product/modules/distribution/src/repository/conf/datasources/master-datasources.xml +++ b/product/modules/distribution/src/repository/conf/datasources/master-datasources.xml @@ -5,14 +5,14 @@ - WSO2_EMM_DB + WSO2DEVICE_DB The datasource used for EMM - jdbc/WSO2EMMDB + jdbc/WSO2DEVICE_DB - jdbc:h2:repository/database/WSO2EMM_DB;DB_CLOSE_ON_EXIT=FALSE + jdbc:h2:repository/database/WSO2DEVICE_DB;DB_CLOSE_ON_EXIT=FALSE wso2carbon wso2carbon org.h2.Driver