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.FileNotFoundException;
024import java.io.IOException;
025import java.util.ArrayList;
026
027import org.nuxeo.common.utils.FileNamePattern;
028import org.nuxeo.common.utils.FileUtils;
029import org.nuxeo.common.utils.Path;
030import org.nuxeo.runtime.deployment.preprocessor.install.Command;
031import org.nuxeo.runtime.deployment.preprocessor.install.CommandContext;
032
033/**
034 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
035 */
036public class AppendCommand implements Command {
037
038    protected final Path src;
039
040    protected final Path dst;
041
042    protected final boolean addNewLine;
043
044    protected final FileNamePattern pattern;
045
046    public AppendCommand(Path src, Path dst, boolean addNewLine, FileNamePattern pattern) {
047        this.src = src;
048        this.dst = dst;
049        this.addNewLine = addNewLine;
050        this.pattern = pattern;
051    }
052
053    public AppendCommand(Path src, Path dst) {
054        this(src, dst, true, null);
055    }
056
057    @Override
058    public void exec(CommandContext ctx) throws IOException {
059        File baseDir = ctx.getBaseDir();
060        File srcFile = new File(baseDir, ctx.expandVars(src.toString()));
061        File dstFile = new File(baseDir, ctx.expandVars(dst.toString()));
062
063        if (pattern == null && !srcFile.exists()) {
064            throw new FileNotFoundException("Could not find the file " + srcFile.getAbsolutePath() + " to append.");
065        }
066
067        if (!dstFile.isFile()) {
068            try {
069                dstFile.createNewFile();
070            } catch (IOException e) {
071                throw new IOException("Could not create " + dstFile, e);
072            }
073        }
074        if (pattern == null) {
075            FileUtils.append(srcFile, dstFile, addNewLine);
076        } else {
077            ArrayList<File> files = new ArrayList<File>();
078            FileUtils.collectFiles(srcFile, pattern, files);
079            for (File file : files) {
080                FileUtils.append(file, dstFile);
081            }
082        }
083    }
084
085    @Override
086    public String toString() {
087        return "append " + src.toString() + " > " + dst.toString();
088    }
089
090    @Override
091    public String toString(CommandContext ctx) {
092        return "append " + ctx.expandVars(src.toString()) + " > " + ctx.expandVars(dst.toString());
093    }
094
095}