javax.servlet.ServletContext.getClassLoader()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(9.5k)|赞(0)|评价(0)|浏览(139)

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

ServletContext.getClassLoader介绍

[英]Get the web application class loader associated with this ServletContext.
[中]获取与此ServletContext关联的web应用程序类加载器。

代码示例

代码示例来源:origin: jooby-project/jooby

@Override
public void contextInitialized(final ServletContextEvent sce) {
 ServletContext ctx = sce.getServletContext();
 String appClass = ctx.getInitParameter("application.class");
 requireNonNull(appClass, "Context param NOT found: application.class");
 Jooby.run(Throwing.throwingSupplier(() -> {
  Jooby app = (Jooby) ctx.getClassLoader().loadClass(appClass).newInstance();
  ctx.setAttribute(Jooby.class.getName(), app);
  return app;
 }), "application.path=" + ctx.getContextPath(), "server.module=" + ServletModule.class.getName());
}

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

public PrimeApplicationContext(FacesContext context) {
  this.environment = new PrimeEnvironment(context);
  this.config = new PrimeConfiguration(context, environment);
  if (this.config.isBeanValidationEnabled()) {
    this.validatorFactory = Validation.buildDefaultValidatorFactory();
    this.validator = validatorFactory.getValidator();
  }
  enumCacheMap = new ConcurrentHashMap<>();
  constantsCacheMap = new ConcurrentHashMap<>();
  if (environment.isPortlet()) {
    //the method is new in Porlets3.x, so we can't use it now
    //applicationClassLoader = ((PortletContext) context.getExternalContext().getContext()).getClassLoader();
    applicationClassLoader = LangUtils.getContextClassLoader();
  }
  else if (context.getExternalContext().getContext() instanceof ServletContext) {
    applicationClassLoader = ((ServletContext) context.getExternalContext().getContext()).getClassLoader();
  }
}

代码示例来源:origin: com.liferay.portal/com.liferay.portal.kernel

public HotDeployEvent(ServletContext servletContext) {
  this(servletContext, servletContext.getClassLoader());
}

代码示例来源:origin: camunda/camunda-bpm-platform

public ClassLoader run() {
  return sce.getServletContext().getClassLoader();
 }
});

代码示例来源:origin: camunda/camunda-bpm-platform

public ClassLoader run() {
  return sce.getServletContext().getClassLoader();
 }
});

代码示例来源:origin: camunda/camunda-bpm-platform

public static ClassLoader getServletContextClassloader(final ServletContextEvent sce) {
 if(System.getSecurityManager() != null) {
  return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
   public ClassLoader run() {
    return sce.getServletContext().getClassLoader();
   }
  });
 } else {
  return sce.getServletContext().getClassLoader();
 }
}

代码示例来源:origin: camunda/camunda-bpm-platform

public static ClassLoader getServletContextClassloader(final ServletContextEvent sce) {
 if(System.getSecurityManager() != null) {
  return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
   public ClassLoader run() {
    return sce.getServletContext().getClassLoader();
   }
  });
 } else {
  return sce.getServletContext().getClassLoader();
 }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-web

private ClassLoader getClassLoader() {
  try {
    // if container is Servlet 3.0, use its getClassLoader method
    // this may look odd, but the call below will throw NoSuchMethodError if user is on Servlet 2.5
    // we compile against 3.0 to support Log4jServletContainerInitializer, but we don't require 3.0
    return this.servletContext.getClassLoader();
  } catch (final Throwable ignore) {
    // LOG4J2-248: use TCCL if possible
    return LoaderUtil.getThreadContextClassLoader();
  }
}

代码示例来源:origin: com.liferay.portal/com.liferay.portal.kernel

/**
 * @deprecated As of Judson (7.1.x), with no direct replacement
 */
@Deprecated
public static ClassLoader getClassLoader(String portletId) {
  PortletBag portletBag = PortletBagPool.get(portletId);
  if (portletBag == null) {
    return null;
  }
  ServletContext servletContext = portletBag.getServletContext();
  return servletContext.getClassLoader();
}

代码示例来源:origin: com.vaadin/vaadin-server

/**
 * Tries to determine if the given servlet registration refers to a Vaadin
 * servlet.
 *
 * @param servletRegistration
 *            The servlet registration info for the servlet
 * @return false if the servlet is definitely not a Vaadin servlet, true
 *         otherwise
 */
protected boolean isVaadinServlet(ServletRegistration servletRegistration,
    ServletContext servletContext) {
  try {
    String servletClassName = servletRegistration.getClassName();
    if (servletClassName.equals("com.ibm.ws.wsoc.WsocServlet")) {
      // Websphere servlet which implements websocket endpoints,
      // dynamically added
      return false;
    }
    // Must use servletContext class loader to load servlet class to
    // work correctly in an OSGi environment (#20024)
    Class<?> servletClass = servletContext.getClassLoader()
        .loadClass(servletClassName);
    return VaadinServlet.class.isAssignableFrom(servletClass);
  } catch (Exception e) {
    // This will fail in OSGi environments, assume everything is a
    // VaadinServlet
    return true;
  }
}

代码示例来源:origin: crashub/crash

