<commit_before>package io.hawt.jmx;

import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.TabularData;

public class QuartzFacade implements QuartzFacadeMBean {

    private ObjectName objectName;
    private MBeanServer mBeanServer;

    public void init() throws Exception {
        if (objectName == null) {
            objectName = getObjectName();
        }

        if (mBeanServer == null) {
            mBeanServer = ManagementFactory.getPlatformMBeanServer();
        }

        if (mBeanServer != null) {
            try {
                mBeanServer.registerMBean(this, objectName);
            } catch (InstanceAlreadyExistsException iaee) {
                // Try to remove and re-register
                mBeanServer.unregisterMBean(objectName);
                mBeanServer.registerMBean(this, objectName);
            }
        }
    }

    public void destroy() throws Exception {
        if (mBeanServer != null) {
            if (objectName != null) {
                mBeanServer.unregisterMBean(objectName);
            }
        }
    }

    protected ObjectName getObjectName() throws Exception {
        return new ObjectName("hawtio:type=QuartzFacade");
    }

    @Override
    @SuppressWarnings("unchecked")
    public void updateSimpleTrigger(String schedulerObjectName, String triggerName, String groupName, int repeatCount, long repeatInterval) throws Exception {
        if (schedulerObjectName == null) {
            throw new IllegalArgumentException("Cannot find quartz scheduler with ObjectName: " + schedulerObjectName);
        }

        ObjectName on = ObjectName.getInstance(schedulerObjectName);
        if (!mBeanServer.isRegistered(on)) {
            throw new IllegalArgumentException("Cannot find quartz scheduler with ObjectName: " + schedulerObjectName);
        }

        // get existing job map
        CompositeData data = (CompositeData) mBeanServer.invoke(on, "getJobDetail", new Object[]{triggerName, groupName}, new String[]{"java.lang.String", "java.lang.String"});
        if (data == null) {
            throw new IllegalArgumentException("Cannot find job details for group: " + groupName + " name: " + triggerName);
        }

        Map jobParams = new HashMap();
        Map jobDataMap = new HashMap();
        initJobParams(jobParams, jobDataMap, triggerName, groupName, data);

        Map triggerParams = new HashMap();
        triggerParams.put("repeatCount", repeatCount);
        triggerParams.put("repeatInterval", repeatInterval);
        triggerParams.put("name", triggerName);
        triggerParams.put("group", groupName);
        triggerParams.put("jobName", triggerName);
        triggerParams.put("jobGroup", groupName);

        // update trigger
        mBeanServer.invoke(on, "scheduleBasicJob", new Object[]{jobParams, triggerParams}, new String[]{"java.util.Map", "java.util.Map"});
    }

    @Override
    @SuppressWarnings("unchecked")
    public void updateCronTrigger(String schedulerObjectName, String triggerName, String groupName, String cronExpression, String timeZone) throws Exception {
        if (schedulerObjectName == null) {
            throw new IllegalArgumentException("Cannot find quartz scheduler with ObjectName: " + schedulerObjectName);
        }

        ObjectName on = ObjectName.getInstance(schedulerObjectName);
        if (!mBeanServer.isRegistered(on)) {
            throw new IllegalArgumentException("Cannot find quartz scheduler with ObjectName: " + schedulerObjectName);
        }

        // get existing job map
        CompositeData data = (CompositeData) mBeanServer.invoke(on, "getJobDetail", new Object[]{triggerName, groupName}, new String[]{"java.lang.String", "java.lang.String"});
        if (data == null) {
            throw new IllegalArgumentException("Cannot find job details for group: " + groupName + " name: " + triggerName);
        }

        Map jobParams = new HashMap();
        Map jobDataMap = new HashMap();
        initJobParams(jobParams, jobDataMap, triggerName, groupName, data);

        Map triggerParams = new HashMap();
        triggerParams.put("cronExpression", cronExpression);
        if (timeZone != null) {
            triggerParams.put("timeZone", timeZone);
        }
        triggerParams.put("name", triggerName);
        triggerParams.put("group", groupName);
        triggerParams.put("jobName", triggerName);
        triggerParams.put("jobGroup", groupName);

        // update trigger
        mBeanServer.invoke(on, "scheduleBasicJob", new Object[]{jobParams, triggerParams}, new String[]{"java.util.Map", "java.util.Map"});
    }

