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 ServerConfigurationSource implements ConfigurationSource {
22:
23: private final String host;
24: private final int port;
25:
26: public ServerConfigurationSource(String host, int port) {
27: Assert.assertNotBlank(host);
28: Assert.assertTrue(port > 0);
29: this .host = host;
30: this .port = port;
31: }
32:
33: public InputStream getInputStream(long maxTimeoutMillis)
34: throws IOException, ConfigurationSetupException {
35: try {
36: URL theURL = new URL("http", host, port, "/config");
37:
38: // JDK: 1.4.2 - These settings are proprietary to Sun's implementation of java.net.URL in version 1.4.2
39: System.setProperty("sun.net.client.defaultConnectTimeout",
40: String.valueOf(maxTimeoutMillis));
41: System.setProperty("sun.net.client.defaultReadTimeout",
42: String.valueOf(maxTimeoutMillis));
43:
44: URLConnection connection = theURL.openConnection();
45: return connection.getInputStream();
46: } catch (MalformedURLException murle) {
47: throw new ConfigurationSetupException(
48: "Can't load configuration from " + this + ".");
49: }
50: }
51:
52: public File directoryLoadedFrom() {
53: return null;
54: }
55:
56: public boolean isTrusted() {
57: return true;
58: }
59:
60: public String toString() {
61: return "server at '" + this .host + ":" + this .port + "'";
62: }
63:
64: }
|