001/*
002 * (C) Copyright 2006-2017 Nuxeo (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.util.Arrays;
023import java.util.Map;
024import java.util.Objects;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.connect.update.PackageException;
029import org.nuxeo.connect.update.task.Command;
030import org.nuxeo.connect.update.task.Task;
031import org.nuxeo.connect.update.task.standalone.commands.UndeployPlaceholder;
032import org.nuxeo.runtime.api.Framework;
033import org.nuxeo.runtime.reload.ReloadContext;
034import org.nuxeo.runtime.reload.ReloadService;
035import org.osgi.framework.BundleException;
036
037/**
038 * Undeploy a runtime bundle, or a directory containing runtime bundles.
039 * <p>
040 * The inverse of this command is Deploy.
041 *
042 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
043 */
044public class Undeploy extends UndeployPlaceholder {
045
046    private static final Log log = LogFactory.getLog(Undeploy.class);
047
048    public Undeploy() {
049        super();
050    }
051
052    public Undeploy(File file) {
053        super(file);
054    }
055
056    /**
057     * @deprecated since 9.3, reload mechanism has changed, keep it for backward compatibility
058     */
059    @Deprecated
060    protected void undeployFile(File file, ReloadService service) throws PackageException {
061        String name = service.getOSGIBundleName(file);
062        if (name == null) {
063            // not an OSGI bundle => ignore
064            return;
065        }
066        try {
067            service.undeployBundle(name, true);
068        } catch (BundleException e) {
069            throw new PackageException("Failed to undeploy bundle " + file, e);
070        }
071    }
072
073    /**
074     * @deprecated since 9.3, reload mechanism has changed, keep it for backward compatibility
075     */
076    @Deprecated
077    protected void undeployDirectory(File dir, ReloadService service) throws PackageException {
078        File[] files = dir.listFiles();
079        if (files != null) {
080            for (File fileInDir : files) {
081                undeployFile(fileInDir, service);
082            }
083        }
084    }
085
086    @Override
087    protected Command doRun(Task task, Map<String, String> prefs) throws PackageException {
088        if (!file.exists()) {
089            log.warn("Can't undeploy file " + file + ". File is missing.");
090            return null;
091        }
092        boolean useCompatReload = Framework.isBooleanPropertyTrue(ReloadService.USE_COMPAT_HOT_RELOAD);
093        if (useCompatReload) {
094            doCompatRun(task);
095            return new Deploy(file);
096        }
097        try {
098            ReloadService srv = Framework.getService(ReloadService.class);
099            if (file.isDirectory()) {
100                _undeployDirectory(file, srv);
101            } else {
102                _undeployFile(file, srv);
103            }
104        } catch (BundleException e) {
105            // ignore uninstall -> this may break the entire chain. Usually
106            // uninstall is done only when rollbacking or uninstalling => force
107            // restart required
108            task.setRestartRequired(true);
109            throw new PackageException("Failed to undeploy bundle " + file, e);
110        }
111        return new Deploy(file);
112    }
113
114    /**
115     * @deprecated since 9.3, reload mechanism has changed, keep it for backward compatibility
116     */
117    @Deprecated
118    protected void doCompatRun(Task task) throws PackageException {
119        try {
120            ReloadService srv = Framework.getService(ReloadService.class);
121            if (file.isDirectory()) {
122                undeployDirectory(file, srv);
123            } else {
124                undeployFile(file, srv);
125            }
126        } catch (PackageException e) {
127            // ignore uninstall -> this may break the entire chain. Usually
128            // uninstall is done only when rollbacking or uninstalling => force
129            // restart required
130            task.setRestartRequired(true);
131            throw new PackageException("Failed to undeploy bundle " + file, e);
132        }
133    }
134
135    protected void _undeployFile(File file, ReloadService service) throws BundleException {
136        String name = service.getOSGIBundleName(file);
137        if (name == null) {
138            // not an OSGI bundle => ignore
139            return;
140        }
141        service.reloadBundles(new ReloadContext().undeploy(name));
142    }
143
144    protected void _undeployDirectory(File dir, ReloadService service) throws BundleException {
145        File[] files = dir.listFiles();
146        if (files != null) {
147            ReloadContext reloadContext = new ReloadContext();
148            Arrays.stream(files)
149                  .map(service::getOSGIBundleName)
150                  .filter(Objects::nonNull)
151                  .forEach(reloadContext::undeploy);
152            service.reloadBundles(reloadContext);
153        }
154    }
155
156}