Search in sources :

Example 91 with UrlResource

use of org.springframework.core.io.UrlResource in project ignite by apache.

the class GridFactorySelfTest method getSpringContext.

/**
 * Gets Spring application context by given path.
 *
 * @param path Spring application context configuration path.
 * @return Spring application context.
 * @throws IgniteCheckedException If given path or xml-configuration at this path is invalid.
 */
private GenericApplicationContext getSpringContext(String path) throws IgniteCheckedException {
    try {
        GenericApplicationContext ctx = new GenericApplicationContext();
        new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(new UrlResource(U.resolveIgniteUrl(path)));
        ctx.refresh();
        return ctx;
    } catch (BeansException e) {
        throw new IgniteCheckedException("Failed to instantiate Spring XML application context: " + e.getMessage(), e);
    }
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) UrlResource(org.springframework.core.io.UrlResource) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) BeansException(org.springframework.beans.BeansException)

Example 92 with UrlResource

use of org.springframework.core.io.UrlResource in project ignite by apache.

the class IgniteSpringHelperImpl method initContext.

/**
 * @param url XML file URL.
 * @return Context.
 * @throws IgniteCheckedException In case of error.
 */
private ApplicationContext initContext(URL url) throws IgniteCheckedException {
    GenericApplicationContext springCtx;
    try {
        springCtx = new GenericApplicationContext();
        new XmlBeanDefinitionReader(springCtx).loadBeanDefinitions(new UrlResource(url));
        springCtx.refresh();
    } catch (BeansException e) {
        if (X.hasCause(e, ClassNotFoundException.class))
            throw new IgniteCheckedException("Failed to instantiate Spring XML application context " + "(make sure all classes used in Spring configuration are present at CLASSPATH) " + "[springUrl=" + url + ']', e);
        else
            throw new IgniteCheckedException("Failed to instantiate Spring XML application context [springUrl=" + url + ", err=" + e.getMessage() + ']', e);
    }
    return springCtx;
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) UrlResource(org.springframework.core.io.UrlResource) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) BeansException(org.springframework.beans.BeansException)

Example 93 with UrlResource

use of org.springframework.core.io.UrlResource in project ignite by apache.

the class IgniteSpringHelperImpl method applicationContext.

/**
 * Creates Spring application context. Optionally excluded properties can be specified,
 * it means that if such a property is found in {@link org.apache.ignite.configuration.IgniteConfiguration}
 * then it is removed before the bean is instantiated.
 * For example, {@code streamerConfiguration} can be excluded from the configs that Visor uses.
 *
 * @param cfgUrl Resource where config file is located.
 * @param excludedProps Properties to be excluded.
 * @return Spring application context.
 * @throws IgniteCheckedException If configuration could not be read.
 */
public static ApplicationContext applicationContext(URL cfgUrl, final String... excludedProps) throws IgniteCheckedException {
    try {
        GenericApplicationContext springCtx = prepareSpringContext(excludedProps);
        new XmlBeanDefinitionReader(springCtx).loadBeanDefinitions(new UrlResource(cfgUrl));
        springCtx.refresh();
        return springCtx;
    } catch (BeansException e) {
        if (X.hasCause(e, ClassNotFoundException.class))
            throw new IgniteCheckedException("Failed to instantiate Spring XML application context " + "(make sure all classes used in Spring configuration are present at CLASSPATH) " + "[springUrl=" + cfgUrl + ']', e);
        else
            throw new IgniteCheckedException("Failed to instantiate Spring XML application context [springUrl=" + cfgUrl + ", err=" + e.getMessage() + ']', e);
    }
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) UrlResource(org.springframework.core.io.UrlResource) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) BeansException(org.springframework.beans.BeansException)

Example 94 with UrlResource

use of org.springframework.core.io.UrlResource in project cxf by apache.

the class BusApplicationContext method getConfigResources.

