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.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
80
81
82 public class PortletDefinitionRegistryServiceFileImpl extends PortletDefinitionRegistryService
83 {
84
85 private String _webXmlRessource;
86
87
88 private String _portletXmlRessource;
89
90
91 private ServletContext _servletContext;
92
93
94
95 private final PortletApplicationDefinitionListImpl _registry;
96 private final Map _mapPortletsKeyObjectId;
97
98
99
100
101 public PortletDefinitionRegistryServiceFileImpl( )
102 {
103 _registry = new PortletApplicationDefinitionListImpl( );
104 _mapPortletsKeyObjectId = new HashMap( );
105 }
106
107
108
109
110 public void init( final ServletConfig config, final Properties properties )
111 throws Exception
112 {
113 _servletContext = config.getServletContext( );
114
115
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(
132 "Failed to load mapping file " + webMapping, e );
133 throw e;
134 }
135
136
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(
153 "Failed to load mapping file " + portletMapping, e );
154 throw e;
155 }
156
157 load( mappingWebXml, mappingPortletXml );
158 fill( );
159 }
160
161
162
163
164 public PortletApplicationDefinitionList getPortletApplicationDefinitionList( )
165 {
166 return _registry;
167 }
168
169
170
171
172 public PortletDefinition getPortletDefinition( final ObjectID id )
173 {
174 return (PortletDefinition) _mapPortletsKeyObjectId.get( id );
175 }
176
177
178
179
180
181
182
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(
192 ) )
193 {
194 Log.debug(
195 "Loading the portlet applications XML file..." );
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
220 webApp.preBuild( structure );
221 webApp.postBuild( structure );
222
223 Log.debug(
224 webApp.toString( ) );
225 Log.debug(
226 portletApp.toString( ) );
227
228 _registry.add( portletApp );
229
230 if ( Log.isDebugEnabled(
231 ) )
232 {
233 Log.debug(
234 "Dumping content of web.xml..." );
235 Log.debug(
236 webApp.toString( ) );
237
238 Log.debug(
239 "Dumping content of portlet.xml..." );
240 Log.debug(
241 portletApp.toString( ) );
242 }
243 }
244
245
246
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
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 }