<commit_before>/**
 *  Copyright (C) 2009 Progress Software, Inc. All rights reserved.
 *  http://fusesource.com
 *
 *  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.fusesource.meshkeeper.distribution;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.fusesource.meshkeeper.MeshKeeper;
import org.fusesource.meshkeeper.MeshKeeperFactory;
import org.fusesource.meshkeeper.RegistryWatcher;
import org.fusesource.meshkeeper.control.ControlServer;
import org.fusesource.meshkeeper.distribution.event.EventClient;
import org.fusesource.meshkeeper.distribution.event.EventClientFactory;
import org.fusesource.meshkeeper.distribution.registry.RegistryClient;
import org.fusesource.meshkeeper.distribution.registry.RegistryFactory;
import org.fusesource.meshkeeper.distribution.remoting.RemotingClient;
import org.fusesource.meshkeeper.distribution.remoting.RemotingFactory;
import org.fusesource.meshkeeper.distribution.repository.RepositoryClient;
import org.fusesource.meshkeeper.distribution.repository.RepositoryProviderFactory;

/**
 * Distributor
 * <p>
 * Description:
 * </p>
 * 
 * @author cmacnaug
 * @version 1.0
 */
class DefaultDistributor implements MeshKeeper {

    private Log log = LogFactory.getLog(this.getClass());

    private String registryUri;
    private String remotingUri;
    private String eventingUri;
    private String repositoryUri;
    private String workingDirectory;
    private String localRepositoryDirectory;
    
    private RemotingWrapper remoting;
    private RegistryClient registry;
    private EventClient eventing;
    private RepositoryClient repository;
    private LaunchClient launchClient;
    private ClassLoader userClassLoader;
    private UserFirstClassLoader pluginUserClassLoader;

    private boolean uuidCreator = true;
    private String uuid;

    private final HashMap<Object, DistributionRef<?>> distributed = new HashMap<Object, DistributionRef<?>>();

    private AtomicBoolean started = new AtomicBoolean(false);
    private AtomicBoolean destroyed = new AtomicBoolean(false);

