Search in sources :

Example 1 with PackageProvider

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);
    }
}
Also used : Configuration(com.opensymphony.xwork2.config.Configuration) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) BundleEvent(org.osgi.framework.BundleEvent) HashSet(java.util.HashSet) OsgiHost(org.apache.struts2.osgi.host.OsgiHost) BundleListener(org.osgi.framework.BundleListener) FileManagerFactory(com.opensymphony.xwork2.FileManagerFactory) ClassLoaderInterface(com.opensymphony.xwork2.util.finder.ClassLoaderInterface) ActionContext(com.opensymphony.xwork2.ActionContext) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) Bundle(org.osgi.framework.Bundle) Inject(com.opensymphony.xwork2.inject.Inject) Properties(java.util.Properties) Set(java.util.Set) VelocityBundleResourceLoader(org.apache.struts2.osgi.loaders.VelocityBundleResourceLoader) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) VelocityManager(org.apache.struts2.views.velocity.VelocityManager) PackageProvider(com.opensymphony.xwork2.config.PackageProvider) BundleContext(org.osgi.framework.BundleContext) Logger(org.apache.logging.log4j.Logger) ObjectFactory(com.opensymphony.xwork2.ObjectFactory) ServletContext(javax.servlet.ServletContext) Velocity(org.apache.velocity.app.Velocity) LogManager(org.apache.logging.log4j.LogManager) ActionContext(com.opensymphony.xwork2.ActionContext) HashSet(java.util.HashSet) PackageProvider(com.opensymphony.xwork2.config.PackageProvider)

Aggregations

ActionContext (com.opensymphony.xwork2.ActionContext)1 FileManagerFactory (com.opensymphony.xwork2.FileManagerFactory)1 ObjectFactory (com.opensymphony.xwork2.ObjectFactory)1 Configuration (com.opensymphony.xwork2.config.Configuration)1 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)1 PackageProvider (com.opensymphony.xwork2.config.PackageProvider)1 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)1 Inject (com.opensymphony.xwork2.inject.Inject)1 ClassLoaderInterface (com.opensymphony.xwork2.util.finder.ClassLoaderInterface)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1 Set (java.util.Set)1 ServletContext (javax.servlet.ServletContext)1 StringUtils (org.apache.commons.lang3.StringUtils)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 OsgiHost (org.apache.struts2.osgi.host.OsgiHost)1 VelocityBundleResourceLoader (org.apache.struts2.osgi.loaders.VelocityBundleResourceLoader)1 VelocityManager (org.apache.struts2.views.velocity.VelocityManager)1