View Javadoc

1   /************************************************************************
2    * Copyright (c) 2000-2006 The Apache Software Foundation.             *
3    * All rights reserved.                                                *
4    * ------------------------------------------------------------------- *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you *
6    * may not use this file except in compliance with the License. You    *
7    * may obtain a copy of the License at:                                *
8    *                                                                     *
9    *     http://www.apache.org/licenses/LICENSE-2.0                      *
10   *                                                                     *
11   * Unless required by applicable law or agreed to in writing, software *
12   * distributed under the License is distributed on an "AS IS" BASIS,   *
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
14   * implied.  See the License for the specific language governing       *
15   * permissions and limitations under the License.                      *
16   ***********************************************************************/
17  
18  package org.apache.james.context;
19  
20  import org.apache.avalon.framework.context.Context;
21  import org.apache.avalon.framework.context.ContextException;
22  
23  import java.io.File;
24  import java.io.IOException;
25  
26  /***
27   * This class is essentially a set of static functions for
28   * extracting information from the Avalon context.  This class
29   * should never be instantiated.  Each function takes the context
30   * as a first argument.
31   */
32  public class AvalonContextUtilities {
33  
34      /***
35       * The file URL prefix
36       */
37      private static String filePrefix = "file://";
38  
39      /***
40       * The file URL prefix length
41       */
42      private static int filePrefixLength = filePrefix.length();
43  
44      /***
45       * Gets the file or directory described by the argument file URL.
46       *
47       * @param context the Avalon context
48       * @param fileURL an appropriately formatted URL describing a file on
49       *                the filesystem on which the server is running.  The 
50       *                URL is evaluated as a location relative to the
51       *                application home, unless it begins with a slash.  In
52       *                the latter case the file location is evaluated relative
53       *                to the underlying file system root.
54       *
55       * @throws IllegalArgumentException if the arguments are null or the file
56       *                                  URL is not appropriately formatted.
57       * @throws ContextException if the underlying context generates a
58       *                          ContextException, if the application home is
59       *                          not correct, or if an IOException is generated
60       *                          while accessing the file.
61       */
62      public static File getFile(Context context, String fileURL)
63              throws Exception {
64          if ((context == null) || (fileURL == null)) {
65              throw new IllegalArgumentException("The getFile method doesn't allow null arguments.");
66          }
67          String internalFileURL = fileURL.trim();
68          if (!(internalFileURL.startsWith(filePrefix))) {
69              throw new IllegalArgumentException("The fileURL argument to getFile doesn't start with the required file prefix - "  + filePrefix);
70          }
71  
72          String fileName = fileURL.substring(filePrefixLength);
73          if (!(fileName.startsWith("/"))) {
74              String baseDirectory = "";
75              try {
76                  File applicationHome =
77                      (File)context.get(AvalonContextConstants.APPLICATION_HOME);
78                  baseDirectory = applicationHome.toString();
79              } catch (ContextException ce) {
80                  throw new ContextException("Encountered exception when resolving application home in Avalon context.", ce);
81              } catch (ClassCastException cce) {
82                  throw new ContextException("Application home object stored in Avalon context was not of type java.io.File.", cce);
83              }
84              StringBuffer fileNameBuffer =
85                  new StringBuffer(128)
86                      .append(baseDirectory)
87                      .append(File.separator)
88                              .append(fileName);
89              fileName = fileNameBuffer.toString();
90          }
91          try {
92              File returnValue = (new File(fileName)).getCanonicalFile();
93              return returnValue;
94          } catch (IOException ioe) {
95              throw new ContextException("Encountered an unexpected exception while retrieving file.", ioe);
96          }
97      }
98  
99      /***
100      * Private constructor to ensure that instances of this class aren't
101      * instantiated.
102      */
103     private AvalonContextUtilities() {}
104 }