View Javadoc

1   /*
2    * $Id: ConfigResource.java 20321 2010-11-24 15:21:24Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
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  {
25      protected String resourceName;
26      private URL url;
27      private InputStream inputStream;
28  
29      public ConfigResource(String resourceName) throws IOException
30      {
31          this.resourceName = resourceName;
32          url = IOUtils.getResourceAsUrl(resourceName, getClass(), true, true);
33          if(url == null)
34          {
35              throw new FileNotFoundException(resourceName);
36          }
37      }
38  
39      public ConfigResource(URL url)
40      {
41          this.url = url;
42          this.resourceName = url.toExternalForm();
43      }
44  
45      public ConfigResource(String resourceName, InputStream inputStream)
46      {
47          this.inputStream = inputStream;
48          this.resourceName = resourceName;
49      }
50  
51      public InputStream getInputStream() throws IOException
52      {
53          if(inputStream==null && url !=null)
54          {
55              inputStream = url.openStream();
56          }
57          return inputStream;
58      }
59  
60      public URL getUrl()
61      {
62          return url;
63      }
64  
65      public String getResourceName()
66      {
67          return resourceName;
68      }
69  
70      public boolean isStreamOpen()
71      {
72          return inputStream!=null;
73      }
74  
75      public boolean equals(Object o)
76      {
77          if (this == o)
78          {
79              return true;
80          }
81          if (o == null || getClass() != o.getClass())
82          {
83              return false;
84          }
85  
86          ConfigResource that = (ConfigResource) o;
87  
88          if (resourceName != null ? !resourceName.equals(that.resourceName) : that.resourceName != null)
89          {
90              return false;
91          }
92  
93          return true;
94      }
95  
96      public int hashCode()
97      {
98          int result;
99          result = (resourceName != null ? resourceName.hashCode() : 0);
100         result = 31 * result + (url != null ? url.hashCode() : 0);
101         return result;
102     }
103 
104 
105     public String toString()
106     {
107         final StringBuffer sb = new StringBuffer();
108         sb.append("ConfigResource");
109         sb.append("{resourceName='").append(resourceName).append('\'');
110         sb.append('}');
111         return sb.toString();
112     }
113 }