    DefaultDistributor() {
        uuid = System.getProperty(MeshKeeperFactory.MESHKEEPER_UUID_PROPERTY);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.fusesource.meshkeeper.MeshKeeper#getExecutorService()
     */
    public ScheduledExecutorService getExecutorService() {
        return DistributorFactory.getExecutorService();
    }

    public void setUserClassLoader(ClassLoader classLoader) {
        if (userClassLoader != classLoader) {
            userClassLoader = classLoader;
            if (userClassLoader == null) {
                pluginUserClassLoader = null;
            } else {
                pluginUserClassLoader = new UserFirstClassLoader(userClassLoader);
            }

            if (registry != null) {
                registry.setUserClassLoader(pluginUserClassLoader);
            }
            if (remoting != null) {
                remoting.setUserClassLoader(pluginUserClassLoader);
            }
            if (eventing != null) {
                eventing.setUserClassLoader(pluginUserClassLoader);
            }
            if (launchClient != null) {
                launchClient.setUserClassLoader(pluginUserClassLoader);
            }
        }

    }

    public ClassLoader getUserClassLoader() {
        return userClassLoader;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.fusesource.meshkeeper.Distributor#getDistributorUri()
     */
    public String getRegistryConnectUri() {
        return registryUri;
    }

    private synchronized <T extends PluginClient> T createPluginClient(String uri, AbstractPluginFactory<T> factory, String lookupPath, String defaultUri) throws PluginCreationException {
        if (destroyed.get()) {
            throw new IllegalStateException("destroyed");
        }
        try {
            //Create Remoting client:
            if (uri == null) {
                if (lookupPath != null) {
                    uri = registry().getRegistryObject(lookupPath);
                }
                if (uri == null) {
                    uri = defaultUri;
                }
            }
            T ret = factory.create(uri);
            ret.setMeshKeeper(this);
            if (userClassLoader != null) {
                ret.setUserClassLoader(pluginUserClassLoader);
            }
            ret.start();
            return ret;
        } catch (Exception e) {
            PluginCreationException re = new PluginCreationException("Unable to create plugin client from " + factory.getClass().getSimpleName(), e);
            log.error(re.getMessage(), re);
            throw re;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.fusesource.meshkeeper.MeshKeeper#getRegistry()
     */
    public synchronized Registry registry() throws PluginCreationException {
        if (registry == null) {
            synchronized (this) {
                if (registry == null) {
                    registry = new RegistryWrapper(createPluginClient(registryUri, new RegistryFactory(), null, ControlServer.DEFAULT_REGISTRY_URI));
                }
            }
        }
        return registry;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.fusesource.meshkeeper.MeshKeeper#getEventing()
     */
    public Eventing eventing() throws PluginCreationException {
        if (eventing == null) {
            synchronized (this) {
                if (eventing == null) {
                    eventing = createPluginClient(eventingUri, new EventClientFactory(), ControlServer.EVENTING_URI_PATH, ControlServer.DEFAULT_EVENT_URI);
                }
            }
        }
        return eventing;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.fusesource.meshkeeper.MeshKeeper#getRemoting()
     */
    public Remoting remoting() throws PluginCreationException {
        if (remoting == null) {
            synchronized (this) {
                if (remoting == null) {
                    if (log.isDebugEnabled()) {
                        log.debug("Creating Remoting Client");
                    }
                    remoting = new RemotingWrapper(createPluginClient(remotingUri, new RemotingFactory(), ControlServer.REMOTING_URI_PATH, ControlServer.DEFAULT_REMOTING_URI));

                }
            }
        }
        return remoting;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.fusesource.meshkeeper.MeshKeeper#getRepository()
     */
    public Repository repository() {
        if (repository == null) {
            if (destroyed.get()) {
                throw new IllegalStateException("destroyed");
            }
            synchronized (this) {
                if(repository == null)
                {
                    try
                    {
                        repository = createPluginClient(repositoryUri,  new RepositoryProviderFactory(), ControlServer.REPOSITORY_URI_PATH, ControlServer.DEFAULT_REPOSITORY_URI);
                        if(localRepositoryDirectory == null) {
                            localRepositoryDirectory = workingDirectory + File.separator + "local-repo";
                        }
                        repository.setLocalRepoDir(localRepositoryDirectory);
                    }
                    catch (Exception e) {
                        RuntimeException re = new RuntimeException("Error creating repository client", e);
                        log.error(re);
                        throw re;
                    }
                }
            }
        }

        return repository;
    }

    public Launcher launcher() {
        if (launchClient == null) {
            if (destroyed.get()) {
                throw new IllegalStateException("destroyed");
            }
            synchronized (this) {
                if (launchClient == null) {
                    launchClient = new LaunchClient();
                    launchClient.setMeshKeeper(this);
                    try {
                        launchClient.start();
                    } catch (Exception e) {
                        log.warn("Error starting launch client", e);
                        throw new PluginCreationException("Error starting launch client", e);
                    }

                    if (userClassLoader != null) {
                        launchClient.setUserClassLoader(pluginUserClassLoader);
                    }
                }
            }
        }
        return launchClient;
    }

    public synchronized void start() throws Exception {
        if (destroyed.get()) {
            throw new IllegalStateException("Can't start destoyed MeshKeeper");
        }
<commit_msg>Reducing log verbosity of plugin creation errors (which are already thrown to application). <commit_after>/**
 *  Copyright (C) 2009 Progress Software, Inc. All rights reserved.
 *  http://fusesource.com
 *
 *  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.fusesource.meshkeeper.distribution;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.fusesource.meshkeeper.MeshKeeper;
import org.fusesource.meshkeeper.MeshKeeperFactory;
import org.fusesource.meshkeeper.RegistryWatcher;
import org.fusesource.meshkeeper.control.ControlServer;
import org.fusesource.meshkeeper.distribution.event.EventClient;
import org.fusesource.meshkeeper.distribution.event.EventClientFactory;
import org.fusesource.meshkeeper.distribution.registry.RegistryClient;
import org.fusesource.meshkeeper.distribution.registry.RegistryFactory;
import org.fusesource.meshkeeper.distribution.remoting.RemotingClient;
import org.fusesource.meshkeeper.distribution.remoting.RemotingFactory;
import org.fusesource.meshkeeper.distribution.repository.RepositoryClient;
import org.fusesource.meshkeeper.distribution.repository.RepositoryProviderFactory;

/**
 * Distributor
 * <p>
 * Description:
 * </p>
 * 
 * @author cmacnaug
 * @version 1.0
 */
class DefaultDistributor implements MeshKeeper {

    private Log log = LogFactory.getLog(this.getClass());

    private String registryUri;
    private String remotingUri;
    private String eventingUri;
    private String repositoryUri;
    private String workingDirectory;
    private String localRepositoryDirectory;
    
    private RemotingWrapper remoting;
    private RegistryClient registry;
    private EventClient eventing;
    private RepositoryClient repository;
    private LaunchClient launchClient;
    private ClassLoader userClassLoader;
    private UserFirstClassLoader pluginUserClassLoader;

    private boolean uuidCreator = true;
    private String uuid;

    private final HashMap<Object, DistributionRef<?>> distributed = new HashMap<Object, DistributionRef<?>>();

    private AtomicBoolean started = new AtomicBoolean(false);
    private AtomicBoolean destroyed = new AtomicBoolean(false);

    DefaultDistributor() {
        uuid = System.getProperty(MeshKeeperFactory.MESHKEEPER_UUID_PROPERTY);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.fusesource.meshkeeper.MeshKeeper#getExecutorService()
     */
    public ScheduledExecutorService getExecutorService() {
        return DistributorFactory.getExecutorService();
    }

    public void setUserClassLoader(ClassLoader classLoader) {
        if (userClassLoader != classLoader) {
            userClassLoader = classLoader;
            if (userClassLoader == null) {
                pluginUserClassLoader = null;
            } else {
                pluginUserClassLoader = new UserFirstClassLoader(userClassLoader);
            }

            if (registry != null) {
                registry.setUserClassLoader(pluginUserClassLoader);
            }
            if (remoting != null) {
                remoting.setUserClassLoader(pluginUserClassLoader);
            }
            if (eventing != null) {
                eventing.setUserClassLoader(pluginUserClassLoader);
            }
            if (launchClient != null) {
                launchClient.setUserClassLoader(pluginUserClassLoader);
            }
        }

    }

    public ClassLoader getUserClassLoader() {
        return userClassLoader;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.fusesource.meshkeeper.Distributor#getDistributorUri()
     */
    public String getRegistryConnectUri() {
        return registryUri;
    }

    private synchronized <T extends PluginClient> T createPluginClient(String uri, AbstractPluginFactory<T> factory, String lookupPath, String defaultUri) throws PluginCreationException {
        if (destroyed.get()) {
            throw new IllegalStateException("destroyed");
        }
        try {
            //Create Remoting client:
            if (uri == null) {
                if (lookupPath != null) {
                    uri = registry().getRegistryObject(lookupPath);
                }
                if (uri == null) {
                    uri = defaultUri;
                }
            }
            T ret = factory.create(uri);
            ret.setMeshKeeper(this);
            if (userClassLoader != null) {
                ret.setUserClassLoader(pluginUserClassLoader);
            }
            ret.start();
            return ret;
        } catch (Exception e) {
            PluginCreationException re = new PluginCreationException("Unable to create plugin client from " + factory.getClass().getSimpleName(), e);
            if(log.isDebugEnabled()) {
                log.debug(re.getMessage(), re);
            }
            throw re;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.fusesource.meshkeeper.MeshKeeper#getRegistry()
     */
    public synchronized Registry registry() throws PluginCreationException {
        if (registry == null) {
            synchronized (this) {
                if (registry == null) {
                    registry = new RegistryWrapper(createPluginClient(registryUri, new RegistryFactory(), null, ControlServer.DEFAULT_REGISTRY_URI));
                }
            }
        }
        return registry;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.fusesource.meshkeeper.MeshKeeper#getEventing()
     */
    public Eventing eventing() throws PluginCreationException {
        if (eventing == null) {
            synchronized (this) {
                if (eventing == null) {
                    eventing = createPluginClient(eventingUri, new EventClientFactory(), ControlServer.EVENTING_URI_PATH, ControlServer.DEFAULT_EVENT_URI);
                }
            }
        }
        return eventing;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.fusesource.meshkeeper.MeshKeeper#getRemoting()
     */
    public Remoting remoting() throws PluginCreationException {
        if (remoting == null) {
            synchronized (this) {
                if (remoting == null) {
                    if (log.isDebugEnabled()) {
                        log.debug("Creating Remoting Client");
                    }
                    remoting = new RemotingWrapper(createPluginClient(remotingUri, new RemotingFactory(), ControlServer.REMOTING_URI_PATH, ControlServer.DEFAULT_REMOTING_URI));

                }
            }
        }
        return remoting;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.fusesource.meshkeeper.MeshKeeper#getRepository()
     */
    public Repository repository() {
        if (repository == null) {
            if (destroyed.get()) {
                throw new IllegalStateException("destroyed");
            }
            synchronized (this) {
                if(repository == null)
                {
                    try
                    {
                        repository = createPluginClient(repositoryUri,  new RepositoryProviderFactory(), ControlServer.REPOSITORY_URI_PATH, ControlServer.DEFAULT_REPOSITORY_URI);
                        if(localRepositoryDirectory == null) {
                            localRepositoryDirectory = workingDirectory + File.separator + "local-repo";
                        }
                        repository.setLocalRepoDir(localRepositoryDirectory);
                    }
                    catch (Exception e) {
                        RuntimeException re = new RuntimeException("Error creating repository client", e);
                        if(log.isDebugEnabled()) {
                            log.debug(re);
                        }
                        throw re;
                    }
                }
            }
        }

        return repository;
    }

    public Launcher launcher() {
        if (launchClient == null) {
            if (destroyed.get()) {
                throw new IllegalStateException("destroyed");
            }
            synchronized (this) {
                if (launchClient == null) {
                    launchClient = new LaunchClient();
                    launchClient.setMeshKeeper(this);
                    try {
                        launchClient.start();
                    } catch (Exception e) {
                        if(log.isDebugEnabled()) {
                            log.debug("Error starting launch client", e);
                        }
                        throw new PluginCreationException("Error starting launch client", e);
                    }

                    if (userClassLoader != null) {
                        launchClient.setUserClassLoader(pluginUserClassLoader);
                    }
                }
            }
        }
        return launchClient;
    }

    public synchronized void start() throws Exception {
        if (destroyed.get()) {
            throw new IllegalStateException("Can't start destoyed MeshKeeper");
        }

{'fraction_non_alphanumeric': 0.08615826452638597, 'fraction_numerical': 0.0010256936253141187, 'mean_word_length': 2.0645921735030646, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1378424', 'n_tokens_mistral': 5011, 'n_tokens_neox': 4752, 'n_words': 1082}