001/*
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.
018 */
019package org.apache.wiki.event;
020
021import java.util.Set;
022
023
024/**
025 * Emits all kind of {@link org.apache.wiki.event.WikiEvent}s.
026 */
027public enum WikiEventEmitter {
028
029    INSTANCE;
030
031    public static WikiEventEmitter get() {
032        return INSTANCE;
033    }
034
035    /**
036     * Fires a Workflow Event from provided source and workflow type.
037     *
038     * @param src the source of the event, which can be any object: a wiki page, group or authentication/authentication/group manager.
039     * @param type the type of event
040     * @return fired {@link WorkflowEvent} or {@code null} if the {@link WikiEventEmitter} instance hasn't listeners attached.
041     */
042    public static WorkflowEvent fireWorkflowEvent( final Object src, final int type ) {
043        return fireEvent( new WorkflowEvent( src, type ) );
044    }
045
046    /**
047     * Fires a Workflow Event from provided source and workflow type.
048     *
049     * @param src the source of the event, which can be any object: a wiki page, group or authentication/authentication/group manager.
050     * @param type the type of event
051     * @return fired {@link WorkflowEvent} or {@code null} if the {@link WikiEventEmitter} instance hasn't listeners attached.
052     */
053    public static WorkflowEvent fireWorkflowEvent( final Object src, final int type, final Object... args ) {
054        return fireEvent( new WorkflowEvent( src, type, args ) );
055    }
056
057    static < T extends WikiEvent > T fireEvent( final T event ) {
058        if( WikiEventManager.isListening( WikiEventEmitter.get() ) ) {
059            WikiEventManager.fireEvent( WikiEventEmitter.get(), event );
060            return event;
061        }
062        return null;
063    }
064
065    /**
066     * Registers a {@link WikiEventListener} so it listens events fired from the {@link WikiEventEmitter} instance. Every other
067     * {@link WikiEventListener} of the same type, listening events from the {@link WikiEventEmitter} instance will stop listening events
068     * from it. This ensures events received by the {@link WikiEventListener} will only process the events once.
069     *
070     * @param listener {@link WikiEventListener}
071     */
072    public static void attach( final WikiEventListener listener ) {
073        if( WikiEventManager.isListening( get() ) ) {
074            final Set< WikiEventListener > attachedListeners = WikiEventManager.getWikiEventListeners( WikiEventEmitter.get() );
075            attachedListeners.stream()
076                             .filter( l -> listener.getClass().isAssignableFrom( l.getClass() ) )
077                             .forEach( WikiEventManager::removeWikiEventListener );
078        }
079        register( listener );
080    }
081
082    /**
083     * Registers a {@link WikiEventListener} so it listens events fired from the {@link WikiEventEmitter} instance. Events received by the
084     * {@link WikiEventListener} could process the events more than once or, several instances of the same {@link WikiEventListener} would
085     * be able to receive the same event.
086     *
087     * @param listener {@link WikiEventListener}
088     */
089    public static void register( final WikiEventListener listener ) {
090        WikiEventManager.addWikiEventListener( WikiEventEmitter.get(), listener );
091    }
092
093}