adding application and release management implementation

merge-requests/7/head
lasantha 7 years ago
parent 1c6393959d
commit c21ddceae1

@ -40,12 +40,10 @@ public class APIUtil {
private static Log log = LogFactory.getLog(APIUtil.class); private static Log log = LogFactory.getLog(APIUtil.class);
private static ApplicationManager applicationManager; private static ApplicationManager applicationManager;
private static PlatformManager platformManager;
private static LifecycleStateManager lifecycleStateManager; private static LifecycleStateManager lifecycleStateManager;
private static ApplicationReleaseManager applicationReleaseManager; private static ApplicationReleaseManager applicationReleaseManager;
private static ApplicationStorageManager applicationStorageManager; private static ApplicationStorageManager applicationStorageManager;
private static SubscriptionManager subscriptionManager; private static SubscriptionManager subscriptionManager;
private static PlatformStorageManager platformStorageManager;
private static CategoryManager categoryManager; private static CategoryManager categoryManager;
public static ApplicationManager getApplicationManager() { public static ApplicationManager getApplicationManager() {
@ -67,24 +65,6 @@ public class APIUtil {
return applicationManager; return applicationManager;
} }
public static PlatformManager getPlatformManager() {
if (platformManager == null) {
synchronized (APIUtil.class) {
if (platformManager == null) {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
platformManager =
(PlatformManager) ctx.getOSGiService(PlatformManager.class, null);
if (platformManager == null) {
String msg = "Platform Manager service has not initialized.";
log.error(msg);
throw new IllegalStateException(msg);
}
}
}
}
return platformManager;
}
public static LifecycleStateManager getLifecycleStateManager() { public static LifecycleStateManager getLifecycleStateManager() {
if (lifecycleStateManager == null) { if (lifecycleStateManager == null) {
synchronized (APIUtil.class) { synchronized (APIUtil.class) {
@ -148,30 +128,6 @@ public class APIUtil {
return applicationStorageManager; return applicationStorageManager;
} }
/**
* To get the Platform Storage Manager from the osgi context.
*
* @return PlatformStoreManager instance in the current osgi context.
*/
public static PlatformStorageManager getPlatformStorageManager() {
if (platformStorageManager == null) {
synchronized (APIUtil.class) {
if (platformStorageManager == null) {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
platformStorageManager = (PlatformStorageManager) ctx
.getOSGiService(PlatformStorageManager.class, null);
if (platformStorageManager == null) {
String msg = "Platform Storage Manager service has not initialized.";
log.error(msg);
throw new IllegalStateException(msg);
}
}
}
}
return platformStorageManager;
}
/** /**
* To get the Category Manager from the osgi context. * To get the Category Manager from the osgi context.
* *

@ -126,95 +126,95 @@ public interface ApplicationManagementAPI {
String SCOPE = "scope"; String SCOPE = "scope";
// @GET @GET
// @Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
// @Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
// @ApiOperation( @ApiOperation(
// consumes = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON,
// produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
// httpMethod = "GET", httpMethod = "GET",
// value = "get all applications", value = "get all applications",
// notes = "This will get all applications", notes = "This will get all applications",
// tags = "Application Management", tags = "Application Management",
// extensions = { extensions = {
// @Extension(properties = { @Extension(properties = {
// @ExtensionProperty(name = SCOPE, value = "perm:application:get") @ExtensionProperty(name = SCOPE, value = "perm:application:get")
// }) })
// } }
// ) )
// @ApiResponses( @ApiResponses(
// value = { value = {
// @ApiResponse( @ApiResponse(
// code = 200, code = 200,
// message = "OK. \n Successfully got application list.", message = "OK. \n Successfully got application list.",
// response = ApplicationList.class), response = ApplicationList.class),
// @ApiResponse( @ApiResponse(
// code = 304, code = 304,
// message = "Not Modified. Empty body because the client already has the latest version " message = "Not Modified. Empty body because the client already has the latest version "
// + "of the requested resource."), + "of the requested resource."),
// @ApiResponse( @ApiResponse(
// code = 500, code = 500,
// message = "Internal Server Error. \n Error occurred while getting the application list.", message = "Internal Server Error. \n Error occurred while getting the application list.",
// response = ErrorResponse.class) response = ErrorResponse.class)
// }) })
// Response getApplications( Response getApplications(
// @ApiParam( @ApiParam(
// name = "offset", name = "offset",
// value = "Provide from which position apps should return", defaultValue = "20") value = "Provide from which position apps should return", defaultValue = "20")
// @QueryParam("offset") int offset, @QueryParam("offset") int offset,
// @ApiParam( @ApiParam(
// name = "limit", name = "limit",
// value = "Provide how many apps it should return", defaultValue = "0") value = "Provide how many apps it should return", defaultValue = "0")
// @QueryParam("limit") int limit, @QueryParam("limit") int limit,
// @ApiParam( @ApiParam(
// name = "searchQuery", name = "searchQuery",
// value = "Relevant search query to search on", defaultValue = "*") value = "Relevant search query to search on", defaultValue = "*")
// @QueryParam("searchQuery") String searchQuery @QueryParam("searchQuery") String searchQuery
// ); );
//
// @GET @GET
// @Path("/{uuid}") @Path("/{appType}")
// @Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
// @Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
// @ApiOperation( @ApiOperation(
// consumes = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON,
// produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
// httpMethod = "GET", httpMethod = "GET",
// value = "get the application specified by the UUID", value = "get the application of requesting application type",
// notes = "This will get the application identified by the UUID, if exists", notes = "This will get the application identified by the application type and name, if exists",
// tags = "Application Management", tags = "Application Management",
// extensions = { extensions = {
// @Extension(properties = { @Extension(properties = {
// @ExtensionProperty(name = SCOPE, value = "perm:application:get") @ExtensionProperty(name = SCOPE, value = "perm:application:get")
// }) })
// } }
// ) )
// @ApiResponses( @ApiResponses(
// value = { value = {
// @ApiResponse( @ApiResponse(
// code = 200, code = 200,
// message = "OK. \n Successfully retrieved relevant application.", message = "OK. \n Successfully retrieved relevant application.",
// response = Application.class), response = Application.class),
// @ApiResponse( @ApiResponse(
// code = 404, code = 404,
// message = "Application not found"), message = "Application not found"),
// @ApiResponse( @ApiResponse(
// code = 500, code = 500,
// message = "Internal Server Error. \n Error occurred while getting relevant application.", message = "Internal Server Error. \n Error occurred while getting relevant application.",
// response = ErrorResponse.class) response = ErrorResponse.class)
// }) })
// Response getApplication( Response getApplication(
// @ApiParam( @ApiParam(
// name = "uuid", name = "appType",
// value = "UUID of the application", value = "Type of the application",
// required = true) required = true)
// @PathParam("uuid") String uuid, @PathParam("appType") String appType,
// @ApiParam( @ApiParam(
// name = "isWithImages", name = "appName",
// value = "Whether to return application with images", value = "Application name",
// required = false) required = true)
// @QueryParam("isWithImages") Boolean isWithImages @QueryParam("isWithImages") String appName
// ); );
@PUT @PUT
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@ -321,7 +321,7 @@ public interface ApplicationManagementAPI {
Response createApplicationRelease( Response createApplicationRelease(
@Multipart(value = "applicationRelease", type = "application/json") ApplicationRelease applicationRelease, @Multipart(value = "applicationRelease", type = "application/json") ApplicationRelease applicationRelease,
@Multipart(value = "binaryFile") Attachment binaryFile, @Multipart(value = "binaryFile") Attachment binaryFile,
@PathParam("appId") int applicationId)); @PathParam("appId") int applicationId);
@POST @POST

@ -69,85 +69,114 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
private static final int DEFAULT_LIMIT = 20; private static final int DEFAULT_LIMIT = 20;
private static Log log = LogFactory.getLog(ApplicationManagementAPIImpl.class); private static Log log = LogFactory.getLog(ApplicationManagementAPIImpl.class);
// @GET @GET
// @Consumes("application/json") @Consumes("application/json")
// public Response getApplications(@QueryParam("offset") int offset, @QueryParam("limit") int limit, public Response getApplications(@QueryParam("offset") int offset, @QueryParam("limit") int limit,
// @QueryParam("query") String searchQuery) { @QueryParam("query") String searchQuery) {
// ApplicationManager applicationManager = APIUtil.getApplicationManager(); ApplicationManager applicationManager = APIUtil.getApplicationManager();
// ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
//
// try { try {
// if (limit == 0) { if (limit == 0) {
// limit = DEFAULT_LIMIT; limit = DEFAULT_LIMIT;
// } }
// Filter filter = new Filter(); Filter filter = new Filter();
// filter.setOffset(offset); filter.setOffset(offset);
// filter.setLimit(limit); filter.setLimit(limit);
// filter.setSearchQuery(searchQuery); filter.setSearchQuery(searchQuery);
//
// ApplicationList applications = applicationManager.getApplications(filter); ApplicationList applications = applicationManager.getApplications(filter);
//
// for (Application application : applications.getApplications()) { for (Application application : applications.getApplications()) {
// ImageArtifact imageArtifact = applicationStorageManager.getImageArtifact(application.getUuid(), // ToDo : use better approach to solve this
// Constants.IMAGE_ARTIFACTS[0], 0); String uuId = applicationManager.getUuidOfLatestRelease(application.getId());
// application.setIcon(imageArtifact); if (uuId != null){
// } application.setUuidOfLatestRelease(uuId);
// return Response.status(Response.Status.OK).entity(applications).build(); ImageArtifact imageArtifact = applicationStorageManager.getImageArtifact(uuId, Constants.IMAGE_ARTIFACTS[0], 0);
// } catch (NotFoundException e) { application.setIconOfLatestRelease(imageArtifact);
// return Response.status(Response.Status.NOT_FOUND).build(); }else{
// } catch (ApplicationManagementException e) { // ToDo set default icon
// String msg = "Error occurred while getting the application list"; }
// log.error(msg, e); }
// return Response.status(Response.Status.BAD_REQUEST).build(); return Response.status(Response.Status.OK).entity(applications).build();
// } catch (ApplicationStorageManagementException e) { } catch (NotFoundException e) {
// log.error("Error occurred while getting the image artifacts of the application", e); return Response.status(Response.Status.NOT_FOUND).build();
// return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } catch (ApplicationManagementException e) {
// } String msg = "Error occurred while getting the application list";
// } log.error(msg, e);
// return Response.status(Response.Status.BAD_REQUEST).build();
// @GET } catch (ApplicationStorageManagementException e) {
// @Consumes("application/json") log.error("Error occurred while getting the image artifacts of the application", e);
// @Path("/{uuid}") return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
// public Response getApplication(@PathParam("uuid") String uuid, @QueryParam("isWithImages") Boolean isWithImages) { }
// ApplicationManager applicationManager = APIUtil.getApplicationManager(); }
// ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
// try { @GET
// Application application = applicationManager.getApplication(uuid); @Consumes("application/json")
// if (application == null) { @Path("/{appType}")
// return Response.status(Response.Status.NOT_FOUND) public Response getApplication(@PathParam("appType") String appType, @QueryParam("appName") String appName) {
// .entity("Application with UUID " + uuid + " not found").build(); ApplicationManager applicationManager = APIUtil.getApplicationManager();
// } ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
// try {
// if (isWithImages != null && isWithImages) { Application application = applicationManager.getApplication(appType, appName);
// ImageArtifact icon = applicationStorageManager.getImageArtifact(uuid, Constants.IMAGE_ARTIFACTS[0], 0); if (application == null) {
// ImageArtifact banner = applicationStorageManager.getImageArtifact(uuid, Constants.IMAGE_ARTIFACTS[1], return Response.status(Response.Status.NOT_FOUND)
// 0); .entity("Application with UUID " + appType + " not found").build();
// int screenShotCount = application.getScreenShotCount(); }
// for (int count = 1; count < screenShotCount; count++) {
// ImageArtifact screenShot = applicationStorageManager.getImageArtifact(uuid, Constants // ToDo : use better approach to solve this
// .IMAGE_ARTIFACTS[2], count); String uuId = applicationManager.getUuidOfLatestRelease(application.getId());
// application.addScreenShot(screenShot); if (uuId != null){
// } application.setUuidOfLatestRelease(uuId);
// application.setIcon(icon); ImageArtifact imageArtifact = applicationStorageManager.getImageArtifact(uuId, Constants.IMAGE_ARTIFACTS[0], 0);
// application.setBanner(banner); application.setIconOfLatestRelease(imageArtifact);
// } }else{
// return Response.status(Response.Status.OK).entity(application).build(); // ToDo set default icon
// } catch (NotFoundException e) { }
// return Response.status(Response.Status.NOT_FOUND).build();
// } catch (ApplicationManagementException e) { return Response.status(Response.Status.OK).entity(application).build();
// log.error("Error occurred while getting application with the uuid " + uuid, e); } catch (NotFoundException e) {
// return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); return Response.status(Response.Status.NOT_FOUND).build();
// } catch (ApplicationStorageManagementException e) { } catch (ApplicationManagementException e) {
// log.error("Error occurred while getting the image artifacts of the application with the uuid " + uuid, e); log.error("Error occurred while getting application with the uuid " + appType, e);
// return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
// } } catch (ApplicationStorageManagementException e) {
// } log.error("Error occurred while getting the image artifacts of the application with the uuid " + appType, e);
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
}
}
@POST
@Consumes("application/json")
public Response createApplication(@Valid Application application) {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
try {
Application createdApplication = applicationManager.createApplication(application);
if (application != null){
return Response.status(Response.Status.CREATED).entity(createdApplication).build();
}else{
String msg = "Given device type is not matched with existing device types";
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).build();
}
}catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting the device type";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).build();
}catch (ApplicationManagementException e) {
String msg = "Error occurred while creating the application";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).build();
}
}
// ToDo
@PUT @PUT
@Consumes("application/json") @Consumes("application/json")
@Path("/{uuid}/lifecycle") @Path("/{uuid}/lifecycle")
public Response changeLifecycleState(@PathParam("uuid") String applicationUUID, public Response changeLifecycleState(@PathParam("uuid") String applicationUUID, @QueryParam("state") String state) {
@QueryParam("state") String state) {
ApplicationManager applicationManager = APIUtil.getApplicationManager(); ApplicationManager applicationManager = APIUtil.getApplicationManager();
if (!Arrays.asList(Constants.LIFE_CYCLES).contains(state)) { if (!Arrays.asList(Constants.LIFE_CYCLES).contains(state)) {
@ -199,33 +228,6 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
} }
} }
@POST
@Consumes("application/json")
public Response createApplication(@Valid Application application) {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
try {
Application createdApplication = applicationManager.createApplication(application);
if (application != null){
return Response.status(Response.Status.CREATED).entity(createdApplication).build();
}else{
String msg = "Given device type is not matched with existing device types";
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).build();
}
}catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting the device type";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).build();
}catch (ApplicationManagementException e) {
String msg = "Error occurred while creating the application";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).build();
}
}
//working on this//
@Override @Override
@POST @POST
@Path("{appId}/release") @Path("{appId}/release")
@ -258,7 +260,6 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
} }
} }
//working on this//
@Override @Override
@POST @POST
@ -393,39 +394,39 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
} }
} }
// @Override @Override
// @POST @POST
// @Path("/release/{uuid}") @Path("/release/{uuid}")
// public Response createApplicationRelease(@PathParam("uuid") String applicationUUID, public Response createApplicationRelease(@PathParam("uuid") String applicationUUID,
// @Multipart("applicationRelease") ApplicationRelease applicationRelease, @Multipart("applicationRelease") ApplicationRelease applicationRelease,
// @Multipart("binaryFile") Attachment binaryFile) { @Multipart("binaryFile") Attachment binaryFile) {
// ApplicationReleaseManager applicationReleaseManager = APIUtil.getApplicationReleaseManager(); ApplicationReleaseManager applicationReleaseManager = APIUtil.getApplicationReleaseManager();
// ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
// try { try {
// applicationRelease = applicationReleaseManager.createRelease(applicationUUID, applicationRelease); applicationRelease = applicationReleaseManager.createRelease(applicationUUID, applicationRelease);
//
// if (binaryFile != null) { if (binaryFile != null) {
// applicationStorageManager.uploadReleaseArtifacts(applicationUUID, applicationRelease.getVersion(), applicationStorageManager.uploadReleaseArtifacts(applicationUUID, applicationRelease.getVersion(),
// binaryFile.getDataHandler().getInputStream()); binaryFile.getDataHandler().getInputStream());
// } }
// return Response.status(Response.Status.CREATED).entity(applicationRelease).build(); return Response.status(Response.Status.CREATED).entity(applicationRelease).build();
// } catch (ApplicationManagementException e) { } catch (ApplicationManagementException e) {
// log.error("Error while creating an application release for the application with UUID " + applicationUUID, log.error("Error while creating an application release for the application with UUID " + applicationUUID,
// e); e);
// return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
// } catch (IOException e) { } catch (IOException e) {
// String errorMessage = String errorMessage =
// "Error while uploading binary file for the application release of the application with UUID " "Error while uploading binary file for the application release of the application with UUID "
// + applicationUUID; + applicationUUID;
// log.error(errorMessage, e); log.error(errorMessage, e);
// return APIUtil.getResponse(new ApplicationManagementException(errorMessage, e), return APIUtil.getResponse(new ApplicationManagementException(errorMessage, e),
// Response.Status.INTERNAL_SERVER_ERROR); Response.Status.INTERNAL_SERVER_ERROR);
// } catch (ResourceManagementException e) { } catch (ResourceManagementException e) {
// log.error("Error occurred while uploading the releases artifacts of the application with the uuid " log.error("Error occurred while uploading the releases artifacts of the application with the uuid "
// + applicationUUID + " for the release " + applicationRelease.getVersion(), e); + applicationUUID + " for the release " + applicationRelease.getVersion(), e);
// return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
// } }
// } }
@Override @Override
@PUT @PUT

@ -91,6 +91,10 @@ public class Application {
this.user = user; this.user = user;
} }
public String uuidOfLatestRelease;
public ImageArtifact iconOfLatestRelease;
public List<UnrestrictedRole> getUnrestrictedRoles() { public List<UnrestrictedRole> getUnrestrictedRoles() {
return unrestrictedRoles; return unrestrictedRoles;
} }
@ -146,4 +150,20 @@ public class Application {
public void setDevicetype(DeviceType devicetype) { public void setDevicetype(DeviceType devicetype) {
this.devicetype = devicetype; this.devicetype = devicetype;
} }
public String getUuidOfLatestRelease() {
return uuidOfLatestRelease;
}
public void setUuidOfLatestRelease(String uuidOfLatestRelease) {
this.uuidOfLatestRelease = uuidOfLatestRelease;
}
public ImageArtifact getIconOfLatestRelease() {
return iconOfLatestRelease;
}
public void setIconOfLatestRelease(ImageArtifact iconOfLatestRelease) {
this.iconOfLatestRelease = iconOfLatestRelease;
}
} }

@ -64,6 +64,9 @@ public interface ApplicationManager {
*/ */
ApplicationList getApplications(Filter filter) throws ApplicationManagementException; ApplicationList getApplications(Filter filter) throws ApplicationManagementException;
String getUuidOfLatestRelease(int appId) throws ApplicationManagementException;
/** /**
* To change the lifecycle of the Application. * To change the lifecycle of the Application.
* *
@ -87,9 +90,10 @@ public interface ApplicationManager {
/** /**
* To get Application with the given UUID. * To get Application with the given UUID.
* *
* @param uuid UUID of the Application * @param appType type of the Application
* @param appName name of the Application
* @return the Application identified by the UUID * @return the Application identified by the UUID
* @throws ApplicationManagementException Application Management Exception. * @throws ApplicationManagementException Application Management Exception.
*/ */
Application getApplication(String uuid) throws ApplicationManagementException; Application getApplication(String appType, String appName) throws ApplicationManagementException;
} }

@ -65,6 +65,14 @@ public interface ApplicationDAO {
*/ */
ApplicationList getApplications(Filter filter, int tenantId) throws ApplicationManagementDAOException; ApplicationList getApplications(Filter filter, int tenantId) throws ApplicationManagementDAOException;
/**
* To get the UUID of latest app release that satisfy the given criteria.
*
* @param appId application id
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
String getUuidOfLatestRelease(int appId) throws ApplicationManagementDAOException;
/** /**
* To get the application with the given uuid * To get the application with the given uuid
* *

@ -179,7 +179,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
} }
} }
sql += " LIMIT ? OFFSET ?"; sql += " LIMIT ? OFFSET ? ORDER BY DESC APP_ID";
pagination.setLimit(filter.getLimit()); pagination.setLimit(filter.getLimit());
pagination.setOffset(filter.getOffset()); pagination.setOffset(filter.getOffset());
@ -220,6 +220,42 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
return applicationList; return applicationList;
} }
@Override
public String getUuidOfLatestRelease(int appId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Getting UUID from the latest app release");
}
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
String sql = "";
int index = 0;
String uuId = null;
try {
conn = this.getDBConnection();
sql += "SELECT APP_RELEASE.UUID AS UUID FROM AP_APP_RELEASE AS APP_RELEASE, AP_APP_LIFECYCLE_STATE "
+ "AS LIFECYCLE WHERE APP_RELEASE.AP_APP_ID=? AND APP_RELEASE.ID = LIFECYCLE.AP_APP_RELEASE_ID "
+ "AND LIFECYCLE.CURRENT_STATE = ? order by APP_RELEASE.ID DESC;";
stmt = conn.prepareStatement(sql);
stmt.setInt(++index, appId);
stmt.setString(++index, "PUBLISHED");
rs = stmt.executeQuery();
if (rs.next()) {
uuId = rs.getString("UUID");
}
return uuId;
} catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while getting uuid of latest app release", e);
} catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection for "
+ "getting app release id", e);
} finally {
Util.cleanupResources(stmt, rs);
}
}
@Override @Override
public int getApplicationCount(Filter filter) throws ApplicationManagementDAOException { public int getApplicationCount(Filter filter) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {

@ -21,6 +21,7 @@ package org.wso2.carbon.device.application.mgt.core.impl;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.application.mgt.common.*; import org.wso2.carbon.device.application.mgt.common.*;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException;
@ -216,30 +217,72 @@ public class ApplicationManagerImpl implements ApplicationManager {
public ApplicationList getApplications(Filter filter) throws ApplicationManagementException { public ApplicationList getApplications(Filter filter) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
ApplicationList roleRestrictedApplicationList = new ApplicationList();
ApplicationList applicationList ;
try { try {
if (isAuthorized(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { filter.setUserName(userName);
userName = "ALL"; ConnectionManagerUtil.openDBConnection();
applicationList = applicationDAO.getApplications(filter, tenantId);
if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) {
return applicationList;
} }
} catch (UserStoreException e) { for (Application application : applicationList.getApplications()) {
if (application.getUnrestrictedRoles().isEmpty()){
roleRestrictedApplicationList.getApplications().add(application);
}else{
if (isRoleExists(application.getUnrestrictedRoles(), userName){
roleRestrictedApplicationList.getApplications().add(application);
}
}
}
return roleRestrictedApplicationList;
}catch (UserStoreException e) {
throw new ApplicationManagementException("User-store exception while checking whether the user " + throw new ApplicationManagementException("User-store exception while checking whether the user " +
userName + " of tenant " + tenantId + " has the publisher permission"); userName + " of tenant " + tenantId + " has the publisher permission");
} }
filter.setUserName(userName); finally {
ConnectionManagerUtil.closeDBConnection();
}
}
@Override
public String getUuidOfLatestRelease(int appId) throws ApplicationManagementException {
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
ApplicationDAO applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); return applicationDAO.getUuidOfLatestRelease(appId);
ApplicationList applicationList = applicationDAO.getApplications(filter, tenantId); }finally {
for (Application application : applicationList.getApplications()) {
application.setVisibility(DataHolder.getInstance().getVisibilityManager().get(application.getId()));
}
return applicationList;
} finally {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} }
private boolean isRoleExists(List<UnrestrictedRole> unrestrictedRoleList, String userName)
throws UserStoreException {
String[] roleList;
roleList = getRoleOfUser(userName);
for (UnrestrictedRole unrestrictedRole : unrestrictedRoleList) {
for (String role : roleList) {
if (unrestrictedRole.getRole().equals(role)) {
return true;
}
}
}
return false;
}
private String[] getRoleOfUser(String userName) throws UserStoreException {
UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm();
String[] roleList = {};
if (userRealm != null) {
roleList = userRealm.getUserStoreManager().getRoleListOfUser(userName);
} else {
log.error("role list is empty of user :"+ userName);
}
return roleList;
}
@Override @Override
public void changeLifecycle(String applicationUuid, String lifecycleIdentifier) throws public void changeLifecycle(String applicationUuid, String lifecycleIdentifier) throws
ApplicationManagementException { ApplicationManagementException {
@ -305,7 +348,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
// log.debug("In order to make the state change to " + transition.getNextState() + " permission " // log.debug("In order to make the state change to " + transition.getNextState() + " permission "
// + permission + " is required"); // + permission + " is required");
// } // }
// if (isAuthorized(userName, tenantId, permission)) { // if (isAdminUser(userName, tenantId, permission)) {
// filteredTransitions.add(transition); // filteredTransitions.add(transition);
// } else { // } else {
// if (log.isDebugEnabled()) { // if (log.isDebugEnabled()) {
@ -333,28 +376,34 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
@Override @Override
public Application getApplication(String uuid) throws ApplicationManagementException { public Application getApplication(String appType, String appName) throws ApplicationManagementException {
// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
// String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
// Application application;
// try { boolean isAppAllowed = false;
// if (isAuthorized(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { try {
// userName = "ALL"; ConnectionManagerUtil.openDBConnection();
// } application = ApplicationManagementDAOFactory.getApplicationDAO().getApplication(appType, appName, tenantId);
// } catch (UserStoreException e) { if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) {
// throw new ApplicationManagementException( return application;
// "User-store exception while getting application with the UUID " + uuid); }
// }
// try { if (application.getUnrestrictedRoles().isEmpty()){
// ConnectionManagerUtil.openDBConnection(); isAppAllowed = true;
// Application application = ApplicationManagementDAOFactory.getApplicationDAO().getApplication(uuid, tenantId, userName); }else if(isRoleExists(application.getUnrestrictedRoles(), userName)){
// if (application != null) { isAppAllowed = true ;
// application.setVisibility(DataHolder.getInstance().getVisibilityManager().get(application.getId())); }
// }
// return application; if (!isAppAllowed){
// } finally { return null;
// ConnectionManagerUtil.closeDBConnection(); }
// } return application;
} catch (UserStoreException e) {
throw new ApplicationManagementException("User-store exception while getting application with the "
+ "application name " + appName);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
} }
/** /**
@ -367,7 +416,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
private boolean isApplicationOwnerOrAdmin(String applicationUUID, String userName, int tenantId) private boolean isApplicationOwnerOrAdmin(String applicationUUID, String userName, int tenantId)
throws ApplicationManagementException { throws ApplicationManagementException {
// try { // try {
// if (isAuthorized(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { // if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) {
// return true; // return true;
// } // }
// } catch (UserStoreException e) { // } catch (UserStoreException e) {
@ -393,7 +442,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
* @return true if the current user has the permission, otherwise false. * @return true if the current user has the permission, otherwise false.
* @throws UserStoreException UserStoreException * @throws UserStoreException UserStoreException
*/ */
private boolean isAuthorized(String username, int tenantId, String permission) throws UserStoreException { private boolean isAdminUser(String username, int tenantId, String permission) throws UserStoreException {
UserRealm userRealm = DataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId); UserRealm userRealm = DataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId);
return userRealm != null && userRealm.getAuthorizationManager() != null && userRealm.getAuthorizationManager() return userRealm != null && userRealm.getAuthorizationManager() != null && userRealm.getAuthorizationManager()
.isUserAuthorized(MultitenantUtils.getTenantAwareUsername(username), .isUserAuthorized(MultitenantUtils.getTenantAwareUsername(username),

@ -73,7 +73,6 @@ public class ServiceComponent {
BundleContext bundleContext = componentContext.getBundleContext(); BundleContext bundleContext = componentContext.getBundleContext();
try { try {
String datasourceName = ConfigurationManager.getInstance().getConfiguration().getDatasourceName(); String datasourceName = ConfigurationManager.getInstance().getConfiguration().getDatasourceName();
// ApplicationManagementDAOFactory.init(datasourceName);
ApplicationManager applicationManager = ApplicationManagementUtil.getApplicationManagerInstance(); ApplicationManager applicationManager = ApplicationManagementUtil.getApplicationManagerInstance();
DataHolder.getInstance().setApplicationManager(applicationManager); DataHolder.getInstance().setApplicationManager(applicationManager);
@ -96,11 +95,6 @@ public class ServiceComponent {
DataHolder.getInstance().setLifecycleStateManager(lifecycleStateManager); DataHolder.getInstance().setLifecycleStateManager(lifecycleStateManager);
bundleContext.registerService(LifecycleStateManager.class.getName(), lifecycleStateManager, null); bundleContext.registerService(LifecycleStateManager.class.getName(), lifecycleStateManager, null);
//remove this
PlatformManager platformManager = ApplicationManagementUtil.getPlatformManagerInstance();
DataHolder.getInstance().setPlatformManager(platformManager);
bundleContext.registerService(PlatformManager.class.getName(), platformManager, null);
SubscriptionManager subscriptionManager = ApplicationManagementUtil.getSubscriptionManagerInstance(); SubscriptionManager subscriptionManager = ApplicationManagementUtil.getSubscriptionManagerInstance();
DataHolder.getInstance().setSubscriptionManager(subscriptionManager); DataHolder.getInstance().setSubscriptionManager(subscriptionManager);
bundleContext.registerService(SubscriptionManager.class.getName(), subscriptionManager, null); bundleContext.registerService(SubscriptionManager.class.getName(), subscriptionManager, null);
@ -114,16 +108,6 @@ public class ServiceComponent {
DataHolder.getInstance().setApplicationStorageManager(applicationStorageManager); DataHolder.getInstance().setApplicationStorageManager(applicationStorageManager);
bundleContext.registerService(ApplicationStorageManager.class.getName(), applicationStorageManager, null); bundleContext.registerService(ApplicationStorageManager.class.getName(), applicationStorageManager, null);
//can remove
PlatformStorageManager platformStorageManager = ApplicationManagementUtil
.getPlatformStorageManagerInstance();
DataHolder.getInstance().setPlatformStorageManager(platformStorageManager);
bundleContext.registerService(PlatformStorageManager.class.getName(), platformStorageManager, null);
//can remove
bundleContext.registerService(Axis2ConfigurationContextObserver.class.getName(),
new PlatformManagementAxis2ConfigurationObserverImpl(), null);
ApplicationManagementDAOFactory.init(datasourceName); ApplicationManagementDAOFactory.init(datasourceName);
ApplicationManagementDAOFactory.initDatabases(); ApplicationManagementDAOFactory.initDatabases();
log.info("ApplicationManagement core bundle has been successfully initialized"); log.info("ApplicationManagement core bundle has been successfully initialized");

@ -73,12 +73,6 @@ public class ApplicationManagementUtil {
return getInstance(extension, LifecycleStateManager.class); return getInstance(extension, LifecycleStateManager.class);
} }
public static PlatformManager getPlatformManagerInstance() throws InvalidConfigurationException {
ConfigurationManager configurationManager = ConfigurationManager.getInstance();
Extension extension = configurationManager.getExtension(Extension.Name.PlatformManager);
return getInstance(extension, PlatformManager.class);
}
public static VisibilityManager getVisibilityManagerInstance() throws InvalidConfigurationException { public static VisibilityManager getVisibilityManagerInstance() throws InvalidConfigurationException {
ConfigurationManager configurationManager = ConfigurationManager.getInstance(); ConfigurationManager configurationManager = ConfigurationManager.getInstance();
Extension extension = configurationManager.getExtension(Extension.Name.VisibilityManager); Extension extension = configurationManager.getExtension(Extension.Name.VisibilityManager);
@ -98,15 +92,6 @@ public class ApplicationManagementUtil {
return getInstance(extension, ApplicationStorageManager.class); return getInstance(extension, ApplicationStorageManager.class);
} }
//can remove
public static PlatformStorageManager getPlatformStorageManagerInstance() throws
InvalidConfigurationException {
ConfigurationManager configurationManager = ConfigurationManager.getInstance();
Extension extension = configurationManager.getExtension(Extension.Name.PlatformStorageManager);
return getInstance(extension, PlatformStorageManager.class);
}
private static <T> T getInstance(Extension extension, Class<T> cls) throws InvalidConfigurationException { private static <T> T getInstance(Extension extension, Class<T> cls) throws InvalidConfigurationException {
try { try {
Class theClass = Class.forName(extension.getClassName()); Class theClass = Class.forName(extension.getClassName());

Loading…
Cancel
Save