Search in sources :

Example 1 with FileData

use of org.gluu.oxtrust.model.FileData in project oxTrust by GluuFederation.

the class LogDir method processInt.

/**
	 * Gather periodically site and server status
	 */
private void processInt() {
    GluuAppliance appliance;
    appliance = applianceService.getAppliance();
    String maxLogSize = appliance.getMaxLogSize();
    log.debug("Max Log Size: " + maxLogSize);
    long maxSize = 0;
    try {
        // MB
        maxSize = Long.parseLong(maxLogSize);
    } catch (Exception ex) {
        log.error("appliance maxLogSize value is invalid: " + maxLogSize);
        log.error("assuming 0");
    }
    if (maxSize > 0) {
        log.debug("Max Log Size: " + maxLogSize);
        long maxSizeInByte = maxSize * 1024 * 1024;
        long currentSize = 0;
        Date today = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String todayStr = sdf.format(today);
        log.debug("Getting the tomcat home directory");
        String filePath = ConfigurationFactory.DIR + ConfigurationFactory.LOG_ROTATION_CONFIGURATION;
        log.debug("FilePath: " + filePath);
        List<LogDir> logDirs = readConfig(filePath);
        List<FileData> fDataList = new ArrayList<FileData>();
        for (LogDir logDir : logDirs) {
            File file = new File(logDir.getLocation());
            File[] files = file.listFiles();
            long totalSize = 0;
            if (files != null && files.length > 0) {
                for (File singleFile : files) {
                    if (singleFile.getName().startsWith(logDir.getPrefix()) && singleFile.getName().endsWith(logDir.getExtension())) {
                        totalSize += singleFile.length();
                        FileData fData = new FileData(singleFile.getName(), logDir.getLocation(), singleFile.lastModified(), singleFile.length());
                        fDataList.add(fData);
                    }
                }
            }
            currentSize += totalSize;
            logDir.setLength(totalSize);
        }
        Collections.sort(fDataList);
        if (currentSize > maxSizeInByte) {
            // empty 15% space
            maxSizeInByte -= (maxSizeInByte * 15) / 100;
        // less of the
        // maximum
        // allocated
        }
        for (FileData fileData : fDataList) {
            if (currentSize < maxSizeInByte) {
                break;
            }
            Date date = new Date(fileData.getLastModified());
            String dateStr = sdf.format(date);
            if (todayStr.equals(dateStr)) {
                log.debug("--Skipped Active File: " + fileData.getName() + " Date: " + sdf.format(date) + " Size: " + fileData.getSize());
                continue;
            }
            File singleFile = new File(fileData.getFilePath() + "/" + fileData.getName());
            if (!singleFile.delete()) {
                log.error("Failed to delete the file.");
            } else {
                currentSize -= fileData.getSize();
                log.debug("--Deleted File Name: " + fileData.getName() + " Date: " + sdf.format(date) + " Size: " + fileData.getSize());
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Date(java.util.Date) GluuAppliance(org.gluu.oxtrust.model.GluuAppliance) SimpleDateFormat(java.text.SimpleDateFormat) FileData(org.gluu.oxtrust.model.FileData) File(java.io.File)

Aggregations

File (java.io.File)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 FileData (org.gluu.oxtrust.model.FileData)1 GluuAppliance (org.gluu.oxtrust.model.GluuAppliance)1