Search in sources :

Example 1 with ResourceAttributes

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

the class WebappClassLoader method findResourceInternalFromRepositories.

/**
 * Attempts to load the requested resource from this classloader's
 * internal repositories.
 *
 * @return The requested resource, or null if not found
 */
private ResourceEntry findResourceInternalFromRepositories(String name, String path) {
    if (repositories == null) {
        return null;
    }
    ResourceEntry entry = null;
    int contentLength = -1;
    InputStream binaryStream = null;
    int repositoriesLength = repositories.length;
    Resource resource = null;
    for (int i = 0; (entry == null) && (i < repositoriesLength); i++) {
        try {
            String fullPath = repositories[i] + path;
            Object lookupResult = resources.lookup(fullPath);
            if (lookupResult instanceof Resource) {
                resource = (Resource) lookupResult;
            }
            // found
            if (securityManager != null) {
                PrivilegedAction<ResourceEntry> dp = new PrivilegedFindResource(files[i], path);
                entry = AccessController.doPrivileged(dp);
            } else {
                entry = findResourceInternal(files[i], path);
            }
            ResourceAttributes attributes = (ResourceAttributes) resources.getAttributes(fullPath);
            contentLength = (int) attributes.getContentLength();
            entry.lastModified = attributes.getLastModified();
            if (resource != null) {
                try {
                    binaryStream = resource.streamContent();
                } catch (IOException e) {
                    return null;
                }
                // Note: Only syncing on a 'constant' object is needed
                synchronized (ALL_PERMISSION) {
                    int j;
                    long[] result2 = new long[lastModifiedDates.length + 1];
                    for (j = 0; j < lastModifiedDates.length; j++) {
                        result2[j] = lastModifiedDates[j];
                    }
                    result2[lastModifiedDates.length] = entry.lastModified;
                    lastModifiedDates = result2;
                    String[] result = new String[paths.length + 1];
                    for (j = 0; j < paths.length; j++) {
                        result[j] = paths[j];
                    }
                    result[paths.length] = fullPath;
                    paths = result;
                }
            }
        } catch (NamingException e) {
        }
    }
    if (entry != null) {
        readEntryData(entry, name, binaryStream, contentLength, null);
    }
    return entry;
}
Also used : Resource(org.apache.naming.resources.Resource) ResourceAttributes(org.apache.naming.resources.ResourceAttributes) NamingException(javax.naming.NamingException)

Example 2 with ResourceAttributes

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

the class HostConfig method checkContextLastModified.

/**
 * Check deployment descriptors last modified date.
 */
protected void checkContextLastModified() {
    if (!(host instanceof Deployer))
        return;
    Deployer deployer = (Deployer) host;
    String[] contextNames = deployer.findDeployedApps();
    for (int i = 0; i < contextNames.length; i++) {
        String contextName = contextNames[i];
        Context context = deployer.findDeployedApp(contextName);
        if (!(context instanceof Lifecycle))
            continue;
        try {
            DirContext resources = context.getResources();
            if (resources == null) {
                // the context
                continue;
            }
            ResourceAttributes webXmlAttributes = (ResourceAttributes) resources.getAttributes("/WEB-INF/web.xml");
            ResourceAttributes webInfAttributes = (ResourceAttributes) resources.getAttributes("/WEB-INF");
            long newLastModified = webXmlAttributes.getLastModified();
            long webInfLastModified = webInfAttributes.getLastModified();
            Long lastModified = webXmlLastModified.get(contextName);
            if (lastModified == null) {
                webXmlLastModified.put(contextName, Long.valueOf(newLastModified));
            } else {
                if (lastModified.longValue() != newLastModified) {
                    if (newLastModified > (webInfLastModified + 5000)) {
                        webXmlLastModified.remove(contextName);
                        restartContext(context);
                    } else {
                        webXmlLastModified.put(contextName, Long.valueOf(newLastModified));
                    }
                }
            }
        } catch (NamingException e) {
            // Ignore
            ;
        }
        Long lastModified = contextXmlLastModified.get(contextName);
        String configBase = configBase().getPath();
        String configFileName = context.getConfigFile();
        if (configFileName != null) {
            File configFile = new File(configFileName);
            if (!configFile.isAbsolute()) {
                configFile = new File(System.getProperty("catalina.base"), configFile.getPath());
            }
            long newLastModified = configFile.lastModified();
            if (lastModified == null) {
                contextXmlLastModified.put(contextName, Long.valueOf(newLastModified));
            } else {
                if (lastModified.longValue() != newLastModified) {
                    contextXmlLastModified.remove(contextName);
                    String fileName = configFileName;
                    if (fileName.startsWith(configBase)) {
                        fileName = fileName.substring(configBase.length() + 1);
                        try {
                            deployed.remove(fileName);
                            if (host.findChild(contextName) != null) {
                                ((Deployer) host).remove(contextName);
                            }
                        } catch (Throwable t) {
                            String msg = MessageFormat.format(rb.getString(LogFacade.ERROR_UNDEPLOYING_JAR_FILE_EXCEPTION), fileName);
                            log.log(Level.SEVERE, msg, t);
                        }
                        deployApps();
                    }
                }
            }
        }
    }
    // Check for WAR modification
    if (isUnpackWARs()) {
        File appBase = appBase();
        if (!appBase.exists() || !appBase.isDirectory())
            return;
        String[] files = appBase.list();
        for (int i = 0; i < files.length; i++) {
            if (files[i].endsWith(".war")) {
                File dir = new File(appBase, files[i]);
                Long lastModified = warLastModified.get(files[i]);
                long dirLastModified = dir.lastModified();
                if (lastModified == null) {
                    warLastModified.put(files[i], Long.valueOf(dir.lastModified()));
                } else if (dirLastModified > lastModified.longValue()) {
                    // The WAR has been modified: redeploy
                    String expandedDir = files[i];
                    int period = expandedDir.lastIndexOf(".");
                    if (period >= 0)
                        expandedDir = expandedDir.substring(0, period);
                    File expanded = new File(appBase, expandedDir);
                    String contextPath = "/" + expandedDir;
                    if (contextPath.equals("/ROOT"))
                        contextPath = "";
                    if (dirLastModified > expanded.lastModified()) {
                        try {
                            // Undeploy current application
                            deployed.remove(files[i]);
                            deployed.remove(expandedDir + ".xml");
                            if (host.findChild(contextPath) != null) {
                                ((Deployer) host).remove(contextPath, false);
                                ExpandWar.deleteDir(expanded);
                            }
                        } catch (Throwable t) {
                            String msg = MessageFormat.format(rb.getString(LogFacade.ERROR_UNDEPLOYING_JAR_FILE_EXCEPTION), files[i]);
                            log.log(Level.SEVERE, msg, t);
                        }
                        deployApps();
                    }
                    // the last modified values
                    if (host.findChild(contextPath) != null) {
                        webXmlLastModified.remove(contextPath);
                        warLastModified.put(files[i], Long.valueOf(dir.lastModified()));
                    }
                }
            }
        }
    }
}
Also used : DirContext(javax.naming.directory.DirContext) DirContext(javax.naming.directory.DirContext) ResourceAttributes(org.apache.naming.resources.ResourceAttributes) NamingException(javax.naming.NamingException) JarFile(java.util.jar.JarFile)

