001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.oscache.extra;
006:
007: import com.opensymphony.oscache.base.events.ScopeEvent;
008: import com.opensymphony.oscache.base.events.ScopeEventListener;
009: import com.opensymphony.oscache.base.events.ScopeEventType;
010:
011: /**
012: * Implementation of a ScopeEventListener that keeps track of the scope flush events.
013: * We are not using any synchronized so that this does not become a bottleneck.
014: * The consequence is that on retrieving values, the operations that are
015: * currently being done won't be counted.
016: *
017: * @version $Revision: 254 $
018: * @author <a href="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
019: */
020: public class ScopeEventListenerImpl implements ScopeEventListener {
021: /**
022: * Scope names
023: */
024: public static final String[] SCOPE_NAMES = { null, "page",
025: "request", "session", "application" };
026:
027: /**
028: * Number of known scopes
029: */
030: public static final int NB_SCOPES = SCOPE_NAMES.length - 1;
031:
032: /**
033: * Page scope number
034: */
035: public static final int PAGE_SCOPE = 1;
036:
037: /**
038: * Request scope number
039: */
040: public static final int REQUEST_SCOPE = 2;
041:
042: /**
043: * Session scope number
044: */
045: public static final int SESSION_SCOPE = 3;
046:
047: /**
048: * Application scope number
049: */
050: public static final int APPLICATION_SCOPE = 4;
051:
052: /**
053: * Flush counter for all scopes.
054: * Add one to the number of scope because the array is being used
055: * from position 1 instead of 0 for convenience
056: */
057: private int[] scopeFlushCount = new int[NB_SCOPES + 1];
058:
059: public ScopeEventListenerImpl() {
060: }
061:
062: /**
063: * Gets the flush count for scope {@link ScopeEventListenerImpl#APPLICATION_SCOPE}.
064: * <p>
065: * @return The total number of application flush
066: */
067: public int getApplicationScopeFlushCount() {
068: return scopeFlushCount[APPLICATION_SCOPE];
069: }
070:
071: /**
072: * Gets the flush count for scope {@link ScopeEventListenerImpl#PAGE_SCOPE}.
073: * @return The total number of page flush
074: */
075: public int getPageScopeFlushCount() {
076: return scopeFlushCount[PAGE_SCOPE];
077: }
078:
079: /**
080: * Gets the flush count for scope {@link ScopeEventListenerImpl#REQUEST_SCOPE}.
081: * @return The total number of request flush
082: */
083: public int getRequestScopeFlushCount() {
084: return scopeFlushCount[REQUEST_SCOPE];
085: }
086:
087: /**
088: * Gets the flush count for scope {@link ScopeEventListenerImpl#SESSION_SCOPE}.
089: * @return The total number of session flush
090: */
091: public int getSessionScopeFlushCount() {
092: return scopeFlushCount[SESSION_SCOPE];
093: }
094:
095: /**
096: * Returns the total flush count.
097: * @return The total number of scope flush
098: */
099: public int getTotalScopeFlushCount() {
100: int total = 0;
101:
102: for (int count = 1; count <= NB_SCOPES; count++) {
103: total += scopeFlushCount[count];
104: }
105:
106: return total;
107: }
108:
109: /**
110: * Handles all the scope flush events.
111: * @param event The scope event
112: */
113: public void scopeFlushed(ScopeEvent event) {
114: // Get the event type and process it
115: ScopeEventType eventType = event.getEventType();
116:
117: if (eventType == ScopeEventType.ALL_SCOPES_FLUSHED) {
118: // All 4 scopes were flushed, increment the counters
119: for (int count = 1; count <= NB_SCOPES; count++) {
120: scopeFlushCount[count]++;
121: }
122: } else if (eventType == ScopeEventType.SCOPE_FLUSHED) {
123: // Get back the scope from the event and increment the flush count
124: scopeFlushCount[event.getScope()]++;
125: } else {
126: // Unknown event!
127: throw new IllegalArgumentException(
128: "Unknown Scope Event type received");
129: }
130: }
131:
132: /**
133: * Returns all the flush counter in a string form.
134: */
135: public String toString() {
136: StringBuffer returnString = new StringBuffer("Flush count for ");
137:
138: for (int count = 1; count <= NB_SCOPES; count++) {
139: returnString.append("scope " + SCOPE_NAMES[count] + " = "
140: + scopeFlushCount[count] + ", ");
141: }
142:
143: // Remove the last 2 chars, which are ", "
144: returnString.setLength(returnString.length() - 2);
145:
146: return returnString.toString();
147: }
148: }
|