<commit_before>/*
 * Copyright (c) 2010 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.cometd.server;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.cometd.bayeux.Message;
import org.cometd.bayeux.Session;
import org.cometd.bayeux.server.BayeuxServer;
import org.cometd.bayeux.server.LocalSession;
import org.cometd.bayeux.server.ServerChannel;
import org.cometd.bayeux.server.ServerMessage;
import org.cometd.bayeux.server.ServerMessage.Mutable;
import org.cometd.bayeux.server.ServerSession;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.util.thread.ThreadPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>{@link AbstractService} provides convenience methods to assist with the
 * creation of a CometD services.</p>
 * <p>A CometD service runs application code whenever a message is received on
 * a particular channel.</p>
 * <p>Specifically it provides:</p>
 * <ul>
 * <li>Mapping of channel subscriptions to method invocation on the derived
 * service class.</li>
 * <li>Optional use of a thread pool used for method invocation if handling
 * can take considerable time and it is desired not to hold up the delivering
 * thread (typically a HTTP request handling thread).</li>
 * <li>The objects returned from method invocation are delivered back to the
 * calling client in a private message.</li>
 * </ul>
 * <p>Subclasses should call {@link #addService(String, String)} in order to
 * map channel subscriptions to method invocations, usually in the subclass
 * constructor.</p>
 * <p>Each CometD service has an associated {@link LocalSession} that can be
 * used as the source for messages published via
 * {@link ServerChannel#publish(Session, Mutable)} or
 * {@link ServerSession#deliver(Session, Mutable)}.</p>
 *
 * @see {@link BayeuxServer#newLocalSession(String)} as an alternative to {@link AbstractService}.
 */
public abstract class AbstractService
{
    protected final Logger _logger = LoggerFactory.getLogger(getClass());
    private final Map<String, Invoker> invokers = new ConcurrentHashMap<String, Invoker>();
    private final String _name;
    private final BayeuxServerImpl _bayeux;
    private final LocalSession _session;
    private ThreadPool _threadPool;
    private boolean _seeOwn = false;

    /**
     * <p>Instantiates a CometD service with the given name.</p>
     *
     * @param bayeux The BayeuxServer instance.
     * @param name   The name of the service (used as client ID prefix).
     */
    public AbstractService(BayeuxServer bayeux, String name)
    {
        this(bayeux, name, 0);
    }

    /**
     * <p>Instantiate a CometD service with the given name and max number of pooled threads.</p>
     *
     * @param bayeux     The BayeuxServer instance.
     * @param name       The name of the service (used as client ID prefix).
     * @param maxThreads The max size of a ThreadPool to create to handle messages.
     */
    public AbstractService(BayeuxServer bayeux, String name, int maxThreads)
    {
        _name = name;
        _bayeux = (BayeuxServerImpl)bayeux;
        _session = _bayeux.newLocalSession(name);
        _session.handshake();
        if (maxThreads > 0)
            setThreadPool(new QueuedThreadPool(maxThreads));
    }

    public BayeuxServer getBayeux()
    {
        return _bayeux;
    }

    /**
     * @return The {@link LocalSession} associated with this CometD service
     */
    public LocalSession getLocalSession()
    {
        return _session;
    }

    /**
     * @return The {@link ServerSession} of the {@link LocalSession} associated
     *         with this CometD service<commit_msg>Added getter for the name of the service.<commit_after>/*
 * Copyright (c) 2010 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.cometd.server;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.cometd.bayeux.Message;
import org.cometd.bayeux.Session;
import org.cometd.bayeux.server.BayeuxServer;
import org.cometd.bayeux.server.LocalSession;
import org.cometd.bayeux.server.ServerChannel;
import org.cometd.bayeux.server.ServerMessage;
import org.cometd.bayeux.server.ServerMessage.Mutable;
import org.cometd.bayeux.server.ServerSession;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.util.thread.ThreadPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>{@link AbstractService} provides convenience methods to assist with the
 * creation of a CometD services.</p>
 * <p>A CometD service runs application code whenever a message is received on
 * a particular channel.</p>
 * <p>Specifically it provides:</p>
 * <ul>
 * <li>Mapping of channel subscriptions to method invocation on the derived
 * service class.</li>
 * <li>Optional use of a thread pool used for method invocation if handling
 * can take considerable time and it is desired not to hold up the delivering
 * thread (typically a HTTP request handling thread).</li>
 * <li>The objects returned from method invocation are delivered back to the
 * calling client in a private message.</li>
 * </ul>
 * <p>Subclasses should call {@link #addService(String, String)} in order to
 * map channel subscriptions to method invocations, usually in the subclass
 * constructor.</p>
 * <p>Each CometD service has an associated {@link LocalSession} that can be
 * used as the source for messages published via
 * {@link ServerChannel#publish(Session, Mutable)} or
 * {@link ServerSession#deliver(Session, Mutable)}.</p>
 *
 * @see {@link BayeuxServer#newLocalSession(String)} as an alternative to {@link AbstractService}.
 */
public abstract class AbstractService
{
    protected final Logger _logger = LoggerFactory.getLogger(getClass());
    private final Map<String, Invoker> invokers = new ConcurrentHashMap<String, Invoker>();
    private final String _name;
    private final BayeuxServerImpl _bayeux;
    private final LocalSession _session;
    private ThreadPool _threadPool;
    private boolean _seeOwn = false;

    /**
     * <p>Instantiates a CometD service with the given name.</p>
     *
     * @param bayeux The BayeuxServer instance.
     * @param name   The name of the service (used as client ID prefix).
     */
    public AbstractService(BayeuxServer bayeux, String name)
    {
        this(bayeux, name, 0);
    }

    /**
     * <p>Instantiate a CometD service with the given name and max number of pooled threads.</p>
     *
     * @param bayeux     The BayeuxServer instance.
     * @param name       The name of the service (used as client ID prefix).
     * @param maxThreads The max size of a ThreadPool to create to handle messages.
     */
    public AbstractService(BayeuxServer bayeux, String name, int maxThreads)
    {
        _name = name;
        _bayeux = (BayeuxServerImpl)bayeux;
        _session = _bayeux.newLocalSession(name);
        _session.handshake();
        if (maxThreads > 0)
            setThreadPool(new QueuedThreadPool(maxThreads));
    }

    public BayeuxServer getBayeux()
    {
        return _bayeux;
    }

    public String getName()
    {
        return _name;
    }

    /**
     * @return The {@link LocalSession} associated with this CometD service
     */
    public LocalSession getLocalSession()
    {
        return _session;
    }

    /**
     * @return The {@link ServerSession} of the {@link LocalSession} associated
     *         with this CometD service
{'fraction_non_alphanumeric': 0.0891486192276842, 'fraction_numerical': 0.002750085940185631, 'mean_word_length': 3.572027239392352, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 53, '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': '4233968', 'n_tokens_mistral': 2507, 'n_tokens_neox': 2465, 'n_words': 883}