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