The rest of the implementation of -noserver hosted
mode test.  r5094 was incomplete.

Review by: jat



git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@5095 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/dev/GWTShell.java b/dev/core/src/com/google/gwt/dev/GWTShell.java
index 1c7b3c9..8b7924d 100644
--- a/dev/core/src/com/google/gwt/dev/GWTShell.java
+++ b/dev/core/src/com/google/gwt/dev/GWTShell.java
@@ -234,7 +234,7 @@
   protected int doStartUpServer() {
     // TODO(bruce): make tomcat work in terms of the modular launcher
     String whyFailed = EmbeddedTomcatServer.start(getTopLogger(), getPort(),
-        options);
+        options, shouldAutoGenerateResources());
 
     // TODO(bruce): test that we can remove this old approach in favor of
     // a better, logger-based error reporting
@@ -260,4 +260,14 @@
      */
     return false;
   }
+
+  /**
+   * Whether this shell should auto-generate GWT resources when it recognizes
+   * requests for them. By default this is true. Subclasses can disable such
+   * auto-generation and make this servlet appear to be like any arbitrary web
+   * server that knows nothing about GWT.
+   */
+  protected boolean shouldAutoGenerateResources() {
+    return true;
+  }
 }
diff --git a/dev/core/src/com/google/gwt/dev/shell/GWTShellServlet.java b/dev/core/src/com/google/gwt/dev/shell/GWTShellServlet.java
index 27ada88..b44ee71 100644
--- a/dev/core/src/com/google/gwt/dev/shell/GWTShellServlet.java
+++ b/dev/core/src/com/google/gwt/dev/shell/GWTShellServlet.java
@@ -116,12 +116,12 @@
 
   private int nextRequestId;
 
-  private WorkDirs workDirs;
-
   private final Object requestIdLock = new Object();
 
   private TreeLogger topLogger;
 
+  private WorkDirs workDirs;
+
   public GWTShellServlet() {
     initMimeTypes();
   }
@@ -409,9 +409,11 @@
     logger = logger.branch(TreeLogger.TRACE, msg, null);
 
     // Handle auto-generation of resources.
-    if (autoGenerateResources(request, response, logger, partialPath,
-        moduleName)) {
-      return;
+    if (shouldAutoGenerateResources()) {
+      if (autoGenerateResources(request, response, logger, partialPath,
+          moduleName)) {
+        return;
+      }
     }
 
     URL foundResource = null;
@@ -419,20 +421,22 @@
       // Look for the requested file on the public path.
       //
       ModuleDef moduleDef = getModuleDef(logger, moduleName);
-      Resource publicResource = moduleDef.findPublicFile(partialPath);
-      if (publicResource != null) {
-        foundResource = publicResource.getURL();
-      }
+      if (shouldAutoGenerateResources()) {
+        Resource publicResource = moduleDef.findPublicFile(partialPath);
+        if (publicResource != null) {
+          foundResource = publicResource.getURL();
+        }
 
-      if (foundResource == null) {
-        // Look for public generated files
-        File shellDir = getShellWorkDirs().getShellPublicGenDir(moduleDef);
-        File requestedFile = new File(shellDir, partialPath);
-        if (requestedFile.exists()) {
-          try {
-            foundResource = requestedFile.toURI().toURL();
-          } catch (MalformedURLException e) {
-            // ignore since it was speculative anyway
+        if (foundResource == null) {
+          // Look for public generated files
+          File shellDir = getShellWorkDirs().getShellPublicGenDir(moduleDef);
+          File requestedFile = new File(shellDir, partialPath);
+          if (requestedFile.exists()) {
+            try {
+              foundResource = requestedFile.toURI().toURL();
+            } catch (MalformedURLException e) {
+              // ignore since it was speculative anyway
+            }
           }
         }
       }
@@ -886,6 +890,16 @@
     response.setHeader(HttpHeaders.EXPIRES, expiresString);
   }
 
+  private boolean shouldAutoGenerateResources() {
+    ServletContext servletContext = getServletContext();
+    final String attr = "com.google.gwt.dev.shell.shouldAutoGenerateResources";
+    Boolean attrValue = (Boolean) servletContext.getAttribute(attr);
+    if (attrValue == null) {
+      return true;
+    }
+    return attrValue;
+  }
+
   private void streamOut(InputStream in, OutputStream out, int bufferSize)
       throws IOException {
     assert (bufferSize >= 0);
diff --git a/dev/core/src/com/google/gwt/dev/shell/tomcat/EmbeddedTomcatServer.java b/dev/core/src/com/google/gwt/dev/shell/tomcat/EmbeddedTomcatServer.java
index cfa4972..5a86612 100644
--- a/dev/core/src/com/google/gwt/dev/shell/tomcat/EmbeddedTomcatServer.java
+++ b/dev/core/src/com/google/gwt/dev/shell/tomcat/EmbeddedTomcatServer.java
@@ -60,14 +60,19 @@
     return sTomcat.port;
   }
 
+  public static String start(TreeLogger topLogger, int port, WorkDirs workDirs) {
+    return start(topLogger, port, workDirs, true);
+  }
+
   public static synchronized String start(TreeLogger topLogger, int port,
-      WorkDirs workDirs) {
+      WorkDirs workDirs, boolean shouldAutoGenerateResources) {
     if (sTomcat != null) {
       throw new IllegalStateException("Embedded Tomcat is already running");
     }
 
     try {
-      new EmbeddedTomcatServer(topLogger, port, workDirs);
+      new EmbeddedTomcatServer(topLogger, port, workDirs,
+          shouldAutoGenerateResources);
       return null;
     } catch (LifecycleException e) {
       String msg = e.getMessage();
@@ -146,7 +151,8 @@
   private final TreeLogger startupBranchLogger;
 
   private EmbeddedTomcatServer(final TreeLogger topLogger, int listeningPort,
-      final WorkDirs workDirs) throws LifecycleException {
+      final WorkDirs workDirs, final boolean shouldAutoGenerateResources)
+      throws LifecycleException {
     if (topLogger == null) {
       throw new NullPointerException("No logger specified");
     }
@@ -233,6 +239,8 @@
           StandardContext webapp = (StandardContext) event.getData();
           publishShellLoggerAttribute(logger, topLogger, webapp);
           publishShellWorkDirsAttribute(logger, workDirs, webapp);
+          publishShouldAutoGenerateResourcesAttribute(logger,
+              shouldAutoGenerateResources, webapp);
         }
       }
     });
@@ -429,4 +437,14 @@
     final String attr = "com.google.gwt.dev.shell.workdirs";
     publishAttributeToWebApp(logger, webapp, attr, workDirs);
   }
+
+  /**
+   * Publish to the web app whether it should automatically generate resources.
+   */
+  private void publishShouldAutoGenerateResourcesAttribute(TreeLogger logger,
+      boolean shouldAutoGenerateResources, StandardContext webapp) {
+    publishAttributeToWebApp(logger, webapp,
+        "com.google.gwt.dev.shell.shouldAutoGenerateResources",
+        shouldAutoGenerateResources);
+  }
 }