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  package fr.paris.lutece.plugins.jsr168.pluto.services.portletdefinitionregistry;
35  
36  import fr.paris.lutece.plugins.jsr168.pluto.LutecePlutoConstant;
37  
38  import org.apache.pluto.om.common.ObjectID;
39  import org.apache.pluto.om.portlet.PortletApplicationDefinition;
40  import org.apache.pluto.om.portlet.PortletApplicationDefinitionList;
41  import org.apache.pluto.om.portlet.PortletDefinition;
42  import org.apache.pluto.portalImpl.om.common.impl.DescriptionImpl;
43  import org.apache.pluto.portalImpl.om.common.impl.DescriptionSetImpl;
44  import org.apache.pluto.portalImpl.om.common.impl.DisplayNameImpl;
45  import org.apache.pluto.portalImpl.om.common.impl.DisplayNameSetImpl;
46  import org.apache.pluto.portalImpl.om.common.impl.LanguageSetImpl;
47  import org.apache.pluto.portalImpl.om.common.impl.ParameterImpl;
48  import org.apache.pluto.portalImpl.om.common.impl.PreferenceImpl;
49  import org.apache.pluto.portalImpl.om.common.impl.PreferenceSetImpl;
50  import org.apache.pluto.portalImpl.om.portlet.impl.ContentTypeImpl;
51  import org.apache.pluto.portalImpl.om.portlet.impl.PortletApplicationDefinitionImpl;
52  import org.apache.pluto.portalImpl.om.portlet.impl.PortletApplicationDefinitionListImpl;
53  import org.apache.pluto.portalImpl.om.portlet.impl.PortletDefinitionImpl;
54  import org.apache.pluto.portalImpl.om.servlet.impl.WebApplicationDefinitionImpl;
55  import org.apache.pluto.portalImpl.services.log.Log;
56  import org.apache.pluto.portalImpl.services.portletdefinitionregistry.PortletDefinitionRegistryService;
57  import org.apache.pluto.portalImpl.util.Properties;
58  import org.apache.pluto.portalImpl.xml.XmlParser;
59  
60  import org.exolab.castor.mapping.Mapping;
61  import org.exolab.castor.xml.Unmarshaller;
62  
63  import org.w3c.dom.Document;
64  
65  import org.xml.sax.InputSource;
66  
67  import java.io.InputStream;
68  
69  import java.util.HashMap;
70  import java.util.Iterator;
71  import java.util.Map;
72  import java.util.Vector;
73  
74  import javax.servlet.ServletConfig;
75  import javax.servlet.ServletContext;
76  
77  
78  /**
79   * A simple XML Castor file based implementation of the <code>PortletRegistryService</config>
80   * <p>This store persit the PortletRegistry informations</p>
81   */
82  public class PortletDefinitionRegistryServiceFileImpl extends PortletDefinitionRegistryService
83  {
84      // web.xml ressource name (resolved in init() method)    
85      private String _webXmlRessource;
86  
87      // portlet.xml ressource name (resolved in init() method)    
88      private String _portletXmlRessource;
89  
90      // Servlet Context
91      private ServletContext _servletContext;
92  
93      // Helper lists and hashtables to access the data as fast as possible
94      // List containing all portlet applications available in the system
95      private final PortletApplicationDefinitionListImpl _registry;
96      private final Map _mapPortletsKeyObjectId;
97  
98      /**
99       * Default constructor
100      */
101     public PortletDefinitionRegistryServiceFileImpl(  )
102     {
103         _registry = new PortletApplicationDefinitionListImpl(  );
104         _mapPortletsKeyObjectId = new HashMap(  );
105     }
106 
107     /**
108          * @see org.apache.pluto.portalImpl.services.Service#init(javax.servlet.ServletConfig, org.apache.pluto.portalImpl.util.Properties)
109          */
110     public void init( final ServletConfig config, final Properties properties )
111         throws Exception
112     {
113         _servletContext = config.getServletContext(  );
114 
115         // get web xml mapping file
116         _webXmlRessource = properties.getString( LutecePlutoConstant.CONFIG_SERVICES_PORTLETDEF_WEBXML_RESSOURCE,
117                 LutecePlutoConstant.CONFIG_SERVICES_PORTLETDEF_WEBXML_RESSOURCE_DEFAULT );
118 
119         final String webMapping = properties.getString( LutecePlutoConstant.CONFIG_SERVICES_PORTLETDEF_WEBXML_MAPPING,
120                 LutecePlutoConstant.CONFIG_SERVICES_PORTLETDEF_WEBXML_MAPPING_DEFAULT );
121         final InputStream webMappingStream = _servletContext.getResourceAsStream( webMapping );
122         final InputSource webMappingSource = new InputSource( webMappingStream );
123         final Mapping mappingWebXml = new Mapping(  );
124 
125         try
126         {
127             mappingWebXml.loadMapping( webMappingSource );
128         }
129         catch ( Exception e )
130         {
131             Log.error(  /*LutecePlutoConstant.LOG_CATEGORY,*/
132                 "Failed to load mapping file " + webMapping, e ); // FIXME
133             throw e;
134         }
135 
136         // get portlet xml mapping file
137         _portletXmlRessource = properties.getString( LutecePlutoConstant.CONFIG_SERVICES_PORTLETDEF_PORTLETXML_RESSOURCE,
138                 LutecePlutoConstant.CONFIG_SERVICES_PORTLETDEF_PORTLETXML_RESSOURCE_DEFAULT );
139 
140         final String portletMapping = properties.getString( LutecePlutoConstant.CONFIG_SERVICES_PORTLETDEF_PORTLETXML_MAPPING,
141                 LutecePlutoConstant.CONFIG_SERVICES_PORTLETDEF_PORTLETXML_MAPPING_DEFAULT );
142         final InputStream portletMappingStream = _servletContext.getResourceAsStream( portletMapping );
143         final InputSource portletMappingSource = new InputSource( portletMappingStream );
144         final Mapping mappingPortletXml = new Mapping(  );
145 
146         try
147         {
148             mappingPortletXml.loadMapping( portletMappingSource );
149         }
150         catch ( Exception e )
151         {
152             Log.error(  /*LutecePlutoConstant.LOG_CATEGORY,*/
153                 "Failed to load mapping file " + portletMapping, e ); // FIXME
154             throw e;
155         }
156 
157         load( mappingWebXml, mappingPortletXml );
158         fill(  );
159     }
160 
161     /**
162          * @see org.apache.pluto.portalImpl.services.portletdefinitionregistry.PortletDefinitionRegistryService#getPortletApplicationDefinitionList()
163          */
164     public PortletApplicationDefinitionList getPortletApplicationDefinitionList(  )
165     {
166         return _registry;
167     }
168 
169     /**
170          * @see org.apache.pluto.portalImpl.services.portletdefinitionregistry.PortletDefinitionRegistryService#getPortletDefinition(org.apache.pluto.om.common.ObjectID)
171          */
172     public PortletDefinition getPortletDefinition( final ObjectID id )
173     {
174         return (PortletDefinition) _mapPortletsKeyObjectId.get( id );
175     }
176 
177     /**
178      * Load <code>web.xml</code> and <code>portlet.xml</code> deploiement descriptor.
179      *
180      * @param mappingWebXml castor mapping for read <code>web.xml</code>
181      * @param mappingPortletXml castor mapping for read <code>portlet.xml</code>
182      * @throws Exception for any exception
183      */
184     private void load( final Mapping mappingWebXml, final Mapping mappingPortletXml )
185         throws Exception
186     {
187         final String webModule = LutecePlutoConstant.WEBAPP;
188 
189         Unmarshaller unmarshaller;
190 
191         if ( Log.isDebugEnabled(  /*LutecePlutoConstant.LOG_CATEGORY */
192              ) )
193         {
194             Log.debug(  /*LutecePlutoConstant.LOG_CATEGORY,*/
195                 "Loading the portlet applications XML file..." ); // FIXME
196         }
197 
198         final InputStream portletXmlIS = _servletContext.getResourceAsStream( _portletXmlRessource );
199         final Document portletDocument = XmlParser.parsePortletXml( portletXmlIS );
200         unmarshaller = new Unmarshaller( mappingPortletXml );
201         unmarshaller.setIgnoreExtraElements( true );
202         unmarshaller.setIgnoreExtraAttributes( true );
203 
204         final PortletApplicationDefinitionImpl portletApp = (PortletApplicationDefinitionImpl) unmarshaller.unmarshal( portletDocument );
205 
206         final InputStream webXmlIS = _servletContext.getResourceAsStream( _webXmlRessource );
207         final Document webDocument = XmlParser.parseWebXml( webXmlIS );
208         unmarshaller = new Unmarshaller( mappingWebXml );
209         unmarshaller.setIgnoreExtraElements( true );
210         unmarshaller.setIgnoreExtraAttributes( true );
211 
212         final WebApplicationDefinitionImpl webApp = (WebApplicationDefinitionImpl) unmarshaller.unmarshal( webDocument );
213 
214         final Vector<Object> structure = new Vector<Object>(  );
215         structure.add( portletApp );
216         structure.add( "/" + webModule );
217         webApp.postLoad( structure );
218 
219         // refill structure with necessary information
220         webApp.preBuild( structure );
221         webApp.postBuild( structure );
222 
223         Log.debug(  /*LutecePlutoConstant.LOG_CATEGORY,*/
224             webApp.toString(  ) ); // FIXME
225         Log.debug(  /*LutecePlutoConstant.LOG_CATEGORY,*/
226             portletApp.toString(  ) ); // FIXME
227 
228         _registry.add( portletApp );
229 
230         if ( Log.isDebugEnabled(  /*LutecePlutoConstant.LOG_CATEGORY */
231              ) )
232         {
233             Log.debug(  /*LutecePlutoConstant.LOG_CATEGORY,*/
234                 "Dumping content of web.xml..." ); // FIXME
235             Log.debug(  /*LutecePlutoConstant.LOG_CATEGORY,*/
236                 webApp.toString(  ) ); // FIXME
237 
238             Log.debug(  /* LutecePlutoConstant.LOG_CATEGORY,*/
239                 "Dumping content of portlet.xml..." ); // FIXME
240             Log.debug(  /*LutecePlutoConstant.LOG_CATEGORY,*/
241                 portletApp.toString(  ) ); // FIXME
242         }
243     }
244 
245     /**
246      * Find all portlet definition from <code>portlet.xml</code> descriptors.
247      */
248     private void fill(  )
249     {
250         Iterator iterator = _registry.iterator(  );
251 
252         while ( iterator.hasNext(  ) )
253         {
254             PortletApplicationDefinition papp = (PortletApplicationDefinition) iterator.next(  );
255 
256             // fill portletsKeyObjectId
257             Iterator portlets = papp.getPortletDefinitionList(  ).iterator(  );
258 
259             while ( portlets.hasNext(  ) )
260             {
261                 PortletDefinition portlet = (PortletDefinition) portlets.next(  );
262                 _mapPortletsKeyObjectId.put( portlet.getId(  ), portlet );
263             }
264         }
265     }
266 }