Search in sources :

Example 6 with FileManager

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

the class BundlePackageLoader method loadPackages.

@Deprecated
@Override
public List<PackageConfig> loadPackages(Bundle bundle, BundleContext bundleContext, ObjectFactory objectFactory, FileManagerFactory fileManagerFactory, Map<String, PackageConfig> pkgConfigs) throws ConfigurationException {
    if (pkgConfigs == null) {
        // Better than a NPE.
        throw new IllegalArgumentException("Cannot load packages from a null package configuration");
    }
    Configuration config = new DefaultConfiguration("struts.xml");
    LOG.trace("LoadPackages - After config constructed.  Before BundleConfigurationProvider constructed");
    BundleConfigurationProvider prov = new BundleConfigurationProvider("struts.xml", bundle, bundleContext);
    LOG.trace("LoadPackages - After BundleConfigurationProvider constructed.  Before config.addPackageConfig loop");
    pkgConfigs.values().forEach(pkg -> {
        config.addPackageConfig(pkg.getName(), pkg);
    });
    LOG.trace("LoadPackages - After config.addPackageConfig loop.  Before prov.setObjectFactory()");
    prov.setObjectFactory(objectFactory);
    if (fileManagerFactory == null || fileManagerFactory.getFileManager() == null) {
        LOG.warn("LoadPackages - FileManagerFactory parameter is null or produces a null FileManager, replacing with a new DefaultFileManagerFactory instance");
        final DefaultFileManagerFactory defaultFileManagerFactory = new DefaultFileManagerFactory();
        final Container container = getContextContainer();
        if (container == null) {
            LOG.warn("LoadPackages - Config Container is null.  May cause a NPE to be thrown");
        } else {
            // Apply configuration (including the container reference) to the DefaultFileManagerFactory instance.
            container.inject(defaultFileManagerFactory);
        }
        prov.setFileManagerFactory(defaultFileManagerFactory);
    } else {
        prov.setFileManagerFactory(fileManagerFactory);
    }
    LOG.trace("LoadPackages - After prov.setFileManagerFactory().  Before init()");
    prov.init(config);
    LOG.trace("LoadPackages - After prov.init().  Before loadPackages()");
    prov.loadPackages();
    LOG.trace("LoadPackages - After prov.loadPackages().  Before config.getPackageConfigs().values()");
    List<PackageConfig> list = new ArrayList<>(config.getPackageConfigs().values());
    LOG.trace("LoadPackages - After config.getPackageConfigs().  Before pkgConfigs.values()");
    list.removeAll(pkgConfigs.values());
    return list;
}
Also used : DefaultFileManagerFactory(com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory) Container(com.opensymphony.xwork2.inject.Container) Configuration(com.opensymphony.xwork2.config.Configuration) DefaultConfiguration(com.opensymphony.xwork2.config.impl.DefaultConfiguration) ArrayList(java.util.ArrayList) DefaultConfiguration(com.opensymphony.xwork2.config.impl.DefaultConfiguration) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Example 7 with FileManager

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

the class Dispatcher method init_FileManager.

private void init_FileManager() throws ClassNotFoundException {
    if (initParams.containsKey(StrutsConstants.STRUTS_FILE_MANAGER)) {
        final String fileManagerClassName = initParams.get(StrutsConstants.STRUTS_FILE_MANAGER);
        final Class<FileManager> fileManagerClass = (Class<FileManager>) Class.forName(fileManagerClassName);
        LOG.info("Custom FileManager specified: {}", fileManagerClassName);
        configurationManager.addContainerProvider(new FileManagerProvider(fileManagerClass, fileManagerClass.getSimpleName()));
    } else {
        // add any other Struts 2 provided implementations of FileManager
        configurationManager.addContainerProvider(new FileManagerProvider(JBossFileManager.class, "jboss"));
    }
    if (initParams.containsKey(StrutsConstants.STRUTS_FILE_MANAGER_FACTORY)) {
        final String fileManagerFactoryClassName = initParams.get(StrutsConstants.STRUTS_FILE_MANAGER_FACTORY);
        final Class<FileManagerFactory> fileManagerFactoryClass = (Class<FileManagerFactory>) Class.forName(fileManagerFactoryClassName);
        LOG.info("Custom FileManagerFactory specified: {}", fileManagerFactoryClassName);
        configurationManager.addContainerProvider(new FileManagerFactoryProvider(fileManagerFactoryClass));
    }
}
Also used : FileManagerFactory(com.opensymphony.xwork2.FileManagerFactory) JBossFileManager(org.apache.struts2.util.fs.JBossFileManager) FileManagerFactoryProvider(com.opensymphony.xwork2.config.FileManagerFactoryProvider) FileManager(com.opensymphony.xwork2.FileManager) JBossFileManager(org.apache.struts2.util.fs.JBossFileManager) FileManagerProvider(com.opensymphony.xwork2.config.FileManagerProvider)

