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;
018
019import java.io.File;
020import java.io.IOException;
021import java.util.List;
022import java.util.Map;
023
024import org.nuxeo.common.utils.FileUtils;
025import org.nuxeo.connect.update.LocalPackage;
026import org.nuxeo.connect.update.PackageException;
027import org.nuxeo.connect.update.PackageState;
028import org.nuxeo.connect.update.PackageUpdateService;
029import org.nuxeo.connect.update.task.live.commands.Flush;
030import org.nuxeo.connect.update.task.standalone.InstallTask;
031import org.nuxeo.runtime.api.Framework;
032import org.nuxeo.runtime.model.ComponentName;
033import org.nuxeo.runtime.model.impl.RegistrationInfoImpl;
034import org.nuxeo.runtime.reload.ReloadService;
035
036/**
037 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
038 */
039public class LiveInstallTask extends InstallTask {
040
041    public LiveInstallTask(PackageUpdateService pus) {
042        super(pus);
043    }
044
045    @Override
046    protected void doRun(Map<String, String> params) throws PackageException {
047        super.doRun(params);
048        // reload components declared in 'reload' file
049        reloadComponents(getPackage());
050    }
051
052    @Override
053    protected void taskDone() throws PackageException {
054        Framework.getService(ReloadService.class).reload();
055        if (isRestartRequired()) {
056            service.setPackageState(pkg, PackageState.INSTALLED);
057        } else {
058            service.setPackageState(pkg, PackageState.STARTED);
059        }
060    }
061
062    /**
063     * @deprecated since 5.6: this way of reloading components is smarter because the package installed can declare what
064     *             needs to be reloaded exactly, but this is too complicated to handle, and risky given potential
065     *             dependency issues => make components listen for the "flush" event instead, @see {@link ReloadService}
066     */
067    @Deprecated
068    protected static void reloadComponents(LocalPackage localPackage) throws PackageException {
069        File file = localPackage.getData().getEntry("reload");
070        if (file.isFile()) {
071            try {
072                List<String> lines = FileUtils.readLines(file);
073                for (String line : lines) {
074                    line = line.trim();
075                    if (line.startsWith("#") || line.length() == 0) {
076                        continue;
077                    }
078                    reloadComponent(line);
079                }
080            } catch (IOException e) {
081                throw new PackageException("Failed to read the 'reload' file", e);
082            }
083        }
084    }
085
086    /**
087     * @deprecated since 5.6: see {@link #reloadComponents(LocalPackage)}
088     */
089    @Deprecated
090    protected static void reloadComponent(String name) throws PackageException {
091        RegistrationInfoImpl ri = (RegistrationInfoImpl) Framework.getRuntime().getComponentManager().getRegistrationInfo(
092                new ComponentName(name));
093        if (ri != null) {
094            ri.reload();
095        }
096    }
097
098    @Override
099    protected void flush() throws PackageException {
100        Flush.flush();
101    }
102
103}