diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java new file mode 100644 index 000000000..8af1724de --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java @@ -0,0 +1,27 @@ +/** + * Copyright (c) 2015, 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.core; + +public final class DeviceManagementConstants { + + public static final class Common { + private Common() { + throw new AssertionError(); + } + public static final String PROPERTY_SETUP = "setup"; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java index 9cf5ed8f3..87ea306ce 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java @@ -35,19 +35,26 @@ public class DeviceManagementRepository { public void addDeviceManagementProvider(DeviceManagerService provider) { String deviceType = provider.getProviderType(); - providers.put(deviceType, provider); try { DeviceManagerUtil.registerDeviceType(deviceType); } catch (DeviceManagementException e) { - log.error("Exception occured while registering the device type.",e); + log.error("Exception occurred while registering the device type.", e); } + providers.put(deviceType, provider); + } + + public void removeDeviceManagementProvider(DeviceManagerService provider) { + String deviceType = provider.getProviderType(); + try { + DeviceManagerUtil.unregisterDeviceType(deviceType); + } catch (DeviceManagementException e) { + log.error("Exception occurred while registering the device type.", e); + } + providers.remove(deviceType); } public DeviceManagerService getDeviceManagementProvider(String type) { return providers.get(type); } - public Map getProviders() { - return providers; - } } \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java index 8735df0fe..678f20d8f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java @@ -28,7 +28,7 @@ public final class DeviceManagementConfig { private DeviceManagementRepository deviceMgtRepository; - @XmlElement(name = "ManagementRepository", nillable = false) + @XmlElement(name = "ManagementRepository", required = true) public DeviceManagementRepository getDeviceMgtRepository() { return deviceMgtRepository; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementRepository.java index b84ef0388..a6bd91a51 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementRepository.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementRepository.java @@ -29,7 +29,7 @@ public class DeviceManagementRepository { private DataSourceConfig dataSourceConfig; - @XmlElement(name = "DataSourceConfiguration", nillable = false) + @XmlElement(name = "DataSourceConfiguration", required = false) public DataSourceConfig getDataSourceConfig() { return dataSourceConfig; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/DataSourceConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/DataSourceConfig.java index f8b07312a..bc7f98e92 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/DataSourceConfig.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/DataSourceConfig.java @@ -21,14 +21,14 @@ import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; /** - * Class for holding data source configuration in cdm-config.xml at parsing with JAXB + * Class for holding data source configuration in malformed-cdm-config-no-mgt-repo.xml at parsing with JAXB */ @XmlRootElement(name = "DataSourceConfiguration") public class DataSourceConfig { private JNDILookupDefinition jndiLookupDefintion; - @XmlElement(name = "JndiLookupDefinition", nillable = true) + @XmlElement(name = "JndiLookupDefinition", required = true) public JNDILookupDefinition getJndiLookupDefintion() { return jndiLookupDefintion; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/JNDILookupDefinition.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/JNDILookupDefinition.java index 2d0004f52..bc85ffb14 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/JNDILookupDefinition.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/JNDILookupDefinition.java @@ -27,7 +27,7 @@ public class JNDILookupDefinition { private String jndiName; private List jndiProperties; - @XmlElement(name = "Name", nillable = false) + @XmlElement(name = "Name", required = false) public String getJndiName() { return jndiName; } @@ -36,7 +36,7 @@ public class JNDILookupDefinition { this.jndiName = jndiName; } - @XmlElementWrapper(name = "Environment", nillable = false) + @XmlElementWrapper(name = "Environment", required = false) @XmlElement(name = "Property", nillable = false) public List getJndiProperties() { return jndiProperties; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java index 29c08d9ca..afeb42eaa 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java @@ -37,4 +37,6 @@ public interface DeviceTypeDAO { Integer getDeviceTypeIdByDeviceTypeName(String type) throws DeviceManagementDAOException; + void removeDeviceType(DeviceType deviceType) throws DeviceManagementDAOException; + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java index 049ed0424..98e6a98c2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java @@ -124,6 +124,11 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO { return deviceTypeId; } + @Override + public void removeDeviceType(DeviceType deviceType) throws DeviceManagementDAOException { + + } + private Connection getConnection() throws DeviceManagementDAOException { try { return dataSource.getConnection(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java index 20fa5e235..04f03a765 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java @@ -63,7 +63,8 @@ public final class DeviceManagementDAOUtil { } /** - * Get id of the current tenant + * Get id of the current tenant. + * * @return tenant id * @throws DeviceManagementDAOException if an error is observed when getting tenant id */ @@ -94,7 +95,7 @@ public final class DeviceManagementDAOUtil { return (DataSource) InitialContext.doLookup(dataSourceName); } final InitialContext context = new InitialContext(jndiProperties); - return (DataSource) context.doLookup(dataSourceName); + return (DataSource) context.lookup(dataSourceName); } catch (Exception e) { throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java index 68d53be20..311e59d96 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -23,6 +23,7 @@ import org.osgi.framework.BundleContext; import org.osgi.service.component.ComponentContext; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService; +import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.DeviceManagementRepository; import org.wso2.carbon.device.mgt.core.DeviceManager; import org.wso2.carbon.device.mgt.core.DeviceManagerImpl; @@ -49,14 +50,17 @@ import org.wso2.carbon.user.core.service.RealmService; * bind="setDeviceManagerService" * unbind="unsetDeviceManagerService" */ +@SuppressWarnings("unused") public class DeviceManagementServiceComponent { - public static final String SETUP_OPTION = "setup"; private static Log log = LogFactory.getLog(DeviceManagementServiceComponent.class); private DeviceManagementRepository pluginRepository = new DeviceManagementRepository(); protected void activate(ComponentContext componentContext) { try { + if (log.isDebugEnabled()) { + log.debug("Initializing device management core bundle"); + } /* Initializing Device Management Configuration */ DeviceConfigurationManager.getInstance().initConfig(); DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); @@ -68,19 +72,24 @@ public class DeviceManagementServiceComponent { DeviceManagementDataHolder.getInstance().setDeviceManager(deviceManager); /* If -Dsetup option enabled then create device management database schema */ - String setupOption = System.getProperty(SETUP_OPTION); + String setupOption = System.getProperty(DeviceManagementConstants.Common.PROPERTY_SETUP); if (setupOption != null) { if (log.isDebugEnabled()) { log.debug("-Dsetup is enabled. Device management repository schema initialization is about " + "to begin"); } - setupDeviceManagementSchema(dsConfig); + this.setupDeviceManagementSchema(dsConfig); } + if (log.isDebugEnabled()) { + log.debug("Registering OSGi service DeviceManagementService"); + } BundleContext bundleContext = componentContext.getBundleContext(); bundleContext.registerService(DeviceManagementService.class.getName(), new DeviceManagementService(), null); - + if (log.isDebugEnabled()) { + log.debug("Device management core bundle has been successfully initialized"); + } } catch (Throwable e) { String msg = "Error occurred while initializing device management core bundle"; log.error(msg, e); @@ -91,38 +100,43 @@ public class DeviceManagementServiceComponent { DeviceManagementSchemaInitializer initializer = new DeviceManagementSchemaInitializer(config); log.info("Initializing device management repository database schema"); - // catch generic exception. If any error occurs wrap and throw DeviceManagementException try { initializer.createRegistryDatabase(); } catch (Exception e) { throw new DeviceManagementException("Error occurred while initializing Device Management " + "database schema", e); } + if (log.isDebugEnabled()) { + log.debug("Device management metadata repository schema has been successfully initialized"); + } } /** - * Sets Device Manager services - * @param deviceManager An instance of DeviceManagerService + * Sets Device Manager service. + * @param deviceManagerService An instance of DeviceManagerService */ - protected void setDeviceManagerService(DeviceManagerService deviceManager) { + protected void setDeviceManagerService(DeviceManagerService deviceManagerService) { if (log.isDebugEnabled()) { - log.debug("Setting Device Management Service"); + log.debug("Setting Device Management Service Provider : '" + + deviceManagerService.getProviderType() + "'"); } - this.getPluginRepository().addDeviceManagementProvider(deviceManager); + this.getPluginRepository().addDeviceManagementProvider(deviceManagerService); } /** - * Unsets Device Management services - * @param deviceManager An instance of DeviceManagerService + * Unsets Device Management service. + * @param deviceManagerService An Instance of DeviceManagerService */ - protected void unsetDeviceManagerService(DeviceManagerService deviceManager) { + protected void unsetDeviceManagerService(DeviceManagerService deviceManagerService) { if (log.isDebugEnabled()) { - log.debug("Unsetting Device Management Service"); + log.debug("Unsetting Device Management Service Provider : '" + + deviceManagerService.getProviderType() + "'"); } + this.getPluginRepository().removeDeviceManagementProvider(deviceManagerService); } /** - * Sets Realm Service + * Sets Realm Service. * @param realmService An instance of RealmService */ protected void setRealmService(RealmService realmService) { @@ -133,7 +147,7 @@ public class DeviceManagementServiceComponent { } /** - * Unsets Realm Service + * Unsets Realm Service. * @param realmService An instance of RealmService */ protected void unsetRealmService(RealmService realmService) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index ab71bdc5c..b973ebd06 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -87,24 +87,47 @@ public final class DeviceManagerUtil { /** * Adds a new device type to the database if it does not exists. * - * @param deviceTypeName device type + * @param deviceType device type * @return status of the operation */ - public static boolean registerDeviceType(String deviceTypeName) throws DeviceManagementException{ - boolean status = false; + public static boolean registerDeviceType(String deviceType) throws DeviceManagementException { + boolean status; try { DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); - Integer deviceTypeId = deviceTypeDAO.getDeviceTypeIdByDeviceTypeName(deviceTypeName); - if(deviceTypeId == null){ - DeviceType deviceType = new DeviceType(); - deviceType.setName(deviceTypeName); - deviceTypeDAO.addDeviceType(deviceType); + Integer deviceTypeId = deviceTypeDAO.getDeviceTypeIdByDeviceTypeName(deviceType); + if (deviceTypeId == null) { + DeviceType dt = new DeviceType(); + dt.setName(deviceType); + deviceTypeDAO.addDeviceType(dt); } status = true; } catch (DeviceManagementDAOException e) { - String msg = "Error occurred while registering the device type " + deviceTypeName; + String msg = "Error occurred while registering the device type " + deviceType; throw new DeviceManagementException(msg, e); } return status; } + + /** + * Unregisters an existing device type from the device management metadata repository. + * + * @param deviceType device type + * @return status of the operation + */ + public static boolean unregisterDeviceType(String deviceType) throws DeviceManagementException { + try { + DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); + Integer deviceTypeId = deviceTypeDAO.getDeviceTypeIdByDeviceTypeName(deviceType); + if (deviceTypeId == null) { + DeviceType dt = new DeviceType(); + dt.setName(deviceType); + deviceTypeDAO.removeDeviceType(dt); + } + return true; + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while registering the device type " + deviceType; + throw new DeviceManagementException(msg, e); + } + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementConfigTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementConfigTests.java new file mode 100644 index 000000000..15a6b5ccd --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementConfigTests.java @@ -0,0 +1,95 @@ +/** + * Copyright (c) 2015, 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.core; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; +import org.xml.sax.SAXException; + +import javax.xml.XMLConstants; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; +import java.io.File; + +public class DeviceManagementConfigTests { + + private static final Log log = LogFactory.getLog(DeviceManagementConfigTests.class); + private static final String MALFORMED_TEST_CONFIG_LOCATION_NO_MGT_REPOSITORY = + "./src/test/resources/config/malformed-cdm-config-no-mgt-repo.xml"; + private static final String MALFORMED_TEST_CONFIG_LOCATION_NO_DS_CONFIG = + "./src/test/resources/config/malformed-cdm-config-no-ds-config.xml"; + private static final String MALFORMED_TEST_CONFIG_LOCATION_NO_JNDI_CONFIG = + "./src/test/resources/config/malformed-cdm-config-no-jndi-config.xml"; + private static final String TEST_CONFIG_SCHEMA_LOCATION = + "./src/test/resources/config/schema/DeviceManagementConfigSchema.xsd"; + + private Schema schema; + + @BeforeClass + private void initSchema() { + File deviceManagementSchemaConfig = new File(DeviceManagementConfigTests.TEST_CONFIG_SCHEMA_LOCATION); + SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); + try { + schema = factory.newSchema(deviceManagementSchemaConfig); + } catch (SAXException e) { + Assert.fail("Invalid schema found", e); + } + } + + @Test() + public void testMandateManagementRepositoryElement() { + File malformedConfig = + new File(DeviceManagementConfigTests.MALFORMED_TEST_CONFIG_LOCATION_NO_MGT_REPOSITORY); + this.validateMalformedConfig(malformedConfig); + } + + @Test + public void testMandateDataSourceConfigurationElement() { + File malformedConfig = new File(DeviceManagementConfigTests.MALFORMED_TEST_CONFIG_LOCATION_NO_DS_CONFIG); + this.validateMalformedConfig(malformedConfig); + } + + @Test + public void testMandateJndiLookupDefinitionElement() { + File malformedConfig = new File(DeviceManagementConfigTests.MALFORMED_TEST_CONFIG_LOCATION_NO_JNDI_CONFIG); + this.validateMalformedConfig(malformedConfig); + } + + private void validateMalformedConfig(File malformedConfig) { + try { + JAXBContext ctx = JAXBContext.newInstance(DeviceManagementConfig.class); + Unmarshaller um = ctx.createUnmarshaller(); + um.setSchema(this.getSchema()); + um.unmarshal(malformedConfig); + Assert.assertTrue(false); + } catch (JAXBException e) { + log.error("Error occurred while unmarsharlling device management config", e); + Assert.assertTrue(true); + } + } + + private Schema getSchema() { + return schema; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java new file mode 100644 index 000000000..c6cfc919d --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java @@ -0,0 +1,58 @@ +/** + * Copyright (c) 2015, 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.core; + +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService; + +public class DeviceManagementRepositoryTests { + + private DeviceManagementRepository repository; + + @BeforeClass + public void initRepository() { + this.repository = new DeviceManagementRepository(); + } + + @Test + public void testAddDeviceManagementService() { + DeviceManagerService sourceProvider = new TestDeviceManagerService(); + this.getRepository().addDeviceManagementProvider(sourceProvider); + + DeviceManagerService targetProvider = + this.getRepository().getDeviceManagementProvider(TestDeviceManagerService.DEVICE_TYPE_TEST); + + Assert.assertEquals(targetProvider.getProviderType(), sourceProvider.getProviderType()); + } + + @Test(dependsOnMethods = "testAddDeviceManagementService") + public void testRemoveDeviceManagementService() { + DeviceManagerService sourceProvider = new TestDeviceManagerService(); + this.getRepository().removeDeviceManagementProvider(sourceProvider); + + DeviceManagerService targetProvider = + this.getRepository().getDeviceManagementProvider(TestDeviceManagerService.DEVICE_TYPE_TEST); + + Assert.assertNull(targetProvider); + } + + private DeviceManagementRepository getRepository() { + return repository; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagerService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagerService.java new file mode 100644 index 000000000..8d217b5eb --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagerService.java @@ -0,0 +1,90 @@ +/** + * Copyright (c) 2012, 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.core; + +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.OperationManager; +import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService; + +import java.util.List; + +public class TestDeviceManagerService implements DeviceManagerService { + + public static final String DEVICE_TYPE_TEST = "Test"; + + @Override + public String getProviderType() { + return TestDeviceManagerService.DEVICE_TYPE_TEST; + } + + @Override + public boolean enrollDevice(Device device) throws DeviceManagementException { + return false; + } + + @Override + public boolean modifyEnrollment(Device device) throws DeviceManagementException { + return false; + } + + @Override + public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + return false; + } + + @Override + public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { + return false; + } + + @Override + public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { + return false; + } + + @Override + public boolean setActive(DeviceIdentifier deviceId, boolean status) throws DeviceManagementException { + return false; + } + + @Override + public List getAllDevices() throws DeviceManagementException { + return null; + } + + @Override + public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + return null; + } + + @Override + public boolean updateDeviceInfo(Device device) throws DeviceManagementException { + return false; + } + + @Override + public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) throws DeviceManagementException { + return false; + } + + @Override + public OperationManager getOperationManager() throws DeviceManagementException { + return null; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestUtils.java new file mode 100644 index 000000000..a12560cc3 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestUtils.java @@ -0,0 +1,54 @@ +/** + * Copyright (c) 2012, 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.core; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class TestUtils { + + private static final Log log = LogFactory.getLog(TestUtils.class); + + public static void cleanupResources(Connection conn, Statement stmt, ResultSet rs) { + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing result set", e); + } + } + if (stmt != null) { + try { + stmt.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing prepared statement", e); + } + } + if (conn != null) { + try { + conn.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing database connection", e); + } + } + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceMgtDAOTestSuite.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOTests.java similarity index 62% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceMgtDAOTestSuite.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOTests.java index 98bb0d437..f41c60097 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceMgtDAOTestSuite.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOTests.java @@ -25,6 +25,7 @@ import org.testng.annotations.Parameters; import org.testng.annotations.Test; import org.w3c.dom.Document; import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.core.TestUtils; import org.wso2.carbon.device.mgt.core.common.DBTypes; import org.wso2.carbon.device.mgt.core.common.TestDBConfiguration; import org.wso2.carbon.device.mgt.core.common.TestDBConfigurations; @@ -40,42 +41,33 @@ import javax.xml.bind.Unmarshaller; import java.io.File; import java.sql.*; import java.util.Date; -import java.util.Iterator; -public class DeviceMgtDAOTestSuite { - - private TestDBConfiguration testDBConfiguration; - private DeviceType devType = new DeviceType(); - private Connection conn = null; - private Statement stmt = null; +public class DeviceManagementDAOTests { @BeforeClass @Parameters("dbType") public void setUpDB(String dbTypeStr) throws Exception { - DBTypes dbType = DBTypes.valueOf(dbTypeStr); - testDBConfiguration = getTestDBConfiguration(dbType); + TestDBConfiguration dbConfig = getTestDBConfiguration(dbType); switch (dbType) { - case H2: - createH2DB(testDBConfiguration); - BasicDataSource testDataSource = new BasicDataSource(); - testDataSource.setDriverClassName(testDBConfiguration.getDriverClass()); - testDataSource.setUrl(testDBConfiguration.getConnectionUrl()); - testDataSource.setUsername(testDBConfiguration.getUserName()); - testDataSource.setPassword(testDBConfiguration.getPwd()); - DeviceManagementDAOFactory.init(testDataSource); - default: + case H2: + createH2DB(dbConfig); + BasicDataSource testDataSource = new BasicDataSource(); + testDataSource.setDriverClassName(dbConfig.getDriverClass()); + testDataSource.setUrl(dbConfig.getConnectionUrl()); + testDataSource.setUsername(dbConfig.getUserName()); + testDataSource.setPassword(dbConfig.getPwd()); + DeviceManagementDAOFactory.init(testDataSource); + default: } } private TestDBConfiguration getTestDBConfiguration(DBTypes dbType) throws DeviceManagementDAOException, DeviceManagementException { - File deviceMgtConfig = new File("src/test/resources/testdbconfig.xml"); - Document doc = null; - testDBConfiguration = null; - TestDBConfigurations testDBConfigurations = null; + Document doc; + TestDBConfigurations dbConfigs; doc = DeviceManagerUtil.convertToDocument(deviceMgtConfig); JAXBContext testDBContext = null; @@ -83,92 +75,100 @@ public class DeviceMgtDAOTestSuite { try { testDBContext = JAXBContext.newInstance(TestDBConfigurations.class); Unmarshaller unmarshaller = testDBContext.createUnmarshaller(); - testDBConfigurations = (TestDBConfigurations) unmarshaller.unmarshal(doc); + dbConfigs = (TestDBConfigurations) unmarshaller.unmarshal(doc); } catch (JAXBException e) { throw new DeviceManagementDAOException("Error parsing test db configurations", e); } - Iterator itrDBConfigs = testDBConfigurations.getDbTypesList().iterator(); - while (itrDBConfigs.hasNext()) { - testDBConfiguration = itrDBConfigs.next(); - if (testDBConfiguration.getDbType().equals(dbType.toString())) { - break; + for (TestDBConfiguration config : dbConfigs.getDbTypesList()) { + if (config.getDbType().equals(dbType.toString())) { + return config; } } - - return testDBConfiguration; + return null; } private void createH2DB(TestDBConfiguration testDBConf) throws Exception { - - Class.forName(testDBConf.getDriverClass()); - conn = DriverManager.getConnection(testDBConf.getConnectionUrl()); - stmt = conn.createStatement(); - stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/CreateH2TestDB.sql'"); - stmt.close(); - conn.close(); - + Connection conn = null; + Statement stmt = null; + try { + Class.forName(testDBConf.getDriverClass()); + conn = DriverManager.getConnection(testDBConf.getConnectionUrl()); + stmt = conn.createStatement(); + stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/CreateH2TestDB.sql'"); + } finally { + TestUtils.cleanupResources(conn, stmt, null); + } } @Test public void addDeviceTypeTest() throws DeviceManagementDAOException, DeviceManagementException { - DeviceTypeDAO deviceTypeMgtDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); - devType.setName("IOS"); + DeviceType deviceType = new DeviceType(); + deviceType.setName("IOS"); - deviceTypeMgtDAO.addDeviceType(devType); + deviceTypeMgtDAO.addDeviceType(deviceType); Long deviceTypeId = null; + Connection conn = null; + Statement stmt = null; try { conn = DeviceManagementDAOFactory.getDataSource().getConnection(); stmt = conn.createStatement(); - ResultSet resultSet = stmt.executeQuery("SELECT ID,NAME from DM_DEVICE_TYPE DType where DType.NAME='IOS'"); + ResultSet resultSet = + stmt.executeQuery("SELECT ID,NAME from DM_DEVICE_TYPE DType where DType.NAME='IOS'"); while (resultSet.next()) { deviceTypeId = resultSet.getLong(1); } - conn.close(); } catch (SQLException sqlEx) { throw new DeviceManagementDAOException("error in fetch device type by name IOS", sqlEx); + } finally { + TestUtils.cleanupResources(conn, stmt, null); } Assert.assertNotNull(deviceTypeId, "Device Type Id is null"); - devType.setId(deviceTypeId); + deviceType.setId(deviceTypeId); } - @Test(dependsOnMethods = { "addDeviceTypeTest" }) + @Test(dependsOnMethods = {"addDeviceTypeTest"}) public void addDeviceTest() throws DeviceManagementDAOException, DeviceManagementException { - DeviceDAO deviceMgtDAO = DeviceManagementDAOFactory.getDeviceDAO(); Device device = new Device(); - device.setDateOfEnrollment(new Date().getTime()); device.setDateOfLastUpdate(new Date().getTime()); device.setDescription("test description"); device.setStatus(Status.ACTIVE); device.setDeviceIdentificationId("111"); - device.setDeviceType(devType.getId().intValue()); + + DeviceType deviceType = new DeviceType(); + deviceType.setId(Long.parseLong("1")); + + device.setDeviceType(deviceType.getId().intValue()); device.setOwnerShip(OwnerShip.BYOD.toString()); device.setOwnerId("111"); device.setTenantId(-1234); deviceMgtDAO.addDevice(device); Long deviceId = null; + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; try { conn = DeviceManagementDAOFactory.getDataSource().getConnection(); - stmt = conn.createStatement(); - ResultSet resultSet = stmt - .executeQuery("SELECT ID from DM_DEVICE DEVICE where DEVICE.DEVICE_IDENTIFICATION='111'"); + stmt = conn.prepareStatement("SELECT ID from DM_DEVICE DEVICE where DEVICE.DEVICE_IDENTIFICATION='111'"); + rs = stmt.executeQuery(); - while (resultSet.next()) { - deviceId = resultSet.getLong(1); + while (rs.next()) { + deviceId = rs.getLong(1); } - conn.close(); - } catch (SQLException sqlEx) { - throw new DeviceManagementDAOException("error in fetch device by device identification id", sqlEx); + } catch (SQLException e) { + throw new DeviceManagementDAOException("error in fetch device by device identification id", e); + } finally { + TestUtils.cleanupResources(conn, stmt, rs); } Assert.assertNotNull(deviceId, "Device Id is null"); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/malformed-cdm-config-no-ds-config.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/malformed-cdm-config-no-ds-config.xml new file mode 100644 index 000000000..a2a20ccd4 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/malformed-cdm-config-no-ds-config.xml @@ -0,0 +1,26 @@ + + + + + + + + jdbc/DEVICE_MGT_DS + + + + + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/malformed-cdm-config-no-jndi.config.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/malformed-cdm-config-no-jndi.config.xml new file mode 100644 index 000000000..5a4e934dc --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/malformed-cdm-config-no-jndi.config.xml @@ -0,0 +1,26 @@ + + + + + + + + jdbc/DEVICE_MGT_DS + + + + + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/malformed-cdm-config-no-mgt-repo.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/malformed-cdm-config-no-mgt-repo.xml new file mode 100644 index 000000000..6f2b60631 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/malformed-cdm-config-no-mgt-repo.xml @@ -0,0 +1,26 @@ + + + + + + + + jdbc/DEVICE_MGT_DS + + + + + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/schema/DeviceManagementConfigSchema.xsd b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/schema/DeviceManagementConfigSchema.xsd new file mode 100644 index 000000000..31337c426 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/schema/DeviceManagementConfigSchema.xsd @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml index 1bf0f08f8..2c5fe6fe7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml @@ -21,8 +21,9 @@ - - + + + \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom.xml index 6e4782972..8bec5af48 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom.xml @@ -52,7 +52,7 @@ ${project.artifactId} ${project.version} Device Management Mobile Impl Bundle - org.wso2.carbon.device.mgt.mobile.internal.MobileDeviceManagementBundleActivator + org.wso2.carbon.device.mgt.mobile.internal org.osgi.framework, diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/AbstractMobileOperationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/AbstractMobileOperationManager.java index 9bdb8c42f..03f5e8812 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/AbstractMobileOperationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/AbstractMobileOperationManager.java @@ -22,15 +22,15 @@ import java.util.List; public abstract class AbstractMobileOperationManager implements OperationManager { - @Override - public List getOperations(DeviceIdentifier deviceIdentifier) - throws OperationManagementException { - return null; - } + @Override + public List getOperations(DeviceIdentifier deviceIdentifier) throws OperationManagementException { + return null; + } + + @Override + public boolean addOperation(Operation operation, + List devices) throws OperationManagementException { + return true; + } - @Override - public boolean addOperation(Operation operation, List devices) throws - OperationManagementException { - return true; - } } \ 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/dao/MobileDeviceManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/MobileDeviceManagementDAOFactory.java index e60e0ec17..dc270e5ff 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/MobileDeviceManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/MobileDeviceManagementDAOFactory.java @@ -18,6 +18,7 @@ package org.wso2.carbon.device.mgt.mobile.dao; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.mobile.DataSourceListener; import org.wso2.carbon.device.mgt.mobile.config.datasource.MobileDataSourceConfig; import org.wso2.carbon.device.mgt.mobile.dao.impl.*; @@ -31,66 +32,68 @@ import javax.sql.DataSource; */ public class MobileDeviceManagementDAOFactory implements DataSourceListener { - private static DataSource dataSource; - private static MobileDataSourceConfig mobileDataSourceConfig; - private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOFactory.class); - - public MobileDeviceManagementDAOFactory() { - - } - - public void init(){ - dataSource = MobileDeviceManagementDAOUtil.resolveDataSource(mobileDataSourceConfig); - if(dataSource!=null){ - MobileDeviceManagementDAOUtil.createDataSource(dataSource); - }else{ - MobileDeviceManagementBundleActivator.registerDataSourceListener(this); - } - } - - public static MobileDeviceDAO getMobileDeviceDAO() { - return new MobileDeviceDAOImpl(dataSource); - } - - public static OperationDAO getOperationDAO(){ - return new OperationDAOImpl(dataSource); - } - - public static OperationPropertyDAO geOperationPropertyDAO(){ - return new OperationPropertyDAOImpl(dataSource); - } - - public static DeviceOperationDAO getDeviceOperationDAO(){ - return new DeviceOperationDAOImpl(dataSource); - } - - public static FeatureDAO getFeatureDAO(){ - return new FeatureDAOImpl(dataSource); - } - - public static FeaturePropertyDAO getFeaturePropertyDAO(){ - return new FeaturePropertyDAOImpl(dataSource); - } - - public static MobileDataSourceConfig getMobileDeviceManagementConfig() { - return mobileDataSourceConfig; - } - - public static void setMobileDataSourceConfig( - MobileDataSourceConfig mobileDataSourceConfig) { - MobileDeviceManagementDAOFactory.mobileDataSourceConfig = - mobileDataSourceConfig; - } - - public static DataSource getDataSource() { - return dataSource; - } - - @Override - public void notifyObserver() { - dataSource = MobileDeviceManagementDAOUtil.resolveDataSource(mobileDataSourceConfig); - if(dataSource!=null){ - MobileDeviceManagementDAOUtil.createDataSource(dataSource); - } - } + private static DataSource dataSource; + private static MobileDataSourceConfig mobileDataSourceConfig; + private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOFactory.class); + + public MobileDeviceManagementDAOFactory() { + + } + + public void init() throws DeviceManagementException { + dataSource = MobileDeviceManagementDAOUtil.resolveDataSource(mobileDataSourceConfig); + if (dataSource != null) { + MobileDeviceManagementDAOUtil.createDataSource(dataSource); + } else { + MobileDeviceManagementBundleActivator.registerDataSourceListener(this); + } + } + + public static MobileDeviceDAO getMobileDeviceDAO() { + return new MobileDeviceDAOImpl(dataSource); + } + + public static OperationDAO getOperationDAO() { + return new OperationDAOImpl(dataSource); + } + + public static OperationPropertyDAO geOperationPropertyDAO() { + return new OperationPropertyDAOImpl(dataSource); + } + + public static DeviceOperationDAO getDeviceOperationDAO() { + return new DeviceOperationDAOImpl(dataSource); + } + + public static FeatureDAO getFeatureDAO() { + return new FeatureDAOImpl(dataSource); + } + + public static FeaturePropertyDAO getFeaturePropertyDAO() { + return new FeaturePropertyDAOImpl(dataSource); + } + + public static MobileDataSourceConfig getMobileDeviceManagementConfig() { + return mobileDataSourceConfig; + } + + public static void setMobileDataSourceConfig( + MobileDataSourceConfig mobileDataSourceConfig) { + MobileDeviceManagementDAOFactory.mobileDataSourceConfig = + mobileDataSourceConfig; + } + + public static DataSource getDataSource() { + return dataSource; + } + + @Override + public void notifyObserver() { + try { + dataSource = MobileDeviceManagementDAOUtil.resolveDataSource(mobileDataSourceConfig); + MobileDeviceManagementDAOUtil.createDataSource(dataSource); + } catch (DeviceManagementException e) { + log.error("Error occurred while resolving mobile device management metadata repository data source", e); + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/util/MobileDeviceManagementDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/util/MobileDeviceManagementDAOUtil.java index e8ab4660d..146a33bcb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/util/MobileDeviceManagementDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/util/MobileDeviceManagementDAOUtil.java @@ -37,136 +37,119 @@ import java.util.List; */ public class MobileDeviceManagementDAOUtil { - 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 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; - } + /** + * 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) throws DeviceManagementException { + 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); + } + } + 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 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.lookup(dataSourceName); + } catch (Exception e) { + String msg = "Error in looking up data source: " + e.getMessage(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } + } - public static void cleanupResources(Connection conn, PreparedStatement stmt, ResultSet rs) { - if (rs != null) { - try { - rs.close(); - } catch (SQLException e) { - log.warn("Error occurred while closing result set", e); - } - } - if (stmt != null) { - try { - stmt.close(); - } catch (SQLException e) { - log.warn("Error occurred while closing prepared statement", e); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException e) { - log.warn("Error occurred while closing database connection", e); - } - } - } + public static void cleanupResources(Connection conn, PreparedStatement stmt, ResultSet rs) { + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing result set", e); + } + } + if (stmt != null) { + try { + stmt.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing prepared statement", e); + } + } + if (conn != null) { + try { + conn.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing database connection", e); + } + } + } - /** - * 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); - } - } - } + /** + * 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); + } + } + } - /** - * 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); - } - } + /** + * 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/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 b0feb6722..39499134c 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 @@ -47,7 +47,7 @@ public class AndroidDeviceManagerService implements DeviceManagerService { @Override public boolean enrollDevice(Device device) throws DeviceManagementException { - boolean status = false; + boolean status; MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device); try { status = MobileDeviceManagementDAOFactory.getMobileDeviceDAO().addDevice(mobileDevice); @@ -62,7 +62,7 @@ public class AndroidDeviceManagerService implements DeviceManagerService { @Override public boolean modifyEnrollment(Device device) throws DeviceManagementException { - boolean status = false; + boolean status; MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device); try { status = MobileDeviceManagementDAOFactory.getMobileDeviceDAO() @@ -78,7 +78,7 @@ public class AndroidDeviceManagerService implements DeviceManagerService { @Override public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - boolean status = false; + boolean status; try { status = MobileDeviceManagementDAOFactory.getMobileDeviceDAO() .deleteDevice(deviceId.getId()); @@ -122,7 +122,7 @@ public class AndroidDeviceManagerService implements DeviceManagerService { @Override public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - Device device = null; + Device device; try { MobileDevice mobileDevice = MobileDeviceManagementDAOFactory.getMobileDeviceDAO(). getDevice(deviceId.getId()); @@ -143,14 +143,13 @@ public class AndroidDeviceManagerService implements DeviceManagerService { @Override public boolean updateDeviceInfo(Device device) throws DeviceManagementException { - boolean status = false; + boolean status; MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device); try { status = MobileDeviceManagementDAOFactory.getMobileDeviceDAO() .updateDevice(mobileDevice); } catch (MobileDeviceManagementDAOException e) { - String msg = - "Error while updating the Android device : " + device.getDeviceIdentifier(); + String msg = "Error while updating the Android device : " + device.getDeviceIdentifier(); log.error(msg, e); throw new DeviceManagementException(msg, e); } @@ -166,9 +165,9 @@ public class AndroidDeviceManagerService implements DeviceManagerService { getAllDevices(); if (mobileDevices != null) { devices = new ArrayList(); - for (int x = 0; x < mobileDevices.size(); x++) { - devices.add(MobileDeviceManagementUtil.convertToDevice(mobileDevices.get(x))); - } + for (MobileDevice mobileDevice : mobileDevices) { + devices.add(MobileDeviceManagementUtil.convertToDevice(mobileDevice)); + } } } catch (MobileDeviceManagementDAOException e) { String msg = "Error while fetching all Android devices."; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/IOSDeviceManagerService.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/IOSDeviceManagerService.java index ebb6038b9..1265aea01 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/IOSDeviceManagerService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/IOSDeviceManagerService.java @@ -88,5 +88,4 @@ public class IOSDeviceManagerService implements DeviceManagerService { return true; } - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementBundleActivator.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementBundleActivator.java index 2d1fb04ad..4f2b8ff50 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementBundleActivator.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementBundleActivator.java @@ -105,7 +105,7 @@ public class MobileDeviceManagementBundleActivator implements BundleActivator, B services */ this.removeAPIs(); } catch (Throwable e) { - log.error("Error occurred while de-activating Mobile Device Management bundle"); + log.error("Error occurred while de-activating Mobile Device Management bundle", e); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementServiceComponent.java new file mode 100644 index 000000000..195929bb2 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementServiceComponent.java @@ -0,0 +1,165 @@ +/** + * Copyright (c) 2015, 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.internal; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; +import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.apimgt.api.APIManagementException; +import org.wso2.carbon.apimgt.api.APIProvider; +import org.wso2.carbon.apimgt.impl.APIManagerConfigurationService; +import org.wso2.carbon.apimgt.impl.APIManagerFactory; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService; +import org.wso2.carbon.device.mgt.mobile.config.APIConfig; +import org.wso2.carbon.device.mgt.mobile.config.MobileDeviceConfigurationManager; +import org.wso2.carbon.device.mgt.mobile.config.MobileDeviceManagementConfig; +import org.wso2.carbon.device.mgt.mobile.config.datasource.MobileDataSourceConfig; +import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOFactory; +import org.wso2.carbon.device.mgt.mobile.impl.android.AndroidDeviceManagerService; +import org.wso2.carbon.device.mgt.mobile.impl.ios.IOSDeviceManagerService; +import org.wso2.carbon.device.mgt.mobile.impl.windows.WindowsDeviceManagerService; +import org.wso2.carbon.device.mgt.mobile.util.DeviceManagementAPIPublisherUtil; + +import java.util.List; + +/** + * @scr.component name="org.wso2.carbon.device.mgt.mobile.impl.internal.MobileDeviceManagementServiceComponent" + * immediate="true" + * @scr.reference name="api.manager.config.service" + * interface="org.wso2.carbon.apimgt.impl.APIManagerConfigurationService" + * cardinality="1..1" + * policy="dynamic" + * bind="setAPIManagerConfigurationService" + * unbind="unsetAPIManagerConfigurationService" + *

+ * Adding reference to API Manager Configuration service is an unavoidable hack to get rid of NPEs thrown while + * initializing APIMgtDAOs attempting to register APIs programmatically. APIMgtDAO needs to be proper cleaned up + * to avoid as an ideal fix + */ +public class MobileDeviceManagementServiceComponent { + + private ServiceRegistration androidServiceRegRef; + private ServiceRegistration iOSServiceRegRef; + private ServiceRegistration windowsServiceRegRef; + + private static final Log log = LogFactory.getLog(MobileDeviceManagementServiceComponent.class); + + protected void activate(ComponentContext ctx) { + if (log.isDebugEnabled()) { + log.debug("Activating Mobile Device Management Service Component"); + } + try { + BundleContext bundleContext = ctx.getBundleContext(); + + /* Initialize the datasource configuration */ + MobileDeviceConfigurationManager.getInstance().initConfig(); + MobileDeviceManagementConfig config = MobileDeviceConfigurationManager.getInstance() + .getMobileDeviceManagementConfig(); + MobileDataSourceConfig dsConfig = + config.getMobileDeviceMgtRepository().getMobileDataSourceConfig(); + + MobileDeviceManagementDAOFactory.setMobileDataSourceConfig(dsConfig); + + 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); + + /* Initialize all API configurations with corresponding API Providers */ + this.initAPIConfigs(); + /* Publish all mobile device management related JAX-RS services as APIs */ + this.publishAPIs(); + + if (log.isDebugEnabled()) { + log.debug("Mobile Device Management Service Component has been successfully activated"); + } + } catch (Throwable e) { + log.error("Error occurred while activating Mobile Device Management Service Component", e); + } + } + + protected void deactivate(ComponentContext ctx) { + if (log.isDebugEnabled()) { + log.debug("De-activating Mobile Device Management Service Component"); + } + try { + BundleContext bundleContext = ctx.getBundleContext(); + + androidServiceRegRef.unregister(); + iOSServiceRegRef.unregister(); + windowsServiceRegRef.unregister(); + + /* Removing all APIs published upon start-up for mobile device management related JAX-RS + services */ + this.removeAPIs(); + if (log.isDebugEnabled()) { + log.debug("Mobile Device Management Service Component has been successfully de-activated"); + } + } catch (Throwable e) { + log.error("Error occurred while de-activating Mobile Device Management bundle", e); + } + } + + private void initAPIConfigs() throws DeviceManagementException { + List apiConfigs = + MobileDeviceConfigurationManager.getInstance().getMobileDeviceManagementConfig(). + getApiPublisherConfig().getAPIs(); + for (APIConfig apiConfig : apiConfigs) { + try { + APIProvider provider = APIManagerFactory.getInstance().getAPIProvider(apiConfig.getOwner()); + apiConfig.init(provider); + } catch (APIManagementException e) { + throw new DeviceManagementException("Error occurred while initializing API Config '" + + apiConfig.getName() + "'", e); + } + } + } + + private void publishAPIs() throws DeviceManagementException { + List apiConfigs = + MobileDeviceConfigurationManager.getInstance().getMobileDeviceManagementConfig(). + getApiPublisherConfig().getAPIs(); + for (APIConfig apiConfig : apiConfigs) { + DeviceManagementAPIPublisherUtil.publishAPI(apiConfig); + } + } + + private void removeAPIs() throws DeviceManagementException { + List apiConfigs = + MobileDeviceConfigurationManager.getInstance().getMobileDeviceManagementConfig(). + getApiPublisherConfig().getAPIs(); + for (APIConfig apiConfig : apiConfigs) { + DeviceManagementAPIPublisherUtil.removeAPI(apiConfig); + } + } + + protected void setAPIManagerConfigurationService(APIManagerConfigurationService service) { + //do nothing + } + + protected void unsetAPIManagerConfigurationService(APIManagerConfigurationService service) { + //do nothing + } + +} diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/conf/mobile-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/conf/mobile-config.xml index bac7bdd5c..0593de9a9 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/conf/mobile-config.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/conf/mobile-config.xml @@ -19,7 +19,7 @@ - jdbc/MOBILE_MGT_DS + jdbc/MobileDM_DS @@ -28,7 +28,7 @@ enrollment - admin + admin enrollment 1.0.0 http://localhost:9763/ diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml index 87176d638..49d19d69f 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml @@ -18,7 +18,7 @@ - jdbc/DEVICE_MGT_DS + jdbc/DM_DS diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql new file mode 100644 index 000000000..783acaa8d --- /dev/null +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -0,0 +1,26 @@ +CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE +( + ID INT(11) auto_increment NOT NULL, + NAME VARCHAR(300) NULL DEFAULT NULL, + PRIMARY KEY (ID) +); + +CREATE TABLE IF NOT EXISTS DM_DEVICE +( + ID INT auto_increment NOT NULL, + DESCRIPTION TEXT NULL DEFAULT NULL, + NAME VARCHAR(100) NULL DEFAULT NULL, + DATE_OF_ENROLLMENT BIGINT NULL DEFAULT NULL, + DATE_OF_LAST_UPDATE BIGINT NULL DEFAULT NULL, + OWNERSHIP VARCHAR(45) NULL DEFAULT NULL, + STATUS VARCHAR(15) NULL DEFAULT NULL, + DEVICE_TYPE_ID INT(11) NULL DEFAULT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NULL DEFAULT NULL, + OWNER VARCHAR(45) NULL DEFAULT NULL, + TENANT_ID INTEGER DEFAULT 0, + PRIMARY KEY (ID), + CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_ID ) + REFERENCES DM_DEVICE_TYPE (ID ) ON DELETE NO ACTION ON UPDATE NO ACTION +); +-- TO:DO - Remove this INSERT sql statement. +Insert into DM_DEVICE_TYPE (ID,NAME) VALUES (1, 'android'); diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql new file mode 100644 index 000000000..bc04de873 --- /dev/null +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -0,0 +1,35 @@ +-- ----------------------------------------------------- +-- Table `DM_DEVICE_TYPE` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `DM_DEVICE_TYPE` ( + `ID` INT(11) NOT NULL , + `NAME` VARCHAR(300) NULL DEFAULT NULL , + PRIMARY KEY (`ID`) ) +ENGINE = InnoDB +DEFAULT CHARACTER SET = latin1; + + +-- ----------------------------------------------------- +-- Table `DM_DEVICE` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `DM_DEVICE` ( + `ID` VARCHAR(20) NOT NULL , + `DESCRIPTION` TEXT NULL DEFAULT NULL , + `NAME` VARCHAR(100) NULL DEFAULT NULL , + `DATE_OF_ENROLLMENT` DATETIME NULL DEFAULT NULL , + `DATE_OF_LAST_UPDATE` DATETIME NULL DEFAULT NULL , + `OWNERSHIP` VARCHAR(45) NULL DEFAULT NULL , + `STATUS` VARCHAR(15) NULL DEFAULT NULL , + `DEVICE_TYPE_ID` INT(11) NULL DEFAULT NULL , + `DEVICE_IDENTIFICATION` VARCHAR(300) NULL DEFAULT NULL , + `OWNER` VARCHAR(45) NULL DEFAULT NULL , + TENANT_ID INTEGER DEFAULT 0, + PRIMARY KEY (`ID`) , + INDEX `fk_DM_DEVICE_DM_DEVICE_TYPE2_idx` (`DEVICE_TYPE_ID` ASC) , + CONSTRAINT `fk_DM_DEVICE_DM_DEVICE_TYPE2` + FOREIGN KEY (`DEVICE_TYPE_ID` ) + REFERENCES `DM_DEVICE_TYPE` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB +DEFAULT CHARACTER SET = latin1; diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf index b5222094b..eabcf094d 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf @@ -1,3 +1,3 @@ instructions.configure = \ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/conf/cdm-config.xml,target:${installFolder}/../../conf/cdm-config.xml,overwrite:true);\ - +org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/dbscripts/cdm,target:${installFolder}/../../../dbscripts/cdm,overwrite:true);\ diff --git a/product/modules/distribution/pom.xml b/product/modules/distribution/pom.xml index 8fbfc24aa..c7ac210bf 100644 --- a/product/modules/distribution/pom.xml +++ b/product/modules/distribution/pom.xml @@ -98,6 +98,40 @@ maven-antrun-plugin + + + create-device-mgt-schema + package + + run + + + + + + + + + + + + + + + + + + + + + create-idp-mgt-schema @@ -132,6 +166,40 @@ + + + create-api-mgt-schema + package + + run + + + + + + + + + + + + + + + + + + + + + 3-extract-docs-from-components package diff --git a/product/modules/distribution/src/assembly/bin.xml b/product/modules/distribution/src/assembly/bin.xml index ac4ba28cd..0209ac3aa 100644 --- a/product/modules/distribution/src/assembly/bin.xml +++ b/product/modules/distribution/src/assembly/bin.xml @@ -187,6 +187,17 @@ + + + + ../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/dbscripts/apimgt + + wso2cdm-${project.version}/dbscripts/apimgt + + */** + + + ../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/repository/modules @@ -197,10 +208,10 @@ - + - src/repository/dbscripts/ - wso2cdm-${project.version}/dbscripts + ../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/dbscripts/cdm + wso2cdm-${project.version}/dbscripts/cdm */** @@ -220,6 +231,7 @@ 755 + wso2cdm-${project.version}/lib/endorsed @@ -238,7 +250,6 @@ - ../agents/android/jax-rs/target/cdm-android-api.war wso2cdm-${pom.version}/repository/deployment/server/webapps @@ -429,6 +440,25 @@ 644 + + + + target/wso2carbon-core-${carbon.kernel.version}/repository/database/WSO2AM_DB.h2.db + + ${pom.artifactId}-${pom.version}/repository/database + WSO2AM_DB.h2.db + 644 + + + + + + target/wso2carbon-core-${carbon.kernel.version}/repository/database/WSO2DM_DB.h2.db + + ${pom.artifactId}-${pom.version}/repository/database + WSO2DM_DB.h2.db + 644 + diff --git a/product/modules/distribution/src/repository/conf/datasources/cdm-datasources.xml b/product/modules/distribution/src/repository/conf/datasources/cdm-datasources.xml index 0797b85da..7d35742b6 100755 --- a/product/modules/distribution/src/repository/conf/datasources/cdm-datasources.xml +++ b/product/modules/distribution/src/repository/conf/datasources/cdm-datasources.xml @@ -5,14 +5,14 @@ - DEVICE_MGT_DS + DM_DS The datasource used for CDM - jdbc/DEVICE_MGT_DS + jdbc/DM_DS - jdbc:h2:repository/database/WSO2DEVICEMGT_DB;DB_CLOSE_ON_EXIT=FALSE + jdbc:h2:repository/database/WSO2DM_DB;DB_CLOSE_ON_EXIT=FALSE wso2carbon wso2carbon org.h2.Driver @@ -25,14 +25,14 @@ - MOBILE_MGT_DS + MobileDM_DS The datasource used for CDM Mobile Device Management - jdbc/MOBILE_MGT_DS + jdbc/MobileDM_DS - jdbc:h2:repository/database/WSO2MOBILE_DB;DB_CLOSE_ON_EXIT=FALSE + jdbc:h2:repository/database/WSO2MobileDM_DB;DB_CLOSE_ON_EXIT=FALSE wso2carbon wso2carbon org.h2.Driver @@ -45,7 +45,7 @@ - WSO2DEVICE_DB + WSO2AM_DS The datasource used for CDM jdbc/WSO2AM_DB diff --git a/product/modules/integration/tests-common/ui-pages/pom.xml b/product/modules/integration/tests-common/ui-pages/pom.xml index a26af8b05..2906ce7f4 100644 --- a/product/modules/integration/tests-common/ui-pages/pom.xml +++ b/product/modules/integration/tests-common/ui-pages/pom.xml @@ -35,11 +35,48 @@ WSO2 CDM - Integration Test Ui Pages - org.wso2.carbon org.wso2.carbon.integration.common.admin.client compile + + + org.wso2.carbon + org.wso2.carbon.user.mgt.stub + + + org.wso2.carbon + org.wso2.carbon.logging.view.stub + + + org.wso2.carbon + org.wso2.carbon.ndatasource.stub + + + org.wso2.carbon + org.wso2.carbon.server.admin.stub + + + org.wso2.carbon + org.wso2.carbon.throttle.stub + + + org.wso2.carbon + org.wso2.carbon.tenant.mgt.stub + + + org.wso2.carbon + org.wso2.carbon.application.mgt.stub + + + org.wso2.carbon + org.wso2.carbon.security.mgt.stub + + + org.wso2.carbon + org.wso2.carbon.identity.user.profile.stub + + org.wso2.carbon.automation