Esempio n. 1
0
  private Set<File> getDependencyFiles() {
    List<Resource> overlays = new ArrayList<Resource>();

    Set<File> dependencies = getClasspath().getFiles();
    logger.debug("Adding dependencies {} for WEB-INF/lib ", dependencies);

    // todo incorporate overlays when our resolved dependencies provide type information
    //            if (artifact.getType().equals("war")) {
    //                try {
    //                    Resource r = Resource.newResource("jar:" +
    // artifact.getFile().toURL().toString() + "!/");
    //                    overlays.add(r);
    //                    getExtraScanTargets().add(artifact.getFile());
    //                }
    //                catch (Exception e) {
    //                    throw new RuntimeException(e);
    //                }
    //                continue;
    //            }
    if (!overlays.isEmpty()) {
      try {
        Resource resource = getWebAppConfig().getBaseResource();
        ResourceCollection rc = new ResourceCollection();
        if (resource == null) {
          // nothing configured, so we automagically enable the overlays
          int size = overlays.size() + 1;
          Resource[] resources = new Resource[size];
          resources[0] = Resource.newResource(getWebAppSourceDirectory().toURI().toURL());
          for (int i = 1; i < size; i++) {
            resources[i] = overlays.get(i - 1);
            logger.info("Adding overlay: " + resources[i]);
          }
          rc.setResources(resources);
        } else {
          if (resource instanceof ResourceCollection) {
            // there was a preconfigured ResourceCollection ... append the artifact wars
            Resource[] old = ((ResourceCollection) resource).getResources();
            int size = old.length + overlays.size();
            Resource[] resources = new Resource[size];
            System.arraycopy(old, 0, resources, 0, old.length);
            for (int i = old.length; i < size; i++) {
              resources[i] = overlays.get(i - old.length);
              logger.info("Adding overlay: " + resources[i]);
            }
            rc.setResources(resources);
          } else {
            // baseResource was already configured w/c could be src/main/webapp
            if (!resource.isDirectory() && String.valueOf(resource.getFile()).endsWith(".war")) {
              // its a war
              resource = Resource.newResource("jar:" + resource.getURL().toString() + "!/");
            }
            int size = overlays.size() + 1;
            Resource[] resources = new Resource[size];
            resources[0] = resource;
            for (int i = 1; i < size; i++) {
              resources[i] = overlays.get(i - 1);
              logger.info("Adding overlay: " + resources[i]);
            }
            rc.setResources(resources);
          }
        }
        getWebAppConfig().setBaseResource(rc);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
    return dependencies;
  }
Esempio n. 2
0
  private static void initWebappContext(Server server, Configs configs)
      throws IOException, URISyntaxException {
    web = new WebAppContext();

    if (configs.getParentLoaderPriority()) {
      System.err.println("ParentLoaderPriority enabled");
      web.setParentLoaderPriority(true);
    }

    web.setContextPath(configs.getContext());
    System.err.println("Context path:" + configs.getContext());
    web.setWar(configs.getWebAppDir());

    /** open a way to set the configuration classes */
    List<String> configurationClasses = configs.getConfigurationClassesList();
    if (configurationClasses.size() != 0) {
      web.setConfigurationClasses(configurationClasses.toArray(new String[0]));

      for (String conf : configurationClasses) System.err.println("Enable config class:" + conf);
    }

    // Fix issue 7, File locking on windows/Disable Jetty's locking of
    // static files
    // http://code.google.com/p/run-jetty-run/issues/detail?id=7
    // by disabling the use of the file mapped buffers. The default Jetty
    // behavior is
    // intended to provide a performance boost, but run-jetty-run is focused
    // on
    // development (especially debugging) of web apps, not high-performance
    // production
    // serving of static content. Therefore, I'm not worried about the
    // performance
    // degradation of this change. My only concern is that there might be a
    // need to
    // test this feature that I'm disabling.
    web.setInitParams(
        Collections.singletonMap("org.mortbay.jetty.servlet.Default.useFileMappedBuffer", "false"));

    if (configs.getWebAppClassPath() != null) {
      ProjectClassLoader loader =
          new ProjectClassLoader(web, configs.getWebAppClassPath(), configs.getExcludedclasspath());
      web.setClassLoader(loader);
    }

    List<Resource> resources = new ArrayList<Resource>();

    URL urlWebapp = new File(configs.getWebAppDir()).toURI().toURL();
    Resource webapp = new FileResource(urlWebapp);
    resources.add(webapp);

    Map<String, String> map = configs.getResourceMap();
    for (String key : map.keySet()) {
      /*
      * 			URL url = new File(map.get(key)).toURI().toURL();
      		Resource resource;
      		try {
      			resource = new FileResource(url);
      			final ResourceHandler handler = new ResourceHandler();
      			handler.setBaseResource(resource);
      			handler.setServer(server);
      			handler.setContextPath(key);
      			web.addHandler(handler);
      		} catch (URISyntaxException e) {
      			e.printStackTrace();
      		}

      */
      resources.add(new VirtualResource(webapp, "/" + key, map.get(key)));
      //			final WebAppContext js = new WebAppContext();
      //			js.setContextPath(key);
      //			js.setResourceBase(map.get(key)); // or whatever the correct path is in your case
      //			js.setParentLoaderPriority(true);
      //			server.addHandler(js);
    }

    ResourceCollection webAppDirResources = new ResourceCollection();
    webAppDirResources.setResources(resources.toArray(new Resource[0]));
    web.setBaseResource(webAppDirResources);

    server.addHandler(web);
  }