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 =&gt; make components listen for the "flush" event instead, @see
073     *             {@link ReloadService}
074     */
075    @Deprecated
076    protected static void reloadComponents(LocalPackage localPackage) throws PackageException {
077        File file = localPackage.getData().getEntry("reload");
078        if (file.isFile()) {
079            try {
080                List<String> lines = FileUtils.readLines(file);
081                for (String line : lines) {
082                    line = line.trim();
083                    if (line.startsWith("#") || line.length() == 0) {
084                        continue;
085                    }
086                    reloadComponent(line);
087                }
088            } catch (IOException e) {
089                throw new PackageException("Failed to read the 'reload' file", e);
090            }
091        }
092    }
093
094    /**
095     * @deprecated since 5.6: see {@link #reloadComponents(LocalPackage)}
096     */
097    @Deprecated
098    protected static void reloadComponent(String name) throws PackageException {
099        RegistrationInfoImpl ri = (RegistrationInfoImpl) Framework.getRuntime().getComponentManager().getRegistrationInfo(
100                new ComponentName(name));
101        if (ri != null) {
102            ri.reload();
103        }
104    }
105
106    @Override
107    protected void flush() throws PackageException {
108        Flush.flush();
109    }
110
111}