From ad10426c110ddde266893d2e07dfdfa3e9bcc21f Mon Sep 17 00:00:00 2001 From: harshanL Date: Wed, 17 Dec 2014 15:43:31 +0530 Subject: [PATCH] Added mobile device management service with db schema --- .../android/AndroidDeviceManagerService.java | 86 +++++++- .../MobileDeviceConfigurationManager.java | 4 +- .../mgt/mobile/impl/dao/MobileDeviceDAO.java | 6 +- .../impl/dao/MobileDeviceDAOFactory.java | 57 ----- .../dao/MobileDeviceManagementDAOFactory.java | 82 +++---- .../mobile/impl/dao/MobileDeviceModelDAO.java | 34 --- .../impl/dao/MobileDeviceVendorDAO.java | 35 --- .../mobile/impl/dao/MobileOSVersionDAO.java | 34 --- .../impl/dao/impl/MobileDeviceDAOImpl.java | 131 ++++++++++- .../dao/impl/MobileDeviceModelDAOImpl.java | 62 ------ .../dao/impl/MobileDeviceVendorDAOImpl.java | 62 ------ .../impl/dao/impl/MobileOSVersionDAOImpl.java | 58 ----- .../util/MobileDeviceManagementDAOUtil.java | 128 +++++++++-- .../mgt/mobile/impl/dto/MobileDevice.java | 30 +-- .../mobile/impl/dto/MobileDeviceModel.java | 44 ---- .../mobile/impl/dto/MobileDeviceVendor.java | 44 ---- .../mgt/mobile/impl/dto/MobileOSVersion.java | 44 ---- ...MobileDeviceManagementBundleActivator.java | 206 ++++++++++-------- ...bileDeviceManagementSchemaInitializer.java | 8 +- .../impl/util/MobileDeviceManagementUtil.java | 109 +++++++++ .../impl/util/MobileDeviceManagerUtil.java | 91 -------- .../cdm/api/android/common/ErrorHandler.java | 2 +- .../repository/dbscripts/cdm/plugins/h2.sql | 35 +-- .../dbscripts/cdm/plugins/mysql.sql | 56 +---- 24 files changed, 587 insertions(+), 861 deletions(-) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceDAOFactory.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceModelDAO.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceVendorDAO.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileOSVersionDAO.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceModelDAOImpl.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceVendorDAOImpl.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileOSVersionDAOImpl.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDeviceModel.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDeviceVendor.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileOSVersion.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagementUtil.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagerUtil.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManagerService.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManagerService.java index 805345e74..78791808e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManagerService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManagerService.java @@ -16,11 +16,17 @@ 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.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.DeviceManagementException; 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; @@ -29,6 +35,8 @@ import java.util.List; */ public class AndroidDeviceManagerService implements DeviceManagerService { + private static final Log log = LogFactory.getLog(AndroidDeviceManagerService.class); + @Override public String getProviderType() { return DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID; @@ -36,22 +44,64 @@ public class AndroidDeviceManagerService implements DeviceManagerService { @Override 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 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 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 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 @@ -72,7 +122,17 @@ public class AndroidDeviceManagerService implements DeviceManagerService { @Override 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 @@ -83,9 +143,15 @@ public class AndroidDeviceManagerService implements DeviceManagerService { @Override 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 - -} +} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceConfigurationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceConfigurationManager.java index 1a2481199..3b69a71e6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceConfigurationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceConfigurationManager.java @@ -18,7 +18,7 @@ package org.wso2.carbon.device.mgt.mobile.impl.config; import org.w3c.dom.Document; 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.utils.CarbonUtils; @@ -53,7 +53,7 @@ public class MobileDeviceConfigurationManager { public synchronized void initConfig() throws DeviceManagementException { try { File mobileDeviceMgtConfig = new File(mobileDeviceMgtConfigXMLPath); - Document doc = MobileDeviceManagerUtil.convertToDocument(mobileDeviceMgtConfig); + Document doc = MobileDeviceManagementUtil.convertToDocument(mobileDeviceMgtConfig); JAXBContext mobileDeviceMgmtContext = JAXBContext.newInstance(MobileDeviceManagementConfig.class); Unmarshaller unmarshaller = mobileDeviceMgmtContext.createUnmarshaller(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceDAO.java index 16330df10..8cba1f8a0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceDAO.java @@ -26,10 +26,10 @@ public interface MobileDeviceDAO { 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; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceDAOFactory.java deleted file mode 100644 index fabb81d8d..000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceDAOFactory.java +++ /dev/null @@ -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); - } - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceManagementDAOFactory.java index 65750447d..c7bc7922a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceManagementDAOFactory.java @@ -18,87 +18,59 @@ package org.wso2.carbon.device.mgt.mobile.impl.dao; import org.apache.commons.logging.Log; 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.JNDILookupDefinition; 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; -import java.util.Hashtable; -import java.util.List; /** * Factory class used to create MobileDeviceManagement related DAO objects. */ -public class MobileDeviceManagementDAOFactory { +public class MobileDeviceManagementDAOFactory implements DataSourceListener { private static DataSource dataSource; + private static MobileDataSourceConfig mobileDataSourceConfig; private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOFactory.class); - public static MobileDeviceDAO getMobileDeviceDAO() { - return new MobileDeviceDAOImpl(dataSource); - } + public MobileDeviceManagementDAOFactory() { - public static MobileDeviceModelDAO getMobileDeviceModelDAO() { - return new MobileDeviceModelDAOImpl(dataSource); } - public static MobileDeviceVendorDAO getMobileDeviceVendorDAO() { - return new MobileDeviceVendorDAOImpl(dataSource); + public void init(){ + dataSource = MobileDeviceManagementDAOUtil.resolveDataSource(mobileDataSourceConfig); + if(dataSource!=null){ + MobileDeviceManagementDAOUtil.createDataSource(dataSource); + }else{ + MobileDeviceManagementBundleActivator.registerDataSourceListener(this); + } } - public static MobileOSVersionDAO getMobileOSVersionDAO() { - return new MobileOSVersionDAOImpl(dataSource); + public static MobileDeviceDAO getMobileDeviceDAO() { + return new MobileDeviceDAOImpl(dataSource); } - public static void init(MobileDataSourceConfig config) { - dataSource = resolveDataSource(config); + public static MobileDataSourceConfig getMobileDeviceManagementConfig() { + return mobileDataSourceConfig; } - public static void init(DataSource dtSource) { - dataSource = dtSource; + public static void setMobileDataSourceConfig( + MobileDataSourceConfig mobileDataSourceConfig) { + MobileDeviceManagementDAOFactory.mobileDataSourceConfig = + mobileDataSourceConfig; } - /** - * 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 jndiPropertyList = - jndiConfig.getJndiProperties(); - if (jndiPropertyList != null) { - Hashtable jndiProperties = new Hashtable(); - for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) { - jndiProperties.put(prop.getName(), prop.getValue()); - } - dataSource = - MobileDeviceManagementDAOUtil - .lookupDataSource(jndiConfig.getJndiName(), jndiProperties); - } else { - dataSource = MobileDeviceManagementDAOUtil - .lookupDataSource(jndiConfig.getJndiName(), null); - } - } + public static DataSource getDataSource() { return dataSource; } - public static DataSource getDataSource() { - return dataSource; + @Override + public void notifyObserver() { + dataSource = MobileDeviceManagementDAOUtil.resolveDataSource(mobileDataSourceConfig); + if(dataSource!=null){ + MobileDeviceManagementDAOUtil.createDataSource(dataSource); + } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceModelDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceModelDAO.java deleted file mode 100644 index 768f29723..000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceModelDAO.java +++ /dev/null @@ -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; -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceVendorDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceVendorDAO.java deleted file mode 100644 index 4a4d24bf7..000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceVendorDAO.java +++ /dev/null @@ -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; -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileOSVersionDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileOSVersionDAO.java deleted file mode 100644 index dcffc528b..000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileOSVersionDAO.java +++ /dev/null @@ -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; -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceDAOImpl.java index d0ab883d6..42164e2c3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceDAOImpl.java @@ -20,9 +20,14 @@ import org.apache.commons.logging.Log; 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.MobileDeviceManagementDAOException; +import org.wso2.carbon.device.mgt.mobile.impl.dao.util.MobileDeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileDevice; import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; /** * Implementation of MobileDeviceDAO. @@ -38,23 +43,139 @@ public class MobileDeviceDAOImpl implements MobileDeviceDAO { @Override 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 - public void addDevice(MobileDevice mobileDevice) + public boolean addDevice(MobileDevice mobileDevice) 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 - public void updateDevice(MobileDevice mobileDevice) + public boolean updateDevice(MobileDevice mobileDevice) 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 - 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); + } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceModelDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceModelDAOImpl.java deleted file mode 100644 index 04dbd9c65..000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceModelDAOImpl.java +++ /dev/null @@ -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 { - - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceVendorDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceVendorDAOImpl.java deleted file mode 100644 index 3908ef3ba..000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceVendorDAOImpl.java +++ /dev/null @@ -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 { - - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileOSVersionDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileOSVersionDAOImpl.java deleted file mode 100644 index 4296ec974..000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileOSVersionDAOImpl.java +++ /dev/null @@ -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 { - - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/util/MobileDeviceManagementDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/util/MobileDeviceManagementDAOUtil.java index bc210f8ff..104de0fd7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/util/MobileDeviceManagementDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/util/MobileDeviceManagementDAOUtil.java @@ -16,14 +16,12 @@ 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.LogFactory; -import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceManagementDAOException; -import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileDevice; +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.util.MobileDeviceManagementSchemaInitializer; import javax.naming.InitialContext; import javax.sql.DataSource; @@ -32,18 +30,79 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Hashtable; +import java.util.List; /** * Utility method required by MobileDeviceManagement DAO classes. */ public class MobileDeviceManagementDAOUtil { - public static DataSource resolveDataSource() { - return null; - } - 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 jndiPropertyList = + jndiConfig.getJndiProperties(); + if (jndiPropertyList != null) { + Hashtable jndiProperties = new Hashtable(); + 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 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) { if (rs != null) { try { @@ -68,23 +127,46 @@ public class MobileDeviceManagementDAOUtil { } } - public static DataSource lookupDataSource(String dataSourceName, - final Hashtable jndiProperties) { - try { - if (jndiProperties == null || jndiProperties.isEmpty()) { - return (DataSource) InitialContext.doLookup(dataSourceName); + /** + * Initializes the creation of mobile device management schema if -Dsetup has provided + * + * @param dataSource Mobile data source + */ + 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 { - MobileDevice mobileDeviceBO = new MobileDevice(); - return mobileDeviceBO; + /** + * Creates the mobile device management schema + * + * @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); + } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDevice.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDevice.java index f7b1d14b1..f054b2bf5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDevice.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDevice.java @@ -27,9 +27,9 @@ public class MobileDevice implements Serializable { private String regId; private String imei; private String imsi; - private int osVersionId; - private int modelId; - private int vendorId; + private String osVersion; + private String model; + private String vendor; public String getMobileDeviceId() { return mobileDeviceId; @@ -63,27 +63,27 @@ public class MobileDevice implements Serializable { this.imsi = imsi; } - public int getOsVersionId() { - return osVersionId; + public String getOsVersion() { + return osVersion; } - public void setOsVersionId(int osVersionId) { - this.osVersionId = osVersionId; + public void setOsVersion(String osVersion) { + this.osVersion = osVersion; } - public int getModelId() { - return modelId; + public String getModel() { + return model; } - public void setModelId(int modelId) { - this.modelId = modelId; + public void setModel(String model) { + this.model = model; } - public int getVendorId() { - return vendorId; + public String getVendor() { + return vendor; } - public void setVendorId(int vendorId) { - this.vendorId = vendorId; + public void setVendor(String vendor) { + this.vendor = vendor; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDeviceModel.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDeviceModel.java deleted file mode 100644 index b5d5e5ebb..000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDeviceModel.java +++ /dev/null @@ -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; - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDeviceVendor.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDeviceVendor.java deleted file mode 100644 index 2d48af52a..000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileDeviceVendor.java +++ /dev/null @@ -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; - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileOSVersion.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileOSVersion.java deleted file mode 100644 index 32a56b6fc..000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dto/MobileOSVersion.java +++ /dev/null @@ -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; - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/internal/MobileDeviceManagementBundleActivator.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/internal/MobileDeviceManagementBundleActivator.java index 9d3e08cad..d544e2fa6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/internal/MobileDeviceManagementBundleActivator.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/internal/MobileDeviceManagementBundleActivator.java @@ -19,14 +19,14 @@ package org.wso2.carbon.device.mgt.mobile.impl.internal; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; 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.mobile.impl.DataSourceListener; 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.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.util.MobileDeviceManagementSchemaInitializer; import org.wso2.carbon.device.mgt.mobile.impl.windows.WindowsDeviceManagerService; import java.util.ArrayList; @@ -34,97 +34,113 @@ import java.util.List; public class MobileDeviceManagementBundleActivator implements BundleActivator, BundleListener { - private ServiceRegistration androidServiceRegRef; - private ServiceRegistration iOSServiceRegRef; - private ServiceRegistration windowsServiceRegRef; - private static List dataSourceListeners = new ArrayList(); - - private static final Log log = LogFactory.getLog(MobileDeviceManagementBundleActivator.class); - private static final String SYMBOLIC_NAME_DATA_SOURCE_COMPONENT = "org.wso2.carbon.ndatasource.core"; - - @Override - public void start(BundleContext bundleContext) throws Exception { - try { - if (log.isDebugEnabled()) { - log.debug("Activating Mobile Device Management Service bundle"); - } - bundleContext.addBundleListener(this); - - /* If -Dsetup option enabled then create device management database schema */ - 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"); - } - setupMobileDeviceManagementSchema(null); - } - - MobileDeviceDAOFactory daoFactory = new MobileDeviceDAOFactory(); - //TODO Register this dao to an appropriate config file - - androidServiceRegRef = - bundleContext.registerService(DeviceManagerService.class.getName(), - new AndroidDeviceManagerService(), null); - iOSServiceRegRef = - bundleContext.registerService(DeviceManagerService.class.getName(), - new IOSDeviceManagerService(), null); - windowsServiceRegRef = - bundleContext.registerService(DeviceManagerService.class.getName(), - new WindowsDeviceManagerService(), null); - 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", e); - } - } - - @Override - public void stop(BundleContext bundleContext) throws Exception { - if (log.isDebugEnabled()) { - log.debug("Deactivating Mobile Device Management Service"); - } - androidServiceRegRef.unregister(); - iOSServiceRegRef.unregister(); - windowsServiceRegRef.unregister(); - - bundleContext.removeBundleListener(this); - } - - @Override - public void bundleChanged(BundleEvent bundleEvent) { - int eventType = bundleEvent.getType(); - String bundleSymbolicName = bundleEvent.getBundle().getSymbolicName(); - if (SYMBOLIC_NAME_DATA_SOURCE_COMPONENT.equals(bundleSymbolicName) && eventType == BundleEvent.STARTED) { - for (DataSourceListener listener : this.getDataSourceListeners()) { - listener.notifyObserver(); - } - } - } - - public static void registerDataSourceListener(DataSourceListener listener) { - dataSourceListeners.add(listener); - } - - private List getDataSourceListeners() { - return dataSourceListeners; - } - - private void setupMobileDeviceManagementSchema(MobileDataSourceConfig config) throws - DeviceManagementException { - MobileDeviceManagementSchemaInitializer initializer = - new MobileDeviceManagementSchemaInitializer(config); - 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); - } - } - + private ServiceRegistration androidServiceRegRef; + private ServiceRegistration iOSServiceRegRef; + private ServiceRegistration windowsServiceRegRef; + private static List dataSourceListeners = + new ArrayList(); + + private static final Log log = LogFactory.getLog(MobileDeviceManagementBundleActivator.class); + private static final String SYMBOLIC_NAME_DATA_SOURCE_COMPONENT = + "org.eclipse.osgi"; + + @Override + public void start(BundleContext bundleContext) throws Exception { + try { + if (log.isDebugEnabled()) { + log.debug("Activating Mobile Device Management Service bundle"); + } + bundleContext.addBundleListener(this); + /* Initialize the datasource configuration */ + MobileDeviceConfigurationManager.getInstance().initConfig(); + MobileDeviceManagementConfig config = MobileDeviceConfigurationManager.getInstance() + .getMobileDeviceManagementConfig(); + MobileDataSourceConfig dsConfig = + config.getMobileDeviceMgtRepository().getMobileDataSourceConfig(); + + MobileDeviceManagementDAOFactory daoFactory = new MobileDeviceManagementDAOFactory(); + daoFactory.setMobileDataSourceConfig(dsConfig); + daoFactory.init(); + + androidServiceRegRef = + bundleContext.registerService(DeviceManagerService.class.getName(), + new AndroidDeviceManagerService(), null); + iOSServiceRegRef = + bundleContext.registerService(DeviceManagerService.class.getName(), + new IOSDeviceManagerService(), null); + windowsServiceRegRef = + bundleContext.registerService(DeviceManagerService.class.getName(), + new WindowsDeviceManagerService(), null); + 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", + e); + } + } + + @Override + public void stop(BundleContext bundleContext) throws Exception { + if (log.isDebugEnabled()) { + log.debug("Deactivating Mobile Device Management Service"); + } + androidServiceRegRef.unregister(); + iOSServiceRegRef.unregister(); + windowsServiceRegRef.unregister(); + bundleContext.removeBundleListener(this); + } + + @Override + public void bundleChanged(BundleEvent bundleEvent) { + int eventType = bundleEvent.getType(); + String bundleSymbolicName = bundleEvent.getBundle().getSymbolicName(); +// log.info("Received Event bundleSymbolicName " + bundleSymbolicName); +// log.info("Received Event :::::::::::::::: " + typeAsString(bundleEvent)); + + if (SYMBOLIC_NAME_DATA_SOURCE_COMPONENT.equals(bundleSymbolicName) && + eventType == BundleEvent.STARTED) { + for (DataSourceListener listener : this.getDataSourceListeners()) { + listener.notifyObserver(); + } + } + } + + public static void registerDataSourceListener(DataSourceListener listener) { + dataSourceListeners.add(listener); + } + + private List getDataSourceListeners() { + return dataSourceListeners; + } + + private static String typeAsString(BundleEvent event) { + if (event == null) { + return "null"; + } + int type = event.getType(); + switch (type) { + case BundleEvent.INSTALLED: + return "INSTALLED"; + 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; + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagementSchemaInitializer.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagementSchemaInitializer.java index f62b09cc1..24e5b57bc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagementSchemaInitializer.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagementSchemaInitializer.java @@ -18,10 +18,10 @@ package org.wso2.carbon.device.mgt.mobile.impl.util; import org.apache.commons.logging.Log; 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.dbcreator.DatabaseCreator; +import javax.sql.DataSource; import java.io.File; 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 String setupSQLScriptBaseLocation = CarbonUtils.getCarbonHome() + File.separator + "dbscripts" + File.separator + "cdm" + - File.separator + "plugins"; + File.separator + "plugins" + File.separator ; - public MobileDeviceManagementSchemaInitializer(MobileDataSourceConfig config) { - super(MobileDeviceManagerUtil.resolveDataSource(config)); + public MobileDeviceManagementSchemaInitializer(DataSource dataSource) { + super(dataSource); } protected String getDbScriptLocation(String databaseType) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagementUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagementUtil.java new file mode 100644 index 000000000..3cc4af661 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagementUtil.java @@ -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 propertyList = new ArrayList(); + 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; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagerUtil.java deleted file mode 100644 index c5b9940fb..000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagerUtil.java +++ /dev/null @@ -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 jndiPropertyList = - jndiConfig.getJndiProperties(); - if (jndiPropertyList != null) { - Hashtable jndiProperties = new Hashtable(); - 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; - } -} diff --git a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/common/ErrorHandler.java b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/common/ErrorHandler.java index 14603fd1b..6fa07dba1 100644 --- a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/common/ErrorHandler.java +++ b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/common/ErrorHandler.java @@ -23,7 +23,7 @@ import javax.ws.rs.ext.ExceptionMapper; public class ErrorHandler implements ExceptionMapper { - @Override public Response toResponse(Throwable throwable) { + public Response toResponse(Throwable throwable) { Response.Status status; status = Response.Status.INTERNAL_SERVER_ERROR; // return Response.status(status).header("exception", exception.getMessage()).build(); diff --git a/product/modules/distribution/src/repository/dbscripts/cdm/plugins/h2.sql b/product/modules/distribution/src/repository/dbscripts/cdm/plugins/h2.sql index f36a034bd..e42642149 100644 --- a/product/modules/distribution/src/repository/dbscripts/cdm/plugins/h2.sql +++ b/product/modules/distribution/src/repository/dbscripts/cdm/plugins/h2.sql @@ -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 ( MOBILE_DEVICE_ID VARCHAR(45) NOT NULL, REG_ID VARCHAR(45) NOT NULL, IMEI VARCHAR(45) NOT NULL, IMSI VARCHAR(45) NOT NULL, - OS_VERSION_ID INT NOT NULL, - DEVICE_MODEL_ID INT NOT NULL, - VENDOR_ID INT NOT NULL, - 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 + OS_VERSION VARCHAR(45) NOT NULL, + DEVICE_MODEL VARCHAR(45) NOT NULL, + VENDOR VARCHAR(45) NOT NULL, + PRIMARY KEY (MOBILE_DEVICE_ID) ); \ No newline at end of file diff --git a/product/modules/distribution/src/repository/dbscripts/cdm/plugins/mysql.sql b/product/modules/distribution/src/repository/dbscripts/cdm/plugins/mysql.sql index 9c5771048..afc4f32d9 100644 --- a/product/modules/distribution/src/repository/dbscripts/cdm/plugins/mysql.sql +++ b/product/modules/distribution/src/repository/dbscripts/cdm/plugins/mysql.sql @@ -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` -- ----------------------------------------------------- @@ -36,28 +6,10 @@ CREATE TABLE IF NOT EXISTS `MBL_DEVICE` ( `REG_ID` VARCHAR(45) NULL, `IMEI` VARCHAR(45) NULL, `IMSI` VARCHAR(45) NULL, - `OS_VERSION_ID` INT NOT NULL, - `DEVICE_MODEL_ID` INT NOT NULL, - `VENDOR_ID` INT NOT NULL, - 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) + `OS_VERSION` VARCHAR(45) NULL, + `DEVICE_MODEL` VARCHAR(45) NULL, + `VENDOR` VARCHAR(45) NULL, + PRIMARY KEY (`MOBILE_DEVICE_ID`)) ENGINE = InnoDB;