View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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.portal.service.servlet;
35  
36  import fr.paris.lutece.portal.service.init.LuteceInitException;
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.portal.service.util.AppLogService;
39  
40  import java.util.ArrayList;
41  import java.util.List;
42  
43  import javax.servlet.ServletContext;
44  import javax.servlet.http.HttpServlet;
45  
46  /**
47   * ServletService
48   */
49  public final class ServletService
50  {
51      private static ServletServicevlet/ServletService.html#ServletService">ServletService _singleton = new ServletService( );
52      private static ServletContext _context;
53      private List<LuteceServlet> _listServlets = new ArrayList<>( );
54  
55      /**
56       * Private constructor
57       */
58      private ServletService( )
59      {
60      }
61  
62      /**
63       * Return the unique instance
64       * 
65       * @return The instance
66       */
67      public static ServletService getInstance( )
68      {
69          return _singleton;
70      }
71  
72      /**
73       * Register a servlet
74       * 
75       * @param entry
76       *            The servlet entry defined in the plugin's XML file
77       * @param plugin
78       *            The plugin
79       */
80      public void registerServlet( ServletEntry entry, Plugin plugin )
81      {
82          try
83          {
84              HttpServlet servlet = (HttpServlet) Class.forName( entry.getServletClass( ) ).newInstance( );
85              LuteceServletervice/servlet/LuteceServlet.html#LuteceServlet">LuteceServlet s = new LuteceServlet( entry.getName( ), servlet, entry.getMappingUrlPattern( ), plugin, entry.getInitParameters( ) );
86              _listServlets.add( s );
87              AppLogService.info( "New Servlet registered : {}", entry.getName( ) );
88  
89              for ( String strKey : entry.getInitParameters( ).keySet( ) )
90              {
91                  AppLogService.info( " * init parameter - name : '{}' - value : '{}'", strKey, entry.getInitParameters( ).get( strKey ) );
92              }
93          }
94          catch( InstantiationException | IllegalAccessException | ClassNotFoundException e )
95          {
96              AppLogService.error( "Error registering a servlet : {}", e.getMessage( ), e );
97          }
98      }
99  
100     /**
101      * Defines the servlet context used by the ServletConfig given to the servlets
102      * 
103      * @param servletContext
104      *            The context
105      */
106     public static void setServletContext( ServletContext servletContext )
107     {
108         _context = servletContext;
109     }
110 
111     /**
112      * Initializes servlets
113      * 
114      * @param context
115      *            The context
116      * @throws LuteceInitException
117      *             If an error occurs
118      */
119     public static void init( ServletContext context ) throws LuteceInitException
120     {
121         AppLogService.info( "Initialize plugins servlets" );
122         _context = context;
123 
124         for ( LuteceServlet servlet : ServletService.getInstance( ).getServlets( ) )
125         {
126             // Catch exception for each servlet to execute all chain
127             try
128             {
129                 if ( servlet.getPlugin( ).isInstalled( ) )
130                 {
131                     // Create a ServletConfig wrapper to provide init parameters to the servlet
132                     LuteceServletConfigeceServletConfig.html#LuteceServletConfig">LuteceServletConfig servletConfig = new LuteceServletConfig( servlet.getName( ), _context, servlet.getInitParameters( ) );
133                     servlet.getServlet( ).init( servletConfig );
134                     AppLogService.info( " * servlet '{}' from plugin {} initialized.", servlet.getName( ), servlet.getPlugin( ).getName( ) );
135                 }
136             }
137             catch( Exception e )
138             {
139                 AppLogService.error( "Error execution init() method - Servlet {}", servlet.getName( ), e );
140                 throw new LuteceInitException( "Error execution init() method - Servlet " + servlet.getName( ), e );
141             }
142         }
143     }
144 
145     /**
146      * Gives the servlets list
147      * 
148      * @return The list of servlets
149      */
150     public List<LuteceServlet> getServlets( )
151     {
152         return _listServlets;
153     }
154 }