KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > workflow > status > support > AbstractStatusMonitor


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.riot.workflow.status.support;
25
26 import java.util.Collection JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Locale JavaDoc;
29
30 import org.riotfamily.common.util.FormatUtils;
31 import org.riotfamily.riot.workflow.status.StatusMessage;
32 import org.riotfamily.riot.workflow.status.StatusMonitor;
33 import org.springframework.beans.factory.BeanNameAware;
34 import org.springframework.context.MessageSource;
35 import org.springframework.context.MessageSourceAware;
36
37 /**
38  * Convenience base class for {@link StatusMonitor} implementations.
39  *
40  * @author Felix Gnass [fgnass at neteye dot de]
41  */

42 public abstract class AbstractStatusMonitor implements StatusMonitor,
43         MessageSourceAware, BeanNameAware {
44
45     private MessageSource messageSource;
46     
47     private String JavaDoc messageKey;
48     
49     private String JavaDoc link;
50
51     private long cacheMillis;
52     
53     private long lastUpdate;
54     
55     private Object JavaDoc[] args;
56     
57     private boolean hideZeroStatus;
58     
59     public void setMessageSource(MessageSource messageSource) {
60         this.messageSource = messageSource;
61     }
62
63     public void setMessageKey(String JavaDoc messageKey) {
64         this.messageKey = messageKey;
65     }
66
67     public boolean isHideZeroStatus() {
68         return this.hideZeroStatus;
69     }
70
71     public void setHideZeroStatus(boolean hideZeroStatus) {
72         this.hideZeroStatus = hideZeroStatus;
73     }
74
75     public void setBeanName(String JavaDoc name) {
76         if (messageKey == null) {
77             messageKey = name;
78         }
79     }
80     
81     public void setLink(String JavaDoc link) {
82         this.link = link;
83     }
84     
85     public void setCache(String JavaDoc period) {
86         cacheMillis = FormatUtils.parseMillis(period);
87     }
88     
89     public Collection JavaDoc getMessages(Locale JavaDoc locale) {
90         updateArgs();
91         if (isVisible(args)) {
92             String JavaDoc message = messageSource.getMessage(messageKey, args, locale);
93             return Collections.singleton(new StatusMessage(message, link));
94         }
95         return null;
96     }
97
98     private synchronized void updateArgs() {
99         if (lastUpdate + cacheMillis < System.currentTimeMillis()) {
100             this.args = FormatUtils.htmlEscapeArgs(getArgs());
101         }
102     }
103     
104     protected boolean isVisible(Object JavaDoc[] args) {
105         if (args == null || args.length == 0) {
106             return false;
107         }
108         if (hideZeroStatus && args[0] instanceof Number JavaDoc) {
109             return ((Number JavaDoc) args[0]).intValue() > 0;
110         }
111         return true;
112     }
113     
114     /**
115      * Subclasses must return an array of objects which will be passed to
116      * the MessageSource as arguments. All arguments that are neither
117      * primitive wrappers nor dates will be HTML-escaped automatically to
118      * prevent XSS attacks.
119      * @see FormatUtils#htmlEscapeArgs(Object[])
120      */

121     protected abstract Object JavaDoc[] getArgs();
122     
123 }
124
Popular Tags