2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.commons.configuration;
20
import java.io.FileNotFoundException;
21
import java.io.FileOutputStream;
22
import java.io.IOException;
23
import java.io.InputStream;
24
import java.io.OutputStream;
25
import java.net.HttpURLConnection;
26
import java.net.MalformedURLException;
28
import java.net.URLConnection;
30
import org.apache.commons.logging.Log;
31
import org.apache.commons.logging.LogFactory;
34
* FileSystem that uses java.io.File or HttpClient
37
* href="http://commons.apache.org/configuration/team-list.html">Commons Configuration team</a>
39
public class DefaultFileSystem extends FileSystem
42
* The Log for diagnostic messages.
44
private Log log = LogFactory.getLog(DefaultFileSystem.class);
47
public InputStream getInputStream(String basePath, String fileName)
48
throws ConfigurationException
52
URL url = ConfigurationUtils.locate(this, basePath, fileName);
56
throw new ConfigurationException("Cannot locate configuration source " + fileName);
58
return getInputStream(url);
60
catch (ConfigurationException e)
66
throw new ConfigurationException("Unable to load the configuration file " + fileName, e);
71
public InputStream getInputStream(URL url) throws ConfigurationException
73
// throw an exception if the target URL is a directory
74
File file = ConfigurationUtils.fileFromURL(url);
75
if (file != null && file.isDirectory())
77
throw new ConfigurationException("Cannot load a configuration from a directory");
82
return url.openStream();
86
throw new ConfigurationException("Unable to load the configuration from the URL " + url, e);
91
public OutputStream getOutputStream(URL url) throws ConfigurationException
93
// file URLs have to be converted to Files since FileURLConnection is
94
// read only (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4191800)
95
File file = ConfigurationUtils.fileFromURL(url);
98
return getOutputStream(file);
102
// for non file URLs save through an URLConnection
106
URLConnection connection = url.openConnection();
107
connection.setDoOutput(true);
109
// use the PUT method for http URLs
110
if (connection instanceof HttpURLConnection)
112
HttpURLConnection conn = (HttpURLConnection) connection;
113
conn.setRequestMethod("PUT");
116
out = connection.getOutputStream();
118
// check the response code for http URLs and throw an exception if an error occured
119
if (connection instanceof HttpURLConnection)
121
out = new HttpOutputStream(out, (HttpURLConnection) connection);
125
catch (IOException e)
127
throw new ConfigurationException("Could not save to URL " + url, e);
133
public OutputStream getOutputStream(File file) throws ConfigurationException
137
// create the file if necessary
139
return new FileOutputStream(file);
141
catch (FileNotFoundException e)
143
throw new ConfigurationException("Unable to save to file " + file, e);
148
public String getPath(File file, URL url, String basePath, String fileName)
151
// if resource was loaded from jar file may be null
154
path = file.getAbsolutePath();
157
// try to see if file was loaded from a jar
162
path = url.getPath();
168
path = getURL(basePath, fileName).getPath();
172
// simply ignore it and return null
173
if (log.isDebugEnabled())
175
log.debug(String.format("Could not determine URL for "
176
+ "basePath = %s, fileName = %s.", basePath,
187
public String getBasePath(String path)
192
url = getURL(null, path);
193
return ConfigurationUtils.getBasePath(url);
202
public String getFileName(String path)
207
url = getURL(null, path);
208
return ConfigurationUtils.getFileName(url);
218
public URL getURL(String basePath, String file) throws MalformedURLException
220
File f = new File(file);
221
if (f.isAbsolute()) // already absolute?
223
return ConfigurationUtils.toURL(f);
228
if (basePath == null)
230
return new URL(file);
234
URL base = new URL(basePath);
235
return new URL(base, file);
238
catch (MalformedURLException uex)
240
return ConfigurationUtils.toURL(ConfigurationUtils.constructFile(basePath, file));
246
public URL locateFromURL(String basePath, String fileName)
251
if (basePath == null)
253
return new URL(fileName);
254
//url = new URL(name);
258
URL baseURL = new URL(basePath);
259
url = new URL(baseURL, fileName);
261
// check if the file exists
262
InputStream in = null;
265
in = url.openStream();
277
catch (IOException e)
279
if (log.isDebugEnabled())
281
log.debug("Could not locate file " + fileName + " at " + basePath + ": " + e.getMessage());
288
* Create the path to the specified file.
290
* @param file the target file
292
private void createPath(File file)
296
// create the path to the file if the file doesn't exist
299
File parent = file.getParentFile();
300
if (parent != null && !parent.exists())
308
* Wraps the output stream so errors can be detected in the HTTP response.
311
* href="http://commons.apache.org/configuration/team-list.html">Commons Configuration team</a>
313
private static class HttpOutputStream extends VerifiableOutputStream
315
/** The wrapped OutputStream */
316
private final OutputStream stream;
318
/** The HttpURLConnection */
319
private final HttpURLConnection connection;
321
public HttpOutputStream(OutputStream stream, HttpURLConnection connection)
323
this.stream = stream;
324
this.connection = connection;
328
public void write(byte[] bytes) throws IOException
334
public void write(byte[] bytes, int i, int i1) throws IOException
336
stream.write(bytes, i, i1);
340
public void flush() throws IOException
346
public void close() throws IOException
352
public void write(int i) throws IOException
358
public String toString()
360
return stream.toString();
364
public void verify() throws IOException
366
if (connection.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST)
368
throw new IOException("HTTP Error " + connection.getResponseCode()
369
+ " " + connection.getResponseMessage());