<commit_before>/*
 * JBoss, Home of Professional Open Source
 * Copyright 2008, Red Hat, Inc., and others contributors as indicated
 * by the @authors tag. All rights reserved.
 * See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License, v. 2.1.
 * This program is distributed in the hope that it will be useful, but WITHOUT A
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License,
 * v.2.1 along with this distribution; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 */
package org.jboss.narayana.blacktie.administration;

import static org.jboss.narayana.blacktie.administration.Authentication.getCallbackHandler;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Hashtable;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.management.MBeanServerConnection;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.client.OperationBuilder;
import org.jboss.dmr.ModelNode;
import org.jboss.narayana.blacktie.administration.core.AdministrationProxy;
import org.jboss.narayana.blacktie.jatmibroker.core.conf.ConfigurationException;
import org.jboss.narayana.blacktie.jatmibroker.core.conf.XMLParser;
import org.jboss.narayana.blacktie.jatmibroker.xatmi.Connection;
import org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException;
import org.jboss.narayana.blacktie.jatmibroker.xatmi.Response;
import org.jboss.narayana.blacktie.jatmibroker.xatmi.TPSVCINFO;
import org.jboss.narayana.blacktie.jatmibroker.xatmi.X_OCTET;
import org.jboss.narayana.blacktie.jatmibroker.xatmi.mdb.MDBBlacktieService;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

