Search in sources :

Example 1 with FileDirContext

use of org.apache.naming.resources.FileDirContext in project tomcat70 by apache.

the class StandardContext method resourcesStart.

/**
 * Allocate resources, including proxy.
 * Return <code>true</code> if initialization was successfull,
 * or <code>false</code> otherwise.
 */
public boolean resourcesStart() {
    boolean ok = true;
    Hashtable<String, String> env = new Hashtable<String, String>();
    if (getParent() != null)
        env.put(ProxyDirContext.HOST, getParent().getName());
    env.put(ProxyDirContext.CONTEXT, getName());
    try {
        ProxyDirContext proxyDirContext = new ProxyDirContext(env, webappResources);
        if (webappResources instanceof FileDirContext) {
            filesystemBased = true;
            ((FileDirContext) webappResources).setAllowLinking(isAllowLinking());
        }
        if (webappResources instanceof BaseDirContext) {
            ((BaseDirContext) webappResources).setDocBase(getBasePath());
            ((BaseDirContext) webappResources).setCached(isCachingAllowed());
            ((BaseDirContext) webappResources).setCacheTTL(getCacheTTL());
            ((BaseDirContext) webappResources).setCacheMaxSize(getCacheMaxSize());
            ((BaseDirContext) webappResources).allocate();
            // Alias support
            ((BaseDirContext) webappResources).setAliases(getAliases());
            if (effectiveMajorVersion >= 3 && addWebinfClassesResources) {
                try {
                    DirContext webInfCtx = (DirContext) webappResources.lookup("/WEB-INF/classes");
                    // Do the lookup to make sure it exists
                    webInfCtx.lookup("META-INF/resources");
                    ((BaseDirContext) webappResources).addAltDirContext(webInfCtx);
                } catch (NamingException e) {
                // Doesn't exist - ignore and carry on
                }
            }
        }
        // Register the cache in JMX
        if (isCachingAllowed() && proxyDirContext.getCache() != null) {
            String contextName = getName();
            if (!contextName.startsWith("/")) {
                contextName = "/" + contextName;
            }
            ObjectName resourcesName = new ObjectName(this.getDomain() + ":type=Cache,host=" + getHostname() + ",context=" + contextName);
            Registry.getRegistry(null, null).registerComponent(proxyDirContext.getCache(), resourcesName, null);
        }
        super.setResources(proxyDirContext);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        log.error(sm.getString("standardContext.resourcesStart"), t);
        ok = false;
    }
    return (ok);
}
Also used : Hashtable(java.util.Hashtable) FileDirContext(org.apache.naming.resources.FileDirContext) BaseDirContext(org.apache.naming.resources.BaseDirContext) NamingException(javax.naming.NamingException) WARDirContext(org.apache.naming.resources.WARDirContext) BaseDirContext(org.apache.naming.resources.BaseDirContext) EmptyDirContext(org.apache.naming.resources.EmptyDirContext) ProxyDirContext(org.apache.naming.resources.ProxyDirContext) DirContext(javax.naming.directory.DirContext) FileDirContext(org.apache.naming.resources.FileDirContext) ProxyDirContext(org.apache.naming.resources.ProxyDirContext) ObjectName(javax.management.ObjectName)

Example 2 with FileDirContext

use of org.apache.naming.resources.FileDirContext in project tomcat70 by apache.

the class StandardContext method setResources.

/**
 * Set the resources DirContext object with which this Container is
 * associated.
 *
 * @param resources The newly associated DirContext
 */
