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.File; 025import java.io.FileNotFoundException; 026import java.io.IOException; 027import java.util.ArrayList; 028 029import com.fasterxml.jackson.databind.ObjectMapper; 030import com.fasterxml.jackson.databind.node.ObjectNode; 031import org.apache.commons.lang.StringUtils; 032import org.nuxeo.common.utils.FileNamePattern; 033import org.nuxeo.common.utils.FileUtils; 034import org.nuxeo.common.utils.Path; 035import org.nuxeo.runtime.deployment.preprocessor.install.Command; 036import org.nuxeo.runtime.deployment.preprocessor.install.CommandContext; 037 038/** 039 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 040 */ 041public class AppendCommand implements Command { 042 043 protected final Path src; 044 045 protected final Path dst; 046 047 protected final boolean addNewLine; 048 049 protected final FileNamePattern pattern; 050 051 public AppendCommand(Path src, Path dst, boolean addNewLine, FileNamePattern pattern) { 052 this.src = src; 053 this.dst = dst; 054 this.addNewLine = addNewLine; 055 this.pattern = pattern; 056 } 057 058 public AppendCommand(Path src, Path dst) { 059 this(src, dst, true, null); 060 } 061 062 @Override 063 public void exec(CommandContext ctx) throws IOException { 064 File baseDir = ctx.getBaseDir(); 065 File srcFile = new File(baseDir, ctx.expandVars(src.toString())); 066 File dstFile = new File(baseDir, ctx.expandVars(dst.toString())); 067 068 if (pattern == null && !srcFile.exists()) { 069 throw new FileNotFoundException("Could not find the file " + srcFile.getAbsolutePath() + " to append."); 070 } 071 072 if (!dstFile.isFile()) { 073 try { 074 dstFile.createNewFile(); 075 } catch (IOException e) { 076 throw new IOException("Could not create " + dstFile, e); 077 } 078 } 079 if (pattern == null) { 080 append(srcFile, dstFile, addNewLine); 081 } else { 082 ArrayList<File> files = new ArrayList<File>(); 083 FileUtils.collectFiles(srcFile, pattern, files); 084 for (File file : files) { 085 append(file, dstFile, false); 086 } 087 } 088 } 089 090 @Override 091 public String toString() { 092 return "append " + src.toString() + " > " + dst.toString(); 093 } 094 095 @Override 096 public String toString(CommandContext ctx) { 097 return "append " + ctx.expandVars(src.toString()) + " > " + ctx.expandVars(dst.toString()); 098 } 099 100 private void append(File srcFile, File dstFile, boolean appendNewLine) throws IOException { 101 String srcExt = FileUtils.getFileExtension(srcFile.getName()); 102 String dstExt = FileUtils.getFileExtension(dstFile.getName()); 103 104 if (StringUtils.equalsIgnoreCase(srcExt, dstExt) && "json".equalsIgnoreCase(srcExt)) { 105 ObjectMapper m = new ObjectMapper(); 106 ObjectNode destNode = m.readValue(dstFile, ObjectNode.class); 107 ObjectNode srcNode = m.readValue(srcFile, ObjectNode.class); 108 destNode.setAll(srcNode); 109 m.writeValue(dstFile, destNode); 110 } else { 111 FileUtils.append(srcFile, dstFile, appendNewLine); 112 } 113 } 114}