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