Added mobile device management service with db schema

revert-70aa11f8
harshanL 10 years ago
parent e4d6070bee
commit ad10426c11

@ -16,11 +16,17 @@
package org.wso2.carbon.device.mgt.mobile.impl.android; package org.wso2.carbon.device.mgt.mobile.impl.android;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
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.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService; import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService;
import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileDevice;
import org.wso2.carbon.device.mgt.mobile.impl.util.MobileDeviceManagementUtil;
import java.util.List; import java.util.List;
@ -29,6 +35,8 @@ import java.util.List;
*/ */
public class AndroidDeviceManagerService implements DeviceManagerService { public class AndroidDeviceManagerService implements DeviceManagerService {
private static final Log log = LogFactory.getLog(AndroidDeviceManagerService.class);
@Override @Override
public String getProviderType() { public String getProviderType() {
return DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID; return DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID;
@ -36,22 +44,64 @@ public class AndroidDeviceManagerService implements DeviceManagerService {
@Override @Override
public boolean enrollDevice(Device device) throws DeviceManagementException { public boolean enrollDevice(Device device) throws DeviceManagementException {
return true; boolean status = false;
MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device);
try {
status = MobileDeviceManagementDAOFactory.getMobileDeviceDAO().addDevice(mobileDevice);
} catch (MobileDeviceManagementDAOException e) {
String msg = "Error while enrolling the Android device : " +
device.getDeviceIdentifier();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
return status;
} }
@Override @Override
public boolean modifyEnrollment(Device device) throws DeviceManagementException { public boolean modifyEnrollment(Device device) throws DeviceManagementException {
return true; boolean status = false;
MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device);
try {
status = MobileDeviceManagementDAOFactory.getMobileDeviceDAO().updateDevice(mobileDevice);
} catch (MobileDeviceManagementDAOException e) {
String msg = "Error while updating the enrollment of the Android device : " +
device.getDeviceIdentifier();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
return status;
} }
@Override @Override
public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
return true; boolean status = false;
try {
status = MobileDeviceManagementDAOFactory.getMobileDeviceDAO().deleteDevice(deviceId.getId());
} catch (MobileDeviceManagementDAOException e) {
String msg = "Error while removing the Android device : " + deviceId.getId();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
return status;
} }
@Override @Override
public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException {
return true; boolean isEnrolled = false;
try {
MobileDevice mobileDevice =
MobileDeviceManagementDAOFactory.getMobileDeviceDAO().getDevice(
deviceId.getId());
if(mobileDevice!=null){
isEnrolled = true;
}
} catch (MobileDeviceManagementDAOException e) {
String msg = "Error while checking the enrollment status of Android device : " +
deviceId.getId();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
return isEnrolled;
} }
@Override @Override
@ -72,7 +122,17 @@ public class AndroidDeviceManagerService implements DeviceManagerService {
@Override @Override
public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
return null; Device device = null;
try {
MobileDevice mobileDevice = MobileDeviceManagementDAOFactory.getMobileDeviceDAO().
getDevice(deviceId.getId());
device = MobileDeviceManagementUtil.convertToDevice(mobileDevice);
} catch (MobileDeviceManagementDAOException e) {
String msg = "Error while fetching the Android device : " + deviceId.getId();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
return device;
} }
@Override @Override
@ -83,9 +143,15 @@ public class AndroidDeviceManagerService implements DeviceManagerService {
@Override @Override
public boolean updateDeviceInfo(Device device) throws DeviceManagementException { public boolean updateDeviceInfo(Device device) throws DeviceManagementException {
return true; boolean status = false;
MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device);
try {
status = MobileDeviceManagementDAOFactory.getMobileDeviceDAO().updateDevice(mobileDevice);
} catch (MobileDeviceManagementDAOException e) {
String msg = "Error while updating the Android device : " + device.getDeviceIdentifier();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
return status;
} }
//should implement equals and hashcode in all service bundles
} }

@ -18,7 +18,7 @@ package org.wso2.carbon.device.mgt.mobile.impl.config;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.mobile.impl.util.MobileDeviceManagerUtil; import org.wso2.carbon.device.mgt.mobile.impl.util.MobileDeviceManagementUtil;
import org.wso2.carbon.device.mgt.mobile.impl.config.datasource.MobileDataSourceConfig; import org.wso2.carbon.device.mgt.mobile.impl.config.datasource.MobileDataSourceConfig;
import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.CarbonUtils;
@ -53,7 +53,7 @@ public class MobileDeviceConfigurationManager {
public synchronized void initConfig() throws DeviceManagementException { public synchronized void initConfig() throws DeviceManagementException {
try { try {
File mobileDeviceMgtConfig = new File(mobileDeviceMgtConfigXMLPath); File mobileDeviceMgtConfig = new File(mobileDeviceMgtConfigXMLPath);
Document doc = MobileDeviceManagerUtil.convertToDocument(mobileDeviceMgtConfig); Document doc = MobileDeviceManagementUtil.convertToDocument(mobileDeviceMgtConfig);
JAXBContext mobileDeviceMgmtContext = JAXBContext mobileDeviceMgmtContext =
JAXBContext.newInstance(MobileDeviceManagementConfig.class); JAXBContext.newInstance(MobileDeviceManagementConfig.class);
Unmarshaller unmarshaller = mobileDeviceMgmtContext.createUnmarshaller(); Unmarshaller unmarshaller = mobileDeviceMgmtContext.createUnmarshaller();

@ -26,10 +26,10 @@ public interface MobileDeviceDAO {
MobileDevice getDevice(String deviceId) throws MobileDeviceManagementDAOException; MobileDevice getDevice(String deviceId) throws MobileDeviceManagementDAOException;
void addDevice(MobileDevice mobileDevice) throws MobileDeviceManagementDAOException; boolean addDevice(MobileDevice mobileDevice) throws MobileDeviceManagementDAOException;
void updateDevice(MobileDevice mobileDevice) throws MobileDeviceManagementDAOException; boolean updateDevice(MobileDevice mobileDevice) throws MobileDeviceManagementDAOException;
void deleteDevice(String deviceId) throws MobileDeviceManagementDAOException; boolean deleteDevice(String deviceId) throws MobileDeviceManagementDAOException;
} }

@ -1,57 +0,0 @@
/**
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed 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.device.mgt.mobile.impl.dao;
import org.wso2.carbon.device.mgt.mobile.impl.DataSourceListener;
import org.wso2.carbon.device.mgt.mobile.impl.dao.impl.MobileDeviceDAOImpl;
import org.wso2.carbon.device.mgt.mobile.impl.dao.impl.MobileDeviceModelDAOImpl;
import org.wso2.carbon.device.mgt.mobile.impl.dao.impl.MobileDeviceVendorDAOImpl;
import org.wso2.carbon.device.mgt.mobile.impl.dao.impl.MobileOSVersionDAOImpl;
import org.wso2.carbon.device.mgt.mobile.impl.dao.util.MobileDeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.mobile.impl.internal.MobileDeviceManagementBundleActivator;
import javax.sql.DataSource;
public class MobileDeviceDAOFactory implements DataSourceListener {
private static DataSource dataSource;
public MobileDeviceDAOFactory() {
MobileDeviceManagementBundleActivator.registerDataSourceListener(this);
}
@Override
public void notifyObserver() {
dataSource = MobileDeviceManagementDAOUtil.resolveDataSource();
}
public static MobileDeviceDAO getMobileDeviceDAO() {
return new MobileDeviceDAOImpl(dataSource);
}
public static MobileDeviceModelDAO getMobileDeviceModelDAO() {
return new MobileDeviceModelDAOImpl(dataSource);
}
public static MobileDeviceVendorDAO getMobileDeviceVendorDAO() {
return new MobileDeviceVendorDAOImpl(dataSource);
}
public static MobileOSVersionDAO getMobileOSVersionDAO() {
return new MobileOSVersionDAOImpl(dataSource);
}
}

@ -18,87 +18,59 @@ package org.wso2.carbon.device.mgt.mobile.impl.dao;
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.mobile.impl.DataSourceListener;
import org.wso2.carbon.device.mgt.mobile.impl.config.datasource.MobileDataSourceConfig; import org.wso2.carbon.device.mgt.mobile.impl.config.datasource.MobileDataSourceConfig;
import org.wso2.carbon.device.mgt.mobile.impl.config.datasource.JNDILookupDefinition;
import org.wso2.carbon.device.mgt.mobile.impl.dao.impl.MobileDeviceDAOImpl; import org.wso2.carbon.device.mgt.mobile.impl.dao.impl.MobileDeviceDAOImpl;
import org.wso2.carbon.device.mgt.mobile.impl.dao.impl.MobileDeviceModelDAOImpl;
import org.wso2.carbon.device.mgt.mobile.impl.dao.impl.MobileDeviceVendorDAOImpl;
import org.wso2.carbon.device.mgt.mobile.impl.dao.impl.MobileOSVersionDAOImpl;
import org.wso2.carbon.device.mgt.mobile.impl.dao.util.MobileDeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.mobile.impl.dao.util.MobileDeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.mobile.impl.internal.MobileDeviceManagementBundleActivator;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.util.Hashtable;
import java.util.List;
/** /**
* Factory class used to create MobileDeviceManagement related DAO objects. * Factory class used to create MobileDeviceManagement related DAO objects.
*/ */
public class MobileDeviceManagementDAOFactory { public class MobileDeviceManagementDAOFactory implements DataSourceListener {
private static DataSource dataSource; private static DataSource dataSource;
private static MobileDataSourceConfig mobileDataSourceConfig;
private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOFactory.class); private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOFactory.class);
public static MobileDeviceDAO getMobileDeviceDAO() { public MobileDeviceManagementDAOFactory() {
return new MobileDeviceDAOImpl(dataSource);
}
public static MobileDeviceModelDAO getMobileDeviceModelDAO() {
return new MobileDeviceModelDAOImpl(dataSource);
} }
public static MobileDeviceVendorDAO getMobileDeviceVendorDAO() { public void init(){
return new MobileDeviceVendorDAOImpl(dataSource); dataSource = MobileDeviceManagementDAOUtil.resolveDataSource(mobileDataSourceConfig);
if(dataSource!=null){
MobileDeviceManagementDAOUtil.createDataSource(dataSource);
}else{
MobileDeviceManagementBundleActivator.registerDataSourceListener(this);
}
} }
public static MobileOSVersionDAO getMobileOSVersionDAO() { public static MobileDeviceDAO getMobileDeviceDAO() {
return new MobileOSVersionDAOImpl(dataSource); return new MobileDeviceDAOImpl(dataSource);
} }
public static void init(MobileDataSourceConfig config) { public static MobileDataSourceConfig getMobileDeviceManagementConfig() {
dataSource = resolveDataSource(config); return mobileDataSourceConfig;
} }
public static void init(DataSource dtSource) { public static void setMobileDataSourceConfig(
dataSource = dtSource; MobileDataSourceConfig mobileDataSourceConfig) {
MobileDeviceManagementDAOFactory.mobileDataSourceConfig =
mobileDataSourceConfig;
} }
/** public static DataSource getDataSource() {
* Resolve data source from the data source definition
*
* @param config data source configuration
* @return data source resolved from the data source definition
*/
private static DataSource resolveDataSource(MobileDataSourceConfig config) {
DataSource dataSource = null;
if (config == null) {
throw new RuntimeException("Device Management Repository data source configuration " +
"is null and thus, is not initialized");
}
JNDILookupDefinition jndiConfig = config.getJndiLookupDefintion();
if (jndiConfig != null) {
if (log.isDebugEnabled()) {
log.debug("Initializing Device Management Repository data source using the JNDI " +
"Lookup Definition");
}
List<JNDILookupDefinition.JNDIProperty> jndiPropertyList =
jndiConfig.getJndiProperties();
if (jndiPropertyList != null) {
Hashtable<Object, Object> jndiProperties = new Hashtable<Object, Object>();
for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) {
jndiProperties.put(prop.getName(), prop.getValue());
}
dataSource =
MobileDeviceManagementDAOUtil
.lookupDataSource(jndiConfig.getJndiName(), jndiProperties);
} else {
dataSource = MobileDeviceManagementDAOUtil
.lookupDataSource(jndiConfig.getJndiName(), null);
}
}
return dataSource; return dataSource;
} }
public static DataSource getDataSource() { @Override
return dataSource; public void notifyObserver() {
dataSource = MobileDeviceManagementDAOUtil.resolveDataSource(mobileDataSourceConfig);
if(dataSource!=null){
MobileDeviceManagementDAOUtil.createDataSource(dataSource);
}
} }
} }

@ -1,34 +0,0 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed 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.device.mgt.mobile.impl.dao;
import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileDeviceModel;
/**
* This class represents the key operations associated with persisting mobile-device model related
* information.
*/
public interface MobileDeviceModelDAO {
MobileDeviceModel getDeviceModel(String modelId) throws MobileDeviceManagementDAOException;
void addDeviceModel(MobileDeviceModel deviceModel) throws MobileDeviceManagementDAOException;
void updateDeviceModel(MobileDeviceModel deviceModel) throws MobileDeviceManagementDAOException;
void deleteDeviceModel(String modelId) throws MobileDeviceManagementDAOException;
}

@ -1,35 +0,0 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed 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.device.mgt.mobile.impl.dao;
import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileDeviceVendor;
/**
* This class represents the key operations associated with persisting mobile-device vendor
* related information.
*/
public interface MobileDeviceVendorDAO {
MobileDeviceVendor getDeviceModel(String vendorId) throws MobileDeviceManagementDAOException;
void addDeviceVendor(MobileDeviceVendor deviceVendor) throws MobileDeviceManagementDAOException;
void updateDeviceVendor(MobileDeviceVendor deviceVendor)
throws MobileDeviceManagementDAOException;
void deleteDeviceVendor(String vendorId) throws MobileDeviceManagementDAOException;
}

@ -1,34 +0,0 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed 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.device.mgt.mobile.impl.dao;
import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileOSVersion;
/**
* This class represents the key operations associated with persisting mobile-device OS version
* related information.
*/
public interface MobileOSVersionDAO {
MobileOSVersion getMobileOSVersion(String versionId) throws MobileDeviceManagementDAOException;
void addMobileOSVersion(MobileOSVersion osVersion) throws MobileDeviceManagementDAOException;
void updateMobileOSVersion(MobileOSVersion osVersion) throws MobileDeviceManagementDAOException;
void deleteMobileOSVersion(String versionId) throws MobileDeviceManagementDAOException;
}

@ -20,9 +20,14 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceDAO; import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceDAO;
import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceManagementDAOException; import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.impl.dao.util.MobileDeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileDevice; import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileDevice;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/** /**
* Implementation of MobileDeviceDAO. * Implementation of MobileDeviceDAO.
@ -38,23 +43,139 @@ public class MobileDeviceDAOImpl implements MobileDeviceDAO {
@Override @Override
public MobileDevice getDevice(String deviceId) throws MobileDeviceManagementDAOException { public MobileDevice getDevice(String deviceId) throws MobileDeviceManagementDAOException {
return null; Connection conn = null;
PreparedStatement stmt = null;
MobileDevice mobileDevice = null;
try {
conn = this.getConnection();
String createDBQuery =
"SELECT * FROM MBL_DEVICE WHERE MOBILE_DEVICE_ID = ?";
stmt = conn.prepareStatement(createDBQuery);
stmt.setString(1, deviceId);
ResultSet resultSet = stmt.executeQuery();
while (resultSet.next()) {
mobileDevice.setMobileDeviceId(resultSet.getString(0));
mobileDevice.setRegId(resultSet.getString(1));
mobileDevice.setImei(resultSet.getString(2));
mobileDevice.setImsi(resultSet.getString(3));
mobileDevice.setOsVersion(resultSet.getString(4));
mobileDevice.setModel(resultSet.getString(5));
mobileDevice.setVendor(resultSet.getString(6));
break;
}
} catch (SQLException e) {
String msg = "Error occurred while fetching mobile device '" +
deviceId + "'";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null);
}
return mobileDevice;
} }
@Override @Override
public void addDevice(MobileDevice mobileDevice) public boolean addDevice(MobileDevice mobileDevice)
throws MobileDeviceManagementDAOException { throws MobileDeviceManagementDAOException {
boolean status = false;
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = this.getConnection();
String createDBQuery =
"INSERT INTO MBL_DEVICE(MOBILE_DEVICE_ID, REG_ID, IMEI, IMSI, OS_VERSION," +
"DEVICE_MODEL, VENDOR) VALUES (?, ?, ?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(createDBQuery);
stmt.setString(1, mobileDevice.getMobileDeviceId());
stmt.setString(2, mobileDevice.getRegId());
stmt.setString(3, mobileDevice.getImei());
stmt.setString(4, mobileDevice.getImsi());
stmt.setString(5, mobileDevice.getOsVersion());
stmt.setString(6, mobileDevice.getModel());
stmt.setString(7, mobileDevice.getVendor());
int rows = stmt.executeUpdate();
if(rows>0){
status = true;
}
} catch (SQLException e) {
String msg = "Error occurred while enrolling mobile device '" +
mobileDevice.getMobileDeviceId() + "'";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null);
}
return status;
} }
@Override @Override
public void updateDevice(MobileDevice mobileDevice) public boolean updateDevice(MobileDevice mobileDevice)
throws MobileDeviceManagementDAOException { throws MobileDeviceManagementDAOException {
boolean status = false;
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = this.getConnection();
String createDBQuery =
"UPDATE MBL_DEVICE SET REG_ID = ?, IMEI = ?, IMSI = ?, OS_VERSION = ?," +
"DEVICE_MODEL = ?, VENDOR = ? WHERE MOBILE_DEVICE_ID = ?";
stmt = conn.prepareStatement(createDBQuery);
stmt.setString(1, mobileDevice.getRegId());
stmt.setString(2, mobileDevice.getImei());
stmt.setString(3, mobileDevice.getImsi());
stmt.setString(4, mobileDevice.getOsVersion());
stmt.setString(5, mobileDevice.getModel());
stmt.setString(6, mobileDevice.getVendor());
stmt.setString(7, mobileDevice.getMobileDeviceId());
int rows = stmt.executeUpdate();
if(rows>0){
status = true;
}
} catch (SQLException e) {
String msg = "Error occurred while updating the mobile device '" +
mobileDevice.getMobileDeviceId() + "'";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null);
}
return status;
} }
@Override @Override
public void deleteDevice(String deviceId) throws MobileDeviceManagementDAOException { public boolean deleteDevice(String deviceId) throws MobileDeviceManagementDAOException {
boolean status = false;
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = this.getConnection();
String createDBQuery =
"DELETE FROM MBL_DEVICE WHERE MOBILE_DEVICE_ID = ?";
stmt = conn.prepareStatement(createDBQuery);
stmt.setString(1,deviceId);
int rows = stmt.executeUpdate();
if(rows>0){
status = true;
}
} catch (SQLException e) {
String msg = "Error occurred while deleting mobile device " + deviceId;
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null);
}
return status;
}
private Connection getConnection() throws MobileDeviceManagementDAOException {
try {
return dataSource.getConnection();
} catch (SQLException e) {
String msg = "Error occurred while obtaining a connection from the mobile device " +
"management metadata repository datasource";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
}
} }
} }

@ -1,62 +0,0 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed 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.device.mgt.mobile.impl.dao.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceModelDAO;
import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileDeviceModel;
import javax.sql.DataSource;
/**
* Implementation of MobileDeviceModel.
*/
public class MobileDeviceModelDAOImpl implements MobileDeviceModelDAO {
private DataSource dataSource;
private static final Log log = LogFactory.getLog(MobileDeviceModelDAOImpl.class);
public MobileDeviceModelDAOImpl(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public MobileDeviceModel getDeviceModel(String modelId)
throws MobileDeviceManagementDAOException {
return null;
}
@Override
public void addDeviceModel(MobileDeviceModel deviceModel)
throws MobileDeviceManagementDAOException {
}
@Override
public void updateDeviceModel(MobileDeviceModel deviceModel)
throws MobileDeviceManagementDAOException {
}
@Override
public void deleteDeviceModel(String modelId)
throws MobileDeviceManagementDAOException {
}
}

@ -1,62 +0,0 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed 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.device.mgt.mobile.impl.dao.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceVendorDAO;
import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileDeviceVendor;
import javax.sql.DataSource;
/**
* Implementation of MobileDeviceVendorDAO.
*/
public class MobileDeviceVendorDAOImpl implements MobileDeviceVendorDAO {
private DataSource dataSource;
private static final Log log = LogFactory.getLog(MobileDeviceVendorDAOImpl.class);
public MobileDeviceVendorDAOImpl(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public MobileDeviceVendor getDeviceModel(String vendorId)
throws MobileDeviceManagementDAOException {
return null;
}
@Override
public void addDeviceVendor(MobileDeviceVendor deviceVendor)
throws MobileDeviceManagementDAOException {
}
@Override
public void updateDeviceVendor(MobileDeviceVendor deviceVendor)
throws MobileDeviceManagementDAOException {
}
@Override
public void deleteDeviceVendor(String vendorId)
throws MobileDeviceManagementDAOException {
}
}

@ -1,58 +0,0 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed 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.device.mgt.mobile.impl.dao.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileOSVersionDAO;
import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileOSVersion;
import javax.sql.DataSource;
/**
* Implementation of MobileOSVersionDAO.
*/
public class MobileOSVersionDAOImpl implements MobileOSVersionDAO {
private DataSource dataSource;
private static final Log log = LogFactory.getLog(MobileOSVersionDAOImpl.class);
public MobileOSVersionDAOImpl(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override public MobileOSVersion getMobileOSVersion(String versionId)
throws MobileDeviceManagementDAOException {
return null;
}
@Override public void addMobileOSVersion(MobileOSVersion osVersion)
throws MobileDeviceManagementDAOException {
}
@Override public void updateMobileOSVersion(MobileOSVersion osVersion)
throws MobileDeviceManagementDAOException {
}
@Override public void deleteMobileOSVersion(String versionId)
throws MobileDeviceManagementDAOException {
}
}

@ -16,14 +16,12 @@
package org.wso2.carbon.device.mgt.mobile.impl.dao.util; package org.wso2.carbon.device.mgt.mobile.impl.dao.util;
import javax.sql.DataSource;
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.Device; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceManagementDAOException; import org.wso2.carbon.device.mgt.mobile.impl.config.datasource.JNDILookupDefinition;
import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileDevice; import org.wso2.carbon.device.mgt.mobile.impl.config.datasource.MobileDataSourceConfig;
import org.wso2.carbon.device.mgt.mobile.impl.util.MobileDeviceManagementSchemaInitializer;
import javax.naming.InitialContext; import javax.naming.InitialContext;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -32,18 +30,79 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.List;
/** /**
* Utility method required by MobileDeviceManagement DAO classes. * Utility method required by MobileDeviceManagement DAO classes.
*/ */
public class MobileDeviceManagementDAOUtil { public class MobileDeviceManagementDAOUtil {
public static DataSource resolveDataSource() {
return null;
}
private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOUtil.class); private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOUtil.class);
/**
* Resolve data source from the data source definition
*
* @param config Mobile data source configuration
* @return data source resolved from the data source definition
*/
public static DataSource resolveDataSource(MobileDataSourceConfig config) {
DataSource dataSource = null;
if (config == null) {
throw new RuntimeException("Device Management Repository data source configuration " +
"is null and thus, is not initialized");
}
JNDILookupDefinition jndiConfig = config.getJndiLookupDefintion();
if (jndiConfig != null) {
if (log.isDebugEnabled()) {
log.debug("Initializing Device Management Repository data source using the JNDI " +
"Lookup Definition");
}
List<JNDILookupDefinition.JNDIProperty> jndiPropertyList =
jndiConfig.getJndiProperties();
if (jndiPropertyList != null) {
Hashtable<Object, Object> jndiProperties = new Hashtable<Object, Object>();
for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) {
jndiProperties.put(prop.getName(), prop.getValue());
}
try {
dataSource = MobileDeviceManagementDAOUtil.lookupDataSource(
jndiConfig.getJndiName(), jndiProperties);
} catch (DeviceManagementException e) {
if (log.isDebugEnabled()) {
log.debug("Error in looking up data source: " + e.getMessage());
}
log.error(e);
}
} else {
try {
dataSource = MobileDeviceManagementDAOUtil.lookupDataSource(
jndiConfig.getJndiName(), null);
} catch (DeviceManagementException e) {
if (log.isDebugEnabled()) {
log.debug("Error in looking up data source: " + e.getMessage());
}
log.error(e);
}
}
}
return dataSource;
}
public static DataSource lookupDataSource(String dataSourceName,
final Hashtable<Object, Object> jndiProperties)
throws DeviceManagementException {
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 DeviceManagementException(
"Error in looking up data source: " + e.getMessage(), e);
}
}
public static void cleanupResources(Connection conn, PreparedStatement stmt, ResultSet rs) { public static void cleanupResources(Connection conn, PreparedStatement stmt, ResultSet rs) {
if (rs != null) { if (rs != null) {
try { try {
@ -68,23 +127,46 @@ public class MobileDeviceManagementDAOUtil {
} }
} }
public static DataSource lookupDataSource(String dataSourceName, /**
final Hashtable<Object, Object> jndiProperties) { * Initializes the creation of mobile device management schema if -Dsetup has provided
try { *
if (jndiProperties == null || jndiProperties.isEmpty()) { * @param dataSource Mobile data source
return (DataSource) InitialContext.doLookup(dataSourceName); */
public static void createDataSource(DataSource dataSource) {
String setupOption = System.getProperty("setup");
if (setupOption != null) {
if (log.isDebugEnabled()) {
log.debug(
"-Dsetup is enabled. Mobile Device management repository schema initialization is about " +
"to begin");
}
try {
MobileDeviceManagementDAOUtil.setupMobileDeviceManagementSchema(dataSource);
} catch (DeviceManagementException e) {
log.error(
"Exception occurred while initializing mobile device management database schema",
e);
} }
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 MobileDevice convertToMobileDevice(Device device) /**
throws MobileDeviceManagementDAOException { * Creates the mobile device management schema
MobileDevice mobileDeviceBO = new MobileDevice(); *
return mobileDeviceBO; * @param dataSource Mobile data source
*/
public static void setupMobileDeviceManagementSchema(DataSource dataSource) throws
DeviceManagementException {
MobileDeviceManagementSchemaInitializer initializer =
new MobileDeviceManagementSchemaInitializer(dataSource);
log.info("Initializing mobile device management repository database schema");
try {
initializer.createRegistryDatabase();
} catch (Exception e) {
throw new DeviceManagementException(
"Error occurred while initializing Mobile Device Management " +
"database schema", e);
}
} }
} }

@ -27,9 +27,9 @@ public class MobileDevice implements Serializable {
private String regId; private String regId;
private String imei; private String imei;
private String imsi; private String imsi;
private int osVersionId; private String osVersion;
private int modelId; private String model;
private int vendorId; private String vendor;
public String getMobileDeviceId() { public String getMobileDeviceId() {
return mobileDeviceId; return mobileDeviceId;
@ -63,27 +63,27 @@ public class MobileDevice implements Serializable {
this.imsi = imsi; this.imsi = imsi;
} }
public int getOsVersionId() { public String getOsVersion() {
return osVersionId; return osVersion;
} }
public void setOsVersionId(int osVersionId) { public void setOsVersion(String osVersion) {
this.osVersionId = osVersionId; this.osVersion = osVersion;
} }
public int getModelId() { public String getModel() {
return modelId; return model;
} }
public void setModelId(int modelId) { public void setModel(String model) {
this.modelId = modelId; this.model = model;
} }
public int getVendorId() { public String getVendor() {
return vendorId; return vendor;
} }
public void setVendorId(int vendorId) { public void setVendor(String vendor) {
this.vendorId = vendorId; this.vendor = vendor;
} }
} }

@ -1,44 +0,0 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed 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.device.mgt.mobile.impl.dto;
import java.io.Serializable;
/**
* DTO of MobileDeviceModel.
*/
public class MobileDeviceModel implements Serializable {
private int modelId;
private String model;
public int getModelId() {
return modelId;
}
public void setModelId(int modelId) {
this.modelId = modelId;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
}

@ -1,44 +0,0 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed 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.device.mgt.mobile.impl.dto;
import java.io.Serializable;
/**
* DTO of MobileVendor.
*/
public class MobileDeviceVendor implements Serializable {
private int vendorId;
private String vendor;
public int getVendorId() {
return vendorId;
}
public void setVendorId(int vendorId) {
this.vendorId = vendorId;
}
public String getVendor() {
return vendor;
}
public void setVendor(String vendor) {
this.vendor = vendor;
}
}

@ -1,44 +0,0 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed 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.device.mgt.mobile.impl.dto;
import java.io.Serializable;
/**
* DTO of MobileOSVersion.
*/
public class MobileOSVersion implements Serializable {
private int osVersionId;
private String osVersion;
public int getOsVersionId() {
return osVersionId;
}
public void setOsVersionId(int osVersionId) {
this.osVersionId = osVersionId;
}
public String getOsVersion() {
return osVersion;
}
public void setOsVersion(String osVersion) {
this.osVersion = osVersion;
}
}

@ -19,14 +19,14 @@ package org.wso2.carbon.device.mgt.mobile.impl.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.*; import org.osgi.framework.*;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService; import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService;
import org.wso2.carbon.device.mgt.mobile.impl.DataSourceListener; import org.wso2.carbon.device.mgt.mobile.impl.DataSourceListener;
import org.wso2.carbon.device.mgt.mobile.impl.android.AndroidDeviceManagerService; import org.wso2.carbon.device.mgt.mobile.impl.android.AndroidDeviceManagerService;
import org.wso2.carbon.device.mgt.mobile.impl.config.MobileDeviceConfigurationManager;
import org.wso2.carbon.device.mgt.mobile.impl.config.MobileDeviceManagementConfig;
import org.wso2.carbon.device.mgt.mobile.impl.config.datasource.MobileDataSourceConfig; import org.wso2.carbon.device.mgt.mobile.impl.config.datasource.MobileDataSourceConfig;
import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceDAOFactory; import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.mobile.impl.ios.IOSDeviceManagerService; import org.wso2.carbon.device.mgt.mobile.impl.ios.IOSDeviceManagerService;
import org.wso2.carbon.device.mgt.mobile.impl.util.MobileDeviceManagementSchemaInitializer;
import org.wso2.carbon.device.mgt.mobile.impl.windows.WindowsDeviceManagerService; import org.wso2.carbon.device.mgt.mobile.impl.windows.WindowsDeviceManagerService;
import java.util.ArrayList; import java.util.ArrayList;
@ -34,97 +34,113 @@ import java.util.List;
public class MobileDeviceManagementBundleActivator implements BundleActivator, BundleListener { public class MobileDeviceManagementBundleActivator implements BundleActivator, BundleListener {
private ServiceRegistration androidServiceRegRef; private ServiceRegistration androidServiceRegRef;
private ServiceRegistration iOSServiceRegRef; private ServiceRegistration iOSServiceRegRef;
private ServiceRegistration windowsServiceRegRef; private ServiceRegistration windowsServiceRegRef;
private static List<DataSourceListener> dataSourceListeners = new ArrayList<DataSourceListener>(); private static List<DataSourceListener> dataSourceListeners =
new ArrayList<DataSourceListener>();
private static final Log log = LogFactory.getLog(MobileDeviceManagementBundleActivator.class);
private static final String SYMBOLIC_NAME_DATA_SOURCE_COMPONENT = "org.wso2.carbon.ndatasource.core"; private static final Log log = LogFactory.getLog(MobileDeviceManagementBundleActivator.class);
private static final String SYMBOLIC_NAME_DATA_SOURCE_COMPONENT =
@Override "org.eclipse.osgi";
public void start(BundleContext bundleContext) throws Exception {
try { @Override
if (log.isDebugEnabled()) { public void start(BundleContext bundleContext) throws Exception {
log.debug("Activating Mobile Device Management Service bundle"); try {
} if (log.isDebugEnabled()) {
bundleContext.addBundleListener(this); log.debug("Activating Mobile Device Management Service bundle");
}
/* If -Dsetup option enabled then create device management database schema */ bundleContext.addBundleListener(this);
String setupOption = System.getProperty("setup"); /* Initialize the datasource configuration */
if (setupOption != null) { MobileDeviceConfigurationManager.getInstance().initConfig();
if (log.isDebugEnabled()) { MobileDeviceManagementConfig config = MobileDeviceConfigurationManager.getInstance()
log.debug( .getMobileDeviceManagementConfig();
"-Dsetup is enabled. Mobile Device management repository schema initialization is about " + MobileDataSourceConfig dsConfig =
"to begin"); config.getMobileDeviceMgtRepository().getMobileDataSourceConfig();
}
setupMobileDeviceManagementSchema(null); MobileDeviceManagementDAOFactory daoFactory = new MobileDeviceManagementDAOFactory();
} daoFactory.setMobileDataSourceConfig(dsConfig);
daoFactory.init();
MobileDeviceDAOFactory daoFactory = new MobileDeviceDAOFactory();
//TODO Register this dao to an appropriate config file androidServiceRegRef =
bundleContext.registerService(DeviceManagerService.class.getName(),
androidServiceRegRef = new AndroidDeviceManagerService(), null);
bundleContext.registerService(DeviceManagerService.class.getName(), iOSServiceRegRef =
new AndroidDeviceManagerService(), null); bundleContext.registerService(DeviceManagerService.class.getName(),
iOSServiceRegRef = new IOSDeviceManagerService(), null);
bundleContext.registerService(DeviceManagerService.class.getName(), windowsServiceRegRef =
new IOSDeviceManagerService(), null); bundleContext.registerService(DeviceManagerService.class.getName(),
windowsServiceRegRef = new WindowsDeviceManagerService(), null);
bundleContext.registerService(DeviceManagerService.class.getName(), if (log.isDebugEnabled()) {
new WindowsDeviceManagerService(), null); log.debug("Mobile Device Management Service bundle is activated");
if (log.isDebugEnabled()) { }
log.debug("Mobile Device Management Service bundle is activated"); } catch (Throwable e) {
} log.error("Error occurred while activating Mobile Device Management Service Component",
} catch (Throwable e) { e);
log.error("Error occurred while activating Mobile Device Management Service Component", e); }
} }
}
@Override
@Override public void stop(BundleContext bundleContext) throws Exception {
public void stop(BundleContext bundleContext) throws Exception { if (log.isDebugEnabled()) {
if (log.isDebugEnabled()) { log.debug("Deactivating Mobile Device Management Service");
log.debug("Deactivating Mobile Device Management Service"); }
} androidServiceRegRef.unregister();
androidServiceRegRef.unregister(); iOSServiceRegRef.unregister();
iOSServiceRegRef.unregister(); windowsServiceRegRef.unregister();
windowsServiceRegRef.unregister(); bundleContext.removeBundleListener(this);
}
bundleContext.removeBundleListener(this);
} @Override
public void bundleChanged(BundleEvent bundleEvent) {
@Override int eventType = bundleEvent.getType();
public void bundleChanged(BundleEvent bundleEvent) { String bundleSymbolicName = bundleEvent.getBundle().getSymbolicName();
int eventType = bundleEvent.getType(); // log.info("Received Event bundleSymbolicName " + bundleSymbolicName);
String bundleSymbolicName = bundleEvent.getBundle().getSymbolicName(); // log.info("Received Event :::::::::::::::: " + typeAsString(bundleEvent));
if (SYMBOLIC_NAME_DATA_SOURCE_COMPONENT.equals(bundleSymbolicName) && eventType == BundleEvent.STARTED) {
for (DataSourceListener listener : this.getDataSourceListeners()) { if (SYMBOLIC_NAME_DATA_SOURCE_COMPONENT.equals(bundleSymbolicName) &&
listener.notifyObserver(); eventType == BundleEvent.STARTED) {
} for (DataSourceListener listener : this.getDataSourceListeners()) {
} listener.notifyObserver();
} }
}
public static void registerDataSourceListener(DataSourceListener listener) { }
dataSourceListeners.add(listener);
} public static void registerDataSourceListener(DataSourceListener listener) {
dataSourceListeners.add(listener);
private List<DataSourceListener> getDataSourceListeners() { }
return dataSourceListeners;
} private List<DataSourceListener> getDataSourceListeners() {
return dataSourceListeners;
private void setupMobileDeviceManagementSchema(MobileDataSourceConfig config) throws }
DeviceManagementException {
MobileDeviceManagementSchemaInitializer initializer = private static String typeAsString(BundleEvent event) {
new MobileDeviceManagementSchemaInitializer(config); if (event == null) {
log.info("Initializing mobile device management repository database schema"); return "null";
try { }
//initializer.createRegistryDatabase(); int type = event.getType();
} catch (Exception e) { switch (type) {
throw new DeviceManagementException( case BundleEvent.INSTALLED:
"Error occurred while initializing Mobile Device Management " + return "INSTALLED";
"database schema", e); case BundleEvent.LAZY_ACTIVATION:
} return "LAZY_ACTIVATION";
} case BundleEvent.RESOLVED:
return "RESOLVED";
case BundleEvent.STARTED:
return "STARTED";
case BundleEvent.STARTING:
return "Starting";
case BundleEvent.STOPPED:
return "STOPPED";
case BundleEvent.UNINSTALLED:
return "UNINSTALLED";
case BundleEvent.UNRESOLVED:
return "UNRESOLVED";
case BundleEvent.UPDATED:
return "UPDATED";
default:
return "unknown event type: " + type;
}
}
} }

@ -18,10 +18,10 @@ package org.wso2.carbon.device.mgt.mobile.impl.util;
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.mobile.impl.config.datasource.MobileDataSourceConfig;
import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.CarbonUtils;
import org.wso2.carbon.utils.dbcreator.DatabaseCreator; import org.wso2.carbon.utils.dbcreator.DatabaseCreator;
import javax.sql.DataSource;
import java.io.File; import java.io.File;
public final class MobileDeviceManagementSchemaInitializer extends DatabaseCreator { public final class MobileDeviceManagementSchemaInitializer extends DatabaseCreator {
@ -29,10 +29,10 @@ public final class MobileDeviceManagementSchemaInitializer extends DatabaseCreat
private static final Log log = LogFactory.getLog(MobileDeviceManagementSchemaInitializer.class); private static final Log log = LogFactory.getLog(MobileDeviceManagementSchemaInitializer.class);
private static final String setupSQLScriptBaseLocation = private static final String setupSQLScriptBaseLocation =
CarbonUtils.getCarbonHome() + File.separator + "dbscripts" + File.separator + "cdm" + CarbonUtils.getCarbonHome() + File.separator + "dbscripts" + File.separator + "cdm" +
File.separator + "plugins"; File.separator + "plugins" + File.separator ;
public MobileDeviceManagementSchemaInitializer(MobileDataSourceConfig config) { public MobileDeviceManagementSchemaInitializer(DataSource dataSource) {
super(MobileDeviceManagerUtil.resolveDataSource(config)); super(dataSource);
} }
protected String getDbScriptLocation(String databaseType) { protected String getDbScriptLocation(String databaseType) {

@ -0,0 +1,109 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed 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.device.mgt.mobile.impl.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.Device;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileDevice;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* Provides utility methods required by the mobile device management bundle.
*/
public class MobileDeviceManagementUtil {
private static final Log log = LogFactory.getLog(MobileDeviceManagementUtil.class);
private static final String MOBILE_DEVICE_IMEI = "imei";
private static final String MOBILE_DEVICE_IMSI = "imsi";
private static final String MOBILE_DEVICE_REG_ID = "regId";
private static final String MOBILE_DEVICE_VENDOR = "vendor";
private static final String MOBILE_DEVICE_OS_VERSION = "osVersion";
private static final String MOBILE_DEVICE_MODEL = "model";
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);
}
}
private static String getPropertyValue(Device device, String property) {
for (Device.Property prop : device.getProperties()) {
if (property.equals(prop.getName())) {
return prop.getValue();
}
}
return null;
}
private static Device.Property getProperty(String property, String value) {
Device.Property prop = null;
if(property != null){
prop = new Device.Property();
prop.setName(property);
prop.setValue(value);
return prop;
}
return prop;
}
public static MobileDevice convertToMobileDevice(Device device) {
MobileDevice mobileDevice = null;
if (device != null) {
mobileDevice = new MobileDevice();
mobileDevice.setMobileDeviceId(device.getDeviceIdentifier());
mobileDevice.setImei(getPropertyValue(device, MOBILE_DEVICE_IMEI));
mobileDevice.setImsi(getPropertyValue(device, MOBILE_DEVICE_IMSI));
mobileDevice.setRegId(getPropertyValue(device, MOBILE_DEVICE_REG_ID));
mobileDevice.setModel(getPropertyValue(device, MOBILE_DEVICE_MODEL));
mobileDevice.setOsVersion(getPropertyValue(device, MOBILE_DEVICE_OS_VERSION));
mobileDevice.setVendor(getPropertyValue(device, MOBILE_DEVICE_VENDOR));
}
return mobileDevice;
}
public static Device convertToDevice(MobileDevice mobileDevice) {
Device device = null;
if(mobileDevice!=null){
device = new Device();
List<Device.Property> propertyList = new ArrayList<Device.Property>();
propertyList.add(getProperty(MOBILE_DEVICE_IMEI,mobileDevice.getImei()));
propertyList.add(getProperty(MOBILE_DEVICE_IMSI,mobileDevice.getImsi()));
propertyList.add(getProperty(MOBILE_DEVICE_REG_ID,mobileDevice.getRegId()));
propertyList.add(getProperty(MOBILE_DEVICE_MODEL,mobileDevice.getModel()));
propertyList.add(getProperty(MOBILE_DEVICE_OS_VERSION,mobileDevice.getOsVersion()));
propertyList.add(getProperty(MOBILE_DEVICE_VENDOR,mobileDevice.getVendor()));
device.setProperties(propertyList);
device.setDeviceIdentifier(mobileDevice.getMobileDeviceId());
}
return device;
}
}

@ -1,91 +0,0 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed 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.device.mgt.mobile.impl.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.mobile.impl.config.datasource.JNDILookupDefinition;
import org.wso2.carbon.device.mgt.mobile.impl.config.datasource.MobileDataSourceConfig;
import org.wso2.carbon.device.mgt.mobile.impl.dao.util.MobileDeviceManagementDAOUtil;
import javax.sql.DataSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.Hashtable;
import java.util.List;
/**
* Created by harshan on 12/15/14.
*/
public class MobileDeviceManagerUtil {
private static final Log log = LogFactory.getLog(MobileDeviceManagerUtil.class);
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);
}
}
/**
* Resolve data source from the data source definition
*
* @param config data source configuration
* @return data source resolved from the data source definition
*/
public static DataSource resolveDataSource(MobileDataSourceConfig config) {
DataSource dataSource = null;
if (config == null) {
throw new RuntimeException(
"Mobile Device Management Repository data source configuration " +
"is null and thus, is not initialized");
}
JNDILookupDefinition jndiConfig = config.getJndiLookupDefintion();
if (jndiConfig != null) {
if (log.isDebugEnabled()) {
log.debug(
"Initializing Mobile Device Management Repository data source using the JNDI " +
"Lookup Definition");
}
List<JNDILookupDefinition.JNDIProperty> jndiPropertyList =
jndiConfig.getJndiProperties();
if (jndiPropertyList != null) {
Hashtable<Object, Object> jndiProperties = new Hashtable<Object, Object>();
for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) {
jndiProperties.put(prop.getName(), prop.getValue());
}
dataSource =
MobileDeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(),
jndiProperties);
} else {
dataSource = MobileDeviceManagementDAOUtil
.lookupDataSource(jndiConfig.getJndiName(), null);
}
}
return dataSource;
}
}

@ -23,7 +23,7 @@ import javax.ws.rs.ext.ExceptionMapper;
public class ErrorHandler implements ExceptionMapper { public class ErrorHandler implements ExceptionMapper {
@Override public Response toResponse(Throwable throwable) { public Response toResponse(Throwable throwable) {
Response.Status status; Response.Status status;
status = Response.Status.INTERNAL_SERVER_ERROR; status = Response.Status.INTERNAL_SERVER_ERROR;
// return Response.status(status).header("exception", exception.getMessage()).build(); // return Response.status(status).header("exception", exception.getMessage()).build();

@ -1,38 +1,11 @@
CREATE TABLE IF NOT EXISTS MBL_OS_VERSION
(
VERSION_ID INT NOT NULL AUTO_INCREMENT,
NAME VARCHAR(45) NULL DEFAULT NULL,
PRIMARY KEY (VERSION_ID)
);
CREATE TABLE IF NOT EXISTS MBL_DEVICE_MODEL
(
MODEL_ID INT NOT NULL AUTO_INCREMENT,
MODEL VARCHAR(45) NULL DEFAULT NULL,
PRIMARY KEY (MODEL_ID)
);
CREATE TABLE IF NOT EXISTS MBL_VENDOR
(
VENDOR_ID INT NOT NULL AUTO_INCREMENT,
VENDOR VARCHAR(45) NULL DEFAULT NULL,
PRIMARY KEY (VENDOR_ID)
);
CREATE TABLE IF NOT EXISTS MBL_DEVICE CREATE TABLE IF NOT EXISTS MBL_DEVICE
( (
MOBILE_DEVICE_ID VARCHAR(45) NOT NULL, MOBILE_DEVICE_ID VARCHAR(45) NOT NULL,
REG_ID VARCHAR(45) NOT NULL, REG_ID VARCHAR(45) NOT NULL,
IMEI VARCHAR(45) NOT NULL, IMEI VARCHAR(45) NOT NULL,
IMSI VARCHAR(45) NOT NULL, IMSI VARCHAR(45) NOT NULL,
OS_VERSION_ID INT NOT NULL, OS_VERSION VARCHAR(45) NOT NULL,
DEVICE_MODEL_ID INT NOT NULL, DEVICE_MODEL VARCHAR(45) NOT NULL,
VENDOR_ID INT NOT NULL, VENDOR VARCHAR(45) NOT NULL,
PRIMARY KEY (MOBILE_DEVICE_ID), PRIMARY KEY (MOBILE_DEVICE_ID)
CONSTRAINT fk_DEVICE_OS_VERSION1 FOREIGN KEY (OS_VERSION_ID )
REFERENCES MBL_OS_VERSION (VERSION_ID ) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_DEVICE_DEVICE_MODEL2 FOREIGN KEY (DEVICE_MODEL_ID )
REFERENCES MBL_DEVICE_MODEL (MODEL_ID ) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_DEVICE_VENDOR1 FOREIGN KEY (VENDOR_ID )
REFERENCES MBL_VENDOR (VENDOR_ID ) ON DELETE NO ACTION ON UPDATE NO ACTION
); );

@ -1,33 +1,3 @@
-- -----------------------------------------------------
-- Table `MBL_OS_VERSION`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `MBL_OS_VERSION` (
`VERSION_ID` INT NOT NULL AUTO_INCREMENT,
`VERSION` VARCHAR(45) NULL,
PRIMARY KEY (`VERSION_ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `MBL_DEVICE_MODEL`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `MBL_DEVICE_MODEL` (
`MODEL_ID` INT NOT NULL AUTO_INCREMENT,
`MODEL` VARCHAR(45) NULL,
PRIMARY KEY (`MODEL_ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `MBL_VENDOR`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `MBL_VENDOR` (
`VENDOR_ID` INT NOT NULL AUTO_INCREMENT,
`VENDOR` VARCHAR(45) NULL,
PRIMARY KEY (`VENDOR_ID`))
ENGINE = InnoDB;
-- ----------------------------------------------------- -- -----------------------------------------------------
-- Table `MBL_DEVICE` -- Table `MBL_DEVICE`
-- ----------------------------------------------------- -- -----------------------------------------------------
@ -36,28 +6,10 @@ CREATE TABLE IF NOT EXISTS `MBL_DEVICE` (
`REG_ID` VARCHAR(45) NULL, `REG_ID` VARCHAR(45) NULL,
`IMEI` VARCHAR(45) NULL, `IMEI` VARCHAR(45) NULL,
`IMSI` VARCHAR(45) NULL, `IMSI` VARCHAR(45) NULL,
`OS_VERSION_ID` INT NOT NULL, `OS_VERSION` VARCHAR(45) NULL,
`DEVICE_MODEL_ID` INT NOT NULL, `DEVICE_MODEL` VARCHAR(45) NULL,
`VENDOR_ID` INT NOT NULL, `VENDOR` VARCHAR(45) NULL,
PRIMARY KEY (`MOBILE_DEVICE_ID`), PRIMARY KEY (`MOBILE_DEVICE_ID`))
INDEX `fk_DEVICE_OS_VERSION1_idx` (`OS_VERSION_ID` ASC),
INDEX `fk_DEVICE_DEVICE_MODEL2_idx` (`DEVICE_MODEL_ID` ASC),
INDEX `fk_DEVICE_VENDOR1_idx` (`VENDOR_ID` ASC),
CONSTRAINT `fk_DEVICE_OS_VERSION1`
FOREIGN KEY (`OS_VERSION_ID`)
REFERENCES `MBL_OS_VERSION` (`VERSION_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_DEVICE_DEVICE_MODEL2`
FOREIGN KEY (`DEVICE_MODEL_ID`)
REFERENCES `MBL_DEVICE_MODEL` (`MODEL_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_DEVICE_VENDOR1`
FOREIGN KEY (`VENDOR_ID`)
REFERENCES `MBL_VENDOR` (`VENDOR_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB; ENGINE = InnoDB;

Loading…
Cancel
Save