@Override
public synchronized void setResources(DirContext resources) {
    if (getState().isAvailable()) {
        throw new IllegalStateException(sm.getString("standardContext.resourcesStart"));
    }
    DirContext oldResources = this.webappResources;
    if (oldResources == resources)
        return;
    if (resources instanceof BaseDirContext) {
        // Caching
        ((BaseDirContext) resources).setCached(isCachingAllowed());
        ((BaseDirContext) resources).setCacheTTL(getCacheTTL());
        ((BaseDirContext) resources).setCacheMaxSize(getCacheMaxSize());
        ((BaseDirContext) resources).setCacheObjectMaxSize(getCacheObjectMaxSize());
        // Alias support
        ((BaseDirContext) resources).setAliases(getAliases());
    }
    if (resources instanceof FileDirContext) {
        filesystemBased = true;
        ((FileDirContext) resources).setAllowLinking(isAllowLinking());
    }
    this.webappResources = resources;
    // The proxied resources will be refreshed on start
    super.setResources(null);
    support.firePropertyChange("resources", oldResources, this.webappResources);
}
Also used : FileDirContext(org.apache.naming.resources.FileDirContext) BaseDirContext(org.apache.naming.resources.BaseDirContext) WARDirContext(org.apache.naming.resources.WARDirContext) BaseDirContext(org.apache.naming.resources.BaseDirContext) EmptyDirContext(org.apache.naming.resources.EmptyDirContext) ProxyDirContext(org.apache.naming.resources.ProxyDirContext) DirContext(javax.naming.directory.DirContext) FileDirContext(org.apache.naming.resources.FileDirContext)

Example 3 with FileDirContext

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

the class StandardContext method setResources.

/**
 * Set the resources DirContext object with which this Container is
 * associated.
 *
 * @param resources The newly associated DirContext
 */
@Override
public synchronized void setResources(DirContext resources) {
    if (started) {
        throw new IllegalStateException(rb.getString(LogFacade.RESOURCES_STARTED));
    }
    DirContext oldResources = this.webappResources;
    if (oldResources == resources) {
        return;
    }
    if (resources instanceof BaseDirContext) {
        BaseDirContext baseDirContext = (BaseDirContext) resources;
        baseDirContext.setCached(isCachingAllowed());
        baseDirContext.setCacheTTL(getCacheTTL());
        baseDirContext.setCacheMaxSize(getCacheMaxSize());
    }
    if (resources instanceof FileDirContext) {
        filesystemBased = true;
        FileDirContext fileDirContext = (FileDirContext) resources;
        fileDirContext.setCaseSensitive(isCaseSensitive());
        fileDirContext.setAllowLinking(isAllowLinking());
    }
    this.webappResources = resources;
    // The proxied resources will be refreshed on start
    this.resources = null;
    support.firePropertyChange("resources", oldResources, this.webappResources);
}
Also used : FileDirContext(org.apache.naming.resources.FileDirContext) BaseDirContext(org.apache.naming.resources.BaseDirContext) WARDirContext(org.apache.naming.resources.WARDirContext) BaseDirContext(org.apache.naming.resources.BaseDirContext) ProxyDirContext(org.apache.naming.resources.ProxyDirContext) DirContext(javax.naming.directory.DirContext) WebDirContext(org.apache.naming.resources.WebDirContext) FileDirContext(org.apache.naming.resources.FileDirContext)

Example 4 with FileDirContext

use of org.apache.naming.resources.FileDirContext in project tomcat70 by apache.

the class TestVirtualWebappLoader method testStartInternal.

@Test
public void testStartInternal() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp-3.0");
    // Must have a real docBase - just use temp
    StandardContext ctx = (StandardContext) tomcat.addContext("", appDir.getAbsolutePath());
    VirtualWebappLoader loader = new VirtualWebappLoader();
    loader.setContainer(ctx);
    ctx.setLoader(loader);
    ctx.setResources(new FileDirContext());
    ctx.resourcesStart();
    File dir = new File("test/webapp-3.0-fragments/WEB-INF/lib");
    loader.setVirtualClasspath(dir.getAbsolutePath() + "/*.jar");
    loader.start();
    String[] repos = loader.getRepositories();
    Assert.assertEquals(2, repos.length);
    loader.stop();
    // ToDo: Why doesn't remove repositories?
    repos = loader.getRepositories();
    Assert.assertEquals(2, repos.length);
    // no leak
    loader.start();
    repos = loader.getRepositories();
    Assert.assertEquals(2, repos.length);
    // clear loader
    ctx.setLoader(null);
    // see tearDown()!
    tomcat.start();
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) FileDirContext(org.apache.naming.resources.FileDirContext) StandardContext(org.apache.catalina.core.StandardContext) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 5 with FileDirContext

