Search in sources :

Example 6 with FileManagerFactory

use of com.opensymphony.xwork2.FileManagerFactory in project struts by apache.

the class NotURLClassLoader method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    result = new EmbeddedJSPResult();
    request = EasyMock.createNiceMock(HttpServletRequest.class);
    response = new MockHttpServletResponse();
    context = new MockServletContext();
    config = new MockServletConfig(context);
    final Map params = new HashMap();
    HttpSession session = EasyMock.createNiceMock(HttpSession.class);
    EasyMock.replay(session);
    EasyMock.expect(request.getSession()).andReturn(session).anyTimes();
    EasyMock.expect(request.getParameterMap()).andReturn(params).anyTimes();
    EasyMock.expect(request.getParameter("username")).andAnswer(() -> ActionContext.getContext().getParameters().get("username").getValue());
    EasyMock.expect(request.getAttribute("something")).andReturn("somethingelse").anyTimes();
    EasyMock.replay(request);
    // mock value stack
    ValueStack valueStack = EasyMock.createNiceMock(ValueStack.class);
    EasyMock.expect(valueStack.getActionContext()).andReturn(ActionContext.getContext()).anyTimes();
    EasyMock.replay(valueStack);
    // mock converter
    XWorkConverter converter = new DummyConverter();
    DefaultFileManager fileManager = new DefaultFileManager();
    fileManager.setReloadingConfigs(false);
    // mock container
    Container container = EasyMock.createNiceMock(Container.class);
    EasyMock.expect(container.getInstance(XWorkConverter.class)).andReturn(converter).anyTimes();
    TextParser parser = new OgnlTextParser();
    EasyMock.expect(container.getInstance(TextParser.class)).andReturn(parser).anyTimes();
    EasyMock.expect(container.getInstanceNames(FileManager.class)).andReturn(new HashSet<>()).anyTimes();
    EasyMock.expect(container.getInstance(FileManager.class)).andReturn(fileManager).anyTimes();
    UrlHelper urlHelper = new DefaultUrlHelper();
    EasyMock.expect(container.getInstance(UrlHelper.class)).andReturn(urlHelper).anyTimes();
    FileManagerFactory fileManagerFactory = new DummyFileManagerFactory();
    EasyMock.expect(container.getInstance(FileManagerFactory.class)).andReturn(fileManagerFactory).anyTimes();
    EasyMock.replay(container);
    ActionContext.of(new HashMap<>()).withParameters(HttpParameters.create(params).build()).withServletRequest(request).withServletResponse(response).withServletContext(context).withContainer(container).withValueStack(valueStack).bind();
}
Also used : OgnlTextParser(com.opensymphony.xwork2.util.OgnlTextParser) FileManagerFactory(com.opensymphony.xwork2.FileManagerFactory) DefaultUrlHelper(org.apache.struts2.views.util.DefaultUrlHelper) UrlHelper(org.apache.struts2.views.util.UrlHelper) ValueStack(com.opensymphony.xwork2.util.ValueStack) TextParser(com.opensymphony.xwork2.util.TextParser) OgnlTextParser(com.opensymphony.xwork2.util.OgnlTextParser) HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) MockServletConfig(org.springframework.mock.web.MockServletConfig) MockServletContext(org.springframework.mock.web.MockServletContext) DefaultFileManager(com.opensymphony.xwork2.util.fs.DefaultFileManager) FileManager(com.opensymphony.xwork2.FileManager) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Container(com.opensymphony.xwork2.inject.Container) DefaultFileManager(com.opensymphony.xwork2.util.fs.DefaultFileManager) XWorkConverter(com.opensymphony.xwork2.conversion.impl.XWorkConverter) DefaultUrlHelper(org.apache.struts2.views.util.DefaultUrlHelper) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HashSet(java.util.HashSet)

Example 7 with FileManagerFactory

use of com.opensymphony.xwork2.FileManagerFactory 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

FileManagerFactory (com.opensymphony.xwork2.FileManagerFactory)5 FileManager (com.opensymphony.xwork2.FileManager)4 DefaultFileManager (com.opensymphony.xwork2.util.fs.DefaultFileManager)4 Configuration (com.opensymphony.xwork2.config.Configuration)3 DefaultFileManagerFactory (com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory)3 HashSet (java.util.HashSet)3 ActionContext (com.opensymphony.xwork2.ActionContext)2 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)2 DefaultConfiguration (com.opensymphony.xwork2.config.impl.DefaultConfiguration)2 Container (com.opensymphony.xwork2.inject.Container)2 ClassLoaderInterface (com.opensymphony.xwork2.util.finder.ClassLoaderInterface)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ServletContext (javax.servlet.ServletContext)2 ObjectFactory (com.opensymphony.xwork2.ObjectFactory)1 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)1 FileManagerFactoryProvider (com.opensymphony.xwork2.config.FileManagerFactoryProvider)1 FileManagerProvider (com.opensymphony.xwork2.config.FileManagerProvider)1 PackageProvider (com.opensymphony.xwork2.config.PackageProvider)1