FileDocCategorySizeDatePackage
AvalonContextUtilities.javaAPI DocApache James 2.3.14790Fri Jan 12 12:56:22 GMT 2007org.apache.james.context

AvalonContextUtilities

public class AvalonContextUtilities extends Object
This class is essentially a set of static functions for extracting information from the Avalon context. This class should never be instantiated. Each function takes the context as a first argument.

Fields Summary
private static String
filePrefix
The file URL prefix
private static int
filePrefixLength
The file URL prefix length
Constructors Summary
private AvalonContextUtilities()
Private constructor to ensure that instances of this class aren't instantiated.

Methods Summary
public static java.io.FilegetFile(org.apache.avalon.framework.context.Context context, java.lang.String fileURL)
Gets the file or directory described by the argument file URL.

param
context the Avalon context
param
fileURL an appropriately formatted URL describing a file on the filesystem on which the server is running. The URL is evaluated as a location relative to the application home, unless it begins with a slash. In the latter case the file location is evaluated relative to the underlying file system root.
throws
IllegalArgumentException if the arguments are null or the file URL is not appropriately formatted.
throws
ContextException if the underlying context generates a ContextException, if the application home is not correct, or if an IOException is generated while accessing the file.


                                                                                                                                                                                                                                                                                                                
          
              
        if ((context == null) || (fileURL == null)) {
            throw new IllegalArgumentException("The getFile method doesn't allow null arguments.");
        }
        String internalFileURL = fileURL.trim();
        if (!(internalFileURL.startsWith(filePrefix))) {
            throw new IllegalArgumentException("The fileURL argument to getFile doesn't start with the required file prefix - "  + filePrefix);
        }

        String fileName = fileURL.substring(filePrefixLength);
        if (!(fileName.startsWith("/"))) {
            String baseDirectory = "";
            try {
                File applicationHome =
                    (File)context.get(AvalonContextConstants.APPLICATION_HOME);
                baseDirectory = applicationHome.toString();
            } catch (ContextException ce) {
                throw new ContextException("Encountered exception when resolving application home in Avalon context.", ce);
            } catch (ClassCastException cce) {
                throw new ContextException("Application home object stored in Avalon context was not of type java.io.File.", cce);
            }
            StringBuffer fileNameBuffer =
                new StringBuffer(128)
                    .append(baseDirectory)
                    .append(File.separator)
                            .append(fileName);
            fileName = fileNameBuffer.toString();
        }
        try {
            File returnValue = (new File(fileName)).getCanonicalFile();
            return returnValue;
        } catch (IOException ioe) {
            throw new ContextException("Encountered an unexpected exception while retrieving file.", ioe);
        }