001 /* 002 * Copyright 2010 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package com.hs.mail.imap.event; 017 018 import java.util.ArrayList; 019 import java.util.List; 020 021 import com.hs.mail.imap.event.EventListener.Event; 022 023 /** 024 * This class is responsible for dispatching all events coming from Message 025 * Store to all event listeners subscribed in this dispatcher. 026 * 027 * @author Won Chul Doh 028 * @since Jul 30, 2010 029 * 030 */ 031 public class EventDispatcher { 032 033 private final List<EventListener> listeners = new ArrayList<EventListener>(); 034 035 /** 036 * Add a listener for events. 037 * 038 * @param listener 039 * the listener for events 040 */ 041 public void addEventListener(EventListener listener) { 042 synchronized (listeners) { 043 listeners.add(listener); 044 } 045 } 046 047 /** 048 * Remove a listener for events. 049 * 050 * @param listener 051 * the listener 052 * @see #addEventListener 053 */ 054 public void removeEventListener(EventListener listener) { 055 synchronized (listeners) { 056 listeners.remove(listener); 057 } 058 } 059 060 public void added(long mailboxID) { 061 notifyEventListeners(new MessageAddedEvent(mailboxID)); 062 } 063 064 public void expunged(long sessionID, long mailboxID, List<Long> uids) { 065 notifyEventListeners(new MessageExpungedEvent(sessionID, mailboxID, uids)); 066 } 067 068 public void flagsUpdated(long sessionID, long mailboxID, List<Long> uids) { 069 notifyEventListeners(new FlagUpdatedEvent(sessionID, mailboxID, uids)); 070 } 071 072 public void mailboxDeleted(long sessionID, long mailboxID) { 073 notifyEventListeners(new MailboxDeletedEvent(sessionID, mailboxID)); 074 } 075 076 /** 077 * Notify all event listeners. 078 * 079 * @param event 080 * event to broadcast 081 */ 082 public void notifyEventListeners(Event event) { 083 synchronized (listeners) { 084 for (EventListener listener : listeners) { 085 listener.event(event); 086 } 087 } 088 } 089 090 }