Java Code Examples for javax.servlet.ServletContext#getClassLoader()

The following examples show how to use javax.servlet.ServletContext#getClassLoader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EmbeddedKeycloakApplication.java    From spring-boot-keycloak-server-example with Apache License 2.0 6 votes vote down vote up
private static ServletContext augmentToRedirectContextPath(ServletContext servletContext) {

        ClassLoader classLoader = servletContext.getClassLoader();
        Class<?>[] interfaces = {ServletContext.class};
        KeycloakCustomProperties.Server server = customProperties.getServer();

        InvocationHandler invocationHandler = (proxy, method, args) -> {

            if ("getContextPath".equals(method.getName())) {
                return server.getContextPath();
            }

            if ("getInitParameter".equals(method.getName()) && args.length == 1 && "keycloak.embedded".equals(args[0])) {
                return "true";
            }

            LOG.info("Invoke on ServletContext: method=[{}] args=[{}]", method.getName(), Arrays.toString(args));

            return method.invoke(servletContext, args);
        };

        return (ServletContext) Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
    }
 
Example 2
Source File: MeecrowaveBus.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
@Inject
public MeecrowaveBus(final ServletContext context) {
    final ClassLoader appLoader = context.getClassLoader();

    final Meecrowave meecrowave = Meecrowave.class.cast(context.getAttribute("meecrowave.instance"));
    final Configuration builder = Configuration.class.cast(context.getAttribute("meecrowave.configuration"));
    if (meecrowave.getClientBus() == null) {
        delegate = new ConfigurableBus();
        if (builder != null && builder.isJaxrsProviderSetup()) {
            delegate.initProviders(builder, appLoader);
        }
    } else {
        delegate = meecrowave.getClientBus();
        if (builder != null && !builder.isInitializeClientBus() && builder.isJaxrsProviderSetup()) {
            delegate.initProviders(builder, appLoader);
        }
    }

    setProperty(ClassUnwrapper.class.getName(), this);
    setExtension(appLoader, ClassLoader.class); // ServletController locks on the classloader otherwise
}
 
Example 3
Source File: WebAppInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private List<String> extractLibJars(ServletContext webapp) {
    ClassLoader classLoader = webapp.getClassLoader();
    if (classLoader instanceof URLClassLoader) {
        URL[] urls = ((URLClassLoader) classLoader).getURLs();
        return extractLibJarNamesFromURLs(urls);
    } else {
        return Collections.emptyList();
    }
}
 
Example 4
Source File: StandardJarScanner.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
protected void doScanClassPath(JarScanType scanType, ServletContext context,
        JarScannerCallback callback, Set<URL> processedURLs) {
    if (log.isTraceEnabled()) {
        log.trace(sm.getString("jarScan.classloaderStart"));
    }

    ClassLoader stopLoader = null;
    if (!isScanBootstrapClassPath()) {
        // Stop when we reach the bootstrap class loader
        stopLoader = ClassLoader.getSystemClassLoader().getParent();
    }

    ClassLoader classLoader = context.getClassLoader();

    // JARs are treated as application provided until the common class
    // loader is reached.
    boolean isWebapp = true;

    // Use a Deque so URLs can be removed as they are processed
    // and new URLs can be added as they are discovered during
    // processing.
    Deque<URL> classPathUrlsToProcess = new LinkedList<>();

    while (classLoader != null && classLoader != stopLoader) {
        if (classLoader instanceof URLClassLoader) {
            if (isWebapp) {
                isWebapp = isWebappClassLoader(classLoader);
            }

            classPathUrlsToProcess.addAll(
                    Arrays.asList(((URLClassLoader) classLoader).getURLs()));

            processURLs(scanType, callback, processedURLs, isWebapp, classPathUrlsToProcess);
        }
        classLoader = classLoader.getParent();
    }

    if (JreCompat.isJre9Available()) {
        // The application and platform class loaders are not
        // instances of URLClassLoader. Use the class path in this
        // case.
        addClassPath(classPathUrlsToProcess);
        // Also add any modules
        JreCompat.getInstance().addBootModulePath(classPathUrlsToProcess);
        processURLs(scanType, callback, processedURLs, false, classPathUrlsToProcess);
    }
}
 
