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