1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| package com.opensymphony.oscache.web.tag; |
6 |
| |
7 |
| import com.opensymphony.oscache.base.Cache; |
8 |
| import com.opensymphony.oscache.base.NeedsRefreshException; |
9 |
| import com.opensymphony.oscache.util.StringUtil; |
10 |
| import com.opensymphony.oscache.web.ServletCacheAdministrator; |
11 |
| import com.opensymphony.oscache.web.WebEntryRefreshPolicy; |
12 |
| |
13 |
| import org.apache.commons.logging.Log; |
14 |
| import org.apache.commons.logging.LogFactory; |
15 |
| |
16 |
| import java.io.IOException; |
17 |
| |
18 |
| import java.util.ArrayList; |
19 |
| import java.util.List; |
20 |
| |
21 |
| import javax.servlet.http.HttpServletRequest; |
22 |
| import javax.servlet.jsp.JspTagException; |
23 |
| import javax.servlet.jsp.PageContext; |
24 |
| import javax.servlet.jsp.tagext.BodyTagSupport; |
25 |
| import javax.servlet.jsp.tagext.TryCatchFinally; |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| public class CacheTag extends BodyTagSupport implements TryCatchFinally { |
50 |
| |
51 |
| |
52 |
| |
53 |
| private final static int SECOND = 1; |
54 |
| private final static int MINUTE = 60 * SECOND; |
55 |
| private final static int HOUR = 60 * MINUTE; |
56 |
| private final static int DAY = 24 * HOUR; |
57 |
| private final static int WEEK = 7 * DAY; |
58 |
| private final static int MONTH = 30 * DAY; |
59 |
| private final static int YEAR = 365 * DAY; |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| private final static String CACHE_TAG_COUNTER_KEY = "__oscache_tag_counter"; |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| final static private int ONE_MINUTE = 60; |
70 |
| final static private int ONE_HOUR = 60 * ONE_MINUTE; |
71 |
| final static private int DEFAULT_TIMEOUT = ONE_HOUR; |
72 |
| private static transient Log log = LogFactory.getLog(CacheTag.class); |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| final static private int SILENT_MODE = 1; |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
| boolean cancelUpdateRequired = false; |
84 |
| private Cache cache = null; |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| private List groups = null; |
90 |
| private ServletCacheAdministrator admin = null; |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| private String actualKey = null; |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| private String content = null; |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| private String cron = null; |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| private String key = null; |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
| private String language = null; |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
| private String refreshPolicyClass = null; |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| private String refreshPolicyParam = null; |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| private boolean refresh = false; |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| private boolean useBody = true; |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
| private int mode = 0; |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| private int scope = PageContext.APPLICATION_SCOPE; |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
| private int time = DEFAULT_TIMEOUT; |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
| |
168 |
| |
169 |
| |
170 |
| |
171 |
| |
172 |
| |
173 |
0
| public void setDuration(String duration) {
|
174 |
0
| try {
|
175 |
| |
176 |
0
| this.time = parseDuration(duration);
|
177 |
| } catch (Exception ex) { |
178 |
0
| if (log.isDebugEnabled()) {
|
179 |
0
| log.debug("Failed parsing simple duration format '" + duration + "' (" + ex.getMessage() + "). Trying ISO-8601 format...");
|
180 |
| } |
181 |
| |
182 |
0
| try {
|
183 |
| |
184 |
0
| this.time = parseISO_8601_Duration(duration);
|
185 |
| } catch (Exception ex1) { |
186 |
| |
187 |
| |
188 |
0
| log.warn("The requested cache duration '" + duration + "' is invalid (" + ex1.getMessage() + "). Reverting to the default timeout");
|
189 |
0
| this.time = DEFAULT_TIMEOUT;
|
190 |
| } |
191 |
| } |
192 |
| } |
193 |
| |
194 |
| |
195 |
| |
196 |
| |
197 |
| |
198 |
0
| public void setCron(String cron) {
|
199 |
0
| this.cron = cron;
|
200 |
| } |
201 |
| |
202 |
| |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
| |
208 |
0
| public void setGroups(String groups) {
|
209 |
0
| this.groups = StringUtil.split(groups, ',');
|
210 |
| } |
211 |
| |
212 |
| |
213 |
| |
214 |
| |
215 |
| |
216 |
| |
217 |
0
| void addGroup(String group) {
|
218 |
0
| if (groups == null) {
|
219 |
0
| groups = new ArrayList();
|
220 |
| } |
221 |
| |
222 |
0
| groups.add(group);
|
223 |
| } |
224 |
| |
225 |
| |
226 |
| |
227 |
| |
228 |
| |
229 |
| |
230 |
0
| public void setKey(String key) {
|
231 |
0
| this.key = key;
|
232 |
| } |
233 |
| |
234 |
| |
235 |
| |
236 |
| |
237 |
| |
238 |
| |
239 |
0
| public void setLanguage(String language) {
|
240 |
0
| this.language = language;
|
241 |
| } |
242 |
| |
243 |
| |
244 |
| |
245 |
| |
246 |
| |
247 |
| |
248 |
| |
249 |
0
| public void setRefresh(boolean refresh) {
|
250 |
0
| this.refresh = refresh;
|
251 |
| } |
252 |
| |
253 |
| |
254 |
| |
255 |
| |
256 |
| |
257 |
| |
258 |
0
| public void setMode(String mode) {
|
259 |
0
| if ("silent".equalsIgnoreCase(mode)) {
|
260 |
0
| this.mode = SILENT_MODE;
|
261 |
| } else { |
262 |
0
| this.mode = 0;
|
263 |
| } |
264 |
| } |
265 |
| |
266 |
| |
267 |
| |
268 |
| |
269 |
0
| public void setRefreshpolicyclass(String refreshPolicyClass) {
|
270 |
0
| this.refreshPolicyClass = refreshPolicyClass;
|
271 |
| } |
272 |
| |
273 |
| |
274 |
| |
275 |
| |
276 |
| |
277 |
0
| public void setRefreshpolicyparam(String refreshPolicyParam) {
|
278 |
0
| this.refreshPolicyParam = refreshPolicyParam;
|
279 |
| } |
280 |
| |
281 |
| |
282 |
| |
283 |
| |
284 |
| |
285 |
| |
286 |
| |
287 |
| |
288 |
0
| public void setScope(String scope) {
|
289 |
0
| if (scope.equalsIgnoreCase(ServletCacheAdministrator.SESSION_SCOPE_NAME)) {
|
290 |
0
| this.scope = PageContext.SESSION_SCOPE;
|
291 |
| } else { |
292 |
0
| this.scope = PageContext.APPLICATION_SCOPE;
|
293 |
| } |
294 |
| } |
295 |
| |
296 |
| |
297 |
| |
298 |
| |
299 |
| |
300 |
| |
301 |
| |
302 |
| |
303 |
| |
304 |
0
| public void setTime(int time) {
|
305 |
0
| this.time = time;
|
306 |
| } |
307 |
| |
308 |
| |
309 |
| |
310 |
| |
311 |
| |
312 |
| |
313 |
| |
314 |
| |
315 |
| |
316 |
| |
317 |
0
| public void setUseBody(boolean useBody) {
|
318 |
0
| if (log.isDebugEnabled()) {
|
319 |
0
| log.debug("<cache>: Set useBody to " + useBody);
|
320 |
| } |
321 |
| |
322 |
0
| this.useBody = useBody;
|
323 |
| } |
324 |
| |
325 |
| |
326 |
| |
327 |
| |
328 |
| |
329 |
| |
330 |
| |
331 |
| |
332 |
0
| public int doAfterBody() throws JspTagException {
|
333 |
0
| String body = null;
|
334 |
| |
335 |
0
| try {
|
336 |
| |
337 |
0
| if ((bodyContent != null) && (useBody || (time == 0)) && ((body = bodyContent.getString()) != null)) {
|
338 |
0
| if ((time != 0) || (refreshPolicyClass != null)) {
|
339 |
| |
340 |
0
| WebEntryRefreshPolicy policy = null;
|
341 |
| |
342 |
0
| if (refreshPolicyClass != null) {
|
343 |
0
| try {
|
344 |
0
| policy = (WebEntryRefreshPolicy) Class.forName(refreshPolicyClass).newInstance();
|
345 |
0
| policy.init(actualKey, refreshPolicyParam);
|
346 |
| } catch (Exception e) { |
347 |
0
| if (log.isInfoEnabled()) {
|
348 |
0
| log.info("<cache>: Problem instantiating or initializing refresh policy : " + refreshPolicyClass);
|
349 |
| } |
350 |
| } |
351 |
| } |
352 |
| |
353 |
0
| if (log.isDebugEnabled()) {
|
354 |
0
| log.debug("<cache>: Updating cache entry with new content : " + actualKey);
|
355 |
| } |
356 |
| |
357 |
0
| cancelUpdateRequired = false;
|
358 |
| |
359 |
0
| if ((groups == null) || groups.isEmpty()) {
|
360 |
0
| cache.putInCache(actualKey, body, policy);
|
361 |
| } else { |
362 |
0
| String[] groupArray = new String[groups.size()];
|
363 |
0
| groups.toArray(groupArray);
|
364 |
0
| cache.putInCache(actualKey, body, groupArray, policy, null);
|
365 |
| } |
366 |
| } |
367 |
| } |
368 |
| |
369 |
| else { |
370 |
0
| if (!useBody && (content != null)) {
|
371 |
0
| if (log.isInfoEnabled()) {
|
372 |
0
| log.info("<cache>: Using cached version as instructed, useBody = false : " + actualKey);
|
373 |
| } |
374 |
| |
375 |
0
| body = content;
|
376 |
| } |
377 |
| |
378 |
| else { |
379 |
0
| if (log.isInfoEnabled()) {
|
380 |
0
| log.info("<cache>: Missing cached content : " + actualKey);
|
381 |
| } |
382 |
| |
383 |
0
| body = "Missing cached content";
|
384 |
| } |
385 |
| } |
386 |
| |
387 |
| |
388 |
0
| if (mode != SILENT_MODE) {
|
389 |
0
| bodyContent.clearBody();
|
390 |
0
| bodyContent.write(body);
|
391 |
0
| bodyContent.writeOut(bodyContent.getEnclosingWriter());
|
392 |
| } |
393 |
| } catch (java.io.IOException e) { |
394 |
0
| throw new JspTagException("IO Error: " + e.getMessage());
|
395 |
| } |
396 |
| |
397 |
0
| return SKIP_BODY;
|
398 |
| } |
399 |
| |
400 |
0
| public void doCatch(Throwable throwable) throws Throwable {
|
401 |
0
| throw throwable;
|
402 |
| } |
403 |
| |
404 |
| |
405 |
| |
406 |
| |
407 |
| |
408 |
| |
409 |
| |
410 |
0
| public int doEndTag() throws JspTagException {
|
411 |
0
| return EVAL_PAGE;
|
412 |
| } |
413 |
| |
414 |
0
| public void doFinally() {
|
415 |
0
| if (cancelUpdateRequired && (actualKey != null)) {
|
416 |
0
| cache.cancelUpdate(actualKey);
|
417 |
| } |
418 |
| |
419 |
| |
420 |
0
| groups = null;
|
421 |
0
| scope = PageContext.APPLICATION_SCOPE;
|
422 |
0
| cron = null;
|
423 |
0
| key = null;
|
424 |
0
| language = null;
|
425 |
0
| refreshPolicyClass = null;
|
426 |
0
| refreshPolicyParam = null;
|
427 |
0
| time = DEFAULT_TIMEOUT;
|
428 |
0
| refresh = false;
|
429 |
0
| mode = 0;
|
430 |
| } |
431 |
| |
432 |
| |
433 |
| |
434 |
| |
435 |
| |
436 |
| |
437 |
| |
438 |
| |
439 |
| |
440 |
| |
441 |
| |
442 |
| |
443 |
0
| public int doStartTag() throws JspTagException {
|
444 |
0
| cancelUpdateRequired = false;
|
445 |
0
| useBody = true;
|
446 |
0
| content = null;
|
447 |
| |
448 |
| |
449 |
0
| int returnCode = EVAL_BODY_BUFFERED;
|
450 |
| |
451 |
0
| if (admin == null) {
|
452 |
0
| admin = ServletCacheAdministrator.getInstance(pageContext.getServletContext());
|
453 |
| } |
454 |
| |
455 |
| |
456 |
0
| if (scope == PageContext.SESSION_SCOPE) {
|
457 |
0
| cache = admin.getSessionScopeCache(((HttpServletRequest) pageContext.getRequest()).getSession(true));
|
458 |
| } else { |
459 |
0
| cache = admin.getAppScopeCache(pageContext.getServletContext());
|
460 |
| } |
461 |
| |
462 |
| |
463 |
| |
464 |
| |
465 |
0
| String suffix = null;
|
466 |
| |
467 |
0
| if (key == null) {
|
468 |
0
| synchronized (pageContext.getRequest()) {
|
469 |
0
| Object o = pageContext.getRequest().getAttribute(CACHE_TAG_COUNTER_KEY);
|
470 |
| |
471 |
0
| if (o == null) {
|
472 |
0
| suffix = "1";
|
473 |
| } else { |
474 |
0
| suffix = Integer.toString(Integer.parseInt((String) o) + 1);
|
475 |
| } |
476 |
| } |
477 |
| |
478 |
0
| pageContext.getRequest().setAttribute(CACHE_TAG_COUNTER_KEY, suffix);
|
479 |
| } |
480 |
| |
481 |
| |
482 |
0
| actualKey = admin.generateEntryKey(key, (HttpServletRequest) pageContext.getRequest(), scope, language, suffix);
|
483 |
| |
484 |
| |
485 |
| |
486 |
| |
487 |
| |
488 |
| |
489 |
| |
490 |
| |
491 |
| |
492 |
0
| try {
|
493 |
0
| if (refresh) {
|
494 |
| |
495 |
0
| content = (String) cache.getFromCache(actualKey, 0, cron);
|
496 |
| } else { |
497 |
| |
498 |
0
| content = (String) cache.getFromCache(actualKey, time, cron);
|
499 |
| } |
500 |
| |
501 |
0
| try {
|
502 |
0
| if (log.isDebugEnabled()) {
|
503 |
0
| log.debug("<cache>: Using cached entry : " + actualKey);
|
504 |
| } |
505 |
| |
506 |
| |
507 |
0
| if ((content != null)) {
|
508 |
0
| if (mode != SILENT_MODE) {
|
509 |
0
| pageContext.getOut().write(content);
|
510 |
| } |
511 |
| |
512 |
0
| returnCode = SKIP_BODY;
|
513 |
| } |
514 |
| } catch (IOException e) { |
515 |
0
| throw new JspTagException("IO Exception: " + e.getMessage());
|
516 |
| } |
517 |
| } catch (NeedsRefreshException nre) { |
518 |
0
| cancelUpdateRequired = true;
|
519 |
0
| content = (String) nre.getCacheContent();
|
520 |
| } |
521 |
| |
522 |
0
| if (returnCode == EVAL_BODY_BUFFERED) {
|
523 |
0
| if (log.isDebugEnabled()) {
|
524 |
0
| log.debug("<cache>: Cached content not used: New cache entry, cache stale or scope flushed : " + actualKey);
|
525 |
| } |
526 |
| } |
527 |
| |
528 |
0
| return returnCode;
|
529 |
| } |
530 |
| |
531 |
| |
532 |
| |
533 |
| |
534 |
| |
535 |
| |
536 |
| |
537 |
| |
538 |
| |
539 |
| |
540 |
| |
541 |
| |
542 |
| |
543 |
| |
544 |
0
| private int parseDuration(String duration) {
|
545 |
0
| int time = 0;
|
546 |
| |
547 |
| |
548 |
0
| try {
|
549 |
0
| time = Integer.parseInt(duration);
|
550 |
| } catch (Exception ex) { |
551 |
| |
552 |
0
| for (int i = 0; i < duration.length(); i++) {
|
553 |
0
| if (!Character.isDigit(duration.charAt(i))) {
|
554 |
0
| time = Integer.parseInt(duration.substring(0, i));
|
555 |
| |
556 |
0
| switch ((int) duration.charAt(i)) {
|
557 |
0
| case (int) 's':
|
558 |
0
| time *= SECOND;
|
559 |
0
| break;
|
560 |
0
| case (int) 'm':
|
561 |
0
| time *= MINUTE;
|
562 |
0
| break;
|
563 |
0
| case (int) 'h':
|
564 |
0
| time *= HOUR;
|
565 |
0
| break;
|
566 |
0
| case (int) 'd':
|
567 |
0
| time *= DAY;
|
568 |
0
| break;
|
569 |
0
| case (int) 'w':
|
570 |
0
| time *= WEEK;
|
571 |
0
| break;
|
572 |
0
| default:
|
573 |
| |
574 |
| } |
575 |
| |
576 |
0
| break;
|
577 |
| } |
578 |
| |
579 |
| |
580 |
| } |
581 |
| |
582 |
| |
583 |
| } |
584 |
| |
585 |
| |
586 |
0
| return time;
|
587 |
| } |
588 |
| |
589 |
| |
590 |
| |
591 |
| |
592 |
| |
593 |
| |
594 |
| |
595 |
| |
596 |
0
| private int parseISO_8601_Duration(String duration) throws Exception {
|
597 |
0
| int years = 0;
|
598 |
0
| int months = 0;
|
599 |
0
| int days = 0;
|
600 |
0
| int hours = 0;
|
601 |
0
| int mins = 0;
|
602 |
0
| int secs = 0;
|
603 |
| |
604 |
| |
605 |
| |
606 |
0
| int index = duration.indexOf("-");
|
607 |
| |
608 |
0
| if (index > 0) {
|
609 |
0
| throw new Exception("Invalid duration (- must be at the beginning)");
|
610 |
| } |
611 |
| |
612 |
| |
613 |
0
| String workValue = duration.substring(index + 1);
|
614 |
| |
615 |
0
| if (workValue.charAt(0) != 'P') {
|
616 |
0
| throw new Exception("Invalid duration (P must be at the beginning)");
|
617 |
| } |
618 |
| |
619 |
| |
620 |
0
| workValue = workValue.substring(1);
|
621 |
| |
622 |
0
| if (workValue.length() == 0) {
|
623 |
0
| throw new Exception("Invalid duration (nothing specified)");
|
624 |
| } |
625 |
| |
626 |
| |
627 |
0
| index = workValue.indexOf('T');
|
628 |
| |
629 |
0
| String timeString = "";
|
630 |
| |
631 |
0
| if (index > 0) {
|
632 |
0
| timeString = workValue.substring(index + 1);
|
633 |
| |
634 |
| |
635 |
0
| if (timeString.equals("")) {
|
636 |
0
| throw new Exception("Invalid duration (T with no time)");
|
637 |
| } |
638 |
| |
639 |
0
| workValue = workValue.substring(0, index);
|
640 |
0
| } else if (index == 0) {
|
641 |
0
| timeString = workValue.substring(1);
|
642 |
0
| workValue = "";
|
643 |
| } |
644 |
| |
645 |
0
| if (!workValue.equals("")) {
|
646 |
0
| validateDateFormat(workValue);
|
647 |
| |
648 |
0
| int yearIndex = workValue.indexOf('Y');
|
649 |
0
| int monthIndex = workValue.indexOf('M');
|
650 |
0
| int dayIndex = workValue.indexOf('D');
|
651 |
| |
652 |
0
| if ((yearIndex != -1) && (monthIndex != -1) && (yearIndex > monthIndex)) {
|
653 |
0
| throw new Exception("Invalid duration (Date part not properly specified)");
|
654 |
| } |
655 |
| |
656 |
0
| if ((yearIndex != -1) && (dayIndex != -1) && (yearIndex > dayIndex)) {
|
657 |
0
| throw new Exception("Invalid duration (Date part not properly specified)");
|
658 |
| } |
659 |
| |
660 |
0
| if ((dayIndex != -1) && (monthIndex != -1) && (monthIndex > dayIndex)) {
|
661 |
0
| throw new Exception("Invalid duration (Date part not properly specified)");
|
662 |
| } |
663 |
| |
664 |
0
| if (yearIndex >= 0) {
|
665 |
0
| years = (new Integer(workValue.substring(0, yearIndex))).intValue();
|
666 |
| } |
667 |
| |
668 |
0
| if (monthIndex >= 0) {
|
669 |
0
| months = (new Integer(workValue.substring(yearIndex + 1, monthIndex))).intValue();
|
670 |
| } |
671 |
| |
672 |
0
| if (dayIndex >= 0) {
|
673 |
0
| if (monthIndex >= 0) {
|
674 |
0
| days = (new Integer(workValue.substring(monthIndex + 1, dayIndex))).intValue();
|
675 |
| } else { |
676 |
0
| if (yearIndex >= 0) {
|
677 |
0
| days = (new Integer(workValue.substring(yearIndex + 1, dayIndex))).intValue();
|
678 |
| } else { |
679 |
0
| days = (new Integer(workValue.substring(0, dayIndex))).intValue();
|
680 |
| } |
681 |
| } |
682 |
| } |
683 |
| } |
684 |
| |
685 |
0
| if (!timeString.equals("")) {
|
686 |
0
| validateHourFormat(timeString);
|
687 |
| |
688 |
0
| int hourIndex = timeString.indexOf('H');
|
689 |
0
| int minuteIndex = timeString.indexOf('M');
|
690 |
0
| int secondIndex = timeString.indexOf('S');
|
691 |
| |
692 |
0
| if ((hourIndex != -1) && (minuteIndex != -1) && (hourIndex > minuteIndex)) {
|
693 |
0
| throw new Exception("Invalid duration (Time part not properly specified)");
|
694 |
| } |
695 |
| |
696 |
0
| if ((hourIndex != -1) && (secondIndex != -1) && (hourIndex > secondIndex)) {
|
697 |
0
| throw new Exception("Invalid duration (Time part not properly specified)");
|
698 |
| } |
699 |
| |
700 |
0
| if ((secondIndex != -1) && (minuteIndex != -1) && (minuteIndex > secondIndex)) {
|
701 |
0
| throw new Exception("Invalid duration (Time part not properly specified)");
|
702 |
| } |
703 |
| |
704 |
0
| if (hourIndex >= 0) {
|
705 |
0
| hours = (new Integer(timeString.substring(0, hourIndex))).intValue();
|
706 |
| } |
707 |
| |
708 |
0
| if (minuteIndex >= 0) {
|
709 |
0
| mins = (new Integer(timeString.substring(hourIndex + 1, minuteIndex))).intValue();
|
710 |
| } |
711 |
| |
712 |
0
| if (secondIndex >= 0) {
|
713 |
0
| if (timeString.length() != (secondIndex + 1)) {
|
714 |
0
| throw new Exception("Invalid duration (Time part not properly specified)");
|
715 |
| } |
716 |
| |
717 |
0
| if (minuteIndex >= 0) {
|
718 |
0
| timeString = timeString.substring(minuteIndex + 1, timeString.length() - 1);
|
719 |
| } else { |
720 |
0
| if (hourIndex >= 0) {
|
721 |
0
| timeString = timeString.substring(hourIndex + 1, timeString.length() - 1);
|
722 |
| } else { |
723 |
0
| timeString = timeString.substring(0, timeString.length() - 1);
|
724 |
| } |
725 |
| } |
726 |
| |
727 |
0
| if (timeString.indexOf('.') == (timeString.length() - 1)) {
|
728 |
0
| throw new Exception("Invalid duration (Time part not properly specified)");
|
729 |
| } |
730 |
| |
731 |
0
| secs = (new Double(timeString)).intValue();
|
732 |
| } |
733 |
| } |
734 |
| |
735 |
| |
736 |
0
| return secs + (mins * MINUTE) + (hours * HOUR) + (days * DAY) + (months * MONTH) + (years * YEAR);
|
737 |
| } |
738 |
| |
739 |
| |
740 |
| |
741 |
| |
742 |
| |
743 |
| |
744 |
| |
745 |
0
| private void validateDateFormat(String basicDate) throws Exception {
|
746 |
0
| int yearCounter = 0;
|
747 |
0
| int monthCounter = 0;
|
748 |
0
| int dayCounter = 0;
|
749 |
| |
750 |
0
| for (int counter = 0; counter < basicDate.length(); counter++) {
|
751 |
| |
752 |
0
| if (!Character.isDigit(basicDate.charAt(counter)) && (basicDate.charAt(counter) != 'Y') && (basicDate.charAt(counter) != 'M') && (basicDate.charAt(counter) != 'D')) {
|
753 |
0
| throw new Exception("Invalid duration (Date part not properly specified)");
|
754 |
| } |
755 |
| |
756 |
| |
757 |
0
| if (basicDate.charAt(counter) == 'Y') {
|
758 |
0
| yearCounter++;
|
759 |
| } |
760 |
| |
761 |
0
| if (basicDate.charAt(counter) == 'M') {
|
762 |
0
| monthCounter++;
|
763 |
| } |
764 |
| |
765 |
0
| if (basicDate.charAt(counter) == 'D') {
|
766 |
0
| dayCounter++;
|
767 |
| } |
768 |
| } |
769 |
| |
770 |
0
| if ((yearCounter > 1) || (monthCounter > 1) || (dayCounter > 1)) {
|
771 |
0
| throw new Exception("Invalid duration (Date part not properly specified)");
|
772 |
| } |
773 |
| } |
774 |
| |
775 |
| |
776 |
| |
777 |
| |
778 |
| |
779 |
| |
780 |
| |
781 |
0
| private void validateHourFormat(String basicHour) throws Exception {
|
782 |
0
| int minuteCounter = 0;
|
783 |
0
| int secondCounter = 0;
|
784 |
0
| int hourCounter = 0;
|
785 |
| |
786 |
0
| for (int counter = 0; counter < basicHour.length(); counter++) {
|
787 |
0
| if (!Character.isDigit(basicHour.charAt(counter)) && (basicHour.charAt(counter) != 'H') && (basicHour.charAt(counter) != 'M') && (basicHour.charAt(counter) != 'S') && (basicHour.charAt(counter) != '.')) {
|
788 |
0
| throw new Exception("Invalid duration (Time part not properly specified)");
|
789 |
| } |
790 |
| |
791 |
0
| if (basicHour.charAt(counter) == 'H') {
|
792 |
0
| hourCounter++;
|
793 |
| } |
794 |
| |
795 |
0
| if (basicHour.charAt(counter) == 'M') {
|
796 |
0
| minuteCounter++;
|
797 |
| } |
798 |
| |
799 |
0
| if (basicHour.charAt(counter) == 'S') {
|
800 |
0
| secondCounter++;
|
801 |
| } |
802 |
| } |
803 |
| |
804 |
0
| if ((hourCounter > 1) || (minuteCounter > 1) || (secondCounter > 1)) {
|
805 |
0
| throw new Exception("Invalid duration (Time part not properly specified)");
|
806 |
| } |
807 |
| } |
808 |
| } |