Example 8 with FileManager

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

the class JSPLoader method compileJava.

/**
 * Compiles the given source code into java bytecode
 */
private void compileJava(String className, final String source, Set<String> extraClassPath) throws IOException {
    LOG.trace("Compiling [{}], source: [{}]", className, source);
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    // the generated bytecode is fed to the class loader
    JavaFileManager jfm = new ForwardingJavaFileManager<StandardJavaFileManager>(compiler.getStandardFileManager(diagnostics, null, null)) {

        @Override
        public JavaFileObject getJavaFileForOutput(Location location, String name, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
            MemoryJavaFileObject fileObject = new MemoryJavaFileObject(name, kind);
            classLoader.addMemoryJavaFileObject(name, fileObject);
            return fileObject;
        }
    };
    // read java source code from memory
    String fileName = className.replace('.', '/') + ".java";
    SimpleJavaFileObject sourceCodeObject = new SimpleJavaFileObject(toURI(fileName), JavaFileObject.Kind.SOURCE) {

        @Override
        public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException, IllegalStateException, UnsupportedOperationException {
            return source;
        }
    };
    // build classpath
    // some entries will be added multiple times, hence the set
    List<String> optionList = new ArrayList<String>();
    Set<String> classPath = new HashSet<String>();
    // find available jars
    ClassLoaderInterface classLoaderInterface = getClassLoaderInterface();
    UrlSet urlSet = new UrlSet(classLoaderInterface);
    // find jars
    List<URL> urls = urlSet.getUrls();
    if (urls != null && urls.size() > 0) {
        final FileManagerFactory fileManagerFactoryGetInstance = ServletActionContext.getActionContext().getInstance(FileManagerFactory.class);
        final FileManagerFactory contextFileManagerFactory = (fileManagerFactoryGetInstance != null ? fileManagerFactoryGetInstance : (FileManagerFactory) ServletActionContext.getActionContext().get(StrutsConstants.STRUTS_FILE_MANAGER_FACTORY));
        final FileManagerFactory fileManagerFactory = (contextFileManagerFactory != null ? contextFileManagerFactory : new DefaultFileManagerFactory());
        final FileManager fileManagerGetInstance = fileManagerFactory.getFileManager();
        final FileManager contextFileManager = (fileManagerGetInstance != null ? fileManagerGetInstance : (FileManager) ServletActionContext.getActionContext().get(StrutsConstants.STRUTS_FILE_MANAGER));
        final FileManager fileManager = (contextFileManager != null ? contextFileManager : new DefaultFileManager());
        for (URL url : urls) {
            URL normalizedUrl = fileManager.normalizeToFileProtocol(url);
            File file = FileUtils.toFile(ObjectUtils.defaultIfNull(normalizedUrl, url));
            if (file.exists())
                classPath.add(file.getAbsolutePath());
        }
    }
    // these should be in the list already, but I am feeling paranoid
    // this jar
    classPath.add(getJarUrl(EmbeddedJSPResult.class));
    // servlet api
    classPath.add(getJarUrl(Servlet.class));
    // jsp api
    classPath.add(getJarUrl(JspPage.class));
    try {
        Class instanceManager = Class.forName("org.apache.tomcat.InstanceManager");
        classPath.add(getJarUrl(instanceManager));
    } catch (ClassNotFoundException e) {
    // ok ignore
    }
    // add extra classpath entries (jars where tlds were found will be here)
    for (Iterator<String> iterator = extraClassPath.iterator(); iterator.hasNext(); ) {
        String entry = iterator.next();
        classPath.add(entry);
    }
    String classPathString = StringUtils.join(classPath, File.pathSeparator);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Compiling [#0] with classpath [#1]", className, classPathString);
    }
    optionList.addAll(Arrays.asList("-classpath", classPathString));
    // compile
    JavaCompiler.CompilationTask task = compiler.getTask(null, jfm, diagnostics, optionList, null, Arrays.asList(sourceCodeObject));
    if (!task.call()) {
        throw new StrutsException("Compilation failed:" + diagnostics.getDiagnostics().get(0).toString());
    }
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) DefaultFileManagerFactory(com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory) ArrayList(java.util.ArrayList) JspPage(javax.servlet.jsp.JspPage) URL(java.net.URL) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) MemoryJavaFileObject(org.apache.struts2.compiler.MemoryJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) UrlSet(com.opensymphony.xwork2.util.finder.UrlSet) Servlet(javax.servlet.Servlet) DiagnosticCollector(javax.tools.DiagnosticCollector) FileObject(javax.tools.FileObject) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) MemoryJavaFileObject(org.apache.struts2.compiler.MemoryJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) MemoryJavaFileObject(org.apache.struts2.compiler.MemoryJavaFileObject) HashSet(java.util.HashSet) FileManagerFactory(com.opensymphony.xwork2.FileManagerFactory) DefaultFileManagerFactory(com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory) JavaFileManager(javax.tools.JavaFileManager) ForwardingJavaFileManager(javax.tools.ForwardingJavaFileManager) StandardJavaFileManager(javax.tools.StandardJavaFileManager) ClassLoaderInterface(com.opensymphony.xwork2.util.finder.ClassLoaderInterface) JavaCompiler(javax.tools.JavaCompiler) DefaultFileManager(com.opensymphony.xwork2.util.fs.DefaultFileManager) JavaFileManager(javax.tools.JavaFileManager) ForwardingJavaFileManager(javax.tools.ForwardingJavaFileManager) FileManager(com.opensymphony.xwork2.FileManager) StandardJavaFileManager(javax.tools.StandardJavaFileManager) ForwardingJavaFileManager(javax.tools.ForwardingJavaFileManager) DefaultFileManager(com.opensymphony.xwork2.util.fs.DefaultFileManager) File(java.io.File)

