From 88879bd0b759ed0c961fb942c590e7ecf88d1215 Mon Sep 17 00:00:00 2001 From: Michael Stewart Date: Tue, 19 Mar 2024 10:22:04 +0000 Subject: [PATCH 1/2] IKASAN-2363 adding feature to persist the ikasan version to the file system along with a command to query the version. --- ikasaneip/cli/shell/jar/Readme.md | 25 +++--- .../cli/shell/command/ModuleCommand.java | 13 +++- .../cli/shell/command/VersionCommand.java | 76 +++++++++++++++++++ .../src/main/resources/application.properties | 2 + .../src/test/resources/application.properties | 2 + .../dao/MigrationPersistenceDaoImpl.java | 8 +- .../migration/service/MigrationService.java | 1 + .../shell/version/model/IkasanVersion.java | 13 ++++ .../version/service/IkasanVersionService.java | 36 +++++++++ .../service/IkasanVersionServiceImpl.java | 30 ++++++++ .../service/IkasanVersionServiceImplTest.java | 74 ++++++++++++++++++ 11 files changed, 261 insertions(+), 19 deletions(-) create mode 100644 ikasaneip/cli/shell/jar/src/main/java/org/ikasan/cli/shell/command/VersionCommand.java create mode 100644 ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/version/model/IkasanVersion.java create mode 100644 ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/version/service/IkasanVersionService.java create mode 100644 ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/version/service/IkasanVersionServiceImpl.java create mode 100644 ikasaneip/cli/shell/process-monitor/src/test/java/org/ikasan/cli/shell/version/service/IkasanVersionServiceImplTest.java diff --git a/ikasaneip/cli/shell/jar/Readme.md b/ikasaneip/cli/shell/jar/Readme.md index f3c3b95856..aaa79d60dd 100644 --- a/ikasaneip/cli/shell/jar/Readme.md +++ b/ikasaneip/cli/shell/jar/Readme.md @@ -129,18 +129,19 @@ Ikasan Shell:> #### Command Options -| Command | Description | Options | Examples | -|:-------------|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| env | Displays the runtime environment variables currently picked up by the CLI shell. Specifying a regexp of a variable name will only show those variables matching. | -names boolean to display variable names only.
-no-expand boolean to specify no wildcard expansion of variables.
-list return the results as a list. | Example 1.
```env h2.java.process``` - will only show the h2.java.process variable.

Example 2.
```env h2 -names``` - will only show the variable names matching h2.

Example 3.
```env h2 -list``` - will show the variables matching h2 as a list.

Example 4.
```env h2 -no-expand``` - will show the variable without expanding wildcards. | -| ps | Displays the status of the H2 JVM and Integration Module JVM as running, true or false. Uses default Integration Module name and username running the CLI. | -name | -| | | -user | -| start-h2 | Starts the H2 JVM process for this Integration Module. Uses default Integration Module name and user. | -name | -| | | -command which overrides h2.java.command | -| stop-h2 | Stops the H2 JVM process for this Integration Module. | -name | -| migrate-h2 | Migrate H2 persistence | --source-h2-version The version of the H2 database we are migrating from. [Optional, default = 1.4.200]
--target-h2-version The version of the H2 database we are migrating to. [Optional, default = 2.2.224]
--h2-user The username of the H2 database to use for the migration. [Optional, default = sa]
--h2-password The password of the H2 database to use for the migration. [Optional, default = sa]
--h2-database-location The path to the database. The general Ikasan convention [/-db/esb] will be used by default. [Optional] | -| start-module | Starts the Integration Module JVM process. Uses default Integration Module name and user. | -name | -| | | -command which overrides module.java.command | -| stop-module | Stops the Integration Module JVM process. | -name | +| Command | Description | Options | Examples | +|:-------------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| env | Displays the runtime environment variables currently picked up by the CLI shell. Specifying a regexp of a variable name will only show those variables matching. | -names boolean to display variable names only.
-no-expand boolean to specify no wildcard expansion of variables.
-list return the results as a list. | Example 1.
```env h2.java.process``` - will only show the h2.java.process variable.

