예제 #1
0
  /**
   * add but don't start This is used only by RouterConsoleRunner, which adds all the webapps first
   * and then starts all at once.
   */
  static WebAppContext addWebApp(
      RouterContext ctx,
      ContextHandlerCollection server,
      String appName,
      String warPath,
      File tmpdir)
      throws IOException {

    // Jetty will happily load one context on top of another without stopping
    // the first one, so we remove any previous one here
    try {
      stopWebApp(appName);
    } catch (Throwable t) {
    }

    // To avoid ZipErrors from JarURLConnetion caching,
    // (used by Jetty JarResource and JarFileResource)
    // copy the war to a new directory if it is newer than the one we loaded originally.
    // Yes, URLConnection has a setDefaultUseCaches() method, but it's hard to get to
    // because it's non-static and the class is abstract, and we don't really want to
    // set the default to false for everything.
    long newmod = (new File(warPath)).lastModified();
    if (newmod <= 0) throw new IOException("Web app " + warPath + " does not exist");
    Long oldmod = warModTimes.get(warPath);
    if (oldmod == null) {
      warModTimes.put(warPath, new Long(newmod));
    } else if (oldmod.longValue() < newmod) {
      // copy war to temporary directory
      File warTmpDir =
          new SecureDirectory(ctx.getTempDir(), "war-copy-" + appName + ctx.random().nextInt());
      warTmpDir.mkdir();
      String tmpPath = (new File(warTmpDir, appName + ".war")).getAbsolutePath();
      if (!FileUtil.copy(warPath, tmpPath, true))
        throw new IOException("Web app failed copy from " + warPath + " to " + tmpPath);
      warPath = tmpPath;
    }

    WebAppContext wac = new WebAppContext(warPath, "/" + appName);
    tmpdir.mkdir();
    wac.setTempDirectory(tmpdir);
    // all the JSPs are precompiled, no need to extract
    wac.setExtractWAR(false);

    // this does the passwords...
    RouterConsoleRunner.initialize(ctx, wac);

    // see WebAppConfiguration for info
    String[] classNames = wac.getConfigurationClasses();
    String[] newClassNames = new String[classNames.length + 1];
    for (int j = 0; j < classNames.length; j++) newClassNames[j] = classNames[j];
    newClassNames[classNames.length] = WebAppConfiguration.class.getName();
    wac.setConfigurationClasses(newClassNames);
    server.addHandler(wac);
    server.mapContexts();
    return wac;
  }
예제 #2
0
  @Test
  public void shouldAddWebAppContextHandler() throws Exception {
    ArgumentCaptor<ContextHandler> captor = ArgumentCaptor.forClass(ContextHandler.class);
    jetty6Server.configure();

    verify(server, times(4)).addHandler(captor.capture());
    List<ContextHandler> handlers = captor.getAllValues();
    assertThat(handlers.size(), is(4));
    ContextHandler handler = handlers.get(3);
    assertThat(handler instanceof WebAppContext, is(true));
    WebAppContext webAppContext = (WebAppContext) handler;
    List<String> configClasses = ArrayUtil.asList(webAppContext.getConfigurationClasses());
    assertThat(configClasses.contains(WebInfConfiguration.class.getCanonicalName()), is(true));
    assertThat(configClasses.contains(WebXmlConfiguration.class.getCanonicalName()), is(true));
    assertThat(configClasses.contains(JettyWebXmlConfiguration.class.getCanonicalName()), is(true));
    assertThat(webAppContext.getContextPath(), is("context"));
    assertThat(webAppContext.getWar(), is("cruise.war"));
    assertThat(webAppContext.isParentLoaderPriority(), is(false));
    assertThat(
        webAppContext.getDefaultsDescriptor(), is("org/mortbay/jetty/webapp/webdefault.xml"));
  }
예제 #3
0
 private String[] removeTagLibConfiguration(WebAppContext context) {
   List<String> configuration =
       with(context.getConfigurationClasses()).remove(endsWith("TagLibConfiguration"));
   return configuration.toArray(new String[configuration.size()]);
 }