Example 5
Source File: FatJarScanner.java    From oxygen with Apache License 2.0 4 votes vote down vote up
private void doScanClassPath(JarScanType scanType, ServletContext context,
    JarScannerCallback callback, Set<URL> processedURLs) {
  if (log.isTraceEnabled()) {
    log.trace(SM.getString("jarScan.classloaderStart"));
  }
  ClassLoader classLoader = context.getClassLoader();

  ClassLoader stopLoader = null;
  if (classLoader.getParent() != null) {
    // Stop when we reach the bootstrap class loader
    stopLoader = classLoader.getParent().getParent();
  }

  // JARs are treated as application provided until the common class
  // loader is reached.
  boolean isWebapp = true;

  // Use a Deque so URLs can be removed as they are processed
  // and new URLs can be added as they are discovered during
  // processing.
  Deque<URL> classPathUrlsToProcess = new LinkedList<>();

  while (classLoader != null && classLoader != stopLoader) {
    if (classLoader instanceof URLClassLoader) {
      if (isWebapp) {
        isWebapp = isWebappClassLoader(classLoader);
      }

      classPathUrlsToProcess.addAll(Arrays.asList(((URLClassLoader) classLoader).getURLs()));

      processURLs(scanType, callback, processedURLs, isWebapp, classPathUrlsToProcess);
    }
    classLoader = classLoader.getParent();
  }

  if (JreCompat.isJre9Available()) {
    // The application and platform class loaders are not
    // instances of URLClassLoader. Use the class path in this
    // case.
    addClassPath(classPathUrlsToProcess);
    // Also add any modules
    JreCompat.getInstance().addBootModulePath(classPathUrlsToProcess);
    processURLs(scanType, callback, processedURLs, false, classPathUrlsToProcess);
  }
}
 
Example 6
Source File: ClassLoaderAwareServletContainerInitializer.java    From flow with Apache License 2.0 4 votes vote down vote up
/**
 * Overridden to use different classloaders if needed.
 * <p>
 * {@inheritDoc}
 */
@Override
default void onStartup(Set<Class<?>> set, ServletContext ctx)
        throws ServletException {
    ClassLoader webClassLoader = ctx.getClassLoader();
    ClassLoader classLoader = getClass().getClassLoader();

    /*
     * Hack is needed to make a workaround for weird behavior of WildFly
     * with skinnywar See https://github.com/vaadin/flow/issues/7805
     */
    boolean noHack = false;
    while (classLoader != null) {
        if (classLoader.equals(webClassLoader)) {
            noHack = true;
            break;
        } else {
            /*
             * The classloader which has loaded this class ({@code
             * classLoader}) should be either the {@code webClassLoader} or
             * its child: in this case it knows how to handle the classes
             * loaded by the {@code webClassLoader} : it either is able to
             * load them itself or delegate to its parent (which is the
             * {@code webClassLoader}): in this case hack is not needed and
             * the {@link #process(Set, ServletContext)} method can be
             * called directly.
             */
            classLoader = classLoader.getParent();
        }
    }

    if (noHack) {
        process(set, ctx);
        return;
    }

    try {
        Class<?> initializer = ctx.getClassLoader()
                .loadClass(getClass().getName());

        String processMethodName = Stream
                .of(ClassLoaderAwareServletContainerInitializer.class
                        .getDeclaredMethods())
                .filter(method -> !method.isDefault()
                        && !method.isSynthetic())
                .findFirst().get().getName();
        Method operation = Stream.of(initializer.getDeclaredMethods())
                .filter(method -> method.getName()
                        .equals(processMethodName))
                .findFirst().get();
        operation.invoke(initializer.newInstance(),
                new Object[] { set, ctx });
    } catch (ClassNotFoundException | IllegalAccessException
            | IllegalArgumentException | InvocationTargetException
            | InstantiationException e) {
        throw new ServletException(e);
    }
}