<commit_before>/*
 * Copyright 2011 <NAME> <<EMAIL>>
 *
 * Licensed 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.
 */
package org.tyrannyofheaven.bukkit.zPermissions;

import static org.tyrannyofheaven.bukkit.util.ToHUtils.colorize;
import static org.tyrannyofheaven.bukkit.util.ToHUtils.copyResourceToFile;
import static org.tyrannyofheaven.bukkit.util.ToHUtils.hasText;
import static org.tyrannyofheaven.bukkit.util.ToHUtils.sendMessage;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.persistence.PersistenceException;

import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.config.ConfigurationNode;
import org.tyrannyofheaven.bukkit.util.ToHUtils;
import org.tyrannyofheaven.bukkit.util.command.ToHCommandExecutor;
import org.tyrannyofheaven.bukkit.util.transaction.AvajeTransactionStrategy;
import org.tyrannyofheaven.bukkit.util.transaction.TransactionCallback;
import org.tyrannyofheaven.bukkit.util.transaction.TransactionStrategy;
import org.tyrannyofheaven.bukkit.zPermissions.dao.AvajePermissionDao;
import org.tyrannyofheaven.bukkit.zPermissions.dao.PermissionDao;
import org.tyrannyofheaven.bukkit.zPermissions.model.Entry;
import org.tyrannyofheaven.bukkit.zPermissions.model.Membership;
import org.tyrannyofheaven.bukkit.zPermissions.model.PermissionEntity;
import org.tyrannyofheaven.bukkit.zPermissions.model.PermissionRegion;
import org.tyrannyofheaven.bukkit.zPermissions.model.PermissionWorld;

import com.avaje.ebean.cache.ServerCache;
import com.avaje.ebean.cache.ServerCacheOptions;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;

/**
 * zPermissions main class.
 * 
 * @author asaddi
 */
public class ZPermissionsPlugin extends JavaPlugin {

    // Name of the default group, in absence of a config file
    private static final String DEFAULT_GROUP = "default";

    // Name of the default track, in absence of a config file
    private static final String DEFAULT_TRACK = "default";

    // Default dump directory
    private static final String DEFAULT_DUMP_DIRECTORY = "zPermissions-dumps";

    // Default timeout for temporary permissions
    private static final int DEFAULT_TEMP_PERMISSION_TIMEOUT = 60;

    // Default max-idle for Avaje bean cache
    private static final int DEFAULT_CACHE_IDLE = 180;

    // Default TTL for Avaje bean cache
    private static final int DEFAULT_CACHE_TTL = 600;

    // Default size of Avaje bean cache
    private static final int DEFAULT_CACHE_SIZE = 1000;

    // This plugin's logger
    private final Logger logger = Logger.getLogger(getClass().getName());

    // Internal state kept about each online player
    private final Map<String, PlayerState> playerStates = new HashMap<String, PlayerState>();

    // Permission resolver
    private final PermissionsResolver resolver = new PermissionsResolver(this);

    // The configured default track
    private String defaultTrack;

    // The configured dump directory
    private File dumpDirectory;

    // The configured default temporary permission timeout
    private int defaultTempPermissionTimeout;

    // The configured bean cache max-idle
    private int cacheMaxIdle;
    
    // The configured bean cache TTL
    private int cacheMaxTtl;

    // The configured bean cache size
    private int cacheSize;

    // Track definitions
    private Map<String, List<String>> tracks = new HashMap<String, List<String>>();

    // TransactionStrategy implementation
    private TransactionStrategy transactionStrategy;

    // DAO implementation
    private PermissionDao dao;

    // WorldGuard, if present
    private WorldGuardPlugin worldGuardPlugin;

    /**
     * Retrieve this plugin's TransactionStrategy
     * 
     * @return the TransactionStrategy
     */
    TransactionStrategy getTransactionStrategy() {
        return transactionStrategy;
    }

    /**
     * Retrieve this plugin's DAO.
     * 
     * @return the DAO
     */
    PermissionDao getDao() {
        return dao;
    }

    // Retrieve the PermissionResolver instance
    PermissionsResolver getResolver() {
        return resolver;
    }

    /* (non-Javadoc)
     * @see org.bukkit.plugin.Plugin#onDisable()
     */
    @Override
    public void onDisable() {
        // Clear any player state

        // Make copy before clearing
        Map<String, PlayerState> copy; 
        synchronized (playerStates) {
            copy = new HashMap<String, PlayerState>(playerStates);
            playerStates.clear();
        }
        // Remove attachments
        for (PlayerState playerState : copy.values()) {
            playerState.getAttachment().remove();
        }

        log("%s disabled.", getDescription().getVersion());
    }

