001/* 002 * (C) Copyright 2006-2015 Nuxeo SA (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-2.1.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 * tdelprat, jcarsique 017 * 018 */ 019 020package org.nuxeo.ecm.platform.commandline.executor.service.executors; 021 022import java.util.ArrayList; 023import java.util.HashMap; 024import java.util.List; 025import java.util.regex.Pattern; 026 027import org.apache.commons.lang3.SystemUtils; 028 029import org.nuxeo.ecm.platform.commandline.executor.api.CmdParameters; 030import org.nuxeo.ecm.platform.commandline.executor.api.CmdParameters.CmdParameter; 031import org.nuxeo.ecm.platform.commandline.executor.api.CommandLineExecutorService; 032import org.nuxeo.ecm.platform.commandline.executor.service.CommandLineDescriptor; 033import org.nuxeo.runtime.api.Framework; 034 035/** 036 * Base class for {@link Executor}. 037 * 038 * @author tiry 039 */ 040public abstract class AbstractExecutor implements Executor { 041 042 /** 043 * @deprecated since 5.7. See {@link CommandLineExecutorService#checkParameter(String)}. 044 */ 045 @Deprecated 046 public static final Pattern VALID_PARAMETER_PATTERN = Pattern.compile("[\\p{L}_0-9-.%:=/\\\\ ]+"); 047 048 /** 049 * @deprecated Since 7.4. Use {@link SystemUtils#IS_OS_WINDOWS} 050 */ 051 @Deprecated 052 public static boolean isWindows() { 053 return SystemUtils.IS_OS_WINDOWS; 054 } 055 056 /** 057 * Returns parameters as a String after having replaced parameterized values inside. 058 * 059 * @param cmdDesc CommandLineDescriptor containing parameters 060 * @param params parameterized values 061 * @return Parameters as a String 062 */ 063 public static String getParametersString(CommandLineDescriptor cmdDesc, CmdParameters params) { 064 String paramString = cmdDesc.getParametersString(); 065 paramString = replaceParams(params, paramString); 066 return paramString; 067 } 068 069 /** 070 * Returns parameters as a String array after having replaced parameterized values inside. 071 * 072 * @param cmdDesc CommandLineDescriptor containing parameters 073 * @param params parameterized values 074 * @return Parameters as a String array 075 * @since 5.5 076 */ 077 public static String[] getParametersArray(CommandLineDescriptor cmdDesc, CmdParameters params) { 078 List<String> res = new ArrayList<>(); 079 String[] paramsArray = cmdDesc.getParametersString().split(" "); 080 for (String paramString : paramsArray) { 081 res.add(replaceParams(params, paramString)); 082 } 083 return res.toArray(new String[] {}); 084 } 085 086 private static String replaceParams(CmdParameters params, String paramString) { 087 CommandLineExecutorService commandLineExecutorService = Framework.getLocalService(CommandLineExecutorService.class); 088 HashMap<String, CmdParameter> paramsValues = params.getCmdParameters(); 089 for (String pname : paramsValues.keySet()) { 090 String param = "#{" + pname + "}"; 091 if (paramString.contains(param)) { 092 CmdParameters.CmdParameter cmdParameter = paramsValues.get(pname); 093 String value = cmdParameter.getValue(); 094 commandLineExecutorService.checkParameter(value); 095 if (cmdParameter.isQuote()) { 096 paramString = paramString.replace("#{" + pname + "}", String.format("\"%s\"", value)); 097 } else { 098 paramString = paramString.replace("#{" + pname + "}", String.format("%s", value)); 099 } 100 } 101 } 102 return paramString; 103 } 104}