use of org.apache.naming.resources.FileDirContext in project tomcat70 by apache.

the class StandardContext method startInternal.

/**
 * Start this component and implement the requirements
 * of {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
@Override
protected synchronized void startInternal() throws LifecycleException {
    if (log.isDebugEnabled())
        log.debug("Starting " + getBaseName());
    // Send j2ee.state.starting notification
    if (this.getObjectName() != null) {
        Notification notification = new Notification("j2ee.state.starting", this.getObjectName(), sequenceNumber.getAndIncrement());
        broadcaster.sendNotification(notification);
    }
    setConfigured(false);
    boolean ok = true;
    // ensure the NamingResources follows the correct lifecycle
    if (namingResources != null) {
        namingResources.start();
    }
    // Add missing components as necessary
    if (webappResources == null) {
        // (1) Required by Loader
        if (log.isDebugEnabled())
            log.debug("Configuring default Resources");
        try {
            String docBase = getDocBase();
            if (docBase == null) {
                setResources(new EmptyDirContext());
            } else if (docBase.endsWith(".war") && !(new File(getBasePath())).isDirectory()) {
                setResources(new WARDirContext());
            } else {
                setResources(new FileDirContext());
            }
        } catch (IllegalArgumentException e) {
            log.error(sm.getString("standardContext.resourcesInit"), e);
            ok = false;
        }
    }
    if (ok) {
        if (!resourcesStart()) {
            throw new LifecycleException("Error in resourceStart()");
        }
    }
    if (getLoader() == null) {
        WebappLoader webappLoader = new WebappLoader(getParentClassLoader());
        webappLoader.setDelegate(getDelegate());
        setLoader(webappLoader);
    }
    // Initialize character set mapper
    getCharsetMapper();
    // Post work directory
    postWorkDirectory();
    // Validate required extensions
    boolean dependencyCheck = true;
    try {
        dependencyCheck = ExtensionValidator.validateApplication(getResources(), this);
    } catch (IOException ioe) {
        log.error(sm.getString("standardContext.extensionValidationError"), ioe);
        dependencyCheck = false;
    }
    if (!dependencyCheck) {
        // do not make application available if dependency check fails
        ok = false;
    }
    // Reading the "catalina.useNaming" environment variable
    String useNamingProperty = System.getProperty("catalina.useNaming");
    if ((useNamingProperty != null) && (useNamingProperty.equals("false"))) {
        useNaming = false;
    }
    if (ok && isUseNaming()) {
        if (getNamingContextListener() == null) {
            NamingContextListener ncl = new NamingContextListener();
            ncl.setName(getNamingContextName());
            ncl.setExceptionOnFailedWrite(getJndiExceptionOnFailedWrite());
            addLifecycleListener(ncl);
            setNamingContextListener(ncl);
        }
    }
    // Standard container startup
    if (log.isDebugEnabled())
        log.debug("Processing standard container startup");
    // Binding thread
    ClassLoader oldCCL = bindThread();
    try {
        if (ok) {
            // Start our subordinate components, if any
            Loader loader = getLoaderInternal();
            if ((loader != null) && (loader instanceof Lifecycle))
                ((Lifecycle) loader).start();
            // since the loader just started, the webapp classloader is now
            // created.
            // By calling unbindThread and bindThread in a row, we setup the
            // current Thread CCL to be the webapp classloader
            unbindThread(oldCCL);
            oldCCL = bindThread();
            // Initialize logger again. Other components might have used it
            // too early, so it should be reset.
            logger = null;
            getLogger();
            Cluster cluster = getClusterInternal();
            if ((cluster != null) && (cluster instanceof Lifecycle))
                ((Lifecycle) cluster).start();
            Realm realm = getRealmInternal();
            if ((realm != null) && (realm instanceof Lifecycle))
                ((Lifecycle) realm).start();
            DirContext resources = getResourcesInternal();
            if ((resources != null) && (resources instanceof Lifecycle))
                ((Lifecycle) resources).start();
            // Notify our interested LifecycleListeners
            fireLifecycleEvent(Lifecycle.CONFIGURE_START_EVENT, null);
            // Start our child containers, if not already started
            for (Container child : findChildren()) {
                if (!child.getState().isAvailable()) {
                    child.start();
                }
            }
            // if any
            if (pipeline instanceof Lifecycle) {
                ((Lifecycle) pipeline).start();
            }
            // Acquire clustered manager
            Manager contextManager = null;
            Manager manager = getManagerInternal();
            if (manager == null) {
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("standardContext.cluster.noManager", Boolean.valueOf((getCluster() != null)), Boolean.valueOf(distributable)));
                }
                if ((getCluster() != null) && distributable) {
                    try {
                        contextManager = getCluster().createManager(getName());
                    } catch (Exception ex) {
                        log.error("standardContext.clusterFail", ex);
                        ok = false;
                    }
                } else {
                    contextManager = new StandardManager();
                }
                manager = contextManager;
            }
            // Configure default manager if none was specified
            if (contextManager != null) {
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("standardContext.manager", contextManager.getClass().getName()));
                }
                setManager(contextManager);
            }
            if (manager != null && (getCluster() != null) && distributable) {
                // let the cluster know that there is a context that is distributable
                // and that it has its own manager
                getCluster().registerManager(manager);
            }
        }
    } finally {
        // Unbinding thread
        unbindThread(oldCCL);
    }
    if (!getConfigured()) {
        log.error(sm.getString("standardContext.configurationFail"));
        ok = false;
    }
    // We put the resources into the servlet context
    if (ok)
        getServletContext().setAttribute(Globals.RESOURCES_ATTR, getResources());
    // Initialize associated mapper
    mapper.setContext(getPath(), welcomeFiles, getResources());
    // Binding thread
    oldCCL = bindThread();
    if (ok) {
        if (getInstanceManager() == null) {
            javax.naming.Context context = null;
            if (isUseNaming() && getNamingContextListener() != null) {
                context = getNamingContextListener().getEnvContext();
            }
            Map<String, Map<String, String>> injectionMap = buildInjectionMap(getIgnoreAnnotations() ? new NamingResources() : getNamingResources());
            setInstanceManager(new DefaultInstanceManager(context, injectionMap, this, this.getClass().getClassLoader()));
            getServletContext().setAttribute(InstanceManager.class.getName(), getInstanceManager());
        }
    }
    try {
        // Create context attributes that will be required
        if (ok) {
            getServletContext().setAttribute(JarScanner.class.getName(), getJarScanner());
        }
        // Set up the context init params
        mergeParameters();
        // Call ServletContainerInitializers
        for (Map.Entry<ServletContainerInitializer, Set<Class<?>>> entry : initializers.entrySet()) {
            try {
                entry.getKey().onStartup(entry.getValue(), getServletContext());
            } catch (ServletException e) {
                log.error(sm.getString("standardContext.sciFail"), e);
                ok = false;
                break;
            }
        }
        // Configure and call application event listeners
        if (ok) {
            if (!listenerStart()) {
                log.error(sm.getString("standardContext.listenerFail"));
                ok = false;
            }
        }
        try {
            // Start manager
            Manager manager = getManagerInternal();
            if ((manager != null) && (manager instanceof Lifecycle)) {
                ((Lifecycle) getManager()).start();
            }
        } catch (Exception e) {
            log.error(sm.getString("standardContext.managerFail"), e);
            ok = false;
        }
        // Configure and call application filters
        if (ok) {
            if (!filterStart()) {
                log.error(sm.getString("standardContext.filterFail"));
                ok = false;
            }
        }
        // Load and initialize all "load on startup" servlets
        if (ok) {
            if (!loadOnStartup(findChildren())) {
                log.error(sm.getString("standardContext.servletFail"));
                ok = false;
            }
        }
        // Start ContainerBackgroundProcessor thread
        super.threadStart();
    } finally {
        // Unbinding thread
        unbindThread(oldCCL);
    }
    // Set available status depending upon startup success
    if (ok) {
        if (log.isDebugEnabled())
            log.debug("Starting completed");
    } else {
        log.error(sm.getString("standardContext.startFailed", getName()));
    }
    startTime = System.currentTimeMillis();
    // Send j2ee.state.running notification
    if (ok && (this.getObjectName() != null)) {
        Notification notification = new Notification("j2ee.state.running", this.getObjectName(), sequenceNumber.getAndIncrement());
        broadcaster.sendNotification(notification);
    }
    // of files on startup
    if (getLoader() instanceof WebappLoader) {
        ((WebappLoader) getLoader()).closeJARs(true);
    }
    // Reinitializing if something went wrong
    if (!ok) {
        setState(LifecycleState.FAILED);
    } else {
        setState(LifecycleState.STARTING);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) FileDirContext(org.apache.naming.resources.FileDirContext) InstanceManager(org.apache.tomcat.InstanceManager) NamingResources(org.apache.catalina.deploy.NamingResources) WebappLoader(org.apache.catalina.loader.WebappLoader) Loader(org.apache.catalina.Loader) WARDirContext(org.apache.naming.resources.WARDirContext) BaseDirContext(org.apache.naming.resources.BaseDirContext) EmptyDirContext(org.apache.naming.resources.EmptyDirContext) ProxyDirContext(org.apache.naming.resources.ProxyDirContext) DirContext(javax.naming.directory.DirContext) FileDirContext(org.apache.naming.resources.FileDirContext) Manager(org.apache.catalina.Manager) InstanceManager(org.apache.tomcat.InstanceManager) StandardManager(org.apache.catalina.session.StandardManager) StandardJarScanner(org.apache.tomcat.util.scan.StandardJarScanner) JarScanner(org.apache.tomcat.JarScanner) Notification(javax.management.Notification) ServletContainerInitializer(javax.servlet.ServletContainerInitializer) ServletException(javax.servlet.ServletException) Container(org.apache.catalina.Container) Realm(org.apache.catalina.Realm) LifecycleException(org.apache.catalina.LifecycleException) WARDirContext(org.apache.naming.resources.WARDirContext) Lifecycle(org.apache.catalina.Lifecycle) StandardManager(org.apache.catalina.session.StandardManager) EmptyDirContext(org.apache.naming.resources.EmptyDirContext) Cluster(org.apache.catalina.Cluster) IOException(java.io.IOException) LifecycleException(org.apache.catalina.LifecycleException) ListenerNotFoundException(javax.management.ListenerNotFoundException) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) NamingException(javax.naming.NamingException) MalformedURLException(java.net.MalformedURLException) WebappLoader(org.apache.catalina.loader.WebappLoader) File(java.io.File) FilterMap(org.apache.catalina.deploy.FilterMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Aggregations

FileDirContext (org.apache.naming.resources.FileDirContext)10 WARDirContext (org.apache.naming.resources.WARDirContext)6 File (java.io.File)5 NamingException (javax.naming.NamingException)5 DirContext (javax.naming.directory.DirContext)5 BaseDirContext (org.apache.naming.resources.BaseDirContext)5 ProxyDirContext (org.apache.naming.resources.ProxyDirContext)5 IOException (java.io.IOException)3 MalformedURLException (java.net.MalformedURLException)3 WebDirContext (org.apache.naming.resources.WebDirContext)3 URL (java.net.URL)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Set (java.util.Set)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Notification (javax.management.Notification)2 ServletContainerInitializer (javax.servlet.ServletContainerInitializer)2 ServletException (javax.servlet.ServletException)2