Petite Abeille
Here is a little example on how to write your own WOResourceManager:
...
- urlForResourceNamed simply build an URL for a resource from wherever you would like (eg a jar file). Unfortunately, urlForResourceNamed uses pathForResourceNamed so you will need to rewrite that also:
Panel |
---|
Wiki Markup |
Code Block |
private URL urlForResourceNamed(String aResourceName)
{
return this.getClass().getResource( aResourceName );
}
public String pathForResourceNamed(String aResourceName, String aFrameworkName, NSArray someLanguages)
{
URL anURL = this.urlForResourceNamed( aResourceName );
if ( anURL != _null_ )____{ ______return_ { return anURL.toExternalForm();____ }____ ____return_ return null;__ }__
__public_ String_ urlForResourceNamed(String_ aResourceName,_ String_ aFrameworkName,_ NSArray_ someLanguages,_ WORequest_ aRequest)__ {____String__anURL_=_ String anURL = super.urlForResourceNamed(_ aResourceName,_ aFrameworkName,_ someLanguages,_ aRequest_ );____ ____ this.bytesForResourceNamed(_ aResourceName,_ aFrameworkName,_ someLanguages_ );__ ____return_ return anURL;__ } |
- bytesForResourceNamed _ simply _ retrieve _ a _ resource _ from _ wherever _ you _ would _ like (eg _ a _ jar _ file):
Panel |
---|
Wiki Markup |
Code Block |
public InputStream inputStreamForResourceNamed(String_ aResourceName,_ String_ aFrameworkName,_ NSArray_ someLanguages)__ {____return_ return this.getClass().getResourceAsStream(_ aResourceName_ );__ }__
__public_ byte[]_ bytesForResourceNamed(String_ aResourceName,_ String_ aFrameworkName,_ NSArray_ someLanguages)__ {____if_(_aResourceName_ if ( aResourceName != null ) {
URL anURL = this.urlForResourceNamed( aResourceName );
if ( anURL != _null_ )______{ ________String______aKey_=_ { String aKey = anURL.toString();________WOURLValuedElementData__anElement_=_ WOURLValuedElementData anElement = (WOURLValuedElementData)_ _cache.get(_ aKey_ );________ ________if_(_anElement_==_null_) ________{ __________synchronized(_this_) __________{ ____________InputStream__anInputStream_=_this.inputStreamForResourceNamed(_aResourceName,_null,_null_); ____________ ____________if_(_anInputStream_ if ( anElement == null ) { synchronized( this ) { InputStream anInputStream = this.inputStreamForResourceNamed( aResourceName, null, null ); if ( anInputStream != null ) {
try
{
InputStream aBufferStream = new BufferedInputStream( anInputStream );
byte[ )] someBytes = new byte( [ aBufferStream.available() ];
aBufferStream.read( someBytes );
aBufferStream.close();
anInputStream.close();
{
Data someData = new Data( someBytes );
String aType = this.contentTypeForResourceNamed( aKey );
anElement = new WOURLValuedElementData( someData, aType, aKey );
_cache.put( aKey, anElement );
}
}
catch(Exception anException)
{
SZLog.warning( anException );
}
}
}
}
return ( (Data) anElement.data() ).bytesNoCopy();
}
SZLog.debug( "Null url for resource named '" + aResourceName + "'." );
return null;
}
throw new IllegalArgumentException ( "ResourceManager.bytesForResourceNamed: null resource name." );
} |
- Last but not least, you need to take care of those funky WOURLValuedElementData so dataForResourceNamed will work:
Panel |
---|
Code Block |
public NSData dataForResourceNamed(String aResourceName)
{
this.bytesForResourceNamed( aResourceName, null, null );
String aKey = this.urlForResourceNamed( aResourceName ).toString();
WOURLValuedElementData anElement = (WOURLValuedElementData) _cache.get( aKey );
return anElement.data();
}
|
- Finally, you need to register your resource manager with WOApplication:
Panelcode |
---|
anApplication.setResourceManager( new ResourceManager() ); |
Handling of languages and frameworks are left as an exercise to the reader.Category:WebObjects