My favorites
▼
|
Sign in
prodeagle-java
The Java ProdEagle library for AppEngine
Project Home
Downloads
Wiki
Issues
Source
READ-ONLY: This project has been
archived
. For more information see
this post
.
Search
Search within:
All issues
Open issues
New issues
Issues to verify
for
Advanced search
Search tips
Subscriptions
Issue
2
attachment: perf.diff
(10.9 KB)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
diff --git a/src/com/prodeagle/java/Authentication.java b/src/com/prodeagle/java/Authentication.java
index 1419659..1cb4517 100644
--- a/src/com/prodeagle/java/Authentication.java
+++ b/src/com/prodeagle/java/Authentication.java
@@ -30,6 +30,8 @@ import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
+import com.google.appengine.api.memcache.MemcacheService;
+import com.google.appengine.api.memcache.MemcacheServiceFactory;
import com.google.appengine.api.urlfetch.HTTPResponse;
import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;
@@ -39,32 +41,35 @@ import com.google.appengine.api.users.UserServiceFactory;
public class Authentication {
private static final Logger _logger = Logger.getLogger(Authentication.class.getSimpleName());
-
+
private static final String NAMESPACE = "prodeagle";
private static final String SECURE_HOST = "https://prod-eagle.appspot.com";
-
+
public static String getKeySecret(HttpServletRequest req, HttpServletResponse resp) {
return getKeySecret(req, resp, null);
}
-
+
public static String getKeySecret(HttpServletRequest req, HttpServletResponse resp, String updateAuth) {
String originalNamespace = NamespaceManager.get();
NamespaceManager.set(NAMESPACE);
-
+
try { //put in a try so that we can have a finally block which resets namespaces
Key key = KeyFactory.createKey("AuthKey", "master");
-
+
DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
-
+ MemcacheService memcacheService = MemcacheServiceFactory.getMemcacheService();
+
// look for the auth key in the datastore
- Entity authEntity = null;
- try {
- authEntity = datastoreService.get(key);
- } catch (EntityNotFoundException e) {
- _logger.info("Authentication entity not found");
- authEntity = null;
+ Entity authEntity = (Entity) memcacheService.get(KeyFactory.keyToString(key));
+ if (authEntity == null) {
+ try {
+ authEntity = datastoreService.get(key);
+ } catch (EntityNotFoundException e) {
+ _logger.info("Authentication entity not found");
+ authEntity = null;
+ }
}
-
+
if (null != updateAuth) {
//if were updating the auth
if (null == authEntity || !authEntity.getProperty("secret").equals(updateAuth)) {
@@ -72,15 +77,16 @@ public class Authentication {
try {
URLFetchService fetchService = URLFetchServiceFactory.getURLFetchService();
HTTPResponse response = fetchService.fetch(new URL(SECURE_HOST + "/auth/?site=" + req.getHeader("Host") + "&auth=" + updateAuth));
-
+
if (response.getResponseCode() == 200) {
//if successfully received a response, save the new auth key
String content = new String(response.getContent());
if (content.equals(new String("OK"))) {
authEntity = new Entity(key);
authEntity.setProperty("secret", updateAuth);
-
+
datastoreService.put(authEntity);
+ memcacheService.put(KeyFactory.keyToString(key), authEntity);
}
}
} catch (Exception e) {
@@ -89,7 +95,7 @@ public class Authentication {
}
}
}
-
+
if (null != authEntity) {
//if the key did match or we've since updated it, return it
return (String) authEntity.getProperty("secret");
@@ -102,24 +108,24 @@ public class Authentication {
NamespaceManager.set(originalNamespace);
}
}
-
+
public static Boolean isProdEagle(HttpServletRequest req, HttpServletResponse resp) {
String possibleAuth = req.getParameter("auth");
-
+
if (null != possibleAuth) {
String secret = getKeySecret(req, resp, possibleAuth);
-
+
if (null != secret && secret.equals(possibleAuth)) {
return true;
}
}
-
+
return false;
}
-
+
public static Boolean isAdministrator(HttpServletRequest req, HttpServletResponse resp) {
UserService userService = UserServiceFactory.getUserService();
-
+
User user = userService.getCurrentUser();
try {
@@ -133,21 +139,21 @@ public class Authentication {
} else {
_logger.info("User is logged in, but not an admin");
resp.getWriter().print(String.format(
- "Please login with an administrator account. <a href=\"%s\">Logout</a>",
+ "Please login with an administrator account. <a href=\"%s\">Logout</a>",
userService.createLogoutURL(req.getRequestURI() + "?" + req.getQueryString())));
}
} catch (IOException e) {
_logger.severe("Failure to write logout info. Exception: " + e);
}
-
+
return false;
}
-
+
public static void addUser(HttpServletRequest req, HttpServletResponse resp) {
if (isAdministrator(req, resp)) {
_logger.info("User is an admin. Getting secret");
String secret = getKeySecret(req, resp);
-
+
try {
if (null == secret) {
resp.getWriter().print("ProdEagle hasn't set your secret yet. Please visit prodeagle.com and register your website.");
@@ -159,7 +165,7 @@ public class Authentication {
type = "viewer";
email = req.getParameter("viewer");
}
-
+
String host = req.getHeader("Host");
resp.sendRedirect(SECURE_HOST + "/auth/?site=" + host + "&auth=" + secret + "&" + type + "=" + email);
}
diff --git a/src/com/prodeagle/java/MemCacheManager.java b/src/com/prodeagle/java/MemCacheManager.java
index 71e51e8..b60d0af 100644
--- a/src/com/prodeagle/java/MemCacheManager.java
+++ b/src/com/prodeagle/java/MemCacheManager.java
@@ -23,6 +23,7 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import com.google.appengine.api.memcache.AsyncMemcacheService;
@@ -31,7 +32,7 @@ import com.google.appengine.api.memcache.MemcacheServiceFactory;
import com.prodeagle.java.counters.CounterUtil;
/**
- * Helper methods for storing values into MemCache - implements some
+ * Helper methods for storing values into MemCache - implements some
* of the methods that the Python API has, but the Java API does not
* @author Edward Hartwell Goose
*
@@ -39,66 +40,64 @@ import com.prodeagle.java.counters.CounterUtil;
public class MemCacheManager {
private static final Logger _logger = Logger.getLogger(MemCacheManager.class.getName());
-
+ private static final Future<?> _futureEmptyMap = ImmediateFuture.of(Collections.emptyMap());
+
/**
* Equivalent of pythons offset_multi function. Stores multiple counters and their values.
- *
+ *
* @param allCounters - the map of counters and their values to store
* @param namespace - the memcache namespace
* @param slotPrefix - the slot these counters are for
* @param initalValue - the inital value if they have none
* @return - the values stored in memcache
*/
- @SuppressWarnings("unchecked")
public static Future<Map<String, Long>> storeMultipleCounters(Map<String, Long> allCounters, String namespace, String slotPrefix, long initalValue) {
if (null == allCounters || allCounters.isEmpty()) {
- return (Future<Map<String, Long>>) Collections.emptyMap();
+ return futureEmptyMap();
}
-
+
Map<String, Long> prefixedCounters = new HashMap<String, Long>();
-
+
for (String counterName : allCounters.keySet()) {
prefixedCounters.put(slotPrefix + "_" + counterName, allCounters.get(counterName));
}
-
+
return storeMultipleCounters(prefixedCounters, namespace, initalValue);
}
-
+
/**
* Equivalent of pythons offset_multi function. Stores multiple counters and their values, with no prefix on the counter names
- *
+ *
* @param allCounters - the map of counters and their values
* @param namespace - the memcache namespace to store the counters in
* @param initalValue - the inital value to store
* @return - the values stored in memcache
*/
- @SuppressWarnings("unchecked")
public static Future<Map<String, Long>> storeMultipleCounters(Map<String, Long> allCounters, String namespace, long initalValue) {
if (null == allCounters || allCounters.isEmpty()) {
- return (Future<Map<String, Long>>) Collections.emptyMap();
+ return futureEmptyMap();
}
-
+
AsyncMemcacheService memcacheService = MemcacheServiceFactory.getAsyncMemcacheService(namespace);
-
+
return memcacheService.incrementAll(allCounters, initalValue);
}
-
+
/**
* Get a set of counters from memcache
- *
+ *
* @param allCounterNames - all the counter names we want the values for
* @param namespace - the memcache namespace to use
* @return - the memcache values currently stored
*/
- @SuppressWarnings("unchecked")
public static Future<Map<String, Object>> getMultipleCounters(Collection<String> allCounterNames, String namespace) {
if (null == allCounterNames || allCounterNames.isEmpty()) {
- return (Future<Map<String, Object>>) Collections.emptyMap();
+ return futureEmptyMap();
}
-
+
AsyncMemcacheService memcacheService = MemcacheServiceFactory.getAsyncMemcacheService(namespace);
-
+
return memcacheService.getAll(allCounterNames);
}
@@ -113,14 +112,14 @@ public class MemCacheManager {
if (null == counterNames || counterNames.isEmpty()) {
return Collections.emptyMap();
}
-
+
Set<String> prefixedCounters = new HashSet<String>();
for (String name : counterNames) {
prefixedCounters.add(slotPrefix + "_" + name); //change each counters name to the slotPrefix_countername
}
MemcacheService memcacheService = MemcacheServiceFactory.getMemcacheService(namespace);
-
+
return memcacheService.getAll(prefixedCounters);
}
@@ -133,10 +132,10 @@ public class MemCacheManager {
if (null == counterNames || counterNames.isEmpty()) {
return;
}
-
+
_logger.info("Deleting counter keys (count: " + counterNames.size() + ")");
AsyncMemcacheService memcacheService = MemcacheServiceFactory.getAsyncMemcacheService(namespace);
-
+
memcacheService.deleteAll(counterNames);
}
@@ -144,4 +143,35 @@ public class MemCacheManager {
MemcacheService memcacheService = MemcacheServiceFactory.getMemcacheService(CounterUtil.NAMESPACE);
memcacheService.clearAll();
}
+
+ @SuppressWarnings("unchecked")
+ private static <K, V> Future<Map<K, V>> futureEmptyMap() {
+ return (Future<Map<K, V>>) _futureEmptyMap;
+ }
+
+ private static final class ImmediateFuture<V> implements Future<V> {
+ private final V value;
+
+ ImmediateFuture(V value) {
+ this.value = value;
+ }
+ static <V> ImmediateFuture<V> of(V value) {
+ return new ImmediateFuture<V>(value);
+ }
+ @Override public boolean cancel(boolean arg0) {
+ return false;
+ }
+ @Override public V get() {
+ return value;
+ }
+ @Override public V get(long duraton, TimeUnit unit) {
+ return get();
+ }
+ @Override public boolean isCancelled() {
+ return false;
+ }
+ @Override public boolean isDone() {
+ return true;
+ }
+ }
}
Powered by
Google Project Hosting