Skip to content
Snippets Groups Projects
Commit ffe227da authored by Falilou Thiam's avatar Falilou Thiam
Browse files

- Some code refactoring/cleaning

- Better error management
parent 6d4a6049
Branches
Tags
No related merge requests found
......@@ -62,6 +62,8 @@ import javax.swing.WindowConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.soleil.archiving.common.api.exception.ArchivingException;
import fr.soleil.archiving.gui.tools.AccountDelegate;
import fr.soleil.archiving.gui.tools.GUIUtilities;
import fr.soleil.archiving.snap.api.manager.SnapManagerApi;
import fr.soleil.archiving.snap.api.tools.SnapshotingException;
......@@ -110,6 +112,7 @@ public class Bensikin {
* @param user
*/
private static void createAndShowGUI() {
try {
// String title = "Bensikin v" + Messages.getMessage("ABOUT_RELEASE") +
// " (" + Messages.getMessage("ABOUT_RELEASE_DATE") + ")";
splash.progress(1);
......@@ -148,6 +151,9 @@ public class Bensikin {
frame.setVisible(true);
LOGGER.debug(Messages.getLogMessage("APPLICATION_STARTED_OK"));
} catch (Exception e) {
Bensikin.treatError(e, splash);
}
}
/**
......@@ -182,7 +188,7 @@ public class Bensikin {
try {
optionsPath = Bensikin.getPathToResources() + "/options";
final File path = new File(optionsPath);
if (!path.canWrite()) {
if (!path.exists()) {
final boolean created = path.mkdirs();
if (!created) {
LOGGER.error("Could not create file {}", path);
......@@ -197,9 +203,6 @@ public class Bensikin {
final String msg = Messages.getLogMessage("APPLICATION_WILL_START_LOAD_OPTIONS_OK");
LOGGER.debug(msg);
// } catch (final FileNotFoundException fnfe) {
// final String msg = Messages.getLogMessage("APPLICATION_WILL_START_LOAD_OPTIONS_WARNING");
// LOGGER.warn(msg, fnfe);
} catch (final Exception e) {
final String msg = Messages.getLogMessage("APPLICATION_WILL_START_LOAD_OPTIONS_KO");
LOGGER.error(msg, e);
......@@ -237,34 +240,14 @@ public class Bensikin {
hires = !(dim.width <= 1024);
}
protected static void launchAccountSelection() {
accountManager.launchAccountSelector();
if (accountManager.getSelectedAccount() == AccountManager.ACCOUNT_SELECTION_CANCELED) {
System.exit(0);
} else if (accountManager.getSelectedAccount() == AccountManager.NO_ACCOUNT_SELECTED) {
launchAccountSelection(); // launches account selector
} else {
pathToResources = accountManager.getSelectedAccountWorkingPath();
try {
if (pathToResources != null) {
File tmp = new File(pathToResources);
if (!tmp.exists()) {
tmp.mkdirs();
}
}
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(splash, "Can't write directory " + pathToResources, "Error!",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
accountManager.clearLock(pathToResources);
}
});
protected static boolean launchAccountSelection() {
boolean canceled;
AccountDelegate delegate = new AccountDelegate();
canceled = delegate.launchAccountSelector(Bensikin.class.getSimpleName(), accountManager, splash, LOGGER);
if (!canceled) {
pathToResources = delegate.getSelectedAccountPath();
}
return canceled;
}
public static boolean isRestricted() {
......@@ -297,15 +280,14 @@ public class Bensikin {
try {
final File path = new File(pathToResources);
if (!path.isDirectory()) {
System.out.println("Path did not exist: creating it");
path.mkdirs();
// throw new Exception("Not a directory");
}
if (!path.canRead()) {
throw new Exception("Invalid path");
throw new ArchivingException("Invalid path");
}
if (!path.canWrite()) {
throw new Exception("The path is read only");
throw new ArchivingException("The path is read only");
}
} catch (final Exception e) {
System.out.println("Incorrect arguments. The parameter " + pathToResources + " is not a valid directory");
......@@ -330,7 +312,9 @@ public class Bensikin {
// resources
System.exit(1);
}
launchAccountSelection();
if (launchAccountSelection()) {
System.exit(0);
} else {
System.out.println("Bensikin start");
initResolutionMode();
......@@ -352,12 +336,14 @@ public class Bensikin {
try {
SnapManagerApi.initFullSnapConnection("", "", "", user, password, "");
} catch (final SnapshotingException e) {
LOGGER.error("", e);
// LOGGER.error("", e);
treatError(e, splash);
}
createAndShowGUI();
}
});
}
}
public static void setUserRights(final String userRights_s) {
try {
......
......@@ -63,6 +63,7 @@ import javax.swing.border.TitledBorder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.soleil.archiving.common.api.exception.ArchivingException;
import fr.soleil.archiving.gui.tools.GUIUtilities;
import fr.soleil.archiving.snap.api.manager.SnapManagerApi;
import fr.soleil.archiving.snap.api.tools.SnapConst;
......@@ -463,17 +464,16 @@ public final class SnapshotPanel extends JPanel {
try {
final File path = new File(pathToResources);
if (!path.isDirectory()) {
System.out.println("Path did not exist: creating it");
final boolean created = path.mkdirs();
if (!created) {
throw new Exception("Could not create the path");
throw new ArchivingException("Could not create the path");
}
}
if (!path.canRead()) {
throw new Exception("Invalid path");
throw new ArchivingException("Invalid path");
}
if (!path.canWrite()) {
throw new Exception("The path is read only");
throw new ArchivingException("The path is read only");
}
} catch (final Exception e) {
System.out.println("Incorrect arguments. The parameter " + pathToResources + " is not a valid directory");
......
......@@ -37,7 +37,7 @@ import fr.soleil.bensikin.xml.BensikinXMLLine;
*/
public class Favorites {
private FavoritesContextSubMenu contextSubMenu = null;
private static Favorites instance = null;
private static final Favorites INSTANCE = new Favorites();
/**
* The XML tag name used in saving/loading
......@@ -57,11 +57,7 @@ public class Favorites {
* @return The instance
*/
public static Favorites getInstance() {
if (instance == null) {
instance = new Favorites();
}
return instance;
return INSTANCE;
}
/**
......@@ -69,6 +65,7 @@ public class Favorites {
*
* @return a XML representation of the favorites
*/
@Override
public String toString() {
String ret = "";
......
......@@ -208,18 +208,14 @@ public class XMLFavoritesManager extends XMLDataManager<Favorites, Map<String, M
if (FavoritesContextSubMenu.XML_TAG.equals(currentBookType)) {
currentBook = loadContextsBook(currentBookNode);
favorites.put(FavoritesContextSubMenu.XML_TAG, currentBook);
}
}
}
}
return favorites;
} catch (Exception e) {
if (e instanceof ArchivingException) {
throw (ArchivingException) e;
} else {
throw new ArchivingException(e);
}
e.printStackTrace();
throw ArchivingException.toArchivingException(e);
}
}
......
......@@ -115,20 +115,20 @@ public class DefaultLifeCycleManager implements LifeCycleManager {
* Not used
*/
@Override
public void applicationWillStart(final Splash splash) {
public void applicationWillStart(final Splash splash) throws ArchivingException {
startFactories();
final Locale currentLocale = new Locale("en", "US");
try {
Messages.initResourceBundle(currentLocale);
} catch (final Exception e) {
Bensikin.treatError(e, splash);
throw ArchivingException.toArchivingException(e);
}
// No exception can be raised
loadOptions(splash);
// No exception can be raised
loadFavorites();
loadFavorites(splash);
if (hasHistorySave) {
// No exception can be raised
......@@ -174,13 +174,13 @@ public class DefaultLifeCycleManager implements LifeCycleManager {
/**
* Loads the application's favorites.
*/
private void loadFavorites() {
private void loadFavorites(Splash splash) {
try {
favoritesPath = Bensikin.getPathToResources() + "/favorites";
final File f = new File(favoritesPath);
if (!f.canWrite()) {
if (!f.exists()) {
// boolean b =
f.mkdir();
f.mkdirs();
}
favoritesPath += "/favorites.xml";
......@@ -195,7 +195,7 @@ public class DefaultLifeCycleManager implements LifeCycleManager {
// LOGGER.warn(msg, fnfe);
} catch (final ArchivingException e) {
final String msg = Messages.getLogMessage("APPLICATION_WILL_START_LOAD_FAVORITES_KO");
LOGGER.error(msg, e);
Bensikin.treatError(e, msg, splash);
}
}
......@@ -238,9 +238,10 @@ public class DefaultLifeCycleManager implements LifeCycleManager {
splash.setMessage("preparing history");
historyPath = Bensikin.getPathToResources() + "/history";
final File f = new File(historyPath);
if (!f.canWrite()) {
if (!f.exists()) {
// boolean b =
f.mkdir();
f.mkdirs();
}
historyPath += "/history.xml";
......@@ -356,9 +357,10 @@ public class DefaultLifeCycleManager implements LifeCycleManager {
final String msg = Messages.getLogMessage("APPLICATION_WILL_STOP_SAVE_FAVORITES_OK");
LOGGER.debug(msg);
} catch (final Exception e) {
e.printStackTrace();
final String msg = Messages.getLogMessage("APPLICATION_WILL_STOP_SAVE_FAVORITES_KO");
LOGGER.error(msg, e);
return;
}
}
......
......@@ -29,6 +29,7 @@
//-======================================================================
package fr.soleil.bensikin.lifecycle;
import fr.soleil.archiving.common.api.exception.ArchivingException;
import fr.soleil.lib.project.swing.Splash;
/**
......@@ -55,7 +56,7 @@ public interface LifeCycleManager {
* @param startParameters
* Can contain parameters if necessary
*/
public void applicationWillStart(Splash splash);
public void applicationWillStart(Splash splash) throws ArchivingException;
/**
* Called just after the GUI graphics containers are instantiated. Must be
......
......@@ -51,8 +51,8 @@ public class PersistentColumnModelEncoder extends XMLEncoder {
String path = Bensikin.getPathToResources();
String absp = path + "/beans";
File f = new File(absp);
if (!f.canWrite()) {
f.mkdir();
if (!f.exists()) {
f.mkdirs();
}
absp += "/columnModel" + ".bean";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment