<commit_before>/**
 * Copyright (C) 2012 Ness Computing, Inc.
 *
 * 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 com.nesscomputing.jmx.starter.guice;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.ImmutableMap;
import com.google.inject.AbstractModule;
import com.google.inject.Scopes;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
import com.google.inject.throwingproviders.ThrowingProviderBinder;
import com.nesscomputing.config.Config;
import com.nesscomputing.jmx.starter.JmxExporter;
import com.nesscomputing.jmx.starter.JmxExporterConfig;
import com.nesscomputing.logging.Log;

public class JmxStarterModule extends AbstractModule
{
    public static final String JMX_STARTER_NAME = "_jmxStarter";
    public static final Named JMX_STARTER_NAMED = Names.named(JMX_STARTER_NAME);

    private static final Log LOG = Log.findLog();

    private final Config config;

    public JmxStarterModule(final Config config)
    {
        this.config = config;
    }

    @Override
    public void configure()
    {
        final JmxStarterConfig jmxStarterConfig = config.getBean(JmxStarterConfig.class);
        bind(JmxStarterConfig.class).toInstance(jmxStarterConfig);

        if (jmxStarterConfig.isEnabled()) {
            LOG.info("Exporting JMX...");

            final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();

            if (StringUtils.isNotBlank(jmxStarterConfig.getPasswordFile())) {
                builder.put("jmx.remote.x.password.file", jmxStarterConfig.getPasswordFile());
            }
            if (StringUtils.isNotBlank(jmxStarterConfig.getAccessFile())) {
                builder.put("jmx.remote.x.access.file", jmxStarterConfig.getAccessFile());
            }

            bind(new TypeLiteral<Map<String, String>>() {}).annotatedWith(JMX_STARTER_NAMED).toInstance(builder.build());

            bind(JmxExporterConfigProvider.class).in(Scopes.SINGLETON);
            ThrowingProviderBinder.create(binder())
                                  .bind(IOExceptionProvider.class, JmxExporterConfig.class)
                                  .to(JmxExporterConfigProvider.class)
                                  .in(Scopes.SINGLETON);

            bind(JmxExporter.class).asEagerSingleton();
        }
        else {
            LOG.info("Not exporting JMX.");
        }
    }
}
<commit_msg>Make the JmxStarterModule safe to instantiate multiply, since currently StandaloneServer and BasicJerseyServerModule fight over it<commit_after>/**
 * Copyright (C) 2012 Ness Computing, Inc.
 *
 * 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 com.nesscomputing.jmx.starter.guice;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.ImmutableMap;
import com.google.inject.AbstractModule;
import com.google.inject.Scopes;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
import com.google.inject.throwingproviders.ThrowingProviderBinder;
import com.nesscomputing.config.Config;
import com.nesscomputing.jmx.starter.JmxExporter;
import com.nesscomputing.jmx.starter.JmxExporterConfig;
import com.nesscomputing.logging.Log;

public class JmxStarterModule extends AbstractModule
{
    public static final String JMX_STARTER_NAME = "_jmxStarter";
    public static final Named JMX_STARTER_NAMED = Names.named(JMX_STARTER_NAME);

    private static final Log LOG = Log.findLog();

    private final Config config;

    public JmxStarterModule(final Config config)
    {
        this.config = config;
    }

    @Override
    public void configure()
    {
        final JmxStarterConfig jmxStarterConfig = config.getBean(JmxStarterConfig.class);
        bind(JmxStarterConfig.class).toInstance(jmxStarterConfig);

        if (jmxStarterConfig.isEnabled()) {
            LOG.info("Exporting JMX...");

            final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();

            if (StringUtils.isNotBlank(jmxStarterConfig.getPasswordFile())) {
                builder.put("jmx.remote.x.password.file", jmxStarterConfig.getPasswordFile());
            }
            if (StringUtils.isNotBlank(jmxStarterConfig.getAccessFile())) {
                builder.put("jmx.remote.x.access.file", jmxStarterConfig.getAccessFile());
            }

            bind(new TypeLiteral<Map<String, String>>() {}).annotatedWith(JMX_STARTER_NAMED).toInstance(builder.build());

            bind(JmxExporterConfigProvider.class).in(Scopes.SINGLETON);
            ThrowingProviderBinder.create(binder())
                                  .bind(IOExceptionProvider.class, JmxExporterConfig.class)
                                  .to(JmxExporterConfigProvider.class)
                                  .in(Scopes.SINGLETON);

            bind(JmxExporter.class).asEagerSingleton();
        }
        else {
            LOG.info("Not exporting JMX.");
        }
    }

    @Override
    public int hashCode()
    {
        return getClass().hashCode();
    }

    @Override
    public boolean equals(Object obj)
    {
        return obj != null && getClass().equals(obj.getClass());
    }
}

{'fraction_non_alphanumeric': 0.09767737830098631, 'fraction_numerical': 0.0028635062042634426, 'mean_word_length': 3.0745301360985096, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6037026', 'n_tokens_mistral': 1803, 'n_tokens_neox': 1754, 'n_words': 403}