DAO Layer Implementation

revert-dabc3590
manoj 10 years ago
parent f37df2e6b7
commit 630c065d4e

@ -14,48 +14,26 @@
*/ */
package org.wso2.carbon.device.mgt.core.config; 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.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
/** /**
* Represents RSS configuration. * Represents Device Mgt configuration.
*/ */
@XmlRootElement(name = "RSSConfiguration") @XmlRootElement(name = "DeviceMgtConfiguration")
public final class DeviceMgtConfig { public final class DeviceMgtConfig {
private String rssProvider; private String deviceMgtProvider;
private Environment[] environments;
private RSSManagementRepository rssMgtRepository;
@XmlElement(name = "ManagementRepository", nillable = false) @XmlElement(name = "ManagementRepository", nillable = false)
public RSSManagementRepository getRSSManagementRepository() { public DeviceManagementRepository getDeviceMgtRepository() {
return rssMgtRepository; return deviceMgtRepository;
}
public void setRSSManagementRepository(RSSManagementRepository rssMgtRepository) {
this.rssMgtRepository = rssMgtRepository;
}
@XmlElementWrapper(name = "Environments", nillable = false)
@XmlElement(name = "Environment", nillable = false)
public Environment[] getRSSEnvironments() {
return environments;
}
public void setRSSEnvironments(Environment[] environments) {
this.environments = environments;
} }
@XmlElement(name = "Provider", nillable = false) public void setDeviceMgtRepository(DeviceManagementRepository deviceMgtRepository) {
public String getRSSProvider() { this.deviceMgtRepository = deviceMgtRepository;
return rssProvider;
} }
public void setRSSProvider(String rssProvider) { private DeviceManagementRepository deviceMgtRepository;
this.rssProvider = rssProvider;
}
} }

@ -1,20 +1,16 @@
/* /*
* Copyright (c) 2013, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License, * WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except * Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. * in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * http://www.apache.org/licenses/LICENSE-2.
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, * Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an * software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the * KIND, either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*
*/ */
package org.wso2.carbon.device.mgt.core.config.datasource; package org.wso2.carbon.device.mgt.core.config.datasource;

@ -1,20 +1,16 @@
/* /*
* Copyright (c) 2005-2013, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License, * WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except * Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. * in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * http://www.apache.org/licenses/LICENSE-2.
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, * Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an * software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the * KIND, either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*
*/ */
package org.wso2.carbon.device.mgt.core.config.datasource; package org.wso2.carbon.device.mgt.core.config.datasource;

