From a5018afadf0936aa4d8f764a849217a316e83788 Mon Sep 17 00:00:00 2001 From: harshanL Date: Mon, 15 Dec 2014 18:43:56 +0530 Subject: [PATCH 1/6] Making the plugin resolution optional for the core component --- .../DeviceManagementServiceComponent.java | 2 +- .../MobileDeviceConfigurationManager.java | 73 ++++++++++++++++++ .../config/MobileDeviceManagementConfig.java | 38 ++++++++++ .../MobileDeviceManagementRepository.java | 40 ++++++++++ .../datasource/JNDILookupDefinition.java | 76 +++++++++++++++++++ .../datasource/MobileDataSourceConfig.java | 39 ++++++++++ .../dao/MobileDeviceManagementDAOFactory.java | 7 ++ ...mpl.java => MobileDeviceModelDAOImpl.java} | 14 +++- ...bileDeviceManagementSchemaInitializer.java | 44 +++++++++++ .../impl/util/MobileDeviceManagerUtil.java | 7 ++ .../src/main/resources/conf/mobile-config.xml | 0 .../repository/dbscripts/cdm/plugins/h2.sql | 0 .../dbscripts/cdm/plugins/mysql.sql | 63 +++++++++++++++ 13 files changed, 401 insertions(+), 2 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceConfigurationManager.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceManagementConfig.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceManagementRepository.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/datasource/JNDILookupDefinition.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/datasource/MobileDataSourceConfig.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceManagementDAOFactory.java rename components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/{MobileDeviceModelImpl.java => MobileDeviceModelDAOImpl.java} (78%) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagementSchemaInitializer.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/MobileDeviceManagerUtil.java create mode 100644 features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/conf/mobile-config.xml create mode 100644 product/modules/distribution/src/repository/dbscripts/cdm/plugins/h2.sql create mode 100644 product/modules/distribution/src/repository/dbscripts/cdm/plugins/mysql.sql 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 85ae3ec4e5..102ca5fb70 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 @@ -41,7 +41,7 @@ import org.wso2.carbon.user.core.service.RealmService; * unbind="unsetRealmService" * @scr.reference name="device.manager.service" * interface="org.wso2.carbon.device.mgt.common.spi.DeviceManagerService" - * cardinality="1..n" + * cardinality="0..n" * policy="dynamic" * bind="setDeviceManagerService" * unbind="unsetDeviceManagerService" 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 new file mode 100644 index 0000000000..3038387c63 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceConfigurationManager.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2. + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.mgt.mobile.impl.config; + +import org.w3c.dom.Document; +import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; +import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; +import org.wso2.carbon.utils.CarbonUtils; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Unmarshaller; +import java.io.File; + +/** + * Class responsible for the mobile device manager configuration initialization + */ +public class MobileDeviceConfigurationManager { + + private MobileDeviceManagementConfig currentDeviceConfig; + private static MobileDeviceConfigurationManager deviceConfigManager; + + private final String deviceMgtConfigXMLPath = CarbonUtils.getCarbonConfigDirPath() + File.separator + + DeviceManagementConstants.DataSourceProperties.DEVICE_CONFIG_XML_NAME; + + public static MobileDeviceConfigurationManager getInstance() { + if (deviceConfigManager == null) { + synchronized (MobileDeviceConfigurationManager.class) { + if (deviceConfigManager == null) { + deviceConfigManager = new MobileDeviceConfigurationManager(); + } + } + } + return deviceConfigManager; + } + + public synchronized void initConfig() throws DeviceManagementException { + try { + File deviceMgtConfig = new File(deviceMgtConfigXMLPath); + Document doc = DeviceManagerUtil.convertToDocument(deviceMgtConfig); + + /* Un-marshaling Device Management configuration */ + JAXBContext rssContext = JAXBContext.newInstance(MobileDeviceManagementConfig.class); + Unmarshaller unmarshaller = rssContext.createUnmarshaller(); + this.currentDeviceConfig = (MobileDeviceManagementConfig) unmarshaller.unmarshal(doc); + } catch (Exception e) { + throw new DeviceManagementException("Error occurred while initializing RSS config", e); + } + } + + public MobileDeviceManagementConfig getDeviceManagementConfig() { + return currentDeviceConfig; + } + + public DataSourceConfig getDataSourceConfig() { + return currentDeviceConfig.getMobileDeviceMgtRepository().getDataSourceConfig(); + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceManagementConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceManagementConfig.java new file mode 100644 index 0000000000..0c9ff1a1b8 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceManagementConfig.java @@ -0,0 +1,38 @@ +/* + * 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.config; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * Represents Device Mgt configuration. + */ +@XmlRootElement(name = "MobileDeviceMgtConfiguration") +public final class MobileDeviceManagementConfig { + + private DeviceManagementRepository deviceMgtRepository; + + @XmlElement(name = "ManagementRepository", nillable = false) + public DeviceManagementRepository getDeviceMgtRepository() { + return deviceMgtRepository; + } + + public void setDeviceMgtRepository(DeviceManagementRepository deviceMgtRepository) { + this.deviceMgtRepository = deviceMgtRepository; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceManagementRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceManagementRepository.java new file mode 100644 index 0000000000..e115d96cf9 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceManagementRepository.java @@ -0,0 +1,40 @@ +/* + * 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.config; + +import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * Class for holding management repository data + */ +@XmlRootElement(name = "ManagementRepository") +public class MobileDeviceManagementRepository { + + private DataSourceConfig dataSourceConfig; + + @XmlElement(name = "DataSourceConfiguration", nillable = false) + public DataSourceConfig getDataSourceConfig() { + return dataSourceConfig; + } + + public void setDataSourceConfig(DataSourceConfig dataSourceConfig) { + this.dataSourceConfig = dataSourceConfig; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/datasource/JNDILookupDefinition.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/datasource/JNDILookupDefinition.java new file mode 100644 index 0000000000..6eadecd50b --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/datasource/JNDILookupDefinition.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2. + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.mgt.mobile.impl.config.datasource; + +import javax.xml.bind.annotation.*; +import java.util.List; + +/** + * Class for hold JndiLookupDefinition of rss-manager.xml at parsing with JAXB + */ +@XmlRootElement(name = "JndiLookupDefinition") +public class JNDILookupDefinition { + + private String jndiName; + private List jndiProperties; + + @XmlElement(name = "Name", nillable = false) + public String getJndiName() { + return jndiName; + } + + public void setJndiName(String jndiName) { + this.jndiName = jndiName; + } + + @XmlElementWrapper(name = "Environment", nillable = false) + @XmlElement(name = "Property", nillable = false) + public List getJndiProperties() { + return jndiProperties; + } + + public void setJndiProperties(List jndiProperties) { + this.jndiProperties = jndiProperties; + } + + @XmlRootElement(name = "Property") + public static class JNDIProperty { + + private String name; + + private String value; + + @XmlAttribute(name = "Name") + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @XmlValue + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + } + +} + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/datasource/MobileDataSourceConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/datasource/MobileDataSourceConfig.java new file mode 100644 index 0000000000..a3e3f6ae0b --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/datasource/MobileDataSourceConfig.java @@ -0,0 +1,39 @@ +/* + * 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.config.datasource; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * Class for holding data source configuration in mobile-config.xml at parsing with JAXB + */ +@XmlRootElement(name = "DataSourceConfiguration") +public class MobileDataSourceConfig { + + private JNDILookupDefinition jndiLookupDefintion; + + @XmlElement(name = "JndiLookupDefinition", nillable = true) + public JNDILookupDefinition getJndiLookupDefintion() { + return jndiLookupDefintion; + } + + public void setJndiLookupDefintion(JNDILookupDefinition jndiLookupDefintion) { + this.jndiLookupDefintion = jndiLookupDefintion; + } + +} 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 new file mode 100644 index 0000000000..dbc129a4e5 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceManagementDAOFactory.java @@ -0,0 +1,7 @@ +package org.wso2.carbon.device.mgt.mobile.impl.dao; + +/** + * Created by harshan on 12/15/14. + */ +public class MobileDeviceManagementDAOFactory { +} 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/MobileDeviceModelImpl.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 similarity index 78% rename from components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceModelImpl.java rename to components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceModelDAOImpl.java index 67179794cd..04dbd9c654 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/impl/MobileDeviceModelImpl.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 @@ -16,14 +16,26 @@ 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 MobileDeviceModelImpl implements MobileDeviceModelDAO { +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 { 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 new file mode 100644 index 0000000000..e6d41a89aa --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagementSchemaInitializer.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2. + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.mgt.mobile.impl.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; +import org.wso2.carbon.utils.CarbonUtils; +import org.wso2.carbon.utils.dbcreator.DatabaseCreator; + +import java.io.File; + +public final class MobileDeviceManagementSchemaInitializer extends DatabaseCreator { + + private static final Log log = LogFactory.getLog(DeviceManagementSchemaInitializer.class); + private static final String setupSQLScriptBaseLocation = + CarbonUtils.getCarbonHome() + File.separator + "dbscripts" + File.separator + "cdm" + File.separator; + + public MobileDeviceManagementSchemaInitializer(DataSourceConfig config) { + super(DeviceManagerUtil.resolveDataSource(config)); + } + + protected String getDbScriptLocation(String databaseType) { + String scriptName = databaseType + ".sql"; + if (log.isDebugEnabled()) { + log.debug("Loading database script from :" + scriptName); + } + return setupSQLScriptBaseLocation.replaceFirst("DBTYPE", databaseType) + scriptName; + } + +} 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 new file mode 100644 index 0000000000..d876b9ccb6 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/util/MobileDeviceManagerUtil.java @@ -0,0 +1,7 @@ +package org.wso2.carbon.device.mgt.mobile.impl.util; + +/** + * Created by harshan on 12/15/14. + */ +public class MobileDeviceManagerUtil { +} 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 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/product/modules/distribution/src/repository/dbscripts/cdm/plugins/h2.sql b/product/modules/distribution/src/repository/dbscripts/cdm/plugins/h2.sql new file mode 100644 index 0000000000..e69de29bb2 diff --git a/product/modules/distribution/src/repository/dbscripts/cdm/plugins/mysql.sql b/product/modules/distribution/src/repository/dbscripts/cdm/plugins/mysql.sql new file mode 100644 index 0000000000..45207b39ac --- /dev/null +++ b/product/modules/distribution/src/repository/dbscripts/cdm/plugins/mysql.sql @@ -0,0 +1,63 @@ +-- ----------------------------------------------------- +-- 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` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `MBL_DEVICE` ( + `MOBILE_DEVICE_ID` VARCHAR(45) NOT NULL, + `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 `mydb`.`MBL_OS_VERSION` (`VERSION_ID`) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `fk_DEVICE_DEVICE_MODEL2` + FOREIGN KEY (`DEVICE_MODEL_ID`) + REFERENCES `mydb`.`MBL_DEVICE_MODEL` (`MODEL_ID`) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `fk_DEVICE_VENDOR1` + FOREIGN KEY (`VENDOR_ID`) + REFERENCES `mydb`.`MBL_VENDOR` (`VENDOR_ID`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB; + + From 4a419f3ebf3d670b0625896bc41a7decdf34aa6e Mon Sep 17 00:00:00 2001 From: harshanL Date: Mon, 15 Dec 2014 20:09:39 +0530 Subject: [PATCH 2/6] Added mobile device management related db classes --- .../mgt/common/DeviceManagementConstants.java | 2 +- .../dao/util/DeviceManagementDAOUtil.java | 8 +- .../pom.xml | 5 +- .../android/AndroidDeviceManagerService.java | 2 + .../MobileDeviceConfigurationManager.java | 95 ++++++++-------- .../config/MobileDeviceManagementConfig.java | 13 +-- .../MobileDeviceManagementRepository.java | 12 +-- .../datasource/JNDILookupDefinition.java | 23 ++-- .../mgt/mobile/impl/dao/MobileDeviceDAO.java | 3 +- .../MobileDeviceManagementDAOException.java | 14 +-- .../dao/MobileDeviceManagementDAOFactory.java | 99 ++++++++++++++++- .../impl/dao/impl/MobileDeviceDAOImpl.java | 12 +++ .../dao/impl/MobileDeviceVendorDAOImpl.java | 12 +++ .../impl/dao/impl/MobileOSVersionDAOImpl.java | 12 +++ .../util/MobileDeviceManagementDAOUtil.java | 59 ++++++++++ ...MobileDeviceManagementBundleActivator.java | 102 ++++++++++++------ ...bileDeviceManagementSchemaInitializer.java | 54 +++++----- .../impl/util/MobileDeviceManagerUtil.java | 84 +++++++++++++++ .../src/main/resources/conf/mobile-config.xml | 26 +++++ .../src/main/resources/p2.inf | 2 + .../src/main/java/cdm/api/android/Device.java | 9 ++ .../main/java/cdm/api/android/Enrollment.java | 12 +++ .../modules/distribution/src/assembly/bin.xml | 9 ++ .../conf/datasources/cdm-datasources.xml | 22 +++- .../repository/dbscripts/cdm/plugins/h2.sql | 38 +++++++ .../dbscripts/cdm/plugins/mysql.sql | 6 +- 26 files changed, 593 insertions(+), 142 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java index 0649a9d6a3..9802fe30fe 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java @@ -34,7 +34,7 @@ public final class DeviceManagementConstants { public static final String SECURE_VAULT_NS = "http://org.wso2.securevault/configuration"; } - public static class MobileDeviceTypes { + public static final class MobileDeviceTypes { private MobileDeviceTypes() { throw new AssertionError(); } 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 b71aaeaa15..0fa7f34af8 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 @@ -109,11 +109,15 @@ public final class DeviceManagementDAOUtil { deviceBO.setName(device.getName()); deviceBO.setDateOfEnrollment(device.getDateOfEnrolment()); deviceBO.setDateOfLastUpdate(device.getDateOfLastUpdate()); - deviceBO.setStatus(Status.valueOf(String.valueOf(device.isStatus()))); + if (!device.isStatus()){ + deviceBO.setStatus(Status.INACTIVE); + }else{ + deviceBO.setStatus(Status.ACTIVE); + } deviceBO.setOwnerId(device.getOwner()); deviceBO.setOwnerShip(device.getOwnership()); deviceBO.setTenantId(DeviceManagementDAOUtil.getTenantId()); - deviceBO.setDeviceType(Integer.parseInt(device.getType())); + //deviceBO.setDeviceType(Integer.parseInt(device.getType())); return deviceBO; } 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 8dd8ef138c..510ee66d72 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 @@ -74,10 +74,13 @@ org.wso2.carbon org.wso2.carbon.logging + + org.wso2.carbon + org.wso2.carbon.utils + org.eclipse.osgi org.eclipse.osgi.services - 3.2.0.v20090520-1800 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 3246b21f21..805345e743 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 @@ -86,4 +86,6 @@ public class AndroidDeviceManagerService implements DeviceManagerService { return true; } + //should implement equals and hashcode in all service bundles + } 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 3038387c63..1a24811996 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 @@ -1,25 +1,25 @@ /* * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. + * + * 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. - * 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. + * + * 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.config; import org.w3c.dom.Document; -import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; -import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; +import org.wso2.carbon.device.mgt.mobile.impl.util.MobileDeviceManagerUtil; +import org.wso2.carbon.device.mgt.mobile.impl.config.datasource.MobileDataSourceConfig; import org.wso2.carbon.utils.CarbonUtils; import javax.xml.bind.JAXBContext; @@ -31,43 +31,46 @@ import java.io.File; */ public class MobileDeviceConfigurationManager { - private MobileDeviceManagementConfig currentDeviceConfig; - private static MobileDeviceConfigurationManager deviceConfigManager; + private static final String MOBILE_DEVICE_CONFIG_XML_NAME = "mobile-config.xml"; + private MobileDeviceManagementConfig currentMobileDeviceConfig; + private static MobileDeviceConfigurationManager mobileDeviceConfigManager; - private final String deviceMgtConfigXMLPath = CarbonUtils.getCarbonConfigDirPath() + File.separator + - DeviceManagementConstants.DataSourceProperties.DEVICE_CONFIG_XML_NAME; + private final String mobileDeviceMgtConfigXMLPath = + CarbonUtils.getCarbonConfigDirPath() + File.separator + + MOBILE_DEVICE_CONFIG_XML_NAME; - public static MobileDeviceConfigurationManager getInstance() { - if (deviceConfigManager == null) { - synchronized (MobileDeviceConfigurationManager.class) { - if (deviceConfigManager == null) { - deviceConfigManager = new MobileDeviceConfigurationManager(); - } - } - } - return deviceConfigManager; - } + public static MobileDeviceConfigurationManager getInstance() { + if (mobileDeviceConfigManager == null) { + synchronized (MobileDeviceConfigurationManager.class) { + if (mobileDeviceConfigManager == null) { + mobileDeviceConfigManager = new MobileDeviceConfigurationManager(); + } + } + } + return mobileDeviceConfigManager; + } - public synchronized void initConfig() throws DeviceManagementException { - try { - File deviceMgtConfig = new File(deviceMgtConfigXMLPath); - Document doc = DeviceManagerUtil.convertToDocument(deviceMgtConfig); + public synchronized void initConfig() throws DeviceManagementException { + try { + File mobileDeviceMgtConfig = new File(mobileDeviceMgtConfigXMLPath); + Document doc = MobileDeviceManagerUtil.convertToDocument(mobileDeviceMgtConfig); + JAXBContext mobileDeviceMgmtContext = + JAXBContext.newInstance(MobileDeviceManagementConfig.class); + Unmarshaller unmarshaller = mobileDeviceMgmtContext.createUnmarshaller(); + this.currentMobileDeviceConfig = + (MobileDeviceManagementConfig) unmarshaller.unmarshal(doc); + } catch (Exception e) { + throw new DeviceManagementException( + "Error occurred while initializing Mobile Device Management config", e); + } + } - /* Un-marshaling Device Management configuration */ - JAXBContext rssContext = JAXBContext.newInstance(MobileDeviceManagementConfig.class); - Unmarshaller unmarshaller = rssContext.createUnmarshaller(); - this.currentDeviceConfig = (MobileDeviceManagementConfig) unmarshaller.unmarshal(doc); - } catch (Exception e) { - throw new DeviceManagementException("Error occurred while initializing RSS config", e); - } - } + public MobileDeviceManagementConfig getMobileDeviceManagementConfig() { + return currentMobileDeviceConfig; + } - public MobileDeviceManagementConfig getDeviceManagementConfig() { - return currentDeviceConfig; - } - - public DataSourceConfig getDataSourceConfig() { - return currentDeviceConfig.getMobileDeviceMgtRepository().getDataSourceConfig(); - } + public MobileDataSourceConfig getMobileDataSourceConfig() { + return currentMobileDeviceConfig.getMobileDeviceMgtRepository().getMobileDataSourceConfig(); + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceManagementConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceManagementConfig.java index 0c9ff1a1b8..203245e5bd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceManagementConfig.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceManagementConfig.java @@ -19,20 +19,21 @@ import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; /** - * Represents Device Mgt configuration. + * Represents Mobile Device Mgt configuration. */ @XmlRootElement(name = "MobileDeviceMgtConfiguration") public final class MobileDeviceManagementConfig { - private DeviceManagementRepository deviceMgtRepository; + private MobileDeviceManagementRepository mobileDeviceMgtRepository; @XmlElement(name = "ManagementRepository", nillable = false) - public DeviceManagementRepository getDeviceMgtRepository() { - return deviceMgtRepository; + public MobileDeviceManagementRepository getMobileDeviceMgtRepository() { + return mobileDeviceMgtRepository; } - public void setDeviceMgtRepository(DeviceManagementRepository deviceMgtRepository) { - this.deviceMgtRepository = deviceMgtRepository; + public void setMobileDeviceMgtRepository( + MobileDeviceManagementRepository mobileDeviceMgtRepository) { + this.mobileDeviceMgtRepository = mobileDeviceMgtRepository; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceManagementRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceManagementRepository.java index e115d96cf9..9987d1763d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceManagementRepository.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/MobileDeviceManagementRepository.java @@ -15,7 +15,7 @@ */ package org.wso2.carbon.device.mgt.mobile.impl.config; -import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; +import org.wso2.carbon.device.mgt.mobile.impl.config.datasource.MobileDataSourceConfig; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @@ -26,15 +26,15 @@ import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "ManagementRepository") public class MobileDeviceManagementRepository { - private DataSourceConfig dataSourceConfig; + private MobileDataSourceConfig mobileDataSourceConfig; @XmlElement(name = "DataSourceConfiguration", nillable = false) - public DataSourceConfig getDataSourceConfig() { - return dataSourceConfig; + public MobileDataSourceConfig getMobileDataSourceConfig() { + return mobileDataSourceConfig; } - public void setDataSourceConfig(DataSourceConfig dataSourceConfig) { - this.dataSourceConfig = dataSourceConfig; + public void setMobileDataSourceConfig(MobileDataSourceConfig mobileDataSourceConfig) { + this.mobileDataSourceConfig = mobileDataSourceConfig; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/datasource/JNDILookupDefinition.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/datasource/JNDILookupDefinition.java index 6eadecd50b..94d62a1d26 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/datasource/JNDILookupDefinition.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/config/datasource/JNDILookupDefinition.java @@ -1,16 +1,17 @@ /* * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. + * + * 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. - * 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. + * + * 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.config.datasource; @@ -19,7 +20,7 @@ import javax.xml.bind.annotation.*; import java.util.List; /** - * Class for hold JndiLookupDefinition of rss-manager.xml at parsing with JAXB + * Class for hold JndiLookupDefinition of mobile-config.xml at parsing with JAXB */ @XmlRootElement(name = "JndiLookupDefinition") public class JNDILookupDefinition { 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 1d4ab3ab80..16330df10c 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 @@ -19,7 +19,8 @@ package org.wso2.carbon.device.mgt.mobile.impl.dao; import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileDevice; /** - * This class represents the key operations associated with persisting mobile-device related information. + * This class represents the key operations associated with persisting mobile-device related + * information. */ public interface MobileDeviceDAO { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceManagementDAOException.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceManagementDAOException.java index 8047d932a2..d0d03fcacf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceManagementDAOException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceManagementDAOException.java @@ -25,9 +25,10 @@ public class MobileDeviceManagementDAOException extends Exception { private static final long serialVersionUID = 2021891706072918865L; /** - * Constructs a new exception with the specified detail message and nested exception. + * Constructs a new MobileDeviceManagementDAOException with the specified detail message and + * nested exception. * - * @param message error message + * @param message error message * @param nestedException exception */ public MobileDeviceManagementDAOException(String message, Exception nestedException) { @@ -36,7 +37,8 @@ public class MobileDeviceManagementDAOException extends Exception { } /** - * Constructs a new exception with the specified detail message and cause. + * Constructs a new MobileDeviceManagementDAOException with the specified detail message + * and cause. * * @param message the detail message. * @param cause the cause of this exception. @@ -47,7 +49,7 @@ public class MobileDeviceManagementDAOException extends Exception { } /** - * Constructs a new exception with the specified detail message + * Constructs a new MobileDeviceManagementDAOException with the specified detail message * * @param message the detail message. */ @@ -57,9 +59,9 @@ public class MobileDeviceManagementDAOException extends Exception { } /** - * Constructs a new exception with the specified and cause. + * Constructs a new MobileDeviceManagementDAOException with the specified and cause. * - * @param cause the cause of this exception. + * @param cause the cause of this exception. */ public MobileDeviceManagementDAOException(Throwable cause) { super(cause); 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 dbc129a4e5..65750447d5 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 @@ -1,7 +1,104 @@ +/* + * 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.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.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 javax.sql.DataSource; +import java.util.Hashtable; +import java.util.List; + /** - * Created by harshan on 12/15/14. + * Factory class used to create MobileDeviceManagement related DAO objects. */ public class MobileDeviceManagementDAOFactory { + + private static DataSource dataSource; + private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOFactory.class); + + 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); + } + + public static void init(MobileDataSourceConfig config) { + dataSource = resolveDataSource(config); + } + + public static void init(DataSource dtSource) { + dataSource = dtSource; + } + + /** + * 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); + } + } + return dataSource; + } + + public static DataSource getDataSource() { + return 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/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 cdd18e69c1..d0ab883d69 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 @@ -16,14 +16,26 @@ 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.MobileDeviceDAO; import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceManagementDAOException; import org.wso2.carbon.device.mgt.mobile.impl.dto.MobileDevice; +import javax.sql.DataSource; + /** * Implementation of MobileDeviceDAO. */ public class MobileDeviceDAOImpl implements MobileDeviceDAO { + + private DataSource dataSource; + private static final Log log = LogFactory.getLog(MobileDeviceDAOImpl.class); + + public MobileDeviceDAOImpl(DataSource dataSource) { + this.dataSource = dataSource; + } + @Override public MobileDevice getDevice(String deviceId) throws MobileDeviceManagementDAOException { return null; 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 index 0f4659d97b..3908ef3ba0 100644 --- 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 @@ -16,14 +16,26 @@ 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 { 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 index 6bfaccad0c..4296ec974a 100644 --- 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 @@ -16,14 +16,26 @@ 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; 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 453a3918d0..eee8f09538 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,8 +16,67 @@ package org.wso2.carbon.device.mgt.mobile.impl.dao.util; +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 javax.naming.InitialContext; +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Hashtable; + /** * Utility method required by MobileDeviceManagement DAO classes. */ public class MobileDeviceManagementDAOUtil { + + private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOUtil.class); + + 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 DataSource lookupDataSource(String dataSourceName, + final Hashtable jndiProperties) { + 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 RuntimeException("Error in looking up data source: " + e.getMessage(), e); + } + } + + public static MobileDevice convertToMobileDevice(Device device) + throws MobileDeviceManagementDAOException { + MobileDevice mobileDeviceBO = new MobileDevice(); + return mobileDeviceBO; + } } 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 e614119492..67cf1873af 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 @@ -21,9 +21,15 @@ import org.apache.commons.logging.Log; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; +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.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.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; public class MobileDeviceManagementBundleActivator implements BundleActivator { @@ -33,37 +39,71 @@ public class MobileDeviceManagementBundleActivator implements BundleActivator { private ServiceRegistration iOSServiceRegRef; private ServiceRegistration windowsServiceRegRef; - @Override - public void start(BundleContext bundleContext) throws Exception { - try { - if (log.isDebugEnabled()) { - log.debug("Activating Mobile Device Management Service bundle"); - } - 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 start(BundleContext bundleContext) throws Exception { + try { + //Initialize Mobile Device Management datasource + MobileDeviceConfigurationManager.getInstance().initConfig(); + MobileDeviceManagementConfig config = MobileDeviceConfigurationManager.getInstance() + .getMobileDeviceManagementConfig(); + MobileDataSourceConfig + dsConfig = config.getMobileDeviceMgtRepository().getMobileDataSourceConfig(); + MobileDeviceManagementDAOFactory.init(dsConfig); - @Override - public void stop(BundleContext bundleContext) throws Exception { - if (log.isDebugEnabled()) { - log.debug("Deactivating Mobile Device Management Service"); - } - androidServiceRegRef.unregister(); - iOSServiceRegRef.unregister(); - windowsServiceRegRef.unregister(); - } + /* 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(dsConfig); + } + + if (log.isDebugEnabled()) { + log.debug("Activating Mobile Device Management Service bundle"); + } + 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); + } + } + + 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); + } + } + + @Override + public void stop(BundleContext bundleContext) throws Exception { + if (log.isDebugEnabled()) { + log.debug("Deactivating Mobile Device Management Service"); + } + androidServiceRegRef.unregister(); + iOSServiceRegRef.unregister(); + windowsServiceRegRef.unregister(); + } } 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 e6d41a89aa..f62b09cc10 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 @@ -1,23 +1,24 @@ /* * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. + * + * 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. - * 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. + * + * 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.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; +import org.wso2.carbon.device.mgt.mobile.impl.config.datasource.MobileDataSourceConfig; import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.dbcreator.DatabaseCreator; @@ -25,20 +26,21 @@ import java.io.File; public final class MobileDeviceManagementSchemaInitializer extends DatabaseCreator { - private static final Log log = LogFactory.getLog(DeviceManagementSchemaInitializer.class); - private static final String setupSQLScriptBaseLocation = - CarbonUtils.getCarbonHome() + File.separator + "dbscripts" + File.separator + "cdm" + File.separator; - - public MobileDeviceManagementSchemaInitializer(DataSourceConfig config) { - super(DeviceManagerUtil.resolveDataSource(config)); - } - - protected String getDbScriptLocation(String databaseType) { - String scriptName = databaseType + ".sql"; - if (log.isDebugEnabled()) { - log.debug("Loading database script from :" + scriptName); - } - return setupSQLScriptBaseLocation.replaceFirst("DBTYPE", databaseType) + scriptName; - } + 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"; + + public MobileDeviceManagementSchemaInitializer(MobileDataSourceConfig config) { + super(MobileDeviceManagerUtil.resolveDataSource(config)); + } + + protected String getDbScriptLocation(String databaseType) { + String scriptName = databaseType + ".sql"; + if (log.isDebugEnabled()) { + log.debug("Loading database script from :" + scriptName); + } + return setupSQLScriptBaseLocation.replaceFirst("DBTYPE", databaseType) + scriptName; + } } 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 index d876b9ccb6..c5b9940fb6 100644 --- 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 @@ -1,7 +1,91 @@ +/* + * 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/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 e69de29bb2..e44dcfaf4e 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 @@ -0,0 +1,26 @@ + + + + + + + + jdbc/WSO2MOBILE_DB + + + + \ No newline at end of file diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/p2.inf b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/p2.inf index e69de29bb2..f410404d26 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/p2.inf +++ b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/p2.inf @@ -0,0 +1,2 @@ +instructions.configure = \ +org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.mobile_${feature.version}/conf/mobile-config.xml,target:${installFolder}/../../conf/mobile-config.xml,overwrite:true);\ \ No newline at end of file diff --git a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Device.java b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Device.java index ae2e805dd2..dd7db5e989 100644 --- a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Device.java +++ b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Device.java @@ -50,6 +50,9 @@ public class Device { } finally { PrivilegedCarbonContext.endTenantFlow(); } + if(log.isDebugEnabled()){ + log.debug("Get all devices"); + } try { if (dmService != null) { result = dmService.getAllDevices( @@ -85,6 +88,9 @@ public class Device { DeviceManagementService dmService; org.wso2.carbon.device.mgt.common.Device device = new org.wso2.carbon.device.mgt.common.Device(); + if(log.isDebugEnabled()){ + log.debug("Get device id : " + id); + } try { dmService = AndroidAPIUtils.getDeviceManagementService(); } finally { @@ -130,6 +136,9 @@ public class Device { } finally { PrivilegedCarbonContext.endTenantFlow(); } + if(log.isDebugEnabled()){ + log.debug("Update device id : " + id + ", payload : " + jsonPayload); + } org.wso2.carbon.device.mgt.common.Device device = AndroidAPIUtils.convertToDeviceObject(jsonPayload); try { diff --git a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java index 7a7b3bd29a..3bc2124dd0 100644 --- a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java +++ b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java @@ -48,6 +48,9 @@ public class Enrollment { } finally { PrivilegedCarbonContext.endTenantFlow(); } + if(log.isDebugEnabled()){ + log.debug("Device Enroll >>> " + jsonPayload); + } Device device = AndroidAPIUtils.convertToDeviceObject(jsonPayload); try { if (dmService != null) { @@ -86,6 +89,9 @@ public class Enrollment { } finally { PrivilegedCarbonContext.endTenantFlow(); } + if(log.isDebugEnabled()){ + log.debug("Device is enrolled >>> " + id); + } DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id); try { if (dmService != null) { @@ -124,6 +130,9 @@ public class Enrollment { } finally { PrivilegedCarbonContext.endTenantFlow(); } + if(log.isDebugEnabled()){ + log.debug("Modify Enrollment >>> id : " + id + ", payload : " + jsonPayload); + } Device device = AndroidAPIUtils.convertToDeviceObject(jsonPayload); try { if (dmService != null) { @@ -162,6 +171,9 @@ public class Enrollment { } finally { PrivilegedCarbonContext.endTenantFlow(); } + if(log.isDebugEnabled()){ + log.debug("DisenrollDevice >>> id : " + id ); + } DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id); try { if (dmService != null) { diff --git a/product/modules/distribution/src/assembly/bin.xml b/product/modules/distribution/src/assembly/bin.xml index be12ea55a1..1372d48f2b 100644 --- a/product/modules/distribution/src/assembly/bin.xml +++ b/product/modules/distribution/src/assembly/bin.xml @@ -328,6 +328,15 @@ 644 + + + ../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/repository/conf/mobile-config.xml + + ${pom.artifactId}-${pom.version}/repository/conf + true + 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 e24759f81b..7b02df9cdf 100755 --- a/product/modules/distribution/src/repository/conf/datasources/cdm-datasources.xml +++ b/product/modules/distribution/src/repository/conf/datasources/cdm-datasources.xml @@ -6,7 +6,7 @@ WSO2DEVICE_DB - The datasource used for EMM + The datasource used for CDM jdbc/WSO2DEVICE_DB @@ -24,5 +24,25 @@ + + WSO2MOBILE_DB + The datasource used for CDM Mobile Device Management + + jdbc/WSO2MOBILE_DB + + + + jdbc:h2:repository/database/WSO2MOBILE_DB;DB_CLOSE_ON_EXIT=FALSE + wso2carbon + wso2carbon + org.h2.Driver + 50 + 60000 + true + SELECT 1 + 30000 + + + 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 e69de29bb2..f36a034bd5 100644 --- a/product/modules/distribution/src/repository/dbscripts/cdm/plugins/h2.sql +++ b/product/modules/distribution/src/repository/dbscripts/cdm/plugins/h2.sql @@ -0,0 +1,38 @@ +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 +); \ 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 45207b39ac..9c57710484 100644 --- a/product/modules/distribution/src/repository/dbscripts/cdm/plugins/mysql.sql +++ b/product/modules/distribution/src/repository/dbscripts/cdm/plugins/mysql.sql @@ -45,17 +45,17 @@ CREATE TABLE IF NOT EXISTS `MBL_DEVICE` ( INDEX `fk_DEVICE_VENDOR1_idx` (`VENDOR_ID` ASC), CONSTRAINT `fk_DEVICE_OS_VERSION1` FOREIGN KEY (`OS_VERSION_ID`) - REFERENCES `mydb`.`MBL_OS_VERSION` (`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 `mydb`.`MBL_DEVICE_MODEL` (`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 `mydb`.`MBL_VENDOR` (`VENDOR_ID`) + REFERENCES `MBL_VENDOR` (`VENDOR_ID`) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB; From bd5971d26338c9c245af7811387d59028fa51b98 Mon Sep 17 00:00:00 2001 From: Geeth Munasinghe Date: Mon, 15 Dec 2014 20:11:19 +0530 Subject: [PATCH 3/6] Comminying policy management related code --- .../{spi => }/FeatureManagerService.java | 2 +- .../{spi => }/PolicyManagerService.java | 2 +- .../mgt/common/impl/PolicyManagement.java | 2 +- .../config/PolicyConfigurationManager.java | 74 +++++++++++ .../core/config/PolicyManagementConfig.java | 37 ++++++ .../config/PolicyManagementRepository.java | 39 ++++++ .../PolicyManagementServiceComponent.java | 84 ++++++++++++ .../internal/PolicyMgtServiceComponent.java | 25 ---- .../core/util/PolicyManagementConstants.java | 24 ++++ .../mgt/core/util/PolicyManagerUtil.java | 85 ++++++++++++ components/policy-mgt/pom.xml | 20 +++ .../pom.xml | 124 ++++++++++++++++++ .../src/main/resources/build.properties | 1 + .../src/main/resources/p2.inf | 0 features/policy-mgt/pom.xml | 43 ++++++ pom.xml | 6 + .../modules/distribution/src/assembly/bin.xml | 8 -- product/modules/p2-profile-gen/pom.xml | 12 ++ 18 files changed, 552 insertions(+), 36 deletions(-) rename components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/{spi => }/FeatureManagerService.java (96%) rename components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/{spi => }/PolicyManagerService.java (98%) create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/config/PolicyConfigurationManager.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/config/PolicyManagementConfig.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/config/PolicyManagementRepository.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java delete mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyMgtServiceComponent.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java create mode 100644 features/policy-mgt/org.wso2.carbon.policy.mgt.server.feature/pom.xml create mode 100644 features/policy-mgt/org.wso2.carbon.policy.mgt.server.feature/src/main/resources/build.properties create mode 100644 features/policy-mgt/org.wso2.carbon.policy.mgt.server.feature/src/main/resources/p2.inf create mode 100644 features/policy-mgt/pom.xml diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/FeatureManagerService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/FeatureManagerService.java similarity index 96% rename from components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/FeatureManagerService.java rename to components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/FeatureManagerService.java index 6ed982cd2e..3737303e5a 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/FeatureManagerService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/FeatureManagerService.java @@ -17,7 +17,7 @@ */ -package org.wso2.carbon.policy.mgt.common.spi; +package org.wso2.carbon.policy.mgt.common; import org.wso2.carbon.policy.mgt.common.Feature; import org.wso2.carbon.policy.mgt.common.FeatureManagementException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyManagerService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyManagerService.java similarity index 98% rename from components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyManagerService.java rename to components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyManagerService.java index ae55266618..0e9605b348 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyManagerService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyManagerService.java @@ -16,7 +16,7 @@ * under the License. */ -package org.wso2.carbon.policy.mgt.common.spi; +package org.wso2.carbon.policy.mgt.common; import org.wso2.carbon.policy.mgt.common.FeatureManagementException; import org.wso2.carbon.policy.mgt.common.Policy; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/impl/PolicyManagement.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/impl/PolicyManagement.java index 6b2a9ee33f..c528dd504e 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/impl/PolicyManagement.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/impl/PolicyManagement.java @@ -21,7 +21,7 @@ package org.wso2.carbon.policy.mgt.common.impl; import org.wso2.carbon.policy.mgt.common.FeatureManagementException; import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; -import org.wso2.carbon.policy.mgt.common.spi.PolicyManagerService; +import org.wso2.carbon.policy.mgt.common.PolicyManagerService; public class PolicyManagement implements PolicyManagerService { @Override diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/config/PolicyConfigurationManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/config/PolicyConfigurationManager.java new file mode 100644 index 0000000000..97fe7c0796 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/config/PolicyConfigurationManager.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2. + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.policy.mgt.core.config; + +import org.w3c.dom.Document; +import org.wso2.carbon.policy.mgt.common.PolicyManagementException; +import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig; +import org.wso2.carbon.policy.mgt.core.util.PolicyManagementConstants; +import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; +import org.wso2.carbon.utils.CarbonUtils; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Unmarshaller; +import java.io.File; + +/** + * Class responsible for the rss manager configuration initialization + */ +public class PolicyConfigurationManager { + + private PolicyManagementConfig currentPolicyConfig; + private static PolicyConfigurationManager policyConfigurationManager; + + private final String deviceMgtConfigXMLPath = CarbonUtils.getCarbonConfigDirPath() + File.separator + + PolicyManagementConstants.DEVICE_CONFIG_XML_NAME; + + public static PolicyConfigurationManager getInstance() { + if (policyConfigurationManager == null) { + synchronized (PolicyConfigurationManager.class) { + if (policyConfigurationManager == null) { + policyConfigurationManager = new PolicyConfigurationManager(); + } + } + } + return policyConfigurationManager; + } + + public synchronized void initConfig() throws PolicyManagementException { + try { + File deviceMgtConfig = new File(deviceMgtConfigXMLPath); + Document doc = PolicyManagerUtil.convertToDocument(deviceMgtConfig); + + /* Un-marshaling Device Management configuration */ + JAXBContext rssContext = JAXBContext.newInstance(PolicyManagementConfig.class); + Unmarshaller unmarshaller = rssContext.createUnmarshaller(); + this.currentPolicyConfig = (PolicyManagementConfig) unmarshaller.unmarshal(doc); + } catch (Exception e) { + throw new PolicyManagementException("Error occurred while initializing RSS config", e); + } + } + + public PolicyManagementConfig getDeviceManagementConfig() { + return currentPolicyConfig; + } + + public DataSourceConfig getDataSourceConfig() { + return currentPolicyConfig.getPolicyManagementRepository().getDataSourceConfig(); + } + + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/config/PolicyManagementConfig.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/config/PolicyManagementConfig.java new file mode 100644 index 0000000000..6d3adbd3b7 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/config/PolicyManagementConfig.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2. + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.carbon.policy.mgt.core.config; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * Represents Device Mgt configuration. + */ +@XmlRootElement(name = "DeviceMgtConfiguration") +public final class PolicyManagementConfig { + + private PolicyManagementRepository policyManagementRepository; + + @XmlElement(name = "ManagementRepository", nillable = false) + public PolicyManagementRepository getPolicyManagementRepository() { + return policyManagementRepository; + } + + public void setPolicyMgtRepository(PolicyManagementRepository policyManagementRepository) { + this.policyManagementRepository = policyManagementRepository; + } + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/config/PolicyManagementRepository.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/config/PolicyManagementRepository.java new file mode 100644 index 0000000000..e7d8623515 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/config/PolicyManagementRepository.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2. + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.carbon.policy.mgt.core.config; + +import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * Class for holding management repository data + */ +@XmlRootElement(name = "ManagementRepository") +public class PolicyManagementRepository { + + private DataSourceConfig dataSourceConfig; + + @XmlElement(name = "DataSourceConfiguration", nillable = false) + public DataSourceConfig getDataSourceConfig() { + return dataSourceConfig; + } + + public void setDataSourceConfig(DataSourceConfig dataSourceConfig) { + this.dataSourceConfig = dataSourceConfig; + } + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java new file mode 100644 index 0000000000..d349bc362d --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java @@ -0,0 +1,84 @@ +/** + * 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.policy.mgt.core.internal; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.policy.mgt.core.config.PolicyConfigurationManager; +import org.wso2.carbon.policy.mgt.core.config.PolicyManagementConfig; +import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig; +import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; +import org.wso2.carbon.user.core.service.RealmService; + +/** + * @scr.component name="org.wso2.carbon.policy.manager" immediate="true" + * @scr.reference name="user.realmservice.default" + * interface="org.wso2.carbon.user.core.service.RealmService" + * cardinality="1..1" + * policy="dynamic" + * bind="setRealmService" + * unbind="unsetRealmService" + */ + +public class PolicyManagementServiceComponent { + + private static Log log = LogFactory.getLog(PolicyManagementServiceComponent.class); + + protected void activate(ComponentContext componentContext) { + + try { + + PolicyConfigurationManager.getInstance().initConfig(); + + PolicyManagementConfig config = PolicyConfigurationManager.getInstance().getDeviceManagementConfig(); + + DataSourceConfig dsConfig = config.getPolicyManagementRepository().getDataSourceConfig(); + PolicyManagementDAOFactory.init(dsConfig); + + + } catch (Throwable t) { + + } + } + + + /** + * Sets Realm Service + * + * @param realmService An instance of RealmService + */ + protected void setRealmService(RealmService realmService) { + + if (log.isDebugEnabled()) { + log.debug("Setting Realm Service"); + } + PolicyManagementDataHolder.getInstance().setRealmService(realmService); + } + + /** + * Unsets Realm Service + * + * @param realmService An instance of RealmService + */ + protected void unsetRealmService(RealmService realmService) { + if (log.isDebugEnabled()) { + log.debug("Unsetting Realm Service"); + } + PolicyManagementDataHolder.getInstance().setRealmService(null); + } + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyMgtServiceComponent.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyMgtServiceComponent.java deleted file mode 100644 index b66e04262e..0000000000 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyMgtServiceComponent.java +++ /dev/null @@ -1,25 +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.policy.mgt.core.internal; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -public class PolicyMgtServiceComponent { - - private static Log log = LogFactory.getLog(PolicyMgtServiceComponent.class); - -} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java new file mode 100644 index 0000000000..22883e5e63 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java @@ -0,0 +1,24 @@ +/* +* Copyright (c) 2014 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you under the Apache License, +* Version 2.0 (the "License"); you may not use this file except +* in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.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.policy.mgt.core.util; + +public final class PolicyManagementConstants { + + public static final String DEVICE_CONFIG_XML_NAME = "cdm-config.xml"; +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java new file mode 100644 index 0000000000..5276012687 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -0,0 +1,85 @@ +/* +* Copyright (c) 2014 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you under the Apache License, +* Version 2.0 (the "License"); you may not use this file except +* in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.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.policy.mgt.core.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.w3c.dom.Document; +import org.wso2.carbon.policy.mgt.common.PolicyManagementException; +import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig; +import org.wso2.carbon.policy.mgt.core.config.datasource.JNDILookupDefinition; +import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; + +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; + +public class PolicyManagerUtil { + + private static final Log log = LogFactory.getLog(PolicyManagerUtil.class); + + public static Document convertToDocument(File file) throws PolicyManagementException { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + try { + DocumentBuilder docBuilder = factory.newDocumentBuilder(); + return docBuilder.parse(file); + } catch (Exception e) { + throw new PolicyManagementException("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(DataSourceConfig 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 = + PolicyManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), jndiProperties); + } else { + dataSource = PolicyManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), null); + } + } + return dataSource; + } +} diff --git a/components/policy-mgt/pom.xml b/components/policy-mgt/pom.xml index 1c6633acd5..53a5d3d0c7 100644 --- a/components/policy-mgt/pom.xml +++ b/components/policy-mgt/pom.xml @@ -62,4 +62,24 @@ + + + + + org.apache.felix + maven-scr-plugin + 1.7.2 + + + generate-scr-scrdescriptor + + scr + + + + + + + + diff --git a/features/policy-mgt/org.wso2.carbon.policy.mgt.server.feature/pom.xml b/features/policy-mgt/org.wso2.carbon.policy.mgt.server.feature/pom.xml new file mode 100644 index 0000000000..1d272a1512 --- /dev/null +++ b/features/policy-mgt/org.wso2.carbon.policy.mgt.server.feature/pom.xml @@ -0,0 +1,124 @@ + + + + + + + org.wso2.carbon + policy-mgt-feature + 2.0.0-SNAPSHOT + ../pom.xml + + + 4.0.0 + org.wso2.carbon.policy.mgt.server.feature + pom + 2.0.0-SNAPSHOT + WSO2 Carbon - Policy Management Server Feature + http://wso2.org + This feature contains the core bundles required for Back-end Devvice Management functionality + + + + + org.eclipse.osgi + org.eclipse.osgi + + + org.eclipse.equinox + org.eclipse.equinox.common + + + org.wso2.carbon + org.wso2.carbon.logging + + + org.wso2.carbon + org.wso2.carbon.policy.mgt.core + + + org.wso2.carbon + org.wso2.carbon.policy.mgt.common + + + + + + + maven-resources-plugin + 2.6 + + + copy-resources + generate-resources + + copy-resources + + + src/main/resources + + + resources + + build.properties + p2.inf + + + + + + + + + org.wso2.maven + carbon-p2-plugin + ${carbon.p2.plugin.version} + + + p2-feature-generation + package + + p2-feature-gen + + + org.wso2.carbon.policy.mgt.server + ../../../features/etc/feature.properties + + + org.wso2.carbon.p2.category.type:server + org.eclipse.equinox.p2.type.group:false + + + + org.wso2.carbon:org.wso2.carbon.policy.mgt.core:${project.version} + + org.wso2.carbon:org.wso2.carbon.policy.mgt.common:${project.version} + + + + org.wso2.carbon.core.server:${carbon.platform.version} + + + + + + + + + diff --git a/features/policy-mgt/org.wso2.carbon.policy.mgt.server.feature/src/main/resources/build.properties b/features/policy-mgt/org.wso2.carbon.policy.mgt.server.feature/src/main/resources/build.properties new file mode 100644 index 0000000000..9c86577d76 --- /dev/null +++ b/features/policy-mgt/org.wso2.carbon.policy.mgt.server.feature/src/main/resources/build.properties @@ -0,0 +1 @@ +custom = true diff --git a/features/policy-mgt/org.wso2.carbon.policy.mgt.server.feature/src/main/resources/p2.inf b/features/policy-mgt/org.wso2.carbon.policy.mgt.server.feature/src/main/resources/p2.inf new file mode 100644 index 0000000000..e69de29bb2 diff --git a/features/policy-mgt/pom.xml b/features/policy-mgt/pom.xml new file mode 100644 index 0000000000..55fb61daac --- /dev/null +++ b/features/policy-mgt/pom.xml @@ -0,0 +1,43 @@ + + + + + + + org.wso2.carbon + wso2cdm-parent + 2.0.0-SNAPSHOT + ../../pom.xml + + + 4.0.0 + org.wso2.carbon + policy-mgt-feature + 2.0.0-SNAPSHOT + pom + WSO2 Carbon - Policy Management Feature + http://wso2.org + + + org.wso2.carbon.policy.mgt.server.feature + + + diff --git a/pom.xml b/pom.xml index fead5b6f57..8f324b9b5b 100644 --- a/pom.xml +++ b/pom.xml @@ -40,6 +40,7 @@ components/device-mgt components/policy-mgt features/device-mgt + features/policy-mgt product @@ -121,6 +122,11 @@ org.wso2.carbon.policy.mgt.common ${project.version} + + org.wso2.carbon + org.wso2.carbon.policy.mgt.core + ${project.version} + diff --git a/product/modules/distribution/src/assembly/bin.xml b/product/modules/distribution/src/assembly/bin.xml index be12ea55a1..ea630866f1 100644 --- a/product/modules/distribution/src/assembly/bin.xml +++ b/product/modules/distribution/src/assembly/bin.xml @@ -338,14 +338,6 @@ 644 - - - diff --git a/product/modules/p2-profile-gen/pom.xml b/product/modules/p2-profile-gen/pom.xml index 5418904a50..e2e880ee2e 100644 --- a/product/modules/p2-profile-gen/pom.xml +++ b/product/modules/p2-profile-gen/pom.xml @@ -110,6 +110,12 @@ org.wso2.carbon:org.wso2.carbon.device.mgt.mobile.feature:${project.version} + + + + org.wso2.carbon:org.wso2.carbon.policy.mgt.server.feature:${project.version} + + org.wso2.carbon:org.wso2.carbon.webapp.mgt.feature:${carbon.platform.version} @@ -168,6 +174,12 @@ org.wso2.carbon.device.mgt.mobile.feature.group ${project.version} + + + org.wso2.carbon.policy.mgt.server.feature.group + ${project.version} + + org.wso2.carbon.webapp.mgt.feature.group ${carbon.platform.version} From 95f72f77f1a86f8f7e92b8cceafd63b40374dcbb Mon Sep 17 00:00:00 2001 From: harshanL Date: Mon, 15 Dec 2014 20:20:11 +0530 Subject: [PATCH 4/6] Commented MobileActivator jndi lookup code --- .../MobileDeviceManagementBundleActivator.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) 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 67cf1873af..54c0c9f229 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 @@ -43,12 +43,12 @@ public class MobileDeviceManagementBundleActivator implements BundleActivator { public void start(BundleContext bundleContext) throws Exception { try { //Initialize Mobile Device Management datasource - MobileDeviceConfigurationManager.getInstance().initConfig(); - MobileDeviceManagementConfig config = MobileDeviceConfigurationManager.getInstance() - .getMobileDeviceManagementConfig(); - MobileDataSourceConfig - dsConfig = config.getMobileDeviceMgtRepository().getMobileDataSourceConfig(); - MobileDeviceManagementDAOFactory.init(dsConfig); +// MobileDeviceConfigurationManager.getInstance().initConfig(); +// MobileDeviceManagementConfig config = MobileDeviceConfigurationManager.getInstance() +// .getMobileDeviceManagementConfig(); +// MobileDataSourceConfig +// dsConfig = config.getMobileDeviceMgtRepository().getMobileDataSourceConfig(); +// MobileDeviceManagementDAOFactory.init(dsConfig); /* If -Dsetup option enabled then create device management database schema */ String setupOption = System.getProperty("setup"); @@ -58,7 +58,7 @@ public class MobileDeviceManagementBundleActivator implements BundleActivator { "-Dsetup is enabled. Mobile Device management repository schema initialization is about " + "to begin"); } - setupMobileDeviceManagementSchema(dsConfig); + setupMobileDeviceManagementSchema(null); } if (log.isDebugEnabled()) { From a69fc9dc4cf6a1e2a03e7241a611d29a99692a06 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Mon, 15 Dec 2014 20:52:38 +0530 Subject: [PATCH 5/6] Fixing issues spotted as a result of the bundle getting activated before the datasources used underneath are registered in JNDI --- .../mgt/mobile/impl/DataSourceListener.java | 22 +++++++ .../impl/dao/MobileDeviceDAOFactory.java | 61 +++++++++++++++++++ .../util/MobileDeviceManagementDAOUtil.java | 7 +++ ...MobileDeviceManagementBundleActivator.java | 48 ++++++++++++--- 4 files changed, 130 insertions(+), 8 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/DataSourceListener.java create 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 diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/DataSourceListener.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/DataSourceListener.java new file mode 100644 index 0000000000..61c881745f --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/DataSourceListener.java @@ -0,0 +1,22 @@ +/** + * 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; + +public interface DataSourceListener { + + void notifyObserver(); + +} 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 new file mode 100644 index 0000000000..0de3bd431c --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/dao/MobileDeviceDAOFactory.java @@ -0,0 +1,61 @@ +/** + * 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.MobileDeviceModelImpl; +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 DataSource dataSource; + + public MobileDeviceDAOFactory() { + MobileDeviceManagementBundleActivator.registerDataSourceListener(this); + } + + @Override + public void notifyObserver() { + this.dataSource = MobileDeviceManagementDAOUtil.resolveDataSource(); + } + + private DataSource getDataSource() { + return dataSource; + } + + public static MobileDeviceDAO getMobileDeviceDAO() { + return new MobileDeviceDAOImpl(); + } + + public static MobileDeviceModelDAO getMobileDeviceModelDAO() { + return new MobileDeviceModelImpl(); + } + + public static MobileDeviceVendorDAO getMobileDeviceVendorDAO() { + return new MobileDeviceVendorDAOImpl(); + } + + public static MobileOSVersionDAO getMobileOSVersionDAO() { + return new MobileOSVersionDAOImpl(); + } + +} 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 453a3918d0..efb4ceff0d 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,8 +16,15 @@ package org.wso2.carbon.device.mgt.mobile.impl.dao.util; +import javax.sql.DataSource; + /** * Utility method required by MobileDeviceManagement DAO classes. */ public class MobileDeviceManagementDAOUtil { + + public static DataSource resolveDataSource() { + return null; + } + } 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 e614119492..e8dc1c1ac4 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 @@ -18,20 +18,26 @@ package org.wso2.carbon.device.mgt.mobile.impl.internal; import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.Log; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceRegistration; +import org.osgi.framework.*; 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.dao.MobileDeviceDAOFactory; import org.wso2.carbon.device.mgt.mobile.impl.ios.IOSDeviceManagerService; import org.wso2.carbon.device.mgt.mobile.impl.windows.WindowsDeviceManagerService; -public class MobileDeviceManagementBundleActivator implements BundleActivator { +import java.util.ArrayList; +import java.util.List; - private static final Log log = LogFactory.getLog(MobileDeviceManagementBundleActivator.class); - private ServiceRegistration androidServiceRegRef; - private ServiceRegistration iOSServiceRegRef; - private ServiceRegistration windowsServiceRegRef; +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 { @@ -39,6 +45,11 @@ public class MobileDeviceManagementBundleActivator implements BundleActivator { if (log.isDebugEnabled()) { log.debug("Activating Mobile Device Management Service bundle"); } + bundleContext.addBundleListener(this); + + MobileDeviceDAOFactory daoFactory = new MobileDeviceDAOFactory(); + //TODO Register this dao to an appropriate config file + androidServiceRegRef = bundleContext.registerService(DeviceManagerService.class.getName(), new AndroidDeviceManagerService(), null); @@ -64,6 +75,27 @@ public class MobileDeviceManagementBundleActivator implements BundleActivator { 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; } } From 674735d9db8506ed291cde9ff83b3aaec1710789 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Mon, 15 Dec 2014 21:06:51 +0530 Subject: [PATCH 6/6] code cleanup --- .../impl/dao/MobileDeviceDAOFactory.java | 18 ++- .../util/MobileDeviceManagementDAOUtil.java | 9 +- ...MobileDeviceManagementBundleActivator.java | 111 +++++------------- 3 files changed, 38 insertions(+), 100 deletions(-) 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 index 0de3bd431c..fabb81d8d3 100644 --- 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 @@ -17,7 +17,7 @@ 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.MobileDeviceModelImpl; +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; @@ -27,7 +27,7 @@ import javax.sql.DataSource; public class MobileDeviceDAOFactory implements DataSourceListener { - private DataSource dataSource; + private static DataSource dataSource; public MobileDeviceDAOFactory() { MobileDeviceManagementBundleActivator.registerDataSourceListener(this); @@ -35,27 +35,23 @@ public class MobileDeviceDAOFactory implements DataSourceListener { @Override public void notifyObserver() { - this.dataSource = MobileDeviceManagementDAOUtil.resolveDataSource(); - } - - private DataSource getDataSource() { - return dataSource; + dataSource = MobileDeviceManagementDAOUtil.resolveDataSource(); } public static MobileDeviceDAO getMobileDeviceDAO() { - return new MobileDeviceDAOImpl(); + return new MobileDeviceDAOImpl(dataSource); } public static MobileDeviceModelDAO getMobileDeviceModelDAO() { - return new MobileDeviceModelImpl(); + return new MobileDeviceModelDAOImpl(dataSource); } public static MobileDeviceVendorDAO getMobileDeviceVendorDAO() { - return new MobileDeviceVendorDAOImpl(); + return new MobileDeviceVendorDAOImpl(dataSource); } public static MobileOSVersionDAO getMobileOSVersionDAO() { - return new MobileOSVersionDAOImpl(); + 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/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 571dc62420..bc210f8ffa 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,9 +16,9 @@ package org.wso2.carbon.device.mgt.mobile.impl.dao.util; -<<<<<<< HEAD + import javax.sql.DataSource; -======= + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; @@ -32,19 +32,16 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Hashtable; ->>>>>>> 95f72f77f1a86f8f7e92b8cceafd63b40374dcbb /** * Utility method required by MobileDeviceManagement DAO classes. */ public class MobileDeviceManagementDAOUtil { -<<<<<<< HEAD public static DataSource resolveDataSource() { return null; } -======= private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOUtil.class); public static void cleanupResources(Connection conn, PreparedStatement stmt, ResultSet rs) { @@ -89,5 +86,5 @@ public class MobileDeviceManagementDAOUtil { MobileDevice mobileDeviceBO = new MobileDevice(); return mobileDeviceBO; } ->>>>>>> 95f72f77f1a86f8f7e92b8cceafd63b40374dcbb + } 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 01819c626a..9d3e08cadd 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 @@ -16,27 +16,15 @@ package org.wso2.carbon.device.mgt.mobile.impl.internal; -import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.Log; -<<<<<<< HEAD +import org.apache.commons.logging.LogFactory; import org.osgi.framework.*; -======= -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceRegistration; import org.wso2.carbon.device.mgt.common.DeviceManagementException; ->>>>>>> 95f72f77f1a86f8f7e92b8cceafd63b40374dcbb 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; -<<<<<<< HEAD -import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceDAOFactory; -======= -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.MobileDeviceManagementDAOFactory; ->>>>>>> 95f72f77f1a86f8f7e92b8cceafd63b40374dcbb +import org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceDAOFactory; 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; @@ -54,7 +42,6 @@ public class MobileDeviceManagementBundleActivator implements BundleActivator, B private static final Log log = LogFactory.getLog(MobileDeviceManagementBundleActivator.class); private static final String SYMBOLIC_NAME_DATA_SOURCE_COMPONENT = "org.wso2.carbon.ndatasource.core"; -<<<<<<< HEAD @Override public void start(BundleContext bundleContext) throws Exception { try { @@ -63,6 +50,17 @@ public class MobileDeviceManagementBundleActivator implements BundleActivator, B } 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 @@ -113,73 +111,20 @@ public class MobileDeviceManagementBundleActivator implements BundleActivator, B private List getDataSourceListeners() { return dataSourceListeners; } -======= - @Override - public void start(BundleContext bundleContext) throws Exception { - try { - //Initialize Mobile Device Management datasource -// MobileDeviceConfigurationManager.getInstance().initConfig(); -// MobileDeviceManagementConfig config = MobileDeviceConfigurationManager.getInstance() -// .getMobileDeviceManagementConfig(); -// MobileDataSourceConfig -// dsConfig = config.getMobileDeviceMgtRepository().getMobileDataSourceConfig(); -// MobileDeviceManagementDAOFactory.init(dsConfig); - - /* 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); - } - - if (log.isDebugEnabled()) { - log.debug("Activating Mobile Device Management Service bundle"); - } - 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); - } - } - - 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); - } - } - - @Override - public void stop(BundleContext bundleContext) throws Exception { - if (log.isDebugEnabled()) { - log.debug("Deactivating Mobile Device Management Service"); - } - androidServiceRegRef.unregister(); - iOSServiceRegRef.unregister(); - windowsServiceRegRef.unregister(); - } ->>>>>>> 95f72f77f1a86f8f7e92b8cceafd63b40374dcbb + + 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); + } + } + }