Coverage Report - org.mule.config.ConfigResource
 
Classes in this File Line Coverage Branch Coverage Complexity
ConfigResource
31%
11/36
4%
1/24
1.9
 
 1  
 /*
 2  
  * $Id: ConfigResource.java 11600 2008-04-18 18:13:47Z dfeist $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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  4
     {
 31  4
         this.resourceName = resourceName;
 32  4
         url = IOUtils.getResourceAsUrl(resourceName, getClass(), true, true);
 33  4
         if(url == null)
 34  
         {
 35  0
             throw new FileNotFoundException(resourceName);
 36  
         }
 37  4
     }
 38  
 
 39  
     public ConfigResource(URL url)
 40  0
     {
 41  0
         this.url = url;
 42  0
         this.resourceName = url.toExternalForm();
 43  0
     }
 44  
 
 45  
     public ConfigResource(String resourceName, InputStream inputStream)
 46  0
     {
 47  0
         this.inputStream = inputStream;
 48  0
         this.resourceName = resourceName;
 49  0
     }
 50  
 
 51  
     public InputStream getInputStream() throws IOException
 52  
     {
 53  0
         if(inputStream==null && url !=null)
 54  
         {
 55  0
             inputStream = url.openStream();
 56  
         }
 57  0
         return inputStream;
 58  
     }
 59  
 
 60  
     public URL getUrl()
 61  
     {
 62  4
         return url;
 63  
     }
 64  
 
 65  
     public String getResourceName()
 66  
     {
 67  0
         return resourceName;
 68  
     }
 69  
 
 70  
     public boolean isStreamOpen()
 71  
     {
 72  0
         return inputStream!=null;
 73  
     }
 74  
 
 75  
     public boolean equals(Object o)
 76  
     {
 77  0
         if (this == o)
 78  
         {
 79  0
             return true;
 80  
         }
 81  0
         if (o == null || getClass() != o.getClass())
 82  
         {
 83  0
             return false;
 84  
         }
 85  
 
 86  0
         ConfigResource that = (ConfigResource) o;
 87  
 
 88  0
         if (resourceName != null ? !resourceName.equals(that.resourceName) : that.resourceName != null)
 89  
         {
 90  0
             return false;
 91  
         }
 92  
 
 93  0
         return true;
 94  
     }
 95  
 
 96  
     public int hashCode()
 97  
     {
 98  
         int result;
 99  0
         result = (resourceName != null ? resourceName.hashCode() : 0);
 100  0
         result = 31 * result + (url != null ? url.hashCode() : 0);
 101  0
         return result;
 102  
     }
 103  
 
 104  
 
 105  
     public String toString()
 106  
     {
 107  4
         final StringBuffer sb = new StringBuffer();
 108  4
         sb.append("ConfigResource");
 109  4
         sb.append("{resourceName='").append(resourceName).append('\'');
 110  4
         sb.append('}');
 111  4
         return sb.toString();
 112  
     }
 113  
 }