Last modified by Pascal Robert on 2010/09/13 00:26

From version 1.1
edited by smmccraw
on 2007/07/08 09:45
Change comment: There is no comment for this version
To version 5.1
edited by Pascal Robert
on 2010/09/13 00:26
Change comment: Migrated to Confluence 4.0

Summary

Details

Page properties
Title
... ... @@ -1,1 +1,1 @@
1 -Programming__WebObjects-Web Applications-Development-Custom Resource Manager
1 +Development-Custom Resource Manager
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.smmccraw
1 +XWiki.probert
Content
... ... @@ -1,4 +1,4 @@
1 -=== Petite Abeille ===
1 +=== Petite Abeille ===
2 2  
3 3  Here is a little example on how to write your own WOResourceManager:
4 4  
... ... @@ -6,127 +6,128 @@
6 6  
7 7  * 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:
8 8  
9 -{{panel}}
9 +{{code}}
10 10  
11 - private URL urlForResourceNamed(String aResourceName)
12 - {
13 - return this.getClass().getResource( aResourceName );
14 - }
15 -
16 - public String pathForResourceNamed(String aResourceName, String aFrameworkName, NSArray someLanguages)
17 - {
18 - URL anURL = this.urlForResourceNamed( aResourceName );
19 -
20 - if ( anURL !=_null_)
21 -____{
22 -______return_anURL.toExternalForm();
23 -____}
24 -____
25 -____return_null;
26 -__}
27 -__
28 -__public_String_urlForResourceNamed(String_aResourceName,_String_aFrameworkName,_NSArray_someLanguages,_WORequest_aRequest)
29 -__{
30 -____String__anURL_=_super.urlForResourceNamed(_aResourceName,_aFrameworkName,_someLanguages,_aRequest_);
31 -____
32 -____this.bytesForResourceNamed(_aResourceName,_aFrameworkName,_someLanguages_);
33 -__
34 -____return_anURL;
35 -__}
11 + private URL urlForResourceNamed(String aResourceName)
12 + {
13 + return this.getClass().getResource( aResourceName );
14 + }
36 36  
37 -{{/panel}}
16 + public String pathForResourceNamed(String aResourceName, String aFrameworkName, NSArray someLanguages)
17 + {
18 + URL anURL = this.urlForResourceNamed( aResourceName );
38 38  
39 -* //bytesForResourceNamed//simply//retrieve//a//resource//from//wherever//you//would//like//(eg//a//jar//file):
20 + if ( anURL != null )
21 + {
22 + return anURL.toExternalForm();
23 + }
40 40  
41 -{{panel}}
25 + return null;
26 + }
42 42  
43 -__public_InputStream_inputStreamForResourceNamed(String_aResourceName,_String_aFrameworkName,_NSArray_someLanguages)
44 -__{
45 -____return_this.getClass().getResourceAsStream(_aResourceName_);
46 -__}
47 -__
48 -__public_byte[]_bytesForResourceNamed(String_aResourceName,_String_aFrameworkName,_NSArray_someLanguages)
49 -__{
50 -____if_(_aResourceName_!= null )
51 - {
52 - URL anURL = this.urlForResourceNamed( aResourceName );
53 -
54 - if ( anURL !=_null_)
55 -______{
56 -________String______aKey_=_anURL.toString();
57 -________WOURLValuedElementData__anElement_=_(WOURLValuedElementData)__cache.get(_aKey_);
58 -________
59 -________if_(_anElement_==_null_)
60 -________{
61 -__________synchronized(_this_)
62 -__________{
63 -____________InputStream__anInputStream_=_this.inputStreamForResourceNamed(_aResourceName,_null,_null_);
64 -____________
65 -____________if_(_anInputStream_!= null )
66 - {
67 - try
68 - {
69 - InputStream aBufferStream = new BufferedInputStream( anInputStream );
70 - byte[) someBytes = new byte( aBufferStream.available() ];
71 -
72 - aBufferStream.read( someBytes );
73 - aBufferStream.close();
74 - anInputStream.close();
75 -
76 - {
77 - Data someData = new Data( someBytes );
78 - String aType = this.contentTypeForResourceNamed( aKey );
79 -
80 - anElement = new WOURLValuedElementData( someData, aType, aKey );
81 -
82 - _cache.put( aKey, anElement );
83 - }
84 - }
85 - catch(Exception anException)
86 - {
87 - SZLog.warning( anException );
88 - }
89 - }
90 - }
91 - }
92 -
93 - return ( (Data) anElement.data() ).bytesNoCopy();
94 - }
95 -
96 - SZLog.debug( "Null url for resource named '" + aResourceName + "'." );
97 -
98 - return null;
99 - }
100 -
101 - throw new IllegalArgumentException ( "ResourceManager.bytesForResourceNamed: null resource name." );
102 - }
28 + public String urlForResourceNamed(String aResourceName, String aFrameworkName, NSArray someLanguages, WORequest aRequest)
29 + {
30 + String anURL = super.urlForResourceNamed( aResourceName, aFrameworkName, someLanguages, aRequest );
103 103  
104 -{{/panel}}
32 + this.bytesForResourceNamed( aResourceName, aFrameworkName, someLanguages );
105 105  
34 + return anURL;
35 + }
36 +
37 +
38 +{{/code}}
39 +
40 +* bytesForResourceNamed simply retrieve a resource from wherever you would like (eg a jar file):
41 +
42 +{{code}}
43 +
44 +public InputStream inputStreamForResourceNamed(String aResourceName, String aFrameworkName, NSArray someLanguages)
45 + {
46 + return this.getClass().getResourceAsStream( aResourceName );
47 + }
48 +
49 + public byte[] bytesForResourceNamed(String aResourceName, String aFrameworkName, NSArray someLanguages)
50 + {
51 + if ( aResourceName != null )
52 + {
53 + URL anURL = this.urlForResourceNamed( aResourceName );
54 +
55 + if ( anURL != null )
56 + {
57 + String aKey = anURL.toString();
58 + WOURLValuedElementData anElement = (WOURLValuedElementData) _cache.get( aKey );
59 +
60 + if ( anElement == null )
61 + {
62 + synchronized( this )
63 + {
64 + InputStream anInputStream = this.inputStreamForResourceNamed( aResourceName, null, null );
65 +
66 + if ( anInputStream != null )
67 + {
68 + try
69 + {
70 + InputStream aBufferStream = new BufferedInputStream( anInputStream );
71 + byte[] someBytes = new byte[ aBufferStream.available() ];
72 +
73 + aBufferStream.read( someBytes );
74 + aBufferStream.close();
75 + anInputStream.close();
76 +
77 + {
78 + Data someData = new Data( someBytes );
79 + String aType = this.contentTypeForResourceNamed( aKey );
80 +
81 + anElement = new WOURLValuedElementData( someData, aType, aKey );
82 +
83 + _cache.put( aKey, anElement );
84 + }
85 + }
86 + catch(Exception anException)
87 + {
88 + SZLog.warning( anException );
89 + }
90 + }
91 + }
92 + }
93 +
94 + return ( (Data) anElement.data() ).bytesNoCopy();
95 + }
96 +
97 + SZLog.debug( "Null url for resource named '" + aResourceName + "'." );
98 +
99 + return null;
100 + }
101 +
102 + throw new IllegalArgumentException ( "ResourceManager.bytesForResourceNamed: null resource name." );
103 + }
104 +
105 +
106 +{{/code}}
107 +
106 106  * Last but not least, you need to take care of those funky WOURLValuedElementData so dataForResourceNamed will work:
107 107  
108 -{{panel}}
110 +{{code}}
109 109  
110 - public NSData dataForResourceNamed(String aResourceName)
111 - {
112 - this.bytesForResourceNamed( aResourceName, null, null );
113 -
114 - String aKey = this.urlForResourceNamed( aResourceName ).toString();
115 - WOURLValuedElementData anElement = (WOURLValuedElementData) _cache.get( aKey );
116 -
117 - return anElement.data();
118 - }
112 +public NSData dataForResourceNamed(String aResourceName)
113 + {
114 + this.bytesForResourceNamed( aResourceName, null, null );
119 119  
120 -{{/panel}}
116 + String aKey = this.urlForResourceNamed( aResourceName ).toString();
117 + WOURLValuedElementData anElement = (WOURLValuedElementData) _cache.get( aKey );
121 121  
119 + return anElement.data();
120 + }
121 +
122 +{{/code}}
123 +
122 122  * Finally, you need to register your resource manager with WOApplication:
123 123  
124 -{{panel}}
126 +{{code}}
125 125  
126 126   anApplication.setResourceManager( new ResourceManager() );
127 127  
128 -{{/panel}}
129 129  
130 -Handling of languages and frameworks are left as an exercise to the reader.
131 +{{/code}}
131 131  
132 -Category:WebObjects
133 +Handling of languages and frameworks are left as an exercise to the reader.