Petite Abeille

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

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

  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;
__}
__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();
________WOURLValuedElementData__anElement_=_(WOURLValuedElementData)__cache.get(_aKey_);
________
________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." );
  }
  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();
  }

anApplication.setResourceManager( new ResourceManager() );

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

Category:WebObjects