001    package org.shiftone.jrat.util.io;
002    
003    
004    import org.shiftone.jrat.core.JRatException;
005    import org.shiftone.jrat.util.Assert;
006    import org.shiftone.jrat.util.log.Logger;
007    
008    import java.io.*;
009    import java.util.Hashtable;
010    import java.util.Map;
011    import java.util.Properties;
012    
013    
014    /**
015     * Class ResourceUtil
016     *
017     * @author jeff@shiftone.org (Jeff Drost)
018     */
019    public class ResourceUtil {
020    
021        private static final Logger LOG = Logger.getLogger(ResourceUtil.class);
022        private static ClassLoader CLASS_LOADER = ResourceUtil.class.getClassLoader();
023        private static Map resourceCache = new Hashtable();
024    
025        static {
026            if (CLASS_LOADER == null) {
027                CLASS_LOADER = Class.class.getClassLoader();
028            }
029        }
030    
031        public static Object newInstance(String className) {
032    
033            Class klass = null;
034            Object instance = null;
035    
036            Assert.assertNotNull("className", className);
037            LOG.debug("newInstance(" + className + ")");
038    
039            try {
040                klass = CLASS_LOADER.loadClass(className);
041            }
042            catch (Exception e) {
043                throw new JRatException("unable to getPreferences class '" + className + "'", e);
044            }
045    
046            try {
047                instance = klass.newInstance();
048            }
049            catch (Exception e) {
050                throw new JRatException("unable to instantiate '" + className + "'", e);
051            }
052    
053            return instance;
054        }
055    
056    
057        public static InputStream loadResourceAsStream(String resourceName) {
058    
059            InputStream inputStream = null;
060    
061            LOG.info("getPreferences resource : " + resourceName);
062            Assert.assertNotNull("resourceName", resourceName);
063    
064            inputStream = CLASS_LOADER.getResourceAsStream(resourceName);
065    
066            if (inputStream == null) {
067                LOG.info("resource not found on classpath, trying to open as file");
068    
069                try {
070                    inputStream = new FileInputStream(resourceName);
071    
072                    LOG.debug("resource opened as file");
073                }
074                catch (Exception e) {
075                    throw new JRatException("unable to locate resource : " + resourceName);
076                }
077            } else {
078                LOG.debug("resource opened from classpath");
079            }
080    
081            return inputStream;
082        }
083    
084    
085        public static byte[] loadResourceAsBytes(String resourceName) {
086    
087            InputStream inputStream = loadResourceAsStream(resourceName);
088            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
089    
090            try {
091                IOUtil.copy(inputStream, outputStream);
092            }
093            finally {
094                IOUtil.close(inputStream);
095            }
096    
097            return outputStream.toByteArray();
098        }
099    
100    
101        private static String fetchResource(String name) {
102    
103            Reader reader = null;
104            StringBuffer sb = null;
105            InputStream inputStream = null;
106            int c = 0;
107            char[] buffer = new char[1025 * 1];
108    
109            Assert.assertNotNull("name", name);
110            LOG.debug("fetchResource : " + name);
111    
112            inputStream = loadResourceAsStream(name);
113            reader = new InputStreamReader(inputStream);
114            sb = new StringBuffer();
115    
116            try {
117                for (c = 0; c >= 0; c = reader.read(buffer)) {
118                    sb.append(buffer, 0, c);
119                }
120            }
121            catch (IOException e) {
122                throw new JRatException("unable to read resource data : " + name, e);
123            }
124    
125            return sb.toString();
126        }
127    
128    
129        public static String loadResource(String name) {
130    
131            String resource = null;
132    
133            Assert.assertNotNull("name", name);
134    
135            resource = (String) resourceCache.get(name);
136    
137            if (resource == null) {
138                LOG.info("loading and caching resource : " + name);
139    
140                resource = fetchResource(name);
141    
142                resourceCache.put(name, resource);
143            }
144    
145            return resource;
146        }
147    
148    
149        public static Properties getResourceAsProperties(String name) {
150    
151            InputStream inputStream = null;
152            Properties props = null;
153    
154            Assert.assertNotNull("name", name);
155            LOG.debug("getResourceAsProperties : " + name);
156    
157            inputStream = loadResourceAsStream(name);
158            props = new Properties();
159    
160            try {
161                props.load(inputStream);
162            }
163            catch (Exception e) {
164                throw new JRatException("unable to getPreferences properties from resource : " + name, e);
165            }
166    
167            return props;
168        }
169    }