private static void deleteLockFiles(File dir, boolean recursive, String endpointPath, GenericFileFilter filter, GenericFileFilter antFilter, Pattern excludePattern, Pattern includePattern) { File[] files = dir.listFiles(); if (files == null || files.length == 0) { return; } for (File file : files) { if (file.getName().startsWith(".")) { // files starting with dot should be skipped continue; } // filter unwanted files and directories to avoid traveling everything if (filter != null || antFilter != null || excludePattern != null || includePattern != null) { if (!acceptFile(file, endpointPath, filter, antFilter, excludePattern, includePattern)) { continue; } } if (file.getName().endsWith(FileComponent.DEFAULT_LOCK_FILE_POSTFIX)) { LOG.warn("Deleting orphaned lock file: " + file); FileUtil.deleteFile(file); } else if (recursive && file.isDirectory()) { deleteLockFiles(file, true, endpointPath, filter, antFilter, excludePattern, includePattern); } } }
/** * Get a map with the route id (key) and the priority (value) for 'from' endpoint * * @return */ protected Map<String, String> getRouteToEndpointPriority() { Map<String, String> answer = new HashMap<String, String>(); List<RouteDefinition> routes = context.getRouteDefinitions(); Assert.assertNotNull(routes); for (RouteDefinition current : routes) { Assert.assertEquals(new Integer(1), new Integer(current.getInputs().size())); Assert.assertNotNull(current.getInputs().get(0)); Assert.assertNotNull(current.getId()); Assert.assertTrue(current.getId().trim().length() > 0); FromDefinition from = current.getInputs().get(0); Endpoint endpoint = getMandatoryEndpoint(from.getUri()); Assert.assertNotNull(endpoint); Assert.assertTrue(endpoint instanceof LoadBalancedFileEndpoint); LoadBalancedFileEndpoint lbEndpoint = (LoadBalancedFileEndpoint)endpoint; GenericFileFilter filter = lbEndpoint.getFilter(); Assert.assertNotNull(filter); Assert.assertTrue(filter instanceof PriorityFileFilter); PriorityFileFilter priorityFileFilter = (PriorityFileFilter)filter; Assert.assertNotNull(priorityFileFilter.getPriorityName()); answer.put(current.getId(), priorityFileFilter.getPriorityName()); } return answer; }
@SuppressWarnings("unchecked") private static boolean acceptFile(File file, String endpointPath, GenericFileFilter filter, GenericFileFilter antFilter, Pattern excludePattern, Pattern includePattern) { GenericFile gf = new GenericFile(); gf.setEndpointPath(endpointPath); gf.setFile(file); gf.setFileNameOnly(file.getName()); gf.setFileLength(file.length()); gf.setDirectory(file.isDirectory()); // must use FileUtil.isAbsolute to have consistent check for whether the file is // absolute or not. As windows do not consider \ paths as absolute where as all // other OS platforms will consider \ as absolute. The logic in Camel mandates // that we align this for all OS. That is why we must use FileUtil.isAbsolute // to return a consistent answer for all OS platforms. gf.setAbsolute(FileUtil.isAbsolute(file)); gf.setAbsoluteFilePath(file.getAbsolutePath()); gf.setLastModified(file.lastModified()); // compute the file path as relative to the starting directory File path; String endpointNormalized = FileUtil.normalizePath(endpointPath); if (file.getPath().startsWith(endpointNormalized + File.separator)) { // skip duplicate endpoint path path = new File(ObjectHelper.after(file.getPath(), endpointNormalized + File.separator)); } else { path = new File(file.getPath()); } if (path.getParent() != null) { gf.setRelativeFilePath(path.getParent() + File.separator + file.getName()); } else { gf.setRelativeFilePath(path.getName()); } // the file name should be the relative path gf.setFileName(gf.getRelativeFilePath()); if (filter != null) { if (!filter.accept(gf)) { return false; } } if (antFilter != null) { if (!antFilter.accept(gf)) { return false; } } // exclude take precedence over include if (excludePattern != null) { if (excludePattern.matcher(file.getName()).matches()) { return false; } } if (includePattern != null) { if (!includePattern.matcher(file.getName()).matches()) { return false; } } return true; }
@Provides @JndiBind("fileFilter") public GenericFileFilter<?> getfileFilter() { return new MyFileFilter<Object>(); }