<commit_before>package de.joerghoh.cq5.healthcheck.providers;

import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.joerghoh.cq5.healthcheck.HealthStatusProvider;

@Component(immediate=true)
public class ReplicationAgentStatusFactory {
    
    private Logger log = LoggerFactory.getLogger(ReplicationAgentStatusFactory.class);
    private BundleContext bundleContext;
    private List<ServiceRegistration> registeredServices = new ArrayList<ServiceRegistration>(5);
    private MBeanServer server;

    private static final String queryString = "com.adobe.granite.replication:type=agent,id=*";
    
    @Activate
    protected void activate (ComponentContext ctx) {
        
    	bundleContext = ctx.getBundleContext();
        server = ManagementFactory.getPlatformMBeanServer();
        ObjectName query;
        try {
            query = new ObjectName (queryString);
            Set<ObjectName> agents = server.queryNames(query, null);
            log.error ("Found " + agents.size() + " agents");
            
            for (ObjectName agent: agents) {
                log.info("registering service for agent " + agent.toString());
                
                String agentName = getAgentName (agent);
                
                Dictionary<String,String> params = new Hashtable<String,String>();
                params.put(Constants.SERVICE_PID, agentName);
                params.put(Constants.SERVICE_DESCRIPTION, "replication agent : " + agentName);
                
                ReplicationAgentStatusProvider rasp = new ReplicationAgentStatusProvider (server, agent);
                
                ServiceRegistration sr = registerService (rasp,params);
                registeredServices.add(sr);
                
            }
            
        } catch (MalformedObjectNameException e) {
            // TODO Auto-generated catch block<commit_msg>fixed PID of ReplicationAgentStatusProvider<commit_after>package de.joerghoh.cq5.healthcheck.providers;

import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.joerghoh.cq5.healthcheck.HealthStatusProvider;

/**
 * Check the available replication agents via JMX and create a ReplicationAgentStatusProvider for each agent.
 * @author joerg
 *
 * TODO: dynamic detection of new/removed agents
 */
@Component(immediate=true)
public class ReplicationAgentStatusFactory {
    
    private Logger log = LoggerFactory.getLogger(ReplicationAgentStatusFactory.class);
    private BundleContext bundleContext;
    private List<ServiceRegistration> registeredServices = new ArrayList<ServiceRegistration>(5);
    private MBeanServer server;

    private static final String queryString = "com.adobe.granite.replication:type=agent,id=*";
    
    @Activate
    protected void activate (ComponentContext ctx) {
        
    	bundleContext = ctx.getBundleContext();
        server = ManagementFactory.getPlatformMBeanServer();
        ObjectName query;
        try {
            query = new ObjectName (queryString);
            Set<ObjectName> agents = server.queryNames(query, null);
            log.error ("Found " + agents.size() + " agents");
            
            for (ObjectName agent: agents) {
                log.info("registering service for agent " + agent.toString());
                
                String agentName = getAgentName (agent);
                
                Dictionary<String,String> params = new Hashtable<String,String>();
                params.put(Constants.SERVICE_PID, this.getClass().getName() + "." + agentName);
                params.put(Constants.SERVICE_DESCRIPTION, "StatusProvider for replication agent " + agentName);
                
                ReplicationAgentStatusProvider rasp = new ReplicationAgentStatusProvider (server, agent);
                
                ServiceRegistration sr = registerService (rasp,params);
                registeredServices.add(sr);
                
            }
            
        } catch (MalformedObjectNameException e) {
            // TODO Auto-generated catch block
{'fraction_non_alphanumeric': 0.08604977216964599, 'fraction_numerical': 0.0017525411847178409, 'mean_word_length': 3.2056005895357407, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '684453', 'n_tokens_mistral': 1445, 'n_tokens_neox': 1425, 'n_words': 305}