@Test(expected = InternalErrorException.class) public void testGenerateAndDeployJVMFailsChangeFileMode() throws CommandFailureException { Jvm mockJvm = mock(Jvm.class); CommandOutput commandOutputFails = new CommandOutput(new ExecReturnCode(1), "", "Fail creating the directory"); CommandOutput commandOutputSucceeds = new CommandOutput(new ExecReturnCode(0), "SUCCESS", ""); when(Config.mockJvmPersistenceService.findJvmByExactName(anyString())).thenReturn(mockJvm); when(Config.mockJvmControlService.executeCreateDirectoryCommand(any(Jvm.class), anyString())).thenReturn(commandOutputSucceeds); when(Config.mockJvmControlService.secureCopyFile(any(ControlJvmRequest.class), anyString(), anyString(), anyString(), anyBoolean())).thenReturn(commandOutputSucceeds); when(Config.mockJvmControlService.executeChangeFileModeCommand(any(Jvm.class), anyString(), anyString(), anyString())).thenReturn(commandOutputFails); when(mockJvm.getId()).thenReturn(new Identifier<Jvm>(111L)); when(mockJvm.getState()).thenReturn(JvmState.JVM_STOPPED); jvmService.generateAndDeployJvm("test-jvm-fails-started", Config.mockUser); }
@Override public Jvm generateAndDeployJvm(String jvmName, User user) { boolean didSucceed = false; Jvm jvm = getJvm(jvmName); LOGGER.debug("Start generateAndDeployJvm for {} by user {}", jvmName, user.getId()); historyFacadeService.write(jvm.getHostName(), jvm.getGroups(), "Starting to generate remote JVM " + jvm.getJvmName() + " on host " + jvm.getHostName(), EventType.USER_ACTION_INFO, user.getId()); binaryDistributionLockManager.writeLock(jvmName + "-" + jvm.getId().toString()); try { if (jvm.getState().isStartedState()) { final String errorMessage = "The remote JVM " + jvm.getJvmName() + " must be stopped before attempting to generate the JVM"; LOGGER.info(errorMessage); throw new InternalErrorException(FaultType.REMOTE_COMMAND_FAILURE, errorMessage); } validateJvmAndAppResources(jvm); checkForJvmBinaries(jvm); distributeBinaries(jvm); createScriptsDirectory(jvm); deployScriptsToUserJwalaScriptsDir(jvm, user); deleteJvmService(jvm, user); final String jvmConfigJar = generateJvmConfigJar(jvm); secureCopyJvmConfigJar(jvm, jvmConfigJar, user); deployJvmConfigJar(jvm, user, jvmConfigJar); deployJvmResourceFiles(jvm, user); deployApplicationContextXMLs(jvm, user); installJvmWindowsService(jvm, user); updateState(jvm.getId(), JvmState.JVM_STOPPED); didSucceed = true; } catch (CommandFailureException | IOException e) { LOGGER.error("Failed to generate the JVM config for {}", jvm.getJvmName(), e); throw new InternalErrorException(FaultType.REMOTE_COMMAND_FAILURE, "Failed to generate the JVM config: " + jvm.getJvmName(), e); } finally { binaryDistributionLockManager.writeUnlock(jvmName + "-" + jvm.getId().toString()); LOGGER.debug("End generateAndDeployJvm for {} by user {}", jvmName, user.getId()); final EventType eventType = didSucceed ? EventType.SYSTEM_INFO : EventType.SYSTEM_ERROR; String historyMessage = didSucceed ? "Remote generation of jvm " + jvm.getJvmName() + " to host " + jvm.getHostName() + " succeeded" : "Remote generation of jvm " + jvm.getJvmName() + " to host " + jvm.getHostName() + " failed"; historyFacadeService.write(jvm.getHostName(), jvm.getGroups(), historyMessage, eventType, user.getId()); } return jvm; }
JvmServiceImpl implements JvmService { @Override public Jvm generateAndDeployJvm(String jvmName, User user) { boolean didSucceed = false; Jvm jvm = getJvm(jvmName); LOGGER.debug("Start generateAndDeployJvm for {} by user {}", jvmName, user.getId()); historyFacadeService.write(jvm.getHostName(), jvm.getGroups(), "Starting to generate remote JVM " + jvm.getJvmName() + " on host " + jvm.getHostName(), EventType.USER_ACTION_INFO, user.getId()); binaryDistributionLockManager.writeLock(jvmName + "-" + jvm.getId().toString()); try { if (jvm.getState().isStartedState()) { final String errorMessage = "The remote JVM " + jvm.getJvmName() + " must be stopped before attempting to generate the JVM"; LOGGER.info(errorMessage); throw new InternalErrorException(FaultType.REMOTE_COMMAND_FAILURE, errorMessage); } validateJvmAndAppResources(jvm); checkForJvmBinaries(jvm); distributeBinaries(jvm); createScriptsDirectory(jvm); deployScriptsToUserJwalaScriptsDir(jvm, user); deleteJvmService(jvm, user); final String jvmConfigJar = generateJvmConfigJar(jvm); secureCopyJvmConfigJar(jvm, jvmConfigJar, user); deployJvmConfigJar(jvm, user, jvmConfigJar); deployJvmResourceFiles(jvm, user); deployApplicationContextXMLs(jvm, user); installJvmWindowsService(jvm, user); updateState(jvm.getId(), JvmState.JVM_STOPPED); didSucceed = true; } catch (CommandFailureException | IOException e) { LOGGER.error("Failed to generate the JVM config for {}", jvm.getJvmName(), e); throw new InternalErrorException(FaultType.REMOTE_COMMAND_FAILURE, "Failed to generate the JVM config: " + jvm.getJvmName(), e); } finally { binaryDistributionLockManager.writeUnlock(jvmName + "-" + jvm.getId().toString()); LOGGER.debug("End generateAndDeployJvm for {} by user {}", jvmName, user.getId()); final EventType eventType = didSucceed ? EventType.SYSTEM_INFO : EventType.SYSTEM_ERROR; String historyMessage = didSucceed ? "Remote generation of jvm " + jvm.getJvmName() + " to host " + jvm.getHostName() + " succeeded" : "Remote generation of jvm " + jvm.getJvmName() + " to host " + jvm.getHostName() + " failed"; historyFacadeService.write(jvm.getHostName(), jvm.getGroups(), historyMessage, eventType, user.getId()); } return jvm; } }
JvmServiceImpl implements JvmService { @Override public Jvm generateAndDeployJvm(String jvmName, User user) { boolean didSucceed = false; Jvm jvm = getJvm(jvmName); LOGGER.debug("Start generateAndDeployJvm for {} by user {}", jvmName, user.getId()); historyFacadeService.write(jvm.getHostName(), jvm.getGroups(), "Starting to generate remote JVM " + jvm.getJvmName() + " on host " + jvm.getHostName(), EventType.USER_ACTION_INFO, user.getId()); binaryDistributionLockManager.writeLock(jvmName + "-" + jvm.getId().toString()); try { if (jvm.getState().isStartedState()) { final String errorMessage = "The remote JVM " + jvm.getJvmName() + " must be stopped before attempting to generate the JVM"; LOGGER.info(errorMessage); throw new InternalErrorException(FaultType.REMOTE_COMMAND_FAILURE, errorMessage); } validateJvmAndAppResources(jvm); checkForJvmBinaries(jvm); distributeBinaries(jvm); createScriptsDirectory(jvm); deployScriptsToUserJwalaScriptsDir(jvm, user); deleteJvmService(jvm, user); final String jvmConfigJar = generateJvmConfigJar(jvm); secureCopyJvmConfigJar(jvm, jvmConfigJar, user); deployJvmConfigJar(jvm, user, jvmConfigJar); deployJvmResourceFiles(jvm, user); deployApplicationContextXMLs(jvm, user); installJvmWindowsService(jvm, user); updateState(jvm.getId(), JvmState.JVM_STOPPED); didSucceed = true; } catch (CommandFailureException | IOException e) { LOGGER.error("Failed to generate the JVM config for {}", jvm.getJvmName(), e); throw new InternalErrorException(FaultType.REMOTE_COMMAND_FAILURE, "Failed to generate the JVM config: " + jvm.getJvmName(), e); } finally { binaryDistributionLockManager.writeUnlock(jvmName + "-" + jvm.getId().toString()); LOGGER.debug("End generateAndDeployJvm for {} by user {}", jvmName, user.getId()); final EventType eventType = didSucceed ? EventType.SYSTEM_INFO : EventType.SYSTEM_ERROR; String historyMessage = didSucceed ? "Remote generation of jvm " + jvm.getJvmName() + " to host " + jvm.getHostName() + " succeeded" : "Remote generation of jvm " + jvm.getJvmName() + " to host " + jvm.getHostName() + " failed"; historyFacadeService.write(jvm.getHostName(), jvm.getGroups(), historyMessage, eventType, user.getId()); } return jvm; } JvmServiceImpl(final JvmPersistenceService jvmPersistenceService,
                          final GroupPersistenceService groupPersistenceService,
                          final ApplicationService applicationService,
                          final SimpMessagingTemplate messagingTemplate,
                          final GroupStateNotificationService groupStateNotificationService,
                          final ResourceService resourceService,
                          final ClientFactoryHelper clientFactoryHelper,
                          final String topicServerStates,
                          final JvmControlService jvmControlService,
                          final BinaryDistributionService binaryDistributionService,
                          final BinaryDistributionLockManager binaryDistributionLockManager,
                          final HistoryFacadeService historyFacadeService,
                          final FileUtility fileUtility); }
