001/*
002 * (C) Copyright 2006-2009 Nuxeo SAS (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 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019package org.nuxeo.ecm.admin.monitoring;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.Arrays;
024import java.util.List;
025
026import org.jboss.seam.ScopeType;
027import org.jboss.seam.annotations.Factory;
028import org.jboss.seam.annotations.Name;
029import org.jboss.seam.annotations.Scope;
030import org.jboss.seam.contexts.Contexts;
031import org.nuxeo.ecm.core.management.events.EventMonitoring;
032
033/**
034 * Simple Seam Bean that wraps {@link EventMonitoring} to expose it to JSF/Seam layer
035 *
036 * @author tiry
037 */
038@Name("eventMonitoringAction")
039@Scope(ScopeType.EVENT)
040public class EventMonitoringActionBean implements Serializable {
041
042    private static final long serialVersionUID = 1L;
043
044    protected EventMonitoring monitor;
045
046    protected EventMonitoring getEventMonitoring() {
047        if (monitor == null) {
048            monitor = new EventMonitoring();
049        }
050        return monitor;
051    }
052
053    public int getActiveThreads() {
054        return getEventMonitoring().getActiveThreadsCount();
055    }
056
057    public int getQueuedEvents() {
058        return getEventMonitoring().getEventsInQueueCount();
059    }
060
061    @Factory(value = "eventSyncStats", scope = ScopeType.EVENT)
062    public List<List<String>> getSyncStats() {
063        String stats = getEventMonitoring().getSyncHandlersExecTime();
064        return formatStats(stats);
065    }
066
067    @Factory(value = "eventAsyncStats", scope = ScopeType.EVENT)
068    public List<List<String>> getAsyncStats() {
069        String stats = getEventMonitoring().getAsyncHandlersExecTime();
070        return formatStats(stats);
071    }
072
073    protected List<List<String>> formatStats(String stats) {
074
075        List<List<String>> result = new ArrayList<List<String>>();
076        if (stats == null || stats.length() == 0) {
077            return result;
078        }
079
080        String[] lines = stats.split("\n");
081        for (String line : lines) {
082            line = line.trim();
083            if (line.length() == 0) {
084                continue;
085            }
086            String[] parts = line.split(" - ");
087            List<String> lin = Arrays.asList(parts);
088            result.add(lin);
089        }
090        return result;
091    }
092
093    public void refresh() {
094        cleanSeamEventCache();
095    }
096
097    public String getEventStatistics() {
098        StringBuilder sb = new StringBuilder();
099
100        sb.append("Active Threads : ");
101        sb.append(getEventMonitoring().getActiveThreadsCount());
102        sb.append("\nQueued events : ");
103        sb.append(getEventMonitoring().getEventsInQueueCount());
104
105        sb.append("\nSync processing time : ");
106        if (getEventMonitoring().isSyncHandlersTrackingEnabled()) {
107            sb.append(getEventMonitoring().getSyncHandlersExecTime());
108        } else {
109            sb.append("[tracking not enabled]");
110        }
111        sb.append("\nAsync processing time : ");
112        if (getEventMonitoring().isAsyncHandlersTrackingEnabled()) {
113            sb.append(getEventMonitoring().getAsyncHandlersExecTime());
114        } else {
115            sb.append("[tracking not enabled]");
116        }
117        return sb.toString();
118    }
119
120    protected void cleanSeamEventCache() {
121        Contexts.getEventContext().remove("eventSyncTrackingEnabled");
122        Contexts.getEventContext().remove("eventAsyncTrackingEnabled");
123        Contexts.getEventContext().remove("eventSyncStats");
124        Contexts.getEventContext().remove("eventAsyncStats");
125    }
126
127    public void enableSyncTracking() {
128        getEventMonitoring().setSyncHandlersTrackingEnabled(true);
129        cleanSeamEventCache();
130    }
131
132    public void enableAsyncTracking() {
133        getEventMonitoring().setAsyncHandlersTrackingEnabled(true);
134        cleanSeamEventCache();
135    }
136
137    public void disableSyncTracking() {
138        getEventMonitoring().setSyncHandlersTrackingEnabled(false);
139        cleanSeamEventCache();
140    }
141
142    public void disableAsyncTracking() {
143        getEventMonitoring().setAsyncHandlersTrackingEnabled(false);
144        cleanSeamEventCache();
145    }
146
147    @Factory(value = "eventSyncTrackingEnabled", scope = ScopeType.EVENT)
148    public boolean isSyncTrackingEnabled() {
149        return getEventMonitoring().isSyncHandlersTrackingEnabled();
150    }
151
152    @Factory(value = "eventAsyncTrackingEnabled", scope = ScopeType.EVENT)
153    public boolean isAsyncTrackingEnabled() {
154        return getEventMonitoring().isAsyncHandlersTrackingEnabled();
155    }
156
157}