    private void initJobParams(Map jobParams, Map jobDataMap, String triggerName, String groupName, CompositeData data) {
        jobParams.put("name", triggerName);
        jobParams.put("group", groupName);
        if (data.get("description") != null) {
            jobParams.put("description", data.get("description"));
        }
        jobParams.put("jobClass", data.get("jobClass"));
        jobParams.put("durability", data.get("durability"));
        jobParams.put("shouldRecover", data.get("shouldRecover"));
        // JMX API with TabularData and CompositeData is cluttered to use
        TabularData tJobDataMap = (TabularData) data.get("jobDataMap");
        for (Object cKey : tJobDataMap.keySet()) {
            Object key = ((List) cKey).get(0);
            Object value = tJobDataMap.get(new Object[]{key});
            if (value != null) {
                CompositeData cd = (CompositeData) value;
                Iterator it = cd.values().iterator();
                Object tKey = it.next();
                Object tValue = it.next();
                jobDataMap.put(tKey, tValue);
            }
        }
        jobParams.put("jobDataMap", jobDataMap);
    }
}<commit_msg>#786: Quartz plugin. Work in progress.<commit_after>package io.hawt.jmx;

import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.TabularData;

public class QuartzFacade implements QuartzFacadeMBean {

    private ObjectName objectName;
    private MBeanServer mBeanServer;

    public void init() throws Exception {
        if (objectName == null) {
            objectName = getObjectName();
        }

        if (mBeanServer == null) {
            mBeanServer = ManagementFactory.getPlatformMBeanServer();
        }

        if (mBeanServer != null) {
            try {
                mBeanServer.registerMBean(this, objectName);
            } catch (InstanceAlreadyExistsException iaee) {
                // Try to remove and re-register
                mBeanServer.unregisterMBean(objectName);
                mBeanServer.registerMBean(this, objectName);
            }
        }
    }

    public void destroy() throws Exception {
        if (mBeanServer != null) {
            if (objectName != null) {
                mBeanServer.unregisterMBean(objectName);
            }
        }
    }

    protected ObjectName getObjectName() throws Exception {
        return new ObjectName("hawtio:type=QuartzFacade");
    }

    @Override
    @SuppressWarnings("unchecked")
    public void updateSimpleTrigger(String schedulerObjectName, String triggerName, String groupName, int repeatCount, long repeatInterval) throws Exception {
        if (schedulerObjectName == null) {
            throw new IllegalArgumentException("Cannot find quartz scheduler with ObjectName: " + schedulerObjectName);
        }

        ObjectName on = ObjectName.getInstance(schedulerObjectName);
        if (!mBeanServer.isRegistered(on)) {
            throw new IllegalArgumentException("Cannot find quartz scheduler with ObjectName: " + schedulerObjectName);
        }

        // get existing job map
        CompositeData data = (CompositeData) mBeanServer.invoke(on, "getJobDetail", new Object[]{triggerName, groupName}, new String[]{"java.lang.String", "java.lang.String"});
        if (data == null) {
            throw new IllegalArgumentException("Cannot find job details for group: " + groupName + " name: " + triggerName);
        }

        Map jobParams = new HashMap();
        Map jobDataMap = new HashMap();
        initJobParams(jobParams, jobDataMap, triggerName, groupName, data);

        // also ensure the job data map is up to date with the simple trigger changes
        Map triggerParams = new HashMap();
        triggerParams.put("repeatCount", repeatCount);
        jobDataMap.put("CamelQuartzTriggerType", "simple");
        jobDataMap.put("CamelQuartzTriggerSimpleRepeatCounter", repeatCount);
        triggerParams.put("repeatInterval", repeatInterval);
        jobDataMap.put("CamelQuartzTriggerSimpleRepeatInterval", repeatCount);
        triggerParams.put("name", triggerName);
        triggerParams.put("group", groupName);
        triggerParams.put("jobName", triggerName);
        triggerParams.put("jobGroup", groupName);

        // update trigger
        mBeanServer.invoke(on, "scheduleBasicJob", new Object[]{jobParams, triggerParams}, new String[]{"java.util.Map", "java.util.Map"});
    }