@Override
protected Resource[] getConfigResources() {
    List<Resource> resources = new ArrayList<>();
    if (includeDefaults) {
        try {
            PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(Thread.currentThread().getContextClassLoader());
            Collections.addAll(resources, resolver.getResources(DEFAULT_CXF_CFG_FILE));
            Resource[] exts = resolver.getResources(DEFAULT_CXF_EXT_CFG_FILE);
            for (Resource r : exts) {
                try (InputStream is = r.getInputStream();
                    BufferedReader rd = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
                    String line = rd.readLine();
                    while (line != null) {
                        if (!"".equals(line)) {
                            resources.add(resolver.getResource(line));
                        }
                        line = rd.readLine();
                    }
                }
            }
        } catch (IOException ex) {
        // ignore
        }
    }
    boolean usingDefault = false;
    if (null == cfgFiles) {
        String cfgFile = SystemPropertyAction.getPropertyOrNull(Configurer.USER_CFG_FILE_PROPERTY_NAME);
        if (cfgFile != null) {
            cfgFiles = new String[] { cfgFile };
        }
    }
    if (null == cfgFiles) {
        cfgFiles = new String[] { Configurer.DEFAULT_USER_CFG_FILE };
        usingDefault = true;
    }
    for (String cfgFile : cfgFiles) {
        final Resource cpr = findResource(cfgFile);
        boolean exists = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {

            public Boolean run() {
                return cpr != null && cpr.exists();
            }
        });
        if (exists) {
            resources.add(cpr);
            LogUtils.log(LOG, Level.INFO, "USER_CFG_FILE_IN_USE", cfgFile);
        } else {
            if (!usingDefault) {
                LogUtils.log(LOG, Level.WARNING, "USER_CFG_FILE_NOT_LOADED", cfgFile);
                String message = (new Message("USER_CFG_FILE_NOT_LOADED", LOG, cfgFile)).toString();
                throw new ApplicationContextException(message);
            }
        }
    }
    if (null != cfgFileURLs) {
        for (URL cfgFileURL : cfgFileURLs) {
            UrlResource ur = new UrlResource(cfgFileURL);
            if (ur.exists()) {
                resources.add(ur);
            } else {
                LogUtils.log(LOG, Level.WARNING, "USER_CFG_FILE_URL_NOT_FOUND_MSG", cfgFileURL);
            }
        }
    }
    String sysCfgFileUrl = SystemPropertyAction.getPropertyOrNull(Configurer.USER_CFG_FILE_PROPERTY_URL);
    if (null != sysCfgFileUrl) {
        try {
            UrlResource ur = new UrlResource(sysCfgFileUrl);
            if (ur.exists()) {
                resources.add(ur);
            } else {
                LogUtils.log(LOG, Level.WARNING, "USER_CFG_FILE_URL_NOT_FOUND_MSG", sysCfgFileUrl);
            }
        } catch (MalformedURLException e) {
            LogUtils.log(LOG, Level.WARNING, "USER_CFG_FILE_URL_ERROR_MSG", sysCfgFileUrl);
        }
    }
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Creating application context with resources: " + resources);
    }
    if (resources.isEmpty()) {
        return null;
    }
    Resource[] res = new Resource[resources.size()];
    res = resources.toArray(res);
    return res;
}
Also used : MalformedURLException(java.net.MalformedURLException) InputStreamReader(java.io.InputStreamReader) Message(org.apache.cxf.common.i18n.Message) InputStream(java.io.InputStream) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ApplicationContextException(org.springframework.context.ApplicationContextException) URL(java.net.URL) UrlResource(org.springframework.core.io.UrlResource) BufferedReader(java.io.BufferedReader) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver)

Example 95 with UrlResource

use of org.springframework.core.io.UrlResource in project zhcet-web by zhcet-amu.

the class FileSystemStorageService method loadAsResource.

@Override
public Resource loadAsResource(FileType fileType, String filename) {
    checkSecurity(filename);
    try {
        Path file = load(fileType, filename);
        Resource resource = new UrlResource(file.toUri());
        if (resource.exists() || resource.isReadable()) {
            return resource;
        } else {
            log.error("Failed to read file {}", filename);
            throw new StorageFileNotFoundException("Could not read file: " + filename);
        }
    } catch (MalformedURLException e) {
        log.error(String.format("Failed to read file %s", filename), e);
        throw new StorageFileNotFoundException("Could not read file: " + filename, e);
    }
}
Also used : Path(java.nio.file.Path) MalformedURLException(java.net.MalformedURLException) UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) Resource(org.springframework.core.io.Resource)

Aggregations

UrlResource (org.springframework.core.io.UrlResource)100 Resource (org.springframework.core.io.Resource)41 URL (java.net.URL)33 Test (org.junit.jupiter.api.Test)30 Test (org.junit.Test)24 ClassPathResource (org.springframework.core.io.ClassPathResource)19 IOException (java.io.IOException)14 ArrayList (java.util.ArrayList)14 FileSystemResource (org.springframework.core.io.FileSystemResource)13 MalformedURLException (java.net.MalformedURLException)11 Requisition (org.opennms.netmgt.provision.persist.requisition.Requisition)10 lombok.val (lombok.val)9 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)9 GenericBean (org.springframework.beans.testfixture.beans.GenericBean)9 File (java.io.File)8 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)8 InputStream (java.io.InputStream)6 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)6 Properties (java.util.Properties)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5