1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
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 * This class is essentially a set of static functions for
32 * extracting information from the Avalon context. This class
33 * should never be instantiated. Each function takes the context
34 * as a first argument.
35 */
36 public class AvalonContextUtilities {
37
38 /**
39 * The file URL prefix
40 */
41 private static String filePrefix = "file://";
42
43 /**
44 * The file URL prefix length
45 */
46 private static int filePrefixLength = filePrefix.length();
47
48 /**
49 * Gets the file or directory described by the argument file URL.
50 *
51 * @param context the Avalon context
52 * @param fileURL an appropriately formatted URL describing a file on
53 * the filesystem on which the server is running. The
54 * URL is evaluated as a location relative to the
55 * application home, unless it begins with a slash. In
56 * the latter case the file location is evaluated relative
57 * to the underlying file system root.
58 *
59 * @throws IllegalArgumentException if the arguments are null or the file
60 * URL is not appropriately formatted.
61 * @throws ContextException if the underlying context generates a
62 * ContextException, if the application home is
63 * not correct, or if an IOException is generated
64 * while accessing the file.
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 * Private constructor to ensure that instances of this class aren't
104 * instantiated.
105 */
106 private AvalonContextUtilities() {}
107 }