KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > config > schema > setup > sources > ServerConfigurationSource


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.config.schema.setup.sources;
5
6 import com.tc.config.schema.setup.ConfigurationSetupException;
7 import com.tc.util.Assert;
8
9 import java.io.File JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.net.MalformedURLException JavaDoc;
13 import java.net.URL JavaDoc;
14 import java.net.URLConnection JavaDoc;
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 JavaDoc host;
24   private final int port;
25
26   public ServerConfigurationSource(String JavaDoc 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 JavaDoc getInputStream(long maxTimeoutMillis) throws IOException JavaDoc, ConfigurationSetupException {
34     try {
35       URL JavaDoc theURL = new URL JavaDoc("http", host, port, "/config");
36   
37       // JDK: 1.4.2 - These settings are proprietary to Sun's implementation of java.net.URL in version 1.4.2
38
System.setProperty("sun.net.client.defaultConnectTimeout", String.valueOf(maxTimeoutMillis));
39       System.setProperty("sun.net.client.defaultReadTimeout", String.valueOf(maxTimeoutMillis));
40
41       URLConnection JavaDoc connection = theURL.openConnection();
42       return connection.getInputStream();
43     } catch (MalformedURLException JavaDoc murle) {
44       throw new ConfigurationSetupException("Can't load configuration from "+this+".");
45     }
46   }
47
48   public File JavaDoc directoryLoadedFrom() {
49     return null;
50   }
51
52   public boolean isTrusted() {
53     return true;
54   }
55
56   public String JavaDoc toString() {
57     return "server at '" + this.host + ":" + this.port + "'";
58   }
59
60 }
61
Popular Tags