View Javadoc

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  package org.apache.james.context;
21  
22  import org.apache.avalon.framework.context.Context;
23  import org.apache.avalon.framework.context.ContextException;
24  
25  import java.io.File;
26  import java.io.IOException;
27  
28  /***
29   * This class is essentially a set of static functions for
30   * extracting information from the Avalon context.  This class
31   * should never be instantiated.  Each function takes the context
32   * as a first argument.
33   */
34  public class AvalonContextUtilities {
35  
36      /***
37       * The file URL prefix
38       */
39      private static String filePrefix = "file://";
40  
41      /***
42       * The file URL prefix length
43       */
44      private static int filePrefixLength = filePrefix.length();
45  
46      /***
47       * Gets the file or directory described by the argument file URL.
48       *
49       * @param context the Avalon context
50       * @param fileURL an appropriately formatted URL describing a file on
51       *                the filesystem on which the server is running.  The 
52       *                URL is evaluated as a location relative to the
53       *                application home, unless it begins with a slash.  In
54       *                the latter case the file location is evaluated relative
55       *                to the underlying file system root.
56       *
57       * @throws IllegalArgumentException if the arguments are null or the file
58       *                                  URL is not appropriately formatted.
59       * @throws ContextException if the underlying context generates a
60       *                          ContextException, if the application home is
61       *                          not correct, or if an IOException is generated
62       *                          while accessing the file.
63       */
64      public static File getFile(Context context, String fileURL)
65              throws Exception {
66          if ((context == null) || (fileURL == null)) {
67              throw new IllegalArgumentException("The getFile method doesn't allow null arguments.");
68          }
69          String internalFileURL = fileURL.trim();
70          if (!(internalFileURL.startsWith(filePrefix))) {
71              throw new IllegalArgumentException("The fileURL argument to getFile doesn't start with the required file prefix - "  + filePrefix);
72          }
73  
74          String fileName = fileURL.substring(filePrefixLength);
75          if (!(fileName.startsWith("/"))) {
76              String baseDirectory = "";
77              try {
78                  File applicationHome =
79                      (File)context.get(AvalonContextConstants.APPLICATION_HOME);
80                  baseDirectory = applicationHome.toString();
81              } catch (ContextException ce) {
82                  throw new ContextException("Encountered exception when resolving application home in Avalon context.", ce);
83              } catch (ClassCastException cce) {
84                  throw new ContextException("Application home object stored in Avalon context was not of type java.io.File.", cce);
85              }
86              StringBuffer fileNameBuffer =
87                  new StringBuffer(128)
88                      .append(baseDirectory)
89                      .append(File.separator)
90                              .append(fileName);
91              fileName = fileNameBuffer.toString();
92          }
93          try {
94              File returnValue = (new File(fileName)).getCanonicalFile();
95              return returnValue;
96          } catch (IOException ioe) {
97              throw new ContextException("Encountered an unexpected exception while retrieving file.", ioe);
98          }
99      }
100 
101     /***
102      * Private constructor to ensure that instances of this class aren't
103      * instantiated.
104      */
105     private AvalonContextUtilities() {}
106 }