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

Show last authors
1 === Petite Abeille ===
2
3 Here is a little example on how to write your own WOResourceManager:
4
5 The main methods to implement are urlForResourceNamed() and bytesForResourceNamed().
6
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
9 {{panel}}
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 __}
36
37 {{/panel}}
38
39 * //bytesForResourceNamed//simply//retrieve//a//resource//from//wherever//you//would//like//(eg//a//jar//file):
40
41 {{panel}}
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 }
103
104 {{/panel}}
105
106 * Last but not least, you need to take care of those funky WOURLValuedElementData so dataForResourceNamed will work:
107
108 {{panel}}
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 }
119
120 {{/panel}}
121
122 * Finally, you need to register your resource manager with WOApplication:
123
124 {{panel}}
125
126 anApplication.setResourceManager( new ResourceManager() );
127
128 {{/panel}}
129
130 Handling of languages and frameworks are left as an exercise to the reader.
131
132 Category:WebObjects