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  import org.apache.avalon.framework.context.Contextualizable;
25  import org.apache.james.services.FileSystem;
26  
27  import java.io.File;
28  import java.io.FileNotFoundException;
29  import java.io.InputStream;
30  import java.io.IOException;
31  import java.io.FileInputStream;
32  
33  /**
34   * Avalon implementation of the FileSystem service
35   * 
36   * @since 2.4
37   */
38  public class AvalonFileSystem implements FileSystem, Contextualizable {
39  
40      /**
41       * Avalon context used by this implementation
42       */
43      private Context context;
44  
45      /**
46       * delegates to method getFile() and returns file as InputStream.
47       * @see org.apache.james.context.AvalonFileSystem#getFile(java.lang.String) 
48       */
49      public InputStream getResource(String url) throws IOException {
50          return new FileInputStream(getFile(url)); 
51      }
52  
53      /**
54       * @see org.apache.james.services.FileSystem#getFile(java.lang.String)
55       */
56      public File getFile(String fileURL) throws FileNotFoundException {
57          try {
58              return AvalonContextUtilities.getFile(context, fileURL);
59          } catch (ContextException e) {
60              throw new FileNotFoundException("Context exception: "+e.getMessage());
61          } catch (IllegalArgumentException e) {
62              throw new FileNotFoundException("Context exception: "+e.getMessage());
63          }
64      }
65      
66      /**
67       * @see org.apache.james.services.FileSystem#getBasedir()
68       */
69      public File getBasedir() throws FileNotFoundException {
70          try {
71              return (File) context.get( "urn:avalon:home" );
72          } catch (ContextException e) {
73              try {
74                  return ((File) context.get( AvalonContextConstants.APPLICATION_HOME) );
75              } catch (ContextException e2) {
76                  throw new FileNotFoundException(e2.getMessage());
77              }
78          }
79      }
80  
81      /**
82       * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
83       */
84      public void contextualize(Context context) throws ContextException {
85          this.context = context;
86      }
87  
88  }