1 package org.whatsitcalled.webflange.file;
2
3 import java.awt.image.BufferedImage;
4 import java.io.File;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.Writer;
9
10 import javax.imageio.ImageIO;
11
12 import org.apache.commons.io.FileUtils;
13 import org.apache.log4j.Logger;
14 import org.apache.wicket.util.file.Folder;
15 import org.apache.wicket.util.io.IOUtils;
16 import org.whatsitcalled.webflange.model.Chart;
17 import org.whatsitcalled.webflange.model.LoadTest;
18 import org.whatsitcalled.webflange.model.Script;
19
20 public class FileManager {
21 private String baseDir = System.getProperty("user.home") + "/webFlange/filestore";
22 private String archiveDir = "/tmp";
23
24 Folder uploadFolder;
25 Folder scriptFolder;
26 Folder propertyFolder;
27 Folder dataFolder;
28
29
30 private Logger LOGGER = Logger.getLogger(FileManager.class);
31
32 public void init() {
33
34 if (uploadFolder == null) setUploadFolder(new Folder(baseDir + "/uploads"));
35 uploadFolder.mkdirs();
36 if (scriptFolder == null) setScriptFolder(new Folder(baseDir + "/scripts"));
37 scriptFolder.mkdirs();
38 if (propertyFolder == null) setPropertyFolder(new Folder(baseDir + "/properties"));
39 propertyFolder.mkdirs();
40 if (dataFolder == null) setDataFolder(new Folder(baseDir + "/data"));
41 dataFolder.mkdirs();
42
43
44 }
45
46
47
48
49
50
51
52 private void saveFile(Folder folder, Object key, InputStream stream) throws FileManagerException{
53
54
55
56
57 File file = new File(folder, key.toString());
58
59
60 LOGGER.debug("Writing file: " + file.getAbsolutePath());
61 Writer writer = null;
62 try {
63 writer = new FileWriter(file);
64 IOUtils.copy(stream, writer);
65 } catch (IOException e) {
66 throw new FileManagerException("Unable to write to file: " + file.getAbsolutePath(),e);
67 } finally {
68 try {
69 writer.close();
70 } catch (IOException e) {
71 throw new FileManagerException("Unable to write to file: " + file.getAbsolutePath(),e);
72 }
73 }
74 }
75
76 public void saveScriptFile(Script script, InputStream stream) throws FileManagerException{
77 String name = getScriptFileName(script);
78 saveFile(getScriptFolder(), name, stream);
79 }
80
81 public void saveUploadFile(Object key, InputStream stream) throws FileManagerException{
82 saveFile(getUploadFolder(), key, stream);
83 }
84
85 public void savePropertyFile(LoadTest test, InputStream stream) throws FileManagerException{
86 String name = getPropertyFileName(test);
87 saveFile(getPropertyFolder(), name, stream);
88 }
89
90
91
92
93
94
95
96 private void deleteFile(Folder folder, Object key) throws FileManagerException {
97 File file = new File(folder, key.toString());
98
99
100 if (file.exists()) {
101 LOGGER.debug("Deleting file: " + file.getAbsolutePath());
102 try {
103 FileUtils.forceDelete(file);
104 } catch (IOException e1) {
105 throw new FileManagerException("Unable to delete file: " + file.getAbsolutePath(), e1);
106 }
107 }
108
109 }
110
111 public void deleteUploadFile(Object key) throws FileManagerException {
112 deleteFile(getUploadFolder(), key);
113 }
114
115 public void deleteScriptFile(Script script) throws FileManagerException {
116 deleteFile(getScriptFolder(), getScriptFileName(script));
117 }
118
119 public void deletePropertyFile(LoadTest test) throws FileManagerException {
120 deleteFile(getPropertyFolder(), getPropertyFileName(test));
121 }
122
123 public String getScriptFileContent(Script script) {
124 File file = new File(getScriptFolder(), getScriptFileName(script));
125 String content = new String();
126 try {
127 content = FileUtils.readFileToString(file);
128 } catch (IOException e) {
129 LOGGER.error("Unable to open file:" + file.getAbsolutePath(), e);
130 }
131
132 return content;
133 }
134
135 public File getPropertyFile(LoadTest test) {
136 return new File(getPropertyFolder(), getPropertyFileName(test));
137 }
138
139 public String getScriptFileName(Script script) {
140
141 return script.getId().toString() + ".py";
142 }
143
144 public String getPropertyFileName(LoadTest test) {
145
146 return test.getId().toString() + ".properties";
147
148 }
149
150 public File getScriptFile(Script script) {
151 return new File(getScriptFolder() + "/" + getScriptFileName(script));
152 }
153
154 public String getBaseDir() {
155 return baseDir;
156 }
157
158 public void setBaseDir(String dir) {
159 this.baseDir = dir;
160 }
161
162 public Folder getUploadFolder() {
163 return uploadFolder;
164 }
165
166 public void setUploadFolder(Folder uploadFolder) {
167 this.uploadFolder = uploadFolder;
168 }
169
170 public Folder getPropertyFolder() {
171 return propertyFolder;
172 }
173
174 public void setPropertyFolder(Folder propertyFolder) {
175 this.propertyFolder = propertyFolder;
176 }
177
178 public Folder getScriptFolder() {
179 return scriptFolder;
180 }
181
182 public void setScriptFolder(Folder scriptFolder) {
183 this.scriptFolder = scriptFolder;
184 }
185
186 public Folder getDataFolder() {
187 return dataFolder;
188 }
189
190 public void setDataFolder(Folder dataFolder) {
191 this.dataFolder = dataFolder;
192 }
193
194
195
196
197 public void deleteChartFile(Chart chart) throws FileManagerException{
198 deleteFile(getDataFolder(), chart.getFileName());
199 }
200
201
202
203
204 public void saveChartFile(Chart chart, String type) throws FileManagerException{
205 try {
206 ImageIO.write(chart.getChartImage(), type, new File(getDataFolder(), chart.getFileName()));
207 } catch (IOException e) {
208 throw new FileManagerException("Unable to write: " + chart.getFileName(),e);
209 }
210
211 }
212
213
214
215
216 public void loadChartFile(Chart chart) throws FileManagerException{
217 BufferedImage bi;
218 try {
219 LOGGER.debug("Loading chart file for chart: " + chart.getId());
220 bi = ImageIO.read(new File(getDataFolder(), chart.getFileName()));
221 chart.setChartImage(bi);
222 } catch (IOException e) {
223 throw new FileManagerException("Unable to read: " + chart.getFileName(),e);
224 }
225 }
226
227 public String getArchiveDir() {
228 return archiveDir;
229 }
230
231 public void setArchiveDir(String archiveDir) {
232 this.archiveDir = archiveDir;
233 }
234
235 }