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
52 public abstract class ResourceService extends AbstractCacheableService
53 {
54
55 private static final String DELIMITER = ",";
56 private static final String UNDEFINED_SERVICE_NAME = "Undefined Service Name";
57
58
59 private String _strName = UNDEFINED_SERVICE_NAME;
60 private List<ResourceLoader> _listLoaders = new ArrayList<ResourceLoader>( );
61
62
63
64
65 protected ResourceService( )
66 {
67 String strLoadersProperty = getLoadersProperty( );
68
69 if ( ( strLoadersProperty != null ) && ( !strLoadersProperty.equals( "" ) ) )
70 {
71 initLoaders( strLoadersProperty );
72
73
74 }
75 else
76 {
77 AppLogService.error( "Resource service : Loaders property key is missing" );
78 }
79 }
80
81
82
83
84
85
86 protected abstract String getLoadersProperty( );
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 protected void setName( String strName )
109 {
110 _strName = strName;
111 }
112
113
114
115
116
117 public String getName( )
118 {
119 return _strName;
120 }
121
122
123
124
125
126 protected void setNameKey( String strKey )
127 {
128 setName( AppPropertiesService.getProperty( strKey, UNDEFINED_SERVICE_NAME ) );
129 }
130
131
132
133
134
135 protected void setCacheKey( String strKey )
136 {
137 String strCache = AppPropertiesService.getProperty( strKey, "false" );
138
139 if ( strCache.equals( "true" ) )
140 {
141 initCache( getName( ) );
142 }
143 }
144
145
146
147
148
149 protected void addLoader( String strLoaderClassName )
150 {
151 try
152 {
153 ResourceLoader loader = (ResourceLoader) Class.forName( strLoaderClassName ).newInstance( );
154 _listLoaders.add( loader );
155 }
156 catch ( IllegalAccessException e )
157 {
158 AppLogService.error( e.getMessage( ), e );
159 }
160 catch ( InstantiationException e )
161 {
162 AppLogService.error( e.getMessage( ), e );
163 }
164 catch ( ClassNotFoundException e )
165 {
166 AppLogService.error( e.getMessage( ), e );
167 }
168 }
169
170
171
172
173
174
175 protected Resource getResource( String strId )
176 {
177 Resource resource = null;
178
179 if ( isCacheEnable( ) )
180 {
181 resource = (Resource) getFromCache( strId );
182
183 if ( resource == null )
184 {
185 resource = loadResource( strId );
186
187 if ( resource != null )
188 {
189 putInCache( strId, resource );
190 }
191 }
192 }
193 else
194 {
195 resource = loadResource( strId );
196 }
197
198 return resource;
199 }
200
201
202
203
204
205
206 private Resource loadResource( String strId )
207 {
208 Resource resource = null;
209 Iterator<ResourceLoader> i = _listLoaders.iterator( );
210
211 while ( i.hasNext( ) && ( resource == null ) )
212 {
213 ResourceLoader loader = (ResourceLoader) i.next( );
214 resource = loader.getResource( strId );
215 }
216
217 return resource;
218 }
219
220
221
222
223
224 protected Collection<Resource> getResources( )
225 {
226 List<Resource> listResources = new ArrayList<Resource>( );
227 Iterator<ResourceLoader> i = _listLoaders.iterator( );
228
229 while ( i.hasNext( ) )
230 {
231 ResourceLoader loader = (ResourceLoader) i.next( );
232 Collection<Resource> colResources = loader.getResources( );
233 Iterator<Resource> j = colResources.iterator( );
234
235 while ( j.hasNext( ) )
236 {
237 Resource resource = (Resource) j.next( );
238 listResources.add( resource );
239 }
240 }
241
242 return listResources;
243 }
244 }