01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.config.schema.setup.sources;
05:
06: import com.tc.config.schema.setup.ConfigurationSetupException;
07: import com.tc.util.Assert;
08:
09: import java.io.File;
10: import java.io.IOException;
11: import java.io.InputStream;
12: import java.net.MalformedURLException;
13: import java.net.URL;
14: import java.net.URLConnection;
15:
16: /**
17: * A {@link ConfigurationSource} that reads from a URL.
18: *
19: * @see URLConfigurationSourceTest
20: */
21: public class URLConfigurationSource implements ConfigurationSource {
22:
23: private final String url;
24:
25: public URLConfigurationSource(String url) {
26: Assert.assertNotBlank(url);
27: this .url = url;
28: }
29:
30: public InputStream getInputStream(long maxTimeoutMillis)
31: throws IOException, ConfigurationSetupException {
32: URL theURL = new URL(this .url);
33:
34: // JDK: 1.4.2 - These settings are proprietary to Sun's implementation of java.net.URL in version 1.4.2
35: System.setProperty("sun.net.client.defaultConnectTimeout",
36: String.valueOf(maxTimeoutMillis));
37: System.setProperty("sun.net.client.defaultReadTimeout", String
38: .valueOf(maxTimeoutMillis));
39:
40: try {
41: URLConnection connection = theURL.openConnection();
42: return connection.getInputStream();
43: } catch (MalformedURLException murle) {
44: throw new ConfigurationSetupException(
45: "The URL '"
46: + this .url
47: + "' is malformed, and thus can't be used to load configuration.");
48: }
49: }
50:
51: public File directoryLoadedFrom() {
52: return null;
53: }
54:
55: public boolean isTrusted() {
56: return false;
57: }
58:
59: public String toString() {
60: return "URL '" + this .url + "'";
61: }
62:
63: }
|