-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathQueueXml.java
More file actions
676 lines (571 loc) · 22.7 KB
/
Copy pathQueueXml.java
File metadata and controls
676 lines (571 loc) · 22.7 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
package com.google.apphosting.utils.config;
import com.google.apphosting.utils.config.AppEngineConfigException;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class QueueXml {
static final String RATE_REGEX = "([0-9]+(\\.[0-9]+)?)/([smhd])";
static final Pattern RATE_PATTERN = Pattern.compile("([0-9]+(\\.[0-9]+)?)/([smhd])");
static final String TOTAL_STORAGE_LIMIT_REGEX = "^([0-9]+(\\.[0-9]*)?[BKMGT]?)";
static final Pattern TOTAL_STORAGE_LIMIT_PATTERN = Pattern.compile("^([0-9]+(\\.[0-9]*)?[BKMGT]?)");
private static final int MAX_QUEUE_NAME_LENGTH = 100;
private static final String QUEUE_NAME_REGEX = "[a-zA-Z\\d-]{1,100}";
private static final Pattern QUEUE_NAME_PATTERN = Pattern.compile("[a-zA-Z\\d-]{1,100}");
private static final String TASK_AGE_LIMIT_REGEX = "([0-9]+(?:\\.?[0-9]*(?:[eE][\\-+]?[0-9]+)?)?)([smhd])";
private static final Pattern TASK_AGE_LIMIT_PATTERN = Pattern.compile("([0-9]+(?:\\.?[0-9]*(?:[eE][\\-+]?[0-9]+)?)?)([smhd])");
private static final String MODE_REGEX = "push|pull";
private static final Pattern MODE_PATTERN = Pattern.compile("push|pull");
private static final int MAX_TARGET_LENGTH = 100;
private static final String TARGET_REGEX = "[a-z\\d\\-\\.]{1,100}";
private static final Pattern TARGET_PATTERN = Pattern.compile("[a-z\\d\\-\\.]{1,100}");
private static final String DEFAULT_QUEUE = "default";
private final LinkedHashMap entries = new LinkedHashMap();
private QueueXml.Entry lastEntry;
private String totalStorageLimit = "";
public static QueueXml.Entry defaultEntry() {
return new QueueXml.Entry("default", 5.0D, QueueXml.RateUnit.SECOND, 5, (Integer)null, (String)null);
}
public QueueXml.Entry addNewEntry() {
this.validateLastEntry();
this.lastEntry = new QueueXml.Entry();
return this.lastEntry;
}
public void addEntry(QueueXml.Entry entry) {
this.validateLastEntry();
this.lastEntry = entry;
this.validateLastEntry();
}
public Collection<QueueXml.Entry> getEntries() {
this.validateLastEntry();
return this.entries.values();
}
public void validateLastEntry() {
if(this.lastEntry != null) {
if(this.lastEntry.getName() == null) {
throw new AppEngineConfigException("Queue entry must have a name.");
} else if(this.entries.containsKey(this.lastEntry.getName())) {
throw new AppEngineConfigException("Queue entry has duplicate name.");
} else {
if("pull".equals(this.lastEntry.getMode())) {
if(this.lastEntry.getRate() != null) {
throw new AppEngineConfigException("Rate must not be specified for pull queue.");
}
if(this.lastEntry.getBucketSize() != null) {
throw new AppEngineConfigException("Bucket size must not be specified for pull queue.");
}
if(this.lastEntry.getMaxConcurrentRequests() != null) {
throw new AppEngineConfigException("MaxConcurrentRequests must not be specified for pull queue.");
}
QueueXml.RetryParameters retryParameters = this.lastEntry.getRetryParameters();
if(retryParameters != null) {
if(retryParameters.getAgeLimitSec() != null) {
throw new AppEngineConfigException("Age limit must not be specified for pull queue.");
}
if(retryParameters.getMinBackoffSec() != null) {
throw new AppEngineConfigException("Min backoff must not be specified for pull queue.");
}
if(retryParameters.getMaxBackoffSec() != null) {
throw new AppEngineConfigException("Max backoff must not be specified for pull queue.");
}
if(retryParameters.getMaxDoublings() != null) {
throw new AppEngineConfigException("Max doublings must not be specified for pull queue.");
}
}
} else if(this.lastEntry.getRate() == null) {
throw new AppEngineConfigException("A queue rate is required for push queue.");
}
this.entries.put(this.lastEntry.getName(), this.lastEntry);
this.lastEntry = null;
}
}
}
public void setTotalStorageLimit(String s) {
this.totalStorageLimit = s;
}
public String getTotalStorageLimit() {
return this.totalStorageLimit;
}
public String toYaml() {
StringBuilder builder = new StringBuilder();
if(this.getTotalStorageLimit().length() > 0) {
builder.append("total_storage_limit: " + this.getTotalStorageLimit() + "\n\n");
}
builder.append("queue:\n");
Iterator i$ = this.getEntries().iterator();
while(true) {
List acl;
do {
if(!i$.hasNext()) {
return builder.toString();
}
QueueXml.Entry ent = (QueueXml.Entry)i$.next();
builder.append("- name: " + ent.getName() + "\n");
Double rate = ent.getRate();
if(rate != null) {
builder.append(" rate: " + rate + '/' + ent.getRateUnit().getIdent() + "\n");
}
Integer bucketSize = ent.getBucketSize();
if(bucketSize != null) {
builder.append(" bucket_size: " + bucketSize + "\n");
}
Integer maxConcurrentRequests = ent.getMaxConcurrentRequests();
if(maxConcurrentRequests != null) {
builder.append(" max_concurrent_requests: " + maxConcurrentRequests + "\n");
}
QueueXml.RetryParameters retryParameters = ent.getRetryParameters();
if(retryParameters != null) {
builder.append(" retry_parameters:\n");
if(retryParameters.getRetryLimit() != null) {
builder.append(" task_retry_limit: " + retryParameters.getRetryLimit() + "\n");
}
if(retryParameters.getAgeLimitSec() != null) {
builder.append(" task_age_limit: " + retryParameters.getAgeLimitSec() + "s\n");
}
if(retryParameters.getMinBackoffSec() != null) {
builder.append(" min_backoff_seconds: " + retryParameters.getMinBackoffSec() + "\n");
}
if(retryParameters.getMaxBackoffSec() != null) {
builder.append(" max_backoff_seconds: " + retryParameters.getMaxBackoffSec() + "\n");
}
if(retryParameters.getMaxDoublings() != null) {
builder.append(" max_doublings: " + retryParameters.getMaxDoublings() + "\n");
}
}
String target = ent.getTarget();
if(target != null) {
builder.append(" target: " + target + "\n");
}
String mode = ent.getMode();
if(mode != null) {
builder.append(" mode: " + mode + "\n");
}
acl = ent.getAcl();
} while(acl == null);
builder.append(" acl:\n");
Iterator i$1 = acl.iterator();
while(i$1.hasNext()) {
QueueXml.AclEntry aclEntry = (QueueXml.AclEntry)i$1.next();
if(aclEntry.getUserEmail() != null) {
builder.append(" - user_email: " + aclEntry.getUserEmail() + "\n");
} else if(aclEntry.getWriterEmail() != null) {
builder.append(" - writer_email: " + aclEntry.getWriterEmail() + "\n");
}
}
}
}
public static class Entry {
private String name;
private Double rate;
private QueueXml.RateUnit rateUnit;
private Integer bucketSize;
private Integer maxConcurrentRequests;
private QueueXml.RetryParameters retryParameters;
private String target;
private String mode;
private List acl;
public Entry() {
this.name = null;
this.rate = null;
this.rateUnit = QueueXml.RateUnit.SECOND;
this.bucketSize = null;
this.maxConcurrentRequests = null;
this.retryParameters = null;
this.target = null;
this.mode = null;
this.acl = null;
}
public Entry(String name, double rate, QueueXml.RateUnit rateUnit, int bucketSize, Integer maxConcurrentRequests, String target) {
this.name = name;
this.rate = Double.valueOf(rate);
this.rateUnit = rateUnit;
this.bucketSize = Integer.valueOf(bucketSize);
this.maxConcurrentRequests = maxConcurrentRequests;
this.target = target;
}
public String getName() {
return this.name;
}
public void setName(String queueName) {
if(queueName != null && queueName.length() != 0 && QueueXml.QUEUE_NAME_PATTERN.matcher(queueName).matches()) {
this.name = queueName;
} else {
throw new AppEngineConfigException("Queue name does not match expression " + QueueXml.QUEUE_NAME_PATTERN + "; found \'" + queueName + "\'");
}
}
public void setMode(String mode) {
if(mode != null && mode.length() != 0 && QueueXml.MODE_PATTERN.matcher(mode).matches()) {
this.mode = mode;
} else {
throw new AppEngineConfigException("mode must be either \'push\' or \'pull\'");
}
}
public String getMode() {
return this.mode;
}
public List getAcl() {
return this.acl;
}
public void setAcl(List acl) {
this.acl = acl;
}
public void addAcl(QueueXml.AclEntry aclEntry) {
this.acl.add(aclEntry);
}
public Double getRate() {
return this.rate;
}
public void setRate(double rate) {
this.rate = Double.valueOf(rate);
}
public void setRate(String rateString) {
if(rateString.equals("0")) {
this.rate = Double.valueOf(0.0D);
this.rateUnit = QueueXml.RateUnit.SECOND;
} else {
Matcher matcher = QueueXml.RATE_PATTERN.matcher(rateString);
if(!matcher.matches()) {
throw new AppEngineConfigException("Invalid queue rate was specified.");
} else {
String digits = matcher.group(1);
this.rateUnit = QueueXml.RateUnit.valueOf(matcher.group(3).charAt(0));
this.rate = Double.valueOf(digits);
}
}
}
public QueueXml.RateUnit getRateUnit() {
return this.rateUnit;
}
public void setRateUnit(QueueXml.RateUnit rateUnit) {
this.rateUnit = rateUnit;
}
public Integer getBucketSize() {
return this.bucketSize;
}
public void setBucketSize(int bucketSize) {
this.bucketSize = Integer.valueOf(bucketSize);
}
public void setBucketSize(String bucketSize) {
try {
this.bucketSize = Integer.valueOf(bucketSize);
} catch (NumberFormatException var3) {
throw new AppEngineConfigException("Invalid bucket-size was specified.", var3);
}
}
public Integer getMaxConcurrentRequests() {
return this.maxConcurrentRequests;
}
public void setMaxConcurrentRequests(int maxConcurrentRequests) {
this.maxConcurrentRequests = Integer.valueOf(maxConcurrentRequests);
}
public void setMaxConcurrentRequests(String maxConcurrentRequests) {
try {
this.maxConcurrentRequests = Integer.valueOf(maxConcurrentRequests);
} catch (NumberFormatException var3) {
throw new AppEngineConfigException("Invalid max-concurrent-requests was specified: \'" + maxConcurrentRequests + "\'", var3);
}
}
public QueueXml.RetryParameters getRetryParameters() {
return this.retryParameters;
}
public void setRetryParameters(QueueXml.RetryParameters retryParameters) {
this.retryParameters = retryParameters;
}
public String getTarget() {
return this.target;
}
public void setTarget(String target) {
Matcher matcher = QueueXml.TARGET_PATTERN.matcher(target);
if(!matcher.matches()) {
throw new AppEngineConfigException("Invalid queue target was specified. Target: \'" + target + "\'");
} else {
this.target = target;
}
}
public int hashCode() {
boolean prime = true;
byte result = 1;
int result1 = 31 * result + (this.acl == null?0:this.acl.hashCode());
result1 = 31 * result1 + (this.bucketSize == null?0:this.bucketSize.hashCode());
result1 = 31 * result1 + (this.maxConcurrentRequests == null?0:this.maxConcurrentRequests.hashCode());
result1 = 31 * result1 + (this.mode == null?0:this.mode.hashCode());
result1 = 31 * result1 + (this.name == null?0:this.name.hashCode());
result1 = 31 * result1 + (this.rate == null?0:this.rate.hashCode());
result1 = 31 * result1 + (this.rateUnit == null?0:this.rateUnit.hashCode());
result1 = 31 * result1 + (this.target == null?0:this.target.hashCode());
result1 = 31 * result1 + (this.retryParameters == null?0:this.retryParameters.hashCode());
return result1;
}
public boolean equals(Object obj) {
if(this == obj) {
return true;
} else if(obj == null) {
return false;
} else if(this.getClass() != obj.getClass()) {
return false;
} else {
QueueXml.Entry other = (QueueXml.Entry)obj;
if(this.acl == null) {
if(other.acl != null) {
return false;
}
} else if(!this.acl.equals(other.acl)) {
return false;
}
if(this.bucketSize == null) {
if(other.bucketSize != null) {
return false;
}
} else if(!this.bucketSize.equals(other.bucketSize)) {
return false;
}
if(this.maxConcurrentRequests == null) {
if(other.maxConcurrentRequests != null) {
return false;
}
} else if(!this.maxConcurrentRequests.equals(other.maxConcurrentRequests)) {
return false;
}
if(this.mode == null) {
if(other.mode != null) {
return false;
}
} else if(!this.mode.equals(other.mode)) {
return false;
}
if(this.name == null) {
if(other.name != null) {
return false;
}
} else if(!this.name.equals(other.name)) {
return false;
}
if(this.rate == null) {
if(other.rate != null) {
return false;
}
} else if(!this.rate.equals(other.rate)) {
return false;
}
if(this.rateUnit == null) {
if(other.rateUnit != null) {
return false;
}
} else if(!this.rateUnit.equals(other.rateUnit)) {
return false;
}
if(this.target == null) {
if(other.target != null) {
return false;
}
} else if(!this.target.equals(other.target)) {
return false;
}
if(this.retryParameters == null) {
if(other.retryParameters != null) {
return false;
}
} else if(!this.retryParameters.equals(other.retryParameters)) {
return false;
}
return true;
}
}
}
public static class RetryParameters {
private Integer retryLimit = null;
private Integer ageLimitSec = null;
private Double minBackoffSec = null;
private Double maxBackoffSec = null;
private Integer maxDoublings = null;
public Integer getRetryLimit() {
return this.retryLimit;
}
public void setRetryLimit(int retryLimit) {
this.retryLimit = Integer.valueOf(retryLimit);
}
public void setRetryLimit(String retryLimit) {
this.retryLimit = Integer.valueOf(retryLimit);
}
public Integer getAgeLimitSec() {
return this.ageLimitSec;
}
public void setAgeLimitSec(String ageLimitString) {
Matcher matcher = QueueXml.TASK_AGE_LIMIT_PATTERN.matcher(ageLimitString);
if(matcher.matches() && matcher.groupCount() == 2) {
double rateUnitSec = (double)QueueXml.RateUnit.valueOf(matcher.group(2).charAt(0)).getSeconds();
Double ageLimit = Double.valueOf(Double.valueOf(matcher.group(1)).doubleValue() * rateUnitSec);
this.ageLimitSec = Integer.valueOf(ageLimit.intValue());
} else {
throw new AppEngineConfigException("Invalid task age limit was specified.");
}
}
public Double getMinBackoffSec() {
return this.minBackoffSec;
}
public void setMinBackoffSec(double minBackoffSec) {
this.minBackoffSec = Double.valueOf(minBackoffSec);
}
public void setMinBackoffSec(String minBackoffSec) {
this.minBackoffSec = Double.valueOf(minBackoffSec);
}
public Double getMaxBackoffSec() {
return this.maxBackoffSec;
}
public void setMaxBackoffSec(double maxBackoffSec) {
this.maxBackoffSec = Double.valueOf(maxBackoffSec);
}
public void setMaxBackoffSec(String maxBackoffSec) {
this.maxBackoffSec = Double.valueOf(maxBackoffSec);
}
public Integer getMaxDoublings() {
return this.maxDoublings;
}
public void setMaxDoublings(int maxDoublings) {
this.maxDoublings = Integer.valueOf(maxDoublings);
}
public void setMaxDoublings(String maxDoublings) {
this.maxDoublings = Integer.valueOf(maxDoublings);
}
public int hashCode() {
boolean prime = true;
byte result = 1;
int result1 = 31 * result + (this.ageLimitSec == null?0:this.ageLimitSec.hashCode());
result1 = 31 * result1 + (this.maxBackoffSec == null?0:this.maxBackoffSec.hashCode());
result1 = 31 * result1 + (this.maxDoublings == null?0:this.maxDoublings.hashCode());
result1 = 31 * result1 + (this.minBackoffSec == null?0:this.minBackoffSec.hashCode());
result1 = 31 * result1 + (this.retryLimit == null?0:this.retryLimit.hashCode());
return result1;
}
public boolean equals(Object obj) {
if(this == obj) {
return true;
} else if(obj == null) {
return false;
} else if(this.getClass() != obj.getClass()) {
return false;
} else {
QueueXml.RetryParameters other = (QueueXml.RetryParameters)obj;
if(this.ageLimitSec == null) {
if(other.ageLimitSec != null) {
return false;
}
} else if(!this.ageLimitSec.equals(other.ageLimitSec)) {
return false;
}
if(this.maxBackoffSec == null) {
if(other.maxBackoffSec != null) {
return false;
}
} else if(!this.maxBackoffSec.equals(other.maxBackoffSec)) {
return false;
}
if(this.maxDoublings == null) {
if(other.maxDoublings != null) {
return false;
}
} else if(!this.maxDoublings.equals(other.maxDoublings)) {
return false;
}
if(this.minBackoffSec == null) {
if(other.minBackoffSec != null) {
return false;
}
} else if(!this.minBackoffSec.equals(other.minBackoffSec)) {
return false;
}
if(this.retryLimit == null) {
if(other.retryLimit != null) {
return false;
}
} else if(!this.retryLimit.equals(other.retryLimit)) {
return false;
}
return true;
}
}
}
public static class AclEntry {
private String userEmail = null;
private String writerEmail = null;
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
public String getUserEmail() {
return this.userEmail;
}
public void setWriterEmail(String writerEmail) {
this.writerEmail = writerEmail;
}
public String getWriterEmail() {
return this.writerEmail;
}
public int hashCode() {
boolean prime = true;
byte result = 1;
int result1 = 31 * result + (this.userEmail == null?0:this.userEmail.hashCode());
result1 = 31 * result1 + (this.writerEmail == null?0:this.writerEmail.hashCode());
return result1;
}
public boolean equals(Object obj) {
if(this == obj) {
return true;
} else if(obj == null) {
return false;
} else if(this.getClass() != obj.getClass()) {
return false;
} else {
QueueXml.AclEntry other = (QueueXml.AclEntry)obj;
if(this.userEmail == null) {
if(other.userEmail != null) {
return false;
}
} else if(!this.userEmail.equals(other.userEmail)) {
return false;
}
if(this.writerEmail == null) {
if(other.writerEmail != null) {
return false;
}
} else if(!this.writerEmail.equals(other.writerEmail)) {
return false;
}
return true;
}
}
}
public static enum RateUnit {
SECOND('s', 1),
MINUTE('m', SECOND.getSeconds() * 60),
HOUR('h', MINUTE.getSeconds() * 60),
DAY('d', HOUR.getSeconds() * 24);
final char ident;
final int seconds;
private RateUnit(char ident, int seconds) {
this.ident = ident;
this.seconds = seconds;
}
static QueueXml.RateUnit valueOf(char unit) {
switch(unit) {
case 'd':
return DAY;
case 'h':
return HOUR;
case 'm':
return MINUTE;
case 's':
return SECOND;
default:
throw new AppEngineConfigException("Invalid rate was specified.");
}
}
public char getIdent() {
return this.ident;
}
public int getSeconds() {
return this.seconds;
}
}
}