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

com.oracle.coherence.cdi.server.FederationEventHandlers Maven / Gradle / Ivy

There is a newer version: 24.09
Show newest version
/*
 * Copyright (c) 2020, 2021 Oracle and/or its affiliates.
 *
 * Licensed under the Universal Permissive License v 1.0 as shown at
 * http://oss.oracle.com/licenses/upl.
 */
package com.oracle.coherence.cdi.server;

import com.oracle.coherence.cdi.events.EventObserverSupport;

import com.oracle.coherence.cdi.events.Backlog;
import com.oracle.coherence.cdi.events.Connecting;
import com.oracle.coherence.cdi.events.Disconnected;
import com.oracle.coherence.cdi.events.Error;
import com.oracle.coherence.cdi.events.CommittingLocal;
import com.oracle.coherence.cdi.events.ParticipantName;
import com.oracle.coherence.cdi.events.CommittingRemote;
import com.oracle.coherence.cdi.events.Replicating;
import com.oracle.coherence.cdi.events.Synced;
import com.oracle.coherence.cdi.events.Syncing;

import com.oracle.coherence.cdi.events.EventObserverSupport.EventObserver;

import com.tangosol.net.events.EventDispatcher;

import com.tangosol.net.events.federation.FederatedChangeEvent;
import com.tangosol.net.events.federation.FederatedConnectionEvent;
import com.tangosol.net.events.federation.FederatedPartitionEvent;

import java.lang.annotation.Annotation;

import java.util.Set;
import java.util.function.Function;

/**
 * Federation event handlers that allow CDI observers to handle any federation
 * event.
 *
 * @author Aleks Seovic  2020.04.13
 * @since 20.06
 */
@SuppressWarnings("unused")
public class FederationEventHandlers
    {
    // ---- inner class: FederationEventHandler -----------------------------

   /**
    * Abstract base class for all observer-based federation interceptors.
    *
    * @param   the type of {@link com.tangosol.net.events.Event} this interceptor accepts
    * @param   the enumeration of event types E supports
    */
   static abstract class FederationEventHandler, T extends Enum>
           extends EventObserverSupport.ServiceEventHandler
       {
       protected FederationEventHandler(EventObserver observer,
                                        Class classType,
                                        Function fnParticipantName)
           {
           super(observer, classType);

           m_fnParticipantName = fnParticipantName;

           String participantName = null;

           for (Annotation a : observer.getObservedQualifiers())
               {
               if (a instanceof ParticipantName)
                   {
                   participantName = ((ParticipantName) a).value();
                   }
               }

           m_participantName = participantName;
           }

       @SuppressWarnings("rawtypes")
       @Override
       protected boolean isApplicable(EventDispatcher dispatcher, String sScopeName)
           {
           Set setSupported = dispatcher.getSupportedTypes();
           boolean   fMatch       = m_setTypes.stream().anyMatch(setSupported::contains);
           return fMatch && super.isApplicable(dispatcher, sScopeName);
           }

       @Override
       protected boolean shouldFire(E event)
           {
           return m_participantName == null || m_participantName.equals(m_fnParticipantName.apply(event));
           }

       // ---- data members ----------------------------------------------------

       protected final String m_participantName;
       protected final Function m_fnParticipantName;
       }

    // ---- inner class: FederatedConnectionEventHandler --------------------

    /**
     * Handler for {@link FederatedConnectionEvent}s.
     */
    static class FederatedConnectionEventHandler
            extends FederationEventHandler
        {
        FederatedConnectionEventHandler(EventObserver observer)
            {
            super(observer, FederatedConnectionEvent.Type.class, FederatedConnectionEvent::getParticipantName);

            for (Annotation a : observer.getObservedQualifiers())
                {
                if (a instanceof Connecting)
                    {
                    addType(FederatedConnectionEvent.Type.CONNECTING);
                    }
                else if (a instanceof Disconnected)
                    {
                    addType(FederatedConnectionEvent.Type.DISCONNECTED);
                    }
                else if (a instanceof Backlog)
                    {
                    Backlog backlog = (Backlog) a;
                    if (backlog.value() == Backlog.Type.EXCESSIVE)
                        {
                        addType(FederatedConnectionEvent.Type.BACKLOG_EXCESSIVE);
                        }
                    else
                        {
                        addType(FederatedConnectionEvent.Type.BACKLOG_NORMAL);
                        }
                    }
                else if (a instanceof Error)
                    {
                    addType(FederatedConnectionEvent.Type.ERROR);
                    }
                }
            }
        }

    // ---- inner class: FederatedChangeEventHandler ------------------------

    /**
     * Handler for {@link FederatedChangeEvent}s.
     */
    static class FederatedChangeEventHandler
            extends FederationEventHandler
        {
        FederatedChangeEventHandler(EventObserver observer)
            {
            super(observer, FederatedChangeEvent.Type.class, FederatedChangeEvent::getParticipant);

            for (Annotation a : observer.getObservedQualifiers())
                {
                if (a instanceof CommittingLocal)
                    {
                    addType(FederatedChangeEvent.Type.COMMITTING_LOCAL);
                    }
                else if (a instanceof CommittingRemote)
                    {
                    addType(FederatedChangeEvent.Type.COMMITTING_REMOTE);
                    }
                else if (a instanceof Replicating)
                    {
                    addType(FederatedChangeEvent.Type.REPLICATING);
                    }
                }
            }
        }

    // ---- inner class: FederatedChangeEventHandler -------------------------

    /**
     * Handler for {@link FederatedPartitionEvent}s.
     */
    static class FederatedPartitionEventHandler
            extends FederationEventHandler
        {
        FederatedPartitionEventHandler(EventObserver observer)
            {
            super(observer, FederatedPartitionEvent.Type.class, FederatedPartitionEvent::getParticipant);

            for (Annotation a : observer.getObservedQualifiers())
                {
                if (a instanceof Syncing)
                    {
                    addType(FederatedPartitionEvent.Type.SYNCING);
                    }
                else if (a instanceof Synced)
                    {
                    addType(FederatedPartitionEvent.Type.SYNCED);
                    }
                }
            }
        }
    }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy