Search in sources :

Example 1 with WebDirContext

use of org.apache.naming.resources.WebDirContext in project Payara by payara.

the class WebappClassLoader method setResources.

/**
 * Set associated resources.
 */
public void setResources(DirContext resources) {
    this.resources = resources;
    DirContext res = resources;
    if (resources instanceof ProxyDirContext) {
        ProxyDirContext proxyRes = (ProxyDirContext) res;
        contextName = proxyRes.getContextName();
        res = proxyRes.getDirContext();
    }
    if (res instanceof WebDirContext) {
        ((WebDirContext) res).setJarFileResourcesProvider(this);
    }
}
Also used : WebDirContext(org.apache.naming.resources.WebDirContext) ProxyDirContext(org.apache.naming.resources.ProxyDirContext) DirContext(javax.naming.directory.DirContext) WebDirContext(org.apache.naming.resources.WebDirContext) ProxyDirContext(org.apache.naming.resources.ProxyDirContext)

Example 2 with WebDirContext

use of org.apache.naming.resources.WebDirContext in project Payara by payara.

the class WebappClassLoaderTest method check_findResources_thread_safety.

@Test
public void check_findResources_thread_safety() throws Exception {
    final WebappClassLoader webappClassLoader = new WebappClassLoader(getClass().getClassLoader(), null);
    webappClassLoader.start();
    webappClassLoader.setResources(new WebDirContext());
    webappClassLoader.addRepository(junitJarFile.getAbsolutePath(), junitJarFile);
    CompletableFuture<Void> result = new CompletableFuture<>();
    // Create the tasks to run
    Runnable lookupTask = waitAndDo(result, () -> findResources(webappClassLoader));
    Runnable addTask = waitAndDo(result, () -> add(webappClassLoader));
    Runnable closeTask = waitAndDo(result, () -> webappClassLoader.closeJARs(true));
    try {
        // Run the methods at the same time
        for (int i = 0; i < EXECUTION_COUNT; i++) {
            executor.execute(addTask);
            executor.execute(lookupTask);
            executor.execute(closeTask);
        }
        // Wait for tasks to execute
        assertTrue("The tasks didn't finish in the allowed time.", latch.await(20, TimeUnit.SECONDS));
        // Check to see if any tasks completed exceptionally
        result.getNow(null);
    } finally {
        webappClassLoader.close();
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) WebDirContext(org.apache.naming.resources.WebDirContext) Test(org.junit.Test)

Example 3 with WebDirContext

use of org.apache.naming.resources.WebDirContext in project Payara by payara.

the class WarHandler method getClassLoader.

@Override
public ClassLoader getClassLoader(final ClassLoader parent, final DeploymentContext context) {
    Application applicationTemp = context.getModuleMetaData(Application.class);
    boolean hotDeploy = context.getCommandParameters(DeployCommandParameters.class).hotDeploy;
    final Application application = applicationTemp == null ? Application.createApplication() : applicationTemp;
    WebappClassLoader cloader = AccessController.doPrivileged(new PrivilegedAction<WebappClassLoader>() {

        @Override
        public WebappClassLoader run() {
            return new WebappClassLoader(parent, application, hotDeploy);
        }
    });
    try {
        WebDirContext r = new WebDirContext();
        File base = new File(context.getSource().getURI());
        r.setDocBase(base.getAbsolutePath());
        cloader.setResources(r);
        cloader.addRepository("WEB-INF/classes/", new File(base, "WEB-INF/classes/"));
        if (context.getScratchDir("ejb") != null) {
            cloader.addRepository(context.getScratchDir("ejb").toURI().toURL().toString().concat("/"));
        }
        if (context.getScratchDir("jsp") != null) {
            cloader.setWorkDir(context.getScratchDir("jsp"));
        }
        // add libraries referenced from manifest
        for (URL url : getManifestLibraries(context)) {
            cloader.addRepository(url.toString());
        }
        WebXmlParser webXmlParser = getWebXmlParser(context.getSource(), application);
        configureLoaderAttributes(cloader, webXmlParser, base);
        configureLoaderProperties(cloader, webXmlParser, base);
        configureContextXmlAttribute(cloader, base, context);
        try {
            doPrivileged(new SetPermissionsAction(CommponentType.war, context, cloader));
        } catch (PrivilegedActionException e) {
            throw new SecurityException(e.getException());
        }
    } catch (XMLStreamException xse) {
        logger.log(SEVERE, xse.getMessage());
        if (logger.isLoggable(FINE)) {
            logger.log(FINE, xse.getMessage(), xse);
        }
        xse.printStackTrace();
    } catch (IOException ioe) {
        logger.log(SEVERE, ioe.getMessage());
        if (logger.isLoggable(FINE)) {
            logger.log(FINE, ioe.getMessage(), ioe);
        }
        ioe.printStackTrace();
    }
    cloader.start();
    return cloader;
}
Also used : WebDirContext(org.apache.naming.resources.WebDirContext) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) URL(java.net.URL) DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) WebappClassLoader(org.glassfish.web.loader.WebappClassLoader) XMLStreamException(javax.xml.stream.XMLStreamException) Application(com.sun.enterprise.deployment.Application) JarFile(java.util.jar.JarFile) File(java.io.File) SetPermissionsAction(com.sun.enterprise.security.permissionsxml.SetPermissionsAction)

