package test.stress; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.nio.channels.FileChannel; import java.util.Enumeration; import java.util.Properties; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Abstract class with methods useful for generating range of experiments. * * @author Jarek Szymczak * @version $Id$ */ public abstract class AbstractExperimentsGenerator { /** Class logger. */ private static final Log LOGGER = LogFactory.getLog(AbstractExperimentsGenerator.class); /** * Copy files with tasks limitations (at least it's written to do it, it can * copy other things - depending on variables). * * @param n * number of files to copy * @param sourceFolderPath * the source folder path * @param destinationFolderPath * the destination folder path * @param variables * variables from file "properties/variables.properties", this * variable contains i.e. templates with file names, etc. */ protected void copyFiles(long n, String sourceFolderPath, String destinationFolderPath, Properties variables) { File dir = new File(destinationFolderPath); if (dir.exists()) deleteDirectory(dir); try { dir.mkdirs(); } catch (SecurityException e) { LOGGER .error("Security violation while trying to create directories: " + dir.getAbsolutePath() + "!"); } try { for (long i = 0; i < n; i++) { String fileName = "/" + (i < 10 ? variables .getProperty("DIGIT_JOB_TEMPLATE") : variables .getProperty("NUMBER_JOB_TEMPLATE")) + Long.toString(i) + variables.getProperty("JOB_EXTENSION"); copyFile(sourceFolderPath + fileName, destinationFolderPath + fileName); } } catch (Exception e) { LOGGER.error("Error while copying experimnets files!"); } } /** * Copies file from source path to destination path - if destination file * exists it will be overwritten. * * @param sourceFilePath * the source file path * @param destFilePath * the dest file path * @throws IOException * Signals that an I/O exception has occurred. */ protected static void copyFile(String sourceFilePath, String destFilePath) throws IOException { File sourceFile = new File(sourceFilePath); File destFile = new File(destFilePath); if (!destFile.exists()) { try { destFile.createNewFile(); } catch (SecurityException e) { LOGGER.error("Security violation while trying to create file: " + destFile.getAbsolutePath() + "!"); } } FileChannel source = null; FileChannel destination = null; try { source = new FileInputStream(sourceFile).getChannel(); destination = new FileOutputStream(destFile).getChannel(); destination.transferFrom(source, 0, source.size()); } finally { if (source != null) source.close(); if (destination != null) destination.close(); } } /** * Deletes directory. * * @param path * the path * @return true, if successful */ protected boolean deleteDirectory(File path) { if (path.exists()) { File[] files = path.listFiles(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { deleteDirectory(files[i]); } else { try { files[i].delete(); } catch (SecurityException e) { LOGGER .error("Security violation while trying to delete file: " + files[i].getAbsolutePath() + "!"); } } } } return (path.delete()); } /** * Substitutes variables in input and returns output. * * @param input * the input with variables * @param variables * the variables * @return the output with replaced variables */ protected String substituteVariables(String input, Properties variables) { String output = input; String key; Enumeration keys = variables.keys(); while (keys.hasMoreElements()) { key = (String) keys.nextElement(); output = output.replaceAll(key, variables.getProperty(key)); } return output; } /** * Creates the file from template by substituting all the variables in * template. * * @param templatePath * the template path * @param outputPath * the output path * @param variables * the variables * @throws IOException * Signals that an I/O exception has occurred. */ protected void createFileFromTemplate(String templatePath, String outputPath, Properties variables) throws IOException { File template = new File(templatePath); File output = new File(outputPath); BufferedReader reader = new BufferedReader(new FileReader(template)); String line = ""; StringBuilder oldtext = new StringBuilder(); while ((line = reader.readLine()) != null) { oldtext.append(line); oldtext.append('\n'); } reader.close(); String newtext = substituteVariables(oldtext.toString(), variables); FileWriter writer = new FileWriter(output); writer.write(newtext); writer.close(); } }