    @Override
    @SuppressWarnings("unchecked")
    public void updateCronTrigger(String schedulerObjectName, String triggerName, String groupName, String cronExpression, String timeZone) throws Exception {
        if (schedulerObjectName == null) {
            throw new IllegalArgumentException("Cannot find quartz scheduler with ObjectName: " + schedulerObjectName);
        }

        ObjectName on = ObjectName.getInstance(schedulerObjectName);
        if (!mBeanServer.isRegistered(on)) {
            throw new IllegalArgumentException("Cannot find quartz scheduler with ObjectName: " + schedulerObjectName);
        }

        // get existing job map
        CompositeData data = (CompositeData) mBeanServer.invoke(on, "getJobDetail", new Object[]{triggerName, groupName}, new String[]{"java.lang.String", "java.lang.String"});
        if (data == null) {
            throw new IllegalArgumentException("Cannot find job details for group: " + groupName + " name: " + triggerName);
        }

        Map jobParams = new HashMap();
        Map jobDataMap = new HashMap();
        initJobParams(jobParams, jobDataMap, triggerName, groupName, data);

        Map triggerParams = new HashMap();
        // also ensure the job data map is up to date with the cron trigger changes
        triggerParams.put("cronExpression", cronExpression);
        jobDataMap.put("CamelQuartzTriggerType", "cron");
        jobDataMap.put("CamelQuartzTriggerCronExpression", cronExpression);
        if (timeZone != null) {
            triggerParams.put("timeZone", timeZone);
            jobDataMap.put("CamelQuartzTriggerCronTimeZone", timeZone);
        }
        triggerParams.put("name", triggerName);
        triggerParams.put("group", groupName);
        triggerParams.put("jobName", triggerName);
        triggerParams.put("jobGroup", groupName);

        // update trigger
        mBeanServer.invoke(on, "scheduleBasicJob", new Object[]{jobParams, triggerParams}, new String[]{"java.util.Map", "java.util.Map"});
    }

    private void initJobParams(Map jobParams, Map jobDataMap, String triggerName, String groupName, CompositeData data) {
        jobParams.put("name", triggerName);
        jobParams.put("group", groupName);
        if (data.get("description") != null) {
            jobParams.put("description", data.get("description"));
        }
        jobParams.put("jobClass", data.get("jobClass"));
        jobParams.put("durability", data.get("durability"));
        jobParams.put("shouldRecover", data.get("shouldRecover"));
        // JMX API with TabularData and CompositeData is cluttered to use
        TabularData tJobDataMap = (TabularData) data.get("jobDataMap");
        for (Object cKey : tJobDataMap.keySet()) {
            Object key = ((List) cKey).get(0);
            Object value = tJobDataMap.get(new Object[]{key});
            if (value != null) {
                CompositeData cd = (CompositeData) value;
                Iterator it = cd.values().iterator();
                Object tKey = it.next();
                Object tValue = it.next();
                jobDataMap.put(tKey, tValue);
            }
        }
        jobParams.put("jobDataMap", jobDataMap);
    }

}
{'fraction_non_alphanumeric': 0.1082897101336042, 'fraction_numerical': 0.00039065551996249706, 'mean_word_length': 2.891760413499544, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3867394', 'n_tokens_mistral': 3380, 'n_tokens_neox': 3298, 'n_words': 809}