Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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):
__public_InputStream_
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:
unmigrated-wiki-markup
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