From 0d7f526c8bd275c9874877a7d9e54fa78ba593ed Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Wed, 17 Oct 2007 13:06:57 +0000
Subject: [PATCH] Add some code in order the code of the setup to be able to be more reactive to the Cancel operation. Now the user does not have to wait until the end of the download to stop the setup process nor to the end of the replication initialization.
---
opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java | 308 ++++++++++++++++++++++++++++++++++---------
opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/offline/OfflineInstaller.java | 1
opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/webstart/WebStartInstaller.java | 52 +++++--
opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/ui/QuickSetup.java | 5
opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/ui/GuiApplication.java | 18 +-
opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/util/ZipExtractor.java | 2
6 files changed, 291 insertions(+), 95 deletions(-)
diff --git a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java
index 573daab..0ab8d81 100644
--- a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java
+++ b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java
@@ -855,7 +855,7 @@
argList.add("-R");
argList.add(getInstallation().getRootDirectory().getAbsolutePath());
- String[] args = new String[argList.size()];
+ final String[] args = new String[argList.size()];
argList.toArray(args);
StringBuilder cmd = new StringBuilder();
for (String s : argList)
@@ -867,23 +867,37 @@
cmd.append(s);
}
LOG.log(Level.INFO, "configure DS cmd: "+cmd);
- try
+ final InstallerHelper helper = new InstallerHelper();
+ InvokeThread thread = new InvokeThread()
{
- InstallerHelper helper = new InstallerHelper();
- int result = helper.invokeConfigureServer(args);
-
- if (result != 0)
+ public void run()
{
- throw new ApplicationException(
- ReturnCode.CONFIGURATION_ERROR,
- INFO_ERROR_CONFIGURING.get(), null);
+ int result = -1;
+ try
+ {
+ result = helper.invokeConfigureServer(args);
+ if (result != 0)
+ {
+ ae = new ApplicationException(
+ ReturnCode.CONFIGURATION_ERROR,
+ INFO_ERROR_CONFIGURING.get(), null);
+ }
+ } catch (Throwable t)
+ {
+ ae = new ApplicationException(
+ ReturnCode.CONFIGURATION_ERROR,
+ getThrowableMsg(INFO_ERROR_CONFIGURING.get(), t), t);
+ }
+ isOver = true;
}
- } catch (Throwable t)
- {
- throw new ApplicationException(
- ReturnCode.CONFIGURATION_ERROR,
- getThrowableMsg(INFO_ERROR_CONFIGURING.get(), t), t);
- }
+ public void abort()
+ {
+ // TODO: implement the abort
+ }
+ };
+ invokeLongOperation(thread);
+
+ checkAbort();
try
{
@@ -1013,7 +1027,7 @@
INFO_PROGRESS_CREATING_BASE_ENTRIES.get()));
}
- InstallerHelper helper = new InstallerHelper();
+ final InstallerHelper helper = new InstallerHelper();
LinkedList<File> ldifFiles = new LinkedList<File>();
@@ -1021,6 +1035,8 @@
{
ldifFiles.add(helper.createBaseEntryTempFile(baseDn));
}
+ checkAbort();
+
ArrayList<String> argList = new ArrayList<String>();
argList.add("-C");
argList.add(getConfigurationClassName());
@@ -1041,32 +1057,45 @@
argList.add("-Q");
- String[] args = new String[argList.size()];
+ final String[] args = new String[argList.size()];
argList.toArray(args);
getApplicationOutputStream().setNotifyListeners(false);
getApplicationErrorStream().setNotifyListeners(false);
- try
- {
- int result = helper.invokeImportLDIF(args);
- if (result != 0)
+ InvokeThread thread = new InvokeThread()
+ {
+ public void run()
{
- throw new ApplicationException(
- ReturnCode.CONFIGURATION_ERROR,
- INFO_ERROR_CREATING_BASE_ENTRY.get(), null);
+ try
+ {
+ int result = helper.invokeImportLDIF(args);
+
+ if (result != 0)
+ {
+ ae = new ApplicationException(
+ ReturnCode.CONFIGURATION_ERROR,
+ INFO_ERROR_CREATING_BASE_ENTRY.get(), null);
+ }
+ } catch (Throwable t)
+ {
+ ae = new ApplicationException(
+ ReturnCode.CONFIGURATION_ERROR,
+ getThrowableMsg(INFO_ERROR_CREATING_BASE_ENTRY.get(), t), t);
+ }
+ finally
+ {
+ getApplicationOutputStream().setNotifyListeners(true);
+ getApplicationErrorStream().setNotifyListeners(true);
+ }
+ isOver = true;
}
- } catch (Throwable t)
- {
- throw new ApplicationException(
- ReturnCode.CONFIGURATION_ERROR,
- getThrowableMsg(INFO_ERROR_CREATING_BASE_ENTRY.get(), t), t);
- }
- finally
- {
- getApplicationOutputStream().setNotifyListeners(true);
- getApplicationErrorStream().setNotifyListeners(true);
- }
+ public void abort()
+ {
+ // TODO: implement the abort
+ }
+ };
+ invokeLongOperation(thread);
notifyListeners(getFormattedDone());
}
@@ -1106,26 +1135,38 @@
argList.add(ldifPath);
}
argList.add("-F");
- String[] args = new String[argList.size()];
+ final String[] args = new String[argList.size()];
argList.toArray(args);
- try
+ InvokeThread thread = new InvokeThread()
{
- InstallerHelper helper = new InstallerHelper();
- int result = helper.invokeImportLDIF(args);
-
- if (result != 0)
+ public void run()
{
- throw new ApplicationException(
- ReturnCode.CONFIGURATION_ERROR,
- INFO_ERROR_IMPORTING_LDIF.get(), null);
+ try
+ {
+ InstallerHelper helper = new InstallerHelper();
+ int result = helper.invokeImportLDIF(args);
+
+ if (result != 0)
+ {
+ ae = new ApplicationException(
+ ReturnCode.CONFIGURATION_ERROR,
+ INFO_ERROR_IMPORTING_LDIF.get(), null);
+ }
+ } catch (Throwable t)
+ {
+ ae = new ApplicationException(
+ ReturnCode.CONFIGURATION_ERROR,
+ getThrowableMsg(INFO_ERROR_IMPORTING_LDIF.get(), t), t);
+ }
+ isOver = true;
}
- } catch (Throwable t)
- {
- throw new ApplicationException(
- ReturnCode.CONFIGURATION_ERROR,
- getThrowableMsg(INFO_ERROR_IMPORTING_LDIF.get(), t), t);
- }
+ public void abort()
+ {
+ // TODO: implement the abort
+ }
+ };
+ invokeLongOperation(thread);
}
/**
@@ -1145,7 +1186,7 @@
for (File templatePath : templatePaths)
{
- ArrayList<String> argList = new ArrayList<String>();
+ final ArrayList<String> argList = new ArrayList<String>();
argList.add("-C");
argList.add(getConfigurationClassName());
@@ -1161,28 +1202,41 @@
// append: each file contains data for each base DN.
argList.add("-a");
- String[] args = new String[argList.size()];
+ final String[] args = new String[argList.size()];
argList.toArray(args);
- try
+ InvokeThread thread = new InvokeThread()
{
- InstallerHelper helper = new InstallerHelper();
- int result = helper.invokeImportLDIF(args);
-
- if (result != 0)
+ public void run()
{
- throw new ApplicationException(
- ReturnCode.CONFIGURATION_ERROR,
- INFO_ERROR_IMPORT_LDIF_TOOL_RETURN_CODE.get(
- Integer.toString(result)), null);
+ try
+ {
+ InstallerHelper helper = new InstallerHelper();
+ int result = helper.invokeImportLDIF(args);
+
+ if (result != 0)
+ {
+ ae = new ApplicationException(
+ ReturnCode.CONFIGURATION_ERROR,
+ INFO_ERROR_IMPORT_LDIF_TOOL_RETURN_CODE.get(
+ Integer.toString(result)), null);
+ }
+ } catch (Throwable t)
+ {
+ ae = new ApplicationException(
+ ReturnCode.CONFIGURATION_ERROR,
+ getThrowableMsg(INFO_ERROR_IMPORT_AUTOMATICALLY_GENERATED.get(
+ listToString(argList, " "), t.getLocalizedMessage()), t),
+ t);
+ }
+ isOver = true;
}
- } catch (Throwable t)
- {
- throw new ApplicationException(
- ReturnCode.CONFIGURATION_ERROR,
- getThrowableMsg(INFO_ERROR_IMPORT_AUTOMATICALLY_GENERATED.get(
- listToString(argList, " "), t.getLocalizedMessage()), t), t);
- }
+ public void abort()
+ {
+ // TODO: implement the abort
+ }
+ };
+ invokeLongOperation(thread);
}
}
@@ -1642,7 +1696,7 @@
*
* @throws ApplicationException thrown if <code>canceled</code>
*/
- protected void checkAbort() throws ApplicationException {
+ public void checkAbort() throws ApplicationException {
if (canceled) {
setCurrentProgressStep(InstallProgressStep.CANCELING);
notifyListeners(null);
@@ -3819,6 +3873,7 @@
String.valueOf(replicaId));
while (!taskCreated)
{
+ checkAbort();
String id = "quicksetup-initialize"+i;
dn = "ds-task-id="+id+",cn=Scheduled Tasks,cn=Tasks";
attrs.put("ds-task-id", id);
@@ -3864,6 +3919,13 @@
int totalEntries = 0;
while (!isOver)
{
+ if (canceled)
+ {
+ // TODO: we should try to cleanly abort the initialize. As we have
+ // aborted the install, the server will be stopped and the remote
+ // server will receive a connect error.
+ checkAbort();
+ }
try
{
Thread.sleep(500);
@@ -3871,6 +3933,13 @@
catch (Throwable t)
{
}
+ if (canceled)
+ {
+ // TODO: we should try to cleanly abort the initialize. As we have
+ // aborted the install, the server will be stopped and the remote
+ // server will receive a connect error.
+ checkAbort();
+ }
try
{
NamingEnumeration res = ctx.search(dn, filter, searchControls);
@@ -4115,6 +4184,7 @@
attrs.put("ds-task-reset-generation-id-domain-base-dn", suffixDn);
while (!taskCreated)
{
+ checkAbort();
String id = "quicksetup-reset-generation-id-"+i;
dn = "ds-task-id="+id+",cn=Scheduled Tasks,cn=Tasks";
attrs.put("ds-task-id", id);
@@ -4194,11 +4264,13 @@
if (helper.isCompletedWithErrors(state))
{
+ LOG.log(Level.WARNING, "Completed with error: "+errorMsg);
notifyListeners(getFormattedWarning(errorMsg));
}
else if (!helper.isSuccessful(state) ||
helper.isStoppedByError(state))
{
+ LOG.log(Level.WARNING, "Error: "+errorMsg);
ApplicationException ae = new ApplicationException(
ReturnCode.APPLICATION_ERROR, errorMsg,
null);
@@ -4220,4 +4292,102 @@
}
}
}
+
+ /**
+ * Invokes a long operation in a separate thread and checks whether the user
+ * canceled the operation or not.
+ * @param thread the Thread that must be launched.
+ * @throws ApplicationException if there was an error executing the task or
+ * if the user canceled the installer.
+ */
+ private void invokeLongOperation(InvokeThread thread)
+ throws ApplicationException
+ {
+ try
+ {
+ thread.start();
+ while (!thread.isOver() && thread.isAlive())
+ {
+ if (canceled)
+ {
+ // Try to abort the thread
+ try
+ {
+ thread.abort();
+ }
+ catch (Throwable t)
+ {
+ LOG.log(Level.WARNING, "Error cancelling thread: "+t, t);
+ }
+ }
+ else if (thread.getException() != null)
+ {
+ throw thread.getException();
+ }
+ else
+ {
+ try
+ {
+ Thread.sleep(100);
+ }
+ catch (Throwable t)
+ {
+ }
+ }
+ }
+ if (canceled)
+ {
+ checkAbort();
+ }
+ }
+ catch (ApplicationException e)
+ {
+ throw e;
+ }
+ catch (Throwable t)
+ {
+ LOG.log(Level.SEVERE, "Error: "+t, t);
+ throw new ApplicationException(ReturnCode.BUG,
+ Utils.getThrowableMsg(INFO_BUG_MSG.get(), t), t);
+ }
+ }
+}
+
+/**
+ * Class used to be able to cancel long operations.
+ */
+abstract class InvokeThread extends Thread implements Runnable
+{
+ protected boolean isOver = false;
+ protected ApplicationException ae;
+
+ /**
+ * Returns <CODE>true</CODE> if the thread is over and <CODE>false</CODE>
+ * otherwise.
+ * @return <CODE>true</CODE> if the thread is over and <CODE>false</CODE>
+ * otherwise.
+ */
+ public boolean isOver()
+ {
+ return isOver;
+ }
+
+ /**
+ * Returns the exception that was encountered running the thread.
+ * @return the exception that was encountered running the thread.
+ */
+ public ApplicationException getException()
+ {
+ return ae;
+ }
+
+ /**
+ * Runnable implementation.
+ */
+ public abstract void run();
+
+ /**
+ * Abort this thread.
+ */
+ public abstract void abort();
}
diff --git a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/offline/OfflineInstaller.java b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/offline/OfflineInstaller.java
index 068754f..1f871fc 100644
--- a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/offline/OfflineInstaller.java
+++ b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/offline/OfflineInstaller.java
@@ -162,6 +162,7 @@
} catch (ApplicationException ex)
{
+ LOG.log(Level.SEVERE, "Caught exception: "+ex, ex);
if (ReturnCode.CANCELLED.equals(ex.getType())) {
uninstall();
setCurrentProgressStep(InstallProgressStep.FINISHED_CANCELED);
diff --git a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/webstart/WebStartInstaller.java b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/webstart/WebStartInstaller.java
index 6de3e2c..aa9e458 100644
--- a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/webstart/WebStartInstaller.java
+++ b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/webstart/WebStartInstaller.java
@@ -105,6 +105,7 @@
initMaps();
PrintStream origErr = System.err;
PrintStream origOut = System.out;
+ boolean downloadedBits = false;
try
{
System.setErr(getApplicationErrorStream());
@@ -125,6 +126,7 @@
createParentDirectoryIfRequired();
extractZipFiles(in, getRatio(InstallProgressStep.EXTRACTING),
getRatio(InstallProgressStep.CONFIGURING_SERVER));
+ downloadedBits = true;
try
{
@@ -212,7 +214,8 @@
} catch (ApplicationException ex)
{
if (ReturnCode.CANCELLED.equals(ex.getType())) {
- uninstall();
+ uninstall(downloadedBits);
+
setCurrentProgressStep(InstallProgressStep.FINISHED_CANCELED);
notifyListeners(null);
} else {
@@ -515,29 +518,46 @@
/**
* Uninstall what has already been installed.
+ * @param downloadedBits whether the bits were downloaded or not.
*/
- private void uninstall() {
- notifyListeners(getTaskSeparator());
+ private void uninstall(boolean downloadedBits) {
+ if (downloadedBits)
+ {
+ notifyListeners(getTaskSeparator());
+ Installation installation = getInstallation();
+ FileManager fm = new FileManager(this);
- Installation installation = getInstallation();
- FileManager fm = new FileManager(this);
+ // Stop the server if necessary
+ if (installation.getStatus().isServerRunning()) {
+ try {
+ new ServerController(installation).stopServer(true);
+ } catch (ApplicationException e) {
+ LOG.log(Level.INFO, "error stopping server", e);
+ }
+ }
- // Stop the server if necessary
- if (installation.getStatus().isServerRunning()) {
+ uninstallServices();
+
try {
- new ServerController(installation).stopServer(true);
+ fm.deleteRecursively(installation.getRootDirectory(), null,
+ FileManager.DeletionPolicy.DELETE_ON_EXIT_IF_UNSUCCESSFUL);
} catch (ApplicationException e) {
- LOG.log(Level.INFO, "error stopping server", e);
+ LOG.log(Level.INFO, "error deleting files", e);
}
}
-
- uninstallServices();
-
- try {
- fm.deleteRecursively(installation.getRootDirectory(), null,
+ else
+ {
+ File serverRoot = new File(getUserData().getServerLocation());
+ if (serverRoot.exists())
+ {
+ FileManager fm = new FileManager(this);
+ try {
+ fm.deleteRecursively(serverRoot, null,
FileManager.DeletionPolicy.DELETE_ON_EXIT_IF_UNSUCCESSFUL);
- } catch (ApplicationException e) {
- LOG.log(Level.INFO, "error deleting files", e);
+ } catch (ApplicationException e) {
+ LOG.log(Level.INFO, "error deleting files", e);
+ }
+ }
}
}
diff --git a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/ui/GuiApplication.java b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/ui/GuiApplication.java
index add42d3..8fbc49a 100644
--- a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/ui/GuiApplication.java
+++ b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/ui/GuiApplication.java
@@ -496,6 +496,7 @@
WebStartDownloader.Status.DOWNLOADING;
while (!loader.isFinished() && (loader.getException() == null))
{
+ checkAbort();
// Pool until is over
int perc = loader.getDownloadPercentage();
WebStartDownloader.Status downloadStatus = loader.getStatus();
@@ -521,16 +522,17 @@
}
loader.setSummary(summary);
notifyListeners(ratio, summary, null);
-
- try
- {
- Thread.sleep(300);
- } catch (Exception ex)
- {
- // do nothing;
- }
+ }
+ checkAbort();
+ try
+ {
+ Thread.sleep(300);
+ } catch (Exception ex)
+ {
+ // do nothing;
}
}
+ checkAbort();
if (loader.getException() != null)
{
diff --git a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/ui/QuickSetup.java b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/ui/QuickSetup.java
index 6557439..eb1e1bc 100644
--- a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/ui/QuickSetup.java
+++ b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/ui/QuickSetup.java
@@ -695,8 +695,9 @@
{
progressDetails.append(additionalDetails);
}
- // Note: progressDetails might have lot of messages and since the fix for
- // issue 2142 was committed there is a limitation in this area. So here
+ // Note: progressDetails might have a certain number of characters that
+ // break Message Formatter (for instance percentages). When fix for
+ // issue 2142 was committed it broke this code. So here
// we use Message.raw instead of calling directly progressDetails.toMessage
return new ProgressDescriptor(status, ratio, newProgressLabel,
Message.raw(progressDetails.toString()));
diff --git a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/util/ZipExtractor.java b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/util/ZipExtractor.java
index cb1f029..8b61608 100644
--- a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/util/ZipExtractor.java
+++ b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/util/ZipExtractor.java
@@ -180,8 +180,10 @@
new HashMap<String, ArrayList<String>>();
try {
+ application.checkAbort();
ZipEntry entry = zipIn.getNextEntry();
while (entry != null) {
+ application.checkAbort();
int ratioBeforeCompleted = minRatio
+ ((nEntries - 1) * (maxRatio - minRatio) / numberZipEntries);
int ratioWhenCompleted =
--
Gitblit v1.10.0