All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.signalfx.shaded.jetty.io.IncludeExcludeConnectionStatistics Maven / Gradle / Ivy

The newest version!
//
//  ========================================================================
//  Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package com.signalfx.shaded.jetty.io;

import java.util.AbstractSet;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.function.Predicate;

import com.signalfx.shaded.jetty.util.IncludeExcludeSet;

public class IncludeExcludeConnectionStatistics extends ConnectionStatistics
{
    private final IncludeExcludeSet, Connection> _set = new IncludeExcludeSet<>(ConnectionSet.class);

    public void include(String className) throws ClassNotFoundException
    {
        _set.include(connectionForName(className));
    }

    public void include(Class clazz)
    {
        _set.include(clazz);
    }

    public void exclude(String className) throws ClassNotFoundException
    {
        _set.exclude(connectionForName(className));
    }

    public void exclude(Class clazz)
    {
        _set.exclude(clazz);
    }

    private Class connectionForName(String className) throws ClassNotFoundException
    {
        Class aClass = Class.forName(className);
        if (!Connection.class.isAssignableFrom(aClass))
            throw new IllegalArgumentException("Class is not a Connection");

        @SuppressWarnings("unchecked")
        Class connectionClass = (Class)aClass;
        return connectionClass;
    }

    @Override
    public void onOpened(Connection connection)
    {
        if (_set.test(connection))
            super.onOpened(connection);
    }

    @Override
    public void onClosed(Connection connection)
    {
        if (_set.test(connection))
            super.onClosed(connection);
    }

    public static class ConnectionSet extends AbstractSet> implements Predicate
    {
        private final Set> set = new HashSet<>();

        @Override
        public boolean add(Class aClass)
        {
            return set.add(aClass);
        }

        @Override
        public boolean remove(Object o)
        {
            return set.remove(o);
        }

        @Override
        public Iterator> iterator()
        {
            return set.iterator();
        }

        @Override
        public int size()
        {
            return set.size();
        }

        @Override
        public boolean test(Connection connection)
        {
            if (connection == null)
                return false;
            return set.stream().anyMatch(c -> c.isAssignableFrom(connection.getClass()));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy