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