Search in sources :

Example 1 with FileManager

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

the class DefaultFileManagerFactory method lookupFileManager.

private FileManager lookupFileManager() {
    Set<String> names = container.getInstanceNames(FileManager.class);
    LOG.debug("Found following implementations of FileManager interface: {}", names);
    Set<FileManager> internals = new HashSet<>();
    Set<FileManager> users = new HashSet<>();
    for (String fmName : names) {
        FileManager fm = container.getInstance(FileManager.class, fmName);
        if (fm.internal()) {
            internals.add(fm);
        } else {
            users.add(fm);
        }
    }
    for (FileManager fm : users) {
        if (fm.support()) {
            LOG.debug("Using FileManager implementation [{}]", fm.getClass().getSimpleName());
            return fm;
        }
    }
    LOG.debug("No user defined FileManager, looking up for internal implementations!");
    for (FileManager fm : internals) {
        if (fm.support()) {
            return fm;
        }
    }
    return null;
}
Also used : FileManager(com.opensymphony.xwork2.FileManager) HashSet(java.util.HashSet)

Example 2 with FileManager

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

the class DefaultFileManagerFactory method getFileManager.

public FileManager getFileManager() {
    if (fileManagerHolder != null) {
        return fileManagerHolder.getFileManager();
    }
    FileManager fileManager = lookupFileManager();
    if (fileManager != null) {
        LOG.debug("Using FileManager implementation [{}]", fileManager.getClass().getSimpleName());
        fileManager.setReloadingConfigs(reloadingConfigs);
        fileManagerHolder = new FileManagerHolder(fileManager);
        return fileManager;
    }
    LOG.debug("Using default implementation of FileManager provided under name [system]: {}", systemFileManager.getClass().getSimpleName());
    systemFileManager.setReloadingConfigs(reloadingConfigs);
    fileManagerHolder = new FileManagerHolder(systemFileManager);
    return systemFileManager;
}
Also used : FileManager(com.opensymphony.xwork2.FileManager)

Example 3 with FileManager

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

the class DummyFileManager method testFileManagerFactoryWithRealConfig.

public void testFileManagerFactoryWithRealConfig() throws Exception {
    // given
    DefaultFileManagerFactory factory = new DefaultFileManagerFactory();
    container.inject(factory);
    // when
    FileManager fm = factory.getFileManager();
    // then
    assertTrue(fm instanceof DefaultFileManager);
}
Also used : FileManager(com.opensymphony.xwork2.FileManager)

Example 4 with FileManager

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

the class DummyFileManager method testCreateDefaultFileManager.

public void testCreateDefaultFileManager() throws Exception {
    // given
    fileManager = null;
    DefaultFileManagerFactory factory = new DefaultFileManagerFactory();
    factory.setFileManager(new DefaultFileManager());
    factory.setContainer(new DummyContainer());
    // when
    FileManager fm = factory.getFileManager();
    // then
    assertTrue(fm instanceof DefaultFileManager);
}
Also used : FileManager(com.opensymphony.xwork2.FileManager)

Example 5 with FileManager

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

the class BaseOsgiHost method getVersion.

/**
 * @param url URL for package
 * @return  the version used to export the packages. it tries to get it from MANIFEST.MF, or the file name
 */
protected String getVersion(URL url) {
    if ("jar".equals(url.getProtocol())) {
        JarFile jarFile = null;
        try {
            ActionContext actionContext = null;
            FileManagerFactory fileManagerFactory = null;
            FileManager fileManager = null;
            actionContext = ServletActionContext.getContext();
            if (actionContext == null) {
                LOG.warn("ActionContext is null.  Cannot load FileManagerFactory from it");
            }
            if (actionContext != null) {
                fileManagerFactory = actionContext.getInstance(FileManagerFactory.class);
            }
            if (fileManagerFactory == null) {
                LOG.warn("FileManagerFactory is null in ActionContext.  Cannot load FileManager from it");
            } else {
                fileManager = fileManagerFactory.getFileManager();
            }
            if (fileManager == null) {
                if (fileManagerFactory != null) {
                    LOG.debug("FileManager is null in FileManagerFactory.  Using a DefaultFileManager");
                } else {
                    LOG.debug("Using a DefaultFileManager");
                }
                fileManager = new DefaultFileManager();
            }
            jarFile = new JarFile(new File(fileManager.normalizeToFileProtocol(url).toURI()));
            Manifest manifest = jarFile.getManifest();
            String jarFileName = jarFile.getName();
            if (jarFileName != null) {
                // Strip extraneous file path elements to limit the string to the JAR name itself, if possible.
                int lastExclamationIndex = jarFileName.lastIndexOf("!");
                if (lastExclamationIndex != -1) {
                    jarFileName = jarFileName.substring(0, lastExclamationIndex);
                }
                if (jarFileName.toLowerCase().endsWith(".jar")) {
                    int lastPathSeparatorIndex = jarFileName.lastIndexOf(File.separator);
                    if (lastPathSeparatorIndex != -1) {
                        jarFileName = jarFileName.substring(lastPathSeparatorIndex + 1);
                    }
                }
            }
            if (manifest != null) {
                String version = manifest.getMainAttributes().getValue("Bundle-Version");
                if (StringUtils.isNotBlank(version)) {
                    LOG.debug("Attempting to get bundle version for [{}] via its JAR manifest", url.toExternalForm());
                    return getVersionFromString(version);
                } else {
                    // try to get the version from the file name
                    LOG.debug("Attempting to get bundle version for [{}] via its filename [{}]", url.toExternalForm(), jarFileName);
                    return getVersionFromString(jarFileName);
                }
            } else {
                // try to get the version from the file name
                LOG.debug("Attempting to get bundle version for [{}] via its filename [{}]", url.toExternalForm(), jarFileName);
                return getVersionFromString(jarFileName);
            }
        } catch (Exception e) {
            LOG.error("Unable to extract version from [{}], defaulting to '1.0.0'", url.toExternalForm());
        } finally {
            if (jarFile != null) {
                try {
                    jarFile.close();
                } catch (Exception ex) {
                }
            }
        }
    }
    return "1.0.0";
}
Also used : FileManagerFactory(com.opensymphony.xwork2.FileManagerFactory) DefaultFileManager(com.opensymphony.xwork2.util.fs.DefaultFileManager) JarFile(java.util.jar.JarFile) ActionContext(com.opensymphony.xwork2.ActionContext) ServletActionContext(org.apache.struts2.ServletActionContext) Manifest(java.util.jar.Manifest) JarFile(java.util.jar.JarFile) File(java.io.File) DefaultFileManager(com.opensymphony.xwork2.util.fs.DefaultFileManager) FileManager(com.opensymphony.xwork2.FileManager) IOException(java.io.IOException) StrutsException(org.apache.struts2.StrutsException)

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