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.InputStream;
11:
12: /**
13: * A {@link ConfigurationSource} that returns data from a Java resource.
14: */
15: public class ResourceConfigurationSource implements ConfigurationSource {
16:
17: private final String path;
18: private final Class relativeTo;
19:
20: public ResourceConfigurationSource(String path, Class relativeTo) {
21: Assert.assertNotBlank(path);
22: Assert.assertNotNull(relativeTo);
23:
24: this .path = path;
25: this .relativeTo = relativeTo;
26: }
27:
28: public InputStream getInputStream(long maxTimeoutMillis)
29: throws ConfigurationSetupException {
30: InputStream out = this .relativeTo
31: .getResourceAsStream(this .path);
32: if (out == null)
33: throw new ConfigurationSetupException("Resource '"
34: + this .path + "', relative to class "
35: + this .relativeTo.getName() + ", does not exist");
36: return out;
37: }
38:
39: public File directoryLoadedFrom() {
40: return null;
41: }
42:
43: public boolean isTrusted() {
44: return false;
45: }
46:
47: public String toString() {
48: return "Java resource at '" + this .path
49: + "', relative to class " + this.relativeTo.getName();
50: }
51:
52: }
|