|
|||||||||||||||||||
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.4 $
|
|
18 |
* @author <a href="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
|
|
19 |
*/
|
|
20 |
public class ScopeEventListenerImpl implements ScopeEventListener { |
|
21 |
/**
|
|
22 |
* Scope names
|
|
23 |
*/
|
|
24 |
public static final String[] SCOPE_NAMES = { |
|
25 |
null, "page", "request", "session", "application" |
|
26 |
}; |
|
27 |
|
|
28 |
/**
|
|
29 |
* Number of known scopes
|
|
30 |
*/
|
|
31 |
public static final int NB_SCOPES = SCOPE_NAMES.length - 1; |
|
32 |
|
|
33 |
/**
|
|
34 |
* Page scope number
|
|
35 |
*/
|
|
36 |
public static final int PAGE_SCOPE = 1; |
|
37 |
|
|
38 |
/**
|
|
39 |
* Request scope number
|
|
40 |
*/
|
|
41 |
public static final int REQUEST_SCOPE = 2; |
|
42 |
|
|
43 |
/**
|
|
44 |
* Session scope number
|
|
45 |
*/
|
|
46 |
public static final int SESSION_SCOPE = 3; |
|
47 |
|
|
48 |
/**
|
|
49 |
* Application scope number
|
|
50 |
*/
|
|
51 |
public static final int APPLICATION_SCOPE = 4; |
|
52 |
|
|
53 |
/**
|
|
54 |
* Flush counter for all scopes.
|
|
55 |
* Add one to the number of scope because the array is being used
|
|
56 |
* from position 1 instead of 0 for convenience
|
|
57 |
*/
|
|
58 |
private int[] scopeFlushCount = new int[NB_SCOPES + 1]; |
|
59 |
|
|
60 | 4 |
public ScopeEventListenerImpl() {
|
61 |
} |
|
62 |
|
|
63 |
/**
|
|
64 |
* Gets the flush count for scope {@link ScopeEventListenerImpl#APPLICATION_SCOPE}.
|
|
65 |
* <p>
|
|
66 |
* @return The total number of application flush
|
|
67 |
*/
|
|
68 | 4 |
public int getApplicationScopeFlushCount() { |
69 | 4 |
return scopeFlushCount[APPLICATION_SCOPE];
|
70 |
} |
|
71 |
|
|
72 |
/**
|
|
73 |
* Gets the flush count for scope {@link ScopeEventListenerImpl#PAGE_SCOPE}.
|
|
74 |
* @return The total number of page flush
|
|
75 |
*/
|
|
76 | 4 |
public int getPageScopeFlushCount() { |
77 | 4 |
return scopeFlushCount[PAGE_SCOPE];
|
78 |
} |
|
79 |
|
|
80 |
/**
|
|
81 |
* Gets the flush count for scope {@link ScopeEventListenerImpl#REQUEST_SCOPE}.
|
|
82 |
* @return The total number of request flush
|
|
83 |
*/
|
|
84 | 4 |
public int getRequestScopeFlushCount() { |
85 | 4 |
return scopeFlushCount[REQUEST_SCOPE];
|
86 |
} |
|
87 |
|
|
88 |
/**
|
|
89 |
* Gets the flush count for scope {@link ScopeEventListenerImpl#SESSION_SCOPE}.
|
|
90 |
* @return The total number of session flush
|
|
91 |
*/
|
|
92 | 4 |
public int getSessionScopeFlushCount() { |
93 | 4 |
return scopeFlushCount[SESSION_SCOPE];
|
94 |
} |
|
95 |
|
|
96 |
/**
|
|
97 |
* Returns the total flush count.
|
|
98 |
* @return The total number of scope flush
|
|
99 |
*/
|
|
100 | 4 |
public int getTotalScopeFlushCount() { |
101 | 4 |
int total = 0;
|
102 |
|
|
103 | 4 |
for (int count = 1; count <= NB_SCOPES; count++) { |
104 | 16 |
total += scopeFlushCount[count]; |
105 |
} |
|
106 |
|
|
107 | 4 |
return total;
|
108 |
} |
|
109 |
|
|
110 |
/**
|
|
111 |
* Handles all the scope flush events.
|
|
112 |
* @param event The scope event
|
|
113 |
*/
|
|
114 | 8 |
public void scopeFlushed(ScopeEvent event) { |
115 |
// Get the event type and process it
|
|
116 | 8 |
ScopeEventType eventType = event.getEventType(); |
117 |
|
|
118 | 8 |
if (eventType == ScopeEventType.ALL_SCOPES_FLUSHED) {
|
119 |
// All 4 scopes were flushed, increment the counters
|
|
120 | 4 |
for (int count = 1; count <= NB_SCOPES; count++) { |
121 | 16 |
scopeFlushCount[count]++; |
122 |
} |
|
123 | 4 |
} else if (eventType == ScopeEventType.SCOPE_FLUSHED) { |
124 |
// Get back the scope from the event and increment the flush count
|
|
125 | 4 |
scopeFlushCount[event.getScope()]++; |
126 |
} else {
|
|
127 |
// Unknown event!
|
|
128 | 0 |
throw new IllegalArgumentException("Unknown Scope Event type received"); |
129 |
} |
|
130 |
} |
|
131 |
|
|
132 |
/**
|
|
133 |
* Returns all the flush counter in a string form.
|
|
134 |
*/
|
|
135 | 0 |
public String toString() {
|
136 | 0 |
StringBuffer returnString = new StringBuffer("Flush count for "); |
137 |
|
|
138 | 0 |
for (int count = 1; count <= NB_SCOPES; count++) { |
139 | 0 |
returnString.append("scope " + SCOPE_NAMES[count] + " = " + scopeFlushCount[count] + ", "); |
140 |
} |
|
141 |
|
|
142 |
// Remove the last 2 chars, which are ", "
|
|
143 | 0 |
returnString.setLength(returnString.length() - 2); |
144 |
|
|
145 | 0 |
return returnString.toString();
|
146 |
} |
|
147 |
} |
|
148 |
|
|