001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.context;
019:
020: import org.apache.avalon.framework.context.Context;
021: import org.apache.avalon.framework.context.ContextException;
022:
023: import java.io.File;
024: import java.io.IOException;
025:
026: /**
027: * This class is essentially a set of static functions for
028: * extracting information from the Avalon context. This class
029: * should never be instantiated. Each function takes the context
030: * as a first argument.
031: */
032: public class AvalonContextUtilities {
033:
034: /**
035: * The file URL prefix
036: */
037: private static String filePrefix = "file://";
038:
039: /**
040: * The file URL prefix length
041: */
042: private static int filePrefixLength = filePrefix.length();
043:
044: /**
045: * Gets the file or directory described by the argument file URL.
046: *
047: * @param context the Avalon context
048: * @param fileURL an appropriately formatted URL describing a file on
049: * the filesystem on which the server is running. The
050: * URL is evaluated as a location relative to the
051: * application home, unless it begins with a slash. In
052: * the latter case the file location is evaluated relative
053: * to the underlying file system root.
054: *
055: * @throws IllegalArgumentException if the arguments are null or the file
056: * URL is not appropriately formatted.
057: * @throws ContextException if the underlying context generates a
058: * ContextException, if the application home is
059: * not correct, or if an IOException is generated
060: * while accessing the file.
061: */
062: public static File getFile(Context context, String fileURL)
063: throws Exception {
064: if ((context == null) || (fileURL == null)) {
065: throw new IllegalArgumentException(
066: "The getFile method doesn't allow null arguments.");
067: }
068: String internalFileURL = fileURL.trim();
069: if (!(internalFileURL.startsWith(filePrefix))) {
070: throw new IllegalArgumentException(
071: "The fileURL argument to getFile doesn't start with the required file prefix - "
072: + filePrefix);
073: }
074:
075: String fileName = fileURL.substring(filePrefixLength);
076: if (!(fileName.startsWith("/"))) {
077: String baseDirectory = "";
078: try {
079: File applicationHome = (File) context
080: .get(AvalonContextConstants.APPLICATION_HOME);
081: baseDirectory = applicationHome.toString();
082: } catch (ContextException ce) {
083: throw new ContextException(
084: "Encountered exception when resolving application home in Avalon context.",
085: ce);
086: } catch (ClassCastException cce) {
087: throw new ContextException(
088: "Application home object stored in Avalon context was not of type java.io.File.",
089: cce);
090: }
091: StringBuffer fileNameBuffer = new StringBuffer(128).append(
092: baseDirectory).append(File.separator).append(
093: fileName);
094: fileName = fileNameBuffer.toString();
095: }
096: try {
097: File returnValue = (new File(fileName)).getCanonicalFile();
098: return returnValue;
099: } catch (IOException ioe) {
100: throw new ContextException(
101: "Encountered an unexpected exception while retrieving file.",
102: ioe);
103: }
104: }
105:
106: /**
107: * Private constructor to ensure that instances of this class aren't
108: * instantiated.
109: */
110: private AvalonContextUtilities() {
111: }
112: }
|