Wiki source code of Development-Custom Resource Manager
Last modified by Pascal Robert on 2010/09/13 00:26
Show last authors
author | version | line-number | content |
---|---|---|---|
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 | {{code}} | ||
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 | |||
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 | |||
108 | * Last but not least, you need to take care of those funky WOURLValuedElementData so dataForResourceNamed will work: | ||
109 | |||
110 | {{code}} | ||
111 | |||
112 | public NSData dataForResourceNamed(String aResourceName) | ||
113 | { | ||
114 | this.bytesForResourceNamed( aResourceName, null, null ); | ||
115 | |||
116 | String aKey = this.urlForResourceNamed( aResourceName ).toString(); | ||
117 | WOURLValuedElementData anElement = (WOURLValuedElementData) _cache.get( aKey ); | ||
118 | |||
119 | return anElement.data(); | ||
120 | } | ||
121 | |||
122 | {{/code}} | ||
123 | |||
124 | * Finally, you need to register your resource manager with WOApplication: | ||
125 | |||
126 | {{code}} | ||
127 | |||
128 | anApplication.setResourceManager( new ResourceManager() ); | ||
129 | |||
130 | |||
131 | {{/code}} | ||
132 | |||
133 | Handling of languages and frameworks are left as an exercise to the reader. |