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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 package fr.paris.lutece.plugins.jsr168.pluto.services.portletentityregistry;
88
89 import fr.paris.lutece.plugins.jsr168.pluto.LutecePlutoConstant;
90
91 import org.apache.pluto.om.common.ObjectID;
92 import org.apache.pluto.om.entity.PortletApplicationEntity;
93 import org.apache.pluto.om.entity.PortletApplicationEntityList;
94 import org.apache.pluto.om.entity.PortletEntity;
95 import org.apache.pluto.portalImpl.om.entity.impl.PortletApplicationEntityListImpl;
96 import org.apache.pluto.portalImpl.services.log.Log;
97 import org.apache.pluto.portalImpl.services.portletentityregistry.PortletEntityRegistryService;
98 import org.apache.pluto.portalImpl.util.Properties;
99
100 import org.exolab.castor.mapping.Mapping;
101 import org.exolab.castor.mapping.MappingException;
102 import org.exolab.castor.xml.MarshalException;
103 import org.exolab.castor.xml.Marshaller;
104 import org.exolab.castor.xml.Unmarshaller;
105 import org.exolab.castor.xml.ValidationException;
106
107 import org.xml.sax.InputSource;
108
109 import java.io.File;
110 import java.io.FileWriter;
111 import java.io.IOException;
112 import java.io.InputStream;
113
114 import java.util.HashMap;
115 import java.util.Iterator;
116 import java.util.Map;
117
118 import javax.servlet.ServletConfig;
119 import javax.servlet.ServletContext;
120
121
122
123
124
125
126 public class PortletEntityRegistryServiceFileImpl extends PortletEntityRegistryService
127 {
128
129 private Mapping _mapping;
130
131
132 private ServletContext _servletContext;
133
134
135 private PortletApplicationEntityListImpl _registry;
136
137
138 private String _strEntityRessource;
139
140
141 private final Map _mapPortletEntitiesKeyObjectID;
142
143
144
145
146 public PortletEntityRegistryServiceFileImpl( )
147 {
148 _mapPortletEntitiesKeyObjectID = new HashMap( );
149 }
150
151
152
153
154 public void init( final ServletConfig servletConfig, final Properties properties )
155 throws Exception
156 {
157 _servletContext = servletConfig.getServletContext( );
158
159 _strEntityRessource = properties.getString( LutecePlutoConstant.CONFIG_SERVICES_ENTITYXML_RESSOURCE,
160 LutecePlutoConstant.CONFIG_SERVICES_ENTITYXML_RESSOURCE_DEFAULT );
161
162 final String entityMapping = properties.getString( LutecePlutoConstant.CONFIG_SERVICES_ENTITYXML_MAPPING,
163 LutecePlutoConstant.CONFIG_SERVICES_ENTITYXML_MAPPING_DEFAULT );
164 final InputStream entityMappingStream = _servletContext.getResourceAsStream( entityMapping );
165 final InputSource entityMappingSource = new InputSource( entityMappingStream );
166
167 _mapping = new Mapping( );
168
169 try
170 {
171 _mapping.loadMapping( entityMappingSource );
172 }
173 catch ( Exception e )
174 {
175 Log.error(
176 "Failed to load mapping file " + _mapping, e );
177 throw e;
178 }
179
180 load( );
181 }
182
183
184
185
186 public PortletApplicationEntityList getPortletApplicationEntityList( )
187 {
188 return _registry;
189 }
190
191
192
193
194 public PortletEntity getPortletEntity( ObjectID id )
195 {
196 return (PortletEntity) _mapPortletEntitiesKeyObjectID.get( id.toString( ) );
197 }
198
199
200
201
202 public void store( ) throws IOException
203 {
204 String filename = _strEntityRessource;
205
206 File f = new File( filename );
207
208 if ( !f.isAbsolute( ) )
209 {
210 filename = _servletContext.getRealPath( filename );
211 }
212
213 FileWriter writer = new FileWriter( filename );
214
215 try
216 {
217 final Marshaller marshaller = new Marshaller( writer );
218
219 marshaller.setMapping( _mapping );
220
221 _registry.preStore( null );
222
223 marshaller.marshal( _registry );
224
225 _registry.postStore( null );
226 }
227 catch ( final MappingException e )
228 {
229 Log.error( LutecePlutoConstant.LOG_CATEGORY, e );
230 throw new IOException( e.toString( ) );
231 }
232 catch ( final ValidationException e )
233 {
234 Log.error( LutecePlutoConstant.LOG_CATEGORY, e );
235 throw new IOException( e.toString( ) );
236 }
237 catch ( final MarshalException e )
238 {
239 Log.error( LutecePlutoConstant.LOG_CATEGORY, e );
240 throw new IOException( e.toString( ) );
241 }
242 catch ( final Exception e )
243 {
244 Log.error( LutecePlutoConstant.LOG_CATEGORY, e );
245 throw new IOException( e.toString( ) );
246 }
247 }
248
249
250
251
252 public void load( ) throws IOException
253 {
254 internalLoad( );
255
256 if ( Log.isDebugEnabled(
257 ) )
258 {
259 Log.debug(
260 "Dumping complete object model description as it is read from the xml file..." );
261 Log.debug(
262 _registry.toString( ) );
263 }
264
265 fill( );
266 }
267
268
269
270
271 public void refresh( PortletEntity portletEntity )
272 {
273 _mapPortletEntitiesKeyObjectID.put( portletEntity.getId( ).toString( ), portletEntity );
274 }
275
276
277
278
279
280
281 private void internalLoad( ) throws IOException
282 {
283 final String filename = _strEntityRessource;
284 final InputStream fileStream = _servletContext.getResourceAsStream( filename );
285 final InputSource fileSource = new InputSource( fileStream );
286
287 try
288 {
289 final Unmarshaller unmarshaller = new Unmarshaller( _mapping );
290 _registry = (PortletApplicationEntityListImpl) unmarshaller.unmarshal( fileSource );
291 _registry.postLoad( null );
292 _registry.preBuild( null );
293 _registry.postBuild( null );
294 }
295 catch ( MappingException e )
296 {
297 Log.error( LutecePlutoConstant.LOG_CATEGORY, e );
298 throw new IOException( e.toString( ) );
299 }
300 catch ( ValidationException e )
301 {
302 Log.error( LutecePlutoConstant.LOG_CATEGORY, e );
303 throw new IOException( e.toString( ) );
304 }
305 catch ( MarshalException e )
306 {
307 Log.error( LutecePlutoConstant.LOG_CATEGORY, e );
308 throw new IOException( e.toString( ) );
309 }
310 catch ( Exception e )
311 {
312 Log.error( LutecePlutoConstant.LOG_CATEGORY, e );
313 throw new IOException( e.toString( ) );
314 }
315 }
316
317
318
319
320 private void fill( )
321 {
322 final Iterator iterator = _registry.iterator( );
323
324 while ( iterator.hasNext( ) )
325 {
326 final PortletApplicationEntity appInst = (PortletApplicationEntity) iterator.next( );
327
328
329 final Iterator portlets = appInst.getPortletEntityList( ).iterator( );
330
331 while ( portlets.hasNext( ) )
332 {
333 final PortletEntity portletInst = (PortletEntity) portlets.next( );
334 _mapPortletEntitiesKeyObjectID.put( portletInst.getId( ).toString( ), portletInst );
335 }
336 }
337 }
338
339
340
341
342
343
344 public Map getPortletEntities( )
345 {
346 return _mapPortletEntitiesKeyObjectID;
347 }
348 }