public void contextInitialized(ServletContextEvent sce) {
 context = sce.getServletContext();
 // Use JVM properties as external config
 setConfig(System.getProperties());
 // Initialise the registerable drivers
 try {
  mountContexts.put("classpath", new ClassPathMountFactory(context.getClassLoader()));
  mountContexts.put("file", new FileMountFactory(Utils.getCurrentDirectory()));
  mountContexts.put("war", new WarMountFactory(context));
 }
 catch (Exception e) {
  log.log(Level.SEVERE, "Coult not initialize classpath driver", e);
  return;
 }
 //
 String contextPath = context.getContextPath();
 synchronized (lock) {
  if (!contextMap.containsKey(contextPath)) {
   ClassLoader webAppLoader = Thread.currentThread().getContextClassLoader();
   PluginDiscovery discovery = createDiscovery(context, webAppLoader);
   PluginContext pluginContext = start(new ServletContextMap(context), discovery, context.getClassLoader());
   contextMap.put(contextPath, pluginContext);
   registered = true;
  }
 }
}

代码示例来源:origin: com.liferay.portal/com.liferay.portal.kernel

public static Object invoke(
    String portletId, MethodKey methodKey, Object... arguments)
  throws Exception {
  portletId = _getRootPortletId(portletId);
  ClassLoader portletClassLoader = PortalClassLoaderUtil.getClassLoader();
  PortletBag portletBag = PortletBagPool.get(portletId);
  if (portletBag != null) {
    ServletContext servletContext = portletBag.getServletContext();
    portletClassLoader = servletContext.getClassLoader();
  }
  Thread currentThread = Thread.currentThread();
  ClassLoader contextClassLoader = currentThread.getContextClassLoader();
  try {
    currentThread.setContextClassLoader(portletClassLoader);
    MethodHandler methodHandler = new MethodHandler(
      methodKey, arguments);
    return methodHandler.invoke();
  }
  finally {
    currentThread.setContextClassLoader(contextClassLoader);
  }
}

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

private static ELInterpreter createInstance(ServletContext context,
    String className) throws Exception {
  return (ELInterpreter) context.getClassLoader().loadClass(
        className).newInstance();
}

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

private static ELInterpreter createInstance(ServletContext context,
    String className) throws Exception {
  return (ELInterpreter) context.getClassLoader().loadClass(
        className).getConstructor().newInstance();
}

代码示例来源:origin: org.apache.johnzon/johnzon-websocket

@Override
public void contextDestroyed(final ServletContextEvent servletContextEvent) {
  final ClassLoader classLoader = servletContextEvent.getServletContext().getClassLoader();
  READER_FACTORY_BY_LOADER.remove(classLoader);
  WRITER_FACTORY_BY_LOADER.remove(classLoader);
}

代码示例来源:origin: caelum/vraptor

public ClassLoader getClassLoader() {
  if (servletContext.getMajorVersion() == 3) {
    try {
      return servletContext.getClassLoader();
    } catch (SecurityException e) {
      logger.error("Could not get class loader from servlet context. " +
          "Using current thread class loader...", e);
    }
  }
  return Thread.currentThread().getContextClassLoader();
}

代码示例来源:origin: org.jooby/jooby-servlet

@Override
public void contextInitialized(final ServletContextEvent sce) {
 ServletContext ctx = sce.getServletContext();
 String appClass = ctx.getInitParameter("application.class");
 requireNonNull(appClass, "Context param NOT found: application.class");
 Jooby.run(Throwing.throwingSupplier(() -> {
  Jooby app = (Jooby) ctx.getClassLoader().loadClass(appClass).newInstance();
  ctx.setAttribute(Jooby.class.getName(), app);
  return app;
 }), "application.path=" + ctx.getContextPath(), "server.module=" + ServletModule.class.getName());
}

代码示例来源:origin: caelum/vraptor

private void defaultExpectations() {
    when(servletContext.getInitParameter(BASE_PACKAGES_PARAMETER_NAME))
      .thenReturn("br.com.caelum.vraptor.ioc.spring.components.registrar");

    when(servletContext.getRealPath("/WEB-INF/classes"))
      .thenReturn(this.getClass().getResource(".").getPath());

    when(servletContext.getInitParameter(SCANNING_PARAM)).thenReturn("enabled");

  when(servletContext.getClassLoader())
    .thenReturn(Thread.currentThread().getContextClassLoader());
  
    when(servletContext.getInitParameterNames()).thenReturn(enumeration(asList(SCANNING_PARAM)));
  }
}

代码示例来源:origin: org.gatein.pc/pc-portlet

public ResourceBundleManager getBundleManager()
{
 if (applicationBundleMgr == null)
 {
   String baseName = ((PortletApplication20MetaData)metaData).getResourceBundle();
   ResourceBundleFactory rbf = new SimpleResourceBundleFactory(webApp.getClassLoader(), baseName);
   applicationBundleMgr = new ResourceBundleManager(null, rbf);
 }
 return applicationBundleMgr;
}

代码示例来源:origin: caelum/vraptor

@Test
public void callerContextDifferentFromRequestedClassLoaderReturnsCurrentClassLoader() {
  ServletContext context = mock(ServletContext.class);
  when(context.getMajorVersion()).thenReturn(3);
  when(context.getClassLoader()).thenThrow(new SecurityException("getClassLoader"));
  WebBasedClasspathResolver resolver = new WebBasedClasspathResolver(context);
  
  assertEquals(resolver.getClassLoader(), Thread.currentThread().getContextClassLoader());
}

相关文章

微信公众号

最新文章

更多

ServletContext类方法