@ -1,29 +1,21 @@
/* /*
* Copyright (c) 2005-2013, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License, * WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except * Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. * in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * http://www.apache.org/licenses/LICENSE-2.
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, * Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an * software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the * KIND, either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*
*/ */
package org.wso2.carbon.device.mgt.core.config.datasource; package org.wso2.carbon.device.mgt.core.config.datasource;
import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.*;
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 java.util.List; import java.util.List;
/** /**

@ -1,29 +1,21 @@
/* /*
* Copyright (c) 2013, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License, * WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except * Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. * in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * http://www.apache.org/licenses/LICENSE-2.
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, * Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an * software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the * KIND, either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*
*/ */
package org.wso2.carbon.device.mgt.core.config.datasource; package org.wso2.carbon.device.mgt.core.config.datasource;
import javax.xml.bind.Marshaller; import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.*;
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 java.util.List; import java.util.List;
/** /**

@ -24,7 +24,7 @@ public class DeviceDAO {
private DataSource dataSource; private DataSource dataSource;
public DeviceDAO(DataSource dataSource) { public DeviceDAO(DataSource dataSource) {
this.dataSource = ; this.dataSource = dataSource;
} }
} }

@ -18,6 +18,7 @@
package org.wso2.carbon.device.mgt.core.dao; 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.DeviceDAOException;
import org.wso2.carbon.device.mgt.core.dao.exception.DeviceDatabaseConnectionException; import org.wso2.carbon.device.mgt.core.dao.exception.DeviceDatabaseConnectionException;
import org.wso2.carbon.device.mgt.core.dto.Device; import org.wso2.carbon.device.mgt.core.dto.Device;
@ -27,14 +28,14 @@ import java.util.List;
public interface DeviceMgtDAO { 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<Device> getDeviceByDeviceId(Long deviceId) throws DeviceDAOException, DeviceDatabaseConnectionException; List<Device> getDeviceByDeviceId(Long deviceId) throws DeviceDAOException, DeviceManagementException;
} }

@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.core.dao.impl;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; 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.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceMgtDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceMgtDAO;
import org.wso2.carbon.device.mgt.core.dao.exception.DeviceDAOException; 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.dao.util.ErrorHandler;
import org.wso2.carbon.device.mgt.core.dto.Device; 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.dto.Status;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.sql.Connection; import java.sql.Connection;
@ -37,13 +39,17 @@ import java.sql.SQLException;
import java.util.Date; import java.util.Date;
import java.util.List; 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 static final Log log = LogFactory.getLog(DeviceMgtDAOImpl.class);
private DataSource dataSource; private DataSource dataSource;
public DeviceMgtDAOImpl() {
this.dataSource = DeviceManagerUtil.getDataSource();
}
@Override @Override
public void addDevice(Device device) throws DeviceDAOException, DeviceDatabaseConnectionException { public void addDevice(Device device) throws DeviceDAOException, DeviceManagementException {
Connection conn = null; Connection conn = null;
PreparedStatement addDeviceDBStatement = null; PreparedStatement addDeviceDBStatement = null;
@ -78,33 +84,33 @@ public class DeviceMgtDAOImpl extends DeviceDAO implements DeviceMgtDAO {
} }
@Override @Override
public void updateDevice(Device device) throws DeviceDAOException, DeviceDatabaseConnectionException { public void updateDevice(Device device) throws DeviceDAOException, DeviceManagementException {
} }
@Override @Override
public void updateDeviceStatus(Long deviceId, Status status) public void updateDeviceStatus(Long deviceId, Status status)
throws DeviceDAOException, DeviceDatabaseConnectionException { throws DeviceDAOException, DeviceManagementException {
} }
@Override @Override
public void deleteDevice(Long deviceId) throws DeviceDAOException, DeviceDatabaseConnectionException { public void deleteDevice(Long deviceId) throws DeviceDAOException, DeviceManagementException {
} }
@Override @Override
public List<Device> getDeviceByDeviceId(Long deviceId) public List<Device> getDeviceByDeviceId(Long deviceId)
throws DeviceDAOException, DeviceDatabaseConnectionException { throws DeviceDAOException, DeviceManagementException {
return null; return null;
} }
private Connection getDataSourceConnection() throws DeviceDatabaseConnectionException { private Connection getDataSourceConnection() throws DeviceManagementException {
try { try {
return dataSource.getConnection(); return dataSource.getConnection();
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error while acquiring the database connection. Meta Repository Database server may down"; String msg = "Error while acquiring the database connection";
throw new DeviceDatabaseConnectionException(msg, e); throw new DeviceManagementException(msg, e);
} }
} }
} }

@ -17,7 +17,18 @@ package org.wso2.carbon.device.mgt.core.internal;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.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" * @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 { public class DeviceMgtServiceComponent {
private static Log log = LogFactory.getLog(DeviceMgtServiceComponent.class); private static Log log = LogFactory.getLog(DeviceMgtServiceComponent.class);
private final String deviceMgtSetupSql = CarbonUtils.getCarbonHome() + File.separator + "dbscripts" ;
protected void setDeviceManagerService(DeviceManagerService deviceManager) { protected void setDeviceManagerService(DeviceManagerService deviceManager) {
if (log.isDebugEnabled()) { 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;
}
} }

@ -5,14 +5,14 @@
<datasources> <datasources>
<datasource> <datasource>
<name>WSO2_EMM_DB</name> <name>WSO2DEVICE_DB</name>
<description>The datasource used for EMM</description> <description>The datasource used for EMM</description>
<jndiConfig> <jndiConfig>
<name>jdbc/WSO2EMMDB</name> <name>jdbc/WSO2DEVICE_DB</name>
</jndiConfig> </jndiConfig>
<definition type="RDBMS"> <definition type="RDBMS">
<configuration> <configuration>
<url>jdbc:h2:repository/database/WSO2EMM_DB;DB_CLOSE_ON_EXIT=FALSE</url> <url>jdbc:h2:repository/database/WSO2DEVICE_DB;DB_CLOSE_ON_EXIT=FALSE</url>
<username>wso2carbon</username> <username>wso2carbon</username>
<password>wso2carbon</password> <password>wso2carbon</password>
<driverClassName>org.h2.Driver</driverClassName> <driverClassName>org.h2.Driver</driverClassName>

Loading…
Cancel
Save