<commit_before>/*
 * Copyright 2008 the original author or authors.
 *
 * 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.rioproject.impl.jmx;

import com.sun.tools.attach.VirtualMachine;
import org.rioproject.config.Constants;
import org.rioproject.net.HostUtil;
import org.rioproject.rmi.RegistryUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.*;
import java.io.File;
import java.lang.management.ManagementFactory;

/**
 * Provides JMX connection utilities.
 *
 * @author <NAME>
 */
@SuppressWarnings("PMD.AvoidThrowingRawExceptionTypes")
public class JMXConnectionUtil {
    static final Logger logger = LoggerFactory.getLogger(JMXConnectionUtil.class);

    /**
     * Create a {@link javax.management.remote.JMXConnectorServer}, bound to
     * the RMI Registry created by an infrastructure service (Cybernode or
     * Monitor).
     *
     * <p>If <tt>JMXConnectorServer</tt> has been created, return immediately.
     * If the <tt>JMXConnectorServer</tt> needs to be created, it will be
     * bound to the RMI Registry, and set the
     * <tt>org.rioproject.jmxServiceURL</tt> system property.
     *
     * <p>This utility uses the {@link org.rioproject.rmi.RegistryUtil} class to
     * obtain the port to access the RMI Registry.
     *
     * @throws Exception If there are errors reading the configuration, or
     * creating the {@link javax.management.remote.JMXConnectorServer}
     */
    public static void createJMXConnection() throws Exception {
        if(System.getProperty(Constants.JMX_SERVICE_URL)!=null)
            return;
        RegistryUtil.checkRegistry();
        String sPort = System.getProperty(Constants.REGISTRY_PORT, "0");
        int registryPort = Integer.parseInt(sPort);

        if(registryPort==0) {
            logger.error("RMI Registry property [{}] not found, unable to create MBeanServer", Constants.REGISTRY_PORT);
            throw new Exception("Unable to create the JMXConnectorServer");
        }

        MBeanServer mbs = MBeanServerFactory.getMBeanServer();
        String hostAddress = HostUtil.getHostAddressFromProperty(Constants.RMI_HOST_ADDRESS);
        JMXServiceURL jmxServiceURL = new JMXServiceURL("service:jmx:rmi://"+hostAddress+":"+registryPort+
                                                        "/jndi/rmi://"+hostAddress+":"+registryPort+"/jmxrmi");
        System.setProperty(Constants.JMX_SERVICE_URL, jmxServiceURL.toString());
        if(logger.isInfoEnabled())
            logger.info("JMXServiceURL={}", jmxServiceURL);
        JMXConnectorServer jmxConn = JMXConnectorServerFactory.newJMXConnectorServer(jmxServiceURL, null, mbs);
        jmxConn.start();
        if(logger.isDebugEnabled())
            logger.debug("JMX Platform MBeanServer exported with RMI Connector");

    }
<commit_msg>Check if jmxrmi is already bound<commit_after>/*
 * Copyright 2008 the original author or authors.
 *
 * 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.rioproject.impl.jmx;

import com.sun.tools.attach.VirtualMachine;
import org.rioproject.config.Constants;
import org.rioproject.net.HostUtil;
import org.rioproject.rmi.RegistryUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.*;
import java.io.File;
import java.lang.management.ManagementFactory;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

/**
 * Provides JMX connection utilities.
 *
 * @author <NAME>
 */
@SuppressWarnings("PMD.AvoidThrowingRawExceptionTypes")
public class JMXConnectionUtil {
    static final Logger logger = LoggerFactory.getLogger(JMXConnectionUtil.class);

    /**
     * Create a {@link javax.management.remote.JMXConnectorServer}, bound to
     * the RMI Registry created by an infrastructure service (Cybernode or
     * Monitor).
     *
     * <p>If <tt>JMXConnectorServer</tt> has been created, return immediately.
     * If the <tt>JMXConnectorServer</tt> needs to be created, it will be
     * bound to the RMI Registry, and set the
     * <tt>org.rioproject.jmxServiceURL</tt> system property.
     *
     * <p>This utility uses the {@link org.rioproject.rmi.RegistryUtil} class to
     * obtain the port to access the RMI Registry.
     *
     * @throws Exception If there are errors reading the configuration, or
     * creating the {@link javax.management.remote.JMXConnectorServer}
     */
    public static void createJMXConnection() throws Exception {
        if(System.getProperty(Constants.JMX_SERVICE_URL)!=null)
            return;
        RegistryUtil.checkRegistry();
        String sPort = System.getProperty(Constants.REGISTRY_PORT, "0");
        int registryPort = Integer.parseInt(sPort);

        if(registryPort==0) {
            logger.error("RMI Registry property [{}] not found, unable to create MBeanServer", Constants.REGISTRY_PORT);
            throw new Exception("Unable to create the JMXConnectorServer");
        }

        MBeanServer mbs = MBeanServerFactory.getMBeanServer();
        String hostAddress = HostUtil.getHostAddressFromProperty(Constants.RMI_HOST_ADDRESS);
        JMXServiceURL jmxServiceURL = new JMXServiceURL("service:jmx:rmi://"+hostAddress+":"+registryPort+
                                                        "/jndi/rmi://"+hostAddress+":"+registryPort+"/jmxrmi");
        System.setProperty(Constants.JMX_SERVICE_URL, jmxServiceURL.toString());
        if(logger.isInfoEnabled())
            logger.info("JMXServiceURL={}", jmxServiceURL);
        Registry registry = LocateRegistry.getRegistry(registryPort);
        for(String s : registry.list()) {
            if(s.equals("jmxrmi")) {
                registry.unbind(s);
            }
        }
        JMXConnectorServer jmxConn = JMXConnectorServerFactory.newJMXConnectorServer(jmxServiceURL, null, mbs);
        jmxConn.start();
        if(logger.isDebugEnabled())
            logger.debug("JMX Platform MBeanServer exported with RMI Connector");

    }

{'fraction_non_alphanumeric': 0.0972644376899696, 'fraction_numerical': 0.0033158331030671458, 'mean_word_length': 3.4465601965601964, 'pattern_counts': {'":': 4, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5834430', 'n_tokens_mistral': 2019, 'n_tokens_neox': 1994, 'n_words': 549}