|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ScopeEventListenerImpl.java | 70% | 70% | 87,5% | 73,7% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* Copyright (c) 2002-2003 by OpenSymphony
|
|
| 3 |
* All rights reserved.
|
|
| 4 |
*/
|
|
| 5 |
package com.opensymphony.oscache.extra;
|
|
| 6 |
|
|
| 7 |
import com.opensymphony.oscache.base.events.ScopeEvent;
|
|
| 8 |
import com.opensymphony.oscache.base.events.ScopeEventListener;
|
|
| 9 |
import com.opensymphony.oscache.base.events.ScopeEventType;
|
|
| 10 |
|
|
| 11 |
/**
|
|
| 12 |
* Implementation of a ScopeEventListener that keeps track of the scope flush events.
|
|
| 13 |
* We are not using any synchronized so that this does not become a bottleneck.
|
|
| 14 |
* The consequence is that on retrieving values, the operations that are
|
|
| 15 |
* currently being done won't be counted.
|
|
| 16 |
*
|
|
| 17 |
* @version $Revision: 1.2 $
|
|
| 18 |
* @author <a href="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
|
|
| 19 |
*/
|
|
| 20 |
public class ScopeEventListenerImpl implements ScopeEventListener { |
|
| 21 |
/**
|
|
| 22 |
* Number of known scopes
|
|
| 23 |
*/
|
|
| 24 |
public static final int NB_SCOPES = 4; |
|
| 25 |
|
|
| 26 |
/**
|
|
| 27 |
* Page scope number
|
|
| 28 |
*/
|
|
| 29 |
public static final int PAGE_SCOPE = 1; |
|
| 30 |
|
|
| 31 |
/**
|
|
| 32 |
* Request scope number
|
|
| 33 |
*/
|
|
| 34 |
public static final int REQUEST_SCOPE = 2; |
|
| 35 |
|
|
| 36 |
/**
|
|
| 37 |
* Session scope number
|
|
| 38 |
*/
|
|
| 39 |
public static final int SESSION_SCOPE = 3; |
|
| 40 |
|
|
| 41 |
/**
|
|
| 42 |
* Application scope number
|
|
| 43 |
*/
|
|
| 44 |
public static final int APPLICATION_SCOPE = 4; |
|
| 45 |
|
|
| 46 |
/**
|
|
| 47 |
* Flush counter for all scopes.
|
|
| 48 |
* Add one to the number of scope because the array is being used
|
|
| 49 |
* from position 1 instead of 0 for convenience
|
|
| 50 |
*/
|
|
| 51 |
private int[] scopeFlushCount = new int[NB_SCOPES + 1]; |
|
| 52 |
|
|
| 53 | 3 |
public ScopeEventListenerImpl() {
|
| 54 |
} |
|
| 55 |
|
|
| 56 |
/**
|
|
| 57 |
* Gets the flush count for scope {@link ScopeEventListenerImpl#APPLICATION_SCOPE}.
|
|
| 58 |
* <p>
|
|
| 59 |
* @return The total number of application flush
|
|
| 60 |
*/
|
|
| 61 | 3 |
public int getApplicationScopeFlushCount() { |
| 62 | 3 |
return scopeFlushCount[APPLICATION_SCOPE];
|
| 63 |
} |
|
| 64 |
|
|
| 65 |
/**
|
|
| 66 |
* Gets the flush count for scope {@link ScopeEventListenerImpl#PAGE_SCOPE}.
|
|
| 67 |
* @return The total number of page flush
|
|
| 68 |
*/
|
|
| 69 | 3 |
public int getPageScopeFlushCount() { |
| 70 | 3 |
return scopeFlushCount[PAGE_SCOPE];
|
| 71 |
} |
|
| 72 |
|
|
| 73 |
/**
|
|
| 74 |
* Gets the flush count for scope {@link ScopeEventListenerImpl#REQUEST_SCOPE}.
|
|
| 75 |
* @return The total number of request flush
|
|
| 76 |
*/
|
|
| 77 | 3 |
public int getRequestScopeFlushCount() { |
| 78 | 3 |
return scopeFlushCount[REQUEST_SCOPE];
|
| 79 |
} |
|
| 80 |
|
|
| 81 |
/**
|
|
| 82 |
* Gets the flush count for scope {@link ScopeEventListenerImpl#SESSION_SCOPE}.
|
|
| 83 |
* @return The total number of session flush
|
|
| 84 |
*/
|
|
| 85 | 3 |
public int getSessionScopeFlushCount() { |
| 86 | 3 |
return scopeFlushCount[SESSION_SCOPE];
|
| 87 |
} |
|
| 88 |
|
|
| 89 |
/**
|
|
| 90 |
* Returns the total flush count.
|
|
| 91 |
* @return The total number of scope flush
|
|
| 92 |
*/
|
|
| 93 | 3 |
public int getTotalScopeFlushCount() { |
| 94 | 3 |
int total = 0;
|
| 95 |
|
|
| 96 | 3 |
for (int count = 1; count <= NB_SCOPES; count++) { |
| 97 | 12 |
total += scopeFlushCount[count]; |
| 98 |
} |
|
| 99 |
|
|
| 100 | 3 |
return total;
|
| 101 |
} |
|
| 102 |
|
|
| 103 |
/**
|
|
| 104 |
* Handles all the scope flush events.
|
|
| 105 |
* @param event The scope event
|
|
| 106 |
*/
|
|
| 107 | 6 |
public void scopeFlushed(ScopeEvent event) { |
| 108 |
// Get the event type and process it
|
|
| 109 | 6 |
ScopeEventType eventType = event.getEventType(); |
| 110 |
|
|
| 111 | 6 |
if (eventType == ScopeEventType.ALL_SCOPES_FLUSHED) {
|
| 112 |
// All 4 scopes were flushed, increment the counters
|
|
| 113 | 3 |
for (int count = 1; count <= NB_SCOPES; count++) { |
| 114 | 12 |
scopeFlushCount[count]++; |
| 115 |
} |
|
| 116 | 3 |
} else if (eventType == ScopeEventType.SCOPE_FLUSHED) { |
| 117 |
// Get back the scope from the event and increment the flush count
|
|
| 118 | 3 |
scopeFlushCount[event.getScope()]++; |
| 119 |
} else {
|
|
| 120 |
// Unknown event!
|
|
| 121 | 0 |
throw new IllegalArgumentException("Unknown Scope Event type received"); |
| 122 |
} |
|
| 123 |
} |
|
| 124 |
|
|
| 125 |
/**
|
|
| 126 |
* Returns all the flush counter in a string form.
|
|
| 127 |
*/
|
|
| 128 | 0 |
public String toString() {
|
| 129 | 0 |
StringBuffer returnString = new StringBuffer("Flush count for "); |
| 130 |
|
|
| 131 | 0 |
for (int count = 1; count <= NB_SCOPES; count++) { |
| 132 | 0 |
returnString.append("scope " + count + " = " + scopeFlushCount[count] + ", "); |
| 133 |
} |
|
| 134 |
|
|
| 135 |
// Remove the last 2 chars, which are ", "
|
|
| 136 | 0 |
returnString.setLength(returnString.length() - 2); |
| 137 |
|
|
| 138 | 0 |
return returnString.toString();
|
| 139 |
} |
|
| 140 |
} |
|
| 141 |
|
|
||||||||||