updated all exceptions

This commit is contained in:
Didier Clermonté 2016-05-22 21:47:41 +02:00
parent 9d47e99781
commit 4fbf01f8a0
18 changed files with 252 additions and 311 deletions

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@ -19,10 +19,6 @@
*/
package org.dclermonte.siba;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.compress.archivers.ArchiveException;
import org.dclermonte.siba.cli.SibaCLI;
import org.dclermonte.siba.gui.SibaGUI;
@ -33,14 +29,9 @@ public class Siba
*
*
* @param args
* @throws ArchiveException
* @throws IOException
* @throws NoSuchAlgorithmException
* @throws SibaException
*/
public static void main(final String[] args)
throws ArchiveException, IOException, NoSuchAlgorithmException, SibaException
{
if (args.length == 0)

View File

@ -1,24 +0,0 @@
package org.dclermonte.siba;
public class SibaBackupSourceMissingException extends SibaException
{
/**
*
*/
private static final long serialVersionUID = 7222468364832008228L;
public SibaBackupSourceMissingException()
{
super();
}
public SibaBackupSourceMissingException(final String message)
{
super(message);
}
public SibaBackupSourceMissingException(final String message, final Throwable cause)
{
super(message, cause);
}
}

View File

@ -1,24 +0,0 @@
package org.dclermonte.siba;
public class SibaBackupSourceNotDirectoryException extends SibaException
{
/**
*
*/
private static final long serialVersionUID = -5089169155692368448L;
public SibaBackupSourceNotDirectoryException()
{
super();
}
public SibaBackupSourceNotDirectoryException(final String message)
{
super(message);
}
public SibaBackupSourceNotDirectoryException(final String message, final Throwable cause)
{
super(message, cause);
}
}

View File

@ -1,26 +0,0 @@
package org.dclermonte.siba;
public class SibaBackupTargetMissingException extends SibaException
{
/**
*
*/
private static final long serialVersionUID = 2249962275756375452L;
public SibaBackupTargetMissingException()
{
super();
}
public SibaBackupTargetMissingException(final String message)
{
super(message);
}
public SibaBackupTargetMissingException(final String message, final Throwable cause)
{
super(message, cause);
}
}

View File

@ -8,6 +8,11 @@
package org.dclermonte.siba;
/**
*
* @author dclermonte
*
*/
public class SibaException extends Exception
{

View File

@ -20,11 +20,8 @@
package org.dclermonte.siba.cli;
import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ResourceBundle;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.lang3.StringUtils;
import org.dclermonte.siba.SibaException;
import org.dclermonte.siba.model.SibaManager;
@ -45,12 +42,9 @@ public class SibaCLI
*
* @param directoryToSave
* @param targetDirectory
* @throws ArchiveException
* @throws IOException
* @throws SibaException
*/
public static void backup(final File directoryToSave, final File targetDirectory)
throws ArchiveException, IOException, SibaException
public static void backup(final File directoryToSave, final File targetDirectory) throws SibaException
{
File result;
result = SibaManager.backup(directoryToSave, targetDirectory);
@ -60,13 +54,12 @@ public class SibaCLI
/**
*
* This method is called for Check from CLI.
* This method is called for Check from CLI. anObject
*
* @param fileToCheck
* @throws NoSuchAlgorithmException
* @throws IOException
* @throws SibaException
*/
public static void check(final File fileToCheck) throws NoSuchAlgorithmException, IOException
public static void check(final File fileToCheck) throws SibaException
{
boolean result;
@ -106,12 +99,8 @@ public class SibaCLI
* This method launch CLI.
*
* @param args
* @throws ArchiveException
* @throws IOException
* @throws NoSuchAlgorithmException
*/
public static void run(final String[] args)
throws ArchiveException, IOException, NoSuchAlgorithmException, SibaException
{
try
{
@ -121,109 +110,76 @@ public class SibaCLI
{
help();
}
else
else if (StringUtils.equals(args[0], "-h") || StringUtils.equals(args[0], "-help")
|| StringUtils.equals(args[0], "--help"))
{
if (StringUtils.equals(args[0], "-h") || StringUtils.equals(args[0], "-help")
|| StringUtils.equals(args[0], "--help"))
help();
}
else if (StringUtils.equals(args[0], "backup"))
{
if (parameterCount == 1)
{
help();
backup(null, null);
}
else if (StringUtils.equals(args[0], "backup"))
else if (parameterCount == 2)
{
if (parameterCount == 1)
{
System.out.println(BUNDLE.getString("missingDirectoryToSave.text"));
help();
}
else if (parameterCount == 2)
{
File directoryToSave = new File(args[1]);
if (directoryToSave.exists())
{
backup(directoryToSave, new File(System.getProperty("user.dir")));
}
else
{
System.out.println(BUNDLE.getString("directoryToSaveNotExist.text"));
help();
}
}
else if (parameterCount == 3)
{
File directoryToSave = new File(args[1]);
File targetDirectory = new File(args[2]);
if (directoryToSave.exists() && targetDirectory.exists())
{
backup(directoryToSave, targetDirectory);
}
else
{
System.out.println(BUNDLE.getString("directoryNotExist.text"));
help();
}
}
else
{
System.out.println(BUNDLE.getString("badUsage.text"));
help();
}
backup(new File(args[1]), null);
}
else if (StringUtils.equals(args[0], "check"))
else if (parameterCount == 3)
{
if (parameterCount == 1)
{
System.out.println(BUNDLE.getString("missingFileToCheck.text"));
help();
}
else
{
String fileToCheckName;
if (!args[1].endsWith("tgz") && !args[1].endsWith("md5"))
{
fileToCheckName = args[1].concat(".tgz.md5");
}
else if (args[1].endsWith("tgz"))
{
fileToCheckName = args[1].concat(".md5");
}
else
{
fileToCheckName = args[1];
}
File fileToCheck = new File(fileToCheckName);
File fileToCheckFinal;
if (!fileToCheck.isAbsolute())
{
fileToCheckFinal = new File(System.getProperty("user.dir") + "/" + fileToCheckName);
}
else
{
fileToCheckFinal = new File(fileToCheckName);
}
if (fileToCheckFinal.exists())
{
check(fileToCheckFinal);
}
else
{
System.out.println(BUNDLE.getString("missingFileToCheck.text"));
help();
}
}
backup(new File(args[1]), new File(args[2]));
}
else
{
System.out.println(BUNDLE.getString("badUsage.text"));
help();
}
}
else if (StringUtils.equals(args[0], "check"))
{
if (parameterCount == 1)
{
check(null);
}
else if (parameterCount == 2)
{
String fileToCheckName;
if (!args[1].endsWith("tgz") && !args[1].endsWith("md5"))
{
fileToCheckName = args[1].concat(".tgz.md5");
}
else if (args[1].endsWith("tgz"))
{
fileToCheckName = args[1].concat(".md5");
}
else
{
fileToCheckName = args[1];
}
File fileToCheck = new File(fileToCheckName);
check(fileToCheck);
}
else
{
System.out.println(BUNDLE.getString("badUsage.text"));
help();
}
}
else
{
System.out.println(BUNDLE.getString("badUsage.text"));
help();
}
}
catch (SibaException nullException)
catch (SibaException sibaException)
{
System.out.println("SibaException = " + nullException.getMessage());
System.err.println("SibaException = " + sibaException.getMessage());
if (!(sibaException.getCause() == null))
{
System.err.println("SibaException chainée = " + sibaException.getCause().getMessage());
}
}
}

View File

@ -8,4 +8,5 @@ directoryToSaveNotExist.text = The directory to save do not exist
directoryNotExist.text = one of the directories does not exist
missingFileToCheck.text = Missing the file to check
missingTargetFile.text=missing target file
NotDirectorySource.text=Source is not a directory
NotDirectorySource.text=Source is not a directory
SibaManageralgorithm.text=Algorithm error in sibaManagerexception

View File

@ -8,4 +8,5 @@ directoryToSaveNotExist.text = Le r
directoryNotExist.text = L'un des répertoires n'existe pas
missingFileToCheck.text = Il manque le fichier à vérifier
missingTargetFile.text=Le répertoire destination n'existe pas
NotDirectorySource.text=La source n'est pas un répertoire
NotDirectorySource.text=La source n'est pas un répertoire
SibaManageralgorithm.text=Erreur d'algorithme dans sibaManager

View File

@ -33,7 +33,7 @@ import javax.swing.JTextPane;
/**
*
* @author papou
* @author dclermonte
*
*/
public class AboutDialog extends JDialog

View File

@ -25,7 +25,6 @@ import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ResourceBundle;
import javax.swing.Box;
@ -37,7 +36,6 @@ import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import org.apache.commons.compress.archivers.ArchiveException;
import org.dclermonte.siba.SibaException;
import org.dclermonte.siba.model.SibaManager;
@ -50,7 +48,7 @@ import com.jgoodies.forms.layout.RowSpec;
*
* This class is the panel for backup.
*
* @author papou
* @author dclermonte
*
*/
public class BackupPanel extends JPanel
@ -198,7 +196,7 @@ public class BackupPanel extends JPanel
}
}
catch (ArchiveException | IOException | SibaException e1)
catch (SibaException e1)
{
// TODO Auto-generated catch block
((Throwable) this.e1).printStackTrace();

View File

@ -33,6 +33,7 @@ import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.dclermonte.siba.SibaException;
import org.dclermonte.siba.model.SibaManager;
import com.jgoodies.forms.layout.ColumnSpec;
@ -44,7 +45,7 @@ import com.jgoodies.forms.layout.Sizes;
/**
* This class is the panel for Check.
*
* @author papa
* @author dclermonte
*/
public class CheckPanel extends JPanel
{
@ -144,15 +145,11 @@ public class CheckPanel extends JPanel
}
}
catch (NoSuchAlgorithmException e)
catch (SibaException exception)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
exception.printStackTrace();
}
}
});

