1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.portal.service.resource;
35
36 import fr.paris.lutece.portal.service.cache.AbstractCacheableService;
37 import fr.paris.lutece.portal.service.util.AppLogService;
38 import fr.paris.lutece.portal.service.util.AppPropertiesService;
39
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.Iterator;
43 import java.util.List;
44 import java.util.StringTokenizer;
45
46
47
48
49
50
51 public abstract class ResourceService extends AbstractCacheableService
52 {
53
54 private static final String DELIMITER = ",";
55 private static final String UNDEFINED_SERVICE_NAME = "Undefined Service Name";
56
57
58 private String _strName = UNDEFINED_SERVICE_NAME;
59 private List<ResourceLoader> _listLoaders = new ArrayList<>( );
60
61
62
63
64 protected ResourceService( )
65 {
66 String strLoadersProperty = getLoadersProperty( );
67
68 if ( ( strLoadersProperty != null ) && ( !strLoadersProperty.equals( "" ) ) )
69 {
70 initLoaders( strLoadersProperty );
71 }
72 else
73 {
74 AppLogService.error( "Resource service : Loaders property key is missing" );
75 }
76 }
77
78
79
80
81
82
83
84 protected abstract String getLoadersProperty( );
85
86
87
88
89
90
91
92 protected void initLoaders( String strKey )
93 {
94 String strLoaders = AppPropertiesService.getProperty( strKey );
95 StringTokenizer st = new StringTokenizer( strLoaders, DELIMITER );
96
97 while ( st.hasMoreTokens( ) )
98 {
99 String strLoaderClassName = st.nextToken( );
100 addLoader( strLoaderClassName );
101 }
102 }
103
104
105
106
107
108
109
110 protected void setName( String strName )
111 {
112 _strName = strName;
113 }
114
115
116
117
118
119
120 public String getName( )
121 {
122 return _strName;
123 }
124
125
126
127
128
129
130
131 protected void setNameKey( String strKey )
132 {
133 setName( AppPropertiesService.getProperty( strKey, UNDEFINED_SERVICE_NAME ) );
134 }
135
136
137
138
139
140
141
142 protected void setCacheKey( String strKey )
143 {
144 String strCache = AppPropertiesService.getProperty( strKey, "false" );
145
146 if ( strCache.equals( "true" ) )
147 {
148 initCache( getName( ) );
149 }
150 }
151
152
153
154
155
156
157
158 protected void addLoader( String strLoaderClassName )
159 {
160 try
161 {
162 ResourceLoader/../../../fr/paris/lutece/portal/service/resource/ResourceLoader.html#ResourceLoader">ResourceLoader loader = (ResourceLoader) Class.forName( strLoaderClassName ).newInstance( );
163 _listLoaders.add( loader );
164 }
165 catch( IllegalAccessException | InstantiationException | ClassNotFoundException e )
166 {
167 AppLogService.error( e.getMessage( ), e );
168 }
169 }
170
171
172
173
174
175
176
177
178 protected Resource getResource( String strId )
179 {
180 Resource resource = null;
181
182 if ( isCacheEnable( ) )
183 {
184 resource = (Resource) getFromCache( strId );
185
186 if ( resource == null )
187 {
188 resource = loadResource( strId );
189
190 if ( resource != null )
191 {
192 putInCache( strId, resource );
193 }
194 }
195 }
196 else
197 {
198 resource = loadResource( strId );
199 }
200
201 return resource;
202 }
203
204
205
206
207
208
209
210
211 private Resource loadResource( String strId )
212 {
213 Resource resource = null;
214 Iterator<ResourceLoader> i = _listLoaders.iterator( );
215
216 while ( i.hasNext( ) && ( resource == null ) )
217 {
218 ResourceLoader loader = i.next( );
219 resource = loader.getResource( strId );
220 }
221
222 return resource;
223 }
224
225
226
227
228
229
230 protected Collection<Resource> getResources( )
231 {
232 List<Resource> listResources = new ArrayList<>( );
233 Iterator<ResourceLoader> i = _listLoaders.iterator( );
234
235 while ( i.hasNext( ) )
236 {
237 ResourceLoader loader = i.next( );
238 Collection<Resource> colResources = loader.getResources( );
239 Iterator<Resource> j = colResources.iterator( );
240
241 while ( j.hasNext( ) )
242 {
243 Resource resource = j.next( );
244 listResources.add( resource );
245 }
246 }
247
248 return listResources;
249 }
250 }