1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
35
36
37
38 public class AvalonFileSystem implements FileSystem, Contextualizable {
39
40
41
42
43 private Context context;
44
45
46
47
48
49 public InputStream getResource(String url) throws IOException {
50 return new FileInputStream(getFile(url));
51 }
52
53
54
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
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
83
84 public void contextualize(Context context) throws ContextException {
85 this.context = context;
86 }
87
88 }