001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and others.
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 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.audit.listener;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.core.event.Event;
027import org.nuxeo.ecm.core.event.EventBundle;
028import org.nuxeo.ecm.core.event.PostCommitFilteringEventListener;
029import org.nuxeo.ecm.platform.audit.api.AuditLogger;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * PostCommit async listener that pushes {@link EventBundle} into the Audit log.
034 *
035 * @author tiry
036 */
037public class AuditEventLogger implements PostCommitFilteringEventListener {
038
039    private static final Log log = LogFactory.getLog(AuditEventLogger.class);
040
041    @Override
042    public boolean acceptEvent(Event event) {
043        AuditLogger logger = Framework.getLocalService(AuditLogger.class);
044        if (logger == null) {
045            return false;
046        }
047        return logger.getAuditableEventNames().contains(event.getName());
048    }
049
050    @Override
051    public void handleEvent(EventBundle events) {
052        AuditLogger logger = Framework.getLocalService(AuditLogger.class);
053        if (logger != null) {
054            logger.logEvents(events);
055        } else {
056            log.error("Can not reach AuditLogger");
057        }
058    }
059
060}