List of usage examples for java.io FileFilter FileFilter
FileFilter
From source file:org.dcm4che3.tool.unvscp.UnvSCP.java
private static void configureManualUploading(UnvSCP main, CommandLine cl) throws ParseException { if (cl.hasOption("upl-dir")) { if (!cl.hasOption("upl-bad-files-dir")) { throw new MissingOptionException(rb.getString("missing-upl-bad-files")); }/* w w w. j a va2 s.com*/ File uplDir = new File(cl.getOptionValue("upl-dir")); if (!uplDir.exists()) { throw new ParseException("upl-dir '" + uplDir.getPath() + "' doesn't exist. Create the directory or change option --upl-dir"); } if (!uplDir.isDirectory()) { throw new ParseException( "upl-dir '" + uplDir.getPath() + "' is not a directory. Change option --upl-dir"); } if (!uplDir.canRead()) { throw new ParseException("Can not read from upl-dir '" + uplDir.getPath() + "'. Check access permissions or change option --upl-dir"); } if (!uplDir.canWrite()) { throw new ParseException("Can not write to upl-dir '" + uplDir.getPath() + "'. Check access permissions or change option --upl-dir"); } File uplBadFilesDir = new File(cl.getOptionValue("upl-bad-files-dir")); if (!uplBadFilesDir.exists()) { throw new ParseException("upl-bad-files-dir '" + uplBadFilesDir.getPath() + "' doesn't exist. Create the directory or change option --upl-bad-files-dir"); } if (!uplBadFilesDir.isDirectory()) { throw new ParseException("upl-bad-files-dir '" + uplBadFilesDir.getPath() + "' is not a directory. Change option --upl-bad-files-dir"); } if (!uplBadFilesDir.canRead()) { throw new ParseException("Can not read from upl-bad-files-dir '" + uplBadFilesDir.getPath() + "'. Check access permissions or change option --upl-bad-files-dir"); } if (!uplBadFilesDir.canWrite()) { throw new ParseException("Can not write to upl-bad-files-dir '" + uplBadFilesDir.getPath() + "'. Check access permissions or change option --upl-bad-files-dir"); } main.uplDir = uplDir; main.uplBadFilesDir = uplBadFilesDir; try { main.uplSleepTime = Integer.parseInt(cl.getOptionValue("upl-sleep-interval", "5")); } catch (NumberFormatException nfe) { throw new ParseException("\"" + cl.getOptionValue("upl-sleep-interval") + "\" is not a valid integer value for --upl-sleep-interval"); } if (cl.hasOption("source-override")) { UnvWebClient.setSourceOverride(cl.getOptionValue("source-override")); } if (cl.hasOption("store-upl-failures")) { final Integer days; try { days = Integer.parseInt(cl.getOptionValue("store-upl-failures")); } catch (NumberFormatException nfe) { throw new ParseException("--store-upl-failures can not accept \"" + cl.getOptionValue("store-upl-failures") + "\" (only integer values are allowed)"); } FileFilter filter = new FileFilter() { private Date theDate = new Date(); { Calendar c = Calendar.getInstance(); c.setTime(theDate); c.add(Calendar.DATE, -1 * Math.abs(days)); theDate = c.getTime(); } @Override public boolean accept(File file) { return file.isDirectory() || file.lastModified() < this.theDate.getTime(); } }; doRecursiveCleanUp(uplBadFilesDir, true, filter); } } }
From source file:com.actuate.development.tool.task.InstallBRDPro.java
private void checkDTPFile(File file, final String pattern, final List<File> files) { File[] children = file.listFiles(new FileFilter() { public boolean accept(File file) { if (file.isFile()) { String fileName = file.getName(); if (fileName.matches(pattern)) { files.add(file);/*from www .j a va 2 s .c o m*/ return false; } } else { return true; } return false; } }); if (children != null && files.isEmpty()) { FileSorter.sortFiles(children); for (int i = children.length - 1; i >= 0; i--) { if (!files.isEmpty()) break; checkDTPFile(children[i], pattern, files); } } }
From source file:de.tudarmstadt.ukp.clarin.webanno.api.dao.RepositoryServiceDbData.java
/** * Creates an annotation document (either user's annotation document or CURATION_USER's * annotation document)/* w w w. j av a2 s.c o m*/ * * @param aDocument * the {@link SourceDocument} * @param aJcas * The annotated CAS object * @param aUserName * the user who annotates the document if it is user's annotation document OR the * CURATION_USER * @throws IOException */ private void writeCas(SourceDocument aDocument, JCas aJcas, String aUserName) throws IOException { log.debug("Updating annotation document [" + aDocument.getName() + "] " + "with ID [" + aDocument.getId() + "] in project ID [" + aDocument.getProject().getId() + "]"); //DebugUtils.smallStack(); synchronized (lock) { File annotationFolder = getAnnotationFolder(aDocument); FileUtils.forceMkdir(annotationFolder); final String username = aUserName; File currentVersion = new File(annotationFolder, username + ".ser"); File oldVersion = new File(annotationFolder, username + ".ser.old"); // Save current version try { // Make a backup of the current version of the file before overwriting if (currentVersion.exists()) { renameFile(currentVersion, oldVersion); } // Now write the new version to "<username>.ser" or CURATION_USER.ser DocumentMetaData md; try { md = DocumentMetaData.get(aJcas); } catch (IllegalArgumentException e) { md = DocumentMetaData.create(aJcas); } md.setDocumentId(aUserName); File targetPath = getAnnotationFolder(aDocument); writeSerializedCas(aJcas, new File(targetPath, aUserName + ".ser")); createLog(aDocument.getProject()) .info("Updated annotation document [" + aDocument.getName() + "] " + "with ID [" + aDocument.getId() + "] in project ID [" + aDocument.getProject().getId() + "]"); createLog(aDocument.getProject()).removeAllAppenders(); // If the saving was successful, we delete the old version if (oldVersion.exists()) { FileUtils.forceDelete(oldVersion); } } catch (IOException e) { // If we could not save the new version, restore the old one. FileUtils.forceDelete(currentVersion); // If this is the first version, there is no old version, so do not restore anything if (oldVersion.exists()) { renameFile(oldVersion, currentVersion); } // Now abort anyway throw e; } // Manage history if (backupInterval > 0) { // Determine the reference point in time based on the current version long now = currentVersion.lastModified(); // Get all history files for the current user File[] history = annotationFolder.listFiles(new FileFilter() { private final Matcher matcher = Pattern .compile(Pattern.quote(username) + "\\.ser\\.[0-9]+\\.bak").matcher(""); @Override public boolean accept(File aFile) { // Check if the filename matches the pattern given above. return matcher.reset(aFile.getName()).matches(); } }); // Sort the files (oldest one first) Arrays.sort(history, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR); // Check if we need to make a new history file boolean historyFileCreated = false; File historyFile = new File(annotationFolder, username + ".ser." + now + ".bak"); if (history.length == 0) { // If there is no history yet but we should keep history, then we create a // history file in any case. FileUtils.copyFile(currentVersion, historyFile); historyFileCreated = true; } else { // Check if the newest history file is significantly older than the current one File latestHistory = history[history.length - 1]; if (latestHistory.lastModified() + backupInterval < now) { FileUtils.copyFile(currentVersion, historyFile); historyFileCreated = true; } } // Prune history based on number of backup if (historyFileCreated) { // The new version is not in the history, so we keep that in any case. That // means we need to keep one less. int toKeep = Math.max(backupKeepNumber - 1, 0); if ((backupKeepNumber > 0) && (toKeep < history.length)) { // Copy the oldest files to a new array File[] toRemove = new File[history.length - toKeep]; System.arraycopy(history, 0, toRemove, 0, toRemove.length); // Restrict the history to what is left File[] newHistory = new File[toKeep]; if (toKeep > 0) { System.arraycopy(history, toRemove.length, newHistory, 0, newHistory.length); } history = newHistory; // Remove these old files for (File file : toRemove) { FileUtils.forceDelete(file); createLog(aDocument.getProject()).info("Removed surplus history file [" + file.getName() + "] " + "for document with ID [" + aDocument.getId() + "] in project ID [" + aDocument.getProject().getId() + "]"); createLog(aDocument.getProject()).removeAllAppenders(); } } // Prune history based on time if (backupKeepTime > 0) { for (File file : history) { if ((file.lastModified() + backupKeepTime) < now) { FileUtils.forceDelete(file); createLog(aDocument.getProject()).info("Removed outdated history file [" + file.getName() + "] " + " for document with ID [" + aDocument.getId() + "] in project ID [" + aDocument.getProject().getId() + "]"); createLog(aDocument.getProject()).removeAllAppenders(); } } } } } } }
From source file:org.dcm4che3.tool.unvscp.UnvSCP.java
private static void configureLog(UnvSCP main, CommandLine cl) throws ParseException, FileNotFoundException, IOException { File logDir = null;/*from w ww .ja v a2 s . c om*/ if (cl.hasOption("log-dir")) { logDir = new File(cl.getOptionValue("log-dir")); if (!logDir.exists()) { throw new ParseException("log-dir '" + logDir.getPath() + "' doesn't exist. Create the directory or change option --log-dir"); } if (!logDir.isDirectory()) { throw new ParseException( "log-dir '" + logDir.getPath() + "' is not a directory. Change option --log-dir"); } if (!logDir.canRead()) { throw new ParseException("Can not read from log-dir '" + logDir.getPath() + "'. Check access permissions or change option --log-dir"); } if (!logDir.canWrite()) { throw new ParseException("Can not write to log-dir '" + logDir.getPath() + "'. Check access permissions or change option --log-dir"); } Properties log4jProps = new Properties(); InputStream configStream = main.getClass().getResourceAsStream("/log4j.properties"); log4jProps.load(configStream); configStream.close(); log4jProps.setProperty("log4j.rootLogger", "INFO, stdout, file"); //log4jProps.setProperty("log4j.appender.file", "org.apache.log4j.DailyRollingFileAppender"); log4jProps.setProperty("log4j.appender.file", "org.apache.log4j.rolling.RollingFileAppender"); log4jProps.setProperty("log4j.appender.file.File", new File(logDir, "latest.log").getPath()); log4jProps.setProperty("log4j.appender.file.ImmediateFlush", "true"); log4jProps.setProperty("log4j.appender.file.Append", "true"); //log4jProps.setProperty("log4j.appender.file.DatePattern", "'-'yyyy-MM-dd-mm-ss'.log'"); //log4jProps.setProperty("log4j.appender.file.MaxBackupIndex", "7"); //log4jProps.setProperty("log4j.appender.file.triggeringPolicy", "org.apache.log4j.rolling.TimeBasedRollingPolicy"); //log4jProps.setProperty("log4j.appender.file.triggeringPolicy.FileNamePattern", new File(logDir, "%d{yyyy-MM-dd-mm-ss}.log").getPath()); log4jProps.setProperty("log4j.appender.file.RollingPolicy", "org.apache.log4j.rolling.TimeBasedRollingPolicy"); log4jProps.setProperty("log4j.appender.file.RollingPolicy.FileNamePattern", new File(logDir, "%d{yyyy-MM-dd}.log").getPath()); log4jProps.setProperty("log4j.appender.file.layout", "org.apache.log4j.PatternLayout"); log4jProps.setProperty("log4j.appender.file.layout.ConversionPattern", "%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"); LogManager.resetConfiguration(); PropertyConfigurator.configure(log4jProps); } else { if (cl.hasOption("store-log")) { throw new MissingOptionException(rb.getString("missing-log-dir")); } } if (cl.hasOption("store-log")) { final Integer days; try { days = Integer.parseInt(cl.getOptionValue("store-log")); } catch (NumberFormatException nfe) { throw new ParseException("--store-log can not accept \"" + cl.getOptionValue("store-log") + "\" (only integer values are allowed)"); } // Temporary Solution for Log clean up FileFilter filter = new FileFilter() { private Date theDate = new Date(); { Calendar c = Calendar.getInstance(); c.setTime(theDate); c.add(Calendar.DATE, -1 * Math.abs(days)); theDate = c.getTime(); } @Override public boolean accept(File file) { return file.isFile() && file.lastModified() < this.theDate.getTime() && file.getName().matches("^\\d\\d\\d\\d-\\d\\d-\\d\\d\\.(?i)(log)$"); } }; File[] logFilesToRemove = logDir.listFiles(filter); if (logFilesToRemove.length > 0) { LOG.info("Cleaning up the log dir (There are {} old log files to remove)", logFilesToRemove.length); } for (File f : logFilesToRemove) { if (!f.delete()) { LOG.warn( "Can not delete log file {}. The file may be in use or the system settings do not allow this operation", f.getName()); } } } }
From source file:org.dspace.statistics.SolrLoggerServiceImpl.java
protected synchronized void initSolrYearCores() { if (statisticYearCoresInit || !(solr instanceof HttpSolrServer)) { return;/*from ww w. j ava 2 s . co m*/ } try { //Attempt to retrieve all the statistic year cores File solrDir = new File( configurationService.getProperty("dspace.dir") + File.separator + "solr" + File.separator); File[] solrCoreFiles = solrDir.listFiles(new FileFilter() { @Override public boolean accept(File file) { //Core name example: statistics-2008 return file.getName().matches("statistics-\\d\\d\\d\\d"); } }); //Base url should like : http://localhost:{port.number}/solr String baseSolrUrl = ((HttpSolrServer) solr).getBaseURL().replace("statistics", ""); for (File solrCoreFile : solrCoreFiles) { log.info("Loading core with name: " + solrCoreFile.getName()); createCore((HttpSolrServer) solr, solrCoreFile.getName()); //Add it to our cores list so we can query it ! statisticYearCores .add(baseSolrUrl.replace("http://", "").replace("https://", "") + solrCoreFile.getName()); } //Also add the core containing the current year ! statisticYearCores .add(((HttpSolrServer) solr).getBaseURL().replace("http://", "").replace("https://", "")); } catch (Exception e) { log.error(e.getMessage(), e); } statisticYearCoresInit = true; }
From source file:com.adito.extensions.store.ExtensionStore.java
private void addAdditionalClasspath() { // Add any additional class path elements StringTokenizer t = new StringTokenizer(SystemProperties.get("adito.additionalClasspath", ""), ","); while (t.hasMoreTokens()) { try {//from www. j a va 2 s. c om String sf = t.nextToken(); File[] f = null; if (sf.endsWith("/*.jar")) { f = new File(sf.substring(0, sf.length() - 6)).listFiles(new FileFilter() { public boolean accept(File pathname) { return pathname.getName().toLowerCase().endsWith(".jar"); } }); } else { f = new File[1]; f[0] = new File(sf); } for (int j = 0; f != null && j < f.length; j++) { if (f[j].exists() && (f[j].isDirectory() || f[j].getName().toLowerCase().endsWith(".jar"))) { URL u = f[j].toURL(); ContextHolder.getContext().addContextLoaderURL(u); } } } catch (MalformedURLException murle) { log.warn("Invalid element in additional classpaths"); } } }
From source file:com.sslexplorer.extensions.store.ExtensionStore.java
private void addAdditionalClasspath() { // Add any additional class path elements StringTokenizer t = new StringTokenizer(SystemProperties.get("sslexplorer.additionalClasspath", ""), ","); while (t.hasMoreTokens()) { try {//from w w w .j a v a 2 s. c om String sf = t.nextToken(); File[] f = null; if (sf.endsWith("/*.jar")) { f = new File(sf.substring(0, sf.length() - 6)).listFiles(new FileFilter() { public boolean accept(File pathname) { return pathname.getName().toLowerCase().endsWith(".jar"); } }); } else { f = new File[1]; f[0] = new File(sf); } for (int j = 0; f != null && j < f.length; j++) { if (f[j].exists() && (f[j].isDirectory() || f[j].getName().toLowerCase().endsWith(".jar"))) { URL u = f[j].toURL(); ContextHolder.getContext().addContextLoaderURL(u); } } } catch (MalformedURLException murle) { log.warn("Invalid element in additional classpaths"); } } }
From source file:com.adito.extensions.store.ExtensionStore.java
private void loadDevExtensions() { List<String> devExtensions = new ArrayList<String>(); String extensionList = SystemProperties.get("adito.devExtensions", ""); StringTokenizer t = new StringTokenizer(extensionList, ","); while (t.hasMoreTokens()) { String ext = t.nextToken(); if (ext.equalsIgnoreCase("all")) { File f = new File(SystemProperties.get("user.dir")).getParentFile(); File[] dirs = f.listFiles(new FileFilter() { public boolean accept(File pathname) { File f = new File(pathname, "extensions"); return f.exists() && f.isDirectory(); }/*w w w . j av a 2 s . co m*/ }); for (int i = 0; dirs != null && i < dirs.length; i++) { devExtensions.add(dirs[i].getName()); } } else if (ext.equalsIgnoreCase("enterprise")) { File f = new File(SystemProperties.get("user.dir")).getParentFile(); File[] dirs = f.listFiles(new FileFilter() { public boolean accept(File pathname) { File f = new File(pathname, "extensions"); return f.exists() && f.isDirectory() && pathname.getName().indexOf("adito-enterprise-") != -1; } }); for (int i = 0; dirs != null && i < dirs.length; i++) { devExtensions.add(dirs[i].getName()); } } else if (ext.equalsIgnoreCase("community")) { File f = new File(SystemProperties.get("user.dir")).getParentFile(); File[] dirs = f.listFiles(new FileFilter() { public boolean accept(File pathname) { File f = new File(pathname, "extensions"); return f.exists() && f.isDirectory() && pathname.getName().indexOf("adito-community-") != -1; } }); for (int i = 0; dirs != null && i < dirs.length; i++) { devExtensions.add(dirs[i].getName()); } } else { if (ext.startsWith("!")) { devExtensions.remove(ext.substring(1)); } else { devExtensions.add(ext); } } } for (Iterator it = devExtensions.iterator(); it.hasNext();) { String ext = (String) it.next(); File d = new File(new File(SystemProperties.get("user.dir")).getParentFile(), ext); File extensionDir = new File(new File(d, "extensions"), d.getName()); File extensionDescriptor = new File(extensionDir, "extension.xml"); if (extensionDescriptor.exists()) { try { loadBundle(extensionDescriptor, true); } catch (Exception e) { log.error("Failed to load dev extension " + extensionDescriptor.getAbsolutePath(), e); } } } }
From source file:com.sslexplorer.extensions.store.ExtensionStore.java
private void loadDevExtensions() { List<String> devExtensions = new ArrayList<String>(); String extensionList = SystemProperties.get("sslexplorer.devExtensions", ""); StringTokenizer t = new StringTokenizer(extensionList, ","); while (t.hasMoreTokens()) { String ext = t.nextToken(); if (ext.equalsIgnoreCase("all")) { File f = new File(SystemProperties.get("user.dir")).getParentFile(); File[] dirs = f.listFiles(new FileFilter() { public boolean accept(File pathname) { File f = new File(pathname, "extensions"); return f.exists() && f.isDirectory(); }/*from www .j a v a 2s. co m*/ }); for (int i = 0; dirs != null && i < dirs.length; i++) { devExtensions.add(dirs[i].getName()); } } else if (ext.equalsIgnoreCase("enterprise")) { File f = new File(SystemProperties.get("user.dir")).getParentFile(); File[] dirs = f.listFiles(new FileFilter() { public boolean accept(File pathname) { File f = new File(pathname, "extensions"); return f.exists() && f.isDirectory() && pathname.getName().indexOf("sslexplorer-enterprise-") != -1; } }); for (int i = 0; dirs != null && i < dirs.length; i++) { devExtensions.add(dirs[i].getName()); } } else if (ext.equalsIgnoreCase("community")) { File f = new File(SystemProperties.get("user.dir")).getParentFile(); File[] dirs = f.listFiles(new FileFilter() { public boolean accept(File pathname) { File f = new File(pathname, "extensions"); return f.exists() && f.isDirectory() && pathname.getName().indexOf("sslexplorer-community-") != -1; } }); for (int i = 0; dirs != null && i < dirs.length; i++) { devExtensions.add(dirs[i].getName()); } } else { if (ext.startsWith("!")) { devExtensions.remove(ext.substring(1)); } else { devExtensions.add(ext); } } } for (Iterator it = devExtensions.iterator(); it.hasNext();) { String ext = (String) it.next(); File d = new File(new File(SystemProperties.get("user.dir")).getParentFile(), ext); File extensionDir = new File(new File(d, "extensions"), d.getName()); File extensionDescriptor = new File(extensionDir, "extension.xml"); if (extensionDescriptor.exists()) { try { loadBundle(extensionDescriptor, true); } catch (Exception e) { log.error("Failed to load dev extension " + extensionDescriptor.getAbsolutePath(), e); } } } }
From source file:hudson.model.Hudson.java
private synchronized void load() throws IOException { long startTime = System.currentTimeMillis(); XmlFile cfg = getConfigFile();//from w w w . j a va2 s .c o m if (cfg.exists()) { // reset some data that may not exit in the disk file // so that we can take a proper compensation action later. primaryView = null; views.clear(); cfg.unmarshal(this); } clouds.setOwner(this); File projectsDir = new File(root, "jobs"); if (!projectsDir.isDirectory() && !projectsDir.mkdirs()) { if (projectsDir.exists()) throw new IOException(projectsDir + " is not a directory"); throw new IOException("Unable to create " + projectsDir + "\nPermission issue? Please create this directory manually."); } File[] subdirs = projectsDir.listFiles(new FileFilter() { public boolean accept(File child) { return child.isDirectory() && Items.getConfigFile(child).exists(); } }); items.clear(); if (PARALLEL_LOAD) { // load jobs in parallel for better performance LOGGER.info("Loading in " + TWICE_CPU_NUM + " parallel threads"); List<Future<TopLevelItem>> loaders = new ArrayList<Future<TopLevelItem>>(); for (final File subdir : subdirs) { loaders.add(threadPoolForLoad.submit(new Callable<TopLevelItem>() { public TopLevelItem call() throws Exception { Thread t = Thread.currentThread(); String name = t.getName(); t.setName("Loading " + subdir); try { long start = System.currentTimeMillis(); TopLevelItem item = (TopLevelItem) Items.load(Hudson.this, subdir); if (LOG_STARTUP_PERFORMANCE) LOGGER.info("Loaded " + item.getName() + " in " + (System.currentTimeMillis() - start) + "ms by " + name); return item; } finally { t.setName(name); } } })); } for (Future<TopLevelItem> loader : loaders) { try { TopLevelItem item = loader.get(); items.put(item.getName(), item); } catch (ExecutionException e) { LOGGER.log(Level.WARNING, "Failed to load a project", e.getCause()); } catch (InterruptedException e) { e.printStackTrace(); // this is probably not the right thing to do } } } else { for (File subdir : subdirs) { try { long start = System.currentTimeMillis(); TopLevelItem item = (TopLevelItem) Items.load(this, subdir); if (LOG_STARTUP_PERFORMANCE) LOGGER.info( "Loaded " + item.getName() + " in " + (System.currentTimeMillis() - start) + "ms"); items.put(item.getName(), item); } catch (Error e) { LOGGER.log(Level.WARNING, "Failed to load " + subdir, e); } catch (RuntimeException e) { LOGGER.log(Level.WARNING, "Failed to load " + subdir, e); } catch (IOException e) { LOGGER.log(Level.WARNING, "Failed to load " + subdir, e); } } } rebuildDependencyGraph(); {// recompute label objects for (Node slave : slaves) slave.getAssignedLabels(); getAssignedLabels(); } // initialize views by inserting the default view if necessary // this is both for clean Hudson and for backward compatibility. if (views.size() == 0 || primaryView == null) { View v = new AllView(Messages.Hudson_ViewName()); v.owner = this; views.add(0, v); primaryView = v.getViewName(); } // read in old data that doesn't have the security field set if (authorizationStrategy == null) { if (useSecurity == null || !useSecurity) authorizationStrategy = AuthorizationStrategy.UNSECURED; else authorizationStrategy = new LegacyAuthorizationStrategy(); } if (securityRealm == null) { if (useSecurity == null || !useSecurity) setSecurityRealm(SecurityRealm.NO_AUTHENTICATION); else setSecurityRealm(new LegacySecurityRealm()); } else { // force the set to proxy setSecurityRealm(securityRealm); } if (useSecurity != null && !useSecurity) { // forced reset to the unsecure mode. // this works as an escape hatch for people who locked themselves out. authorizationStrategy = AuthorizationStrategy.UNSECURED; setSecurityRealm(SecurityRealm.NO_AUTHENTICATION); } // Initialize the filter with the crumb issuer setCrumbIssuer(crumbIssuer); // auto register root actions actions.addAll(getExtensionList(RootAction.class)); LOGGER.info(String.format("Took %s ms to load", System.currentTimeMillis() - startTime)); if (KILL_AFTER_LOAD) System.exit(0); }