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; 025 026import org.nuxeo.common.utils.FileUtils; 027import org.nuxeo.common.utils.Path; 028import org.nuxeo.common.utils.PathFilter; 029import org.nuxeo.runtime.deployment.preprocessor.install.Command; 030import org.nuxeo.runtime.deployment.preprocessor.install.CommandContext; 031 032/** 033 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 034 */ 035public class CopyCommand implements Command { 036 037 protected final Path src; 038 039 protected final Path dst; 040 041 protected final Path prefix; 042 043 protected final PathFilter filter; 044 045 /** 046 * Constructor for copy command. 047 * 048 * @param src the path relative to the root container. The path will be made absolute if not already 049 * @param dst the path relative to teh root container of the destination. If it is ending with a slash '/' the 050 * destination path is treated as a directory 051 */ 052 public CopyCommand(Path src, Path dst) { 053 this(src, dst, null); 054 } 055 056 public CopyCommand(Path src, Path dst, PathFilter filter) { 057 this(src, dst, new Path("/"), filter); 058 } 059 060 public CopyCommand(Path src, Path dst, Path prefix, PathFilter filter) { 061 this.src = src; 062 this.dst = dst; 063 this.prefix = prefix; 064 this.filter = filter; 065 } 066 067 @Override 068 public void exec(CommandContext ctx) throws IOException { 069 File baseDir = ctx.getBaseDir(); 070 File srcFile = new File(baseDir, ctx.expandVars(src.toString())); 071 File dstFile = new File(baseDir, ctx.expandVars(dst.toString())); 072 073 if (!srcFile.exists()) { 074 throw new FileNotFoundException("Could not find the file " + srcFile.getAbsolutePath() + " to copy."); 075 } 076 077 // canonicalize paths 078 dstFile = new File(dstFile.getCanonicalPath()); 079 srcFile = new File(srcFile.getCanonicalPath()); 080 081 if (!dstFile.exists()) { 082 if (dst.hasTrailingSeparator()) { 083 dstFile.mkdirs(); 084 } else { 085 // make sure parent dirs exists 086 File parent = dstFile.getParentFile(); 087 if (!parent.isDirectory()) { 088 parent.mkdirs(); 089 } 090 } 091 } 092 if (filter == null) { 093 if (srcFile.isDirectory() && src.hasTrailingSeparator()) { 094 FileUtils.copy(srcFile.listFiles(), dstFile); 095 } else { 096 FileUtils.copy(srcFile, dstFile); 097 } 098 } else { 099 if (srcFile.isDirectory() && src.hasTrailingSeparator()) { 100 FileUtils.copyTree(srcFile, dstFile, prefix, filter); 101 } else { 102 FileUtils.copy(srcFile, dstFile); 103 } 104 } 105 } 106 107 @Override 108 public String toString() { 109 return "copy " + src.toString() + " > " + dst.toString(); 110 } 111 112 @Override 113 public String toString(CommandContext ctx) { 114 return "copy " + ctx.expandVars(src.toString()) + " > " + ctx.expandVars(dst.toString()); 115 } 116 117}