View File

@ -50,7 +50,7 @@ import org.dclermonte.siba.gui.utils.GUIToolBox;
/**
* The main class for GUI (Graphical User Interface )
*
* @author papou
* @author dclermonte
*
*/
public class SibaGUI
@ -251,9 +251,14 @@ public class SibaGUI
SibaGUI window = new SibaGUI();
window.frmSimpleBackup.setVisible(true);
}
catch (SibaException nullException)
catch (SibaException sibaException)
{
String dataMissing = nullException.getMessage();
String dataMissing = sibaException.getMessage();
if (!(sibaException.getCause() == null))
{
dataMissing.concat(sibaException.getCause().getMessage());
}
String titleWarning = BUNDLE.getString("BackupPanel.TitleWarning.text");
JOptionPane.showMessageDialog(null, dataMissing, titleWarning, JOptionPane.INFORMATION_MESSAGE);
}

View File

@ -7,8 +7,8 @@ import javax.swing.UIManager;
/**
* Some utility
*
* @author papou
*
* @author dclermonte
*
*/
public class GUIToolBox

View File

@ -33,27 +33,22 @@ import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ResourceBundle;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.dclermonte.siba.SibaBackupSourceMissingException;
import org.dclermonte.siba.SibaBackupSourceNotDirectoryException;
import org.dclermonte.siba.SibaBackupTargetMissingException;
import org.dclermonte.siba.SibaException;
/**
* The model class
*
* @author papou
* @author dclermonte
*
*/
public class SibaManager
{
private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("org.dclermonte.siba.cli.messages"); //$NON-NLS-1$
private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("org.dclermonte.siba.model.messages"); //$NON-NLS-1$
/**
* This method perform the backup.
@ -61,47 +56,58 @@ public class SibaManager
* @param fileToSave
* @param target
* @return
* @throws ArchiveException
* @throws IOException
* @throws SibaBackupTargetMissingException
* @throws SibaException
*/
public static File backup(final File fileToSave, final File target)
throws ArchiveException, IOException, SibaException
public static File backup(final File fileToSave, File target) throws SibaException
{
File result = null;
try
{
if (!target.exists())
if (!(fileToSave == null) && fileToSave.exists())
{
throw new SibaBackupTargetMissingException(BUNDLE.getString("missingTargetFile.text"));
}
else if (!fileToSave.exists())
{
throw new SibaBackupSourceMissingException(BUNDLE.getString("missingDirectoryToSave.text"));
}
else if (!fileToSave.isDirectory())
{
throw new SibaBackupSourceNotDirectoryException(BUNDLE.getString("NotDirectorySource.text"));
if (fileToSave.isDirectory())
{
if ((target == null))
{
target = new File(System.getProperty("user.dir"));
}
if (target.isDirectory())
{
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");
}
else
{
throw new SibaException(BUNDLE.getString("NotDirectoryTarget.text"));
}
}
else
{
throw new SibaException(BUNDLE.getString("NotDirectorySource.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");
throw new SibaException(BUNDLE.getString("sourceNotExist.text"));
}
return result;
}
catch (IOException ioExceptionBackup)
{
throw new SibaException(BUNDLE.getString("ioExceptionBackup.text"), ioExceptionBackup); //$NON-NLS-1$
}
return result;
}
/**
@ -109,41 +115,63 @@ public class SibaManager
*
* @param choosenFile
* @return
* @throws IOException
* @throws NoSuchAlgorithmException
* @throws SibaException
*/
public static boolean check(final File choosenFile) throws IOException, NoSuchAlgorithmException
public static boolean check(final File choosenFile) throws SibaException
{
boolean result;
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++)
boolean result = false;
try
{
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];
}
if (choosenFile.exists() && (choosenFile.length() > 32))
{
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 = new File(choosenFile.getParent() + "/" + fileNameToString);
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;
if (StringUtils.equals(md5String, md5(fileToCheck1)))
{
result = true;
}
else
{
result = false;
}
}
else
{
throw new SibaException(BUNDLE.getString("NotExistFileToCheck.text"));
}
}
else
catch (IOException ioExceptionCheck)
{
result = false;
throw new SibaException(BUNDLE.getString("ioExceptionCheck.text"), ioExceptionCheck); //$NON-NLS-1$
}
return result;
}
@ -155,42 +183,48 @@ public class SibaManager
* @param directory
* @param outputStream
* @param pathLength
* @throws IOException
* @throws SibaException
*/
public static void directoryToSave(final File directory, final TarArchiveOutputStream outputStream,
final int pathLength) throws IOException
final int pathLength) throws SibaException
{
for (File file : directory.listFiles())
try
{
if (file.isDirectory())
for (File file : directory.listFiles())
{
if (file.listFiles().length == 0)
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
{
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$
}
}
/**
@ -200,19 +234,24 @@ public class SibaManager
* @param directoryToSave
* @param destination
* @return
* @throws IOException
* @throws SibaException
*/
public static File fileCheckMD5(final File directoryToSave, final String destination) throws IOException
public static File fileCheckMD5(final File directoryToSave, final String destination) throws SibaException
{
File result;
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();
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();
}
catch (IOException ioExceptionFileMD5)
{
throw new SibaException(BUNDLE.getString("ioExceptionFileMD5.text"), ioExceptionFileMD5); //$NON-NLS-1$
}
return result;
}
@ -222,9 +261,9 @@ public class SibaManager
*
* @param input
* @return
* @throws IOException
* @throws SibaException
*/
public static String md5(final File input) throws IOException
public static String md5(final File input) throws SibaException
{
String result;
StringBuilder hashString = new StringBuilder();
@ -236,7 +275,6 @@ public class SibaManager
{
byteInput[index1] = (byte) fileInputStream.read();
}
byte[] hash = null;
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
hash = messageDigest.digest(byteInput);
@ -256,10 +294,13 @@ public class SibaManager
}
fileInputStream.close();
}
catch (NoSuchAlgorithmException e)
catch (NoSuchAlgorithmException noSuchAlgorithmException)
{
// TODO Auto-generated catch block
e.printStackTrace();
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$
}
result = hashString.toString();
return result;

