(1) Where is the ContextClassloader?
(2) Why do you assume that the ResourceLoader is loaded by the same
classloader as the calling class?
Sharma, Siddharth wrote:
Try using this file for loading resources.
Currently it loads only as properties and String but you may choose to
return anyway you want.
import java.util.Properties;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
/**
* Uses the classloader to load resources from the file system
*/
public class ResourceLoader
{
/**
* Useful int to define a buffer size
*/
private static final int aBuffSize = 123123;
/**
* inputstream reader int
*/
private static int k;
/**
* Useful array for copying data
*/
private static byte[] buff = new byte[aBuffSize];
/**
* The XML String to return
*/
private static String strXml;
/**
* Returns the resource as a java.util.Properties object
* @param resourceBundleName String
* @param strPath String
* @throws IOException
* @return Properties
*/
public static final Properties getPropertiesFromResourceBundle(
String resourceBundleName,
String strPath )
throws IOException
{
InputStream inputStream = null;
Properties properties = null;
try
{
// fetch the properties file as a stream
try
{
inputStream = getResourceAsStream(
resourceBundleName,
strPath );
}
catch ( IOException objIoe )
{
throw objIoe;
}
// if the resource was found, load it into the
result Properties object
if( inputStream != null )
{
try
{
properties = new Properties();
properties.load( inputStream );
}
catch( IOException e )
{
StringBuffer strBuff = new
StringBuffer(
CPSConfigurationManager.getPropertiesFromResourceBundle() caught
IOException when trying to load properties file );
strBuff.append( resourceBundleName
);
strBuff.append( : );
strBuff.append( e.getMessage() );
throw new IOException(
strBuff.toString() );
}
}
}
catch(IOException ioe)
{
throw ioe;
}
finally {
try
{
if(inputStream != null)
{
inputStream.close();
}
}
catch( IOException ex )
{
throw ex;
}
}
return properties;
} // end of method getPropertiesFromResourceBundle()
/**
* Returns the resource as a String object
* @param resourceBundleName String
* @param strPath String
* @throws IOException
* @return String
*/
public static final String getXmlFromResourceBundle(
String resourceBundleName,
String strPath ) throws IOException
{
OutputStream outputStream = null;
InputStream inputStream = null;
try {
// fetch the xml file as a stream
try {
inputStream = getResourceAsStream(
resourceBundleName,
strPath );
}
catch ( IOException objIoe )
{
throw objIoe;
}
// if the resource was found, load it
if( inputStream != null )
{
try
{
// I wish I could just do this:
strXml = inputStream.toString();
outputStream = new
ByteArrayOutputStream(aBuffSize);
synchronized ( outputStream )
{
while (
(k=inputStream.read(buff) ) != -1)
{
outputStream.write(
buff, 0, k);
}
// I can now grab the string I
want
strXml =
outputStream.toString();
outputStream.close();
return strXml;
} // end of synchronized block
// That was a lot of work to pull a
String out of an inputStream.
}
catch( IOException e )
{
throw new IOException(
PSConfigurationManager.getPropertiesFromResourceBundle() caught IOException
when trying to load properties file + e.getMessage() );
}
}
}
catch( IOException ioe )
{
throw ioe;
}
finally
{
try
{
inputStream.close();
}
catch( IOException ex )
{
throw ex;
}
}
return ;
} // end of method getPropertiesFromResourceBundle()
/**
* Returns the resource as a stream. Uses this class classloader to
find the resource
* relative to this class. Since this class is loaded by the
classloader of the
* calling class (say servlet), the resource will be searched
realtive to the calling
* class
*/
private static final InputStream getResourceAsStream( String
resourceBundleName,
String strPath )
throws IOException
{
InputStream inputStream = null;
try {
// null safety check
if( resourceBundleName == null || strPath == null)
{
throw new IOException(
ResourceLoader.getResourceAsStream() called with null filename );
}
StringBuffer resourceBundlePath = new
StringBuffer(strPath);
resourceBundlePath.append(/);
resourceBundlePath.append( resourceBundleName );
ClassLoader loader =
ResourceLoader.class.getClassLoader();
inputStream