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 static < T extends WikiEvent > T fireEvent( final T event ) { 047 if( WikiEventManager.isListening( WikiEventEmitter.get() ) ) { 048 WikiEventManager.fireEvent( WikiEventEmitter.get(), event ); 049 return event; 050 } 051 return null; 052 } 053 054 /** 055 * Registers a {@link WikiEventListener} so it listens events fired from the {@link WikiEventEmitter} instance. Every other 056 * {@link WikiEventListener} of the same type, listening events from the {@link WikiEventEmitter} instance will stop listening events 057 * from it. This ensures events received by the {@link WikiEventListener} will only process the events once. 058 * 059 * @param listener {@link WikiEventListener} 060 */ 061 public static void attach( final WikiEventListener listener ) { 062 if( WikiEventManager.isListening( get() ) ) { 063 final Set< WikiEventListener > attachedListeners = WikiEventManager.getWikiEventListeners( WikiEventEmitter.get() ); 064 attachedListeners.stream() 065 .filter( l -> listener.getClass().isAssignableFrom( l.getClass() ) ) 066 .forEach( WikiEventManager::removeWikiEventListener ); 067 } 068 register( listener ); 069 } 070 071 /** 072 * Registers a {@link WikiEventListener} so it listens events fired from the {@link WikiEventEmitter} instance. Events received by the 073 * {@link WikiEventListener} could process the events more than once or, several instances of the same {@link WikiEventListener} would 074 * be able to receive the same event. 075 * 076 * @param listener {@link WikiEventListener} 077 */ 078 public static void register( final WikiEventListener listener ) { 079 WikiEventManager.addWikiEventListener( WikiEventEmitter.get(), listener ); 080 } 081 082}