<commit_before>/**
 * Copyright (c) 2002-2010 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.neo4j.ext.udc.impl;

import java.util.Properties;
import java.util.Timer;

import org.neo4j.ext.udc.UdcProperties;
import org.neo4j.helpers.Service;
import org.neo4j.kernel.Config;
import org.neo4j.kernel.KernelExtension;
import org.neo4j.kernel.Version;
import org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaDataSource;

/**
 * Kernel extension for UDC, the Usage Data Collector. The UDC runs as a background
 * daemon, waking up once a day to collect basic usage information about a long
 * running graph database.
 * <p/>
 * The first update is delayed to avoid needless activity during integration
 * testing and short-run applications. Subsequent updates are made at regular
 * intervals. Both times are specified in milliseconds.
 *
 */
@Service.Implementation(KernelExtension.class)
public class UdcExtensionImpl extends KernelExtension implements UdcProperties {

    public static final String UDC_SOURCE_DISTRIBUTION_KEY = "neo4j.ext.udc.host";

    /**
     * Delay, in milliseconds, before the first UDC update is sent.
     *
     * Defaults to 10 minutes.
     */
    private int firstDelay = 10 * 1000 * 60;

    /**
     * Millisecond interval for subsequent updates.
     *
     * Defaults to 24 hours.
     */
    private int interval = 1000 * 60 * 60 * 24;

    /**
     * Host address to which UDC updates will be sent.
     */
    private String hostAddress = "udc.neo4j.org";

    /**
     * Disable the extension.
     *
     * Defaults to false.
     */
    private boolean disabled = false;

    /**
     * No-arg constructor, sets the extension key to "kernel udc".
     *  
     */
    public UdcExtensionImpl() {
        super("kernel udc");
    }

    /**
     * Lifecycle load event, which occurs during the startup of
     * a new GraphDbInstance.
     *
     * Configuration information is retrieved from the KernelData,
     * then a daemon thread is started to periodically collect
     * and submit usage data.
     *
     * @param kernel reference to the loading graph database kernel.
     */
    @Override
    protected void load(KernelData kernel) {
        configure(kernel.getConfig());

        // ABK: a hack to register this extension with the kernel, which
        // only knows about extensions that have a saved state
        kernel.setState(this, new Object());

        if (!disabled) {
            Timer timer = new Timer();
            NeoStoreXaDataSource ds = (NeoStoreXaDataSource) kernel.getConfig().getTxModule().getXaDataSourceManager().getXaDataSource("nioneodb");
            String storeId = Long.toHexString(ds.getRandomIdentifier());
            UdcTimerTask task = new UdcTimerTask(hostAddress, Version.VERSION.getRevision(), storeId);
            timer.scheduleAtFixedRate(task, firstDelay, interval);
        }
    }

    /**
     * Attempt to retrieve configuration provided by user.
     *<commit_msg>added an explicit unload implementation to cancel the thread<commit_after>/**
 * Copyright (c) 2002-2010 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.neo4j.ext.udc.impl;

import java.util.Properties;
import java.util.Timer;

import org.neo4j.ext.udc.UdcProperties;
import org.neo4j.helpers.Service;
import org.neo4j.kernel.Config;
import org.neo4j.kernel.KernelExtension;
import org.neo4j.kernel.Version;
import org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaDataSource;

/**
 * Kernel extension for UDC, the Usage Data Collector. The UDC runs as a background
 * daemon, waking up once a day to collect basic usage information about a long
 * running graph database.
 * <p/>
 * The first update is delayed to avoid needless activity during integration
 * testing and short-run applications. Subsequent updates are made at regular
 * intervals. Both times are specified in milliseconds.
 *
 */
@Service.Implementation(KernelExtension.class)
public class UdcExtensionImpl extends KernelExtension implements UdcProperties {

    public static final String UDC_SOURCE_DISTRIBUTION_KEY = "neo4j.ext.udc.host";

    /**
     * Delay, in milliseconds, before the first UDC update is sent.
     *
     * Defaults to 10 minutes.
     */
    private int firstDelay = 10 * 1000 * 60;

    /**
     * Millisecond interval for subsequent updates.
     *
     * Defaults to 24 hours.
     */
    private int interval = 1000 * 60 * 60 * 24;

    /**
     * Host address to which UDC updates will be sent.
     */
    private String hostAddress = "udc.neo4j.org";

    /**
     * Disable the extension.
     *
     * Defaults to false.
     */
    private boolean disabled = false;

    private Timer timer;

    /**
     * No-arg constructor, sets the extension key to "kernel udc".
     *  
     */
    public UdcExtensionImpl() {
        super("kernel udc");
    }

    /**
     * Lifecycle load event, which occurs during the startup of
     * a new GraphDbInstance.
     *
     * Configuration information is retrieved from the KernelData,
     * then a daemon thread is started to periodically collect
     * and submit usage data.
     *
     * @param kernel reference to the loading graph database kernel.
     */
    @Override
    protected void load(KernelData kernel) {
        configure(kernel.getConfig());

        // ABK: a hack to register this extension with the kernel, which
        // only knows about extensions that have a saved state
        kernel.setState(this, new Object());

        if (!disabled) {
            timer = new Timer();
            NeoStoreXaDataSource ds = (NeoStoreXaDataSource) kernel.getConfig().getTxModule().getXaDataSourceManager().getXaDataSource("nioneodb");
            String storeId = Long.toHexString(ds.getRandomIdentifier());
            UdcTimerTask task = new UdcTimerTask(hostAddress, Version.VERSION.getRevision(), storeId);
            timer.scheduleAtFixedRate(task, firstDelay, interval);
        }
    }


    @Override
    protected void unload(KernelData kernel) {
        timer.cancel();
    }

    /**
     * Attempt to retrieve configuration provided by user.
     *
{'fraction_non_alphanumeric': 0.08421330517423442, 'fraction_numerical': 0.011087645195353749, 'mean_word_length': 2.9198137609932746, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6202906', 'n_tokens_mistral': 2145, 'n_tokens_neox': 2023, 'n_words': 784}