source: xssim/src/test/stress/AbstractExperimentsGenerator.java @ 104

Revision 104, 5.2 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package test.stress;
2
3import java.io.BufferedReader;
4import java.io.File;
5import java.io.FileInputStream;
6import java.io.FileOutputStream;
7import java.io.FileReader;
8import java.io.FileWriter;
9import java.io.IOException;
10import java.nio.channels.FileChannel;
11import java.util.Enumeration;
12import java.util.Properties;
13
14import org.apache.commons.logging.Log;
15import org.apache.commons.logging.LogFactory;
16
17/**
18 * Abstract class with methods useful for generating range of experiments.
19 *
20 * @author Jarek Szymczak
21 * @version $Id$
22 */
23public abstract class AbstractExperimentsGenerator {
24
25        /** Class logger. */
26        private static final Log LOGGER =
27                        LogFactory.getLog(AbstractExperimentsGenerator.class);
28
29        /**
30         * Copy files with tasks limitations (at least it's written to do it, it can
31         * copy other things - depending on variables).
32         *
33         * @param n
34         *            number of files to copy
35         * @param sourceFolderPath
36         *            the source folder path
37         * @param destinationFolderPath
38         *            the destination folder path
39         * @param variables
40         *            variables from file "properties/variables.properties", this
41         *            variable contains i.e. templates with file names, etc.
42         */
43        protected void copyFiles(long n, String sourceFolderPath,
44                        String destinationFolderPath, Properties variables) {
45                File dir = new File(destinationFolderPath);
46                if (dir.exists())
47                        deleteDirectory(dir);
48                try {
49                        dir.mkdirs();
50                } catch (SecurityException e) {
51                        LOGGER
52                                        .error("Security violation while trying to create directories: "
53                                                        + dir.getAbsolutePath() + "!");
54                }
55                try {
56                        for (long i = 0; i < n; i++) {
57                                String fileName =
58                                                "/"
59                                                                + (i < 10 ? variables
60                                                                                .getProperty("DIGIT_JOB_TEMPLATE")
61                                                                                : variables
62                                                                                                .getProperty("NUMBER_JOB_TEMPLATE"))
63                                                                + Long.toString(i)
64                                                                + variables.getProperty("JOB_EXTENSION");
65                                copyFile(sourceFolderPath + fileName, destinationFolderPath
66                                                + fileName);
67                        }
68                } catch (Exception e) {
69                        LOGGER.error("Error while copying experimnets files!");
70                }
71
72        }
73
74        /**
75         * Copies file from source path to destination path - if destination file
76         * exists it will be overwritten.
77         *
78         * @param sourceFilePath
79         *            the source file path
80         * @param destFilePath
81         *            the dest file path
82         * @throws IOException
83         *             Signals that an I/O exception has occurred.
84         */
85        protected static void copyFile(String sourceFilePath, String destFilePath)
86                        throws IOException {
87
88                File sourceFile = new File(sourceFilePath);
89                File destFile = new File(destFilePath);
90
91                if (!destFile.exists()) {
92                        try {
93                                destFile.createNewFile();
94                        } catch (SecurityException e) {
95                                LOGGER.error("Security violation while trying to create file: "
96                                                + destFile.getAbsolutePath() + "!");
97                        }
98                }
99
100                FileChannel source = null;
101                FileChannel destination = null;
102                try {
103                        source = new FileInputStream(sourceFile).getChannel();
104                        destination = new FileOutputStream(destFile).getChannel();
105                        destination.transferFrom(source, 0, source.size());
106                } finally {
107                        if (source != null)
108                                source.close();
109                        if (destination != null)
110                                destination.close();
111                }
112        }
113
114        /**
115         * Deletes directory.
116         *
117         * @param path
118         *            the path
119         * @return true, if successful
120         */
121        protected boolean deleteDirectory(File path) {
122                if (path.exists()) {
123                        File[] files = path.listFiles();
124                        for (int i = 0; i < files.length; i++) {
125                                if (files[i].isDirectory()) {
126                                        deleteDirectory(files[i]);
127                                } else {
128                                        try {
129                                                files[i].delete();
130                                        } catch (SecurityException e) {
131                                                LOGGER
132                                                                .error("Security violation while trying to delete file: "
133                                                                                + files[i].getAbsolutePath() + "!");
134                                        }
135                                }
136                        }
137                }
138                return (path.delete());
139        }
140
141        /**
142         * Substitutes variables in input and returns output.
143         *
144         * @param input
145         *            the input with variables
146         * @param variables
147         *            the variables
148         * @return the output with replaced variables
149         */
150        protected String substituteVariables(String input, Properties variables) {
151                String output = input;
152                String key;
153                Enumeration<Object> keys = variables.keys();
154                while (keys.hasMoreElements()) {
155                        key = (String) keys.nextElement();
156                        output = output.replaceAll(key, variables.getProperty(key));
157                }
158                return output;
159        }
160
161        /**
162         * Creates the file from template by substituting all the variables in
163         * template.
164         *
165         * @param templatePath
166         *            the template path
167         * @param outputPath
168         *            the output path
169         * @param variables
170         *            the variables
171         * @throws IOException
172         *             Signals that an I/O exception has occurred.
173         */
174        protected void createFileFromTemplate(String templatePath,
175                        String outputPath, Properties variables) throws IOException {
176                File template = new File(templatePath);
177                File output = new File(outputPath);
178
179                BufferedReader reader = new BufferedReader(new FileReader(template));
180                String line = "";
181                StringBuilder oldtext = new StringBuilder();
182                while ((line = reader.readLine()) != null) {
183                        oldtext.append(line);
184                        oldtext.append('\n');
185                }
186
187                reader.close();
188
189                String newtext = substituteVariables(oldtext.toString(), variables);
190
191                FileWriter writer = new FileWriter(output);
192                writer.write(newtext);
193                writer.close();
194        }
195}
Note: See TracBrowser for help on using the repository browser.