    /* (non-Javadoc)
     * @see org.bukkit.plugin.Plugin#onEnable()
     */
    @Override
    public void onEnable() {
        log("%s starting...", getDescription().getVersion());

        // Create data directory, if needed
        if (!getDataFolder().exists()) {
            if (!getDataFolder().mkdirs()) {
                ToHUtils.log(this, Level.SEVERE, "Unable to create data folder");<commit_msg>Make sure cache monitor is canceled on disable.<commit_after>/*
 * Copyright 2011 <NAME> <<EMAIL>>
 *
 * Licensed 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.
 */
package org.tyrannyofheaven.bukkit.zPermissions;

import static org.tyrannyofheaven.bukkit.util.ToHUtils.colorize;
import static org.tyrannyofheaven.bukkit.util.ToHUtils.copyResourceToFile;
import static org.tyrannyofheaven.bukkit.util.ToHUtils.hasText;
import static org.tyrannyofheaven.bukkit.util.ToHUtils.sendMessage;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.persistence.PersistenceException;

import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.config.ConfigurationNode;
import org.tyrannyofheaven.bukkit.util.ToHUtils;
import org.tyrannyofheaven.bukkit.util.command.ToHCommandExecutor;
import org.tyrannyofheaven.bukkit.util.transaction.AvajeTransactionStrategy;
import org.tyrannyofheaven.bukkit.util.transaction.TransactionCallback;
import org.tyrannyofheaven.bukkit.util.transaction.TransactionStrategy;
import org.tyrannyofheaven.bukkit.zPermissions.dao.AvajePermissionDao;
import org.tyrannyofheaven.bukkit.zPermissions.dao.PermissionDao;
import org.tyrannyofheaven.bukkit.zPermissions.model.Entry;
import org.tyrannyofheaven.bukkit.zPermissions.model.Membership;
import org.tyrannyofheaven.bukkit.zPermissions.model.PermissionEntity;
import org.tyrannyofheaven.bukkit.zPermissions.model.PermissionRegion;
import org.tyrannyofheaven.bukkit.zPermissions.model.PermissionWorld;

import com.avaje.ebean.cache.ServerCache;
import com.avaje.ebean.cache.ServerCacheOptions;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;

/**
 * zPermissions main class.
 * 
 * @author asaddi
 */
public class ZPermissionsPlugin extends JavaPlugin {

    // Name of the default group, in absence of a config file
    private static final String DEFAULT_GROUP = "default";

    // Name of the default track, in absence of a config file
    private static final String DEFAULT_TRACK = "default";

    // Default dump directory
    private static final String DEFAULT_DUMP_DIRECTORY = "zPermissions-dumps";

    // Default timeout for temporary permissions
    private static final int DEFAULT_TEMP_PERMISSION_TIMEOUT = 60;

    // Default max-idle for Avaje bean cache
    private static final int DEFAULT_CACHE_IDLE = 180;

    // Default TTL for Avaje bean cache
    private static final int DEFAULT_CACHE_TTL = 600;

    // Default size of Avaje bean cache
    private static final int DEFAULT_CACHE_SIZE = 1000;

    // This plugin's logger
    private final Logger logger = Logger.getLogger(getClass().getName());

    // Internal state kept about each online player
    private final Map<String, PlayerState> playerStates = new HashMap<String, PlayerState>();

    // Permission resolver
    private final PermissionsResolver resolver = new PermissionsResolver(this);

    // The configured default track
    private String defaultTrack;

    // The configured dump directory
    private File dumpDirectory;

    // The configured default temporary permission timeout
    private int defaultTempPermissionTimeout;

    // The configured bean cache max-idle
    private int cacheMaxIdle;
    
    // The configured bean cache TTL
    private int cacheMaxTtl;

    // The configured bean cache size
    private int cacheSize;

    // Track definitions
    private Map<String, List<String>> tracks = new HashMap<String, List<String>>();

    // TransactionStrategy implementation
    private TransactionStrategy transactionStrategy;

    // DAO implementation
    private PermissionDao dao;

    // WorldGuard, if present
    private WorldGuardPlugin worldGuardPlugin;

    /**
     * Retrieve this plugin's TransactionStrategy
     * 
     * @return the TransactionStrategy
     */
    TransactionStrategy getTransactionStrategy() {
        return transactionStrategy;
    }

    /**
     * Retrieve this plugin's DAO.
     * 
     * @return the DAO
     */
    PermissionDao getDao() {
        return dao;
    }

    // Retrieve the PermissionResolver instance
    PermissionsResolver getResolver() {
        return resolver;
    }

    /* (non-Javadoc)
     * @see org.bukkit.plugin.Plugin#onDisable()
     */
    @Override
    public void onDisable() {
        // Shut off monitor task, if running
        getServer().getScheduler().cancelTasks(this);

        // Clear any player state

        // Make copy before clearing
        Map<String, PlayerState> copy; 
        synchronized (playerStates) {
            copy = new HashMap<String, PlayerState>(playerStates);
            playerStates.clear();
        }
        // Remove attachments
        for (PlayerState playerState : copy.values()) {
            playerState.getAttachment().remove();
        }

        log("%s disabled.", getDescription().getVersion());
    }

    /* (non-Javadoc)
     * @see org.bukkit.plugin.Plugin#onEnable()
     */
    @Override
    public void onEnable() {
        log("%s starting...", getDescription().getVersion());

        // Create data directory, if needed
        if (!getDataFolder().exists()) {
            if (!getDataFolder().mkdirs()) {
                ToHUtils.log(this, Level.SEVERE, "Unable to create data folder");
{'fraction_non_alphanumeric': 0.0883672488383272, 'fraction_numerical': 0.004806921967633392, 'mean_word_length': 3.5927152317880795, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '3143421', 'n_tokens_mistral': 3559, 'n_tokens_neox': 3427, 'n_words': 939}