Example usage for java.io FilenameFilter FilenameFilter

List of usage examples for java.io FilenameFilter FilenameFilter

Introduction

In this page you can find the example usage for java.io FilenameFilter FilenameFilter.

Prototype

FilenameFilter

Source Link

Usage

From source file:eu.crowdrec.contest.sender.RequestSender.java

/**
 * read logFile then sends line by line to server.
 * //from  w ww  .j  a  v a  2s .  com
 * @param inLogFileName
 *            path to log file. That can be a zip file or text file.
 * @param outLogFile
 *            path to outLog file. The outLog file should be analyzed by the evaluator.
 *            if the filename is null; no output will be generated
 * @param serverURL
 *            URL of the server
 */
public static void sender(final String inLogFileName, final String outLogFile, final String serverURL) {

    // handle the log file
    // check type of file

    // try to read the defined logFile
    BufferedReader br = null;
    BufferedWriter bw = null;
    try {
        // if outLogFile name is not null, create an output file
        if (outLogFile != null && outLogFile.length() > 0) {
            bw = new BufferedWriter(new FileWriter(new File(outLogFile), false));
        }

        // support a list of files in a directory
        File inLogFile = new File(inLogFileName);
        InputStream is;
        if (inLogFile.isFile()) {
            is = new FileInputStream(inLogFileName);
            // support gZip files
            if (inLogFile.getName().toLowerCase().endsWith(".gz")) {
                is = new GZIPInputStream(is);
            }
        } else {
            // if the input is a directory, consider all files based on a pattern
            File[] childs = inLogFile.listFiles(new FilenameFilter() {

                @Override
                public boolean accept(File dir, String name) {
                    final String fileName = name.toLowerCase();
                    return fileName.endsWith("data.idomaar.txt.gz") || fileName.endsWith("data.idomaar.txt")
                            || fileName.endsWith(".data.gz");
                }
            });
            if (childs == null || childs.length == 0) {
                throw new IOException("invalid inLogFileName or empty directory");
            }
            Arrays.sort(childs, new Comparator<File>() {

                @Override
                public int compare(File o1, File o2) {
                    return o1.getName().compareTo(o2.getName());
                }
            });
            Vector<InputStream> isChilds = new Vector<InputStream>();
            for (int i = 0; i < childs.length; i++) {
                InputStream tmpIS = new FileInputStream(childs[i]);
                // support gZip files
                if (childs[i].getName().toLowerCase().endsWith(".gz")) {
                    tmpIS = new GZIPInputStream(tmpIS);
                }
                isChilds.add(tmpIS);
            }
            is = new SequenceInputStream(isChilds.elements());
        }

        // read the log file line by line
        br = new BufferedReader(new InputStreamReader(is));
        try {
            for (String line = br.readLine(); line != null; line = br.readLine()) {

                // ignore invalid lines and header
                if (line.startsWith("null") || line.startsWith("#")) {
                    continue;
                }

                if (useThreadPool) {
                    RequestSenderThread t = new RequestSenderThread(line, serverURL, bw);
                    try {
                        // try to limit the speed of sending requests
                        if (Thread.activeCount() > 1000) {
                            if (logger.isDebugEnabled()) {
                                logger.debug("Thread.activeCount() = " + Thread.activeCount());
                            }
                            Thread.sleep(200);
                        }
                    } catch (Exception e) {
                        logger.info(e.toString());
                    }
                    t.start();
                } else {
                    // send parameters to http server and get response.
                    final long startTime = System.currentTimeMillis();
                    String result = HttpLib.HTTPCLIENT.equals(httpLib)
                            ? excutePostWithHttpClient(line, serverURL)
                            : excutePost(line, serverURL);

                    // analyze whether an answer is expected
                    boolean answerExpected = false;
                    if (line.contains("\"event_type\": \"recommendation_request\"")) {
                        answerExpected = true;
                    }
                    if (answerExpected) {
                        if (logger.isInfoEnabled()) {
                            logger.info("serverResponse: " + result);
                        }

                        // if the output file is not null, write the output in a synchronized way
                        if (bw != null) {
                            String[] data = LogFileUtils.extractEvaluationRelevantDataFromInputLine(line);
                            String requestId = data[0];
                            String userId = data[1];
                            String itemId = data[2];
                            String domainId = data[3];
                            String timeStamp = data[4];
                            long responseTime = System.currentTimeMillis() - startTime;
                            synchronized (bw) {
                                try {
                                    bw.write("prediction\t" + requestId + "\t" + timeStamp + "\t" + responseTime
                                            + "\t" + itemId + "\t" + userId + "\t" + domainId + "\t" + result);
                                    bw.newLine();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            logger.warn(e.toString(), e);
        }
    } catch (FileNotFoundException e) {
        logger.error("logFile not found e:" + e.toString());
    } catch (IOException e) {
        logger.error("reading the logFile failed e:" + e.toString());
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                logger.debug("close read-log file failed");
            }
        }
        if (bw != null) {
            try {
                // wait for ensuring that all request are finished
                // this simplifies the management of thread and worked fine for all test machines
                Thread.sleep(5000);
                bw.flush();
            } catch (Exception e) {
                logger.debug("close write-log file failed");
            }
        }
    }
}

From source file:com.streamsets.datacollector.execution.store.FilePipelineStateStore.java

private File[] getHistoryStateFiles(String pipelineName, String rev) {
    // RollingFileAppender creates backup files when the error files reach the size limit.
    // The backup files are of the form errors.json.1, errors.json.2 etc
    // Need to delete all the backup files
    File pipelineDir = PipelineDirectoryUtil.getPipelineDir(runtimeInfo, pipelineName, rev);
    File[] errorFiles = pipelineDir.listFiles(new FilenameFilter() {
        @Override/*  w  w w . ja  v  a  2s.c o m*/
        public boolean accept(File dir, String name) {
            if (name.contains(STATE_FILE_HISTORY)) {
                return true;
            }
            return false;
        }
    });
    return errorFiles;
}

From source file:com.f9g4.webapp.servicesFacade.impl.UploadFacadeApacheCommonsUploadImpl.java

public boolean upload(UploadProperties uploadDetails, int uploadMode, UploadResult result) {
    int exitVal;//w w  w.j  ava2 s  .c  o m
    boolean isFailedProcessing = false;
    //check the folder, if not exist create it.=================
    uploadMode = 3;
    System.out.println("mode ");
    logger.trace("Beginning");

    File folder = new File(profileLocation);
    File folder_100x100 = new File(profileLocation_100);
    File folder_400x400 = new File(profileLocation_400);
    File folder_bigimage = new File(profileLocation_bigimage);
    File folder_source = new File(profileLocation_source);
    //Add folder for files
    File folder_files = new File(profileLocation_files);
    if (!folder.exists())
        folder.mkdir();
    if (!folder_100x100.exists())
        folder_100x100.mkdir();
    if (!folder_400x400.exists())
        folder_400x400.mkdir();
    if (!folder_bigimage.exists())
        folder_bigimage.mkdir();
    if (!folder_source.exists())
        folder_source.mkdir();
    if (!folder_files.exists())
        folder_files.mkdir();

    //      User u = userDAO.getUser(username);

    FileOutputStream fos = null;
    int extIndex = uploadDetails.getFiledata().getOriginalFilename().lastIndexOf('.');
    //fileName = file.name.substr(0, extIndex);
    String original_ext = uploadDetails.getFiledata().getOriginalFilename().substring(extIndex,
            uploadDetails.getFiledata().getOriginalFilename().length());
    //normalize file extension 06/11/2013
    original_ext = original_ext.toLowerCase(); //change to lower case
    if (extIndex != -1 && uploadMode != 3) //skip the ext check when uploadMode is files 
    {
        //if the extension is pdf, then change to AI
        if (original_ext.equals(".pdf"))
            original_ext = ".ai";
        if (original_ext.equals(".jpeg")) //if the extension equals to jpeg, reset to jpg
            original_ext = ".jpg";
    }
    //create filename
    final String uuid = UUID.randomUUID().toString();
    String filename = profileLocation + uuid + original_ext;
    /* 
    this.fileName=uuid + original_ext;
    this.fileName_size_100=UUID.randomUUID().toString()+".jpg";
    this.fileName_size_400=UUID.randomUUID().toString()+".jpg";
    this.fileName_size_original=UUID.randomUUID().toString()+".jpg";
    this.originalFileName = uploadDetails.getFiledata().getOriginalFilename();
    */

    //Set value to UploadResult object
    result.setOriginal_ext(original_ext.replace(".", ""));
    result.setFileName(uuid + original_ext);
    result.setFileName_size_100(UUID.randomUUID().toString() + ".jpg");
    result.setFileName_size_400(UUID.randomUUID().toString() + ".jpg");
    result.setFileName_size_original(UUID.randomUUID().toString() + ".jpg");
    result.setOriginalFileName(uploadDetails.getFiledata().getOriginalFilename());

    try {

        File f = new File(filename);
        if (!f.exists()) {
            f.createNewFile();
        }
        //      
        //         if (!f.exists()){
        //            boolean result = f.mkdir();
        //            
        //            if (!result){
        //               System.out.println("Could not create dir");
        //            }
        //            
        //         }

        //         fos = new FileOutputStream("C:\\Users\\kamlesh\\Downloads\\" + uploadDetails.getFiledata().getOriginalFilename().toUpperCase());
        fos = new FileOutputStream(f);

        fos.write(uploadDetails.getFiledata().getBytes());
        fos.flush();
        fos.close();

        //Convert AI files to Jpeg - use Utility along ghost script
        //decide the parameter by uploadMode
        //Mode==1, no watermark; Mode==2, with watermark; Mode==3, files mode. skip the plmsplitter process.

        String runtimecoomand = "";
        if (uploadMode == 1) {
            runtimecoomand = plmsplitter + " " + filename + " " + plmsplitterparameters + " Renderer="
                    + ghostscript;
        } else if (uploadMode == 2) {
            runtimecoomand = plmsplitter + " " + filename + " " + plmsplitterparametersWithWatermark
                    + " Renderer=" + ghostscript;
        } else if (uploadMode == 3) {
            // Do not need to run plmsplitter.
            // Move the file to files folder.
            //get file
            File temp = new File(filename);
            //create new file
            File dest = new File(profileLocation_files + result.getFileName());
            temp.renameTo(dest);
            return true;
        } else {
            runtimecoomand = plmsplitter + " " + filename + " " + plmsplitterparametersWithWatermark
                    + " Renderer=" + ghostscript;
        }

        //Process process= Runtime.getRuntime().exec(runtimecoomand);
        logger.trace("COMPLETE");

        //wait the process finish and continue the program.====================
        try {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec(runtimecoomand);

            // any error message?
            StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERR");

            // any output?
            StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUT");

            // kick them off
            errorGobbler.start();
            outputGobbler.start();

            // any error???
            exitVal = proc.waitFor();
            //this.uploadStatus=exitVal;
            if (exitVal != 0)
                isFailedProcessing = true;
            else
                isFailedProcessing = false;
        } catch (Throwable t) {
            t.printStackTrace();
            result.setFailedProcessing(true);
            return false;
        }
        /*try
        {
           process.wait();
        }
        catch (Exception e)
        //catch (InterruptedException e) 
        {
           // TODO Auto-generated catch block
           e.printStackTrace();
        }*/

        logger.trace("READEING FILE LIST");
        File dir = new File(profileLocation);
        FilenameFilter filefilter = new FilenameFilter() {
            public boolean accept(File dir, String name) {
                //if the file extension is .jpg return true, else false
                return name.matches(".*" + uuid + ".*jpg");
            }
        };
        String[] fileList = dir.list(filefilter);
        if (fileList.length <= 0) //if no thumbnails images are created after plmsplitter, it's failed processing.
        {
            isFailedProcessing = true;
        }
        //create a FilenameFilter and override its accept-method
        if (isFailedProcessing) {
            //create the dummy images to thumbnails folder to occupies the file name.
            //get dummy image file
            File dummyImageFile = new File(dummyImage);
            File dest100 = new File(profileLocation_100 + result.getFileName_size_100());
            File dest400 = new File(profileLocation_400 + result.getFileName_size_400());
            File destOriginalSize = new File(profileLocation_bigimage + result.getFileName_size_original());
            FileUtils.copyFile(dummyImageFile, dest100); //100*100
            FileUtils.copyFile(dummyImageFile, dest400); //400*400
            FileUtils.copyFile(dummyImageFile, destOriginalSize); //original image size
            //move the file to error files folder
            File temp = new File(filename);
            //create new file
            File dest = new File(profileLocation_error_files + result.getFileName());
            temp.renameTo(dest);
        } else {
            //move source files==================================
            //get file
            File temp = new File(filename);
            //create new file
            File dest = new File(profileLocation_source + result.getFileName());
            temp.renameTo(dest);
            //move generated jpg files.
            for (int i = 0; i < fileList.length; i++) {
                if (fileList[i].matches(".*400x400.*")) {
                    logger.trace("MOVE " + fileList[i] + " to 400x400 folder");
                    //move to 400x400 folder
                    //get file
                    temp = new File(profileLocation + fileList[i]);
                    //create new file
                    dest = new File(profileLocation_400 + result.getFileName_size_400());
                    temp.renameTo(dest);
                } else if (fileList[i].matches(".*100x100.*")) {
                    logger.trace("MOVE " + fileList[i] + " to 100x100 folder");
                    //move to 100x100 folder
                    //get file
                    temp = new File(profileLocation + fileList[i]);
                    //create new file
                    dest = new File(profileLocation_100 + result.getFileName_size_100());
                    temp.renameTo(dest);
                } else {
                    logger.trace("MOVE " + fileList[i] + " to bigimage folder");
                    //move to original folder
                    //get file
                    temp = new File(profileLocation + fileList[i]);
                    //create new file
                    dest = new File(profileLocation_bigimage + result.getFileName_size_original());
                    temp.renameTo(dest);
                }
            }
        }
        /*Update uploadResult object*/
        result.setUploadStatus(exitVal);
        result.setFailedProcessing(isFailedProcessing);
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return false;

    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // Add Runtime to parse the AI files and convert it to jpg    
}

From source file:com.rapid.server.RapidServletContextListener.java

public static int loadConnectionAdapters(ServletContext servletContext) throws Exception {

    int adapterCount = 0;

    // retain our class constructors in a hashtable - this speeds up initialisation
    HashMap<String, Constructor> connectionConstructors = new HashMap<String, Constructor>();

    // create an array list of json objects which we will sort later according to the order
    ArrayList<JSONObject> connectionAdapters = new ArrayList<JSONObject>();

    // get the directory in which the control xml files are stored
    File dir = new File(servletContext.getRealPath("/WEB-INF/database/"));

    // create a filter for finding .control.xml files
    FilenameFilter xmlFilenameFilter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.toLowerCase().endsWith(".connectionadapter.xml");
        }/* ww w .ja  va2  s.  co m*/
    };

    // create a schema object for the xsd
    Schema schema = _schemaFactory
            .newSchema(new File(servletContext.getRealPath("/WEB-INF/schemas/") + "/connectionAdapter.xsd"));
    // create a validator
    Validator validator = schema.newValidator();

    // loop the xml files in the folder
    for (File xmlFile : dir.listFiles(xmlFilenameFilter)) {

        // read the xml into a string
        String xml = Strings.getString(xmlFile);

        // validate the control xml file against the schema
        validator.validate(new StreamSource(new ByteArrayInputStream(xml.getBytes("UTF-8"))));

        // convert the string into JSON
        JSONObject jsonConnectionAdapter = org.json.XML.toJSONObject(xml).getJSONObject("connectionAdapter");

        // get the class name from the json
        String className = jsonConnectionAdapter.getString("class");
        // get the class 
        Class classClass = Class.forName(className);
        // check the class extends com.rapid.data.ConnectionAdapter
        if (!Classes.extendsClass(classClass, com.rapid.data.ConnectionAdapter.class))
            throw new Exception(
                    classClass.getCanonicalName() + " must extend com.rapid.data.ConnectionAdapter");
        // check this class is unique
        if (connectionConstructors.get(className) != null)
            throw new Exception(className + " connection adapter already loaded.");
        // add to constructors hashmap referenced by type
        connectionConstructors.put(className, classClass.getConstructor(ServletContext.class, String.class,
                String.class, String.class, String.class));

        // add to to our array list
        connectionAdapters.add(jsonConnectionAdapter);

        // increment the count
        adapterCount++;

    }

    // sort the connection adapters according to their order property
    Collections.sort(connectionAdapters, new Comparator<JSONObject>() {
        @Override
        public int compare(JSONObject o1, JSONObject o2) {
            try {
                return o1.getInt("order") - o2.getInt("order");
            } catch (JSONException e) {
                return 999;
            }
        }
    });

    // create a JSON Array object which will hold json for all of the available security adapters
    JSONArray jsonConnectionAdapters = new JSONArray();

    // loop the sorted connection adapters and add to the json array
    for (JSONObject jsonConnectionAdapter : connectionAdapters)
        jsonConnectionAdapters.put(jsonConnectionAdapter);

    // put the jsonControls in a context attribute (this is available via the getJsonActions method in RapidHttpServlet)
    servletContext.setAttribute("jsonConnectionAdapters", jsonConnectionAdapters);

    // put the constructors hashmapin a context attribute (this is available via the getContructor method in RapidHttpServlet)
    servletContext.setAttribute("securityConstructors", connectionConstructors);

    _logger.info(adapterCount + " connection adapters loaded in .connectionAdapter.xml files");

    return adapterCount;

}

From source file:io.druid.initialization.Initialization.java

public static List<URL> getURLsForClasspath(String cp) {
    try {/*from   ww  w.  jav  a  2  s  .  c o m*/
        String[] paths = cp.split(File.pathSeparator);

        List<URL> urls = new ArrayList<>();
        for (int i = 0; i < paths.length; i++) {
            File f = new File(paths[i]);
            if ("*".equals(f.getName())) {
                File parentDir = f.getParentFile();
                if (parentDir.isDirectory()) {
                    File[] jars = parentDir.listFiles(new FilenameFilter() {
                        @Override
                        public boolean accept(File dir, String name) {
                            return name != null && (name.endsWith(".jar") || name.endsWith(".JAR"));
                        }
                    });
                    for (File jar : jars) {
                        urls.add(jar.toURI().toURL());
                    }
                }
            } else {
                urls.add(new File(paths[i]).toURI().toURL());
            }
        }
        return urls;
    } catch (IOException ex) {
        throw Throwables.propagate(ex);
    }
}

From source file:org.opennms.ng.dao.support.PropertiesGraphDao.java

private void scanIncludeDirectory(PrefabGraphTypeDao type) throws IOException {
    Resource includeDirectoryResource = type.getIncludeDirectoryResource();

    if (includeDirectoryResource != null) {
        File includeDirectory = includeDirectoryResource.getFile();
        // Include all the files in the directory, knowing that the
        // format is slightly different (no report name required in
        // each property name, and report.id is expected)
        FilenameFilter propertyFilesFilter = new FilenameFilter() {
            @Override/*  w  w  w . ja va 2s .co  m*/
            public boolean accept(File dir, String name) {
                return (name.endsWith(".properties"));
            }
        };
        File[] propertyFiles = includeDirectory.listFiles(propertyFilesFilter);

        for (File file : propertyFiles) {
            loadIncludedFile(type, file);
        }
    }
    type.setLastIncludeScan(System.currentTimeMillis());
}

From source file:com.extjs.JSBuilder2.java

public static void writeHeadersToTargets() {
    Collection<File> outFiles = FileHelper.listFiles(deployDir, new FilenameFilter() {
        private Pattern pattern = Pattern.compile(".*[\\.js|\\.css]");

        public boolean accept(File dir, String name) {
            return pattern.matcher(name).matches()
                    && !(new File(dir.getAbsolutePath() + File.separatorChar + name).isDirectory());
        }/*ww w.ja  v a  2  s . c  o m*/
    }, true);

    for (File f : outFiles) {
        String headerContents = FileHelper.readFileToString(headerFile);
        String codeContents = FileHelper.readFileToString(f);
        FileHelper.writeStringToFile(headerContents, f, false);
        FileHelper.writeStringToFile(codeContents, f, true);
    }
}

From source file:com.awesheet.models.Workbook.java

@Override
public void onMessage(final UIMessage message) {
    switch (message.getType()) {
    case UIMessageType.CREATE_SHEET: {
        addSheet(new Sheet(this, "Sheet " + (newSheetID + 1)));
        break;/*  ww w  .  ja  va 2s.  com*/
    }

    case UIMessageType.SELECT_SHEET: {
        SelectSheetMessage uiMessage = (SelectSheetMessage) message;
        selectSheet(uiMessage.getSheet());
        break;
    }

    case UIMessageType.DELETE_SHEET: {
        DeleteSheetMessage uiMessage = (DeleteSheetMessage) message;
        removeSheet(uiMessage.getSheet());
        break;
    }

    case UIMessageType.CREATE_BAR_CHART: {
        CreateBarChartMessage uiMessage = (CreateBarChartMessage) message;

        // Get selected cells.
        Cell selectedCells[] = getSelectedSheet().collectSelectedCells();

        BarChart chart = new BarChart(selectedCells);
        chart.setNameX(uiMessage.getXaxis());
        chart.setNameY(uiMessage.getYaxis());
        chart.setTitle(uiMessage.getTitle());

        if (!chart.generateImageData()) {
            UIMessageManager.getInstance().dispatchAction(
                    new ShowPopupAction<MessagePopup>(UIPopupType.MESSAGE_POPUP, new MessagePopup("Error",
                            "Could not create a chart. Please make sure the cells you selected are in the correct format.")));
            break;
        }

        UIMessageManager.getInstance()
                .dispatchAction(new ShowPopupAction<ChartPopup>(UIPopupType.VIEW_CHART_POPUP,
                        new ChartPopup(new Base64().encodeAsString(chart.getImageData()))));

        break;
    }

    case UIMessageType.CREATE_LINE_CHART: {
        CreateLineChartMessage uiMessage = (CreateLineChartMessage) message;

        // Get selected cells.
        Cell selectedCells[] = getSelectedSheet().collectSelectedCells();

        LineChart chart = new LineChart(selectedCells);
        chart.setNameX(uiMessage.getXaxis());
        chart.setNameY(uiMessage.getYaxis());
        chart.setTitle(uiMessage.getTitle());

        if (!chart.generateImageData()) {
            UIMessageManager.getInstance().dispatchAction(
                    new ShowPopupAction<MessagePopup>(UIPopupType.MESSAGE_POPUP, new MessagePopup("Error",
                            "Could not create a chart. Please make sure the cells you selected are in the correct format.")));
            break;
        }

        UIMessageManager.getInstance()
                .dispatchAction(new ShowPopupAction<ChartPopup>(UIPopupType.VIEW_CHART_POPUP,
                        new ChartPopup(new Base64().encodeAsString(chart.getImageData()))));

        break;
    }

    case UIMessageType.SAVE_CHART_IMAGE: {
        final SaveChartImageMessage uiMessage = (SaveChartImageMessage) message;

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {

                byte imageData[] = new Base64().decode(uiMessage.getImageData());

                FileDialog dialog = new FileDialog(MainFrame.getInstance(), "Save Chart Image",
                        FileDialog.SAVE);
                dialog.setFile("*.png");
                dialog.setVisible(true);
                dialog.setFilenameFilter(new FilenameFilter() {
                    public boolean accept(File dir, String name) {
                        return (dir.isFile() && name.endsWith(".png"));
                    }
                });

                String filePath = dialog.getFile();
                String directory = dialog.getDirectory();
                dialog.dispose();

                if (directory != null && filePath != null) {
                    String absolutePath = new File(directory + filePath).getAbsolutePath();

                    if (!FileManager.getInstance().saveFile(absolutePath, imageData)) {
                        UIMessageManager.getInstance()
                                .dispatchAction(new ShowPopupAction<MessagePopup>(UIPopupType.MESSAGE_POPUP,
                                        new MessagePopup("Error", "Could not save chart image.")));
                    }
                }
            }
        });

        break;
    }
    }
}

From source file:com.meltmedia.cadmium.core.FileSystemManager.java

public static void cleanUpOld(final String lastUpdatedDir, final int numToKeep) {
    final File lastUpdated = new File(lastUpdatedDir);
    if (lastUpdated.exists()) {
        File parentDir = lastUpdated.getParentFile();
        Matcher nameMatcher = FNAME_PATTERN.matcher(lastUpdated.getName());
        if (nameMatcher.matches()) {
            final String prefixName = nameMatcher.group(1);
            final int dirNumber = Integer.parseInt(nameMatcher.group(2));
            File renderedDirs[] = parentDir.listFiles(new FilenameFilter() {

                @Override/*from  ww w.j  a va 2  s  .c o m*/
                public boolean accept(File file, String name) {
                    if (name.startsWith(prefixName)) {
                        return true;
                    }
                    return false;
                }

            });

            for (File renderedDir : renderedDirs) {
                if (renderedDir.getName().equals(prefixName) && dirNumber > numToKeep) {
                    try {
                        deleteDeep(renderedDir.getAbsolutePath());
                    } catch (Exception e) {
                        log.warn("Failed to delete old dir {}, {}", renderedDir.getName(), e.getMessage());
                    }
                } else {
                    Matcher otherNameMatcher = FNAME_PATTERN.matcher(renderedDir.getName());
                    if (otherNameMatcher.matches()) {
                        int otherDirNumber = Integer.parseInt(otherNameMatcher.group(2));
                        if (otherDirNumber < (dirNumber - numToKeep)) {
                            try {
                                deleteDeep(renderedDir.getAbsolutePath());
                            } catch (Exception e) {
                                log.warn("Failed to delete old dir {}, {}", renderedDir.getName(),
                                        e.getMessage());
                            }
                        }
                    }
                }
            }
        }
    }
}