001/*
002 * (C) Copyright 2006-2011 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 */
021package org.nuxeo.ecm.core.management.events;
022
023import java.util.Arrays;
024import java.util.List;
025
026import org.nuxeo.ecm.core.api.LifeCycleConstants;
027import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
028import org.nuxeo.ecm.core.event.EventBundle;
029import org.nuxeo.ecm.core.event.PostCommitEventListener;
030import org.nuxeo.ecm.core.event.impl.ReconnectedEventBundleImpl;
031import org.nuxeo.runtime.management.counters.CounterHelper;
032
033/**
034 * AsyncEventListener that collects events to update the Simon counters
035 *
036 * @author Tiry (tdelprat@nuxeo.com)
037 */
038public class EventCounterListener implements PostCommitEventListener {
039
040    // Counters used
041    public static final String EVENT_CREATE_COUNTER = "org.nuxeo.event.create";
042
043    public static final String EVENT_UPDATE_COUNTER = "org.nuxeo.event.update";
044
045    public static final String EVENT_REMOVE_COUNTER = "org.nuxeo.event.remove";
046
047    // Event tracked
048    protected static final List<String> createEvents = Arrays.asList(new String[] {
049            DocumentEventTypes.DOCUMENT_CREATED, DocumentEventTypes.DOCUMENT_CREATED_BY_COPY,
050            DocumentEventTypes.DOCUMENT_IMPORTED, DocumentEventTypes.DOCUMENT_PROXY_PUBLISHED,
051            DocumentEventTypes.DOCUMENT_PROXY_UPDATED });
052
053    protected static final List<String> updateEvents = Arrays.asList(new String[] {
054            DocumentEventTypes.DOCUMENT_CHECKEDIN, DocumentEventTypes.DOCUMENT_CHECKEDOUT,
055            DocumentEventTypes.DOCUMENT_CHILDREN_ORDER_CHANGED, DocumentEventTypes.DOCUMENT_LOCKED,
056            DocumentEventTypes.DOCUMENT_MOVED, DocumentEventTypes.DOCUMENT_PUBLISHED,
057            DocumentEventTypes.DOCUMENT_SECURITY_UPDATED, DocumentEventTypes.DOCUMENT_UNLOCKED,
058            DocumentEventTypes.DOCUMENT_UPDATED, LifeCycleConstants.TRANSITION_EVENT });
059
060    protected static final List<String> removeEvents = Arrays.asList(new String[] {
061            DocumentEventTypes.DOCUMENT_REMOVED, DocumentEventTypes.VERSION_REMOVED });
062
063    @Override
064    public void handleEvent(EventBundle events) {
065        if (events instanceof ReconnectedEventBundleImpl) {
066            ReconnectedEventBundleImpl bundle = (ReconnectedEventBundleImpl) events;
067            updateCounters(bundle.getEventNames());
068        }
069    }
070
071    protected void updateCounters(List<String> eventNames) {
072
073        int created = 0;
074        int updated = 0;
075        int removed = 0;
076
077        for (String eventName : eventNames) {
078            if (createEvents.contains(eventName)) {
079                created += 1;
080            } else if (updateEvents.contains(eventName)) {
081                updated += 1;
082            } else if (removeEvents.contains(eventName)) {
083                removed += 1;
084            }
085        }
086
087        if (created > 0) {
088            CounterHelper.increaseCounter(EVENT_CREATE_COUNTER, created);
089        }
090        if (updated > 0) {
091            CounterHelper.increaseCounter(EVENT_UPDATE_COUNTER, updated);
092        }
093        if (removed > 0) {
094            CounterHelper.increaseCounter(EVENT_REMOVE_COUNTER, removed);
095        }
096    }
097
098}