com.izforge.izpack.rules.JavaCondition Maven / Gradle / Ivy
/*
* IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
*
* http://izpack.org/
* http://izpack.codehaus.org/
*
* Copyright 2007 Dennis Reil
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.izforge.izpack.rules;
import com.izforge.izpack.util.Debug;
import net.n3.nanoxml.XMLElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Properties;
/**
* A condition based on the value of a static java field or static java method.
*
* @author Dennis Reil,
*/
public class JavaCondition extends Condition
{
/**
*
*/
private static final long serialVersionUID = -7649870719815066537L;
protected String classname;
protected String methodname;
protected String fieldname;
protected boolean complete;
protected String returnvalue;
protected String returnvaluetype;
protected Class usedclass;
protected Field usedfield;
protected Method usedmethod;
public JavaCondition()
{
}
private boolean isTrue(Properties variables)
{
if (!this.complete)
{
return false;
}
else
{
if (this.usedclass == null)
{
ClassLoader loader = ClassLoader.getSystemClassLoader();
try
{
this.usedclass = loader.loadClass(this.classname);
}
catch (ClassNotFoundException e)
{
Debug.log("Can't find class " + this.classname);
return false;
}
}
if ((this.usedfield == null) && (this.fieldname != null))
{
try
{
this.usedfield = this.usedclass.getField(this.fieldname);
}
catch (SecurityException e)
{
Debug.log("No permission to access specified field: " + this.fieldname);
return false;
}
catch (NoSuchFieldException e)
{
Debug.log("No such field: " + this.fieldname);
return false;
}
}
if ((this.usedmethod == null) && (this.methodname != null))
{
Debug.log("not implemented yet.");
return false;
}
if (this.usedfield != null)
{
// access field
if ("boolean".equals(this.returnvaluetype))
{
try
{
boolean returnval = this.usedfield.getBoolean(null);
boolean expectedreturnval = Boolean.valueOf(this.returnvalue);
return returnval == expectedreturnval;
}
catch (IllegalArgumentException e)
{
Debug.log("IllegalArgumentexeption " + this.fieldname);
}
catch (IllegalAccessException e)
{
Debug.log("IllegalAccessException " + this.fieldname);
}
}
else
{
Debug.log("not implemented yet.");
return false;
}
}
return false;
}
}
public void readFromXML(XMLElement xmlcondition)
{
if (xmlcondition.getChildrenCount() != 2)
{
Debug.log("Condition of type java needs (java,returnvalue)");
return;
}
XMLElement javael = xmlcondition.getFirstChildNamed("java");
XMLElement classel = javael.getFirstChildNamed("class");
if (classel != null)
{
this.classname = classel.getContent();
}
else
{
Debug.log("Java-Element needs (class,method?,field?)");
return;
}
XMLElement methodel = javael.getFirstChildNamed("method");
if (methodel != null)
{
this.methodname = methodel.getContent();
}
XMLElement fieldel = javael.getFirstChildNamed("field");
if (fieldel != null)
{
this.fieldname = fieldel.getContent();
}
if ((this.methodname == null) && (this.fieldname == null))
{
Debug.log("java element needs (class, method?,field?)");
return;
}
XMLElement returnvalel = xmlcondition.getFirstChildNamed("returnvalue");
if (returnvalel != null)
{
this.returnvalue = returnvalel.getContent();
this.returnvaluetype = returnvalel.getAttribute("type");
}
else
{
Debug.log("no returnvalue-element specified.");
return;
}
this.complete = true;
}
public boolean isTrue()
{
return this.isTrue(this.installdata.getVariables());
}
/* (non-Javadoc)
* @see com.izforge.izpack.rules.Condition#getDependenciesDetails()
*/
public String getDependenciesDetails()
{
StringBuffer details = new StringBuffer();
details.append(this.id);
details.append(" depends on the ");
if (this.fieldname != null)
{
details.append("value of field ");
details.append(this.fieldname);
details.append("");
}
else
{
details.append("return value of method ");
details.append(this.methodname);
details.append("");
}
details.append(" on an instance of class ");
details.append(this.classname);
details.append(" which should be ");
details.append(this.returnvalue);
details.append("
");
return details.toString();
}
}