Search in sources :

Example 1 with ResourceFinder

use of com.opensymphony.xwork2.util.finder.ResourceFinder in project struts by apache.

the class BaseOsgiHost method addExportedPackages.

/**
 * Find sub-packages of the packages defined in the property file and export them
 *
 * @param strutsConfigProps Struts-OSGi configuration properties containing the {@link SCANNING_PACKAGE_INCLUDES} property
 *                          containing comma-separated top-level package values.
 * @param configProps OSGi configuration properties for which the {@link Constants.FRAMEWORK_SYSTEMPACKAGES} property's
 *                    value will have the sub-packages of strutsConfigProps appended to it.
 *                    If no {@link Constants.FRAMEWORK_SYSTEMPACKAGES} property exists, and the exported packages is non-empty,
 *                    then one is created that will contain the sub-packages of strutsConfigProps.
 */
protected void addExportedPackages(Properties strutsConfigProps, Properties configProps) {
    if (strutsConfigProps == null) {
        throw new IllegalArgumentException("Cannot add exported packages using a null struts config properties reference");
    }
    LOG.debug("  scanning.package.includes lookup returns: [{}]", (String) strutsConfigProps.get(SCANNING_PACKAGE_INCLUDES));
    String[] rootPackages = StringUtils.split((String) strutsConfigProps.get(SCANNING_PACKAGE_INCLUDES), ",");
    ResourceFinder finder = new ResourceFinder(StringUtils.EMPTY);
    List<String> exportedPackages = new ArrayList<>();
    if (rootPackages == null || rootPackages.length == 0) {
        LOG.warn("Struts config roperties required key [{}] is missing or empty.  No exported packages are available to add", SCANNING_PACKAGE_INCLUDES);
        return;
    }
    // build a list of subpackages
    for (String rootPackage : rootPackages) {
        LOG.debug("  Looking for root package: [{}] ", rootPackage);
        try {
            String version = null;
            if (rootPackage.indexOf(";") > 0) {
                String[] splitted = rootPackage.split(";");
                rootPackage = splitted[0];
                version = splitted[1];
            }
            Map<URL, Set<String>> subpackagesMap = finder.findPackagesMap(StringUtils.replace(rootPackage.trim(), ".", "/"));
            LOG.debug("  Trimmed package map for: [{}] has size: [{}]", rootPackage.trim(), subpackagesMap.size());
            for (Map.Entry<URL, Set<String>> entry : subpackagesMap.entrySet()) {
                URL url = entry.getKey();
                Set<String> packages = entry.getValue();
                // get version if not set
                if (StringUtils.isBlank(version)) {
                    version = getVersion(url);
                    LOG.debug("  Version was null.  Retrieved version: [{}] for [{}]", version, (url != null ? url.toString() : null));
                }
                if (packages != null) {
                    LOG.debug("  Subpackages size: [{}]", packages.size());
                    for (String subpackage : packages) {
                        LOG.trace("  Adding subppackage: [{}; version=\"{}\"]", subpackage, version);
                        exportedPackages.add(subpackage + "; version=\"" + version + "\"");
                    }
                } else {
                    LOG.debug("  Subpackages is null");
                }
            }
        } catch (IOException e) {
            LOG.error("Unable to find subpackages of [{}]", rootPackage, e);
        }
    }
    // make a string with the exported packages and add it to the system properties
    if (!exportedPackages.isEmpty() && configProps != null) {
        String systemPackages = (String) configProps.get(Constants.FRAMEWORK_SYSTEMPACKAGES);
        if (systemPackages == null || systemPackages.isEmpty()) {
            LOG.warn("Config properties required key [{}] is missing or empty.  Only the exported packages will be present", Constants.FRAMEWORK_SYSTEMPACKAGES);
            systemPackages = StringUtils.join(exportedPackages, ",");
        } else {
            systemPackages = StringUtils.removeEnd(systemPackages, ",") + "," + StringUtils.join(exportedPackages, ",");
        }
        configProps.put(Constants.FRAMEWORK_SYSTEMPACKAGES, systemPackages);
        LOG.debug("Adding exported framework packages: [{}]", systemPackages);
    } else {
        LOG.warn("NOT adding any exported framework packages.  No exported packages or config props is null");
    }
}
Also used : ResourceFinder(com.opensymphony.xwork2.util.finder.ResourceFinder) Set(java.util.Set) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URL(java.net.URL) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ResourceFinder

