Fixed logger usage.
[guacamole.git] / src / main / java / net / sourceforge / guacamole / net / basic / WebSocketSupportLoader.java
1 package net.sourceforge.guacamole.net.basic;
2
3 /*
4  *  Guacamole - Clientless Remote Desktop
5  *  Copyright (C) 2010  Michael Jumper
6  *
7  *  This program is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU Affero General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU Affero General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Affero General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23 import javax.servlet.Servlet;
24 import javax.servlet.ServletContext;
25 import javax.servlet.ServletContextEvent;
26 import javax.servlet.ServletContextListener;
27 import net.sourceforge.guacamole.GuacamoleException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Simple ServletContextListener which loads a WebSocket tunnel implementation
33  * if available, using the Servlet 3.0 API to dynamically load and install
34  * the tunnel servlet.
35  * 
36  * Note that because Guacamole depends on the Servlet 2.5 API, and 3.0 may
37  * not be available or needed if WebSocket is not desired, the 3.0 API is
38  * detected and invoked dynamically via reflection.
39  * 
40  * @author Michael Jumper
41  */
42 public class WebSocketSupportLoader implements ServletContextListener {
43
44     private Logger logger = LoggerFactory.getLogger(WebSocketSupportLoader.class);
45
46     @Override
47     public void contextDestroyed(ServletContextEvent sce) {
48     }
49
50     @Override
51     public void contextInitialized(ServletContextEvent sce) {
52
53         try {
54
55             // Attempt to find WebSocket servlet
56             Class<Servlet> servlet = (Class<Servlet>) GuacamoleClassLoader.getInstance().findClass(
57                 "net.sourceforge.guacamole.net.basic.BasicGuacamoleWebSocketTunnelServlet"
58             ); 
59
60             // Dynamically add servlet IF SERVLET 3.0 API AVAILABLE!
61             try {
62
63                 // Get servlet registration class
64                 Class regClass = Class.forName("javax.servlet.ServletRegistration");
65
66                 // Get and invoke addServlet()
67                 Method addServlet = ServletContext.class.getMethod("addServlet", String.class, Class.class);
68                 Object reg = addServlet.invoke(sce.getServletContext(), "WebSocketTunnel", servlet);
69
70                 // Get and invoke addMapping()
71                 Method addMapping = regClass.getMethod("addMapping", String[].class);
72                 addMapping.invoke(reg, (Object) new String[]{"/websocket-tunnel"});
73
74                 // If we succesfully load and register the WebSocket tunnel servlet,
75                 // WebSocket is supported.
76                 logger.info("WebSocket support found and loaded.");
77
78             }
79
80             // Servlet API 3.0 unsupported
81             catch (ClassNotFoundException e) {
82                 logger.info("Servlet API 3.0 not found.", e);
83             }
84             catch (NoSuchMethodException e) {
85                 logger.warn("Servlet API 3.0 found, but incomplete.", e);
86             }
87
88             // Servlet API 3.0 found, but errors during use
89             catch (IllegalAccessException e) {
90                 logger.error("Unable to load WebSocket tunnel servlet.", e);
91             }
92             catch (InvocationTargetException e) {
93                 logger.error("Internal error loading WebSocket tunnel servlet.", e);
94             }
95
96         }
97
98         // If no such servlet class, WebSocket support not present
99         catch (ClassNotFoundException e) {
100             logger.info("WebSocket support not found.");
101         }
102
103         // Log all GuacamoleExceptions
104         catch (GuacamoleException e) {
105             logger.error("Unable to load/detect WebSocket support.", e);
106         }
107
108     }
109
110 }
111