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