You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Petite Abeille

Here is a little example on how to write your own WOResourceManager:

The main methods to implement are urlForResourceNamed() and bytesForResourceNamed().

  • 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:

private URL urlForResourceNamed(String aResourceName)

Unknown macro: { return this.getClass().getResource( aResourceName ); }

public String pathForResourceNamed(String aResourceName, String aFrameworkName, NSArray someLanguages)
{
URL anURL = this.urlForResourceNamed( aResourceName );

if ( anURL !=null)
____

Unknown macro: {______return_anURL.toExternalForm();____}

____
____return_null;
__}
__
__public_String_urlForResourceNamed(String_aResourceName,_String_aFrameworkName,_NSArray_someLanguages,_WORequest_aRequest)
__

Unknown macro: {____String__anURL_=_super.urlForResourceNamed(_aResourceName,_aFrameworkName,_someLanguages,_aRequest_);________this.bytesForResourceNamed(_aResourceName,_aFrameworkName,_someLanguages_);______return_anURL;__}
  • bytesForResourceNamed_simply_retrieve_a_resource_from_wherever_you_would_like(eg_a_jar_file):

__public_InputStream_inputStreamForResourceNamed(String_aResourceName,_String_aFrameworkName,_NSArray_someLanguages)
__

Unknown macro: {____return_this.getClass().getResourceAsStream(_aResourceName_);__}

__
__public_byte[]_bytesForResourceNamed(String_aResourceName,_String_aFrameworkName,_NSArray_someLanguages)
__{
___if(aResourceName!= null )
{
URL anURL = this.urlForResourceNamed( aResourceName );

if ( anURL !=null)
______{
_______String____aKey=_anURL.toString();
_______WOURLValuedElementDataanElement=(WOURLValuedElementData)cache.get(_aKey);
________
_______if(anElement==null)
________{
_________synchronized(_this)
__________{
___________InputStreamanInputStream=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();

Unknown macro: { Data someData = new Data( someBytes ); String aType = this.contentTypeForResourceNamed( aKey ); anElement = new WOURLValuedElementData( someData, aType, aKey ); _cache.put( aKey, anElement ); }

}
catch(Exception anException)

Unknown macro: { 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:

public NSData dataForResourceNamed(String aResourceName)

Unknown macro: { 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:

anApplication.setResourceManager( new ResourceManager() );

Handling of languages and frameworks are left as an exercise to the reader.

Category:WebObjects

  • No labels