Programming__WebObjects-Web Applications-Development-Custom Resource Manager

Version 2.1 by smmccraw on 2007/07/08 09:45

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)
  {
    return this.getClass().getResource( aResourceName );
  }
 
  public String pathForResourceNamed(String aResourceName, String aFrameworkName, NSArray someLanguages)
  {
    URL  anURL = this.urlForResourceNamed( aResourceName );
   
    if ( anURL !=_null_)
{
return_anURL.toExternalForm();
}

return_null;
}

public_String_urlForResourceNamed(String_aResourceName,_String_aFrameworkName,_NSArray_someLanguages,_WORequest_aRequest)
{
String
anURL_=_super.urlForResourceNamed(_aResourceName,_aFrameworkName,_someLanguages,_aRequest_);

this.bytesForResourceNamed(_aResourceName,_aFrameworkName,_someLanguages_);

return_anURL;
}

  • bytesForResourceNamedsimplyretrievearesourcefromwhereveryouwouldlike(egajarfile):

public_InputStream_inputStreamForResourceNamed(String_aResourceName,_String_aFrameworkName,_NSArray_someLanguages)
{
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();
   
                {
                  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:

  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:

  anApplication.setResourceManager( new ResourceManager() );

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

Category:WebObjects