<commit_before>/*
*  Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  WSO2 Inc. licenses this file to you under the Apache License,
*  Version 2.0 (the "License"); you may not use this file except
*  in compliance with the License.
*  You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException;
import communicator.CPUPublisher;
import communicator.DASConfigurations;
import communicator.GarbageCollectionPublisher;
import communicator.MemoryPublisher;
import exceptions.PropertyCannotBeLoadedException;
import exceptions.PublisherInitializationException;
import jvmmonitor.UsageMonitorAgent;
import jvmmonitor.UsageMonitorAgentFatory;
import jvmmonitor.exceptions.AccessingUsageStatisticFailedException;
import jvmmonitor.exceptions.MonitorAgentInitializationFailed;
import jvmmonitor.exceptions.UnknownMonitorAgentTypeException;
import org.apache.log4j.Logger;
import org.wso2.carbon.databridge.agent.exception.DataEndpointAgentConfigurationException;
import org.wso2.carbon.databridge.agent.exception.DataEndpointAuthenticationException;
import org.wso2.carbon.databridge.agent.exception.DataEndpointConfigurationException;
import org.wso2.carbon.databridge.agent.exception.DataEndpointException;
import org.wso2.carbon.databridge.commons.exception.TransportException;
import util.PropertyLoader;

import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Class to start JVMMonitor agent
 * Perform the mode checks
 */
public class JVMMonitorAgent {

    private final static Logger logger = Logger.getLogger(JVMMonitorAgent.class);

    public static void main(String[] args) throws AgentLoadException, AgentInitializationException {

        try {
            PropertyLoader.loadProperties();
            logger.info("Properties loaded successfully");

            JVMMonitorAgent jvmMonitor = new JVMMonitorAgent();
            jvmMonitor.runAgent();

        } catch (PropertyCannotBeLoadedException | PublisherInitializationException | MonitorAgentInitializationFailed
                | UnknownMonitorAgentTypeException | AccessingUsageStatisticFailedException e) {
            logger.error(e.getMessage(), e);
        }
    }


    /**
     * Start monitoring of JVMs
     * Select between remote monitoring and local monitoring according to the configurations
     */
    private void runAgent() throws MonitorAgentInitializationFailed, UnknownMonitorAgentTypeException,
            AccessingUsageStatisticFailedException, PublisherInitializationException {

        GarbageCollectionPublisher dasGCPublisher;
        MemoryPublisher dasMemoryPublisher;
        CPUPublisher dasCPUPublisher;

        DASConfigurations dasConfigurations = new DASConfigurations(PropertyLoader.dasAddress,
                PropertyLoader.dasThriftPort, PropertyLoader.dasSecurePort, PropertyLoader.dasUsername,
                PropertyLoader.dasPassword, PropertyLoader.dataAgentConfPath, PropertyLoader.trustStorePath,
                PropertyLoader.trustStorePassword);

        try {

            dasMemoryPublisher = new MemoryPublisher(dasConfigurations);
            dasCPUPublisher = new CPUPublisher(dasConfigurations);
            dasGCPublisher = new GarbageCollectionPublisher(dasConfigurations);

        } catch (DataEndpointException | TransportException | DataEndpointAuthenticationException
                | DataEndpointAgentConfigurationException | DataEndpointConfigurationException e) {
            throw new PublisherInitializationException(e.getMessage(), e);
        }

        //Create usage monitor agent according to the mode in jma.properties
        UsageMonitorAgent usageMonitorAgent = UsageMonitorAgentFatory.getUsageMonitor(PropertyLoader.mode);
        //get generated targeted app_id
        String targetedApplicationId = usageMonitorAgent.getTargetedApplicationId();

        ExecutorService executor = Executors.newFixedThreadPool(4);

        int counter = 0;
        long timeStamp;

        while (true) {

            counter++;

            //get time stamp when publishing the data
            //should be unique to all publishers
            timeStamp = new Date().getTime();

            //Set data to publisher
            dasGCPublisher.setGarbageCollectionStatistic(usageMonitorAgent.getGarbageCollectionStatistics()
                    , targetedApplicationId, timeStamp);

            executor.execute(dasGCPublisher);

            if (counter == 10) {

                dasMemoryPublisher.setMemoryStatistic(usageMonitorAgent.getMemoryStatistics(), targetedApplicationId
                        , timeStamp);
                dasCPUPublisher.setCPUStatistic(usageMonitorAgent.getCPUStatistics(), targetedApplicationId
                        , timeStamp);

                executor.execute(dasMemoryPublisher);
                executor.execute(dasCPUPublisher);

                counter = 0;
            }

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }
    }
}<commit_msg>Improve periodic task using TimerTask<commit_after>/*
*  Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  WSO2 Inc. licenses this file to you under the Apache License,
*  Version 2.0 (the "License"); you may not use this file except
*  in compliance with the License.
*  You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import communicator.CPUPublisher;
import communicator.DASConfigurations;
import communicator.GarbageCollectionPublisher;
import communicator.MemoryPublisher;
import exceptions.PropertyCannotBeLoadedException;
import exceptions.PublisherInitializationException;
import jvmmonitor.UsageMonitorAgent;
import jvmmonitor.UsageMonitorAgentFatory;
import jvmmonitor.exceptions.AccessingUsageStatisticFailedException;
import jvmmonitor.exceptions.MonitorAgentInitializationFailed;
import jvmmonitor.exceptions.UnknownMonitorAgentTypeException;
import org.apache.log4j.Logger;
import org.wso2.carbon.databridge.agent.exception.DataEndpointAgentConfigurationException;
import org.wso2.carbon.databridge.agent.exception.DataEndpointAuthenticationException;
import org.wso2.carbon.databridge.agent.exception.DataEndpointConfigurationException;
import org.wso2.carbon.databridge.agent.exception.DataEndpointException;
import org.wso2.carbon.databridge.commons.exception.TransportException;
import util.PropertyLoader;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Class to start JVMMonitor agent
 * Perform the mode checks
 */
