Search in sources :

Example 1 with AddOnUninstallListener

use of org.zaproxy.zap.extension.autoupdate.UninstallationProgressDialogue.AddOnUninstallListener in project zaproxy by zaproxy.

the class ExtensionAutoUpdate method uninstallAddOnsWithView.

boolean uninstallAddOnsWithView(final Window caller, final Set<AddOn> addOns, final boolean updates, final Set<AddOn> failedUninstallations) {
    if (addOns == null || addOns.isEmpty()) {
        return true;
    }
    if (!EventQueue.isDispatchThread()) {
        try {
            EventQueue.invokeAndWait(new Runnable() {

                @Override
                public void run() {
                    uninstallAddOnsWithView(caller, addOns, updates, failedUninstallations);
                }
            });
        } catch (InvocationTargetException | InterruptedException e) {
            logger.error("Failed to uninstall add-ons:", e);
            return false;
        }
        return failedUninstallations.isEmpty();
    }
    final Window parent = getWindowParent(caller);
    final UninstallationProgressDialogue waitDialogue = new UninstallationProgressDialogue(parent, addOns);
    waitDialogue.addAddOnUninstallListener(new AddOnUninstallListener() {

        @Override
        public void uninstallingAddOn(AddOn addOn, boolean updating) {
            if (updating) {
                String message = MessageFormat.format(Constant.messages.getString("cfu.output.replacing") + "\n", addOn.getName(), Integer.valueOf(addOn.getFileVersion()));
                getView().getOutputPanel().append(message);
            }
        }

        @Override
        public void addOnUninstalled(AddOn addOn, boolean update, boolean uninstalled) {
            if (uninstalled) {
                if (!update && addonsDialog != null) {
                    addonsDialog.notifyAddOnUninstalled(addOn);
                }
                String message = MessageFormat.format(Constant.messages.getString("cfu.output.uninstalled") + "\n", addOn.getName(), Integer.valueOf(addOn.getFileVersion()));
                getView().getOutputPanel().append(message);
            } else {
                if (addonsDialog != null) {
                    addonsDialog.notifyAddOnFailedUninstallation(addOn);
                }
                String message;
                if (update) {
                    message = MessageFormat.format(Constant.messages.getString("cfu.output.replace.failed") + "\n", addOn.getName(), Integer.valueOf(addOn.getFileVersion()));
                } else {
                    message = MessageFormat.format(Constant.messages.getString("cfu.output.uninstall.failed") + "\n", addOn.getName(), Integer.valueOf(addOn.getFileVersion()));
                }
                getView().getOutputPanel().append(message);
            }
        }
    });
    SwingWorker<Void, UninstallationProgressEvent> a = new SwingWorker<Void, UninstallationProgressEvent>() {

        @Override
        protected void process(List<UninstallationProgressEvent> events) {
            waitDialogue.update(events);
        }

        @Override
        protected Void doInBackground() {
            UninstallationProgressHandler progressHandler = new UninstallationProgressHandler() {

                @Override
                protected void publishEvent(UninstallationProgressEvent event) {
                    publish(event);
                }
            };
            for (AddOn addOn : addOns) {
                if (!uninstall(addOn, updates, progressHandler)) {
                    failedUninstallations.add(addOn);
                }
            }
            if (!failedUninstallations.isEmpty()) {
                logger.warn("Not all add-ons were successfully uninstalled: " + failedUninstallations);
            }
            return null;
        }
    };
    waitDialogue.bind(a);
    a.execute();
    waitDialogue.setSynchronous(updates);
    waitDialogue.setVisible(true);
    return failedUninstallations.isEmpty();
}
Also used : Window(java.awt.Window) AddOn(org.zaproxy.zap.control.AddOn) UninstallationProgressHandler(org.zaproxy.zap.extension.autoupdate.UninstallationProgressDialogue.UninstallationProgressHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException) UninstallationProgressEvent(org.zaproxy.zap.extension.autoupdate.UninstallationProgressDialogue.UninstallationProgressEvent) AddOnUninstallListener(org.zaproxy.zap.extension.autoupdate.UninstallationProgressDialogue.AddOnUninstallListener) SwingWorker(javax.swing.SwingWorker) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

Window (java.awt.Window)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 SwingWorker (javax.swing.SwingWorker)1 AddOn (org.zaproxy.zap.control.AddOn)1 AddOnUninstallListener (org.zaproxy.zap.extension.autoupdate.UninstallationProgressDialogue.AddOnUninstallListener)1 UninstallationProgressEvent (org.zaproxy.zap.extension.autoupdate.UninstallationProgressDialogue.UninstallationProgressEvent)1 UninstallationProgressHandler (org.zaproxy.zap.extension.autoupdate.UninstallationProgressDialogue.UninstallationProgressHandler)1