JvmServiceImpl implements JvmService { @Override public Jvm generateAndDeployJvm(String jvmName, User user) { boolean didSucceed = false; Jvm jvm = getJvm(jvmName); LOGGER.debug("Start generateAndDeployJvm for {} by user {}", jvmName, user.getId()); historyFacadeService.write(jvm.getHostName(), jvm.getGroups(), "Starting to generate remote JVM " + jvm.getJvmName() + " on host " + jvm.getHostName(), EventType.USER_ACTION_INFO, user.getId()); binaryDistributionLockManager.writeLock(jvmName + "-" + jvm.getId().toString()); try { if (jvm.getState().isStartedState()) { final String errorMessage = "The remote JVM " + jvm.getJvmName() + " must be stopped before attempting to generate the JVM"; LOGGER.info(errorMessage); throw new InternalErrorException(FaultType.REMOTE_COMMAND_FAILURE, errorMessage); } validateJvmAndAppResources(jvm); checkForJvmBinaries(jvm); distributeBinaries(jvm); createScriptsDirectory(jvm); deployScriptsToUserJwalaScriptsDir(jvm, user); deleteJvmService(jvm, user); final String jvmConfigJar = generateJvmConfigJar(jvm); secureCopyJvmConfigJar(jvm, jvmConfigJar, user); deployJvmConfigJar(jvm, user, jvmConfigJar); deployJvmResourceFiles(jvm, user); deployApplicationContextXMLs(jvm, user); installJvmWindowsService(jvm, user); updateState(jvm.getId(), JvmState.JVM_STOPPED); didSucceed = true; } catch (CommandFailureException | IOException e) { LOGGER.error("Failed to generate the JVM config for {}", jvm.getJvmName(), e); throw new InternalErrorException(FaultType.REMOTE_COMMAND_FAILURE, "Failed to generate the JVM config: " + jvm.getJvmName(), e); } finally { binaryDistributionLockManager.writeUnlock(jvmName + "-" + jvm.getId().toString()); LOGGER.debug("End generateAndDeployJvm for {} by user {}", jvmName, user.getId()); final EventType eventType = didSucceed ? EventType.SYSTEM_INFO : EventType.SYSTEM_ERROR; String historyMessage = didSucceed ? "Remote generation of jvm " + jvm.getJvmName() + " to host " + jvm.getHostName() + " succeeded" : "Remote generation of jvm " + jvm.getJvmName() + " to host " + jvm.getHostName() + " failed"; historyFacadeService.write(jvm.getHostName(), jvm.getGroups(), historyMessage, eventType, user.getId()); } return jvm; } JvmServiceImpl(final JvmPersistenceService jvmPersistenceService,
                          final GroupPersistenceService groupPersistenceService,
                          final ApplicationService applicationService,
                          final SimpMessagingTemplate messagingTemplate,
                          final GroupStateNotificationService groupStateNotificationService,
                          final ResourceService resourceService,
                          final ClientFactoryHelper clientFactoryHelper,
                          final String topicServerStates,
                          final JvmControlService jvmControlService,
                          final BinaryDistributionService binaryDistributionService,
                          final BinaryDistributionLockManager binaryDistributionLockManager,
                          final HistoryFacadeService historyFacadeService,
                          final FileUtility fileUtility); @Override @Transactional Jvm createJvm(CreateJvmAndAddToGroupsRequest createJvmAndAddToGroupsRequest, User user); @Override @Transactional void createDefaultTemplates(final String jvmName, Group parentGroup); @Override @Transactional(readOnly = true) Jvm getJvm(final Identifier<Jvm> aJvmId); @Override @Transactional(readOnly = true) Jvm getJvm(final String jvmName); @Override @Transactional(readOnly = true) List<Jvm> getJvms(); @Override List<Jvm> getJvmsByGroupName(String name); @Override @Transactional Jvm updateJvm(final UpdateJvmRequest updateJvmRequest, final boolean updateJvmPassword); @Override @Transactional void deleteJvm(final Identifier<Jvm> id, boolean hardDelete, final User user); @Override Jvm generateAndDeployJvm(String jvmName, User user); Jvm upgradeJDK(String jvmName, User user); @Override Jvm generateAndDeployFile(String jvmName, String fileName, User user); @Override @Transactional void performDiagnosis(Identifier<Jvm> aJvmId, final User user); @Override @Transactional String previewResourceTemplate(String fileName, String jvmName, String groupName, String template); @Override @Transactional(readOnly = true) List<String> getResourceTemplateNames(final String jvmName); @Override @Transactional String getResourceTemplate(final String jvmName,
                                      final String resourceTemplateName,
                                      final boolean tokensReplaced); @Override @Transactional String updateResourceTemplate(final String jvmName, final String resourceTemplateName, final String template); @Override @Transactional(propagation = Propagation.REQUIRES_NEW) void updateState(final Identifier<Jvm> id, final JvmState state); @Override @Transactional JvmHttpRequestResult pingAndUpdateJvmState(final Jvm jvm); @Override @Transactional void deployApplicationContextXMLs(Jvm jvm, User user); @Override @Transactional(readOnly = true) Long getJvmStartedCount(final String groupName); @Override @Transactional(readOnly = true) Long getJvmCount(final String groupName); @Override Long getJvmStoppedCount(final String groupName); @Override Long getJvmForciblyStoppedCount(final String groupName); @Override @Transactional void deleteJvm(final String name, final String userName); }
