1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.james.context;
23
24 import org.apache.avalon.framework.context.Context;
25 import org.apache.avalon.framework.context.ContextException;
26
27 import java.io.File;
28 import java.io.IOException;
29
30
31
32
33
34
35
36 public class AvalonContextUtilities {
37
38
39
40
41 private static String filePrefix = "file://";
42
43
44
45
46 private static int filePrefixLength = filePrefix.length();
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public static File getFile(Context context, String fileURL) throws ContextException, IllegalArgumentException {
67 if ((context == null) || (fileURL == null)) {
68 throw new IllegalArgumentException("The getFile method doesn't allow null arguments.");
69 }
70 String internalFileURL = fileURL.trim();
71 if (!(internalFileURL.startsWith(filePrefix))) {
72 throw new IllegalArgumentException("file URL '" + fileURL + "' is missing required prefix '" + filePrefix + "'");
73 }
74
75 String fileName = fileURL.substring(filePrefixLength);
76 if (!(fileName.startsWith("/"))) {
77 String baseDirectory = "";
78 try {
79 File applicationHome =
80 (File)context.get(AvalonContextConstants.APPLICATION_HOME);
81 baseDirectory = applicationHome.toString();
82 } catch (ContextException ce) {
83 throw new ContextException("Encountered exception when resolving application home in Avalon context.", ce);
84 } catch (ClassCastException cce) {
85 throw new ContextException("Application home object stored in Avalon context was not of type java.io.File.", cce);
86 }
87 StringBuffer fileNameBuffer =
88 new StringBuffer(128)
89 .append(baseDirectory)
90 .append(File.separator)
91 .append(fileName);
92 fileName = fileNameBuffer.toString();
93 }
94 try {
95 File returnValue = (new File(fileName)).getCanonicalFile();
96 return returnValue;
97 } catch (IOException ioe) {
98 throw new ContextException("Encountered an unexpected exception while retrieving file.", ioe);
99 }
100 }
101
102
103
104
105
106 private AvalonContextUtilities() {}
107 }