KickJava   Java API By Example, From Geeks To Geeks.

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


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 URLConfigurationSource implements ConfigurationSource {
22
23   private final String JavaDoc url;
24
25   public URLConfigurationSource(String JavaDoc url) {
26     Assert.assertNotBlank(url);
27     this.url = url;
28   }
29
30   public InputStream JavaDoc getInputStream(long maxTimeoutMillis) throws IOException JavaDoc, ConfigurationSetupException {
31     URL JavaDoc theURL = new URL JavaDoc(this.url);
32     
33     // JDK: 1.4.2 - These settings are proprietary to Sun's implementation of java.net.URL in version 1.4.2
34
System.setProperty("sun.net.client.defaultConnectTimeout", String.valueOf(maxTimeoutMillis));
35     System.setProperty("sun.net.client.defaultReadTimeout", String.valueOf(maxTimeoutMillis));
36     
37     try {
38       URLConnection JavaDoc connection = theURL.openConnection();
39       return connection.getInputStream();
40     } catch (MalformedURLException JavaDoc murle) {
41       throw new ConfigurationSetupException("The URL '" + this.url
42                                             + "' is malformed, and thus can't be used to load configuration.");
43     }
44   }
45
46   public File JavaDoc directoryLoadedFrom() {
47     return null;
48   }
49
50   public boolean isTrusted() {
51     return false;
52   }
53
54   public String JavaDoc toString() {
55     return "URL '" + this.url + "'";
56   }
57
58 }
59
Popular Tags