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 | 119 | { |
52 | 119 | setLogger(null); |
53 | 119 | } |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public Log getLogger() |
61 | |
{ |
62 | 0 | return log; |
63 | |
} |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public void setLogger(Log log) |
75 | |
{ |
76 | 119 | this.log = (log != null) ? log : new NoOpLog(); |
77 | 119 | } |
78 | |
|
79 | |
static |
80 | |
{ |
81 | 1 | String fsClassName = System.getProperty(FILE_SYSTEM); |
82 | 1 | if (fsClassName != null) |
83 | |
{ |
84 | 0 | Log log = LogFactory.getLog(FileSystem.class); |
85 | |
|
86 | |
try |
87 | |
{ |
88 | 0 | Class<?> clazz = Class.forName(fsClassName); |
89 | 0 | if (FileSystem.class.isAssignableFrom(clazz)) |
90 | |
{ |
91 | 0 | fileSystem = (FileSystem) clazz.newInstance(); |
92 | 0 | if (log.isDebugEnabled()) |
93 | |
{ |
94 | 0 | log.debug("Using " + fsClassName); |
95 | |
} |
96 | |
} |
97 | |
} |
98 | 0 | catch (InstantiationException ex) |
99 | |
{ |
100 | 0 | log.error("Unable to create " + fsClassName, ex); |
101 | |
} |
102 | 0 | catch (IllegalAccessException ex) |
103 | |
{ |
104 | 0 | log.error("Unable to create " + fsClassName, ex); |
105 | |
} |
106 | 0 | catch (ClassNotFoundException ex) |
107 | |
{ |
108 | 0 | log.error("Unable to create " + fsClassName, ex); |
109 | 0 | } |
110 | |
} |
111 | |
|
112 | 1 | if (fileSystem == null) |
113 | |
{ |
114 | 1 | fileSystem = new DefaultFileSystem(); |
115 | |
} |
116 | 1 | } |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
public static void setDefaultFileSystem(FileSystem fs) throws NullPointerException |
124 | |
{ |
125 | 53 | if (fs == null) |
126 | |
{ |
127 | 0 | throw new NullPointerException("A FileSystem implementation is required"); |
128 | |
} |
129 | 53 | fileSystem = fs; |
130 | 53 | } |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public static void resetDefaultFileSystem() |
136 | |
{ |
137 | 58 | fileSystem = new DefaultFileSystem(); |
138 | 58 | } |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
public static FileSystem getDefaultFileSystem() |
145 | |
{ |
146 | 1799 | return fileSystem; |
147 | |
} |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
public void setFileOptionsProvider(FileOptionsProvider provider) |
154 | |
{ |
155 | 0 | this.optionsProvider = provider; |
156 | 0 | } |
157 | |
|
158 | |
public FileOptionsProvider getFileOptionsProvider() |
159 | |
{ |
160 | 221 | 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 | |
} |