/** * Load the configuration from the specified URL. This does not change the source of the * configuration (i.e. the internally maintained file name). Use on of the setter methods for this * purpose. * * @param url the URL of the file to be loaded * @throws ConfigurationException if an error occurs */ public void load(URL url) throws ConfigurationException { if (sourceURL == null) { if (StringUtils.isEmpty(getBasePath())) { // ensure that we have a valid base path setBasePath(url.toString()); } sourceURL = url; } // throw an exception if the target URL is a directory File file = ConfigurationUtils.fileFromURL(url); if (file != null && file.isDirectory()) { throw new ConfigurationException("Cannot load a configuration from a directory"); } InputStream in = null; try { in = url.openStream(); load(in); } catch (ConfigurationException e) { throw e; } catch (Exception e) { throw new ConfigurationException("Unable to load the configuration from the URL " + url, e); } finally { // close the input stream try { if (in != null) { in.close(); } } catch (IOException e) { getLogger().warn("Could not close input stream", e); } } }
/** * Return the file where the configuration is stored. If the base path is a URL with a protocol * different than "file", or the configuration file is within a compressed archive, the * return value will not point to a valid file object. * * @return the file where the configuration is stored; this can be <b>null</b> */ public File getFile() { if (getFileName() == null && sourceURL == null) { return null; } else if (sourceURL != null) { return ConfigurationUtils.fileFromURL(sourceURL); } else { return ConfigurationUtils.getFile(getBasePath(), getFileName()); } }
/** * Save the configuration to the specified URL. This doesn't change the source of the * configuration, use setURL() if you need it. * * @param url the URL * @throws ConfigurationException if an error occurs during the save operation */ public void save(URL url) throws ConfigurationException { // file URLs have to be converted to Files since FileURLConnection is // read only (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4191800) File file = ConfigurationUtils.fileFromURL(url); if (file != null) { save(file); } else { // for non file URLs save through an URLConnection OutputStream out = null; try { URLConnection connection = url.openConnection(); connection.setDoOutput(true); // use the PUT method for http URLs if (connection instanceof HttpURLConnection) { HttpURLConnection conn = (HttpURLConnection) connection; conn.setRequestMethod("PUT"); } out = connection.getOutputStream(); save(out); // check the response code for http URLs and throw an exception if an error occured if (connection instanceof HttpURLConnection) { HttpURLConnection conn = (HttpURLConnection) connection; if (conn.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) { throw new IOException( "HTTP Error " + conn.getResponseCode() + " " + conn.getResponseMessage()); } } } catch (IOException e) { throw new ConfigurationException("Could not save to URL " + url, e); } finally { closeSilent(out); } } }