01: /*
02: * $Id: ConfigResource.java 10764 2008-02-10 22:41:10Z rossmason $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10: package org.mule.config;
11:
12: import org.mule.util.IOUtils;
13:
14: import java.io.FileNotFoundException;
15: import java.io.InputStream;
16: import java.io.IOException;
17: import java.net.URL;
18:
19: /**
20: * A ConfigResource holds the url description (or location) and the url stream. It is useful to associate the two
21: * for error reporting when the stream cannot be read.
22: */
23: public class ConfigResource {
24: private String resourceName;
25: private URL url;
26: private InputStream inputStream;
27:
28: public ConfigResource(String resourceName) throws IOException {
29: this .resourceName = resourceName;
30: url = IOUtils.getResourceAsUrl(resourceName, getClass(), true,
31: true);
32: if (url == null) {
33: throw new FileNotFoundException(resourceName);
34: }
35: }
36:
37: public ConfigResource(URL url) {
38: this .url = url;
39: this .resourceName = url.toExternalForm();
40: }
41:
42: public ConfigResource(String resourceName, InputStream inputStream) {
43: this .inputStream = inputStream;
44: this .resourceName = resourceName;
45: }
46:
47: public InputStream getInputStream() throws IOException {
48: if (inputStream == null && url != null) {
49: inputStream = url.openStream();
50: }
51: return inputStream;
52: }
53:
54: public URL getUrl() {
55: return url;
56: }
57:
58: public String getResourceName() {
59: return resourceName;
60: }
61:
62: public boolean isStreamOpen() {
63: return inputStream != null;
64: }
65:
66: public boolean equals(Object o) {
67: if (this == o) {
68: return true;
69: }
70: if (o == null || getClass() != o.getClass()) {
71: return false;
72: }
73:
74: ConfigResource that = (ConfigResource) o;
75:
76: if (resourceName != null ? !resourceName
77: .equals(that.resourceName) : that.resourceName != null) {
78: return false;
79: }
80:
81: return true;
82: }
83:
84: public int hashCode() {
85: int result;
86: result = (resourceName != null ? resourceName.hashCode() : 0);
87: result = 31 * result + (url != null ? url.hashCode() : 0);
88: return result;
89: }
90:
91: public String toString() {
92: final StringBuffer sb = new StringBuffer();
93: sb.append("ConfigResource");
94: sb.append("{resourceName='").append(resourceName).append('\'');
95: sb.append('}');
96: return sb.toString();
97: }
98: }
|