From 071e45129d1f6a98ae501f6582a34cd88b928bef Mon Sep 17 00:00:00 2001 From: Chathura Ekanayake Date: Tue, 8 Aug 2017 10:22:48 +0530 Subject: [PATCH 1/2] implementing application lifecycle management --- .../device/application/mgt/api/APIUtil.java | 20 +++++ .../api/services/LifecycleManagementAPI.java | 76 ++++++++++++++++ .../impl/ApplicationManagementAPIImpl.java | 23 +++++ .../impl/LifecycleManagementAPIImpl.java | 88 +++++++++++++++++++ .../src/main/webapp/WEB-INF/cxf-servlet.xml | 2 + .../LifecycleManagementException.java | 32 +++++++ .../common/services/ApplicationManager.java | 10 ++- .../services/LifecycleStateManager.java | 10 +++ .../mgt/core/dao/ApplicationDAO.java | 2 + .../mgt/core/dao/LifecycleStateDAO.java | 11 ++- .../application/H2ApplicationDAOImpl.java | 5 ++ .../application/MySQLApplicationDAOImpl.java | 28 ++++++ .../GenericLifecycleStateImpl.java | 74 +++++++++++++++- .../mgt/core/exception/DAOException.java | 29 ++++++ .../mgt/core/impl/ApplicationManagerImpl.java | 11 +++ .../core/impl/LifecycleStateManagerImpl.java | 55 ++++++++++++ .../mgt/core/util/ConnectionManagerUtil.java | 22 +++-- 17 files changed, 485 insertions(+), 13 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/LifecycleManagementAPI.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/LifecycleManagementAPIImpl.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/LifecycleManagementException.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/DAOException.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java index 5ffc2ccda5..6817698a35 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java @@ -24,6 +24,7 @@ import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.application.mgt.api.beans.ErrorResponse; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; +import org.wso2.carbon.device.application.mgt.common.services.LifecycleStateManager; import org.wso2.carbon.device.application.mgt.common.services.PlatformManager; import javax.ws.rs.core.Response; @@ -38,6 +39,7 @@ public class APIUtil { private static ApplicationManager applicationManager; private static PlatformManager platformManager; + private static LifecycleStateManager lifecycleStateManager; public static ApplicationManager getApplicationManager() { if (applicationManager == null) { @@ -76,6 +78,24 @@ public class APIUtil { return platformManager; } + public static LifecycleStateManager getLifecycleStateManager() { + if (lifecycleStateManager == null) { + synchronized (APIUtil.class) { + if (lifecycleStateManager == null) { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + lifecycleStateManager = + (LifecycleStateManager) ctx.getOSGiService(LifecycleStateManager.class, null); + if (lifecycleStateManager == null) { + String msg = "Lifecycle Manager service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + } + } + } + return lifecycleStateManager; + } + public static Response getResponse(ApplicationManagementException ex, Response.Status status) { return getResponse(ex.getMessage(), status); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/LifecycleManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/LifecycleManagementAPI.java new file mode 100644 index 0000000000..b2f5685100 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/LifecycleManagementAPI.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2016, 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.application.mgt.api.services; + +import io.swagger.annotations.Api; +import io.swagger.annotations.Extension; +import io.swagger.annotations.ExtensionProperty; +import io.swagger.annotations.Info; +import io.swagger.annotations.SwaggerDefinition; +import io.swagger.annotations.Tag; +import org.wso2.carbon.apimgt.annotations.api.Scope; +import org.wso2.carbon.apimgt.annotations.api.Scopes; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +@SwaggerDefinition( + info = @Info( + version = "1.0.0", + title = "Lifecycle Management Service", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = "name", value = "LifecycleManagementService"), + @ExtensionProperty(name = "context", value = "/api/application-mgt/v1.0/lifecycle"), + }) + } + ), + tags = { + @Tag(name = "lifecycle_management", description = "Lifecycle Management related APIs") + } +) +@Scopes( + scopes = { + @Scope( + name = "Get Lifecycle Details", + description = "Get lifecycle details", + key = "perm:lifecycle:get", + permissions = {"/device-mgt/lifecycle/get"} + ), + @Scope( + name = "Add a lifecycle state", + description = "Add a lifecycle state", + key = "perm:lifecycle:add", + permissions = {"/device-mgt/lifecycle/add"} + ), + } +) +@Path("/lifecycle") +@Api(value = "Lifecycle Management", description = "This API carries all lifecycle management related operations.") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public interface LifecycleManagementAPI { + + @GET + @Path("/states") + Response getLifecycleStates(); +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementAPIImpl.java index d1a9bd3249..3fa2ffc4c2 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementAPIImpl.java @@ -64,6 +64,29 @@ public class ApplicationManagementAPIImpl { } } + @GET + @Consumes("application/json") + @Path("applications/{uuid}") + public Response getApplication(@PathParam("uuid") String uuid) { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + return null; + } + + @PUT + @Consumes("application/json") + @Path("applications/{uuid}/lifecycle") + public Response changeLifecycleState(@PathParam("uuid") String applicationUUID, @QueryParam("state") String state) { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + try { + applicationManager.changeLifecycle(applicationUUID, state); + } catch (ApplicationManagementException e) { + String msg = "Error occurred while changing the lifecycle of application: " + applicationUUID; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).build(); + } + return Response.status(Response.Status.OK).entity("Successfully changed the lifecycle state of the application: " + applicationUUID).build(); + } + @POST @Consumes("application/json") @Path("applications") diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/LifecycleManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/LifecycleManagementAPIImpl.java new file mode 100644 index 0000000000..4be4b90ac7 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/LifecycleManagementAPIImpl.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2016, 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.application.mgt.api.services.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.application.mgt.api.APIUtil; +import org.wso2.carbon.device.application.mgt.api.services.LifecycleManagementAPI; +import org.wso2.carbon.device.application.mgt.common.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.User; +import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; +import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; +import org.wso2.carbon.device.application.mgt.common.services.LifecycleStateManager; + +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.core.Response; +import java.util.ArrayList; +import java.util.List; + +@Path("/lifecycle") +public class LifecycleManagementAPIImpl implements LifecycleManagementAPI { + + private static Log log = LogFactory.getLog(LifecycleManagementAPIImpl.class); + + @GET + @Path("/states") + public Response getLifecycleStates() { + LifecycleStateManager lifecycleStateManager = APIUtil.getLifecycleStateManager(); + List lifecycleStates = new ArrayList<>(); + try { + lifecycleStates = lifecycleStateManager.getLifecycleStates(); + } catch (LifecycleManagementException e) { + String msg = "Error occurred while retrieving lifecycle states."; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).build(); + } + return Response.status(Response.Status.OK).entity(lifecycleStates).build(); + } + + @POST + @Path("/states") + public Response addLifecycleState(LifecycleState state) { + LifecycleStateManager lifecycleStateManager = APIUtil.getLifecycleStateManager(); + try { + lifecycleStateManager.addLifecycleState(state); + } catch (LifecycleManagementException e) { + String msg = "Error occurred while adding lifecycle state."; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).build(); + } + return Response.status(Response.Status.OK).entity("Lifecycle state added successfully.").build(); + } + + @DELETE + @Path("/states/{identifier}") + public Response deleteLifecycleState(@PathParam("identifier") String identifier) { + LifecycleStateManager lifecycleStateManager = APIUtil.getLifecycleStateManager(); + try { + lifecycleStateManager.deleteLifecycleState(identifier); + } catch (LifecycleManagementException e) { + String msg = "Error occurred while deleting lifecycle state."; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).build(); + } + return Response.status(Response.Status.OK).entity("Lifecycle state deleted successfully.").build(); + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml index 8d238a363d..b215ae2108 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -26,6 +26,7 @@ http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> + @@ -33,6 +34,7 @@ http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> + diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/LifecycleManagementException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/LifecycleManagementException.java new file mode 100644 index 0000000000..09fe89a4d9 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/LifecycleManagementException.java @@ -0,0 +1,32 @@ +/* +* Copyright (c) 2017, 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.application.mgt.common.exception; + +/** + * Exception caused during the lifecycle management. + */ +public class LifecycleManagementException extends ApplicationManagementException { + + public LifecycleManagementException(String message, Throwable ex) { + super(message, ex); + } + + public LifecycleManagementException(String message) { + super(message); + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java index cc4f8790c6..58b03d4ccd 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java @@ -34,7 +34,7 @@ public interface ApplicationManager { * @return Created application * @throws ApplicationManagementException Application Management Exception */ - public Application createApplication(Application application) throws ApplicationManagementException; + Application createApplication(Application application) throws ApplicationManagementException; /** * Updates an already existing application. @@ -42,14 +42,14 @@ public interface ApplicationManager { * @return Updated Application * @throws ApplicationManagementException Application Management Exception */ - public Application editApplication(Application application) throws ApplicationManagementException; + Application editApplication(Application application) throws ApplicationManagementException; /** * Delete an application identified by the unique ID. * @param uuid Unique ID for tha application * @throws ApplicationManagementException Application Management Exception */ - public void deleteApplication(String uuid) throws ApplicationManagementException; + void deleteApplication(String uuid) throws ApplicationManagementException; /** * To get the applications based on the search filter. @@ -57,5 +57,7 @@ public interface ApplicationManager { * @return Applications that matches the given filter criteria. * @throws ApplicationManagementException Application Management Exception */ - public ApplicationList getApplications(Filter filter) throws ApplicationManagementException; + ApplicationList getApplications(Filter filter) throws ApplicationManagementException; + + void changeLifecycle(String applicationUUID, String lifecycleIdentifier) throws ApplicationManagementException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/LifecycleStateManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/LifecycleStateManager.java index fa3bb75a69..82238a716a 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/LifecycleStateManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/LifecycleStateManager.java @@ -18,9 +18,19 @@ */ package org.wso2.carbon.device.application.mgt.common.services; +import org.wso2.carbon.device.application.mgt.common.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; + +import java.util.List; + /** * This interface manages all the operations related with lifecycle state. */ public interface LifecycleStateManager { + List getLifecycleStates() throws LifecycleManagementException; + + void addLifecycleState(LifecycleState state) throws LifecycleManagementException; + + void deleteLifecycleState(String identifier) throws LifecycleManagementException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java index 860ca00c89..6b9695f059 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java @@ -52,4 +52,6 @@ public interface ApplicationDAO { void addRelease(ApplicationRelease release) throws ApplicationManagementDAOException; + void changeLifecycle(String applicationUUID, String lifecycleIdentifier) throws ApplicationManagementDAOException; + } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/LifecycleStateDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/LifecycleStateDAO.java index 9f4674d069..dbe164b7cb 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/LifecycleStateDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/LifecycleStateDAO.java @@ -20,9 +20,18 @@ package org.wso2.carbon.device.application.mgt.core.dao; import org.wso2.carbon.device.application.mgt.common.LifecycleState; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; +import org.wso2.carbon.device.application.mgt.core.exception.DAOException; + +import java.util.List; public interface LifecycleStateDAO { - public LifecycleState getLifeCycleStateByIdentifier(String identifier) throws ApplicationManagementDAOException; + LifecycleState getLifeCycleStateByIdentifier(String identifier) throws ApplicationManagementDAOException; + + List getLifecycleStates() throws DAOException; + + void addLifecycleState(LifecycleState state) throws DAOException; + + void deleteLifecycleState(String identifier) throws DAOException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/H2ApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/H2ApplicationDAOImpl.java index 695ff6052d..5a6cd5fe6c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/H2ApplicationDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/H2ApplicationDAOImpl.java @@ -47,6 +47,11 @@ public class H2ApplicationDAOImpl extends AbstractApplicationDAOImpl { return null; } + @Override + public void changeLifecycle(String applicationUUID, String lifecycleIdentifier) throws ApplicationManagementDAOException { + + } + @Override public ApplicationList getApplications(Filter filter) throws ApplicationManagementDAOException { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/MySQLApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/MySQLApplicationDAOImpl.java index 5e9c74370b..b55a9fba4a 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/MySQLApplicationDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/MySQLApplicationDAOImpl.java @@ -375,6 +375,34 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl { } } + @Override + public void changeLifecycle(String applicationUUID, String lifecycleIdentifier) throws ApplicationManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + conn = this.getDBConnection(); + String sql = "UPDATE APPM_APPLICATION SET " + + "LIFECYCLE_STATE_ID = (SELECT ID FROM APPM_LIFECYCLE_STATE WHERE IDENTIFIER = ?), " + + "LIFECYCLE_STATE_MODIFIED_BY = ?, " + + "LIFECYCLE_STATE_MODIFIED_AT = ? " + + "WHERE UUID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, lifecycleIdentifier); + stmt.setString(2, "admin"); + stmt.setDate(3, new Date(System.currentTimeMillis())); + stmt.setString(4, applicationUUID); + stmt.executeUpdate(); + + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException("Error occurred while changing lifecycle of application: " + applicationUUID + " to: " + lifecycleIdentifier + " state.", e); + } finally { + Util.cleanupResources(stmt, rs); + } + } + @Override public void deleteTags(int applicationId) throws ApplicationManagementDAOException { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateImpl.java index 41640e0669..6b3cef24a0 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateImpl.java @@ -24,11 +24,14 @@ import org.wso2.carbon.device.application.mgt.core.dao.LifecycleStateDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.Util; import org.wso2.carbon.device.application.mgt.core.dao.impl.AbstractDAOImpl; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; +import org.wso2.carbon.device.application.mgt.core.exception.DAOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; public class GenericLifecycleStateImpl extends AbstractDAOImpl implements LifecycleStateDAO { @@ -59,7 +62,6 @@ public class GenericLifecycleStateImpl extends AbstractDAOImpl implements Lifecy lifecycleState.setIdentifier(rs.getString("IDENTIFIER")); lifecycleState.setDescription(rs.getString("DESCRIPTION")); } - return lifecycleState; } catch (SQLException e) { @@ -69,7 +71,77 @@ public class GenericLifecycleStateImpl extends AbstractDAOImpl implements Lifecy } finally { Util.cleanupResources(stmt, rs); } + } + @Override + public List getLifecycleStates() throws DAOException { + List lifecycleStates = new ArrayList<>(); + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + conn = this.getDBConnection(); + String sql = "SELECT IDENTIFIER, NAME, DESCRIPTION FROM APPM_LIFECYCLE_STATE"; + stmt = conn.prepareStatement(sql); + rs = stmt.executeQuery(); + while(rs.next()) { + LifecycleState lifecycleState = new LifecycleState(); + lifecycleState.setIdentifier(rs.getString("IDENTIFIER")); + lifecycleState.setName(rs.getString("NAME")); + lifecycleState.setDescription(rs.getString("DESCRIPTION")); + lifecycleStates.add(lifecycleState); + } + } catch (DBConnectionException e) { + throw new DAOException("Error occurred while obtaining the DB connection.", e); + } catch (SQLException e) { + throw new DAOException("Error occurred while retrieving lifecycle states.", e); + } finally { + Util.cleanupResources(stmt, rs); + } + return lifecycleStates; + } + @Override + public void addLifecycleState(LifecycleState state) throws DAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + conn = this.getDBConnection(); + String sql = "INSERT INTO APPM_LIFECYCLE_STATE ('NAME', 'IDENTIFIER', 'DESCRIPTION') VALUES (?, ?, ?)"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, state.getName()); + stmt.setString(2, state.getIdentifier()); + stmt.setString(3, state.getDescription()); + stmt.executeUpdate(); + + } catch (DBConnectionException e) { + throw new DAOException("Error occurred while obtaining the DB connection.", e); + } catch (SQLException e) { + throw new DAOException("Error occurred while adding lifecycle: " + state.getIdentifier(), e); + } finally { + Util.cleanupResources(stmt, rs); + } + } + + @Override + public void deleteLifecycleState(String identifier) throws DAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + conn = this.getDBConnection(); + String sql = "DELETE FROM APPM_LIFECYCLE_STATE WHERE IDENTIFIER = ?"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, identifier); + stmt.executeUpdate(); + + } catch (DBConnectionException e) { + throw new DAOException("Error occurred while obtaining the DB connection.", e); + } catch (SQLException e) { + throw new DAOException("Error occurred while deleting lifecycle: " + identifier, e); + } finally { + Util.cleanupResources(stmt, rs); + } } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/DAOException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/DAOException.java new file mode 100644 index 0000000000..2d5121bdca --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/DAOException.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2016, 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.application.mgt.core.exception; + +public class DAOException extends Exception { + + public DAOException(String message) { + super(message); + } + + public DAOException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index e3e5bf0879..5b14e04c5d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -148,6 +148,17 @@ public class ApplicationManagerImpl implements ApplicationManager { } + @Override + public void changeLifecycle(String applicationUUID, String lifecycleIdentifier) throws ApplicationManagementException { + try { + ConnectionManagerUtil.openDBConnection(); + ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO(); + applicationDAO.changeLifecycle(applicationUUID, lifecycleIdentifier); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + private void validateApplication(Application application, boolean isEdit) throws ValidationException { if (application.getName() == null) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/LifecycleStateManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/LifecycleStateManagerImpl.java index 2c71738f40..eccf7d4530 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/LifecycleStateManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/LifecycleStateManagerImpl.java @@ -17,7 +17,62 @@ */ package org.wso2.carbon.device.application.mgt.core.impl; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.application.mgt.common.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; +import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.common.services.LifecycleStateManager; +import org.wso2.carbon.device.application.mgt.core.dao.LifecycleStateDAO; +import org.wso2.carbon.device.application.mgt.core.dao.common.DAOFactory; +import org.wso2.carbon.device.application.mgt.core.exception.DAOException; +import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; + +import java.util.List; public class LifecycleStateManagerImpl implements LifecycleStateManager { + + private static final Log log = LogFactory.getLog(LifecycleStateManagerImpl.class); + + @Override + public List getLifecycleStates() throws LifecycleManagementException { + List lifecycleStates = null; + try { + ConnectionManagerUtil.openDBConnection(); + LifecycleStateDAO lifecycleStateDAO = DAOFactory.getLifecycleStateDAO(); + lifecycleStates = lifecycleStateDAO.getLifecycleStates(); + } catch (DAOException | DBConnectionException e) { + throw new LifecycleManagementException("Failed get lifecycle states.", e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + return lifecycleStates; + } + + @Override + public void addLifecycleState(LifecycleState state) throws LifecycleManagementException { + try { + ConnectionManagerUtil.openDBConnection(); + LifecycleStateDAO lifecycleStateDAO = DAOFactory.getLifecycleStateDAO(); + lifecycleStateDAO.addLifecycleState(state); + } catch (DAOException | DBConnectionException e) { + throw new LifecycleManagementException("Failed to add lifecycle state", e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @Override + public void deleteLifecycleState(String identifier) throws LifecycleManagementException { + + try { + ConnectionManagerUtil.openDBConnection(); + LifecycleStateDAO lifecycleStateDAO = DAOFactory.getLifecycleStateDAO(); + lifecycleStateDAO.deleteLifecycleState(identifier); + } catch (DAOException | DBConnectionException e) { + throw new LifecycleManagementException("Failed to add lifecycle state: " + identifier, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java index d663a53c0a..25fd828d34 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java @@ -41,15 +41,23 @@ public class ConnectionManagerUtil { private static ThreadLocal currentTxState = new ThreadLocal<>(); private static DataSource dataSource; + public static void openDBConnection() throws DBConnectionException { + Connection conn = currentConnection.get(); + if (conn != null) { + throw new IllegalTransactionStateException("Database connection has already been obtained."); + } + try { + conn = dataSource.getConnection(); + } catch (SQLException e) { + throw new DBConnectionException("Failed to get a database connection.", e); + } + currentConnection.set(conn); + } + public static Connection getDBConnection() throws DBConnectionException { Connection conn = currentConnection.get(); if (conn == null) { - try { - conn = dataSource.getConnection(); - currentConnection.set(conn); - } catch (SQLException e) { - throw new DBConnectionException("Failed to get database connection.", e); - } + throw new IllegalTransactionStateException("Database connection is not active."); } return conn; } @@ -57,7 +65,7 @@ public class ConnectionManagerUtil { public static void beginDBTransaction() throws TransactionManagementException, DBConnectionException { Connection conn = currentConnection.get(); if (conn == null) { - conn = getDBConnection(); + throw new IllegalTransactionStateException("Database connection is not active."); } if (inTransaction(conn)) { From 9f76f51ee08cd206b48735a64a35f6aa2c0abc5b Mon Sep 17 00:00:00 2001 From: Chathura Ekanayake Date: Tue, 8 Aug 2017 16:13:49 +0530 Subject: [PATCH 2/2] lifecycle management changes --- .../api/services/LifecycleManagementAPI.java | 111 +++++++++++++++++- .../impl/LifecycleManagementAPIImpl.java | 6 +- .../src/main/webapp/WEB-INF/cxf-servlet.xml | 2 + .../mgt/core/util/ConnectionManagerUtil.java | 13 ++ 4 files changed, 123 insertions(+), 9 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/LifecycleManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/LifecycleManagementAPI.java index b2f5685100..fdd33f1a29 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/LifecycleManagementAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/LifecycleManagementAPI.java @@ -18,6 +18,9 @@ package org.wso2.carbon.device.application.mgt.api.services; import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; import io.swagger.annotations.Extension; import io.swagger.annotations.ExtensionProperty; import io.swagger.annotations.Info; @@ -25,13 +28,19 @@ import io.swagger.annotations.SwaggerDefinition; import io.swagger.annotations.Tag; import org.wso2.carbon.apimgt.annotations.api.Scope; import org.wso2.carbon.apimgt.annotations.api.Scopes; +import org.wso2.carbon.device.application.mgt.api.beans.ErrorResponse; +import org.wso2.carbon.device.application.mgt.common.Application; +import org.wso2.carbon.device.application.mgt.common.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.Platform; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import java.util.List; @SwaggerDefinition( info = @Info( @@ -40,7 +49,7 @@ import javax.ws.rs.core.Response; extensions = { @Extension(properties = { @ExtensionProperty(name = "name", value = "LifecycleManagementService"), - @ExtensionProperty(name = "context", value = "/api/application-mgt/v1.0/lifecycle"), + @ExtensionProperty(name = "context", value = "/api/application-mgt/v1.0/lifecycles"), }) } ), @@ -54,23 +63,115 @@ import javax.ws.rs.core.Response; name = "Get Lifecycle Details", description = "Get lifecycle details", key = "perm:lifecycle:get", - permissions = {"/device-mgt/lifecycle/get"} + permissions = {"/device-mgt/lifecycles/get"} ), @Scope( name = "Add a lifecycle state", description = "Add a lifecycle state", key = "perm:lifecycle:add", - permissions = {"/device-mgt/lifecycle/add"} + permissions = {"/device-mgt/lifecycles/add"} ), + @Scope( + name = "Delete a lifecycle state", + description = "Delete a lifecycle state", + key = "perm:lifecycle:delete", + permissions = {"/device-mgt/lifecycles/delete"} + ) } ) -@Path("/lifecycle") +@Path("/lifecycles") @Api(value = "Lifecycle Management", description = "This API carries all lifecycle management related operations.") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public interface LifecycleManagementAPI { + String SCOPE = "scope"; + @GET - @Path("/states") + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "get lifecycle states", + notes = "Get all lifecycle states", + tags = "Lifecycle Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:lifecycle:get") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully retrieved lifecycle states.", + response = List.class, + responseContainer = "List"), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while getting the lifecycle list.", + response = ErrorResponse.class) + }) Response getLifecycleStates(); + + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "Add a lifecycle state", + notes = "This will add a new lifecycle state", + tags = "Lifecycle Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:lifecycle:add") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 201, + message = "OK. \n Successfully add a lifecycle state.", + response = Application.class), + @ApiResponse( + code = 304, + message = "Not Modified. \n " + + "Empty body because the client already has the latest version of the requested resource."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred adding a lifecycle state.", + response = ErrorResponse.class) + }) + Response addLifecycleState(LifecycleState state); + + @Path("/{identifier}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "DELETE", + value = "Remove lifecycle state", + notes = "Remove lifecycle state", + tags = "Lifecycle Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:platform:remove") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully deleted the platform"), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while deleting the platform.", + response = ErrorResponse.class) + }) + Response deleteLifecycleState(@PathParam("identifier") String identifier); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/LifecycleManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/LifecycleManagementAPIImpl.java index 4be4b90ac7..b310d31dfd 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/LifecycleManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/LifecycleManagementAPIImpl.java @@ -38,13 +38,12 @@ import javax.ws.rs.core.Response; import java.util.ArrayList; import java.util.List; -@Path("/lifecycle") +@Path("/lifecycles") public class LifecycleManagementAPIImpl implements LifecycleManagementAPI { private static Log log = LogFactory.getLog(LifecycleManagementAPIImpl.class); @GET - @Path("/states") public Response getLifecycleStates() { LifecycleStateManager lifecycleStateManager = APIUtil.getLifecycleStateManager(); List lifecycleStates = new ArrayList<>(); @@ -59,7 +58,6 @@ public class LifecycleManagementAPIImpl implements LifecycleManagementAPI { } @POST - @Path("/states") public Response addLifecycleState(LifecycleState state) { LifecycleStateManager lifecycleStateManager = APIUtil.getLifecycleStateManager(); try { @@ -73,7 +71,7 @@ public class LifecycleManagementAPIImpl implements LifecycleManagementAPI { } @DELETE - @Path("/states/{identifier}") + @Path("/{identifier}") public Response deleteLifecycleState(@PathParam("identifier") String identifier) { LifecycleStateManager lifecycleStateManager = APIUtil.getLifecycleStateManager(); try { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml index 1347461c54..5b4af27dbe 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -27,6 +27,7 @@ http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> + @@ -35,6 +36,7 @@ http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> + diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java index d96854bb9f..ce957da19e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java @@ -44,6 +44,19 @@ public class ConnectionManagerUtil { private static ThreadLocal currentTxState = new ThreadLocal<>(); private static DataSource dataSource; + public static void openDBConnection() throws DBConnectionException { + Connection conn = currentConnection.get(); + if (conn != null) { + throw new IllegalTransactionStateException("Database connection has already been obtained."); + } + try { + conn = dataSource.getConnection(); + } catch (SQLException e) { + throw new DBConnectionException("Failed to get a database connection.", e); + } + currentConnection.set(conn); + } + public static Connection getDBConnection() throws DBConnectionException { Connection conn = currentConnection.get(); if (conn == null) {