01: package com.ice.jcvsweb.helper;
02:
03: import java.io.File;
04: import java.io.IOException;
05: import javax.servlet.ServletContext;
06:
07: import java.security.MessageDigest;
08: import java.security.NoSuchAlgorithmException;
09:
10: /**
11: * This method provides utilities related to the servlet context of
12: * our webapp. For example, getting the absolute paths of files.
13: *
14: * @author Tim Endres,
15: * <a href="mailto:time@BlockIslandGroup.com">time@BlockIslandGroup.com</a>
16: *
17: */
18:
19: public class ContextHelper {
20: public static String getRealAbsolutePath(ServletContext ctx,
21: String path) {
22: String result = path;
23: String basePath = ctx.getRealPath("/");
24:
25: File f = new File(path);
26: if (!f.isAbsolute()) {
27: if (path.startsWith("./")) {
28: path = path.substring(2);
29: }
30:
31: result = basePath + File.separator + path;
32: }
33:
34: return result;
35: }
36:
37: public static void ensureRealPath(ServletContext ctx, String path) {
38: String realPath = ContextHelper.getRealAbsolutePath(ctx, path);
39:
40: File realDirF = new File(realPath);
41:
42: if (!realDirF.exists()) {
43: if (!realDirF.mkdirs()) {
44: ctx.log("ERROR cound not create path '"
45: + realDirF.getPath() + "'");
46: }
47: } else if (realDirF.isFile()) {
48: ctx.log("WARNING path '" + realDirF.getPath()
49: + "' is a file.");
50: }
51: }
52:
53: }
|