public class JVMMonitorAgent extends TimerTask {

    private final static Logger logger = Logger.getLogger(JVMMonitorAgent.class);

    //DAS Publisher
    private GarbageCollectionPublisher dasGCPublisher;
    private MemoryPublisher dasMemoryPublisher;
    private CPUPublisher dasCPUPublisher;

    private UsageMonitorAgent usageMonitorAgent;
    private String targetedApplicationId;
    private ExecutorService executor;
    private int counter = 1;

    /**
     * Constructor
     * <p>
     * Initialize DASConfigurations
     * Initialize DAS publisher
     * Select between remote monitoring and local monitoring according to the configurations
     */
    private JVMMonitorAgent() throws MonitorAgentInitializationFailed, UnknownMonitorAgentTypeException,
            PublisherInitializationException {

        DASConfigurations dasConfigurations = new DASConfigurations(PropertyLoader.dasAddress,
                PropertyLoader.dasThriftPort, PropertyLoader.dasSecurePort, PropertyLoader.dasUsername,
                PropertyLoader.dasPassword, PropertyLoader.dataAgentConfPath, PropertyLoader.trustStorePath,
                PropertyLoader.trustStorePassword);

        try {

            dasMemoryPublisher = new MemoryPublisher(dasConfigurations);
            dasCPUPublisher = new CPUPublisher(dasConfigurations);
            dasGCPublisher = new GarbageCollectionPublisher(dasConfigurations);

        } catch (DataEndpointException | TransportException | DataEndpointAuthenticationException
                | DataEndpointAgentConfigurationException | DataEndpointConfigurationException e) {
            throw new PublisherInitializationException(e.getMessage(), e);
        }

        //Initialize usage monitor agent according to the mode in jma.properties
        usageMonitorAgent = UsageMonitorAgentFatory.getUsageMonitor(PropertyLoader.mode);
        //Get generated targeted app_id
        targetedApplicationId = usageMonitorAgent.getTargetedApplicationId();

        executor = Executors.newFixedThreadPool(4);

    }

    @Override
    public void run() {

        //get time stamp when publishing the data (should be unique to all publishers)
        long timeStamp = new Date().getTime();

        try {
            //Set garbage collection statistic to publish
            dasGCPublisher.setGarbageCollectionStatistic(usageMonitorAgent.getGarbageCollectionStatistics()
                    , targetedApplicationId, timeStamp);
            executor.execute(dasGCPublisher);

        } catch (AccessingUsageStatisticFailedException e) {
            logger.error(e.getMessage(), e);
        }

        if (counter == 10) {
            try {
                //Set Memory statistic to publish
                dasMemoryPublisher.setMemoryStatistic(usageMonitorAgent.getMemoryStatistics(), targetedApplicationId
                        , timeStamp);
                executor.execute(dasMemoryPublisher);

                //Set CPU statistic to publish
                dasCPUPublisher.setCPUStatistic(usageMonitorAgent.getCPUStatistics(), targetedApplicationId
                        , timeStamp);
                executor.execute(dasCPUPublisher);

            } catch (AccessingUsageStatisticFailedException e) {
                logger.error(e.getMessage(), e);
            }

            counter = 0;
        }

        counter++;
    }


    public static void main(String[] args) {

        try {
            PropertyLoader.loadProperties();
            logger.info("Properties loaded successfully");

            JVMMonitorAgent jvmMonitor = new JVMMonitorAgent();

            Timer timer = new Timer();
            timer.schedule(jvmMonitor, 0, 100);

        } catch (PropertyCannotBeLoadedException | PublisherInitializationException | MonitorAgentInitializationFailed
                | UnknownMonitorAgentTypeException e) {
            logger.error(e.getMessage(), e);
        }

    }

}
{'fraction_non_alphanumeric': 0.06767376877401328, 'fraction_numerical': 0.004453370590289905, 'mean_word_length': 3.341546626231994, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1557944', 'n_tokens_mistral': 2903, 'n_tokens_neox': 2701, 'n_words': 680}