Move prettify to be loaded as part of our patch split point

This way we don't download its JavaScript or CSS as part of the
initial download.  It saves 2 round-trips during bootstrap.

Change-Id: I430dcd5832ecaa8eafb79ab92a2c24be85608a78
Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce
2009-12-19 19:41:20 -08:00
parent 4811392146
commit d83ac11a52
17 changed files with 98 additions and 97 deletions

View File

@@ -20,7 +20,6 @@ import com.google.gerrit.common.PageLinks;
import com.google.gerrit.httpd.raw.CatServlet;
import com.google.gerrit.httpd.raw.HostPageServlet;
import com.google.gerrit.httpd.raw.LegacyGerritServlet;
import com.google.gerrit.httpd.raw.PrettifyServlet;
import com.google.gerrit.httpd.raw.SshInfoServlet;
import com.google.gerrit.httpd.raw.StaticServlet;
import com.google.gerrit.reviewdb.RevId;
@@ -47,7 +46,6 @@ class UrlModule extends ServletModule {
serve("/Gerrit/*").with(legacyGerritScreen());
serve("/cat/*").with(CatServlet.class);
serve("/logout").with(HttpLogoutServlet.class);
serve("/prettify/*").with(PrettifyServlet.class);
serve("/signout").with(HttpLogoutServlet.class);
serve("/ssh_info").with(SshInfoServlet.class);
serve("/static/*").with(StaticServlet.class);

View File

@@ -1,91 +0,0 @@
// Copyright (C) 2009 The Android Open Source Project
//
// 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 com.google.gerrit.httpd.raw;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Singleton
public class PrettifyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String VERSION = "20090521";
private final byte[] content;
@Inject
PrettifyServlet(final ServletContext servletContext) throws IOException {
final String myDir = "/gerrit/prettify" + VERSION + "/";
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
load(buffer, servletContext, myDir + "prettify.js");
for (Object p : servletContext.getResourcePaths(myDir)) {
String name = (String) p;
if (name.startsWith(myDir + "lang-") && name.endsWith(".js")) {
load(buffer, servletContext, name);
}
}
content = buffer.toByteArray();
}
private void load(final OutputStream buffer,
final ServletContext servletContext, final String path)
throws IOException {
final InputStream in = servletContext.getResourceAsStream(path);
if (in != null) {
try {
final byte[] tmp = new byte[4096];
int cnt;
while ((cnt = in.read(tmp)) > 0) {
buffer.write(tmp, 0, cnt);
}
buffer.write(';');
buffer.write('\n');
in.close();
} catch (IOException e) {
throw new IOException("Cannot read " + path, e);
}
}
}
@Override
protected void doGet(final HttpServletRequest req,
final HttpServletResponse rsp) throws IOException {
final String want = req.getPathInfo();
if (want.equals("/" + VERSION + ".js")) {
final long now = System.currentTimeMillis();
rsp.setHeader("Cache-Control", "max-age=31536000,public");
rsp.setDateHeader("Expires", now + 31536000000L);
rsp.setDateHeader("Date", now);
rsp.setContentType("application/x-javascript");
rsp.setContentLength(content.length);
rsp.getOutputStream().write(content);
} else {
rsp.setHeader("Expires", "Fri, 01 Jan 1980 00:00:00 GMT");
rsp.setHeader("Pragma", "no-cache");
rsp.setHeader("Cache-Control", "no-cache, must-revalidate");
rsp.setDateHeader("Date", System.currentTimeMillis());
rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
}
}
}

View File

@@ -32,7 +32,6 @@
</script>
<script id="gerrit_hostpagedata" defer="defer"></script>
<script id="gerrit_module" defer="defer"></script>
<script src="prettify/20090521.js" type="text/javascript" language="javascript" defer="true"></script>
<style id="gerrit_sitecss" type="text/css"></style>
<link rel="icon" type="image/gif" href="favicon.ico" />
</head>

View File

@@ -14,6 +14,6 @@
limitations under the License.
-->
<module>
<stylesheet src='prettify20090521/prettify.css' />
<inherits name='com.google.gwt.resources.Resources'/>
<inherits name='com.google.gwtexpui.safehtml.SafeHtml'/>
</module>

View File

@@ -14,6 +14,9 @@
package com.google.gwtexpui.safehtml.client;
import com.google.gwt.resources.client.TextResource;
import com.google.gwtexpui.safehtml.client.prettify.Resources;
import java.util.HashMap;
import java.util.Map;
@@ -122,10 +125,32 @@ public abstract class PrettyFormatter {
}
private static class Pretty extends PrettyFormatter {
static {
Resources.I.css().ensureInjected();
eval(Resources.I.core());
eval(Resources.I.lang_css());
eval(Resources.I.lang_hs());
eval(Resources.I.lang_lisp());
eval(Resources.I.lang_lua());
eval(Resources.I.lang_ml());
eval(Resources.I.lang_proto());
eval(Resources.I.lang_sql());
eval(Resources.I.lang_vb());
eval(Resources.I.lang_wiki());
}
static final boolean loaded = isLoaded();
private static native boolean isLoaded()
/*-{ return $wnd['prettyPrintOne'] != null }-*/;
/*-{ return window.prettyPrintOne != null }-*/;
private static void eval(final TextResource core) {
eval(core.getText());
}
private static native void eval(String js)
/*-{ eval(js); }-*/;
private final String srcType;
private final MultiLineStyle commentStyle;
@@ -170,6 +195,6 @@ public abstract class PrettyFormatter {
}
private static native String prettifyNative(String srcText, String srcType)
/*-{ return $wnd.prettyPrintOne(srcText, srcType); }-*/;
/*-{ return prettyPrintOne(srcText, srcType); }-*/;
}
}

View File

@@ -0,0 +1,57 @@
// Copyright (C) 2009 The Android Open Source Project
//
// 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 com.google.gwtexpui.safehtml.client.prettify;
import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.resources.client.TextResource;
public interface Resources extends ClientBundle {
public static final Resources I = GWT.create(Resources.class);
@Source("prettify.css")
CssResource css();
@Source("prettify.js")
TextResource core();
@Source("lang-css.js")
TextResource lang_css();
@Source("lang-hs.js")
TextResource lang_hs();
@Source("lang-lisp.js")
TextResource lang_lisp();
@Source("lang-lua.js")
TextResource lang_lua();
@Source("lang-ml.js")
TextResource lang_ml();
@Source("lang-proto.js")
TextResource lang_proto();
@Source("lang-sql.js")
TextResource lang_sql();
@Source("lang-vb.js")
TextResource lang_vb();
@Source("lang-wiki.js")
TextResource lang_wiki();
}

View File

@@ -1,5 +1,18 @@
/* Pretty printing styles. Used with prettify.js. */
@external .str;
@external .kwd;
@external .com;
@external .typ;
@external .lit;
@external .pun;
@external .pln;
@external .tag;
@external .atn;
@external .atv;
@external .dec;
@external .prettyprint;
.str { color: #080; }
.kwd { color: #008; }
.com { color: #800; }