com.googlecode.gwtrpcplus.server.ModuleGwtRpcPlus Maven / Gradle / Ivy
package com.googlecode.gwtrpcplus.server;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServlet;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.inject.Singleton;
import com.google.inject.servlet.ServletModule;
import com.googlecode.gwtrpcplus.server.servlet.GwtRpcPlusBasicServlet;
import com.googlecode.gwtrpcplus.server.servlet.GwtRpcPlusBundleServlet;
import com.googlecode.gwtrpcplus.server.servlet.GwtRpcPlusWebsocketDummy;
import com.googlecode.gwtrpcplus.server.util.Logger;
public class ModuleGwtRpcPlus extends ServletModule {
private final static Logger logger = new Logger(ModuleGwtRpcPlus.class);
private final String modulename;
private final Set> servletClasses;
/**
* @param base
* for example the projectName
* @param servletClasses
* Set of all ServletClasses
*/
public ModuleGwtRpcPlus(String modulename, @SuppressWarnings("unchecked") Class extends RemoteServiceServlet>... servletClasses) {
Set> classes = new HashSet>();
for (Class extends RemoteServiceServlet> c : servletClasses)
classes.add(c);
this.modulename = modulename;
this.servletClasses = classes;
}
/**
* @param base
* for example the projectName
* @param servletClasses
* Set of all ServletClasses
*/
public ModuleGwtRpcPlus(String modulename, Set> servletClasses) {
this.modulename = modulename;
this.servletClasses = servletClasses;
}
@Override
protected final void configureServlets() {
// Place for the user to add custom Code, when inherit from this Module
configureCustomServlets();
// WebsocketConnection
boolean websocketsAdded = addWebsockets();
// Serve with dummy, returning a NotImplemented-State
if (!websocketsAdded) {
serve("/" + modulename + "/gwtRpcPlusWebsocket").with(GwtRpcPlusWebsocketDummy.class);
}
// ConnectionBasic
serve("/" + modulename + "/gwtRpcPlusBasic").with(GwtRpcPlusBasicServlet.class);
// ConnectionBundle
serve("/" + modulename + "/gwtRpcPlusBundle").with(GwtRpcPlusBundleServlet.class);
bind(ServletList.class).toInstance(new ServletList() {
@Override
public Set> getServletClasses() {
return servletClasses;
}
});
}
protected void configureCustomServlets() {
}
private boolean addWebsockets() {
try {
@SuppressWarnings("unchecked")
Class extends HttpServlet> c = (Class extends HttpServlet>) Class.forName("com.googlecode.gwtrpcplus.server.servlet.GwtRpcPlusWebsocket");
// Check for correct Jetty Version
if (!getServletContext().getServerInfo().startsWith("jetty/9.")) {
throw new RuntimeException("Only supported in jetty 9 yet (working since jetty 9.0.0), but was " + getServletContext().getServerInfo());
}
// Try adding the WebsocketServlet
Map params = new HashMap();
params.put("bufferSize", "100000");
serve("/" + modulename + "/gwtRpcPlusWebsocket").with(c, params);
return true;
} catch (ClassNotFoundException e) {
// Ignore when not added
return false;
} catch (Throwable e) {
logger.trace("Ignoring creation the WebSocketServlet. Using only HTTP Calls. Exception", e);
logger.warn("Ignoring creation the WebSocketServlet. Using only HTTP Calls. Exception:" + e.getClass().getName() + " :: " + e.getMessage());
return false;
}
}
/**
* Optional usage for adding the Service to gwtrpcplus and to the servlet.
*
* It binds the Service as Singleton too.
*
* @param clazz
* ServiceImpl to add
*/
protected void bindGwtServlet(Class extends RemoteServiceServlet> clazz) {
servletClasses.add(clazz);
bind(clazz).in(Singleton.class);
serve("/" + modulename + "/" + findName(clazz)).with(clazz);
}
private String findName(Class> clazz) {
for (Class> c : clazz.getInterfaces())
if (c.isAnnotationPresent(RemoteServiceRelativePath.class))
return c.getAnnotation(RemoteServiceRelativePath.class).value();
return findName(clazz.getSuperclass());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy