use of org.springframework.core.io.support.ResourcePatternResolver in project OpenClinica by OpenClinica.
the class CoreResources method copyBaseToDest.
private void copyBaseToDest(ResourceLoader resourceLoader) {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(resourceLoader);
Resource[] resources;
try {
/*
* Use classpath* to search for resources that match this pattern in ALL of the jars in the application
* class path. See:
* http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/resources
* .html#resources-classpath-wildcards
*/
resources = resolver.getResources("classpath*:properties/xslt/*.xsl");
} catch (IOException ioe) {
logger.debug(ioe.getMessage(), ioe);
throw new OpenClinicaSystemException("Unable to read source files", ioe);
}
File dest = new File(getField("filePath") + "xslt");
if (!dest.exists()) {
if (!dest.mkdirs()) {
throw new OpenClinicaSystemException("Copying files, Could not create direcotry: " + dest.getAbsolutePath() + ".");
}
}
for (Resource r : resources) {
File f = new File(dest, r.getFilename());
try {
FileOutputStream out = new FileOutputStream(f);
IOUtils.copy(r.getInputStream(), out);
out.close();
} catch (IOException ioe) {
logger.debug(ioe.getMessage(), ioe);
throw new OpenClinicaSystemException("Unable to copy file: " + r.getFilename() + " to " + f.getAbsolutePath(), ioe);
}
}
}
use of org.springframework.core.io.support.ResourcePatternResolver in project commons by terran4j.
the class Classes method scanClasses.
/**
* 扫描指定的包,将有指定注解的类找出来。
*
* @param basePackageClass 待扫描的包中的直接任意类,也就是根据这个类找到它的包。
* @param recursively 是否递归的方式在子包中找, true表示要在所有子包中找,false表示只在本包中找。
* @param filter 指定的过滤条件,只有符合过滤条件的类才会被找出来;如果为 null,则不过滤。
* @param classLoader 类加载器。
* @return 符合条件的类集合。
* @throws IOException IO异常
* @throws ClassNotFoundException 类找不到。
*/
public static final Set<Class<?>> scanClasses(Class<?> basePackageClass, boolean recursively, TypeFilter filter, ClassLoader classLoader) throws IOException, ClassNotFoundException {
if (basePackageClass == null) {
throw new NullPointerException("basePackageClass is null.");
}
final Set<Class<?>> classes = new HashSet<Class<?>>();
String packageName = basePackageClass.getPackage().getName();
final String resourcePattern = recursively ? "/**/*.class" : "/*.class";
String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(packageName) + resourcePattern;
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(resourcePatternResolver);
final String KEY_maxCount = "terran4j.util.maxScanClassCount";
String maxCountText = System.getProperty(KEY_maxCount, "1024").trim();
int maxCount = 1024;
try {
maxCount = Integer.parseInt(maxCountText);
} catch (NumberFormatException e) {
throw new RuntimeException("Can't parse as int of java property[" + KEY_maxCount + "]: " + maxCountText);
}
Resource[] resources = resourcePatternResolver.getResources(pattern);
for (Resource resource : resources) {
if (resource.isReadable()) {
// 找到这个类。
MetadataReader reader = readerFactory.getMetadataReader(resource);
String className = reader.getClassMetadata().getClassName();
boolean matched = false;
if (filter != null) {
matched = filter.match(reader, readerFactory);
} else {
matched = true;
}
if (matched) {
Class<?> clazz = classLoader.loadClass(className);
classes.add(clazz);
// 调用方指定的范围太大的话,
if (classes.size() > maxCount) {
throw new RuntimeException("too many classes be scaned, can't more than: " + maxCount);
}
}
}
}
// 输出日志
if (log.isInfoEnabled()) {
if (filter == null) {
log.info("Found classes in package[{}]: \n{}", packageName, Joiner.on("\n").join(classes.iterator()));
} else {
log.info("Found classes with filter[{}] in package[{}]: \n{}", filter, packageName, Joiner.on("\n").join(classes.iterator()));
}
}
return classes;
}
use of org.springframework.core.io.support.ResourcePatternResolver in project commons by terran4j.
the class Classes method scanResources.
/**
* 搜索匹配路径的搜索。
*
* @param pathPattern 路径匹配模式。
* @return 找到的资源文件。
* @throws IOException IO异常。
*/
public static final Resource[] scanResources(String pathPattern) throws IOException {
String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + pathPattern;
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources(pattern);
return resources;
}
use of org.springframework.core.io.support.ResourcePatternResolver in project cosmic by MissionCriticalCloud.
the class ClasspathModuleDefinitionLocator method locateModules.
@Override
public Collection<ModuleDefinition> locateModules(final String context) throws IOException {
final ResourcePatternResolver resolver = getResolver();
final Map<String, ModuleDefinition> allModules = discoverModules(context, resolver);
return allModules.values();
}
use of org.springframework.core.io.support.ResourcePatternResolver in project syndesis by syndesisio.
the class Application method generateIntegrationProject.
private void generateIntegrationProject(File project) throws IOException {
final ReadApiClientData reader = new ReadApiClientData();
final ArrayList<Step> steps = new ArrayList<>();
String deploymentText;
try (InputStream is = resourceLoader.getResource("io/syndesis/server/dao/deployment.json").getInputStream()) {
deploymentText = reader.from(is);
}
final List<ModelData<?>> modelList = reader.readDataFromString(deploymentText);
for (final ModelData<?> model : modelList) {
if (model.getKind() == Kind.Connector) {
final Connector connector = (Connector) model.getData();
for (final Action action : connector.getActions()) {
steps.add(new Step.Builder().stepKind(StepKind.endpoint).connection(new Connection.Builder().connector(connector).connectorId(connector.getId().get()).build()).action(action).build());
}
}
if (model.getKind() == Kind.ConnectorTemplate) {
final ConnectorTemplate template = (ConnectorTemplate) model.getData();
steps.add(new Step.Builder().stepKind(StepKind.endpoint).connection(new Connection.Builder().connectorId("connector-" + template.getId()).build()).action(new ConnectorAction.Builder().descriptor(new ConnectorDescriptor.Builder().camelConnectorGAV(template.getCamelConnectorGAV()).camelConnectorPrefix(template.getCamelConnectorPrefix()).build()).build()).build());
}
}
try {
final ResourcePatternResolver resolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
final Resource[] resources = resolver.getResources("classpath:/META-INF/syndesis/connector/*.json");
if (resources != null) {
for (Resource resource : resources) {
Connector connector = Json.reader().forType(Connector.class).readValue(resource.getInputStream());
if (connector != null) {
for (final Action action : connector.getActions()) {
steps.add(new Step.Builder().stepKind(StepKind.endpoint).connection(new Connection.Builder().connector(connector).connectorId(connector.getId().get()).build()).action(action).build());
}
}
}
}
} catch (FileNotFoundException ignored) {
// ignore
}
Integration integration = new Integration.Builder().id("Integration").name("Integration").description("This integration is used to prime the .m2 repo").steps(steps).build();
generate(integration, project);
}
Aggregations