List of usage examples for org.springframework.core.io.support PathMatchingResourcePatternResolver setPathMatcher
public void setPathMatcher(PathMatcher pathMatcher)
From source file:com.logsniffer.model.file.WildcardLogsSource.java
@Override public List<Log> getLogs() throws IOException { final PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); resolver.setPathMatcher(new AntPathMatcher()); final Resource[] resources = resolver.getResources("file:" + getPattern()); final ArrayList<Log> logs = new ArrayList<Log>(resources.length); // TODO Decouple direct file log association for (int i = 0; i < resources.length; i++) { if (resources[i].exists()) { if (resources[i].getFile().isFile()) { logs.add(new FileLog(resources[i].getFile())); }//from w ww. java 2s.co m } else { logger.info("Ignore not existent file: {}", resources[i].getFile()); } } return logs; }
From source file:com.foilen.smalltools.upgrader.tasks.AbstractFlywayMigrateOffUpgradeTask.java
@Override public void execute() { DataSourceTransactionManager transactionManager = new DataSourceTransactionManager( jdbcTemplate.getDataSource()); // Check if the schema_version table exists List<String> tableNames = mysqlTablesFindAll(); List<String> executed; if (tableNames.contains("schema_version")) { executed = jdbcTemplate.queryForList("SELECT script FROM schema_version WHERE success = 1", String.class); jdbcTemplate.update("DELETE FROM schema_version WHERE success = 0"); } else {/* ww w .j ava 2s . com*/ executed = new ArrayList<>(); logger.info("Flyway table does not exists. Creating it"); updateFromResource("flyway-schema_version.sql", AbstractFlywayMigrateOffUpgradeTask.class); } PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); AntPathMatcher pathMatcher = new AntPathMatcher(); resolver.setPathMatcher(pathMatcher); Resource[] resources; try { resources = resolver.getResources("classpath:db/migration/*.sql"); } catch (IOException e) { throw new SmallToolsException("Problem getting the sql files", e); } int rank = executed.size() + 1; List<String> scriptNames = Arrays.asList(resources).stream() // .map(Resource::getFilename) // .sorted() // .collect(Collectors.toList()); for (String scriptName : scriptNames) { boolean needRetry = true; if (executed.contains(scriptName)) { logger.info("[{}] Already executed. Skip", scriptName); } else { logger.info("[{}] To execute", scriptName); for (int retryCount = 0; needRetry; ++retryCount) { needRetry = false; TransactionStatus transactionStatus = transactionManager .getTransaction(new DefaultTransactionDefinition()); try { // Do the update updateFromResource("/db/migration/" + scriptName); // Save in schema_version jdbcTemplate.update("INSERT INTO schema_version " // + "(version_rank, installed_rank, version, description, type, script, installed_by, execution_time, success) " // + "VALUES (?,?,?,'','SQL',?,'upgrader',1, 1)", // rank, rank, // scriptName.substring(0, Math.min(50, scriptName.length())), // scriptName // ); ++rank; transactionManager.commit(transactionStatus); } catch (Exception e) { logger.warn("[{}] Problem executing script. Will purge the connections and retry", scriptName); transactionManager.rollback(transactionStatus); needRetry = true; purgeConnections(); if (retryCount > 5) { throw new SmallToolsException("Problem executing script: " + scriptName, e); } } } } } if (deleteSchemaTable) { logger.info("Deleting the Flyway schema_version table"); jdbcTemplate.update("DROP TABLE schema_version"); } }
From source file:org.codehaus.groovy.grails.context.annotation.ClosureClassIgnoringComponentScanBeanDefinitionParser.java
@Override protected ClassPathBeanDefinitionScanner configureScanner(ParserContext parserContext, Element element) { final ClassPathBeanDefinitionScanner scanner = super.configureScanner(parserContext, element); final ResourceLoader originalResourceLoader = parserContext.getReaderContext().getResourceLoader(); if (LOG.isDebugEnabled()) { LOG.debug("Scanning only this classloader:" + originalResourceLoader.getClassLoader()); }//from w w w . j a v a 2 s . c o m ResourceLoader parentOnlyResourceLoader; try { parentOnlyResourceLoader = new ResourceLoader() { ClassLoader parentOnlyGetResourcesClassLoader = new ParentOnlyGetResourcesClassLoader( originalResourceLoader.getClassLoader()); public Resource getResource(String location) { return originalResourceLoader.getResource(location); } public ClassLoader getClassLoader() { return parentOnlyGetResourcesClassLoader; } }; } catch (Throwable t) { // restrictive classloading environment, use the original parentOnlyResourceLoader = originalResourceLoader; } final PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver( parentOnlyResourceLoader) { @Override protected Resource[] findAllClassPathResources(String location) throws IOException { Set<Resource> result = new LinkedHashSet<Resource>(16); @SuppressWarnings("unused") URL classesDir = null; final boolean warDeployed = Metadata.getCurrent().isWarDeployed(); if (!warDeployed) { BuildSettings buildSettings = BuildSettingsHolder.getSettings(); if (buildSettings != null && buildSettings.getClassesDir() != null) { classesDir = buildSettings.getClassesDir().toURI().toURL(); } } // only scan classes from project classes directory String path = location; if (path.startsWith("/")) { path = path.substring(1); } Enumeration<URL> resourceUrls = getClassLoader().getResources(path); while (resourceUrls.hasMoreElements()) { URL url = resourceUrls.nextElement(); if (LOG.isDebugEnabled()) { LOG.debug( "Scanning URL " + url.toExternalForm() + " while searching for '" + location + "'"); } /* if (!warDeployed && classesDir!= null && url.equals(classesDir)) { result.add(convertClassLoaderURL(url)); } else if (warDeployed) { result.add(convertClassLoaderURL(url)); } */ result.add(convertClassLoaderURL(url)); } return result.toArray(new Resource[result.size()]); } }; resourceResolver.setPathMatcher(new AntPathMatcher() { @Override public boolean match(String pattern, String path) { if (path.endsWith(".class")) { String filename = FilenameUtils.getBaseName(path); if (filename.indexOf("$") > -1) return false; } return super.match(pattern, path); } }); scanner.setResourceLoader(resourceResolver); return scanner; }
From source file:org.grails.spring.context.annotation.ClosureClassIgnoringComponentScanBeanDefinitionParser.java
@Override protected ClassPathBeanDefinitionScanner configureScanner(ParserContext parserContext, Element element) { final ClassPathBeanDefinitionScanner scanner = super.configureScanner(parserContext, element); final ResourceLoader originalResourceLoader = parserContext.getReaderContext().getResourceLoader(); if (LOG.isDebugEnabled()) { LOG.debug("Scanning only this classloader:" + originalResourceLoader.getClassLoader()); }/*from w w w . ja va 2 s . c om*/ ResourceLoader parentOnlyResourceLoader; try { parentOnlyResourceLoader = new ResourceLoader() { ClassLoader parentOnlyGetResourcesClassLoader = new ParentOnlyGetResourcesClassLoader( originalResourceLoader.getClassLoader()); public Resource getResource(String location) { return originalResourceLoader.getResource(location); } public ClassLoader getClassLoader() { return parentOnlyGetResourcesClassLoader; } }; } catch (Throwable t) { // restrictive classloading environment, use the original parentOnlyResourceLoader = originalResourceLoader; } final PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver( parentOnlyResourceLoader) { @Override protected Resource[] findAllClassPathResources(String location) throws IOException { Set<Resource> result = new LinkedHashSet<Resource>(16); if (BuildSettings.CLASSES_DIR != null) { @SuppressWarnings("unused") URL classesDir = BuildSettings.CLASSES_DIR.toURI().toURL(); // only scan classes from project classes directory String path = location; if (path.startsWith("/")) { path = path.substring(1); } Enumeration<URL> resourceUrls = getClassLoader().getResources(path); while (resourceUrls.hasMoreElements()) { URL url = resourceUrls.nextElement(); if (LOG.isDebugEnabled()) { LOG.debug("Scanning URL " + url.toExternalForm() + " while searching for '" + location + "'"); } /* if (!warDeployed && classesDir!= null && url.equals(classesDir)) { result.add(convertClassLoaderURL(url)); } else if (warDeployed) { result.add(convertClassLoaderURL(url)); } */ result.add(convertClassLoaderURL(url)); } } return result.toArray(new Resource[result.size()]); } }; resourceResolver.setPathMatcher(new AntPathMatcher() { @Override public boolean match(String pattern, String path) { if (path.endsWith(".class")) { String filename = GrailsStringUtils.getFileBasename(path); if (filename.contains("$")) return false; } return super.match(pattern, path); } }); scanner.setResourceLoader(resourceResolver); return scanner; }