Additional Methods for Reflection-Level Insertion of Variable Values From a Script ---------------------------------------------------------------------------------- Some of the Service methods require the code to be executed locally only. You therefore need to add some code to your Service-derived class, to make use of these methods. Fortunately, it can be added as is and is included below. Simply copy and paste this code into your Service-derived class to make full use of the licas functionality. You can also omit this code to use your service without it. These methods allow you to declare varibles in the admin script and either initialise them through the script or save their values permanently to a script, for reloading later. If you do not use this feature, you do not need to add them. NOTE: to use Reflection and the 'Service' default process, the variables must either be local to the class or public to the derived class set. Alternatively, you can simply parse/serialize the XML using a custom-made parser in your own methods, to set the values without using reflection. /** * Set actual values for variable instances if they are defined. *

* {@code NOTE:} you must add this method to your own derived class if you use it, * so that the private variables in that class can be accessed. It is ignored * unless it gets called through the script initialisation process. * @param instanceValues the admin script part that defines instance values. * @throws java.lang.Exception any error. */ protected void setInstanceValues(Element instanceValues) throws Exception { int i; String varName; //variable name Element instanceValue; //instance value Element nextElem; //next element Vector elemList; //list of elements Field instField; //instance field Object valueObj; //value object if (instanceValues != null) { elemList = instanceValues.getChildren(); for (i = 0; i < elemList.size(); i++) { instanceValue = (Element)elemList.elementAt(i); varName = instanceValue.getAttributeValue(Const.NAME); nextElem = instanceValue.getElementChildAtIndex(0); valueObj = Parsers.parse(nextElem); if (valueObj != null) { instField = ReflectionHandler.getInstanceField(this, varName); if (instField != null) { try { instField.set(this, valueObj); } catch (Exception ex) {} } } } } } /** * Get the value of the specified variable if it exists. *

* {@code NOTE:} you must add this method to your own derived class if you use it, * so that the private variables in that class can be accessed. It is ignored * unless it gets called through the script initialisation process. * @param varName the name of the variable to check for. * @throws java.lang.Exception any error. */ protected Object getInstanceValue(String varName) throws Exception { int i; Field nextField; //next field Field[] fields; //fields from this class Object fieldValue; //field value fieldValue = null; if ((varName != null) && !varName.equals("")) { fields = this.getClass().getDeclaredFields(); for (i = 0; (fieldValue == null) && (i < fields.length); i++) { nextField = fields[i]; if (nextField.getName().equalsIgnoreCase(varName)) { try { fieldValue = fields[i].get(this); } catch (Exception ex) { fieldValue = null; } } } if (fieldValue == null) { fields = this.getClass().getFields(); for (i = 0; (fieldValue == null) && (i < fields.length); i++) { nextField = fields[i]; if (nextField.getName().equalsIgnoreCase(varName)) { try { fieldValue = fields[i].get(this); } catch (Exception ex) { fieldValue = null; } } } } } return fieldValue; } /** * Set actual values for serialized variable instances if they are defined. *

* {@code NOTE:} you must add this method to your own derived class if you use it, * so that the private variables in that class can be accessed. * @param serializeXml the admin script part that defines serialize instance values. * @throws java.lang.Exception any error. */ protected void setSerializeValues(Element serializeXml) throws Exception { int i; String varName; //variable name Element nextElem; //next element Element nextElem2; //next element Vector elemList; //element list Field instField; //instance field Object valueObj; //value object if (serializeXml != null) { elemList = serializeXml.getChildren(); for (i = 0; i < elemList.size(); i++) { nextElem = (Element)elemList.elementAt(i); if (nextElem.getName().equals(Const.SERIALISEVALUE)) { varName = nextElem.getAttributeValue(Const.NAME); if (!varName.equals(MetaConst.ADMIN)) { nextElem2 = nextElem.getElementChildAtIndex(0); valueObj = Parsers.parse(nextElem2); if (valueObj != null) { instField = ReflectionHandler.getInstanceField(this, varName); if (instField != null) { try { instField.set(this, valueObj); } catch (Exception ex) {} } } } } } } }