1 21 package com.izforge.izpack.rules; 22 23 import java.lang.reflect.Field ; 24 import java.lang.reflect.Method ; 25 import java.util.Properties ; 26 27 import net.n3.nanoxml.XMLElement; 28 import com.izforge.izpack.util.Debug; 29 30 35 public class JavaCondition extends Condition { 36 protected String classname; 37 protected String methodname; 38 protected String fieldname; 39 protected boolean complete; 40 protected String returnvalue; 41 protected String returnvaluetype; 42 43 44 protected Class usedclass; 45 protected Field usedfield; 46 protected Method usedmethod; 47 48 public JavaCondition() { 49 50 } 51 52 private boolean isTrue(Properties variables) { 53 if (!this.complete) { 54 return false; 55 } else { 56 if (this.usedclass == null) { 57 ClassLoader loader = ClassLoader.getSystemClassLoader(); 58 try { 59 this.usedclass = loader.loadClass(this.classname); 60 } catch (ClassNotFoundException e) { 61 Debug.log("Can't find class " + this.classname); 62 return false; 63 } 64 } 65 if ((this.usedfield == null) && (this.fieldname != null)) { 66 try { 67 this.usedfield = this.usedclass.getField(this.fieldname); 68 } catch (SecurityException e) { 69 Debug.log("No permission to access specified field: " + this.fieldname); 70 return false; 71 } catch (NoSuchFieldException e) { 72 Debug.log("No such field: " + this.fieldname); 73 return false; 74 } 75 } 76 if ((this.usedmethod == null) && (this.methodname != null)) { 77 Debug.log("not implemented yet."); 78 return false; 79 } 80 81 if (this.usedfield != null) { 82 if ("boolean".equals(this.returnvaluetype)) { 84 try { 85 boolean returnval = this.usedfield.getBoolean(null); 86 boolean expectedreturnval = Boolean.valueOf(this.returnvalue).booleanValue(); 87 return returnval == expectedreturnval; 88 } catch (IllegalArgumentException e) { 89 Debug.log("IllegalArgumentexeption " + this.fieldname); 90 } catch (IllegalAccessException e) { 91 Debug.log("IllegalAccessException " + this.fieldname); 92 } 93 } else { 94 Debug.log("not implemented yet."); 95 return false; 96 } 97 } 98 return false; 99 } 100 } 101 102 public void readFromXML(XMLElement xmlcondition) { 103 if (xmlcondition.getChildrenCount() != 2) { 104 Debug.log("Condition of type java needs (java,returnvalue)"); 105 return; 106 } 107 XMLElement javael = xmlcondition.getFirstChildNamed("java"); 108 XMLElement classel = javael.getFirstChildNamed("class"); 109 if (classel != null) { 110 this.classname = classel.getContent(); 111 } else { 112 Debug.log("Java-Element needs (class,method?,field?)"); 113 return; 114 } 115 XMLElement methodel = javael.getFirstChildNamed("method"); 116 if (methodel != null) { 117 this.methodname = methodel.getContent(); 118 } 119 XMLElement fieldel = javael.getFirstChildNamed("field"); 120 if (fieldel != null) { 121 this.fieldname = fieldel.getContent(); 122 } 123 if ((this.methodname == null) && (this.fieldname == null)) { 124 Debug.log("java element needs (class, method?,field?)"); 125 return; 126 } 127 XMLElement returnvalel = xmlcondition.getFirstChildNamed("returnvalue"); 128 if (returnvalel != null) { 129 this.returnvalue = returnvalel.getContent(); 130 this.returnvaluetype = returnvalel.getAttribute("type"); 131 } else { 132 Debug.log("no returnvalue-element specified."); 133 return; 134 } 135 this.complete = true; 136 } 137 138 public boolean isTrue() 139 { 140 return this.isTrue(this.installdata.getVariables()); 141 } 142 143 } 144 | Popular Tags |