001/*
002 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 */
017package org.nuxeo.connect.update.task.live.commands;
018
019import java.io.File;
020import java.util.Map;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.connect.update.PackageException;
025import org.nuxeo.connect.update.task.Command;
026import org.nuxeo.connect.update.task.Task;
027import org.nuxeo.connect.update.task.standalone.commands.UndeployPlaceholder;
028import org.nuxeo.runtime.api.Framework;
029import org.nuxeo.runtime.reload.ReloadService;
030import org.osgi.framework.BundleException;
031
032/**
033 * Undeploy a runtime bundle, or a directory containing runtime bundles.
034 * <p>
035 * The inverse of this command is Deploy.
036 *
037 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
038 */
039public class Undeploy extends UndeployPlaceholder {
040
041    private static final Log log = LogFactory.getLog(Undeploy.class);
042
043    public Undeploy() {
044        super();
045    }
046
047    public Undeploy(File file) {
048        super(file);
049    }
050
051    protected void undeployFile(File file, ReloadService service) throws PackageException {
052        String name = service.getOSGIBundleName(file);
053        if (name == null) {
054            // not an OSGI bundle => ignore
055            return;
056        }
057        try {
058            service.undeployBundle(file, true);
059        } catch (BundleException e) {
060            throw new PackageException("Failed to undeploy bundle " + file, e);
061        }
062    }
063
064    protected void undeployDirectory(File dir, ReloadService service) throws PackageException {
065        File[] files = dir.listFiles();
066        if (files != null) {
067            for (File fileInDir : files) {
068                undeployFile(fileInDir, service);
069            }
070        }
071    }
072
073    @Override
074    protected Command doRun(Task task, Map<String, String> prefs) throws PackageException {
075        if (!file.exists()) {
076            log.warn("Can't undeploy file " + file + ". File is missing.");
077            return null;
078        }
079        try {
080            ReloadService srv = Framework.getLocalService(ReloadService.class);
081            if (file.isDirectory()) {
082                undeployDirectory(file, srv);
083            } else {
084                undeployFile(file, srv);
085            }
086        } catch (PackageException e) {
087            // ignore uninstall -> this may break the entire chain. Usually
088            // uninstall is done only when rollbacking or uninstalling => force
089            // restart required
090            task.setRestartRequired(true);
091            throw new PackageException("Failed to undeploy bundle " + file, e);
092        }
093        return new Deploy(file);
094    }
095
096}