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