Java Objects |
Active Web |
The script language allows you to create and use any Java object.
Active Web is deployed as a Web Application and as such adheres to the Web Application Archive packaging standard as defined by Sun Microsystems. The Active Web Runtime is contained within the concerto.jar file; which can be found in the WEB-INF/lib directory of the distribution WAR file.
The Web Application Archive standard allows Java classes and libraries to be added into a WAR file in either the WEB-INF/classes or WEB-INF/lib directory; the web application container knows to look in these locations for Servlet’s referenced by the web application on deployment.
To make your Java objects available to Active Web you first need to put your java classes in either the classes directory of the Active Web Universal WAR file (maintaining the package hierarchy) or into your own Java Archive (JAR) file which you place in the lib directory.
Example1: If a Java file called 'MyTest1.java' has the following package statement 'package com.mycompany.test;' then the Java class file should be in the directory 'WEB-INF\classes\com\mycompany\test' (you will probably have to create some of these directories).
Example2: If a Java file called 'MyTest2.java' has no package statement then the Java class file should be in the directory 'WEB-INF\classes'.
To create a Java object in the script you use the 'new' operator. You must include the full Java package name when creating your Java object.
var myJavaObject = new com.mycompany.test.MyTest1();
In general the script compiler will assume that any reference that begins with a name of 'com', 'org' or 'java' is a Java reference. If you want to force any other reference to be a Java one then put 'Packages.' in front of the reference. The script interpreter will recognise this as a Java reference and remove it before looking for the Java file.
var myJavaObject = new Packages.MyTest2();
When you create a Java object the script maintains a reference to the real Java object and you can run any public method or access any public property inside the Java object. Java objects can also be used in method or function calls.
Note: Once a Java Class has been loaded by Active Web any changes made to the class file will not be reflected by Active Web; you will need to restart the Active Web application for the changes to take effect.
When objects are passed between the script world and the Java world (e.g. parameters for a method call), then they are automatically converted if required.
A script string is converted to a Java String or Object.
A script number is converted to a Java byte, char, int, long, double or float depending on what is required.
A script boolean is converted to a Java boolean.
A script null is converted to a Java null.
A script array will be converted to the appropriate Java array.
When a Java method is called the script interpreter checks what parameters have been supplied and looks for a 'best match' in the Java object. In most cases this will be obvious but because Java methods (unlike script functions) can have the same name but with different parameters, it is sometimes possible to have two or more methods that match the call. In this case which method is called is undefined, one of them will be called but it is not possible to say which one.
For example: If the methods 'myTest(int count)' and 'myTest(double count)' are defined in a Java object then the function call 'myJavaObject.myTest(6)' will match both methods as the script number can be converted to an int or a double.
A Java method that has no parameters can be called as normal but can also be referenced as a property. Using the name of the method as a property reference on the Java object automatically calls the method and returns the value returned by the call.
An object returned by a Java method call is always a Java object and is thus never primitive. This can be important for assignment and function calls as Java objects are always assigned by reference and not by value.
When you call a Java method; be it in your own Java Objects or one from the Java Runtime Class Library; there is a potential for an exception to be thrown. In previous versions of Active Web and Concerto this would cause the executing page to abort with a runtime error; the exception message would then be written to the Server or Servlet error log.
Active Web has enhanced support for exceptions thrown from Java method calls. When an exception occurs, Active Web will catch it and present it as a property (javaException) of the object whose method has been called. The developer can then take appropriate action depending on the contents of the Java exception object.
When Java objects are used in such a way that their value is required (e.g. a Java object is put in a string, or multiplied by a number) then the following rules are used for obtaining the object value:
If the Java object is a String object use its string.
For all other objects call the 'toString()' method on the object and use the string returned.
If the Java object is a numeric (byte, char, int, long, double or float) use its number.
If the Java object is a String that can be converted to a number then use it.
For all other objects use NaN.
If the Java object is boolean use its value.
If the Java object is numeric use 'true' if the number value is not zero.
If the Java object is a String use 'true' if the string length is greater than zero.
For all other objects use 'true' if the object is not null.
Topic ID: 150067