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.nio.file.Files; 028 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 MoveCommand implements Command { 038 039 protected final Path src; 040 041 protected final Path dst; 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 MoveCommand(Path src, Path dst) { 053 this(src, dst, null); 054 } 055 056 public MoveCommand(Path src, Path dst, PathFilter filter) { 057 this.src = src; 058 this.dst = dst; 059 this.filter = filter; 060 } 061 062 @Override 063 public void exec(CommandContext ctx) throws IOException { 064 File baseDir = ctx.getBaseDir(); 065 File srcFile = new File(baseDir, ctx.expandVars(src.toString())); 066 File dstFile = new File(baseDir, ctx.expandVars(dst.toString())); 067 068 if (!srcFile.exists()) { 069 throw new FileNotFoundException("Could not find the file " + srcFile.getAbsolutePath() + " to move."); 070 } 071 072 if (filter != null && !filter.accept(new Path(dstFile.getAbsolutePath()))) { 073 return; 074 } 075 076 if (!dstFile.exists()) { 077 if (dst.hasTrailingSeparator()) { 078 dstFile.mkdirs(); 079 } else { 080 // make sure parent dirs exists 081 File parent = dstFile.getParentFile(); 082 if (!parent.isDirectory()) { 083 parent.mkdirs(); 084 } 085 } 086 } 087 088 if (dstFile.exists()) { 089 Files.delete(dstFile.toPath()); 090 } 091 092 Files.move(srcFile.toPath(), dstFile.toPath()); 093 } 094 095 @Override 096 public String toString() { 097 return "copy " + src.toString() + " > " + dst.toString(); 098 } 099 100 @Override 101 public String toString(CommandContext ctx) { 102 return "copy " + ctx.expandVars(src.toString()) + " > " + ctx.expandVars(dst.toString()); 103 } 104 105}