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