
org.atmosphere.guice.AtmosphereGuiceServlet Maven / Gradle / Ivy
/*
* Copyright 2013 Jeanfrancois Arcand
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.atmosphere.guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;
import org.atmosphere.cpr.ApplicationConfig;
import org.atmosphere.cpr.AtmosphereFramework;
import org.atmosphere.cpr.AtmosphereServlet;
import org.atmosphere.cpr.DefaultBroadcasterFactory;
import org.atmosphere.cpr.FrameworkConfig;
import org.atmosphere.handler.ReflectorServletProcessor;
import org.atmosphere.jersey.JerseyBroadcaster;
import com.vaadin.external.org.slf4j.Logger;
import com.vaadin.external.org.slf4j.LoggerFactory;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import java.util.Map;
import static org.atmosphere.cpr.FrameworkConfig.JERSEY_CONTAINER;
/**
* Google Guice Integration. To use it, just do in web.xml:
*
*
* <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
* xmlns:j2ee = "http://java.sun.com/xml/ns/j2ee"
* xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
* xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
* <listener>
* <listener-class>org.atmosphere.samples.guice.GuiceChatConfig</listener-class>
* </listener>
* <description>Atmosphere Chat</description>
* <display-name>Atmosphere Chat</display-name>
* <servlet>
* <description>AtmosphereServlet</description>
* <servlet-name>AtmosphereServlet</servlet-name>
* <servlet-class>org.atmosphere.guice.AtmosphereGuiceServlet</servlet-class>
* <load-on-startup>0</load-on-startup>
* </servlet>
* <servlet-mapping>
* <servlet-name>AtmosphereServlet</servlet-name>
* <url-pattern>/chat/*</url-pattern>
* </servlet-mapping>
* </web-app>
*
* and then
*
* public class GuiceConfig extends GuiceServletContextListener {
*
* @author Jeanfrancois Arcand
* @author Richard Wallace
* @Override protected Injector getInjector() {
* return Guice.createInjector(new ServletModule() {
* @Override protected void configureServlets() {
* bind(PubSubTest.class);
* bind(new TypeLiteral<Map<String, String>>() {
* }).annotatedWith(Names.named(AtmosphereGuiceServlet.JERSEY_PROPERTIES)).toInstance(
* Collections.<String, String>emptyMap());
* }
* });
* }
* }
*
*/
public class AtmosphereGuiceServlet extends AtmosphereServlet {
private static final Logger logger = LoggerFactory.getLogger(AtmosphereGuiceServlet.class);
public static final String JERSEY_PROPERTIES = AtmosphereGuiceServlet.class.getName() + ".properties";
private static final String GUICE_FILTER = "com.google.inject.servlet.GuiceFilter";
private boolean guiceInstalled = false;
public AtmosphereGuiceServlet() {
this(false,true);
}
/**
* Create an Atmosphere Servlet.
*
* @param isFilter true if this instance is used as an {@link org.atmosphere.cpr.AtmosphereFilter}
*/
public AtmosphereGuiceServlet(boolean isFilter, boolean autoDetectHandlers) {
framework = new AtmosphereFramework(isFilter, autoDetectHandlers) {
/**
* Install Guice event if other extension has been already installed.
*
* @param sc {@link javax.servlet.ServletConfig}
* @throws ServletException
*/
public void loadConfiguration(ServletConfig sc) throws ServletException {
super.loadConfiguration(sc);
if (!guiceInstalled) {
detectSupportedFramework(sc);
}
}
/**
* Auto-detect Jersey when no atmosphere.xml file are specified.
*
* @param sc {@link javax.servlet.ServletConfig}
* @return true if Jersey classes are detected
*/
protected boolean detectSupportedFramework(ServletConfig sc) {
Injector injector = (Injector) framework().getAtmosphereConfig().getServletContext().getAttribute(Injector.class.getName());
GuiceContainer guiceServlet = injector.getInstance(GuiceContainer.class);
setUseStreamForFlushingComments(false);
ReflectorServletProcessor rsp = new ReflectorServletProcessor();
boolean isJersey = false;
try {
Thread.currentThread().getContextClassLoader().loadClass(JERSEY_CONTAINER);
setDefaultBroadcasterClassName(broadcasterClassName)
.setUseStreamForFlushingComments(true)
.getAtmosphereConfig().setSupportSession(false);
isJersey = true;
} catch (Throwable t) {
}
rsp.setServlet(guiceServlet);
String mapping = sc.getInitParameter(ApplicationConfig.PROPERTY_SERVLET_MAPPING);
if (mapping == null) {
mapping = "/*";
}
if (isJersey) {
try {
Map props = injector.getInstance(
Key.get(new TypeLiteral
© 2015 - 2025 Weber Informatics LLC | Privacy Policy