001/* 002 * (C) Copyright 2006-2011 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 * bstefanescu 018 */ 019package org.nuxeo.connect.update.task.standalone.commands; 020 021import static java.nio.charset.StandardCharsets.UTF_8; 022 023import java.io.File; 024import java.io.IOException; 025import java.util.Map; 026 027import org.apache.commons.io.FileUtils; 028import org.nuxeo.common.utils.StringUtils; 029import org.nuxeo.connect.update.PackageException; 030 031/** 032 * Copy a file to the given target directory or file. If the target is a directory the file name is preserved. If the 033 * target file exists it will be replaced if overwrite is true otherwise the command validation fails. 034 * <p> 035 * If md5 is set then the copy command will be validated only if the target file has the same md5 as the one specified 036 * in the command. 037 * <p> 038 * The Copy command has as inverse either Delete either another Copy command. If the file was copied without overwriting 039 * then Delete is the inverse (with a md5 set to the one of the copied file). If the file was overwritten then the Copy 040 * command has an inverse another copy command with the md5 to the one of the copied file and the overwrite flag to 041 * true. The file to copy will be the backup of the overwritten file. 042 * 043 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 044 */ 045public class ParameterizedCopy extends Copy { 046 047 @SuppressWarnings("hiding") 048 public static final String ID = "pcopy"; 049 050 public ParameterizedCopy() { 051 super(ID); 052 } 053 054 public ParameterizedCopy(File file, File tofile, String md5, boolean overwrite) { 055 this(); 056 this.file = file; 057 this.tofile = tofile; 058 this.md5 = md5; 059 this.overwrite = overwrite; 060 } 061 062 @Override 063 protected String getContentToCopy(File fileToCopy, Map<String, String> prefs) throws PackageException { 064 try { 065 String content = FileUtils.readFileToString(fileToCopy, UTF_8); 066 return StringUtils.expandVars(content, prefs); 067 } catch (IOException e) { 068 throw new PackageException("Failed to run parameterized copy for: " + fileToCopy.getName(), e); 069 } 070 } 071 072}