1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration;
18
19 import java.io.File;
20 import java.io.InputStream;
21 import java.io.OutputStream;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.commons.logging.impl.NoOpLog;
28
29
30
31
32
33
34
35
36 public abstract class FileSystem
37 {
38
39 private static final String FILE_SYSTEM = "org.apache.commons.configuration.filesystem";
40
41
42 private static FileSystem fileSystem;
43
44
45 private Log log;
46
47
48 private FileOptionsProvider optionsProvider;
49
50 public FileSystem()
51 {
52 setLogger(null);
53 }
54
55
56
57
58
59
60 public Log getLogger()
61 {
62 return log;
63 }
64
65
66
67
68
69
70
71
72
73
74 public void setLogger(Log log)
75 {
76 this.log = (log != null) ? log : new NoOpLog();
77 }
78
79 static
80 {
81 String fsClassName = System.getProperty(FILE_SYSTEM);
82 if (fsClassName != null)
83 {
84 Log log = LogFactory.getLog(FileSystem.class);
85
86 try
87 {
88 Class<?> clazz = Class.forName(fsClassName);
89 if (FileSystem.class.isAssignableFrom(clazz))
90 {
91 fileSystem = (FileSystem) clazz.newInstance();
92 if (log.isDebugEnabled())
93 {
94 log.debug("Using " + fsClassName);
95 }
96 }
97 }
98 catch (InstantiationException ex)
99 {
100 log.error("Unable to create " + fsClassName, ex);
101 }
102 catch (IllegalAccessException ex)
103 {
104 log.error("Unable to create " + fsClassName, ex);
105 }
106 catch (ClassNotFoundException ex)
107 {
108 log.error("Unable to create " + fsClassName, ex);
109 }
110 }
111
112 if (fileSystem == null)
113 {
114 fileSystem = new DefaultFileSystem();
115 }
116 }
117
118
119
120
121
122
123 public static void setDefaultFileSystem(FileSystem fs) throws NullPointerException
124 {
125 if (fs == null)
126 {
127 throw new NullPointerException("A FileSystem implementation is required");
128 }
129 fileSystem = fs;
130 }
131
132
133
134
135 public static void resetDefaultFileSystem()
136 {
137 fileSystem = new DefaultFileSystem();
138 }
139
140
141
142
143
144 public static FileSystem getDefaultFileSystem()
145 {
146 return fileSystem;
147 }
148
149
150
151
152
153 public void setFileOptionsProvider(FileOptionsProvider provider)
154 {
155 this.optionsProvider = provider;
156 }
157
158 public FileOptionsProvider getFileOptionsProvider()
159 {
160 return this.optionsProvider;
161 }
162
163 public abstract InputStream getInputStream(String basePath, String fileName)
164 throws ConfigurationException;
165
166 public abstract InputStream getInputStream(URL url) throws ConfigurationException;
167
168 public abstract OutputStream getOutputStream(URL url) throws ConfigurationException;
169
170 public abstract OutputStream getOutputStream(File file) throws ConfigurationException;
171
172 public abstract String getPath(File file, URL url, String basePath, String fileName);
173
174 public abstract String getBasePath(String path);
175
176 public abstract String getFileName(String path);
177
178 public abstract URL locateFromURL(String basePath, String fileName);
179
180 public abstract URL getURL(String basePath, String fileName) throws MalformedURLException;
181 }