Skip to content

Commit

Permalink
MAILBOX-211 Should be based on more generic components
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/james/project/trunk@1716954 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
chibenwa committed Nov 28, 2015
1 parent b31610b commit cb98746
Show file tree
Hide file tree
Showing 6 changed files with 372 additions and 223 deletions.
Expand Up @@ -55,8 +55,8 @@
import org.apache.james.mailbox.model.SimpleMailboxACL;
import org.apache.james.mailbox.quota.QuotaManager;
import org.apache.james.mailbox.quota.QuotaRootResolver;
import org.apache.james.mailbox.store.event.AbstractDelegatingMailboxListener;
import org.apache.james.mailbox.store.event.HashMapDelegatingMailboxListener;
import org.apache.james.mailbox.store.event.DelegatingMailboxListener;
import org.apache.james.mailbox.store.event.DefaultDelegatingMailboxListener;
import org.apache.james.mailbox.store.event.MailboxEventDispatcher;
import org.apache.james.mailbox.store.mail.MailboxMapper;
import org.apache.james.mailbox.store.mail.model.MailboxId;
Expand Down Expand Up @@ -88,7 +88,7 @@ public class StoreMailboxManager<Id extends MailboxId> implements MailboxManager
public static final int DEFAULT_FETCH_BATCH_SIZE = 200;

private MailboxEventDispatcher<Id> dispatcher;
private AbstractDelegatingMailboxListener delegatingListener = null;
private DelegatingMailboxListener delegatingListener = null;
private final MailboxSessionMapperFactory<Id> mailboxSessionMapperFactory;

private final Authenticator authenticator;
Expand Down Expand Up @@ -193,13 +193,13 @@ public void init() throws MailboxException {
}

/**
* Return the {@link AbstractDelegatingMailboxListener} which is used by this {@link MailboxManager}
* Return the {@link DelegatingMailboxListener} which is used by this {@link MailboxManager}
*
* @return delegatingListener
*/
public AbstractDelegatingMailboxListener getDelegationListener() {
public DelegatingMailboxListener getDelegationListener() {
if (delegatingListener == null) {
delegatingListener = new HashMapDelegatingMailboxListener();
delegatingListener = new DefaultDelegatingMailboxListener();
}
return delegatingListener;
}
Expand Down Expand Up @@ -253,12 +253,12 @@ public GroupMembershipResolver getGroupMembershipResolver() {
}

/**
* Set the {@link AbstractDelegatingMailboxListener} to use with this {@link MailboxManager} instance. If none is set here a {@link HashMapDelegatingMailboxListener} instance will
* Set the {@link DelegatingMailboxListener} to use with this {@link MailboxManager} instance. If none is set here a {@link DefaultDelegatingMailboxListener} instance will
* be created lazy
*
* @param delegatingListener
*/
public void setDelegatingMailboxListener(AbstractDelegatingMailboxListener delegatingListener) {
public void setDelegatingMailboxListener(DelegatingMailboxListener delegatingListener) {
this.delegatingListener = delegatingListener;
dispatcher = new MailboxEventDispatcher<Id>(getDelegationListener());
}
Expand Down

This file was deleted.

@@ -0,0 +1,103 @@
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/

package org.apache.james.mailbox.store.event;

import java.util.Collection;

import org.apache.james.mailbox.MailboxListener;
import org.apache.james.mailbox.MailboxSession;
import org.apache.james.mailbox.exception.MailboxException;
import org.apache.james.mailbox.model.MailboxPath;

/**
* Receive a {@link org.apache.james.mailbox.MailboxListener.Event} and delegate it to an other
* {@link MailboxListener} depending on the registered name
*
* This is a mono instance Thread safe implementation for DelegatingMailboxListener
*/
public class DefaultDelegatingMailboxListener implements DelegatingMailboxListener {

private MailboxListenerRegistry registry;

@Override
public ListenerType getType() {
return ListenerType.EACH_NODE;
}

public DefaultDelegatingMailboxListener() {
this.registry = new MailboxListenerRegistry();
}

@Override
public void addListener(MailboxPath path, MailboxListener listener, MailboxSession session) throws MailboxException {
if (listener.getType() != ListenerType.MAILBOX) {
throw new MailboxException(listener.getClass().getCanonicalName() + " registred on specific MAILBOX operation while its listener type was " + listener.getType());
}
registry.addListener(path, listener);
}

@Override
public void addGlobalListener(MailboxListener listener, MailboxSession session) throws MailboxException {
if (listener.getType() != ListenerType.EACH_NODE && listener.getType() != ListenerType.ONCE) {
throw new MailboxException(listener.getClass().getCanonicalName() + " registered on global event dispatching while its listener type was " + listener.getType());
}
registry.addGlobalListener(listener);
}

@Override
public void removeListener(MailboxPath mailboxPath, MailboxListener listener, MailboxSession session) throws MailboxException {
registry.removeListener(mailboxPath, listener);
}

@Override
public void removeGlobalListener(MailboxListener listener, MailboxSession session) throws MailboxException {
registry.removeGlobalListener(listener);
}

@Override
public void event(Event event) {
Collection<MailboxListener> listenerSnapshot = registry.getLocalMailboxListeners(event.getMailboxPath());
if (event instanceof MailboxDeletion) {
registry.deleteRegistryFor(event.getMailboxPath());
} else if (event instanceof MailboxRenamed) {
MailboxRenamed renamed = (MailboxRenamed) event;
registry.handleRename(renamed.getMailboxPath(), renamed.getNewPath());
}
deliverEventToMailboxListeners(event, listenerSnapshot);
deliverEventToGlobalListeners(event);
}

protected void deliverEventToMailboxListeners(Event event, Collection<MailboxListener> listenerSnapshot) {
for (MailboxListener listener : listenerSnapshot) {
deliverEvent(event, listener);
}
}

protected void deliverEventToGlobalListeners(Event event) {
for (MailboxListener mailboxListener : registry.getGlobalListeners()) {
deliverEvent(event, mailboxListener);
}
}

private void deliverEvent(Event event, MailboxListener listener) {
listener.event(event);
}

}
Expand Up @@ -19,37 +19,9 @@

package org.apache.james.mailbox.store.event;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.james.mailbox.MailboxListener;
import org.apache.james.mailbox.model.MailboxPath;

/**
* Receive a {@link org.apache.james.mailbox.MailboxListener.Event} and delegate it to an other
* {@link MailboxListener} depending on the registered name
*
*/
public class HashMapDelegatingMailboxListener extends AbstractDelegatingMailboxListener{

private Map<MailboxPath, List<MailboxListener>> listeners = new HashMap<MailboxPath, List<MailboxListener>>();
private List<MailboxListener> globalListeners = new ArrayList<MailboxListener>();

@Override
public ListenerType getType() {
return ListenerType.EACH_NODE;
}
import org.apache.james.mailbox.MailboxListenerSupport;

@Override
protected Map<MailboxPath, List<MailboxListener>> getListeners() {
return listeners;
}
public interface DelegatingMailboxListener extends MailboxListenerSupport, MailboxListener{

@Override
protected List<MailboxListener> getGlobalListeners() {
return globalListeners;
}

}

0 comments on commit cb98746

Please sign in to comment.