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