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");
}
}
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);
}
}
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;
}
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;
}
Aggregations