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; 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.cli.SibaCLI;
import org.dclermonte.siba.gui.SibaGUI; import org.dclermonte.siba.gui.SibaGUI;
@ -33,14 +29,9 @@ public class Siba
* *
* *
* @param args * @param args
* @throws ArchiveException
* @throws IOException
* @throws NoSuchAlgorithmException
* @throws SibaException
*/ */
public static void main(final String[] args) public static void main(final String[] args)
throws ArchiveException, IOException, NoSuchAlgorithmException, SibaException
{ {
if (args.length == 0) 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; package org.dclermonte.siba;
/**
*
* @author dclermonte
*
*/
public class SibaException extends Exception public class SibaException extends Exception
{ {

View File

@ -20,11 +20,8 @@
package org.dclermonte.siba.cli; package org.dclermonte.siba.cli;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.dclermonte.siba.SibaException; import org.dclermonte.siba.SibaException;
import org.dclermonte.siba.model.SibaManager; import org.dclermonte.siba.model.SibaManager;
@ -45,12 +42,9 @@ public class SibaCLI
* *
* @param directoryToSave * @param directoryToSave
* @param targetDirectory * @param targetDirectory
* @throws ArchiveException
* @throws IOException
* @throws SibaException * @throws SibaException
*/ */
public static void backup(final File directoryToSave, final File targetDirectory) public static void backup(final File directoryToSave, final File targetDirectory) throws SibaException
throws ArchiveException, IOException, SibaException
{ {
File result; File result;
result = SibaManager.backup(directoryToSave, targetDirectory); 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 * @param fileToCheck
* @throws NoSuchAlgorithmException * @throws SibaException
* @throws IOException
*/ */
public static void check(final File fileToCheck) throws NoSuchAlgorithmException, IOException public static void check(final File fileToCheck) throws SibaException
{ {
boolean result; boolean result;
@ -106,12 +99,8 @@ public class SibaCLI
* This method launch CLI. * This method launch CLI.
* *
* @param args * @param args
* @throws ArchiveException
* @throws IOException
* @throws NoSuchAlgorithmException
*/ */
public static void run(final String[] args) public static void run(final String[] args)
throws ArchiveException, IOException, NoSuchAlgorithmException, SibaException
{ {
try try
{ {
@ -121,9 +110,7 @@ public class SibaCLI
{ {
help(); help();
} }
else else if (StringUtils.equals(args[0], "-h") || StringUtils.equals(args[0], "-help")
{
if (StringUtils.equals(args[0], "-h") || StringUtils.equals(args[0], "-help")
|| StringUtils.equals(args[0], "--help")) || StringUtils.equals(args[0], "--help"))
{ {
help(); help();
@ -132,36 +119,15 @@ public class SibaCLI
{ {
if (parameterCount == 1) if (parameterCount == 1)
{ {
System.out.println(BUNDLE.getString("missingDirectoryToSave.text")); backup(null, null);
help();
} }
else if (parameterCount == 2) else if (parameterCount == 2)
{ {
File directoryToSave = new File(args[1]); backup(new File(args[1]), null);
if (directoryToSave.exists())
{
backup(directoryToSave, new File(System.getProperty("user.dir")));
}
else
{
System.out.println(BUNDLE.getString("directoryToSaveNotExist.text"));
help();
}
} }
else if (parameterCount == 3) else if (parameterCount == 3)
{ {
File directoryToSave = new File(args[1]); backup(new File(args[1]), new File(args[2]));
File targetDirectory = new File(args[2]);
if (directoryToSave.exists() && targetDirectory.exists())
{
backup(directoryToSave, targetDirectory);
}
else
{
System.out.println(BUNDLE.getString("directoryNotExist.text"));
help();
}
} }
else else
{ {
@ -173,10 +139,9 @@ public class SibaCLI
{ {
if (parameterCount == 1) if (parameterCount == 1)
{ {
System.out.println(BUNDLE.getString("missingFileToCheck.text")); check(null);
help();
} }
else else if (parameterCount == 2)
{ {
String fileToCheckName; String fileToCheckName;
if (!args[1].endsWith("tgz") && !args[1].endsWith("md5")) if (!args[1].endsWith("tgz") && !args[1].endsWith("md5"))
@ -192,27 +157,14 @@ public class SibaCLI
fileToCheckName = args[1]; fileToCheckName = args[1];
} }
File fileToCheck = new File(fileToCheckName); File fileToCheck = new File(fileToCheckName);
File fileToCheckFinal; check(fileToCheck);
if (!fileToCheck.isAbsolute())
{
fileToCheckFinal = new File(System.getProperty("user.dir") + "/" + fileToCheckName);
} }
else else
{ {
fileToCheckFinal = new File(fileToCheckName); System.out.println(BUNDLE.getString("badUsage.text"));
}
if (fileToCheckFinal.exists())
{
check(fileToCheckFinal);
}
else
{
System.out.println(BUNDLE.getString("missingFileToCheck.text"));
help(); help();
} }
} }
}
else else
{ {
System.out.println(BUNDLE.getString("badUsage.text")); System.out.println(BUNDLE.getString("badUsage.text"));
@ -220,10 +172,14 @@ public class SibaCLI
} }
} }
} catch (SibaException sibaException)
catch (SibaException nullException)
{ {
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

@ -9,3 +9,4 @@ directoryNotExist.text = one of the directories does not exist
missingFileToCheck.text = Missing the file to check missingFileToCheck.text = Missing the file to check
missingTargetFile.text=missing target file 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

@ -9,3 +9,4 @@ directoryNotExist.text = L'un des r
missingFileToCheck.text = Il manque le fichier à vérifier missingFileToCheck.text = Il manque le fichier à vérifier
missingTargetFile.text=Le répertoire destination n'existe pas 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 public class AboutDialog extends JDialog

View File

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

View File

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

View File

@ -50,7 +50,7 @@ import org.dclermonte.siba.gui.utils.GUIToolBox;
/** /**
* The main class for GUI (Graphical User Interface ) * The main class for GUI (Graphical User Interface )
* *
* @author papou * @author dclermonte
* *
*/ */
public class SibaGUI public class SibaGUI
@ -251,9 +251,14 @@ public class SibaGUI
SibaGUI window = new SibaGUI(); SibaGUI window = new SibaGUI();
window.frmSimpleBackup.setVisible(true); 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"); String titleWarning = BUNDLE.getString("BackupPanel.TitleWarning.text");
JOptionPane.showMessageDialog(null, dataMissing, titleWarning, JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, dataMissing, titleWarning, JOptionPane.INFORMATION_MESSAGE);
} }

View File

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

View File

@ -33,27 +33,22 @@ import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.ResourceBundle; 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.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang3.StringUtils; 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; import org.dclermonte.siba.SibaException;
/** /**
* The model class
* *
* @author papou * @author dclermonte
* *
*/ */
public class SibaManager 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. * This method perform the backup.
@ -61,31 +56,24 @@ public class SibaManager
* @param fileToSave * @param fileToSave
* @param target * @param target
* @return * @return
* @throws ArchiveException * @throws SibaException
* @throws IOException
* @throws SibaBackupTargetMissingException
*/ */
public static File backup(final File fileToSave, final File target) public static File backup(final File fileToSave, File target) throws SibaException
throws ArchiveException, IOException, SibaException
{ {
File result = null; File result = null;
try
{ {
if (!target.exists()) if (!(fileToSave == null) && fileToSave.exists())
{ {
throw new SibaBackupTargetMissingException(BUNDLE.getString("missingTargetFile.text")); if (fileToSave.isDirectory())
{
if ((target == null))
{
target = new File(System.getProperty("user.dir"));
} }
else if (!fileToSave.exists()) if (target.isDirectory())
{
throw new SibaBackupSourceMissingException(BUNDLE.getString("missingDirectoryToSave.text"));
}
else if (!fileToSave.isDirectory())
{
throw new SibaBackupSourceNotDirectoryException(BUNDLE.getString("NotDirectorySource.text"));
}
else
{ {
int pathLength = fileToSave.getParentFile().getAbsolutePath().length(); int pathLength = fileToSave.getParentFile().getAbsolutePath().length();
LocalDateTime date = LocalDateTime.now(); LocalDateTime date = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH'h'mm'mn'ss's'"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH'h'mm'mn'ss's'");
String textDate = date.format(formatter); String textDate = date.format(formatter);
@ -99,22 +87,43 @@ public class SibaManager
out.close(); out.close();
fileCheckMD5(result, target + "/" + outputFileNameWithoutExtension + ".tgz.md5"); fileCheckMD5(result, target + "/" + outputFileNameWithoutExtension + ".tgz.md5");
} }
else
return result; {
throw new SibaException(BUNDLE.getString("NotDirectoryTarget.text"));
} }
} }
else
{
throw new SibaException(BUNDLE.getString("NotDirectorySource.text"));
}
}
else
{
throw new SibaException(BUNDLE.getString("sourceNotExist.text"));
}
}
catch (IOException ioExceptionBackup)
{
throw new SibaException(BUNDLE.getString("ioExceptionBackup.text"), ioExceptionBackup); //$NON-NLS-1$
}
return result;
}
/** /**
* This method is used to check the file. * This method is used to check the file.
* *
* @param choosenFile * @param choosenFile
* @return * @return
* @throws IOException * @throws SibaException
* @throws NoSuchAlgorithmException
*/ */
public static boolean check(final File choosenFile) throws IOException, NoSuchAlgorithmException public static boolean check(final File choosenFile) throws SibaException
{
boolean result = false;
try
{
if (choosenFile.exists() && (choosenFile.length() > 32))
{ {
boolean result;
FileReader fileReader = new FileReader(choosenFile); FileReader fileReader = new FileReader(choosenFile);
char[] md5 = new char[32]; char[] md5 = new char[32];
byte[] md5byte = new byte[32]; byte[] md5byte = new byte[32];
@ -135,7 +144,16 @@ public class SibaManager
} }
fileReader.close(); fileReader.close();
File fileToCheck1 = new File(choosenFile.getParent() + "/" + fileNameToString); 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))) if (StringUtils.equals(md5String, md5(fileToCheck1)))
{ {
@ -145,6 +163,16 @@ public class SibaManager
{ {
result = false; result = false;
} }
}
else
{
throw new SibaException(BUNDLE.getString("NotExistFileToCheck.text"));
}
}
catch (IOException ioExceptionCheck)
{
throw new SibaException(BUNDLE.getString("ioExceptionCheck.text"), ioExceptionCheck); //$NON-NLS-1$
}
return result; return result;
} }
@ -155,10 +183,12 @@ public class SibaManager
* @param directory * @param directory
* @param outputStream * @param outputStream
* @param pathLength * @param pathLength
* @throws IOException * @throws SibaException
*/ */
public static void directoryToSave(final File directory, final TarArchiveOutputStream outputStream, public static void directoryToSave(final File directory, final TarArchiveOutputStream outputStream,
final int pathLength) throws IOException final int pathLength) throws SibaException
{
try
{ {
for (File file : directory.listFiles()) for (File file : directory.listFiles())
{ {
@ -190,7 +220,11 @@ public class SibaManager
} }
} }
}
catch (IOException ioExceptionTarEntry)
{
throw new SibaException(BUNDLE.getString("ioExceptionTarEntry.text"), ioExceptionTarEntry); //$NON-NLS-1$
}
} }
/** /**
@ -200,19 +234,24 @@ public class SibaManager
* @param directoryToSave * @param directoryToSave
* @param destination * @param destination
* @return * @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; File result;
try
{
result = new File(destination); result = new File(destination);
FileWriter fileWriter = new FileWriter(result); FileWriter fileWriter = new FileWriter(result);
fileWriter.write(md5(directoryToSave)); fileWriter.write(md5(directoryToSave));
String newLine = System.getProperty("line.separator"); String newLine = System.getProperty("line.separator");
fileWriter.append(" " + directoryToSave.getName() + newLine); fileWriter.append(" " + directoryToSave.getName() + newLine);
fileWriter.close(); fileWriter.close();
}
catch (IOException ioExceptionFileMD5)
{
throw new SibaException(BUNDLE.getString("ioExceptionFileMD5.text"), ioExceptionFileMD5); //$NON-NLS-1$
}
return result; return result;
} }
@ -222,9 +261,9 @@ public class SibaManager
* *
* @param input * @param input
* @return * @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; String result;
StringBuilder hashString = new StringBuilder(); StringBuilder hashString = new StringBuilder();
@ -236,7 +275,6 @@ public class SibaManager
{ {
byteInput[index1] = (byte) fileInputStream.read(); byteInput[index1] = (byte) fileInputStream.read();
} }
byte[] hash = null; byte[] hash = null;
MessageDigest messageDigest = MessageDigest.getInstance("MD5"); MessageDigest messageDigest = MessageDigest.getInstance("MD5");
hash = messageDigest.digest(byteInput); hash = messageDigest.digest(byteInput);
@ -256,10 +294,13 @@ public class SibaManager
} }
fileInputStream.close(); fileInputStream.close();
} }
catch (NoSuchAlgorithmException e) catch (NoSuchAlgorithmException noSuchAlgorithmException)
{ {
// TODO Auto-generated catch block throw new SibaException(BUNDLE.getString("noSuchAlgorithm.text"), noSuchAlgorithmException); //$NON-NLS-1$
e.printStackTrace(); }
catch (IOException ioException)
{
throw new SibaException(BUNDLE.getString("ioExceptionMD5.text"), ioException); //$NON-NLS-1$
} }
result = hashString.toString(); result = hashString.toString();
return result; 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