1 package net.sourceforge.guacamole.net.basic;
4 * Guacamole - Clientless Remote Desktop
5 * Copyright (C) 2010 Michael Jumper
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.
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.
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/>.
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;
32 * Simple ServletContextListener which loads a WebSocket tunnel implementation
33 * if available, using the Servlet 3.0 API to dynamically load and install
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.
40 * @author Michael Jumper
42 public class WebSocketSupportLoader implements ServletContextListener {
44 private Logger logger = LoggerFactory.getLogger(WebSocketSupportLoader.class);
47 public void contextDestroyed(ServletContextEvent sce) {
51 public void contextInitialized(ServletContextEvent sce) {
55 // Attempt to find WebSocket servlet
56 Class<Servlet> servlet = (Class<Servlet>) GuacamoleClassLoader.getInstance().findClass(
57 "net.sourceforge.guacamole.net.basic.BasicGuacamoleWebSocketTunnelServlet"
60 // Dynamically add servlet IF SERVLET 3.0 API AVAILABLE!
63 // Get servlet registration class
64 Class regClass = Class.forName("javax.servlet.ServletRegistration");
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);
70 // Get and invoke addMapping()
71 Method addMapping = regClass.getMethod("addMapping", String[].class);
72 addMapping.invoke(reg, (Object) new String[]{"/websocket-tunnel"});
74 // If we succesfully load and register the WebSocket tunnel servlet,
75 // WebSocket is supported.
76 logger.info("WebSocket support found and loaded.");
80 // Servlet API 3.0 unsupported
81 catch (ClassNotFoundException e) {
82 logger.info("Servlet API 3.0 not found.", e);
84 catch (NoSuchMethodException e) {
85 logger.warn("Servlet API 3.0 found, but incomplete.", e);
88 // Servlet API 3.0 found, but errors during use
89 catch (IllegalAccessException e) {
90 logger.error("Unable to load WebSocket tunnel servlet.", e);
92 catch (InvocationTargetException e) {
93 logger.error("Internal error loading WebSocket tunnel servlet.", e);
98 // If no such servlet class, WebSocket support not present
99 catch (ClassNotFoundException e) {
100 logger.info("WebSocket support not found.");
103 // Log all GuacamoleExceptions
104 catch (GuacamoleException e) {
105 logger.error("Unable to load/detect WebSocket support.", e);