001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Copyright 2007 Dennis Reil
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021: package com.izforge.izpack.rules;
022:
023: import java.lang.reflect.Field;
024: import java.lang.reflect.Method;
025: import java.util.Properties;
026:
027: import net.n3.nanoxml.XMLElement;
028: import com.izforge.izpack.util.Debug;
029:
030: /**
031: * A condition based on the value of a static java field or static java method.
032: *
033: * @author Dennis Reil, <Dennis.Reil@reddot.de>
034: */
035: public class JavaCondition extends Condition {
036: /**
037: *
038: */
039: private static final long serialVersionUID = -7649870719815066537L;
040: protected String classname;
041: protected String methodname;
042: protected String fieldname;
043: protected boolean complete;
044: protected String returnvalue;
045: protected String returnvaluetype;
046:
047: protected Class usedclass;
048: protected Field usedfield;
049: protected Method usedmethod;
050:
051: public JavaCondition() {
052:
053: }
054:
055: private boolean isTrue(Properties variables) {
056: if (!this .complete) {
057: return false;
058: } else {
059: if (this .usedclass == null) {
060: ClassLoader loader = ClassLoader.getSystemClassLoader();
061: try {
062: this .usedclass = loader.loadClass(this .classname);
063: } catch (ClassNotFoundException e) {
064: Debug.log("Can't find class " + this .classname);
065: return false;
066: }
067: }
068: if ((this .usedfield == null) && (this .fieldname != null)) {
069: try {
070: this .usedfield = this .usedclass
071: .getField(this .fieldname);
072: } catch (SecurityException e) {
073: Debug
074: .log("No permission to access specified field: "
075: + this .fieldname);
076: return false;
077: } catch (NoSuchFieldException e) {
078: Debug.log("No such field: " + this .fieldname);
079: return false;
080: }
081: }
082: if ((this .usedmethod == null) && (this .methodname != null)) {
083: Debug.log("not implemented yet.");
084: return false;
085: }
086:
087: if (this .usedfield != null) {
088: // access field
089: if ("boolean".equals(this .returnvaluetype)) {
090: try {
091: boolean returnval = this .usedfield
092: .getBoolean(null);
093: boolean expectedreturnval = Boolean
094: .valueOf(this .returnvalue);
095: return returnval == expectedreturnval;
096: } catch (IllegalArgumentException e) {
097: Debug.log("IllegalArgumentexeption "
098: + this .fieldname);
099: } catch (IllegalAccessException e) {
100: Debug.log("IllegalAccessException "
101: + this .fieldname);
102: }
103: } else {
104: Debug.log("not implemented yet.");
105: return false;
106: }
107: }
108: return false;
109: }
110: }
111:
112: public void readFromXML(XMLElement xmlcondition) {
113: if (xmlcondition.getChildrenCount() != 2) {
114: Debug
115: .log("Condition of type java needs (java,returnvalue)");
116: return;
117: }
118: XMLElement javael = xmlcondition.getFirstChildNamed("java");
119: XMLElement classel = javael.getFirstChildNamed("class");
120: if (classel != null) {
121: this .classname = classel.getContent();
122: } else {
123: Debug.log("Java-Element needs (class,method?,field?)");
124: return;
125: }
126: XMLElement methodel = javael.getFirstChildNamed("method");
127: if (methodel != null) {
128: this .methodname = methodel.getContent();
129: }
130: XMLElement fieldel = javael.getFirstChildNamed("field");
131: if (fieldel != null) {
132: this .fieldname = fieldel.getContent();
133: }
134: if ((this .methodname == null) && (this .fieldname == null)) {
135: Debug.log("java element needs (class, method?,field?)");
136: return;
137: }
138: XMLElement returnvalel = xmlcondition
139: .getFirstChildNamed("returnvalue");
140: if (returnvalel != null) {
141: this .returnvalue = returnvalel.getContent();
142: this .returnvaluetype = returnvalel.getAttribute("type");
143: } else {
144: Debug.log("no returnvalue-element specified.");
145: return;
146: }
147: this .complete = true;
148: }
149:
150: public boolean isTrue() {
151: return this .isTrue(this .installdata.getVariables());
152: }
153:
154: /* (non-Javadoc)
155: * @see com.izforge.izpack.rules.Condition#getDependenciesDetails()
156: */
157: public String getDependenciesDetails() {
158: StringBuffer details = new StringBuffer();
159: details.append(this .id);
160: details.append(" depends on the ");
161: if (this .fieldname != null) {
162: details.append("value of field <b>");
163: details.append(this .fieldname);
164: details.append("</b>");
165: } else {
166: details.append("return value of method <b>");
167: details.append(this .methodname);
168: details.append("</b>");
169: }
170: details.append(" on an instance of class <b>");
171: details.append(this .classname);
172: details.append("</b> which should be <b>");
173: details.append(this .returnvalue);
174: details.append("</b><br/>");
175: return details.toString();
176: }
177:
178: }
|