use of com.opensymphony.xwork2.util.finder.ResourceFinder in project struts by apache.

the class DefaultResultMapBuilder method createFromResources.

/**
 * Creates any result types from the resources available in the web application. This scans the
 * web application resources using the servlet context.
 *
 * @param   actionClass The action class the results are being built for.
 * @param   results The results map to put the result configs created into.
 * @param   resultPath The calculated path to the resources.
 * @param   resultPrefix The prefix for the result. This is usually <code>/resultPath/actionName</code>.
 * @param   actionName The action name which is used only for logging in this implementation.
 * @param   packageConfig The package configuration which is passed along in order to determine
 * @param   resultsByExtension The map of extensions to result type configuration instances.
 */
protected void createFromResources(Class<?> actionClass, Map<String, ResultConfig> results, final String resultPath, final String resultPrefix, final String actionName, PackageConfig packageConfig, Map<String, ResultTypeConfig> resultsByExtension) {
    if (LOG.isTraceEnabled()) {
        LOG.trace("Searching for results in the Servlet container at [{}]" + " with result prefix of [#1]", resultPath, resultPrefix);
    }
    // Build from web application using the ServletContext
    @SuppressWarnings("unchecked") Set<String> paths = servletContext.getResourcePaths(flatResultLayout ? resultPath : resultPrefix);
    if (paths != null) {
        for (String path : paths) {
            LOG.trace("Processing resource path [{}]", path);
            String fileName = StringUtils.substringAfterLast(path, "/");
            if (StringUtils.isBlank(fileName) || StringUtils.startsWith(fileName, ".")) {
                LOG.trace("Ignoring file without name [{}]", path);
                continue;
            } else if (fileName.lastIndexOf(".") > 0) {
                String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
                if (conventionsService.getResultTypesByExtension(packageConfig).get(suffix) == null) {
                    LOG.debug("No result type defined for file suffix : [{}]. Ignoring file {}", suffix, fileName);
                    continue;
                }
            }
            makeResults(actionClass, path, resultPrefix, results, packageConfig, resultsByExtension);
        }
    }
    // Building from the classpath
    String classPathLocation = resultPath.startsWith("/") ? resultPath.substring(1, resultPath.length()) : resultPath;
    if (LOG.isTraceEnabled()) {
        LOG.trace("Searching for results in the class path at [{}]" + " with a result prefix of [{}] and action name [{}]", classPathLocation, resultPrefix, actionName);
    }
    ResourceFinder finder = new ResourceFinder(classPathLocation, getClassLoaderInterface());
    try {
        Map<String, URL> matches = finder.getResourcesMap("");
        if (matches != null) {
            Test<URL> resourceTest = getResourceTest(resultPath, actionName);
            for (Map.Entry<String, URL> entry : matches.entrySet()) {
                if (resourceTest.test(entry.getValue())) {
                    LOG.trace("Processing URL [{}]", entry.getKey());
                    String urlStr = entry.getValue().toString();
                    int index = urlStr.lastIndexOf(resultPrefix);
                    String path = urlStr.substring(index);
                    makeResults(actionClass, path, resultPrefix, results, packageConfig, resultsByExtension);
                }
            }
        }
    } catch (IOException ex) {
        LOG.error("Unable to scan directory [{}] for results", ex, classPathLocation);
    }
}
Also used : ResourceFinder(com.opensymphony.xwork2.util.finder.ResourceFinder) IOException(java.io.IOException) URL(java.net.URL)

Example 3 with ResourceFinder

