Source Code Cross Referenced for IOUtils.java in  » ESB » mule » org » mule » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ESB » mule » org.mule.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: IOUtils.java 11338 2008-03-12 23:10:14Z aperepel $
003:         * --------------------------------------------------------------------------------------
004:         * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
005:         *
006:         * The software in this package is published under the terms of the CPAL v1.0
007:         * license, a copy of which has been included with this distribution in the
008:         * LICENSE.txt file.
009:         */
010:
011:        package org.mule.util;
012:
013:        import org.mule.config.i18n.CoreMessages;
014:
015:        import java.io.File;
016:        import java.io.IOException;
017:        import java.io.InputStream;
018:        import java.net.MalformedURLException;
019:        import java.net.URL;
020:        import java.security.AccessController;
021:        import java.security.PrivilegedAction;
022:
023:        import org.apache.commons.logging.Log;
024:        import org.apache.commons.logging.LogFactory;
025:
026:        // @ThreadSafe
027:        /**
028:         * Mule input/output utilities.
029:         */
030:        public class IOUtils extends org.apache.commons.io.IOUtils {
031:            /** Logger. */
032:            private static final Log logger = LogFactory.getLog(IOUtils.class);
033:
034:            /**
035:             * Attempts to load a resource from the file system, from a URL, or from the
036:             * classpath, in that order.
037:             * 
038:             * @param resourceName The name of the resource to load
039:             * @param callingClass The Class object of the calling object
040:             * @return the requested resource as a string
041:             * @throws java.io.IOException IO error
042:             */
043:            public static String getResourceAsString(final String resourceName,
044:                    final Class callingClass) throws IOException {
045:                InputStream is = getResourceAsStream(resourceName, callingClass);
046:                if (is != null) {
047:                    return toString(is);
048:                } else {
049:                    throw new IOException("Unable to load resource "
050:                            + resourceName);
051:                }
052:            }
053:
054:            /**
055:             * Attempts to load a resource from the file system, from a URL, or from the
056:             * classpath, in that order.
057:             * 
058:             * @param resourceName The name of the resource to load
059:             * @param callingClass The Class object of the calling object
060:             * @return an InputStream to the resource or null if resource not found
061:             * @throws java.io.IOException IO error
062:             */
063:            public static InputStream getResourceAsStream(
064:                    final String resourceName, final Class callingClass)
065:                    throws IOException {
066:                return getResourceAsStream(parseResourceName(resourceName),
067:                        callingClass, true, true);
068:            }
069:
070:            /**
071:             * Attempts to load a resource from the file system, from a URL, or from the
072:             * classpath, in that order.
073:             * 
074:             * @param resourceName The name of the resource to load
075:             * @param callingClass The Class object of the calling object
076:             * @param tryAsFile - try to load the resource from the local file system
077:             * @param tryAsUrl - try to load the resource as a URL
078:             * @return an InputStream to the resource or null if resource not found
079:             * @throws java.io.IOException IO error
080:             */
081:            public static InputStream getResourceAsStream(
082:                    final String resourceName, final Class callingClass,
083:                    boolean tryAsFile, boolean tryAsUrl) throws IOException {
084:
085:                URL url = getResourceAsUrl(resourceName, callingClass,
086:                        tryAsFile, tryAsUrl);
087:
088:                if (url == null) {
089:                    return null;
090:                } else {
091:                    return url.openStream();
092:                }
093:            }
094:
095:            /**
096:             * Attempts to load a resource from the file system or from the classpath, in
097:             * that order.
098:             * 
099:             * @param resourceName The name of the resource to load
100:             * @param callingClass The Class object of the calling object
101:             * @return an URL to the resource or null if resource not found
102:             */
103:            public static URL getResourceAsUrl(final String resourceName,
104:                    final Class callingClass) {
105:                return getResourceAsUrl(resourceName, callingClass, true, true);
106:            }
107:
108:            /**
109:             * Attempts to load a resource from the file system or from the classpath, in
110:             * that order.
111:             * 
112:             * @param resourceName The name of the resource to load
113:             * @param callingClass The Class object of the calling object
114:             * @param tryAsFile - try to load the resource from the local file system
115:             * @param tryAsUrl - try to load the resource as a Url string
116:             * @return an URL to the resource or null if resource not found
117:             */
118:            public static URL getResourceAsUrl(final String resourceName,
119:                    final Class callingClass, boolean tryAsFile,
120:                    boolean tryAsUrl) {
121:                if (resourceName == null) {
122:                    throw new IllegalArgumentException(CoreMessages
123:                            .objectIsNull("Resource name").getMessage());
124:                }
125:                URL url = null;
126:
127:                // Try to load the resource from the file system.
128:                if (tryAsFile) {
129:                    try {
130:                        File file = FileUtils.newFile(resourceName);
131:                        if (file.exists()) {
132:                            url = file.getAbsoluteFile().toURL();
133:                        } else {
134:                            logger
135:                                    .debug("Unable to load resource from the file system: "
136:                                            + file.getAbsolutePath());
137:                        }
138:                    } catch (Exception e) {
139:                        logger
140:                                .debug("Unable to load resource from the file system: "
141:                                        + e.getMessage());
142:                    }
143:                }
144:
145:                // Try to load the resource from the classpath.
146:                if (url == null) {
147:                    try {
148:                        url = (URL) AccessController
149:                                .doPrivileged(new PrivilegedAction() {
150:                                    public Object run() {
151:                                        return ClassUtils.getResource(
152:                                                resourceName, callingClass);
153:                                    }
154:                                });
155:                        if (url == null) {
156:                            logger.debug("Unable to load resource "
157:                                    + resourceName + " from the classpath");
158:                        }
159:                    } catch (Exception e) {
160:                        logger.debug("Unable to load resource " + resourceName
161:                                + " from the classpath: " + e.getMessage());
162:                    }
163:                }
164:
165:                if (url == null) {
166:                    try {
167:                        url = new URL(resourceName);
168:                    } catch (MalformedURLException e) {
169:                        //ignore
170:                    }
171:                }
172:                return url;
173:            }
174:
175:            /**
176:             * This method checks whether the name of the resource needs to be parsed. If it
177:             * is, it parses the name and tries to get the variable from the Environmental
178:             * Variables configured on the system.
179:             * 
180:             * @param src
181:             * @return
182:             */
183:            private static String parseResourceName(String src) {
184:                String var;
185:                String[] split;
186:                String ps = File.separator;
187:
188:                if (src.indexOf('$') > -1) {
189:                    split = src.split("}");
190:                } else {
191:                    return src;
192:                }
193:
194:                var = split[0].substring(2);
195:                var = SystemUtils.getenv(var);
196:                if (split.length > 1) {
197:                    if (var == null) {
198:                        var = System.getProperty(split[0].substring(2));
199:                        if (var == null) {
200:                            return split[1].substring(1);
201:                        } else {
202:                            return var + ps + split[1].substring(1);
203:                        }
204:                    } else {
205:                        return var + ps + split[1].substring(1);
206:                    }
207:                } else {
208:                    if (var == null) {
209:                        return "";
210:                    } else {
211:                        return var;
212:                    }
213:                }
214:            }
215:
216:            /**
217:             * This method wraps {@link org.apache.commons.io.IOUtils}' <code>toString(InputStream)</code>
218:             * method but catches any {@link IOException} and wraps it into a {@link RuntimeException}.
219:             */
220:            public static String toString(InputStream input) {
221:                try {
222:                    return org.apache.commons.io.IOUtils.toString(input);
223:                } catch (IOException iox) {
224:                    throw new RuntimeException(iox);
225:                }
226:            }
227:
228:            /**
229:             * This method wraps {@link org.apache.commons.io.IOUtils}' <code>toByteArray(InputStream)</code>
230:             * method but catches any {@link IOException} and wraps it into a {@link RuntimeException}.
231:             */
232:            public static byte[] toByteArray(InputStream input) {
233:                try {
234:                    return org.apache.commons.io.IOUtils.toByteArray(input);
235:                } catch (IOException iox) {
236:                    throw new RuntimeException(iox);
237:                }
238:            }
239:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.