KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > ResourceHelper


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.util;
25
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.InputStreamReader JavaDoc;
29 import java.io.Reader JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 import org.apache.log4j.Logger;
34 import org.infoglue.cms.exception.ConfigurationError;
35
36
37 /**
38  * Utility class for loading/manipulating resources (must be accessible from the classpath).
39  *
40  * @author <a HREF="mailto:meat_for_the_butcher@yahoo.com">Patrik Nyborg</a>
41  */

42 public class ResourceHelper {
43   // --- [Constants] -----------------------------------------------------------
44

45   // The size of the buffer to use when working with I/O (4 kB).
46
private static final int CHAR_BUFFER_SIZE = 4096;
47
48
49
50   // --- [Attributes] ----------------------------------------------------------
51
// --- [Static] --------------------------------------------------------------
52

53   // The string manager for this package.
54
private static final StringManager sm = StringManagerFactory.getSystemStringManager(Constants.PACKAGE_NAME);
55
56   // The logger for this class.
57
private static final Logger logger = Logger.getLogger(RegexpHelper.class.getName());
58
59
60
61   // --- [Constructors] --------------------------------------------------------
62

63   /**
64    * Static class; don't allow instantiation.
65    */

66   private ResourceHelper() {}
67
68
69
70   // --- [Public] --------------------------------------------------------------
71

72   /**
73    * <p>Loads the specified property file into a Properties object.</p>
74    *
75    * <p>Example: <code>ResourceHelper.loadProperties("se/sprawl/cms/CMSProperties.properties");</code></p>
76    *
77    * @param name the name of the resource ("/"-separated path name).
78    */

79   public static synchronized Properties JavaDoc loadProperties(String JavaDoc name) {
80     logger.debug(sm.getString("resource.loadProperties.info", name));
81     try {
82       final Properties JavaDoc properties = new Properties JavaDoc();
83       properties.load(getResourceAsStream(name, ResourceHelper.class));
84       return properties;
85     } catch(Exception JavaDoc e) {
86       throw new ConfigurationError(sm.getString("resource.loadProperties.error", name), e);
87     }
88   }
89
90   /**
91    * <p>Loads the specified resource file and returns the content as a string.</p>
92    *
93    * <p>Example: <code>ResourceHelper.readResource("se/sprawl/cms/CMSSQL.sql");</code></p>
94    *
95    * @param name the name of the resource ("/"-separated path name).
96    */

97   public static synchronized String JavaDoc readResource(String JavaDoc name) {
98     logger.debug(sm.getString("resource.readResource.info", name));
99     try {
100       final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
101       final Reader JavaDoc reader = new InputStreamReader JavaDoc(getResourceAsStream(name, ResourceHelper.class));
102       char[] buf = new char[CHAR_BUFFER_SIZE];
103       int count = 0;
104       while((count = reader.read(buf, 0, CHAR_BUFFER_SIZE)) > 0) {
105         sb.append(buf, 0, count);
106       }
107       return sb.toString();
108     } catch(Exception JavaDoc e) {
109       throw new ConfigurationError(sm.getString("resource.readResource.error", name), e);
110     }
111   }
112
113     /**
114      * Find a resource with a variety of fallback ClassLoaders.
115      *
116      * @param resourceName The name of the resource to find. ("/"-separated path name)
117      * @param callingClass The class looking for the resource
118      * @return The InputStream for the given resource name, or null if one is not found
119      */

120     public static InputStream JavaDoc getResourceAsStream(String JavaDoc resourceName, Class JavaDoc callingClass)
121     {
122         URL JavaDoc url = null;
123
124         url = Thread.currentThread().getContextClassLoader().getResource(resourceName);
125
126         if (url == null)
127             url = ResourceHelper.class.getClassLoader().getResource(resourceName);
128
129         if (url == null)
130             url = callingClass.getClassLoader().getResource(resourceName);
131
132         try
133         {
134             return url != null ? url.openStream() : null;
135         }
136         catch (IOException JavaDoc e)
137         {
138             return null;
139         }
140     }
141 }
Popular Tags