001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.connect.update.task.live.commands;
020
021import java.io.File;
022import java.io.IOException;
023import java.util.Map;
024
025import org.nuxeo.connect.update.PackageException;
026import org.nuxeo.connect.update.task.Command;
027import org.nuxeo.connect.update.task.Task;
028import org.nuxeo.connect.update.task.update.Rollback;
029import org.nuxeo.connect.update.task.update.RollbackOptions;
030import org.nuxeo.runtime.api.Framework;
031import org.nuxeo.runtime.reload.ReloadService;
032
033/**
034 * @since 5.6
035 */
036public class RollbackAndUndeploy extends Rollback {
037
038    // needed for deserialization
039    public RollbackAndUndeploy() {
040        super();
041    }
042
043    public RollbackAndUndeploy(RollbackOptions opt) {
044        super(opt);
045    }
046
047    @Override
048    protected Command doRun(Task task, Map<String, String> prefs) throws PackageException {
049        Command res = null;
050        try {
051            res = super.doRun(task, prefs);
052
053            // run deployment preprocessor only if server use former hot reload (this task is performed by new reload)
054            boolean useCompatReload = Framework.isBooleanPropertyTrue(ReloadService.USE_COMPAT_HOT_RELOAD);
055            if (useCompatReload) {
056                // then re-build the war now that jar is deleted
057                ReloadService srv = Framework.getService(ReloadService.class);
058                srv.runDeploymentPreprocessor();
059            }
060        } catch (PackageException | IOException e) {
061            // ignore uninstall -> this may break the entire chain. Usually
062            // uninstall is done only when rollbacking or uninstalling => force
063            // restart required
064            task.setRestartRequired(true);
065            throw new PackageException("Failed to undeploy bundle", e);
066        }
067        return res;
068    }
069
070    @Override
071    protected Command getUndeployCommand(File targetFile) {
072        return new Undeploy(targetFile);
073    }
074
075}