org.apache.catalina.WebResourceRoot类的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(14.1k)|赞(0)|评价(0)|浏览(353)

本文整理了Java中org.apache.catalina.WebResourceRoot类的一些代码示例,展示了WebResourceRoot类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WebResourceRoot类的具体详情如下:
包路径:org.apache.catalina.WebResourceRoot
类名称:WebResourceRoot

WebResourceRoot介绍

[英]Represents the complete set of resources for a web application. The resources for a web application comprise of multiple ResourceSets and when looking for a Resource, the ResourceSets are processed in the following order:

  1. Pre - Resources defined by the <PreResource> element in the web application's context.xml. Resources will be searched in the order they were specified.
  2. Main - The main resources for the web application - i.e. the WAR or the directory containing the expanded WAR
  3. JARs - Resource JARs as defined by the Servlet specification. JARs will be searched in the order they were added to the ResourceRoot.
  4. Post - Resources defined by the <PostResource> element in the web application's context.xml. Resources will be searched in the order they were specified.
    The following conventions should be noted:
  • Write operations (including delete) will only be applied to the main ResourceSet. The write operation will fail if the presence of a Resource in one of the other ResourceSets effectively makes the operation on the main ResourceSet a NO-OP.

  • A file in a ResourceSet will hide a directory of the same name (and all the contents of that directory) in a ResourceSet that is later in the search order.

  • Only the main ResourceSet may define a META-INF/context.xml since that file defines the Pre- and Post-Resources.

  • As per the Servlet specification, any META-INF or WEB-INF directories in a resource JAR will be ignored.

  • Pre- and Post-Resources may define WEB-INF/lib and WEB-INF/classes in order to make additional libraries and/or classes available to the web application.
    This mechanism replaces and extends the following features that were present in earlier versions:

  • Aliases - Replaced by Post-Resources with the addition of support for single files as well as directories and JARs.

  • VirtualWebappLoader - Replaced by Pre- and Post-Resources mapped to WEB-INF/lib and WEB-INF/classes

  • VirtualDirContext - Replaced by Pre- and Post-Resources

  • External repositories - Replaced by Pre- and Post-Resources mapped to WEB-INF/lib and WEB-INF/classes

  • Resource JARs - Same feature but implemented using the same mechanism as all the other additional resources.
    [中]表示web应用程序的完整资源集。web应用程序的资源由多个资源集组成,在查找资源时,资源集的处理顺序如下:
    1.由web应用程序上下文中的<PreResource>元素定义的预资源。xml。将按指定的顺序搜索资源。
    1.Main——web应用程序的主要资源——即WAR或包含扩展WAR的目录
    1.JARs——Servlet规范定义的资源JAR。JAR将按照它们添加到ResourceRoot的顺序进行搜索。
    1.Post—由web应用程序上下文中的<PostResource>元素定义的资源。xml。将按指定的顺序搜索资源。
    应注意以下公约:
    *写入操作(包括删除)将仅应用于主资源集。如果其他资源集中的一个资源有效地使主资源集上的操作成为禁止操作,则写入操作将失败。
    *资源集中的文件将在搜索顺序后面的资源集中隐藏同名目录(以及该目录的所有内容)。
    *只有主资源集可以定义META-INF/context。xml,因为该文件定义了Pre-and-Post资源。
    *根据Servlet规范,资源JAR中的任何META-INF或WEB-INF目录都将被忽略。
    *Pre-and-Post资源可以定义WEB-INF/lib和WEB-INF/classes,以便为WEB应用程序提供额外的库和/或类。
    此机制取代并扩展了早期版本中的以下功能:
    *别名——被Post资源取代,增加了对单个文件、目录和JAR的支持。
    *VirtualWebappLoader——被映射到WEB-INF/lib和WEB-INF/classes的Pre-and-Post资源所取代
    *VirtualDircContext-被前期和后期资源取代
    *外部存储库——被映射到WEB-INF/lib和WEB-INF/classes的Pre-and-Post资源所取代
    *资源罐——与所有其他额外资源相同的功能,但使用相同的机制实现。

代码示例

代码示例来源:origin: org.springframework.boot/spring-boot

