001/*
002 * (C) Copyright 2015 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-2.1.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 *     Antoine Taillefer <ataillefer@nuxeo.com>
016 */
017
018package org.nuxeo.drive.listener;
019
020import java.util.Collections;
021
022import org.apache.commons.lang.ArrayUtils;
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.drive.service.NuxeoDriveEvents;
026import org.nuxeo.ecm.core.event.Event;
027import org.nuxeo.ecm.core.event.EventBundle;
028import org.nuxeo.ecm.core.event.EventContext;
029import org.nuxeo.ecm.core.event.PostCommitFilteringEventListener;
030import org.nuxeo.ecm.platform.audit.api.AuditLogger;
031import org.nuxeo.ecm.platform.audit.api.LogEntry;
032import org.nuxeo.runtime.api.Framework;
033
034/**
035 * Post-commit asynchronous listener that pushes the virtual events generated by
036 * {@link NuxeoDriveFileSystemDeletionListener} to the Audit log.
037 *
038 * @since 7.4
039 */
040public class NuxeoDriveVirtualEventLogger implements PostCommitFilteringEventListener {
041
042    private static final Log log = LogFactory.getLog(NuxeoDriveVirtualEventLogger.class);
043
044    @Override
045    public boolean acceptEvent(Event event) {
046        return NuxeoDriveEvents.VIRTUAL_EVENT_CREATED.equals(event.getName());
047    }
048
049    @Override
050    public void handleEvent(EventBundle events) {
051        AuditLogger logger = Framework.getService(AuditLogger.class);
052        if (logger != null) {
053            for (Event event : events) {
054                EventContext ctx = event.getContext();
055                Object[] args = ctx.getArguments();
056                if (ArrayUtils.isEmpty(args)) {
057                    return;
058                }
059                Object arg = args[0];
060                if (!(arg instanceof LogEntry)) {
061                    return;
062                }
063                LogEntry entry = (LogEntry) arg;
064                logger.addLogEntries(Collections.singletonList(entry));
065            }
066        } else {
067            log.error("Can not reach AuditLogger");
068        }
069    }
070
071}