here. And it works really good so far.
The author of this component says that the echo2 code has to be patched such that the google map api can be loaded at page load time which is needed for google maps (at least with echo2 version 2.1.0beta5).
I managed to get the component running with echo2 2.1.0rc2 without patching:
The starting point for an echo2 application is a servlet which extends
nextapp.echo2.webrender.WebRenderServlet
. The normal behaviour of this servlet is to register some services. One of these services is the nextapp.echo2.webcontainer.WindowHtmlService
which produces the output in its service method. The trick is now to replace this class with your own implementation.I made it this way:
The constructor of my
HelloWorldServlet
which extends nextapp.echo2.webrender.WebRenderServlet
and which is the starting point of my echo2 application is the following:
public HelloWorldServlet()
{
super();
final String METHOD = "HelloWorldServlet: ";
ServiceRegistry serviceRegistry = WebRenderServlet.getServiceRegistry();
serviceRegistry.remove(WindowHtmlService.INSTANCE);
serviceRegistry.remove(NewInstanceService.INSTANCE);
serviceRegistry.add(MyNewInstanceService.INSTANCE);
serviceRegistry.add(MyWindowHtmlService.INSTANCE);
log.debug(METHOD+"successful");
}
This removes the old service implementations and replaces it with my own implementations:
package de.banapple.echo2test;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import nextapp.echo2.webcontainer.ContainerInstance;
import nextapp.echo2.webrender.Connection;
import nextapp.echo2.webrender.Service;
public class MyNewInstanceService
implements Service
{
private static Log log = LogFactory.getLog(MyNewInstanceService.class);
public String getId()
{
return "Echo.NewInstance";
}
public int getVersion()
{
return -1;
}
public void service(Connection conn)
throws IOException
{
final String METHOD = "service: ";
log.debug(METHOD+"conn="+conn);
ContainerInstance.newInstance(conn);
MyWindowHtmlService.INSTANCE.service(conn);
}
public static final MyNewInstanceService INSTANCE =
new MyNewInstanceService();
}
package de.banapple.echo2test;
import java.io.IOException;
import nextapp.echo2.webcontainer.ContainerInstance;
import nextapp.echo2.webcontainer.WindowHtmlService;
import nextapp.echo2.webrender.BaseHtmlDocument;
import nextapp.echo2.webrender.Connection;
import nextapp.echo2.webrender.ContentType;
import nextapp.echo2.webrender.output.CssStyle;
import nextapp.echo2.webrender.service.CoreServices;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Element;
public class MyWindowHtmlService
extends WindowHtmlService
{
private static Log log = LogFactory.getLog(MyWindowHtmlService.class);
public void service(Connection conn)
throws IOException
{
final String METHOD = "service: ";
log.debug(METHOD+"conn="+conn);
ContainerInstance ci = (ContainerInstance)conn.getUserInstance();
conn.setContentType(ContentType.TEXT_HTML);
boolean debug = !"false".equals(conn.getServlet().getInitParameter("echo2.debug"));
BaseHtmlDocument baseDoc = new BaseHtmlDocument("c_root");
baseDoc.setGenarator("NextApp Echo v2.1.0.rc2");
/* add google maps api */
baseDoc.addJavaScriptInclude(
"js/googleload.js");
baseDoc.addJavaScriptInclude(ci.getServiceUri(CoreServices.CLIENT_ENGINE));
baseDoc.getBodyElement().setAttribute("onload", "EchoClientEngine.init('" + ci.getServletUri() + "', " + debug + ");");
Element bodyElement = baseDoc.getBodyElement();
CssStyle cssStyle = new CssStyle();
cssStyle.setAttribute("position", "absolute");
cssStyle.setAttribute("font-family", "verdana, arial, helvetica, sans-serif");
cssStyle.setAttribute("font-size", "10pt");
cssStyle.setAttribute("height", "100%");
cssStyle.setAttribute("width", "100%");
cssStyle.setAttribute("padding", "0px");
cssStyle.setAttribute("margin", "0px");
cssStyle.setAttribute("overflow", "hidden");
bodyElement.setAttribute("style", cssStyle.renderInline());
baseDoc.render(conn.getWriter());
}
public static final MyWindowHtmlService INSTANCE =
new MyWindowHtmlService();
}
The implementation of the service method of
MyWindowHtmlService
is copied from the original implementation and extended with the baseDoc.addJavaScriptInclude("js/googleload.js");
which loads the google maps api with my key.Ok, that is not really far away from patching the class, but still... It might work with forthcoming versions of echo2.