Example 4 with WebDirContext

use of org.apache.naming.resources.WebDirContext in project Payara by payara.

the class StandardContext method start.

/**
 * Start this Context component.
 *
 * @exception LifecycleException if a startup error occurs
 */
@Override
public synchronized void start() throws LifecycleException {
    if (started) {
        if (log.isLoggable(Level.INFO)) {
            log.log(Level.INFO, LogFacade.CONTAINER_ALREADY_STARTED_EXCEPTION, logName());
        }
        return;
    }
    long startupTimeStart = System.currentTimeMillis();
    if (!initialized) {
        try {
            init();
        } catch (Exception ex) {
            throw new LifecycleException("Error initializaing ", ex);
        }
    }
    if (log.isLoggable(Level.FINE)) {
        log.log(Level.FINE, "Starting {0}", "".equals(getName()) ? "ROOT" : getName());
    }
    // Set JMX object name for proper pipeline registration
    preRegisterJMX();
    // Notify our interested LifecycleListeners
    lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
    setAvailable(false);
    setConfigured(false);
    // Add missing components as necessary
    if (webappResources == null) {
        // (1) Required by Loader
        if (log.isLoggable(Level.FINE)) {
            log.log(Level.FINE, "Configuring default Resources");
        }
        try {
            if ((docBase != null) && (docBase.endsWith(".war")) && (!(new File(docBase).isDirectory()))) {
                setResources(new WARDirContext());
            } else {
                setResources(new WebDirContext());
            }
        } catch (IllegalArgumentException e) {
            throw new LifecycleException(rb.getString(LogFacade.INIT_RESOURCES_EXCEPTION), e);
        }
    }
    resourcesStart();
    // Add alternate resources
    if (alternateDocBases != null && !alternateDocBases.isEmpty()) {
        for (AlternateDocBase alternateDocBase : alternateDocBases) {
            String docBase = alternateDocBase.getDocBase();
            if (log.isLoggable(Level.FINE)) {
                log.log(Level.FINE, "Configuring alternate resources");
            }
            try {
                if (docBase != null && docBase.endsWith(".war") && (!(new File(docBase).isDirectory()))) {
                    setAlternateResources(alternateDocBase, new WARDirContext());
                } else {
                    setAlternateResources(alternateDocBase, new FileDirContext());
                }
            } catch (IllegalArgumentException e) {
                throw new LifecycleException(rb.getString(LogFacade.INIT_RESOURCES_EXCEPTION), e);
            }
        }
        alternateResourcesStart();
    }
    if (getLoader() == null) {
        createLoader();
    }
    // Initialize character set mapper
    getCharsetMapper();
    // Post work directory
    postWorkDirectory();
    // Validate required extensions
    try {
        ExtensionValidator.validateApplication(getResources(), this);
    } catch (IOException ioe) {
        String msg = MessageFormat.format(rb.getString(LogFacade.DEPENDENCY_CHECK_EXCEPTION), this);
        throw new LifecycleException(msg, ioe);
    }
    // Reading the "catalina.useNaming" environment variable
    String useNamingProperty = System.getProperty("catalina.useNaming");
    if ((useNamingProperty != null) && ("false".equals(useNamingProperty))) {
        useNaming = false;
    }
    if (isUseNaming()) {
        if (namingContextListener == null) {
            namingContextListener = new NamingContextListener();
            namingContextListener.setDebug(getDebug());
            namingContextListener.setName(getNamingContextName());
            addLifecycleListener(namingContextListener);
        }
    }
    // Binding thread
    // START OF SJSAS 8.1 6174179
    // ClassLoader oldCCL = bindThread();
    ClassLoader oldCCL = null;
    try {
        started = true;
        // Start our subordinate components, if any
        if ((loader != null) && (loader instanceof Lifecycle)) {
            ((Lifecycle) loader).start();
        }
        if ((logger != null) && (logger instanceof Lifecycle)) {
            ((Lifecycle) logger).start();
        }
        // Unbinding thread
        // START OF SJSAS 8.1 6174179
        // unbindThread(oldCCL);
        // END OF SJSAS 8.1 6174179
        // Binding thread
        oldCCL = bindThread();
        if ((realm != null) && (realm instanceof Lifecycle)) {
            ((Lifecycle) realm).start();
        }
        if ((resources != null) && (resources instanceof Lifecycle)) {
            ((Lifecycle) resources).start();
        }
        // Start our child containers, if any
        for (Container child : findChildren()) {
            if (child instanceof Lifecycle) {
                ((Lifecycle) child).start();
            }
        }
        // if any
        if (pipeline instanceof Lifecycle) {
            ((Lifecycle) pipeline).start();
        }
        // START SJSAS 8.1 5049111
        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(START_EVENT, null);
    // END SJSAS 8.1 5049111
    } catch (Throwable t) {
        throw new LifecycleException(t);
    } finally {
        // Unbinding thread
        unbindThread(oldCCL);
    }
    if (!getConfigured()) {
        String msg = MessageFormat.format(rb.getString(LogFacade.STARTUP_CONTEXT_FAILED_EXCEPTION), getName());
        throw new LifecycleException(msg);
    }
    // Store some required info as ServletContext attributes
    postResources();
    if (orderedLibs != null && !orderedLibs.isEmpty()) {
        getServletContext().setAttribute(ServletContext.ORDERED_LIBS, orderedLibs);
        context.setAttributeReadOnly(ServletContext.ORDERED_LIBS);
    }
    // Initialize associated mapper
    mapper.setContext(getPath(), welcomeFiles, ContextsAdapterUtility.wrap(resources));
    // Binding thread
    oldCCL = bindThread();
    try {
        // Set up the context init params
        mergeParameters();
        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);
        // Support for pluggability : this has to be done before
        // listener events are fired
        callServletContainerInitializers();
        // Configure and call application event listeners
        contextListenerStart();
        // Start manager
        if ((manager != null) && (manager instanceof Lifecycle)) {
            ((Lifecycle) getManager()).start();
        }
        // Start ContainerBackgroundProcessor thread
        super.threadStart();
        // Configure and call application filters
        filterStart();
        // Load and initialize all "load on startup" servlets
        loadOnStartup(findChildren());
    } catch (Throwable t) {
        log.log(Level.SEVERE, LogFacade.STARTUP_CONTEXT_FAILED_EXCEPTION, getName());
        try {
            stop();
        } catch (Throwable tt) {
            log.log(Level.SEVERE, LogFacade.CLEANUP_FAILED_EXCEPTION, tt);
        }
        throw new LifecycleException(t);
    } finally {
        // Unbinding thread
        unbindThread(oldCCL);
    }
    // Set available status depending upon startup success
    if (log.isLoggable(Level.FINEST)) {
        log.log(Level.FINEST, "Startup successfully completed");
    }
    setAvailable(true);
    // JMX registration
    registerJMX();
    startTimeMillis = System.currentTimeMillis();
    startupTime = startTimeMillis - startupTimeStart;
    // Send j2ee.state.running notification
    if (getObjectName() != null) {
        Notification notification = new Notification("j2ee.state.running", this, sequenceNumber++);
        sendNotification(notification);
    }
    // of files on startup
    if (getLoader() instanceof WebappLoader) {
        ((WebappLoader) getLoader()).closeJARs(true);
    }
}
Also used : LifecycleException(org.apache.catalina.LifecycleException) WebDirContext(org.apache.naming.resources.WebDirContext) WARDirContext(org.apache.naming.resources.WARDirContext) FileDirContext(org.apache.naming.resources.FileDirContext) Lifecycle(org.apache.catalina.Lifecycle) IOException(java.io.IOException) LifecycleException(org.apache.catalina.LifecycleException) MalformedObjectNameException(javax.management.MalformedObjectNameException) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) NamingException(javax.naming.NamingException) MBeanRegistrationException(javax.management.MBeanRegistrationException) MalformedURLException(java.net.MalformedURLException) Notification(javax.management.Notification) Container(org.apache.catalina.Container) AlternateDocBase(org.glassfish.grizzly.http.server.util.AlternateDocBase) WebappClassLoader(org.glassfish.web.loader.WebappClassLoader) WebappLoader(org.apache.catalina.loader.WebappLoader) File(java.io.File)

Aggregations

WebDirContext (org.apache.naming.resources.WebDirContext)4 File (java.io.File)2 IOException (java.io.IOException)2 WebappClassLoader (org.glassfish.web.loader.WebappClassLoader)2 Application (com.sun.enterprise.deployment.Application)1 SetPermissionsAction (com.sun.enterprise.security.permissionsxml.SetPermissionsAction)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 PrivilegedActionException (java.security.PrivilegedActionException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 JarFile (java.util.jar.JarFile)1 MBeanRegistrationException (javax.management.MBeanRegistrationException)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 Notification (javax.management.Notification)1 NamingException (javax.naming.NamingException)1 DirContext (javax.naming.directory.DirContext)1 ServletException (javax.servlet.ServletException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 Container (org.apache.catalina.Container)1 Lifecycle (org.apache.catalina.Lifecycle)1