001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id: ActionFilterRegistry.java 20637 2007-06-17 12:37:03Z sfermigier $
018 */
019
020package org.nuxeo.ecm.platform.actions;
021
022import java.io.Serializable;
023import java.util.Collection;
024import java.util.Collections;
025import java.util.HashMap;
026import java.util.Map;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030
031/**
032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
033 */
034public class ActionFilterRegistry implements Serializable {
035
036    private static final Log log = LogFactory.getLog(ActionFilterRegistry.class);
037
038    private static final long serialVersionUID = 1L;
039
040    private final Map<String, ActionFilter> filters;
041
042    public ActionFilterRegistry() {
043        filters = new HashMap<String, ActionFilter>();
044    }
045
046    public synchronized void addFilter(ActionFilter filter) {
047        String id = filter.getId();
048        if (log.isDebugEnabled()) {
049            if (filters.containsKey(id)) {
050                log.debug("Overriding action filter: " + id);
051            } else {
052                log.debug("Registering action filter: " + id);
053            }
054        }
055        filters.put(id, filter);
056    }
057
058    public synchronized ActionFilter removeFilter(String id) {
059        if (log.isDebugEnabled()) {
060            log.debug("Un-Registering action filter: " + id);
061        }
062
063        return filters.remove(id);
064    }
065
066    public synchronized Collection<ActionFilter> getFilters() {
067        return Collections.unmodifiableCollection(filters.values());
068    }
069
070    public synchronized ActionFilter getFilter(String id) {
071        return filters.get(id);
072    }
073
074}