ServletService.java

  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. import fr.paris.lutece.portal.service.init.LuteceInitException;
  36. import fr.paris.lutece.portal.service.plugin.Plugin;
  37. import fr.paris.lutece.portal.service.util.AppLogService;

  38. import java.util.ArrayList;
  39. import java.util.List;

  40. import javax.servlet.ServletContext;
  41. import javax.servlet.http.HttpServlet;

  42. /**
  43.  * ServletService
  44.  */
  45. public final class ServletService
  46. {
  47.     private static ServletService _singleton = new ServletService( );
  48.     private static ServletContext _context;
  49.     private List<LuteceServlet> _listServlets = new ArrayList<>( );

  50.     /**
  51.      * Private constructor
  52.      */
  53.     private ServletService( )
  54.     {
  55.     }

  56.     /**
  57.      * Return the unique instance
  58.      *
  59.      * @return The instance
  60.      */
  61.     public static ServletService getInstance( )
  62.     {
  63.         return _singleton;
  64.     }

  65.     /**
  66.      * Register a servlet
  67.      *
  68.      * @param entry
  69.      *            The servlet entry defined in the plugin's XML file
  70.      * @param plugin
  71.      *            The plugin
  72.      */
  73.     public void registerServlet( ServletEntry entry, Plugin plugin )
  74.     {
  75.         try
  76.         {
  77.             HttpServlet servlet = (HttpServlet) Class.forName( entry.getServletClass( ) ).newInstance( );
  78.             LuteceServlet s = new LuteceServlet( entry.getName( ), servlet, entry.getMappingUrlPattern( ), plugin, entry.getInitParameters( ) );
  79.             _listServlets.add( s );
  80.             AppLogService.info( "New Servlet registered : {}", entry.getName( ) );

  81.             for ( String strKey : entry.getInitParameters( ).keySet( ) )
  82.             {
  83.                 AppLogService.info( " * init parameter - name : '{}' - value : '{}'", strKey, entry.getInitParameters( ).get( strKey ) );
  84.             }
  85.         }
  86.         catch( InstantiationException | IllegalAccessException | ClassNotFoundException e )
  87.         {
  88.             AppLogService.error( "Error registering a servlet : {}", e.getMessage( ), e );
  89.         }
  90.     }

  91.     /**
  92.      * Defines the servlet context used by the ServletConfig given to the servlets
  93.      *
  94.      * @param servletContext
  95.      *            The context
  96.      */
  97.     public static void setServletContext( ServletContext servletContext )
  98.     {
  99.         _context = servletContext;
  100.     }

  101.     /**
  102.      * Initializes servlets
  103.      *
  104.      * @param context
  105.      *            The context
  106.      * @throws LuteceInitException
  107.      *             If an error occurs
  108.      */
  109.     public static void init( ServletContext context ) throws LuteceInitException
  110.     {
  111.         AppLogService.info( "Initialize plugins servlets" );
  112.         _context = context;

  113.         for ( LuteceServlet servlet : ServletService.getInstance( ).getServlets( ) )
  114.         {
  115.             // Catch exception for each servlet to execute all chain
  116.             try
  117.             {
  118.                 if ( servlet.getPlugin( ).isInstalled( ) )
  119.                 {
  120.                     // Create a ServletConfig wrapper to provide init parameters to the servlet
  121.                     LuteceServletConfig servletConfig = new LuteceServletConfig( servlet.getName( ), _context, servlet.getInitParameters( ) );
  122.                     servlet.getServlet( ).init( servletConfig );
  123.                     AppLogService.info( " * servlet '{}' from plugin {} initialized.", servlet.getName( ), servlet.getPlugin( ).getName( ) );
  124.                 }
  125.             }
  126.             catch( Exception e )
  127.             {
  128.                 AppLogService.error( "Error execution init() method - Servlet {}", servlet.getName( ), e );
  129.                 throw new LuteceInitException( "Error execution init() method - Servlet " + servlet.getName( ), e );
  130.             }
  131.         }
  132.     }

  133.     /**
  134.      * Gives the servlets list
  135.      *
  136.      * @return The list of servlets
  137.      */
  138.     public List<LuteceServlet> getServlets( )
  139.     {
  140.         return _listServlets;
  141.     }
  142. }