From 243f41910669a5ba870ab3704093516d66cc8419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Clermont=C3=A9?= Date: Tue, 31 May 2016 16:25:35 +0200 Subject: [PATCH] updated SibaManager --- src/org/dclermonte/siba/Siba.java | 2 +- src/org/dclermonte/siba/cli/SibaCLI.java | 15 +- src/org/dclermonte/siba/gui/SibaGUI.java | 5 +- .../dclermonte/siba/model/SibaManager.java | 325 ++++++++++-------- 4 files changed, 193 insertions(+), 154 deletions(-) diff --git a/src/org/dclermonte/siba/Siba.java b/src/org/dclermonte/siba/Siba.java index 4e4608e..7367c12 100644 --- a/src/org/dclermonte/siba/Siba.java +++ b/src/org/dclermonte/siba/Siba.java @@ -37,7 +37,7 @@ import org.slf4j.LoggerFactory; public class Siba { - private static final Logger logger = LoggerFactory.getLogger(Siba.class); + public static final Logger logger = LoggerFactory.getLogger(Siba.class); public static void main(final String[] args) { diff --git a/src/org/dclermonte/siba/cli/SibaCLI.java b/src/org/dclermonte/siba/cli/SibaCLI.java index edc1197..49e30be 100644 --- a/src/org/dclermonte/siba/cli/SibaCLI.java +++ b/src/org/dclermonte/siba/cli/SibaCLI.java @@ -23,6 +23,7 @@ import java.io.File; import java.util.ResourceBundle; import org.apache.commons.lang3.StringUtils; +import org.dclermonte.siba.Siba; import org.dclermonte.siba.SibaException; import org.dclermonte.siba.model.SibaManager; @@ -36,6 +37,14 @@ public class SibaCLI { private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("org.dclermonte.siba.cli.messages"); //$NON-NLS-1$ + public static void backup(final File directoryToSave) throws SibaException + { + File result; + result = SibaManager.backup(directoryToSave); + System.out.println(BUNDLE.getString("confirmDialog.text") + result.getName()); //$NON-NLS-1$ + return; + } + /** * * This method is called for Backup from CLI. @@ -123,7 +132,7 @@ public class SibaCLI } else if (parameterCount == 2) { - backup(new File(args[1]), null); + backup(new File(args[1])); } else if (parameterCount == 3) { @@ -173,10 +182,10 @@ public class SibaCLI catch (SibaException sibaException) { System.err.println("SibaException = " + sibaException.getMessage()); - + sibaException.printStackTrace(); if (!(sibaException.getCause() == null)) { - System.err.println("SibaException chainée = " + sibaException.getCause().getMessage()); + Siba.logger.info("SibaException chainée = " + sibaException.getCause().getMessage()); } help(); } diff --git a/src/org/dclermonte/siba/gui/SibaGUI.java b/src/org/dclermonte/siba/gui/SibaGUI.java index 043cb36..5b40847 100644 --- a/src/org/dclermonte/siba/gui/SibaGUI.java +++ b/src/org/dclermonte/siba/gui/SibaGUI.java @@ -45,6 +45,7 @@ import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import org.apache.commons.lang3.StringUtils; +import org.dclermonte.siba.Siba; import org.dclermonte.siba.SibaException; import org.dclermonte.siba.gui.utils.GUIToolBox; @@ -256,10 +257,12 @@ public class SibaGUI } catch (SibaException sibaException) { + sibaException.printStackTrace(); String dataMissing = sibaException.getMessage(); if (!(sibaException.getCause() == null)) { - dataMissing.concat(sibaException.getCause().getMessage()); + Siba.logger.info("SibaException chainée = " + sibaException.getCause().getMessage()); + // dataMissing.concat(sibaException.getCause().getMessage()); } String titleWarning = BUNDLE.getString("BackupPanel.TitleWarning.text"); diff --git a/src/org/dclermonte/siba/model/SibaManager.java b/src/org/dclermonte/siba/model/SibaManager.java index 64c5948..cb8a9d0 100644 --- a/src/org/dclermonte/siba/model/SibaManager.java +++ b/src/org/dclermonte/siba/model/SibaManager.java @@ -20,7 +20,6 @@ package org.dclermonte.siba.model; import java.io.BufferedOutputStream; -import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -43,7 +42,7 @@ import org.dclermonte.siba.SibaException; /** * - * @author dclermonte + * @author Didier Clermonté (dclermonte@april.org) * */ @@ -51,6 +50,22 @@ public class SibaManager { private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("org.dclermonte.siba.model.messages"); //$NON-NLS-1$ + /** + * This method perform the backup. + * + * @param fileToSave + * @return + * @throws SibaException + */ + public static File backup(final File fileToSave) throws SibaException + { + File result; + File target = new File(System.getProperty("user.dir")); + result = backup(fileToSave, target); + // + return result; + } + /** * This method perform the backup. * @@ -59,58 +74,52 @@ public class SibaManager * @return * @throws SibaException */ - public static File backup(final File fileToSave, File target) throws SibaException + public static File backup(final File fileToSave, final File target) throws SibaException { File result; - + TarArchiveOutputStream out = null; try { + if ((fileToSave == null) || !(fileToSave.exists())) { throw new SibaException(BUNDLE.getString("sourceNotExist.text")); } + else if (!(fileToSave.isDirectory())) + { + throw new SibaException(BUNDLE.getString("NotDirectorySource.text")); + } + else if (!(target.isDirectory())) + { + throw new SibaException(BUNDLE.getString("NotDirectoryTarget.text")); + } else { - if (!(fileToSave.isDirectory())) - { - throw new SibaException(BUNDLE.getString("NotDirectorySource.text")); - } - else - { - if ((target == null)) - { - target = new File(System.getProperty("user.dir")); - } - if (!(target.isDirectory())) - { - throw new SibaException(BUNDLE.getString("NotDirectoryTarget.text")); - } - else - { - int pathLength = fileToSave.getParentFile().getAbsolutePath().length(); - LocalDateTime date = LocalDateTime.now(); - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH'h'mm'mn'ss's'"); - String textDate = date.format(formatter); - String outputFileNameWithoutExtension = fileToSave.getName() + "-" + textDate; - - result = new File(target + "/" + outputFileNameWithoutExtension + ".tgz"); - OutputStream gzipOutputStream = new GzipCompressorOutputStream( - new BufferedOutputStream(new FileOutputStream(result))); - TarArchiveOutputStream out = new TarArchiveOutputStream(gzipOutputStream); - directoryToSave(fileToSave, out, pathLength); - out.close(); - fileCheckMD5(result, target + "/" + outputFileNameWithoutExtension + ".tgz.md5"); - } - } + int pathLength = fileToSave.getParentFile().getAbsolutePath().length(); + LocalDateTime date = LocalDateTime.now(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH'h'mm'mn'ss's'"); + String textDate = date.format(formatter); + String outputFileNameWithoutExtension = fileToSave.getName() + "-" + textDate; + result = new File(target + "/" + outputFileNameWithoutExtension + ".tgz"); + OutputStream gzipOutputStream = new GzipCompressorOutputStream( + new BufferedOutputStream(new FileOutputStream(result))); + out = new TarArchiveOutputStream(gzipOutputStream); + directoryToSave(fileToSave, out, pathLength); + out.close(); + fileWithMD5(result, target + "/" + outputFileNameWithoutExtension + ".tgz.md5"); } + } catch (IOException ioExceptionBackup) { throw new SibaException(BUNDLE.getString("ioExceptionBackup.text"), ioExceptionBackup); //$NON-NLS-1$ } + catch (NoSuchAlgorithmException noSuchAlgorithmException) + { + throw new SibaException(BUNDLE.getString("noSuchAlgorithm.text"), noSuchAlgorithmException); //$NON-NLS-1$ + } finally { - Closeable out = null; IOUtils.closeQuietly(out); } @@ -128,59 +137,28 @@ public class SibaManager public static boolean check(final File choosenFile) throws SibaException { boolean result; + try { - if (choosenFile.exists() && (choosenFile.length() > 32)) + String md5String = loadMD5Sum(choosenFile); + File fileToCheck1 = loadFileToCheck(choosenFile); + if (StringUtils.equals(md5String, md5(fileToCheck1))) { - FileReader fileReader = new FileReader(choosenFile); - char[] md5 = new char[32]; - byte[] md5byte = new byte[32]; - char[] fileToCheck = new char[(int) (choosenFile.length()) - 32]; - String md5String = new String(); - for (int index = 0; index < 32; index++) - { - md5[index] = (char) fileReader.read(); - md5byte[index] = (byte) md5[index]; - md5String = md5String + md5[index]; - } - fileReader.read(); - String fileNameToString = new String(); - for (int index = 36; index < (choosenFile.length() + 2); index++) - { - fileToCheck[index - 36] = (char) fileReader.read(); - fileNameToString = fileNameToString + fileToCheck[index - 36]; - } - - fileReader.close(); - File fileToCheck1; - if (choosenFile.isAbsolute()) - { - fileToCheck1 = new File(choosenFile.getParent() + "/" + fileNameToString); - } - else - { - String path = System.getProperty("user.dir"); - fileToCheck1 = new File(path + "/" + fileNameToString); - } - - if (StringUtils.equals(md5String, md5(fileToCheck1))) - { - result = true; - } - else - { - result = false; - } + result = true; } else { - throw new SibaException(BUNDLE.getString("NotExistFileToCheck.text")); + result = false; } } catch (IOException ioExceptionCheck) { throw new SibaException(BUNDLE.getString("ioExceptionCheck.text"), ioExceptionCheck); //$NON-NLS-1$ } + catch (NoSuchAlgorithmException noSuchAlgorithmException) + { + throw new SibaException(BUNDLE.getString("noSuchAlgorithm.text"), noSuchAlgorithmException); //$NON-NLS-1$ + } // return result; } @@ -192,47 +170,40 @@ public class SibaManager * @param directory * @param outputStream * @param pathLength - * @throws SibaException + * @throws IOException */ public static void directoryToSave(final File directory, final TarArchiveOutputStream outputStream, - final int pathLength) throws SibaException + final int pathLength) throws IOException { - try + for (File file : directory.listFiles()) { - for (File file : directory.listFiles()) + + if (file.isDirectory()) { - - if (file.isDirectory()) - { - if (file.listFiles().length == 0) - { - TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(file); - String pathPartiel = file.getPath().substring(pathLength); - tarArchiveEntry.setName(pathPartiel); - outputStream.putArchiveEntry(tarArchiveEntry); - outputStream.closeArchiveEntry(); - } - else - { - directoryToSave(file, outputStream, pathLength); - } - - } - else + if (file.listFiles().length == 0) { TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(file); String pathPartiel = file.getPath().substring(pathLength); tarArchiveEntry.setName(pathPartiel); outputStream.putArchiveEntry(tarArchiveEntry); - IOUtils.copy(new FileInputStream(file), outputStream); outputStream.closeArchiveEntry(); - } + else + { + directoryToSave(file, outputStream, pathLength); + } + + } + else + { + TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(file); + String pathPartiel = file.getPath().substring(pathLength); + tarArchiveEntry.setName(pathPartiel); + outputStream.putArchiveEntry(tarArchiveEntry); + IOUtils.copy(new FileInputStream(file), outputStream); + outputStream.closeArchiveEntry(); + } - } - catch (IOException ioExceptionTarEntry) - { - throw new SibaException(BUNDLE.getString("ioExceptionTarEntry.text"), ioExceptionTarEntry); //$NON-NLS-1$ } } @@ -243,24 +214,88 @@ public class SibaManager * @param directoryToSave * @param destination * @return - * @throws SibaException + * @throws IOException + * @throws NoSuchAlgorithmException */ - public static File fileCheckMD5(final File directoryToSave, final String destination) throws SibaException + public static File fileWithMD5(final File directoryToSave, final String destination) + throws IOException, NoSuchAlgorithmException { File result; - try + + result = new File(destination); + FileWriter fileWriter = new FileWriter(result); + fileWriter.write(md5(directoryToSave)); + String newLine = System.getProperty("line.separator"); + fileWriter.append(" " + directoryToSave.getName() + newLine); + fileWriter.close(); + + // + return result; + } + + /** + * + * @param choosenFile + * @return + * @throws IOException + */ + public static File loadFileToCheck(final File choosenFile) throws IOException + { + File result = null; + if (choosenFile.exists() && (choosenFile.length() > 32)) { - result = new File(destination); - FileWriter fileWriter = new FileWriter(result); - fileWriter.write(md5(directoryToSave)); - String newLine = System.getProperty("line.separator"); - fileWriter.append(" " + directoryToSave.getName() + newLine); - fileWriter.close(); + FileReader fileReader = new FileReader(choosenFile); + + char[] fileToCheck = new char[(int) (choosenFile.length()) - 32]; + fileReader.read(); + fileReader.skip(32); + String fileNameToString = new String(); + for (int index = 36; index < (choosenFile.length() + 2); index++) + { + fileToCheck[index - 36] = (char) fileReader.read(); + fileNameToString = fileNameToString + fileToCheck[index - 36]; + } + + fileReader.close(); + if (choosenFile.isAbsolute()) + { + result = new File(choosenFile.getParent() + "/" + fileNameToString); + } + else + { + String path = System.getProperty("user.dir"); + result = new File(path + "/" + fileNameToString); + } } - catch (IOException ioExceptionFileMD5) + // + return result; + } + + /** + * + * @param choosenFile + * @return + * @throws IOException + */ + public static String loadMD5Sum(final File choosenFile) throws IOException + { + String result = ""; + + if (choosenFile.exists() && (choosenFile.length() > 32)) { - throw new SibaException(BUNDLE.getString("ioExceptionFileMD5.text"), ioExceptionFileMD5); //$NON-NLS-1$ + FileReader fileReader = new FileReader(choosenFile); + char[] md5 = new char[32]; + byte[] md5byte = new byte[32]; + for (int index = 0; index < 32; index++) + { + md5[index] = (char) fileReader.read(); + md5byte[index] = (byte) md5[index]; + result = result + md5[index]; + } + fileReader.close(); } + + // // return result; } @@ -270,47 +305,39 @@ public class SibaManager * * @param input * @return - * @throws SibaException + * @throws IOException + * @throws NoSuchAlgorithmException */ - public static String md5(final File input) throws SibaException + public static String md5(final File input) throws IOException, NoSuchAlgorithmException { String result; StringBuilder hashString = new StringBuilder(); - try - { - byte[] byteInput = new byte[(int) input.length()]; - FileInputStream fileInputStream = new FileInputStream(input); - for (int index1 = 0; index1 < input.length(); index1++) - { - byteInput[index1] = (byte) fileInputStream.read(); - } - byte[] hash = null; - MessageDigest messageDigest = MessageDigest.getInstance("MD5"); - hash = messageDigest.digest(byteInput); - for (int index = 0; index < hash.length; index++) + byte[] byteInput = new byte[(int) input.length()]; + FileInputStream fileInputStream = new FileInputStream(input); + for (int index1 = 0; index1 < input.length(); index1++) + { + byteInput[index1] = (byte) fileInputStream.read(); + } + byte[] hash = null; + MessageDigest messageDigest = MessageDigest.getInstance("MD5"); + hash = messageDigest.digest(byteInput); + + for (int index = 0; index < hash.length; index++) + { + String hex = Integer.toHexString(hash[index]); + if (hex.length() == 1) { - String hex = Integer.toHexString(hash[index]); - if (hex.length() == 1) - { - hashString.append('0'); - hashString.append(hex.charAt(hex.length() - 1)); - } - else - { - hashString.append(hex.substring(hex.length() - 2)); - } + hashString.append('0'); + hashString.append(hex.charAt(hex.length() - 1)); + } + else + { + hashString.append(hex.substring(hex.length() - 2)); } - fileInputStream.close(); - } - catch (NoSuchAlgorithmException noSuchAlgorithmException) - { - throw new SibaException(BUNDLE.getString("noSuchAlgorithm.text"), noSuchAlgorithmException); //$NON-NLS-1$ - } - catch (IOException ioException) - { - throw new SibaException(BUNDLE.getString("ioExceptionMD5.text"), ioException); //$NON-NLS-1$ } + fileInputStream.close(); + result = hashString.toString(); // return result;