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);
}
}
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;
}
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);
}
}
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;
}
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);
}
}
Aggregations