001/*
002 * (C) Copyright 2006-2007 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 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.runtime.deployment.preprocessor.install.commands;
023
024import java.io.BufferedOutputStream;
025import java.io.File;
026import java.io.FileInputStream;
027import java.io.FileNotFoundException;
028import java.io.FileOutputStream;
029import java.io.IOException;
030import java.io.InputStream;
031import java.io.OutputStream;
032import java.util.ArrayList;
033
034import org.apache.commons.io.IOUtils;
035import org.apache.commons.lang.StringUtils;
036import org.nuxeo.common.utils.FileNamePattern;
037import org.nuxeo.common.utils.FileUtils;
038import org.nuxeo.common.utils.Path;
039import org.nuxeo.runtime.deployment.preprocessor.install.Command;
040import org.nuxeo.runtime.deployment.preprocessor.install.CommandContext;
041
042import com.fasterxml.jackson.databind.ObjectMapper;
043import com.fasterxml.jackson.databind.node.ObjectNode;
044
045/**
046 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
047 */
048public class AppendCommand implements Command {
049
050    protected final Path src;
051
052    protected final Path dst;
053
054    protected final boolean addNewLine;
055
056    protected final FileNamePattern pattern;
057
058    public AppendCommand(Path src, Path dst, boolean addNewLine, FileNamePattern pattern) {
059        this.src = src;
060        this.dst = dst;
061        this.addNewLine = addNewLine;
062        this.pattern = pattern;
063    }
064
065    public AppendCommand(Path src, Path dst) {
066        this(src, dst, true, null);
067    }
068
069    @Override
070    public void exec(CommandContext ctx) throws IOException {
071        File baseDir = ctx.getBaseDir();
072        File srcFile = new File(baseDir, ctx.expandVars(src.toString()));
073        File dstFile = new File(baseDir, ctx.expandVars(dst.toString()));
074
075        if (pattern == null && !srcFile.exists()) {
076            throw new FileNotFoundException("Could not find the file " + srcFile.getAbsolutePath()
077                    + " to append to ' when deploying bundle '" + ctx.get("bundle") + "'.");
078        }
079
080        if (!dstFile.isFile()) {
081            try {
082                dstFile.createNewFile();
083            } catch (IOException e) {
084                throw new IOException(
085                        "Could not create '" + dstFile + "' when deploying bundle '" + ctx.get("bundle") + "'.", e);
086            }
087        }
088        if (pattern == null) {
089            append(srcFile, dstFile, addNewLine);
090        } else {
091            ArrayList<File> files = new ArrayList<File>();
092            FileUtils.collectFiles(srcFile, pattern, files);
093            for (File file : files) {
094                append(file, dstFile, false);
095            }
096        }
097    }
098
099    @Override
100    public String toString() {
101        return "append " + src.toString() + " > " + dst.toString();
102    }
103
104    @Override
105    public String toString(CommandContext ctx) {
106        return "append " + ctx.expandVars(src.toString()) + " > " + ctx.expandVars(dst.toString());
107    }
108
109    private void append(File srcFile, File dstFile, boolean appendNewLine) throws IOException {
110        String srcExt = FileUtils.getFileExtension(srcFile.getName());
111        String dstExt = FileUtils.getFileExtension(dstFile.getName());
112
113        if (StringUtils.equalsIgnoreCase(srcExt, dstExt) && "json".equalsIgnoreCase(srcExt)) {
114            ObjectMapper m = new ObjectMapper();
115            ObjectNode destNode = m.readValue(dstFile, ObjectNode.class);
116            ObjectNode srcNode = m.readValue(srcFile, ObjectNode.class);
117            destNode.setAll(srcNode);
118            m.writeValue(dstFile, destNode);
119        } else {
120            try (InputStream in = new FileInputStream(srcFile);
121                    OutputStream out = new BufferedOutputStream(new FileOutputStream(dstFile, true))) {
122                if (appendNewLine) {
123                    out.write(System.getProperty("line.separator").getBytes());
124                }
125                IOUtils.copy(in, out);
126            }
127        }
128    }
129}