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