@MessageDriven(activationConfig = {
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/BTR_BTStompAdmin") })
public class BlacktieStompAdministrationService extends MDBBlacktieService implements javax.jms.MessageListener {
    private static final Logger log = LogManager.getLogger(BlacktieStompAdministrationService.class);

    private static MBeanServerConnection beanServerConnection;
    private static Properties prop = new Properties();

    private static Hashtable<String, Long> QUEUE_CREATION_TIMES = new Hashtable<String, Long>();
    private static Hashtable<String, ServerInfo> SERVICE_OWNERS = new Hashtable<String, ServerInfo>();

    private static ModelControllerClient client;

    private class ServerInfo {
	public final String name;
        public final boolean conversational;
        public final String type;
        public ServerInfo(String name, boolean conversational, String type) {
           this.name = name;
           this.conversational = conversational;
           this.type = type; 
        } 
    }

    public BlacktieStompAdministrationService() throws ConfigurationException {
        super("BlacktieStompAdministrationService");
    }

    static void applyUpdate(ModelNode update, final ModelControllerClient client) throws IOException {
        ModelNode result = client.execute(new OperationBuilder(update).build());
        if (result.hasDefined("outcome") && "success".equals(result.get("outcome").asString())) {
            if (result.hasDefined("result")) {
                System.out.println(result.get("result"));
            }
        } else if (result.hasDefined("failure-description")) {
            throw new RuntimeException(result.get("failure-description").toString());
        } else {
            throw new RuntimeException("Operation not successful; outcome = " + result.get("outcome"));
        }
    }

    private static boolean isDeployQueue(String serviceName) throws Exception {

        boolean conversational = false;
        String type = "queue";
        if (!serviceName.startsWith(".")) {
            ServerInfo info = SERVICE_OWNERS.get(serviceName);
	    if(info != null)
            {
               conversational = info.conversational;
               type = info.type;
            }
            else
            {
               return false;
            }
        }
        String prefix = null;
        if (conversational) {
            prefix = "BTC_";
        } else {
            prefix = "BTR_";
        }
        ObjectName objName = new ObjectName("jboss.as:subsystem=messaging,hornetq-server=default,jms-" + type + "=" + prefix
                + "*");
        ObjectInstance[] dests = getMBeanServerConnection().queryMBeans(objName, null).toArray(new ObjectInstance[] {});
        for (int i = 0; i < dests.length; i++) {
            String serviceComponentOfObjectName = dests[i].getObjectName().getCanonicalName();
            serviceComponentOfObjectName = serviceComponentOfObjectName.substring(
                    serviceComponentOfObjectName.indexOf('_') + 1,
                    serviceComponentOfObjectName.indexOf(",", serviceComponentOfObjectName.indexOf('_')));
            log.debug("Service name component of ObjectName is: " + serviceComponentOfObjectName);
            if (serviceComponentOfObjectName.equals(serviceName)) {
                log.debug("find serviceName " + serviceName + " in Queues");
                return true;
            }
        }

        log.trace("did not find serviceName " + serviceName);
        return false;
    }

    private static MBeanServerConnection getMBeanServerConnection() throws ConfigurationException, UnknownHostException {
        initStatic();
        return beanServerConnection;
    }

    private static Object getProperty(String string) throws ConfigurationException, UnknownHostException {
        initStatic();
        return prop.get(string);
    }

    private static ModelControllerClient getClient() throws ConfigurationException, UnknownHostException {
        initStatic();
        return client;
    }

    private static void initStatic() throws ConfigurationException, UnknownHostException {
        synchronized (prop) {
            if (prop.isEmpty()) {
                XMLParser.loadProperties("btconfig.xsd", "btconfig.xml", prop);
                beanServerConnection = java.lang.management.ManagementFactory.getPlatformMBeanServer();
                String managementAddress = System.getProperty("jboss.bind.address.management", "127.0.0.1");
                if (managementAddress.equals("0.0.0.0")) {
                    managementAddress = "127.0.0.1";
                }
                client = ModelControllerClient.Factory.create("remote", InetAddress.getByName(managementAddress), 9999,
                        getCallbackHandler());
            }
        }
    }

    int consumerCount(String serviceName) throws Exception {
        log.trace("consCount" + serviceName);
        boolean conversational = false;
        String type = "queue";

        if (!serviceName.startsWith(".")) {<commit_msg>Revert "JBTM-2073 update localhost to 127.0.0.1"<commit_after>/*
 * JBoss, Home of Professional Open Source
 * Copyright 2008, Red Hat, Inc., and others contributors as indicated
 * by the @authors tag. All rights reserved.
 * See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License, v. 2.1.
 * This program is distributed in the hope that it will be useful, but WITHOUT A
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License,
 * v.2.1 along with this distribution; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 */
package org.jboss.narayana.blacktie.administration;

import static org.jboss.narayana.blacktie.administration.Authentication.getCallbackHandler;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Hashtable;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.management.MBeanServerConnection;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.client.OperationBuilder;
import org.jboss.dmr.ModelNode;
import org.jboss.narayana.blacktie.administration.core.AdministrationProxy;
import org.jboss.narayana.blacktie.jatmibroker.core.conf.ConfigurationException;
import org.jboss.narayana.blacktie.jatmibroker.core.conf.XMLParser;
import org.jboss.narayana.blacktie.jatmibroker.xatmi.Connection;
import org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException;
import org.jboss.narayana.blacktie.jatmibroker.xatmi.Response;
import org.jboss.narayana.blacktie.jatmibroker.xatmi.TPSVCINFO;
import org.jboss.narayana.blacktie.jatmibroker.xatmi.X_OCTET;
import org.jboss.narayana.blacktie.jatmibroker.xatmi.mdb.MDBBlacktieService;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

@MessageDriven(activationConfig = {
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/BTR_BTStompAdmin") })
public class BlacktieStompAdministrationService extends MDBBlacktieService implements javax.jms.MessageListener {
    private static final Logger log = LogManager.getLogger(BlacktieStompAdministrationService.class);

    private static MBeanServerConnection beanServerConnection;
    private static Properties prop = new Properties();

    private static Hashtable<String, Long> QUEUE_CREATION_TIMES = new Hashtable<String, Long>();
    private static Hashtable<String, ServerInfo> SERVICE_OWNERS = new Hashtable<String, ServerInfo>();

    private static ModelControllerClient client;

    private class ServerInfo {
	public final String name;
        public final boolean conversational;
        public final String type;
        public ServerInfo(String name, boolean conversational, String type) {
           this.name = name;
           this.conversational = conversational;
           this.type = type; 
        } 
    }

    public BlacktieStompAdministrationService() throws ConfigurationException {
        super("BlacktieStompAdministrationService");
    }

    static void applyUpdate(ModelNode update, final ModelControllerClient client) throws IOException {
        ModelNode result = client.execute(new OperationBuilder(update).build());
        if (result.hasDefined("outcome") && "success".equals(result.get("outcome").asString())) {
            if (result.hasDefined("result")) {
                System.out.println(result.get("result"));
            }
        } else if (result.hasDefined("failure-description")) {
            throw new RuntimeException(result.get("failure-description").toString());
        } else {
            throw new RuntimeException("Operation not successful; outcome = " + result.get("outcome"));
        }
    }

    private static boolean isDeployQueue(String serviceName) throws Exception {

        boolean conversational = false;
        String type = "queue";
        if (!serviceName.startsWith(".")) {
            ServerInfo info = SERVICE_OWNERS.get(serviceName);
	    if(info != null)
            {
               conversational = info.conversational;
               type = info.type;
            }
            else
            {
               return false;
            }
        }
        String prefix = null;
        if (conversational) {
            prefix = "BTC_";
        } else {
            prefix = "BTR_";
        }
        ObjectName objName = new ObjectName("jboss.as:subsystem=messaging,hornetq-server=default,jms-" + type + "=" + prefix
                + "*");
        ObjectInstance[] dests = getMBeanServerConnection().queryMBeans(objName, null).toArray(new ObjectInstance[] {});
        for (int i = 0; i < dests.length; i++) {
            String serviceComponentOfObjectName = dests[i].getObjectName().getCanonicalName();
            serviceComponentOfObjectName = serviceComponentOfObjectName.substring(
                    serviceComponentOfObjectName.indexOf('_') + 1,
                    serviceComponentOfObjectName.indexOf(",", serviceComponentOfObjectName.indexOf('_')));
            log.debug("Service name component of ObjectName is: " + serviceComponentOfObjectName);
            if (serviceComponentOfObjectName.equals(serviceName)) {
                log.debug("find serviceName " + serviceName + " in Queues");
                return true;
            }
        }

        log.trace("did not find serviceName " + serviceName);
        return false;
    }

    private static MBeanServerConnection getMBeanServerConnection() throws ConfigurationException, UnknownHostException {
        initStatic();
        return beanServerConnection;
    }

    private static Object getProperty(String string) throws ConfigurationException, UnknownHostException {
        initStatic();
        return prop.get(string);
    }

    private static ModelControllerClient getClient() throws ConfigurationException, UnknownHostException {
        initStatic();
        return client;
    }

    private static void initStatic() throws ConfigurationException, UnknownHostException {
        synchronized (prop) {
            if (prop.isEmpty()) {
                XMLParser.loadProperties("btconfig.xsd", "btconfig.xml", prop);
                beanServerConnection = java.lang.management.ManagementFactory.getPlatformMBeanServer();
                String managementAddress = System.getProperty("jboss.bind.address.management", "localhost");
                if (managementAddress.equals("0.0.0.0")) {
                    managementAddress = "localhost";
                }
                client = ModelControllerClient.Factory.create("remote", InetAddress.getByName(managementAddress), 9999,
                        getCallbackHandler());
            }
        }
    }

    int consumerCount(String serviceName) throws Exception {
        log.trace("consCount" + serviceName);
        boolean conversational = false;
        String type = "queue";

        if (!serviceName.startsWith(".")) {
{'fraction_non_alphanumeric': 0.0935672514619883, 'fraction_numerical': 0.00554016620498615, 'mean_word_length': 3.3485010706638114, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 20}, 'pii_count': 5, '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': '3027414', 'n_tokens_mistral': 4453, 'n_tokens_neox': 4250, 'n_words': 1021}