Example 2.
```env h2 -names``` - will only show the variable names matching h2.

Example 3.
```env h2 -list``` - will show the variables matching h2 as a list.

Example 4.
```env h2 -no-expand``` - will show the variable without expanding wildcards. | +| ps | Displays the status of the H2 JVM and Integration Module JVM as running, true or false. Uses default Integration Module name and username running the CLI. | -name | +| | | -user | +| start-h2 | Starts the H2 JVM process for this Integration Module. Uses default Integration Module name and user. | -name | +| | | -command which overrides h2.java.command | +| stop-h2 | Stops the H2 JVM process for this Integration Module. | -name | +| migrate-h2 | Migrate H2 persistence | --source-h2-version The version of the H2 database we are migrating from. [Optional, default = 1.4.200]
--target-h2-version The version of the H2 database we are migrating to. [Optional, default = 2.2.224]
--h2-user The username of the H2 database to use for the migration. [Optional, default = sa]
--h2-password The password of the H2 database to use for the migration. [Optional, default = sa]
--h2-database-location The path to the database. The general Ikasan convention [/-db/esb] will be used by default. [Optional] | +| start-module | Starts the Integration Module JVM process. Uses default Integration Module name and user. | -name | +| | | -command which overrides module.java.command | +| stop-module | Stops the Integration Module JVM process. | -name | +| version | Get the Ikasan version of the module. | | #### Sample Usage Command diff --git a/ikasaneip/cli/shell/jar/src/main/java/org/ikasan/cli/shell/command/ModuleCommand.java b/ikasaneip/cli/shell/jar/src/main/java/org/ikasan/cli/shell/command/ModuleCommand.java index ec62bec394..2daba4b227 100644 --- a/ikasaneip/cli/shell/jar/src/main/java/org/ikasan/cli/shell/command/ModuleCommand.java +++ b/ikasaneip/cli/shell/jar/src/main/java/org/ikasan/cli/shell/command/ModuleCommand.java @@ -41,13 +41,11 @@ package org.ikasan.cli.shell.command; import org.ikasan.cli.shell.operation.model.ProcessType; +import org.ikasan.cli.shell.version.service.IkasanVersionService; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Value; import org.springframework.shell.command.annotation.Command; import org.springframework.shell.command.annotation.Option; -import org.springframework.shell.standard.ShellComponent; -import org.springframework.shell.standard.ShellMethod; -import org.springframework.shell.standard.ShellOption; /** * Commands to start and stop the integration module. @@ -63,6 +61,12 @@ public class ModuleCommand extends ActionCommand @Value("${module.java.command:null}") String moduleJavaCommand; + @Value("${persistence.dir}") + private String persistenceDir; + + @Value("${pom.version}") + private String pomVersion; + /** * Start Integration Module. * @param altModuleName @@ -89,6 +93,9 @@ JSONObject _startmodule(String altModuleName, String altCommand) command = altCommand; } + // Write the ikasan version to the non-transient persistent file system. + IkasanVersionService.instance(this.persistenceDir).writeVersion(this.pomVersion); + return this.start(this.processType, name, command); } diff --git a/ikasaneip/cli/shell/jar/src/main/java/org/ikasan/cli/shell/command/VersionCommand.java b/ikasaneip/cli/shell/jar/src/main/java/org/ikasan/cli/shell/command/VersionCommand.java new file mode 100644 index 0000000000..1278015392 --- /dev/null +++ b/ikasaneip/cli/shell/jar/src/main/java/org/ikasan/cli/shell/command/VersionCommand.java @@ -0,0 +1,76 @@ +/* + * $Id$ + * $URL$ + * + * ==================================================================== + * Ikasan Enterprise Integration Platform + * + * Distributed under the Modified BSD License. + * Copyright notice: The copyright for this software and a full listing + * of individual contributors are as shown in the packaged copyright.txt + * file. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of the ORGANIZATION nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + */ +package org.ikasan.cli.shell.command; + +import org.ikasan.cli.shell.version.model.IkasanVersion; +import org.ikasan.cli.shell.version.service.IkasanVersionService; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.shell.command.annotation.Command; + +/** + * Command to get the Ikasan version of the module. + * + * @author Ikasan Development Team + */ +@Command +public class VersionCommand +{ + @Value("${persistence.dir}") + private String persistenceDir; + + /** + * Get the Ikasan version of the module. + * + * @return the module version + */ + @Command(description = "Get the Ikasan version of the module", group = "Ikasan Commands", command = "version") + public String version() { + IkasanVersion ikasanVersion = IkasanVersionService.instance(this.persistenceDir).find(); + + if(ikasanVersion != null) { + return ikasanVersion.getVersion(); + } + else { + return "The module version is not available!"; + } + } + +} \ No newline at end of file diff --git a/ikasaneip/cli/shell/jar/src/main/resources/application.properties b/ikasaneip/cli/shell/jar/src/main/resources/application.properties index 75861fa852..9e1f8974a3 100644 --- a/ikasaneip/cli/shell/jar/src/main/resources/application.properties +++ b/ikasaneip/cli/shell/jar/src/main/resources/application.properties @@ -8,6 +8,8 @@ logging.level.org.jline=ERROR shell.history.file=log/ikasan-shell.log +pom.version=@project.version@ + # default some variables module.jar.name=${module.name} h2.jar.name=h2-*.jar diff --git a/ikasaneip/cli/shell/jar/src/test/resources/application.properties b/ikasaneip/cli/shell/jar/src/test/resources/application.properties index b0afcd0147..1430d9f2c8 100644 --- a/ikasaneip/cli/shell/jar/src/test/resources/application.properties +++ b/ikasaneip/cli/shell/jar/src/test/resources/application.properties @@ -9,6 +9,8 @@ logging.level.org.ikasan=INFO logging.file=logs/application.log logging.level.org.ikasan.cli.shell=ERROR +pom.version=@project.version@ + # Ikasan Shell h2.java.command=java -Dmodule.name=filesystem-im -classpath ./lib/h2-1.4.200.jar org.h2.tools.Server -ifNotExists -tcp -tcpAllowOthers -tcpPort 8888 module.java.command=java -server -Xms256m -Xmx256m -XX:MaxMetaspaceSize=128m -Dspring.jta.logDir=./persistence/filesystem-im-ObjectStore -Dorg.apache.activemq.SERIALIZABLE_PACKAGES=* -Dmodule.name=filesystem-im -jar ./lib/filesystem-im-1.0.0-SNAPSHOT.jar diff --git a/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/migration/dao/MigrationPersistenceDaoImpl.java b/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/migration/dao/MigrationPersistenceDaoImpl.java index 834a4fcdf3..0e8108bb64 100644 --- a/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/migration/dao/MigrationPersistenceDaoImpl.java +++ b/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/migration/dao/MigrationPersistenceDaoImpl.java @@ -101,7 +101,7 @@ public MigrationPersistenceDaoImpl(String persistenceDir) { @Override public void save(IkasanMigration ikasanMigration) { - String path = getPidFQN(ikasanMigration.getType() + String path = getMigrationManifestFilePath(ikasanMigration.getType() , ikasanMigration.getSourceVersion(), ikasanMigration.getTargetVersion()); try(Output output = new Output(new FileOutputStream(path))) { @@ -114,7 +114,7 @@ public void save(IkasanMigration ikasanMigration) @Override public IkasanMigration find(String type, String sourceVersion, String targetVersion) { - String path = getPidFQN(type, sourceVersion, targetVersion); + String path = getMigrationManifestFilePath(type, sourceVersion, targetVersion); try (Input input = new Input(new FileInputStream(path))) { return this.objectMapper.readValue(input, IkasanMigration.class); } @@ -126,7 +126,7 @@ public IkasanMigration find(String type, String sourceVersion, String targetVers @Override public void delete(String type, String sourceVersion, String targetVersion) { - String path = getPidFQN(type, sourceVersion, targetVersion); + String path = getMigrationManifestFilePath(type, sourceVersion, targetVersion); try { Files.delete(Path.of(path)); } @@ -135,7 +135,7 @@ public void delete(String type, String sourceVersion, String targetVersion) { } } - protected String getPidFQN(String type, String sourceVersion, String targetVersion) { + protected String getMigrationManifestFilePath(String type, String sourceVersion, String targetVersion) { return persistenceDir + FileSystems.getDefault().getSeparator() + type + "_" + sourceVersion + "_" + targetVersion; } diff --git a/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/migration/service/MigrationService.java b/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/migration/service/MigrationService.java index d6ed254e25..35f2e329aa 100644 --- a/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/migration/service/MigrationService.java +++ b/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/migration/service/MigrationService.java @@ -49,6 +49,7 @@ * @author Ikasan Development Team */ public interface MigrationService { + /** * Returns an instance of MigrationService. * diff --git a/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/version/model/IkasanVersion.java b/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/version/model/IkasanVersion.java new file mode 100644 index 0000000000..3b57a57b47 --- /dev/null +++ b/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/version/model/IkasanVersion.java @@ -0,0 +1,13 @@ +package org.ikasan.cli.shell.version.model; + +public class IkasanVersion { + private String version; + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } +} diff --git a/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/version/service/IkasanVersionService.java b/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/version/service/IkasanVersionService.java new file mode 100644 index 0000000000..d9bb8fcf4f --- /dev/null +++ b/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/version/service/IkasanVersionService.java @@ -0,0 +1,36 @@ +package org.ikasan.cli.shell.version.service; + +import org.ikasan.cli.shell.version.dao.IkasanVersionPersistenceDaoImpl; +import org.ikasan.cli.shell.version.model.IkasanVersion; + +public interface IkasanVersionService { + + /** + * Returns an instance of IkasanVersionService. + * + * @return an instance of IkasanVersionService + */ + static IkasanVersionService instance(String persistenceDir) { + return new IkasanVersionServiceImpl(new IkasanVersionPersistenceDaoImpl(persistenceDir+"/version_manifest")); + } + + /** + * Writes the Ikasan version to the persistent file system. + * + * @param version to be saved. + */ + void writeVersion(String version); + + /** + * Finds the IkasanVersion object. + * + * @return the found IkasanVersion object, or null if not found + */ + IkasanVersion find(); + + + /** + * Deletes IkasanVersion. + */ + void delete(); +} diff --git a/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/version/service/IkasanVersionServiceImpl.java b/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/version/service/IkasanVersionServiceImpl.java new file mode 100644 index 0000000000..4ed16a4346 --- /dev/null +++ b/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/version/service/IkasanVersionServiceImpl.java @@ -0,0 +1,30 @@ +package org.ikasan.cli.shell.version.service; + +import org.ikasan.cli.shell.version.dao.IkasanVersionPersistenceDao; +import org.ikasan.cli.shell.version.model.IkasanVersion; + +public class IkasanVersionServiceImpl implements IkasanVersionService { + + private IkasanVersionPersistenceDao ikasanVersionPersistenceDao; + + public IkasanVersionServiceImpl(IkasanVersionPersistenceDao ikasanVersionPersistenceDao) { + this.ikasanVersionPersistenceDao = ikasanVersionPersistenceDao; + } + + @Override + public void writeVersion(String version) { + IkasanVersion ikasanVersion = new IkasanVersion(); + ikasanVersion.setVersion(version); + this.ikasanVersionPersistenceDao.save(ikasanVersion); + } + + @Override + public IkasanVersion find() { + return this.ikasanVersionPersistenceDao.find(); + } + + @Override + public void delete() { + this.ikasanVersionPersistenceDao.delete(); + } +} diff --git a/ikasaneip/cli/shell/process-monitor/src/test/java/org/ikasan/cli/shell/version/service/IkasanVersionServiceImplTest.java b/ikasaneip/cli/shell/process-monitor/src/test/java/org/ikasan/cli/shell/version/service/IkasanVersionServiceImplTest.java new file mode 100644 index 0000000000..34cdb629f9 --- /dev/null +++ b/ikasaneip/cli/shell/process-monitor/src/test/java/org/ikasan/cli/shell/version/service/IkasanVersionServiceImplTest.java @@ -0,0 +1,74 @@ +package org.ikasan.cli.shell.version.service; + +import org.ikasan.cli.shell.migration.dao.MigrationPersistenceDaoImpl; +import org.ikasan.cli.shell.migration.model.IkasanMigration; +import org.ikasan.cli.shell.migration.model.MigrationType; +import org.ikasan.cli.shell.version.dao.IkasanVersionPersistenceDao; +import org.ikasan.cli.shell.version.dao.IkasanVersionPersistenceDaoImpl; +import org.ikasan.cli.shell.version.model.IkasanVersion; +import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Test methods for KryoMigrationPersistenceDaoImpl + */ +class IkasanVersionServiceImplTest { + + private static final String PERSISTENCE_DIR = "./persistence"; + + @BeforeEach + void setUp() { + deleteFilesInDirectory(); + } + + + /** + * Test the save method of KryoMigrationPersistenceDaoImpl + */ + @Test + void test_save_and_find_success() { + IkasanVersionPersistenceDao persistenceDao = new IkasanVersionPersistenceDaoImpl(PERSISTENCE_DIR); + IkasanVersionService ikasanVersionService = new IkasanVersionServiceImpl(persistenceDao); + + ikasanVersionService.writeVersion("1.0.0"); + + IkasanVersion ikasanVersion = ikasanVersionService.find(); + + Assert.assertEquals("1.0.0", ikasanVersion.getVersion()); + } + + @Test + void test_save_and_delete_success() { + IkasanVersionPersistenceDao persistenceDao = new IkasanVersionPersistenceDaoImpl(PERSISTENCE_DIR); + IkasanVersionService ikasanVersionService = new IkasanVersionServiceImpl(persistenceDao); + + ikasanVersionService.writeVersion("1.0.0"); + + ikasanVersionService.delete(); + + IkasanVersion ikasanVersion = ikasanVersionService.find(); + + Assert.assertNull(ikasanVersion); + } + + + // Helper method to delete all files between tests to provide isolation + private void deleteFilesInDirectory() { + Path directoryPath = Paths.get(PERSISTENCE_DIR); + File directory = directoryPath.toFile(); + if (directory.exists()) { + for (File file : directory.listFiles()) { + file.delete(); + } + } + } + + +} \ No newline at end of file From cdde42bf9c8038b48a2f47cb987cf445a39cd23b Mon Sep 17 00:00:00 2001 From: Michael Stewart Date: Tue, 19 Mar 2024 10:58:08 +0000 Subject: [PATCH 2/2] IKASAN-2363 adding missing files. --- .../dao/IkasanVersionPersistenceDao.java | 73 +++++++++ .../dao/IkasanVersionPersistenceDaoImpl.java | 142 ++++++++++++++++++ 2 files changed, 215 insertions(+) create mode 100644 ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/version/dao/IkasanVersionPersistenceDao.java create mode 100644 ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/version/dao/IkasanVersionPersistenceDaoImpl.java diff --git a/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/version/dao/IkasanVersionPersistenceDao.java b/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/version/dao/IkasanVersionPersistenceDao.java new file mode 100644 index 0000000000..a3cdd05678 --- /dev/null +++ b/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/version/dao/IkasanVersionPersistenceDao.java @@ -0,0 +1,73 @@ +/* + * $Id$ + * $URL$ + * + * ==================================================================== + * Ikasan Enterprise Integration Platform + * + * Distributed under the Modified BSD License. + * Copyright notice: The copyright for this software and a full listing + * of individual contributors are as shown in the packaged copyright.txt + * file. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of the ORGANIZATION nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + */ +package org.ikasan.cli.shell.version.dao; + +import org.ikasan.cli.shell.migration.model.IkasanMigration; +import org.ikasan.cli.shell.version.model.IkasanVersion; + +/** + * Process persistence contract. + * + * @author Ikasan Development Team + */ +public interface IkasanVersionPersistenceDao +{ + /** + * Saves the given IkasanVersion object. + * + * @param ikasanVersion the IkasanVersion object to be saved + */ + void save(IkasanVersion ikasanVersion); + + + /** + * Finds the IkasanVersion object. + * + * @return the found IkasanVersion object, or null if not found + */ + IkasanVersion find(); + + + /** + * Deletes IkasanVersion. + */ + void delete(); +} \ No newline at end of file diff --git a/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/version/dao/IkasanVersionPersistenceDaoImpl.java b/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/version/dao/IkasanVersionPersistenceDaoImpl.java new file mode 100644 index 0000000000..9c36bd7c8b --- /dev/null +++ b/ikasaneip/cli/shell/process-monitor/src/main/java/org/ikasan/cli/shell/version/dao/IkasanVersionPersistenceDaoImpl.java @@ -0,0 +1,142 @@ +/* + * $Id$ + * $URL$ + * + * ==================================================================== + * Ikasan Enterprise Integration Platform + * + * Distributed under the Modified BSD License. + * Copyright notice: The copyright for this software and a full listing + * of individual contributors are as shown in the packaged copyright.txt + * file. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - Neither the name of the ORGANIZATION nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + */ +package org.ikasan.cli.shell.version.dao; + +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.ikasan.cli.shell.migration.model.IkasanMigration; +import org.ikasan.cli.shell.version.model.IkasanVersion; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; + +/** + * Serialiser for IkasanProcess. + * + * @author Ikasan Development Team + */ +public class IkasanVersionPersistenceDaoImpl implements IkasanVersionPersistenceDao +{ + /** logger instance */ + private static Logger logger = LoggerFactory.getLogger(IkasanVersionPersistenceDaoImpl.class); + + private ObjectMapper objectMapper = new ObjectMapper(); + + /** persistence directory */ + private String persistenceDir; + + private File persistenceDirFile; + + + /** + * Constructor + * @param persistenceDir + */ + public IkasanVersionPersistenceDaoImpl(String persistenceDir) { + this.persistenceDir = persistenceDir; + if(persistenceDir == null) { + throw new IllegalArgumentException("persistence directory cannot be 'null"); + } + + this.persistenceDirFile = new File(persistenceDir); + if(!persistenceDirFile.exists()) { + persistenceDirFile.mkdirs(); + File directorySafeGuardFile = new File(this.persistenceDir + "/DO_NOT_DELETE_ANY_FILES_IN_THIS_DIRECTORY"); + if(!directorySafeGuardFile.exists()) { + try { + directorySafeGuardFile.createNewFile(); + } + catch (IOException e) { + logger.warn("An error has occurred creating the directory safe guard file!", e); + } + } + } + } + + @Override + public void save(IkasanVersion ikasanVersion) + { + String path = getVersionFilePath(); + + try(Output output = new Output(new FileOutputStream(path))) { + objectMapper.writeValue(output, ikasanVersion); + } + catch(IOException e) { + throw new RuntimeException("Failed to save the IkasanProcess", e); + } + } + + @Override + public IkasanVersion find() { + String path = getVersionFilePath(); + try (Input input = new Input(new FileInputStream(path))) { + return this.objectMapper.readValue(input, IkasanVersion.class); + } + catch(IOException e) { + logger.debug("File [" + path + "] not found", e); + return null; + } + } + + @Override + public void delete() { + String path = getVersionFilePath(); + try { + Files.delete(Path.of(path)); + } + catch(IOException e) { + logger.warn("Failed to delete [" + path + "] file may be missing or some other IO issue" + e.getMessage()); + } + } + + protected String getVersionFilePath() { + return persistenceDir + FileSystems.getDefault().getSeparator() + "ikasan-version.mf"; + } + +} \ No newline at end of file