KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > rules > JavaCondition


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2007 Dennis Reil
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21 package com.izforge.izpack.rules;
22
23 import java.lang.reflect.Field JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import net.n3.nanoxml.XMLElement;
28 import com.izforge.izpack.util.Debug;
29
30 /**
31  * A condition based on the value of a static java field or static java method.
32  *
33  * @author Dennis Reil, <Dennis.Reil@reddot.de>
34  */

35 public class JavaCondition extends Condition {
36     protected String JavaDoc classname;
37     protected String JavaDoc methodname;
38     protected String JavaDoc fieldname;
39     protected boolean complete;
40     protected String JavaDoc returnvalue;
41     protected String JavaDoc returnvaluetype;
42
43
44     protected Class JavaDoc usedclass;
45     protected Field JavaDoc usedfield;
46     protected Method JavaDoc usedmethod;
47
48     public JavaCondition() {
49
50     }
51
52     private boolean isTrue(Properties JavaDoc variables) {
53         if (!this.complete) {
54             return false;
55         } else {
56             if (this.usedclass == null) {
57                 ClassLoader JavaDoc loader = ClassLoader.getSystemClassLoader();
58                 try {
59                     this.usedclass = loader.loadClass(this.classname);
60                 } catch (ClassNotFoundException JavaDoc 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 JavaDoc e) {
69                     Debug.log("No permission to access specified field: " + this.fieldname);
70                     return false;
71                 } catch (NoSuchFieldException JavaDoc 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                 // access field
83
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 JavaDoc e) {
89                         Debug.log("IllegalArgumentexeption " + this.fieldname);
90                     } catch (IllegalAccessException JavaDoc 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