1 /***
2 * IOManager.java
3 *
4 * Project: Dependency Tool
5 *
6 * WHEN WHO WHAT
7 * 06.06.2003 pko initial public release
8 * 01.11.2002 pko creation
9 *
10 * Copyright 2003 ELCA Informatique SA
11 * Av. de la Harpe 22-24, 1000 Lausanne 13, Switzerland
12 * www.elca.ch
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public License
16 * as published by the Free Software Foundation; either version 2.1 of
17 * the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 * USA
28 */
29
30 package ch.elca.dependency.util;
31
32 import java.io.File;
33 import java.io.IOException;
34 import java.net.URL;
35
36 import org.apache.log4j.Logger;
37
38 /***
39 * <code>IOManager</code> is used as manager of all operations that
40 * include the world outside the jvm Dependency Tool runs within.
41 *
42 * @tbd improve io-management, think about a user starting dpt from a
43 * cd and analyzing a project on a read-only medium. ask user to
44 * specify output and temp directories.
45 * @tbd improve finding dot.exe: if dpt-directory is placed in a dir
46 * named with a space, the overview layout doesn't work.
47 *
48 * @author Pawel Kowalski
49 * @version 1.0-beta
50 */
51 public class IOManager {
52
53 /***
54 * Log4j Logger.
55 */
56 private static final Logger LOG = Logger.getLogger(IOManager.class);
57
58 /***
59 * Basic name of the DPT output Directory.
60 */
61 private static final String DPT_OUTPUT_DIR = "dptOutput";
62
63 /***
64 * Basic name of the DPT temporary output Directory.
65 */
66 private static final String DPT_TEMP_DIR = "dptTemp";
67
68 /***
69 * A Separator to be used to resolve directroy conflicts
70 */
71 private static final String SEPARATOR = "_";
72
73 /***
74 * Relative path to the dot.exe graph layouter
75 */
76 private static final String RELATIVE_DOT_EXE_LOCATION = "dot/bin/";
77
78 /***
79 * Name of the dot.exe graph layouter
80 */
81 private static final String DOT_EXE_NAME = "dot.exe";
82
83 /***
84 * The directory used to write files into
85 */
86 private static File s_outDir = null;
87
88 /***
89 * The directory used to write temporary files into
90 */
91 private static File s_tempDir = null;
92
93 /***
94 * The File Separator used on this System.
95 */
96 private static final String fileSeparator = System.getProperty("file.separator");
97
98 /***
99 * Get an empty File for writing reports into. Class
100 * ch.elca.dependency.report.Report uses this method to get a File
101 * for writing its reports.
102 *
103 * @param filename a <code>String</code> value
104 * @return a <code>File</code> value
105 * @exception IOException if an error occurs
106 * @exception SecurityException if an error occurs
107 */
108 public static File getNewReportFile(String filename)
109 throws IOException, SecurityException{
110
111 File s_outDir = getOutputDir();
112 File reportFile = new File(s_outDir.getPath(), filename);
113
114 if (reportFile.exists()) {
115 reportFile.delete();
116 }
117
118 reportFile.createNewFile();
119 return reportFile;
120 }
121
122 /***
123 * Get the installation path of the Dependency Tool.
124 *
125 * @return a <code>String</code> value
126 */
127 public static String getToolInstallationPath() {
128
129 ClassLoader classLoader = ClassLoader.getSystemClassLoader();
130
131 // create path to this class
132 //
133 String thisClassLocation = IOManager.class.getName();
134 thisClassLocation = thisClassLocation.replace('.', '/');
135 thisClassLocation = thisClassLocation.concat(".class");
136
137 // get the url of this class
138 //
139 URL url = classLoader.getSystemResource(thisClassLocation);
140
141 // transform to path of our installation
142 //
143 String path = url.getPath();
144 int index = path.indexOf("jar");
145
146 // dpt was in a packed jar file
147 //
148 if (index != -1) {
149 path = path.substring(0, index);
150 index = path.lastIndexOf('/');
151 path = path.substring(0, index + 1);
152 index = path.indexOf(":");
153 path = path.substring(index + 2, path.length());
154 }
155
156 // dpt was unpacked
157 //
158 else {
159 path = path.substring(1, path.length());
160 index = path.lastIndexOf(thisClassLocation);
161 path = path.substring(0, index);
162 }
163
164 path = path.replaceAll("%20", " ");
165 return path;
166 }
167
168 /***
169 * Get the absolute path to the dot.exe graph layouter.
170 *
171 * @return a <code>String</code> value
172 */
173 public static String getDotExePath() {
174 return getToolInstallationPath().concat(RELATIVE_DOT_EXE_LOCATION);
175 }
176
177 /***
178 * Get name of the dot.exe graph layouter
179 *
180 * @return a <code>String</code> value
181 */
182 public static String getDotExeName() {
183 return DOT_EXE_NAME;
184 }
185
186 /***
187 * Get the complete path and the name of the dot.exe layouter
188 *
189 * @return a <code>String</code> value
190 */
191 public static String getDotExe() {
192 String pathAndName = IOManager.getDotExePath() + IOManager.getDotExeName();
193 if (!(new File(pathAndName)).isFile()) {
194 LOG.error("Could not find dot.exe for layouting");
195 return "";
196 }
197 LOG.debug("Found dot.exe: " + pathAndName);
198 return "\"" + pathAndName + "\"";
199 }
200
201 /***
202 * Get the path to the output directory. The Dependency Tool uses
203 * this dir to write reports and to save graphs.
204 *
205 * @return a <code>String</code> value
206 * @exception SecurityException if an error occurs
207 */
208 public static String getOutputPath() throws SecurityException {
209 return getOutputDir().getPath() + fileSeparator ;
210 }
211
212 /***
213 * Set the output directory Dependency Tool will use to write
214 * files into. This directory will be created in function of the
215 * root file that has been specified for analysis.
216 *
217 * @param analyseRoot a <code>String</code> value
218 */
219 public static synchronized void setOutputRoot(String analyseRoot) {
220 File rootFile = new File(analyseRoot);
221 String effOutputDir = "";
222
223 effOutputDir = rootFile.getAbsolutePath();
224 effOutputDir = effOutputDir.substring(0, effOutputDir.lastIndexOf(fileSeparator));
225 s_outDir = new File(effOutputDir = (effOutputDir + fileSeparator + DPT_OUTPUT_DIR));
226
227 if (!s_outDir.exists()) {
228 s_outDir.mkdirs();
229 }
230 }
231
232 /***
233 * Get the the output directory. The Dependency Tool uses
234 * this dir to write reports and to save graphs.
235 *
236 * @return a <code>File</code> value
237 * @exception SecurityException if an error occurs
238 */
239 public static synchronized File getOutputDir() throws SecurityException {
240 if (s_outDir == null) {
241 String dummyName = "dummy";
242 dummyName = new File(dummyName).getAbsolutePath();
243 setOutputRoot(dummyName.substring(0, dummyName.lastIndexOf(fileSeparator)));
244 }
245 return s_outDir;
246 }
247
248 /***
249 * Create a temporary directory which will be used for unzipping
250 * jars.
251 *
252 * @return a <code>File</code> value
253 */
254 public static synchronized File getTempDir() {
255 if (s_tempDir == null) {
256 s_tempDir = new MyFile(DPT_TEMP_DIR);
257 s_tempDir.mkdir();
258 s_tempDir.deleteOnExit();
259 }
260 return s_tempDir;
261 }
262
263 public static synchronized void deleteTempDir() {
264 if (s_tempDir != null) {
265 s_tempDir.delete();
266 }
267 s_tempDir = null;
268 }
269 }
This page was automatically generated by Maven