From 6c857fdc4d7c28efa516839dca2abd8c75804147 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Thu, 23 Jul 2015 14:35:02 +0530 Subject: [PATCH 1/3] Removing user-api-publisher-config.xml and making JAX-RS services get auto-published as APIs depending on parameters configured in web.xml of each corresponding web applications --- .../org.wso2.carbon.device.mgt.core/pom.xml | 8 +- .../device/mgt/core/api/mgt/APIConfig.java | 11 ++ .../mgt/APIRegistrationStartupObserver.java | 82 ----------- .../api/mgt/config/APIPublisherConfig.java | 110 --------------- .../APIPublisherLifecycleListener.java | 131 ++++++++++++++++++ .../DeviceManagementServiceComponent.java | 3 - 6 files changed, 149 insertions(+), 196 deletions(-) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIRegistrationStartupObserver.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/config/APIPublisherConfig.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/lifecycle/listener/APIPublisherLifecycleListener.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml index 9d5069b8a9..1de4ce1c0f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml @@ -83,7 +83,9 @@ org.wso2.carbon.identity.oauth.stub, org.wso2.carbon.identity.oauth.stub.dto, org.wso2.carbon.ndatasource.core, - org.apache.catalina + org.apache.catalina, + org.apache.catalina.core, + javax.servlet !org.wso2.carbon.device.mgt.core.internal, @@ -212,6 +214,10 @@ org.wso2.tomcat tomcat + + org.wso2.tomcat + tomcat-servlet-api + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIConfig.java index 03d18c0397..61a549ced7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIConfig.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIConfig.java @@ -53,6 +53,7 @@ public class APIConfig { private String version; private String transports; private APIProvider provider; + private boolean isSecured; public void init() throws DeviceManagementException { try { @@ -126,4 +127,14 @@ public class APIConfig { this.transports = transports; } + @XmlElement(name = "isSecured", required = false) + public boolean isSecured() { + return isSecured; + } + + @SuppressWarnings("unused") + public void setSecured(boolean secured) { + isSecured = secured; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIRegistrationStartupObserver.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIRegistrationStartupObserver.java deleted file mode 100644 index c0c8207b8e..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIRegistrationStartupObserver.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (c) 2015, 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.device.mgt.core.api.mgt; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.apimgt.api.APIManagementException; -import org.wso2.carbon.apimgt.api.APIProvider; -import org.wso2.carbon.apimgt.api.model.API; -import org.wso2.carbon.apimgt.impl.APIManagerFactory; -import org.wso2.carbon.core.ServerStartupObserver; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.core.api.mgt.config.APIPublisherConfig; -import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; -import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; - -import java.util.List; - -/** - * This particular class corresponding to the ServerStartupObserver written for publishing the set of APIs used by - * the device management related components. - * - * Note: Using this particular approach is not a must, had there been a proper programming interface provided by the - * underlying API-Management infrastructure for manipulating the APIs. Even though, there's one, its concrete - * implementation consumes a set of OSGi declarative services for initializing some of its internal states, which - * prevents us from, simply, instantiating the APIPublisher implementation and using for device management related - * tasks. The aforesaid complication lead us to go for this alternative approach to get the same done. - */ -public class APIRegistrationStartupObserver implements ServerStartupObserver { - - private static final Log log = LogFactory.getLog(APIRegistrationStartupObserver.class); - - @Override - public void completingServerStartup() { - - } - - @Override - public void completedServerStartup() { - /* Publish all mobile device management related JAX-RS services as APIs */ - if (log.isDebugEnabled()) { - log.debug("Publishing all mobile device management related JAX-RS services as APIs"); - } - List apiConfigs = APIPublisherConfig.getInstance().getApiConfigs(); - for (APIConfig apiConfig : apiConfigs) { - try { - /* API Config is initialized at this point in order to avoid OSGi declarative services which - the APIManagerComponent depend on, are deployed and initialized before invoking methods in - APIManagerFactory */ - apiConfig.init(); - - API api = DeviceManagerUtil.getAPI(apiConfig); - DeviceManagementDataHolder.getInstance().getApiPublisherService().publishAPI(api); - } catch (Throwable e) { - /* Throwable is caught as none of the RuntimeExceptions that can potentially occur at this point - does not seem to be logged anywhere else within the framework */ - log.error("Error occurred while publishing API '" + apiConfig.getName() + "' with the context '" + - apiConfig.getContext() + "' and version '" + apiConfig.getVersion() + "'", e); - } - } - if (log.isDebugEnabled()) { - log.debug("End of publishing all mobile device management related JAX-RS services as APIs"); - } - } - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/config/APIPublisherConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/config/APIPublisherConfig.java deleted file mode 100644 index 85ada40bb1..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/config/APIPublisherConfig.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright (c) 2015, 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.device.mgt.core.api.mgt.config; - -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.core.api.mgt.APIConfig; -import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; -import org.wso2.carbon.utils.CarbonUtils; -import org.xml.sax.SAXException; - -import javax.xml.XMLConstants; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Unmarshaller; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlElementWrapper; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.validation.Schema; -import javax.xml.validation.SchemaFactory; -import java.io.File; -import java.util.List; - -/** - * This class carries all API configurations used by device management components, that need to be published within - * the underlying API-Management infrastructure. - */ -@XmlRootElement(name = "APIPublisherConfig") -public class APIPublisherConfig { - - private List apiConfigs; - private static APIPublisherConfig config; - - private static final Log log = LogFactory.getLog(APIPublisherConfig.class); - private static final String USER_DEFINED_API_CONFIG_PATH = - CarbonUtils.getEtcCarbonConfigDirPath() + File.separator + "user-api-publisher-config.xml"; - private static final String USER_DEFINED_API_CONFIG_SCHEMA_PATH = - "resources/config/schema/api-publisher-config-schema.xsd"; - - private static final Object LOCK = new Object(); - - public static APIPublisherConfig getInstance() { - if (config == null) { - synchronized (LOCK) { - try { - init(); - } catch (DeviceManagementException e) { - log.error("Error occurred while initializing API Publisher Config", e); - } - } - } - return config; - } - - @XmlElementWrapper(name = "APIs", required = true) - @XmlElement(name = "API", required = true) - public List getApiConfigs() { - return apiConfigs; - } - - @SuppressWarnings("unused") - public void setApiConfigs(List apiConfigs) { - this.apiConfigs = apiConfigs; - } - - private static void init() throws DeviceManagementException { - try { - File publisherConfig = new File(APIPublisherConfig.USER_DEFINED_API_CONFIG_PATH); - Document doc = DeviceManagerUtil.convertToDocument(publisherConfig); - - /* Un-marshaling API publisher configuration */ - JAXBContext ctx = JAXBContext.newInstance(APIPublisherConfig.class); - Unmarshaller unmarshaller = ctx.createUnmarshaller(); - //unmarshaller.setSchema(getSchema()); - config = (APIPublisherConfig) unmarshaller.unmarshal(doc); - } catch (JAXBException e) { - throw new DeviceManagementException("Error occurred while un-marshalling API Publisher Config", e); - } - } - - private static Schema getSchema() throws DeviceManagementException { - try { - File deviceManagementSchemaConfig = new File(APIPublisherConfig.USER_DEFINED_API_CONFIG_SCHEMA_PATH); - SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); - return factory.newSchema(deviceManagementSchemaConfig); - } catch (SAXException e) { - throw new DeviceManagementException("Error occurred while initializing the schema of " + - "user-api-publisher-config.xml", e); - } - } - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/lifecycle/listener/APIPublisherLifecycleListener.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/lifecycle/listener/APIPublisherLifecycleListener.java new file mode 100644 index 0000000000..8816e20f42 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/lifecycle/listener/APIPublisherLifecycleListener.java @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2015, 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.device.mgt.core.api.mgt.lifecycle.listener; + +import org.apache.catalina.Lifecycle; +import org.apache.catalina.LifecycleEvent; +import org.apache.catalina.LifecycleListener; +import org.apache.catalina.core.StandardContext; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.apimgt.api.model.API; +import org.wso2.carbon.device.mgt.core.api.mgt.APIConfig; +import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; +import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; + +import javax.servlet.ServletContext; + +public class APIPublisherLifecycleListener implements LifecycleListener { + + private static final String API_CONFIG_DEFAULT_VERSION = "1.0.0"; + private static final Log log = LogFactory.getLog(APIPublisherLifecycleListener.class); + + @Override + public void lifecycleEvent(LifecycleEvent lifecycleEvent) { + if (Lifecycle.AFTER_START_EVENT.equals(lifecycleEvent.getType())) { + StandardContext context = (StandardContext) lifecycleEvent.getLifecycle(); + ServletContext servletContext = context.getServletContext(); + + String param = servletContext.getInitParameter("managed-api-enabled"); + boolean isManagedApi = (param != null && !"".equals(param)) && Boolean.parseBoolean(param); + + if (isManagedApi) { + APIConfig apiConfig = this.buildApiConfig(servletContext); + try { + apiConfig.init(); + API api = DeviceManagerUtil.getAPI(apiConfig); + DeviceManagementDataHolder.getInstance().getApiPublisherService().publishAPI(api); + } catch (Throwable e) { + /* Throwable is caught as none of the RuntimeExceptions that can potentially occur at this point + does not seem to be logged anywhere else within the framework */ + log.error("Error occurred while publishing API '" + apiConfig.getName() + "' with the context '" + + apiConfig.getContext() + "' and version '" + apiConfig.getVersion() + "'", e); + } + } + } + } + + private APIConfig buildApiConfig(ServletContext servletContext) { + APIConfig apiConfig = new APIConfig(); + + String name = servletContext.getInitParameter("managed-api-name"); + if (name == null || "".equals(name)) { + if (log.isDebugEnabled()) { + log.debug("'managed-api-name' attribute is not configured. Therefore, using the default, " + + "which is the name of the web application"); + } + name = servletContext.getServletContextName(); + } + apiConfig.setName(name); + + String version = servletContext.getInitParameter("managed-api-version"); + if (version == null || "".equals(version)) { + if (log.isDebugEnabled()) { + log.debug("'managed-api-version' attribute is not configured. Therefore, using the " + + "default, which is '1.0.0'"); + } + version = API_CONFIG_DEFAULT_VERSION; + } + apiConfig.setVersion(version); + + String context = servletContext.getInitParameter("managed-api-context"); + if (context == null || "".equals(context)) { + if (log.isDebugEnabled()) { + log.debug("'managed-api-context' attribute is not configured. Therefore, using the default, " + + "which is the original context assigned to the web application"); + } + context = servletContext.getContextPath(); + } + apiConfig.setContext(context); + + String endpoint = servletContext.getInitParameter("managed-api-endpoint"); + if (endpoint == null || "".equals(endpoint)) { + if (log.isDebugEnabled()) { + log.debug("'managed-api-endpoint' attribute is not configured"); + } + } + apiConfig.setEndpoint(endpoint); + + String owner = servletContext.getInitParameter("managed-api-owner"); + if (owner == null || "".equals(owner)) { + if (log.isDebugEnabled()) { + log.debug("'managed-api-owner' attribute is not configured"); + } + } + apiConfig.setOwner(owner); + + String isSecuredParam = servletContext.getInitParameter("managed-api-isSecured"); + boolean isSecured = + (isSecuredParam != null && !"".equals(isSecuredParam)) && Boolean.parseBoolean(isSecuredParam); + apiConfig.setSecured(isSecured); + + String transports = servletContext.getInitParameter("managed-api-transports"); + if (transports == null || "".equals(transports)) { + if (log.isDebugEnabled()) { + log.debug("'managed-api-transports' attribute is not configured. Therefore using the defaults, " + + "which are 'http' and 'https'"); + } + transports = "http,https"; + } + apiConfig.setTransports(transports); + + return apiConfig; + } + +} 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 1aa6ea5a9a..5dde3b2f6b 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 @@ -35,7 +35,6 @@ import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository; import org.wso2.carbon.device.mgt.core.api.mgt.APIPublisherService; import org.wso2.carbon.device.mgt.core.api.mgt.APIPublisherServiceImpl; -import org.wso2.carbon.device.mgt.core.api.mgt.APIRegistrationStartupObserver; import org.wso2.carbon.device.mgt.core.api.mgt.ApplicationManagementProviderService; import org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagerProviderServiceImpl; import org.wso2.carbon.device.mgt.core.app.mgt.config.AppManagementConfig; @@ -198,8 +197,6 @@ public class DeviceManagementServiceComponent { DeviceManagementDataHolder.getInstance().setApiPublisherService(publisher); bundleContext.registerService(APIPublisherService.class, publisher, null); - bundleContext.registerService(ServerStartupObserver.class, new APIRegistrationStartupObserver(), null); - /* Registering App Management service */ try { AppManagementConfigurationManager.getInstance().initConfig(); From 701228810bd06ee47a7a0ca934957b19ea21cd2a Mon Sep 17 00:00:00 2001 From: prabathabey Date: Thu, 23 Jul 2015 14:35:25 +0530 Subject: [PATCH 2/3] Removing user-api-publisher-config.xml and making JAX-RS services get auto-published as APIs depending on parameters configured in web.xml of each corresponding web applications --- .../conf/user-api-publisher-config.xml | 46 ------------------- .../src/main/resources/p2.inf | 1 - 2 files changed, 47 deletions(-) delete mode 100644 features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/user-api-publisher-config.xml diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/user-api-publisher-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/user-api-publisher-config.xml deleted file mode 100644 index 6307629e04..0000000000 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/user-api-publisher-config.xml +++ /dev/null @@ -1,46 +0,0 @@ - - - - - - AppManagerController - admin - appmanager - 1.0.0 - http://localhost:9763/appmanager - http,https - - - AndroidAgent - admin - mdm-android-agent - 1.0.0 - http://localhost:9763/mdm-android-agent - http,https - - - MDMAdmin - admin - mdm-admin - 1.0.0 - http://localhost:9763/mdm-admin - http,https - - - diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf index 465b3b8ea7..8da383c571 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf @@ -2,7 +2,6 @@ instructions.configure = \ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/conf/cdm-config.xml,target:${installFolder}/../../conf/cdm-config.xml,overwrite:true);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/conf/notification-messages.xml,target:${installFolder}/../../conf/notification-messages.xml,overwrite:true);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/conf/license-config.xml,target:${installFolder}/../../conf/etc/license-config.xml,overwrite:true);\ -org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/conf/user-api-publisher-config.xml,target:${installFolder}/../../conf/etc/user-api-publisher-config.xml,overwrite:true);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/conf/app-management-config.xml,target:${installFolder}/../../conf/etc/app-management-config.xml,overwrite:true);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/dbscripts/cdm,target:${installFolder}/../../../dbscripts/cdm,overwrite:true);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/rxts/license.rxt,target:${installFolder}/../../../repository/resources/rxts/license.rxt,overwrite:true);\ \ No newline at end of file From 6f56e27f1d8a7f6e176e3381b399ef047ed6fc49 Mon Sep 17 00:00:00 2001 From: milanperera Date: Thu, 23 Jul 2015 14:46:22 +0530 Subject: [PATCH 3/3] Refactored SQL queries --- .../mgt/core/dao/impl/DeviceDAOImpl.java | 117 ++++++++++-------- 1 file changed, 67 insertions(+), 50 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index fdb18454f7..bd0cbe6038 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -48,7 +48,7 @@ public class DeviceDAOImpl implements DeviceDAO { conn = this.getConnection(); String sql = "INSERT INTO DM_DEVICE(DESCRIPTION, NAME, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, TENANT_ID) " + - "VALUES (?, ?, ?, ?, ?)"; + "VALUES (?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); stmt.setString(1, device.getDescription()); stmt.setString(2, device.getName()); @@ -64,7 +64,7 @@ public class DeviceDAOImpl implements DeviceDAO { return deviceId; } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while enrolling device " + - "'" + device.getName() + "'", e); + "'" + device.getName() + "'", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -78,7 +78,8 @@ public class DeviceDAOImpl implements DeviceDAO { int deviceId = -1; try { conn = this.getConnection(); - String sql = "UPDATE DM_DEVICE SET DESCRIPTION = ?, NAME = ? WHERE DEVICE_IDENTIFICATION = ? AND " + + String sql = + "UPDATE DM_DEVICE SET DESCRIPTION = ?, NAME = ? WHERE DEVICE_IDENTIFICATION = ? AND " + "DEVICE_TYPE_ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); stmt.setString(1, device.getDescription()); @@ -95,7 +96,7 @@ public class DeviceDAOImpl implements DeviceDAO { return deviceId; } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while enrolling device '" + - device.getName() + "'", e); + device.getName() + "'", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -116,9 +117,11 @@ public class DeviceDAOImpl implements DeviceDAO { conn = this.getConnection(); String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " + - "e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " + - "t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " + - "t.NAME = ? AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?"; + "e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT " + + "FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " + + "t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " + + "t.NAME = ? AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID " + + "AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, deviceId.getType()); stmt.setString(2, deviceId.getId()); @@ -130,7 +133,7 @@ public class DeviceDAOImpl implements DeviceDAO { } } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while listing devices for type " + - "'" + deviceId.getType() + "'", e); + "'" + deviceId.getType() + "'", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -145,8 +148,10 @@ public class DeviceDAOImpl implements DeviceDAO { List devices = null; try { conn = this.getConnection(); - String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " + - "e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e, (SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME," + + String sql = + "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " + + "e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID " + + "FROM DM_ENROLMENT e, (SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME," + "d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, DM_DEVICE_TYPE t " + "WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); @@ -160,7 +165,7 @@ public class DeviceDAOImpl implements DeviceDAO { } } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while retrieving information of all " + - "registered devices", e); + "registered devices", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -175,11 +180,13 @@ public class DeviceDAOImpl implements DeviceDAO { List devices = null; try { conn = this.getConnection(); - String selectDBQueryForType = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION," + - "e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " + - "d.DEVICE_IDENTIFICATION, d.OWNER, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, DM_DEVICE_TYPE t " + - "WHERE d.DM_DEVICE.DEVICE_TYPE_ID = t.ID AND t.NAME = ? AND d.TENANT_ID = ?) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; - stmt = conn.prepareStatement(selectDBQueryForType); + String sql = + "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " + + "e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID " + + "FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.OWNER, t.NAME " + + "AS DEVICE_TYPE FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " + + "AND d.TENANT_ID = ?) d1 WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; + stmt = conn.prepareStatement(sql); stmt.setString(1, type); stmt.setInt(2, tenantId); stmt.setInt(3, tenantId); @@ -204,14 +211,17 @@ public class DeviceDAOImpl implements DeviceDAO { List devices = new ArrayList(); try { conn = this.getConnection(); - stmt = conn.prepareStatement( - "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d.DEVICE_IDENTIFICATION" + - " e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e, (SELECT t.NAME AS DEVICE_TYPE, d.ID, d.DESCRIPTION, " + - "d.NAME, d.DEVICE_IDENTIFICATION FROM " + - "DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.OWNER =? AND d.TENANT_ID = ?) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"); - stmt.setString(1, username); + String sql = + "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e, (SELECT t.NAME AS DEVICE_TYPE, d.ID, d.DESCRIPTION, " + + "d.NAME, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t " + + "WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " + + "WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? AND e.OWNER = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, tenantId); stmt.setInt(2, tenantId); - stmt.setInt(3, tenantId); + stmt.setString(3, username); ResultSet rs = stmt.executeQuery(); while (rs.next()) { @@ -220,7 +230,7 @@ public class DeviceDAOImpl implements DeviceDAO { } } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while fetching the list of devices belongs to '" + - username + "'", e); + username + "'", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, null); } @@ -275,16 +285,16 @@ public class DeviceDAOImpl implements DeviceDAO { List devices = new ArrayList(); try { conn = this.getConnection(); - stmt = conn.prepareStatement( - "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d.DEVICE_IDENTIFICATION " + - "e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e, (SELECT d.ID AS DEVICE_ID, d.NAME, d.DESCRIPTION, " + - "t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM " + - "DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID " + - "AND d.NAME LIKE ? AND d.TENANT_ID = ?) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"); + String sql = + "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " + + "e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID " + + "FROM DM_ENROLMENT e, (SELECT d.ID, d.NAME, d.DESCRIPTION, t.NAME AS DEVICE_TYPE, " + + "d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID " + + "AND d.NAME LIKE ? AND d.TENANT_ID = ?) d1 WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; + stmt = conn.prepareStatement(sql); stmt.setString(1, deviceName + "%"); stmt.setInt(2, tenantId); stmt.setInt(3, tenantId); - stmt.setInt(4, tenantId); ResultSet rs = stmt.executeQuery(); while (rs.next()) { @@ -293,7 +303,7 @@ public class DeviceDAOImpl implements DeviceDAO { } } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches " + - "'" + deviceName + "'", e); + "'" + deviceName + "'", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, null); } @@ -308,8 +318,9 @@ public class DeviceDAOImpl implements DeviceDAO { int enrolmentId = -1; try { conn = this.getConnection(); - String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, OWNER, OWNERSHIP, STATUS, " + - "DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, TENANT_ID) VALUES(?, ?, ?, ?, ?, ?, ?)"; + String sql = + "INSERT INTO DM_ENROLMENT(DEVICE_ID, OWNER, OWNERSHIP, STATUS,DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, " + + "TENANT_ID) VALUES(?, ?, ?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); stmt.setInt(1, device.getId()); stmt.setString(2, device.getEnrolmentInfo().getOwner()); @@ -339,9 +350,10 @@ public class DeviceDAOImpl implements DeviceDAO { PreparedStatement stmt = null; try { conn = this.getConnection(); - String sql = "UPDATE DM_ENROLMENT SET STATUS = ? WHERE DEVICE_ID = " + - "(SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND " + - "d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) AND OWNER = ? AND TENANT_ID = ?"; + String sql = + "UPDATE DM_ENROLMENT SET STATUS = ? WHERE DEVICE_ID = (SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t " + + "WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) " + + "AND OWNER = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, status.toString()); stmt.setString(2, deviceId.getId()); @@ -367,7 +379,8 @@ public class DeviceDAOImpl implements DeviceDAO { Status status = null; try { conn = this.getConnection(); - String sql = "SELECT STATUS FROM DM_ENROLMENT WHERE DEVICE_ID = " + + String sql = + "SELECT STATUS FROM DM_ENROLMENT WHERE DEVICE_ID = " + "(SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND " + "d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) AND OWNER = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); @@ -398,10 +411,11 @@ public class DeviceDAOImpl implements DeviceDAO { EnrolmentInfo enrolmentInfo = null; try { conn = this.getConnection(); - String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, DATE_OF_ENROLMENT, " + - "DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = " + - "(SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND" + - " d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) AND OWNER = ? AND TENANT_ID = ?"; + String sql = + "SELECT ID AS ENROLMENT_ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, " + + "TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = (SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t " + + "WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) " + + "AND OWNER = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, deviceId.getId()); stmt.setString(2, deviceId.getType()); @@ -415,7 +429,7 @@ public class DeviceDAOImpl implements DeviceDAO { return enrolmentInfo; } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " + - "information of user '" + currentOwner + "' upon device '" + deviceId + "'", e); + "information of user '" + currentOwner + "' upon device '" + deviceId + "'", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -442,18 +456,21 @@ public class DeviceDAOImpl implements DeviceDAO { return enrolmentInfo; } - public List getDevicesByStatus(EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException { + public List getDevicesByStatus(EnrolmentInfo.Status status, int tenantId) + throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; List devices = new ArrayList(); try { conn = this.getConnection(); - stmt = conn.prepareStatement("SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, t.NAME AS DEVICE_TYPE, " + - "d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + - "e.DATE_OF_ENROLMENT FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " + - "e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE FROM DM_ENROLMENT e WHERE TENANT_ID = ? " + - "AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_ID = e.DEVICE_ID " + - "AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"); + String sql = + "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, t.NAME AS DEVICE_TYPE, " + + "d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " + + "e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE FROM DM_ENROLMENT e WHERE TENANT_ID = ? " + + "AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_ID = e.DEVICE_ID " + + "AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; + stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setString(2, status.toString()); stmt.setInt(3, tenantId);