001/*
002 * (C) Copyright 2012 Nuxeo SA (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 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.directory;
018
019import java.util.List;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.ecm.directory.api.DirectoryService;
024import org.nuxeo.runtime.api.Framework;
025import org.nuxeo.runtime.reload.ReloadEventNames;
026import org.nuxeo.runtime.services.event.Event;
027import org.nuxeo.runtime.services.event.EventListener;
028
029/**
030 * Event listener that flushes the {@link DirectoryService} caches.
031 *
032 * @since 5.6
033 */
034public class DirectoryCacheFlusher implements EventListener {
035
036    private static final Log log = LogFactory.getLog(DirectoryCacheFlusher.class);
037
038    @Override
039    public boolean aboutToHandleEvent(Event event) {
040        return false;
041    }
042
043    @Override
044    public void handleEvent(Event event) {
045        if (!Framework.isDevModeSet()) {
046            log.info("Do not flush the directory caches: dev mode is not set");
047            return;
048        }
049        if (!ReloadEventNames.FLUSH_EVENT_ID.equals(event.getId())) {
050            return;
051        }
052        try {
053            DirectoryService service = Framework.getLocalService(DirectoryService.class);
054            List<Directory> directories = service.getDirectories();
055            if (directories != null) {
056                for (Directory directory : directories) {
057                    directory.getCache().invalidateAll();
058                }
059            }
060        } catch (DirectoryException e) {
061            log.error("Error while flushing the directory caches", e);
062        }
063    }
064
065}