1 7 package org.jahia.security.license; 8 9 import java.io.IOException ; 10 import java.lang.reflect.Constructor ; 11 import java.lang.reflect.InvocationTargetException ; 12 13 import org.jahia.utils.xml.XmlWriter; 14 import org.jahia.resourcebundle.ResourceMessage; 15 16 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 klass; 29 private String name; 30 private String valueStr; 31 private LimitValue value; 32 private Validator validator; 33 private License license; 34 private boolean initialized = false; 35 36 40 public Limit (String klass, String name, String 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 klass, String name, String value) { 55 String className = klass; 56 try { 57 Class validatorClass = Class.forName(className); 58 Class [] parameterTypes = new Class [3]; 59 parameterTypes[0] = String .class; 60 parameterTypes[1] = String .class; 61 parameterTypes[2] = License.class; 62 Constructor validatorConstructor = validatorClass.getConstructor( 63 parameterTypes); 64 Object [] initArgs = new Object [3]; 65 initArgs[0] = name; 66 initArgs[1] = value; 67 initArgs[2] = license; 68 validator = (Validator) validatorConstructor.newInstance(initArgs); 69 } catch (ClassNotFoundException cnfe) { 70 logger.error("Couldn't find validator class " + klass, cnfe); 71 validator = null; 72 } catch (IllegalAccessException iae) { 73 logger.error("Error accessing validator class " + klass, iae); 74 validator = null; 75 } catch (InstantiationException ie) { 76 logger.error("Validation class " + klass + 77 " instantiation exception", ie); 78 validator = null; 79 } catch (InvocationTargetException ite) { 80 logger.error( 81 "Invocation target exception while invoking validator class " + 82 klass, ite); 83 validator = null; 84 } catch (NoSuchMethodException 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 96 public String getName () { 97 return name; 98 } 99 100 103 public LimitValue getValue () { 104 return value; 105 } 106 107 public void toXML (XmlWriter xmlWriter) 108 throws IOException { 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 toSignatureData () { 141 StringBuffer signDataBuf = new StringBuffer (); 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 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 |