use of com.opensymphony.xwork2.config.PackageProvider in project struts by apache.
the class OsgiConfigurationProvider method loadConfigFromBundle.
/**
* Loads XML config as well as Convention config from a bundle
* Limitation: Constants and Beans are ignored on XML config
*
* @param bundle the bundle
*/
protected void loadConfigFromBundle(Bundle bundle) {
if (bundle == null) {
// Better than a NPE.
throw new IllegalArgumentException("Cannot load configuration from a null bundle");
}
if (configuration == null) {
// Better than a NPE.
throw new IllegalStateException("Struts OSGi configuration is null. Cannot load bundle configuration");
}
if (bundleAccessor == null) {
LOG.warn("BundleAccessor is null (may not have been injected). May cause NPE to be thrown");
}
if (fileManagerFactory == null) {
LOG.warn("FileManagerFactory is null, FileManagerFactory may not have been set. May cause NPE to be thrown");
}
String bundleName = bundle.getSymbolicName();
LOG.debug("Loading packages from bundle [{}]", bundleName);
// init action context
ActionContext ctx = ActionContext.getContext();
if (ctx == null) {
ctx = createActionContext();
}
try {
// the Convention plugin will use BundleClassLoaderInterface from the ActionContext to find resources
// and load classes
ctx.put(ClassLoaderInterface.CLASS_LOADER_INTERFACE, new BundleClassLoaderInterface());
ctx.put(BundleAccessor.CURRENT_BUNDLE_NAME, bundleName);
LOG.trace("Loading XML config from bundle [{}]", bundleName);
// XML config
PackageLoader loader = new BundlePackageLoader();
loader.loadPackages(bundle, bundleContext, objectFactory, fileManagerFactory, configuration.getPackageConfigs()).stream().map(pkg -> {
configuration.addPackageConfig(pkg.getName(), pkg);
return pkg;
}).forEachOrdered(pkg -> {
bundleAccessor.addPackageFromBundle(bundle, pkg.getName());
});
// Convention
// get the existing packages before reloading the provider (se we can figure out what are the new packages)
Set<String> packagesBeforeLoading = new HashSet<>(configuration.getPackageConfigNames());
PackageProvider conventionPackageProvider = configuration.getContainer().getInstance(PackageProvider.class, "convention.packageProvider");
if (conventionPackageProvider != null) {
LOG.trace("Loading Convention config from bundle [{}]", bundleName);
conventionPackageProvider.loadPackages();
}
Set<String> packagesAfterLoading = new HashSet<>(configuration.getPackageConfigNames());
packagesAfterLoading.removeAll(packagesBeforeLoading);
if (!packagesAfterLoading.isEmpty()) {
// add the new packages to the map of bundle -> package
packagesAfterLoading.forEach(packageName -> {
bundleAccessor.addPackageFromBundle(bundle, packageName);
});
}
if (this.configuration.getRuntimeConfiguration() != null) {
// if there is a runtime config, it meas that this method was called froma bundle start event
// instead of the initial load, in that case, reload the config
this.configuration.rebuildRuntimeConfiguration();
}
} finally {
ctx.put(BundleAccessor.CURRENT_BUNDLE_NAME, null);
ctx.put(ClassLoaderInterface.CLASS_LOADER_INTERFACE, null);
}
}
Aggregations