JvmServiceImpl implements JvmService { @Override public Jvm generateAndDeployJvm(String jvmName, User user) { boolean didSucceed = false; Jvm jvm = getJvm(jvmName); LOGGER.debug("Start generateAndDeployJvm for {} by user {}", jvmName, user.getId()); historyFacadeService.write(jvm.getHostName(), jvm.getGroups(), "Starting to generate remote JVM " + jvm.getJvmName() + " on host " + jvm.getHostName(), EventType.USER_ACTION_INFO, user.getId()); binaryDistributionLockManager.writeLock(jvmName + "-" + jvm.getId().toString()); try { if (jvm.getState().isStartedState()) { final String errorMessage = "The remote JVM " + jvm.getJvmName() + " must be stopped before attempting to generate the JVM"; LOGGER.info(errorMessage); throw new InternalErrorException(FaultType.REMOTE_COMMAND_FAILURE, errorMessage); } validateJvmAndAppResources(jvm); checkForJvmBinaries(jvm); distributeBinaries(jvm); createScriptsDirectory(jvm); deployScriptsToUserJwalaScriptsDir(jvm, user); deleteJvmService(jvm, user); final String jvmConfigJar = generateJvmConfigJar(jvm); secureCopyJvmConfigJar(jvm, jvmConfigJar, user); deployJvmConfigJar(jvm, user, jvmConfigJar); deployJvmResourceFiles(jvm, user); deployApplicationContextXMLs(jvm, user); installJvmWindowsService(jvm, user); updateState(jvm.getId(), JvmState.JVM_STOPPED); didSucceed = true; } catch (CommandFailureException | IOException e) { LOGGER.error("Failed to generate the JVM config for {}", jvm.getJvmName(), e); throw new InternalErrorException(FaultType.REMOTE_COMMAND_FAILURE, "Failed to generate the JVM config: " + jvm.getJvmName(), e); } finally { binaryDistributionLockManager.writeUnlock(jvmName + "-" + jvm.getId().toString()); LOGGER.debug("End generateAndDeployJvm for {} by user {}", jvmName, user.getId()); final EventType eventType = didSucceed ? EventType.SYSTEM_INFO : EventType.SYSTEM_ERROR; String historyMessage = didSucceed ? "Remote generation of jvm " + jvm.getJvmName() + " to host " + jvm.getHostName() + " succeeded" : "Remote generation of jvm " + jvm.getJvmName() + " to host " + jvm.getHostName() + " failed"; historyFacadeService.write(jvm.getHostName(), jvm.getGroups(), historyMessage, eventType, user.getId()); } return jvm; } JvmServiceImpl(final JvmPersistenceService jvmPersistenceService,
                          final GroupPersistenceService groupPersistenceService,
                          final ApplicationService applicationService,
                          final SimpMessagingTemplate messagingTemplate,
                          final GroupStateNotificationService groupStateNotificationService,
                          final ResourceService resourceService,
                          final ClientFactoryHelper clientFactoryHelper,
                          final String topicServerStates,
                          final JvmControlService jvmControlService,
                          final BinaryDistributionService binaryDistributionService,
                          final BinaryDistributionLockManager binaryDistributionLockManager,
                          final HistoryFacadeService historyFacadeService,
                          final FileUtility fileUtility); @Override @Transactional Jvm createJvm(CreateJvmAndAddToGroupsRequest createJvmAndAddToGroupsRequest, User user); @Override @Transactional void createDefaultTemplates(final String jvmName, Group parentGroup); @Override @Transactional(readOnly = true) Jvm getJvm(final Identifier<Jvm> aJvmId); @Override @Transactional(readOnly = true) Jvm getJvm(final String jvmName); @Override @Transactional(readOnly = true) List<Jvm> getJvms(); @Override List<Jvm> getJvmsByGroupName(String name); @Override @Transactional Jvm updateJvm(final UpdateJvmRequest updateJvmRequest, final boolean updateJvmPassword); @Override @Transactional void deleteJvm(final Identifier<Jvm> id, boolean hardDelete, final User user); @Override Jvm generateAndDeployJvm(String jvmName, User user); Jvm upgradeJDK(String jvmName, User user); @Override Jvm generateAndDeployFile(String jvmName, String fileName, User user); @Override @Transactional void performDiagnosis(Identifier<Jvm> aJvmId, final User user); @Override @Transactional String previewResourceTemplate(String fileName, String jvmName, String groupName, String template); @Override @Transactional(readOnly = true) List<String> getResourceTemplateNames(final String jvmName); @Override @Transactional String getResourceTemplate(final String jvmName,
                                      final String resourceTemplateName,
                                      final boolean tokensReplaced); @Override @Transactional String updateResourceTemplate(final String jvmName, final String resourceTemplateName, final String template); @Override @Transactional(propagation = Propagation.REQUIRES_NEW) void updateState(final Identifier<Jvm> id, final JvmState state); @Override @Transactional JvmHttpRequestResult pingAndUpdateJvmState(final Jvm jvm); @Override @Transactional void deployApplicationContextXMLs(Jvm jvm, User user); @Override @Transactional(readOnly = true) Long getJvmStartedCount(final String groupName); @Override @Transactional(readOnly = true) Long getJvmCount(final String groupName); @Override Long getJvmStoppedCount(final String groupName); @Override Long getJvmForciblyStoppedCount(final String groupName); @Override @Transactional void deleteJvm(final String name, final String userName);  }