KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > security > license > Limit


1 /*
2  * Created on Sep 14, 2003
3  *
4  * To change the template for this generated file go to
5  * Window>Preferences>Java>Code Generation>Code and Comments
6  */

7 package org.jahia.security.license;
8
9 import java.io.IOException JavaDoc;
10 import java.lang.reflect.Constructor JavaDoc;
11 import java.lang.reflect.InvocationTargetException JavaDoc;
12
13 import org.jahia.utils.xml.XmlWriter;
14 import org.jahia.resourcebundle.ResourceMessage;
15
16 /**
17  * Generic limit
18  * @author loom
19  *
20  * To change the template for this generated type comment go to
21  * Window>Preferences>Java>Code Generation>Code and Comments
22  */

23 public class Limit {
24
25     private static org.apache.log4j.Logger logger =
26         org.apache.log4j.Logger.getLogger(Limit.class);
27
28     private String JavaDoc klass;
29     private String JavaDoc name;
30     private String JavaDoc valueStr;
31     private LimitValue value;
32     private Validator validator;
33     private License license;
34     private boolean initialized = false;
35
36     /**
37      * @param name
38      * @param value
39      */

40     public Limit (String JavaDoc klass, String JavaDoc name, String JavaDoc value, License license) {
41         this.klass = klass;
42         this.name = name;
43         this.valueStr = value;
44         if (ListLimitValue.isListValue(value)) {
45             this.value = new ListLimitValue(value);
46         } else if (RangeLimitValue.isRangeValue(value)) {
47             this.value = new RangeLimitValue(value);
48         } else {
49             this.value = new LimitValue(value);
50         }
51         this.license = license;
52     }
53
54     private void init (String JavaDoc klass, String JavaDoc name, String JavaDoc value) {
55         String JavaDoc className = klass;
56         try {
57             Class JavaDoc validatorClass = Class.forName(className);
58             Class JavaDoc[] parameterTypes = new Class JavaDoc[3];
59             parameterTypes[0] = String JavaDoc.class;
60             parameterTypes[1] = String JavaDoc.class;
61             parameterTypes[2] = License.class;
62             Constructor JavaDoc validatorConstructor = validatorClass.getConstructor(
63                 parameterTypes);
64             Object JavaDoc[] initArgs = new Object JavaDoc[3];
65             initArgs[0] = name;
66             initArgs[1] = value;
67             initArgs[2] = license;
68             validator = (Validator) validatorConstructor.newInstance(initArgs);
69         } catch (ClassNotFoundException JavaDoc cnfe) {
70             logger.error("Couldn't find validator class " + klass, cnfe);
71             validator = null;
72         } catch (IllegalAccessException JavaDoc iae) {
73             logger.error("Error accessing validator class " + klass, iae);
74             validator = null;
75         } catch (InstantiationException JavaDoc ie) {
76             logger.error("Validation class " + klass +
77                          " instantiation exception", ie);
78             validator = null;
79         } catch (InvocationTargetException JavaDoc ite) {
80             logger.error(
81                 "Invocation target exception while invoking validator class " +
82                 klass, ite);
83             validator = null;
84         } catch (NoSuchMethodException JavaDoc nsme) {
85             logger.error(
86                 "No such method exception while creating instance of validator class " +
87                 klass, nsme);
88             validator = null;
89         }
90         initialized = true;
91     }
92
93     /**
94      * @return
95      */

96     public String JavaDoc getName () {
97         return name;
98     }
99
100     /**
101      * @return
102      */

103     public LimitValue getValue () {
104         return value;
105     }
106
107     public void toXML (XmlWriter xmlWriter)
108         throws IOException JavaDoc {
109         xmlWriter.writeEntity("limit");
110         xmlWriter.writeAttribute("class", klass);
111         xmlWriter.writeAttribute("name", name);
112         xmlWriter.writeAttribute("value", value.toXMLString());
113         xmlWriter.endEntity();
114     }
115
116     public boolean check () {
117         init(klass, name, valueStr);
118         if (validator == null) {
119             logger.warn(
120                 "Failing check because we couldn't instantiate the validator class " +
121                 klass);
122             return false;
123         }
124         if (!value.check(validator)) {
125             logger.error("Validator [" + name + "] value=[" + valueStr +
126                          "] failed check !");
127             return false;
128         }
129         return true;
130     }
131
132     public ResourceMessage getErrorMessage () {
133         if (validator != null) {
134             return value.getErrorMessage(validator);
135         } else {
136             return null;
137         }
138     }
139
140     public String JavaDoc toSignatureData () {
141         StringBuffer JavaDoc signDataBuf = new StringBuffer JavaDoc();
142         signDataBuf.append(klass);
143         signDataBuf.append("\n");
144         signDataBuf.append(name);
145         signDataBuf.append("\n");
146         signDataBuf.append(valueStr);
147         signDataBuf.append("\n");
148         return signDataBuf.toString();
149     }
150
151     public String JavaDoc getValueStr () {
152         return valueStr;
153     }
154
155     public License getLicense () {
156         return license;
157     }
158     public Validator getValidator() {
159         if (!initialized) {
160             init(klass, name, valueStr);
161         }
162         return validator;
163     }
164
165 }
Popular Tags