This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository tutti. See http://git.codelutin.com/tutti.git commit c6126b531306711070d0f5812fe4b7a9b5bb008e Author: Tony CHEMIT <chemit@codelutin.com> Date: Mon Jan 26 11:35:43 2015 +0100 amelioration du code de mise à jour --- .../fr/ifremer/tutti/ui/swing/updater/Module.java | 34 ++++ .../swing/updater/RecursiveDeleteFileVisitor.java | 61 ++++++ .../fr/ifremer/tutti/ui/swing/updater/Updater.java | 225 +++++++++++++-------- 3 files changed, 234 insertions(+), 86 deletions(-) diff --git a/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/Module.java b/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/Module.java new file mode 100644 index 0000000..5ecd331 --- /dev/null +++ b/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/Module.java @@ -0,0 +1,34 @@ +package fr.ifremer.tutti.ui.swing.updater; + +/** +* Created on 1/26/15. +* +* @author Tony Chemit - chemit@codelutin.com +* @since 3.12 +*/ +public enum Module { + + launcher(true), + jre(true), + + application(false), + i18n(false), + help(false), + report(false), + ichtyometer(false); + + private final boolean runtimeUpdate; + + Module(boolean runtimeUpdate) { + this.runtimeUpdate = runtimeUpdate; + } + + public boolean isRuntimeUpdate() { + return runtimeUpdate; + } + + public String getModuleLoggerName() { + String moduleNameStr = String.format("[%s$10s]", name()); + return moduleNameStr; + } +} diff --git a/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/RecursiveDeleteFileVisitor.java b/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/RecursiveDeleteFileVisitor.java new file mode 100644 index 0000000..ccc6f7a --- /dev/null +++ b/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/RecursiveDeleteFileVisitor.java @@ -0,0 +1,61 @@ +package fr.ifremer.tutti.ui.swing.updater; + +import java.io.IOException; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.PathMatcher; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; + +/** + * Created on 1/26/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.12 + */ +public class RecursiveDeleteFileVisitor extends SimpleFileVisitor<Path> { + + public static RecursiveDeleteFileVisitor newVisitor() { + return new RecursiveDeleteFileVisitor(null); + } + + public static RecursiveDeleteFileVisitor newVisitor(Path baseDir, String glob) { + + PathMatcher matcher; + if (glob != null) { + matcher = baseDir.getFileSystem().getPathMatcher("glob:" + glob); + } else { + matcher = null; + } + return new RecursiveDeleteFileVisitor(matcher); + } + + private final PathMatcher matcher; + + protected RecursiveDeleteFileVisitor(PathMatcher matcher) { + this.matcher = matcher; + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + + // If the file name matches the glob or if no matcher, delete the file + if (matcher == null || matcher.matches(file.getFileName())) { + Files.deleteIfExists(file); + } + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + + // if no matcher, delete the directory + if (matcher == null) { + Files.deleteIfExists(dir); + } + + return FileVisitResult.CONTINUE; + } + +} diff --git a/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/Updater.java b/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/Updater.java index 54b05b4..a8904fd 100644 --- a/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/Updater.java +++ b/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/Updater.java @@ -5,44 +5,33 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.nio.charset.StandardCharsets; -import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.PathMatcher; import java.nio.file.Paths; -import java.nio.file.SimpleFileVisitor; import java.nio.file.StandardCopyOption; -import java.nio.file.attribute.BasicFileAttributes; +import java.nio.file.attribute.PosixFilePermission; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.HashSet; import java.util.List; +import java.util.Set; /** - * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro> + * @author Ludovic Pecquot (ludovic.pecquot@e-is.pro) + * @author tony Chemit (chemit@codelutin.com) + * @since 3.12 */ public class Updater { - public static final String APPLICATION_UPDATER_TITLE = "Allegro Campagne UI Updater"; + public static final String APPLICATION_UPDATER_TITLE = "Allegro Campaign UI Updater"; public static final String NEW_DIR = "NEW"; public static final String OLD_DIR = "OLD"; - public static final String JRE_DIR = "jre"; - public static final String LAUNCHER_DIR = "launcher"; - public static final String APPLICATION_DIR = "tutti"; - - public static final String I18N_DIR = "i18n"; - - public static final String HELP_DIR = "help"; - - public static final String REPORT_DIR = "report"; - - public static final String ICHTYOMETER_DIR = "report"; - public static final String UPDATE_RUNTIME_CMD = "update_runtime"; public static final String BATCH_WINDOWS_EXT = ".bat"; @@ -88,8 +77,22 @@ public class Updater { } + private Path getBackupDirectory() throws IOException { + + Path backupDirectory = baseDir.resolve(OLD_DIR); + if (!Files.isDirectory(backupDirectory)) { + Files.createDirectory(backupDirectory); + } + + return backupDirectory; + + } + + public int execute() { + System.out.println("updater started at " + new Date().toString()); + int exitCode; try { @@ -117,6 +120,8 @@ public class Updater { } + System.out.println("updater ended at " + new Date().toString()); + return exitCode; } @@ -131,10 +136,23 @@ public class Updater { boolean mustUpdateRuntime = false; - if (Files.isDirectory(baseDir.resolve(NEW_DIR).resolve(JRE_DIR)) - || Files.isDirectory(baseDir.resolve(NEW_DIR).resolve(LAUNCHER_DIR))) { + for (Module module : Module.values()) { + + if (module.isRuntimeUpdate()) { + + boolean updateFound = updateRuntimeModule(module); + + if (updateFound) { + mustUpdateRuntime = true; + } + + } + + } - // A new jre or/and a new launcher is available, so generate the script + if (mustUpdateRuntime) { + + // A runtime update is available, so generate the script URL resource = getClass().getResource("/" + scriptFilename); try (InputStream stream = resource.openStream()) { @@ -143,10 +161,11 @@ public class Updater { } + makeExecutable(runtimeUpdater); + String message = String.format("Runtime updates available.\nYou must execute '%s' manually to apply new runtime.", runtimeUpdater.getFileName()); System.out.println(message); JOptionPane.showMessageDialog(null, message); - mustUpdateRuntime = true; } @@ -156,49 +175,96 @@ public class Updater { protected void launchUpdate() throws IOException { - // Compute the date to create backup directories - Date now = new Date(); + for (Module module : Module.values()) { - System.out.println("updater started at " + now.toString()); + if (!module.isRuntimeUpdate()) { - updateModule(baseDir, APPLICATION_DIR); - updateModule(baseDir, I18N_DIR); - updateModule(baseDir, HELP_DIR); - updateModule(baseDir, REPORT_DIR); - updateModule(baseDir, ICHTYOMETER_DIR); + updateModule(module); + + } + } // Cleaning process cleanObsoleteFiles(); cleanPath(baseDir.resolve(NEW_DIR)); - System.out.println("updater ended at " + new Date().toString()); + } + + protected boolean updateRuntimeModule(Module module) throws IOException { + + boolean updateFound; + String moduleName = module.name(); + + String moduleNameStr = module.getModuleLoggerName(); + + Path moduleNewPath = baseDir.resolve(NEW_DIR).resolve(moduleName); + + if (Files.isDirectory(moduleNewPath)) { + + String newVersion = getVersion(moduleNewPath); + + System.out.println(String.format("%s New version detected %s", moduleNameStr, newVersion)); + + Path backupDirectory = getBackupDirectory(); + + // Remove older backup + System.out.println(String.format("%s Clean backup directory %s", moduleNameStr, backupDirectory)); + cleanPath(backupDirectory, moduleName + "-*"); + + updateFound = true; + + } else { + + System.out.println(String.format("%s No update.", moduleNameStr)); + + updateFound = false; + } + + return updateFound; } - private void updateModule(Path basePath, String moduleName) throws IOException { + private void updateModule(Module module) throws IOException { + + String moduleName = module.name(); + + String moduleNameStr = module.getModuleLoggerName(); - // Update a single module. moduleName corresponds to the name of the directory inside the basePath - Path modulePath = basePath.resolve(moduleName); - Path moduleNewPath = basePath.resolve(NEW_DIR).resolve(moduleName); + // Update a single module. moduleName corresponds to the name of the directory inside the baseDir + Path moduleNewPath = baseDir.resolve(NEW_DIR).resolve(moduleName); if (Files.isDirectory(moduleNewPath)) { + String newVersion = getVersion(moduleNewPath); + System.out.println(String.format("%s New version detected %s", moduleNameStr, newVersion)); + + Path backupDirectory = getBackupDirectory(); + + // Remove older backup + System.out.println(String.format("%s Clean backup directory %s", moduleNameStr, backupDirectory)); + cleanPath(backupDirectory, moduleName + "-*"); + // Backup existing module + Path modulePath = baseDir.resolve(moduleName); if (Files.isDirectory(modulePath)) { String oldVersion = getVersion(modulePath); - Path moduleOldPath = basePath.resolve(OLD_DIR).resolve(String.format("%s-%s-%s", moduleName, oldVersion, backupDate)); - System.out.println(String.format("backup %s %s to %s", moduleName, oldVersion, moduleOldPath.toString())); - if (!Files.isDirectory(basePath.resolve(OLD_DIR))) { - Files.createDirectory(basePath.resolve(OLD_DIR)); - } + Path moduleOldPath = backupDirectory.resolve(String.format("%s-%s-%s", moduleNameStr, oldVersion, backupDate)); + System.out.println(String.format("%s Backup old version %s to %s", moduleNameStr, oldVersion, moduleOldPath.toString())); + Files.move(modulePath, moduleOldPath); } // Installing new module - System.out.println(String.format("install %s %s", moduleName, newVersion)); + System.out.println(String.format("%s Install new version %s", moduleNameStr, newVersion)); Files.move(moduleNewPath, modulePath, StandardCopyOption.REPLACE_EXISTING); + + } else { + + System.out.println(String.format("%s No update.", moduleNameStr)); + } + } private String getVersion(Path path) throws IOException { @@ -214,30 +280,55 @@ public class Updater { private void cleanPath(Path path) throws IOException { if (Files.isDirectory(path)) { - Files.walkFileTree(path, new RecursiveDeleteFileVisitor()); + Files.walkFileTree(path, RecursiveDeleteFileVisitor.newVisitor()); } } private void cleanPath(Path path, String glob) throws IOException { if (Files.isDirectory(path)) { - Files.walkFileTree(path, new RecursiveDeleteFileVisitor(glob)); + Files.walkFileTree(path, RecursiveDeleteFileVisitor.newVisitor(baseDir, glob)); + } + } + + protected void makeExecutable(Path path) throws IOException { + + if (!windowsOS) { + + Set<PosixFilePermission> perms = new HashSet<>(); + //add owners permission + perms.add(PosixFilePermission.OWNER_READ); + perms.add(PosixFilePermission.OWNER_WRITE); + perms.add(PosixFilePermission.OWNER_EXECUTE); + //add group permissions + perms.add(PosixFilePermission.GROUP_READ); + perms.add(PosixFilePermission.GROUP_WRITE); + perms.add(PosixFilePermission.GROUP_EXECUTE); + //add others permissions + perms.add(PosixFilePermission.OTHERS_READ); + perms.add(PosixFilePermission.OTHERS_EXECUTE); + + Files.setPosixFilePermissions(path, perms); + } + } private void cleanObsoleteFiles() throws IOException { + Path applicationDirectoryPath = baseDir.resolve(Module.application.name()); + if (windowsOS) { // Delete obsolete batch files Files.deleteIfExists(baseDir.resolve("tutti.bat")); - Files.deleteIfExists(baseDir.resolve(APPLICATION_DIR).resolve("launch.bat")); + Files.deleteIfExists(applicationDirectoryPath.resolve("launch.bat")); // Delete non Windows files cleanPath(baseDir, "*" + BATCH_UNIX_EXT); } else { // Delete obsolete script files - Files.deleteIfExists(baseDir.resolve(APPLICATION_DIR).resolve("launch.sh")); + Files.deleteIfExists(applicationDirectoryPath.resolve("launch.sh")); // Delete Windows files cleanPath(baseDir, "*" + BATCH_WINDOWS_EXT); @@ -245,49 +336,11 @@ public class Updater { } // Delete embedded files - Files.deleteIfExists(baseDir.resolve(APPLICATION_DIR).resolve("tutti.exe")); - Files.deleteIfExists(baseDir.resolve(APPLICATION_DIR).resolve("tutti.sh")); - cleanPath(baseDir.resolve(APPLICATION_DIR).resolve("launcher")); + Files.deleteIfExists(applicationDirectoryPath.resolve("tutti.exe")); + Files.deleteIfExists(applicationDirectoryPath.resolve("tutti.sh")); + cleanPath(applicationDirectoryPath.resolve("launcher")); } - private class RecursiveDeleteFileVisitor extends SimpleFileVisitor<Path> { - - PathMatcher matcher; - - public RecursiveDeleteFileVisitor() { - // no matcher == delete all - } - - public RecursiveDeleteFileVisitor(String glob) { - super(); - - // Create a matcher according the glob parameter - matcher = baseDir.getFileSystem().getPathMatcher("glob:" + glob); - } - - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - - // If the file name matches the glob or if no matcher, delete the file - if (matcher == null || matcher.matches(file.getFileName())) { - Files.deleteIfExists(file); - } - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { - - // if no matcher, delete the directory - if (matcher == null) { - Files.deleteIfExists(dir); - } - - return FileVisitResult.CONTINUE; - } - - } - } -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.