001/*
002 * (C) Copyright 2006-2007 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 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.runtime.deployment.preprocessor.install.commands;
021
022import java.io.File;
023import java.io.IOException;
024
025import org.nuxeo.common.utils.FileUtils;
026import org.nuxeo.common.utils.Path;
027import org.nuxeo.runtime.deployment.preprocessor.install.Command;
028import org.nuxeo.runtime.deployment.preprocessor.install.CommandContext;
029
030/**
031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
032 */
033public class DeleteCommand implements Command {
034
035    protected final Path path;
036
037    /**
038     * Constructor for delete command.
039     *
040     * @param path the path relative to the root container.
041     */
042    public DeleteCommand(Path path) {
043        this.path = path;
044    }
045
046    @Override
047    public void exec(CommandContext ctx) throws IOException {
048        File baseDir = ctx.getBaseDir();
049        File file = new File(baseDir, ctx.expandVars(path.toString()));
050
051        if (file.isFile()) {
052            file.delete();
053        } else if (file.isDirectory()) {
054            FileUtils.deleteTree(file);
055        }
056    }
057
058    @Override
059    public String toString() {
060        return "delete " + path.toString();
061    }
062
063    @Override
064    public String toString(CommandContext ctx) {
065        return "delete " + ctx.expandVars(path.toString());
066    }
067
068}