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.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
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
58
59 private ServletService( )
60 {
61 }
62
63
64
65
66
67 public static ServletService getInstance( )
68 {
69 return _singleton;
70 }
71
72
73
74
75
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
109
110
111 public static void setServletContext( ServletContext servletContext )
112 {
113 _context = servletContext;
114 }
115
116
117
118
119
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
129 try
130 {
131 if ( servlet.getPlugin( ).isInstalled( ) )
132 {
133
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
151
152
153 public List<LuteceServlet> getServlets( )
154 {
155 return _listServlets;
156 }
157 }