use of com.opensymphony.xwork2.util.finder.ResourceFinder in project struts by apache.

the class BaseOsgiHost method getRunLevelDirs.

/**
 * @param dir directory
 * @return  a list of directories under a directory whose name is a number
 */
protected Map<String, String> getRunLevelDirs(String dir) {
    Map<String, String> dirs = new HashMap<>();
    try {
        ResourceFinder finder = new ResourceFinder();
        URL url = finder.find("bundles");
        if (url != null) {
            if ("file".equals(url.getProtocol())) {
                File bundlesDir = new File(url.toURI());
                String[] runLevelDirs = bundlesDir.list((File file, String name) -> {
                    try {
                        return file.isDirectory() && Integer.valueOf(name) > 0;
                    } catch (NumberFormatException ex) {
                        // the name is not a number
                        return false;
                    }
                });
                if (runLevelDirs != null && runLevelDirs.length > 0) {
                    // add all the dirs to the list
                    for (String runLevel : runLevelDirs) {
                        dirs.put(runLevel, StringUtils.removeEnd(dir, "/") + "/" + runLevel);
                    }
                } else {
                    LOG.debug("No run level directories found under the [{}] directory", dir);
                }
            } else {
                LOG.warn("Unable to read [{}] directory.  Protocol [{}] is not supported (try exploded WAR files)", dir, url.getProtocol());
            }
        } else {
            LOG.warn("The [{}] directory was not found", dir);
        }
    } catch (Exception e) {
        LOG.warn("Unable load bundles from the [{}] directory", dir, e);
    }
    return dirs;
}
Also used : ResourceFinder(com.opensymphony.xwork2.util.finder.ResourceFinder) HashMap(java.util.HashMap) JarFile(java.util.jar.JarFile) File(java.io.File) URL(java.net.URL) IOException(java.io.IOException) StrutsException(org.apache.struts2.StrutsException)

Example 4 with ResourceFinder

use of com.opensymphony.xwork2.util.finder.ResourceFinder in project struts by apache.

the class BaseOsgiHost method getBundlesInDir.

protected List<String> getBundlesInDir(String dir) {
    List<String> bundleJars = new ArrayList<>();
    try {
        ResourceFinder finder = new ResourceFinder();
        URL url = finder.find(dir);
        if (url != null) {
            if ("file".equals(url.getProtocol())) {
                File bundlesDir = new File(url.toURI());
                File[] bundles = bundlesDir.listFiles((File file, String name) -> StringUtils.endsWith(name, ".jar"));
                if (bundles != null && bundles.length > 0) {
                    // add all the bundles to the list
                    for (File bundle : bundles) {
                        String externalForm = bundle.toURI().toURL().toExternalForm();
                        LOG.debug("Adding bundle [{}]", externalForm);
                        bundleJars.add(externalForm);
                    }
                } else {
                    LOG.debug("No bundles found under the [{}] directory", dir);
                }
            } else {
                LOG.warn("Unable to read [{}] directory.  Protocol [{}] is not supported (try exploded WAR files)", dir, url.getProtocol());
            }
        } else {
            LOG.warn("The [{}] directory was not found", dir);
        }
    } catch (Exception e) {
        LOG.warn("Unable load bundles from the [{}] directory", dir, e);
    }
    return bundleJars;
}
Also used : ResourceFinder(com.opensymphony.xwork2.util.finder.ResourceFinder) ArrayList(java.util.ArrayList) JarFile(java.util.jar.JarFile) File(java.io.File) URL(java.net.URL) IOException(java.io.IOException) StrutsException(org.apache.struts2.StrutsException)

Aggregations

ResourceFinder (com.opensymphony.xwork2.util.finder.ResourceFinder)4 IOException (java.io.IOException)4 URL (java.net.URL)4 File (java.io.File)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 JarFile (java.util.jar.JarFile)2 StrutsException (org.apache.struts2.StrutsException)2 Map (java.util.Map)1 Set (java.util.Set)1