View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  /*
35   * The Apache Software License, Version 1.1
36   *
37   * Copyright (c) 2003 The Apache Software Foundation.  All rights
38   * reserved.
39   *
40   * Redistribution and use in source and binary forms, with or without
41   * modification, are permitted provided that the following conditions
42   * are met:
43   *
44   * 1. Redistributions of source code must retain the above copyright
45   *    notice, this list of conditions and the following disclaimer.
46   *
47   * 2. Redistributions in binary form must reproduce the above copyright
48   *    notice, this list of conditions and the following disclaimer in
49   *    the documentation and/or other materials provided with the
50   *    distribution.
51   *
52   * 3. The end-user documentation included with the redistribution, if
53   *    any, must include the following acknowlegement:
54   *       "This product includes software developed by the
55   *        Apache Software Foundation (http://www.apache.org/)."
56   *    Alternately, this acknowlegement may appear in the software itself,
57   *    if and wherever such third-party acknowlegements normally appear.
58   *
59   * 4. The names "The Jakarta Project", "Pluto", and "Apache Software
60   *    Foundation" must not be used to endorse or promote products derived
61   *    from this software without prior written permission. For written
62   *    permission, please contact apache@apache.org.
63   *
64   * 5. Products derived from this software may not be called "Apache"
65   *    nor may "Apache" appear in their names without prior written
66   *    permission of the Apache Group.
67   *
68   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
69   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
70   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
71   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
72   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
73   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
74   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
75   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
76   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
77   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
78   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
79   * SUCH DAMAGE.
80   * ====================================================================
81   *
82   * This software consists of voluntary contributions made by many
83   * individuals on behalf of the Apache Software Foundation.  For more
84   * information on the Apache Software Foundation, please see
85   * <http://www.apache.org/>.
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  * A simple XML Castor file based implementation of the <code>PortletEntityRegistryService</config>
124  * <p>This store persit the PortletEntityRegistry informations</p>
125  */
126 public class PortletEntityRegistryServiceFileImpl extends PortletEntityRegistryService
127 {
128     // Mapping
129     private Mapping _mapping;
130 
131     // Servlet Context
132     private ServletContext _servletContext;
133 
134     // Registry
135     private PortletApplicationEntityListImpl _registry;
136 
137     // Entity ressource name (resolved in init() method)    
138     private String _strEntityRessource;
139 
140     // Helper lists and hashtables to access the data as fast as possible
141     private final Map _mapPortletEntitiesKeyObjectID;
142 
143     /**
144      * Default constructor
145      */
146     public PortletEntityRegistryServiceFileImpl(  )
147     {
148         _mapPortletEntitiesKeyObjectID = new HashMap(  );
149     }
150 
151     /**
152          * @see org.apache.pluto.portalImpl.services.Service#init(javax.servlet.ServletConfig, org.apache.pluto.portalImpl.util.Properties)
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(  /*LutecePlutoConstant.LOG_CATEGORY,*/
176                 "Failed to load mapping file " + _mapping, e ); // FIXME
177             throw e;
178         }
179 
180         load(  );
181     }
182 
183     /**
184          * @see org.apache.pluto.portalImpl.services.portletentityregistry.PortletEntityRegistryService#getPortletApplicationEntityList()
185          */
186     public PortletApplicationEntityList getPortletApplicationEntityList(  )
187     {
188         return _registry;
189     }
190 
191     /**
192          * @see org.apache.pluto.portalImpl.services.portletentityregistry.PortletEntityRegistryService#getPortletEntity(org.apache.pluto.om.common.ObjectID)
193          */
194     public PortletEntity getPortletEntity( ObjectID id )
195     {
196         return (PortletEntity) _mapPortletEntitiesKeyObjectID.get( id.toString(  ) );
197     }
198 
199     /**
200          * @see org.apache.pluto.portalImpl.services.portletentityregistry.PortletEntityRegistryService#store()
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          * @see org.apache.pluto.portalImpl.services.portletentityregistry.PortletEntityRegistryService#load()
251          */
252     public void load(  ) throws IOException
253     {
254         internalLoad(  );
255 
256         if ( Log.isDebugEnabled(  /*LutecePlutoConstant.LOG_CATEGORY*/
257              ) )
258         {
259             Log.debug(  /*LutecePlutoConstant.LOG_CATEGORY,*/
260                 "Dumping complete object model description as it is read from the xml file..." ); // FIXME
261             Log.debug(  /*LutecePlutoConstant.LOG_CATEGORY,*/
262                 _registry.toString(  ) ); // FIXME
263         }
264 
265         fill(  );
266     }
267 
268     /**
269          * @see org.apache.pluto.portalImpl.services.portletentityregistry.PortletEntityRegistryService#refresh(org.apache.pluto.om.entity.PortletEntity)
270          */
271     public void refresh( PortletEntity portletEntity )
272     {
273         _mapPortletEntitiesKeyObjectID.put( portletEntity.getId(  ).toString(  ), portletEntity );
274     }
275 
276     /**
277      * Load portlet entity file
278      *
279      * @throws IOException For file access exception or any other error
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      * Agregate all entities definitions (only one for Lutece/Pluto)
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             // fill portletEntitiesKeyObjectID
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      * Return portlet entities defined for this web application
341      *
342      * @return portlet entities defined for this web application
343      */
344     public Map getPortletEntities(  )
345     {
346         return _mapPortletEntitiesKeyObjectID;
347     }
348 }