View File

@ -0,0 +1,10 @@
NotDirectorySource.text= Source is not a directory
sourceNotExist.text= The source does not exist
NotDirectoryTarget.text=The target is not a directory
noSuchAlgorithm.text= no such algorithm
ioExceptionMD5.text= Input/output Exception in MD5
ioExceptionFileMD5.text = Input/output Exception during creating File MD5
ioExceptionTarEntry.text= Input/output Exception during creating Tar Entry
ioExceptionCheck.text = Input/output Exception in Check
ioExceptionBackup.text = Input/output Exception in Backup
NotExistFileToCheck.text = The file to check do not exist or is not correct

View File

@ -0,0 +1,10 @@
NotDirectorySource.text=La source n'est pas un répertoire
sourceNotExist.text= La source n'existe pas
NotDirectoryTarget.text= La destination n'est pas un répertoire
noSuchAlgorithm.text = Mauvais choix d'algorithm
ioExceptionMD5.text = Erreur d'entrée-sortie dans MD5
ioExceptionFileMD5.text= Erreur d'entrée sortie dans la création du fichier MD5
ioExceptionTarEntry.text= Erreur d'entrée sortie dans la génération des Tar Entry
ioExceptionCheck.text = Erreur d'entrée sortie dans Check
ioExceptionBackup.text= Erreur d'entrée sortie dans Backup
NotExistFileToCheck.text= Le fichier à vérifier n'existe pas ou n'est pas valable