Example 3 with ResourceAttributes

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

the class WebappClassLoader method modified.

/**
 * Have one or more classes or resources been modified so that a reload
 * is appropriate?
 */
public boolean modified() {
    if (logger.isLoggable(Level.FINER))
        logger.log(Level.FINER, "modified()");
    // Checking for modified loaded resources
    int length = paths.length;
    // A rare race condition can occur in the updates of the two arrays
    // It's totally ok if the latest class added is not checked (it will
    // be checked the next time
    int length2 = lastModifiedDates.length;
    if (length > length2)
        length = length2;
    for (int i = 0; i < length; i++) {
        try {
            long lastModified = ((ResourceAttributes) resources.getAttributes(paths[i])).getLastModified();
            if (lastModified != lastModifiedDates[i]) {
                if (logger.isLoggable(Level.FINER))
                    logger.log(Level.FINER, "  Resource '" + paths[i] + "' was modified; Date is now: " + new java.util.Date(lastModified) + " Was: " + new java.util.Date(lastModifiedDates[i]));
                return (true);
            }
        } catch (NamingException e) {
            logger.log(Level.SEVERE, LogFacade.MISSING_RESOURCE, paths[i]);
            return (true);
        }
    }
    length = jarNames.size();
    // Check if JARs have been added or removed
    if (getJarPath() != null) {
        try {
            NamingEnumeration<Binding> enumeration = resources.listBindings(getJarPath());
            int i = 0;
            while (enumeration.hasMoreElements() && (i < length)) {
                NameClassPair ncPair = enumeration.nextElement();
                String name = ncPair.getName();
                // START OF IASRI 4657979
                if (!name.endsWith(".jar") && !name.endsWith(".zip"))
                    // END OF IASRI 4657979
                    continue;
                if (!name.equals(jarNames.get(i))) {
                    // Missing JAR
                    logger.log(Level.FINER, "    Additional JARs have been added : '" + name + "'");
                    return (true);
                }
                i++;
            }
            if (enumeration.hasMoreElements()) {
                while (enumeration.hasMoreElements()) {
                    NameClassPair ncPair = enumeration.nextElement();
                    String name = ncPair.getName();
                    // START OF IASRI 4657979
                    if (name.endsWith(".jar") || name.endsWith(".zip")) {
                        // END OF IASRI 4657979
                        // There was more JARs
                        logger.log(Level.FINER, "    Additional JARs have been added");
                        return (true);
                    }
                }
            } else if (i < jarNames.size()) {
                // There was less JARs
                logger.log(Level.FINER, "    Additional JARs have been added");
                return (true);
            }
        } catch (NamingException e) {
            if (logger.isLoggable(Level.FINER))
                logger.log(Level.FINER, "    Failed tracking modifications of '" + getJarPath() + "'");
        } catch (ClassCastException e) {
            logger.log(Level.SEVERE, LogFacade.FAILED_TRACKING_MODIFICATIONS, new Object[] { getJarPath(), e.getMessage() });
        }
    }
    // No classes have been modified
    return (false);
}
Also used : Binding(javax.naming.Binding) java.util(java.util) NameClassPair(javax.naming.NameClassPair) NamingException(javax.naming.NamingException) ResourceAttributes(org.apache.naming.resources.ResourceAttributes)

Aggregations

NamingException (javax.naming.NamingException)3 ResourceAttributes (org.apache.naming.resources.ResourceAttributes)3 java.util (java.util)1 JarFile (java.util.jar.JarFile)1 Binding (javax.naming.Binding)1 NameClassPair (javax.naming.NameClassPair)1 DirContext (javax.naming.directory.DirContext)1 Resource (org.apache.naming.resources.Resource)1