private void addResourceSet(String resource) {
  try {
    if (isInsideNestedJar(resource)) {
      // It's a nested jar but we now don't want the suffix because Tomcat
      // is going to try and locate it as a root URL (not the resource
      // inside it)
      resource = resource.substring(0, resource.length() - 2);
    }
    URL url = new URL(resource);
    String path = "/META-INF/resources";
    this.context.getResources().createWebResourceSet(
        ResourceSetType.RESOURCE_JAR, "/", url, path);
  }
  catch (Exception ex) {
    // Ignore (probably not a directory)
  }
}

代码示例来源:origin: psi-probe/psi-probe

@Override
public boolean resourceExists(String name, Context context) {
 return context.getResources().getResource(name) != null;
}

代码示例来源:origin: stackoverflow.com

String webappDirLocation = "src/main/webapp/";
Tomcat tomcat = new Tomcat();
tomcat.setPort(8080);

StandardContext ctx = (StandardContext) tomcat.addWebapp("/embeddedTomcat",
        new File(webappDirLocation).getAbsolutePath());

//declare an alternate location for your "WEB-INF/classes" dir:     
File additionWebInfClasses = new File("target/classes");
WebResourceRoot resources = new StandardRoot(ctx);
resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes", additionWebInfClasses.getAbsolutePath(), "/"));
ctx.setResources(resources);

tomcat.start();
tomcat.getServer().await();

代码示例来源:origin: org.apache.tomcat/tomcat-catalina

