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