Example 9 with FileManager

use of com.opensymphony.xwork2.FileManager 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 10 with FileManager

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

the class AnnotationActionValidatorManagerTest method testGetValidatorsForGivenMethodNameWithoutReloading.

public void testGetValidatorsForGivenMethodNameWithoutReloading() throws ValidationException {
    FileManager fileManager = container.getInstance(FileManagerFactory.class).getFileManager();
    List validatorList = annotationActionValidatorManager.getValidators(SimpleAnnotationAction.class, alias, "execute");
    // disable configuration reload/devmode
    fileManager.setReloadingConfigs(false);
    // 17 in the class level + 0 in the alias
    assertEquals(12, validatorList.size());
    validatorList = annotationActionValidatorManager.getValidators(SimpleAnnotationAction.class, alias, "execute");
    // expect same number of validators
    assertEquals(12, validatorList.size());
}
Also used : FileManagerFactory(com.opensymphony.xwork2.FileManagerFactory) List(java.util.List) SimpleAnnotationAction(com.opensymphony.xwork2.SimpleAnnotationAction) FileManager(com.opensymphony.xwork2.FileManager)

Aggregations

FileManager (com.opensymphony.xwork2.FileManager)12 FileManagerFactory (com.opensymphony.xwork2.FileManagerFactory)6 DefaultFileManager (com.opensymphony.xwork2.util.fs.DefaultFileManager)4 HashSet (java.util.HashSet)3 Container (com.opensymphony.xwork2.inject.Container)2 DefaultFileManagerFactory (com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory)2 File (java.io.File)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 ActionContext (com.opensymphony.xwork2.ActionContext)1 SimpleAnnotationAction (com.opensymphony.xwork2.SimpleAnnotationAction)1 Configuration (com.opensymphony.xwork2.config.Configuration)1 FileManagerFactoryProvider (com.opensymphony.xwork2.config.FileManagerFactoryProvider)1 FileManagerProvider (com.opensymphony.xwork2.config.FileManagerProvider)1 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)1 DefaultConfiguration (com.opensymphony.xwork2.config.impl.DefaultConfiguration)1 XWorkConverter (com.opensymphony.xwork2.conversion.impl.XWorkConverter)1 OgnlTextParser (com.opensymphony.xwork2.util.OgnlTextParser)1 TextParser (com.opensymphony.xwork2.util.TextParser)1 ValueStack (com.opensymphony.xwork2.util.ValueStack)1