if (log.isDebugEnabled())
  log.debug("modified()");
  long lastModified = resources.getClassLoaderResource(
      entry.getKey()).getLastModified();
  if (lastModified != cachedLastModified) {
    if( log.isDebugEnabled() )
      log.debug(sm.getString("webappClassLoader.resourceModified",
          entry.getKey(),
          new Date(cachedLastModified),
WebResource[] jars = resources.listResources("/WEB-INF/lib");
    if (recordedLastModified == null) {
      log.info(sm.getString("webappClassLoader.jarsAdded",
          resources.getContext().getName()));
      return true;
      log.info(sm.getString("webappClassLoader.jarsModified",
          resources.getContext().getName()));
      return true;
  log.info(sm.getString("webappClassLoader.jarsRemoved",
      resources.getContext().getName()));
  return true;

代码示例来源:origin: justlive1/oxygen

@Override
protected void processResourceJARs(Set<WebXml> fragments) {
 for (WebXml fragment : fragments) {
  URL url = fragment.getURL();
  try {
   String urlString = url.toString();
   if (urlString.indexOf(Constants.JAR_URL_SEPARATOR) < urlString
     .lastIndexOf(Constants.JAR_URL_SEPARATOR)) {
    urlString = urlString.substring(0, urlString.length() - 2);
   }
   url = new URL(urlString);
   if (Constants.URL_PROTOCOL_JAR.equals(url.getProtocol()) || url.toString()
     .endsWith(Constants.JAR_EXT)) {
    processJar(url);
   } else if (Constants.URL_PROTOCOL_FILE.equals(url.getProtocol())) {
    File file = new File(url.toURI());
    File resources = new File(file, Constants.META_INF_RESOURCES);
    if (resources.isDirectory()) {
     context.getResources()
       .createWebResourceSet(WebResourceRoot.ResourceSetType.RESOURCE_JAR,
         Constants.ROOT_PATH, resources.getAbsolutePath(), null, Constants.ROOT_PATH);
    }
   }
  } catch (IOException | URISyntaxException e) {
   log.error(sm.getString("contextConfig.resourceJarFail", url, context.getName()));
  }
 }
}

代码示例来源:origin: ch.rasc/embeddedtc

log.error("PORT " + this.httpPort + " ALREADY IN USE");
return;
  rootCtx.setPrivileged(true);
  Tomcat.addServlet(rootCtx, "listContexts",
      new ListContextsServlet(rootCtx)).addMapping("/");
DirResourceSet dirResource = new DirResourceSet(resourceRoot,
    "/WEB-INF/classes", "./target/classes", "/");
resourceRoot.addPreResources(dirResource);
ctx.setResources(resourceRoot);
ctx.setPrivileged(true);

代码示例来源:origin: org.apache.tomcat/tomcat-catalina

private WebXml getTomcatWebXmlFragment(WebXmlParser webXmlParser) {
  WebXml webXmlTomcatFragment = createWebXml();
  webXmlTomcatFragment.setOverridable(true);
  // Set to distributable else every app will be prevented from being
  // distributable when the Tomcat fragment is merged with the main
  // web.xml
  webXmlTomcatFragment.setDistributable(true);
  // When merging, the default welcome files are only used if the app has
  // not defined any welcomes files.
  webXmlTomcatFragment.setAlwaysAddWelcomeFiles(false);
  WebResource resource = context.getResources().getResource(Constants.TomcatWebXml);
  if (resource.isFile()) {
    try {
      InputSource source = new InputSource(resource.getURL().toURI().toString());
      source.setByteStream(resource.getInputStream());
      if (!webXmlParser.parseWebXml(source, webXmlTomcatFragment, false)) {
        ok = false;
      }
    } catch (URISyntaxException e) {
      log.error(sm.getString("contextConfig.tomcatWebXmlError"), e);
    }
  }
  return webXmlTomcatFragment;
}

代码示例来源:origin: codefollower/Tomcat-Research

loader.backgroundProcess();
  } catch (Exception e) {
    log.warn(sm.getString(
        "standardContext.backgroundProcess.loader", loader), e);
    manager.backgroundProcess();
  } catch (Exception e) {
    log.warn(sm.getString(
        "standardContext.backgroundProcess.manager", manager),
        e);
if (resources != null) {
  try {
    resources.backgroundProcess();
  } catch (Exception e) {
    log.warn(sm.getString(
        "standardContext.backgroundProcess.resources",
        resources), e);

代码示例来源:origin: org.apache.tomcat/tomcat-catalina

throws IOException {
String appName = context.getName();
List<ManifestResource> appManifestResources = new ArrayList<>();
WebResource resource = resources.getResource("/META-INF/MANIFEST.MF");
if (resource.isFile()) {
  try (InputStream inputStream = resource.getInputStream()) {
    Manifest manifest = new Manifest(inputStream);
    ManifestResource mre = new ManifestResource
      (sm.getString("extensionValidator.web-application-manifest"),
      manifest, ManifestResource.WAR);
    appManifestResources.add(mre);
    resources.getClassLoaderResources("/META-INF/MANIFEST.MF");
for (WebResource manifestResource : manifestResources) {
  if (manifestResource.isFile()) {

代码示例来源:origin: org.apache.tomcat/tomcat-catalina

@Override
public URL getResource(String path) throws MalformedURLException {
  String validatedPath = validateResourcePath(path, false);
  if (validatedPath == null) {
    throw new MalformedURLException(
        sm.getString("applicationContext.requestDispatcher.iae", path));
  }
  WebResourceRoot resources = context.getResources();
  if (resources != null) {
    return resources.getResource(validatedPath).getURL();
  }
  return null;
}

代码示例来源:origin: org.apache.tomcat/tomcat-catalina

@Override
public String getHostName() {
  if (resources != null) {
    Container host = resources.getContext().getParent();
    if (host != null) {
      return host.getName();
    }
  }
  return null;
}

代码示例来源:origin: joinfaces/joinfaces

@Test
public void jarResourcesNull() {
  Context standardContext = mock(Context.class);
  WebResourceRoot webResourceRoot = mock(WebResourceRoot.class);
  Mockito.when(standardContext.getResources()).thenReturn(webResourceRoot);
  Mockito.when(standardContext.getAddWebinfClassesResources()).thenReturn(Boolean.FALSE);
  Mockito.when(webResourceRoot.getJarResources()).thenReturn(null);
  JsfTomcatContextCustomizer jsfTomcatContextCustomizer = new JsfTomcatContextCustomizer();
  jsfTomcatContextCustomizer.customize(standardContext);
  JsfTomcatApplicationListener jsfTomcatApplicationListener = new JsfTomcatApplicationListener(jsfTomcatContextCustomizer.getContext());
  jsfTomcatApplicationListener.onApplicationEvent(mock(ApplicationReadyEvent.class));
  assertThat(jsfTomcatApplicationListener)
    .isNotNull();
}

代码示例来源:origin: justlive1/oxygen

@Override
 public void lifecycleEvent(LifecycleEvent event) {
  if (event.getType().equals(Lifecycle.BEFORE_START_EVENT)) {
   Context context = (Context) event.getLifecycle();
   WebResourceRoot resources = context.getResources();
   if (resources == null) {
    resources = new StandardRoot(context);
    context.setResources(resources);
   }

   // 使用embedded tomcat时 WEB-INF放在了classpath下
   URL resource = context.getParentClassLoader().getResource(Constants.WEB_INF);
   if (resource != null) {
    String webXmlUrlString = resource.toString();
    try {
     URL root = new URL(
       webXmlUrlString.substring(0, webXmlUrlString.length() - Constants.WEB_INF.length()));
     resources.createWebResourceSet(ResourceSetType.RESOURCE_JAR, Constants.WEB_INF_PATH, root,
       Constants.WEB_INF_PATH);
    } catch (MalformedURLException e) {
     // ignore
    }
   }
  }
 }
}

代码示例来源:origin: codefollower/Tomcat-Research

/**
 * Return the context name for this class loader.
 */
public String getContextName() {
  if (resources == null) {
    return "Unknown";
  } else {
    return resources.getContext().getName();
  }
}

代码示例来源:origin: org.joinfaces/jsf-spring-boot-autoconfigure

public ContextMock()  {
  this.standardContext = Mockito.mock(Context.class);
  this.webResourceRoot = Mockito.mock(WebResourceRoot.class);
  LifecycleState state = LifecycleState.NEW;
  Mockito.when(this.webResourceRoot.getContext())
    .thenReturn(this.standardContext);
  Mockito.when(this.webResourceRoot.getState())
    .thenReturn(state);
  Mockito.when(this.standardContext.getResources())
    .thenReturn(this.webResourceRoot);
  Mockito.when(this.standardContext.getAddWebinfClassesResources())
    .thenReturn(Boolean.FALSE);
}

代码示例来源:origin: joinfaces/joinfaces

@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
  if (this.context != null) {
    WebResourceRoot resources = this.context.getResources();
    if (resources != null && resources.getJarResources() != null) {
      TomcatRuntime tomcatRuntime = getTomcatRuntime(resources);

代码示例来源:origin: org.apache.tomcat/tomcat-catalina

setBase(base);
if (root.getContext().getAddWebinfClassesResources()) {
  File f = new File(base, internalPath);
  f = new File(f, "/WEB-INF/classes/META-INF/resources");
    root.createWebResourceSet(ResourceSetType.RESOURCE_JAR, "/",
         f.getAbsolutePath(), null, "/");
if (getRoot().getState().isAvailable()) {
  try {
    start();

代码示例来源:origin: org.apache.tomcat/tomcat-catalina

/**
 * Allocate resources, including proxy.
 * @throws LifecycleException if a start error occurs
 */
public void resourcesStart() throws LifecycleException {
  // Check current status in case resources were added that had already
  // been started
  if (!resources.getState().isAvailable()) {
    resources.start();
  }
  if (effectiveMajorVersion >=3 && addWebinfClassesResources) {
    WebResource webinfClassesResource = resources.getResource(
        "/WEB-INF/classes/META-INF/resources");
    if (webinfClassesResource.isDirectory()) {
      getResources().createWebResourceSet(
          WebResourceRoot.ResourceSetType.RESOURCE_JAR, "/",
          webinfClassesResource.getURL(), "/");
    }
  }
}

代码示例来源:origin: com.github.bordertech.lde/lde-tomcat

/**
 * @param context the context to configure
 * @throws IOException an IO Exception
 * @throws ServletException a Servlet Exception
 */
protected void configWebApp(final Context context) throws IOException, ServletException {
  final String libDir = getLibDir();
  final String classesDir = getClassesDir();
  configJarScanner(context);
  WebResourceRoot resources = new StandardRoot(context);
  context.setResources(resources);
  // Declare an alternative location for the "WEB-INF/lib" dir
  if (libDir != null && !libDir.isEmpty()) {
    resources.addPreResources(new DirResourceSet(resources, Constants.WEB_INF_LIB, libDir, "/"));
  }
  // Declare an alternative location for the "WEB-INF/classes" dir
  if (classesDir != null && !classesDir.isEmpty()) {
    resources.addPreResources(new DirResourceSet(resources, Constants.WEB_INF_CLASSES, classesDir, "/"));
  }
  // Stop persistent sessions
  StandardManager mgr = new StandardManager();
  mgr.setPathname(null);
  context.setManager(mgr);
  // Delay for requets to stop processing in milliseconds
  ((StandardContext) context).setUnloadDelay(10000);
}

代码示例来源:origin: org.apache.tomee/tomee-catalina

@Override
public void createWebResourceSet(final ResourceSetType type, final String webAppMount, final String base, final String archivePath, final String internalPath) {
  delegate.createWebResourceSet(type, webAppMount, base, archivePath, internalPath);
}

相关文章

微信公众号

最新文章

更多