1 10 11 package org.mule.util; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.net.MalformedURLException ; 17 import java.net.URL ; 18 import java.security.AccessController ; 19 import java.security.PrivilegedAction ; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.mule.config.i18n.Message; 24 import org.mule.config.i18n.Messages; 25 26 30 public class IOUtils extends org.apache.commons.io.IOUtils 31 { 32 33 private static final Log logger = LogFactory.getLog(IOUtils.class); 34 35 44 public static String getResourceAsString(final String resourceName, final Class callingClass) 45 throws IOException 46 { 47 InputStream is = getResourceAsStream(resourceName, callingClass); 48 if (is != null) 49 { 50 return toString(is); 51 } 52 else 53 { 54 throw new IOException ("Unable to load resource " + resourceName); 55 } 56 } 57 58 67 public static InputStream getResourceAsStream(final String resourceName, final Class callingClass) 68 throws IOException 69 { 70 return getResourceAsStream(resourceName, callingClass, true, true); 71 } 72 73 84 public static InputStream getResourceAsStream(final String resourceName, 85 final Class callingClass, 86 boolean tryAsFile, 87 boolean tryAsUrl) throws IOException 88 { 89 90 URL url = getResourceAsUrl(resourceName, callingClass, tryAsFile); 91 92 if ((url == null) && (tryAsUrl)) 94 { 95 try 96 { 97 url = new URL (resourceName); 98 } 99 catch (MalformedURLException e) 100 { 101 logger.debug("Unable to load resource as a URL: " + resourceName); 102 } 103 } 104 105 if (url == null) 106 { 107 return null; 108 } 109 else 110 { 111 return url.openStream(); 112 } 113 } 114 115 123 public static URL getResourceAsUrl(final String resourceName, final Class callingClass) 124 { 125 return getResourceAsUrl(resourceName, callingClass, true); 126 } 127 128 137 public static URL getResourceAsUrl(final String resourceName, final Class callingClass, boolean tryAsFile) 138 { 139 if (resourceName == null) 140 { 141 throw new IllegalArgumentException (new Message(Messages.X_IS_NULL, "Resource name").getMessage()); 142 } 143 URL url = null; 144 145 if (tryAsFile) 147 { 148 try 149 { 150 File file = FileUtils.newFile(resourceName); 151 if (file.exists()) 152 { 153 url = file.getAbsoluteFile().toURL(); 154 } 155 else 156 { 157 logger.debug("Unable to load resource from the file system: " + file.getAbsolutePath()); 158 } 159 } 160 catch (Exception e) 161 { 162 logger.debug("Unable to load resource from the file system: " + e.getMessage()); 163 } 164 } 165 166 if (url == null) 168 { 169 try 170 { 171 url = (URL )AccessController.doPrivileged(new PrivilegedAction () 172 { 173 public Object run() 174 { 175 return ClassUtils.getResource(resourceName, callingClass); 176 } 177 }); 178 if (url == null) 179 { 180 logger.debug("Unable to load resource from the classpath"); 181 } 182 } 183 catch (Exception e) 184 { 185 logger.debug("Unable to load resource from the classpath: " + e.getMessage()); 186 } 187 } 188 189 return url; 190 } 191 } 192 | Popular Tags |