Wiki source code of Development-Custom Resource Manager
Last modified by Pascal Robert on 2010/09/13 00:26
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
3.1 | 1 | === Petite Abeille === |
![]() |
1.1 | 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 | |||
![]() |
3.1 | 9 | {{code}} |
![]() |
1.1 | 10 | |
![]() |
3.1 | 11 | private URL urlForResourceNamed(String aResourceName) |
12 | { | ||
13 | return this.getClass().getResource( aResourceName ); | ||
14 | } | ||
![]() |
1.1 | 15 | |
![]() |
3.1 | 16 | public String pathForResourceNamed(String aResourceName, String aFrameworkName, NSArray someLanguages) |
17 | { | ||
18 | URL anURL = this.urlForResourceNamed( aResourceName ); | ||
![]() |
1.1 | 19 | |
![]() |
3.1 | 20 | if ( anURL != null ) |
21 | { | ||
22 | return anURL.toExternalForm(); | ||
23 | } | ||
![]() |
1.1 | 24 | |
![]() |
3.1 | 25 | return null; |
26 | } | ||
![]() |
1.1 | 27 | |
![]() |
3.1 | 28 | public String urlForResourceNamed(String aResourceName, String aFrameworkName, NSArray someLanguages, WORequest aRequest) |
29 | { | ||
30 | String anURL = super.urlForResourceNamed( aResourceName, aFrameworkName, someLanguages, aRequest ); | ||
![]() |
1.1 | 31 | |
![]() |
3.1 | 32 | this.bytesForResourceNamed( aResourceName, aFrameworkName, someLanguages ); |
![]() |
1.1 | 33 | |
![]() |
3.1 | 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 | |||
![]() |
1.1 | 108 | * Last but not least, you need to take care of those funky WOURLValuedElementData so dataForResourceNamed will work: |
109 | |||
![]() |
3.1 | 110 | {{code}} |
![]() |
1.1 | 111 | |
![]() |
3.1 | 112 | public NSData dataForResourceNamed(String aResourceName) |
113 | { | ||
114 | this.bytesForResourceNamed( aResourceName, null, null ); | ||
![]() |
1.1 | 115 | |
![]() |
3.1 | 116 | String aKey = this.urlForResourceNamed( aResourceName ).toString(); |
117 | WOURLValuedElementData anElement = (WOURLValuedElementData) _cache.get( aKey ); | ||
![]() |
1.1 | 118 | |
![]() |
3.1 | 119 | return anElement.data(); |
120 | } | ||
121 | |||
122 | {{/code}} | ||
123 | |||
![]() |
1.1 | 124 | * Finally, you need to register your resource manager with WOApplication: |
125 | |||
![]() |
3.1 | 126 | {{code}} |
![]() |
1.1 | 127 | |
128 | anApplication.setResourceManager( new ResourceManager() ); | ||
129 | |||
130 | |||
![]() |
3.1 | 131 | {{/code}} |
132 | |||
![]() |
1.1 | 133 | Handling of languages and frameworks are left as an exercise to the reader. |