001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (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.io.IOException;
021import java.util.Map;
022import java.util.jar.JarFile;
023import java.util.jar.Manifest;
024
025import org.nuxeo.connect.update.PackageException;
026import org.nuxeo.connect.update.PackageUpdateComponent;
027import org.nuxeo.connect.update.task.Command;
028import org.nuxeo.connect.update.task.Task;
029import org.nuxeo.connect.update.task.standalone.commands.UninstallPlaceholder;
030import org.osgi.framework.Bundle;
031import org.osgi.framework.BundleContext;
032import org.osgi.framework.BundleException;
033
034/**
035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
036 * @deprecated since 5.6, use {@link Undeploy} instead
037 */
038@Deprecated
039public class Uninstall extends UninstallPlaceholder {
040
041    public Uninstall() {
042        super();
043    }
044
045    public Uninstall(File file) {
046        super(file);
047    }
048
049    @Override
050    protected Command doRun(Task task, Map<String, String> prefs) throws PackageException {
051        BundleContext ctx = PackageUpdateComponent.getContext().getBundle().getBundleContext();
052        JarFile jar = null;
053        try {
054            jar = new JarFile(file);
055            Manifest mf = jar.getManifest();
056            String name = mf.getMainAttributes().getValue("Bundle-SymbolicName");
057            if (name != null) { // ignore errors
058                for (Bundle bundle : ctx.getBundles()) {
059                    if (name.equals(bundle.getSymbolicName())) {
060                        try {
061                            if (bundle.getState() == Bundle.ACTIVE) {
062                                bundle.uninstall();
063                            }
064                        } catch (BundleException e) {
065                            // ignore uninstall -> this may break the entire
066                            // chain. Usually uninstall is done only when
067                            // rollbacking or uninstalling - force restart
068                            // required
069                            task.setRestartRequired(true);
070                        }
071                        break;
072                    }
073                }
074            }
075        } catch (IOException e) {
076            throw new PackageException("Failed to uninstall bundle: " + file.getName(), e);
077        } finally {
078            if (jar != null) {
079                try {
080                    jar.close();
081                } catch (IOException e) {
082                    // nothing to do
083                }
084            }
085        }
086        return new Install(file);
087    }
088
089}