All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bdware.doip.audit.writer.OriginalAuditConfig Maven / Gradle / Ivy

The newest version!
package org.bdware.doip.audit.writer;

import com.google.gson.JsonObject;

import org.bdware.doip.audit.BasicDB;
import org.bdware.doip.audit.OriginalAuditDB;
import org.bdware.doip.auditrepo.AutoAuditDO;

import java.sql.Timestamp;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class OriginalAuditConfig extends AuditConfig {
    OriginalAuditDB originalAuditDB;
    String auditLogTableName;
    String auditRuleTableName;

    transient OldDataPruner pruner;

    public void clearPruner() {
        if (pruner != null)
            pruner.stop();
    }

    class OldDataPruner extends TimerTask {

        long interval;
        private Timer timer = new Timer();

        public OldDataPruner(long clearInterval) {
            interval = clearInterval;
        }

        public void start() {
            timer.scheduleAtFixedRate(this, 0, interval);
        }

        @Override
        public void run() {
            configurePurgeStrategyByTime(System.currentTimeMillis() - interval);
        }

        public boolean configurePurgeStrategyByTime(long timeStamp) {
            OriginalAuditConfig.this.configurePurgeStrategyByTime(timeStamp);
            return true;
        }

        public void stop() {
            this.cancel();
            this.timer.cancel();
        }
    }

    public OriginalAuditConfig(AuditRepo repo, JsonObject extraArgs) {
        super(repo);
        try {
            JsonObject auditLogConfig = extraArgs.get("originalDBConfig").getAsJsonObject();
            String driverClass = auditLogConfig.get("driverClass").getAsString();
            String url = auditLogConfig.get("url").getAsString();
            String user = auditLogConfig.get("user").getAsString();
            String password = auditLogConfig.get("password").getAsString();

            auditLogTableName = auditLogConfig.get("auditLogTableName").getAsString();
            auditRuleTableName = auditLogConfig.get("auditRuleTableName").getAsString();
            originalAuditDB = new OriginalAuditDB(driverClass, url, user, password);
            if (auditLogConfig.has("clearInterval")) {
                long clearInterval = auditLogConfig.get("clearInterval").getAsLong();
                pruner = new OldDataPruner(clearInterval);
                pruner.start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List getOriginalLogByHash(String requestHash, String responseHash) {
        return originalAuditDB.retrieve(auditLogTableName, requestHash, responseHash);
    }

    public boolean isExist(String requestHash, String responseHash) {
        return originalAuditDB.isExist(auditLogTableName, requestHash, responseHash);
    }

    @Override
    public LogWriter createLogWriterInstance() {
        return new OriginalWriter(this, originalAuditDB, auditLogTableName);
    }

    @Override
    public AuditType getAuditType() {
        return AuditType.WithOriginal;
    }

    public boolean configurePurgeStrategyByTime(long timestamp) {
        int count = originalAuditDB.removeByTime(auditLogTableName, "create_time", timestamp);
        return count>=0;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy