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.commands;
018
019import java.io.IOException;
020import java.util.Map;
021
022import org.nuxeo.connect.update.PackageException;
023import org.nuxeo.connect.update.task.Command;
024import org.nuxeo.connect.update.task.Task;
025import org.nuxeo.connect.update.task.standalone.commands.FlushPlaceholder;
026import org.nuxeo.runtime.api.Framework;
027import org.nuxeo.runtime.reload.ReloadService;
028
029/**
030 * Flush all Nuxeo caches by calling {@link ReloadService#flush()}
031 * <p>
032 * The inverse of this command is itself.
033 *
034 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
035 */
036public class Flush extends FlushPlaceholder {
037
038    /**
039     * Calls the {@link ReloadService#flush()} method
040     *
041     * @since 5.6
042     * @throws PackageException
043     */
044    public static void flush() throws PackageException {
045        try {
046            Framework.getRuntime().reloadProperties();
047            ReloadService deployer = Framework.getLocalService(ReloadService.class);
048            deployer.flush();
049        } catch (IOException e) {
050            throw new PackageException("Failed to reload repository", e);
051        }
052    }
053
054    @Override
055    protected Command doRun(Task task, Map<String, String> prefs) throws PackageException {
056        flush();
057        return new Flush();
058    }
059
060}