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            // then re-build the war now that jar is deleted
054            ReloadService srv = Framework.getLocalService(ReloadService.class);
055            srv.runDeploymentPreprocessor();
056        } catch (PackageException | IOException e) {
057            // ignore uninstall -> this may break the entire chain. Usually
058            // uninstall is done only when rollbacking or uninstalling => force
059            // restart required
060            task.setRestartRequired(true);
061            throw new PackageException("Failed to undeploy bundle", e);
062        }
063        return res;
064    }
065
066    protected Command getUndeployCommand(File targetFile) {
067        return new Undeploy(targetFile);
068    }
069
070}