Chapter 7 PowerDynamo and Java


Using Java in a PowerDynamo script

Java classes can be stored either in the PowerDynamo Web site, a dynamic Web site, or somewhere on the file system. Importing a Java class into a PowerDynamo Web site is the same as importing any other file. Once the Java class is in an accessible location, you are ready to use the class in a Dynamo script.

Java classes can be accessed from within a database or within the file system. Depending on which of these locations you will be accessing your Java class from, you will have to perform certain steps. Generally, you should store Java classes within a database when you want to replicate the classes along with the database. If you don't need the Java classes stored in the database, you should probably store them in the file system for performance reasons.

Steps To call Java classes stored in the file system:

  1. Write and compile your Java code.

  2. Set the required Java VM from within the Sybase Central Configuration folder.

  3. Modify the CLASSPATH environment variable to point to the location of the Java class in the file system.

  4. Restart PowerDynamo.

  5. Write your PowerDynamo script.

  6. Execute.


Steps To call Java classes stored in a Dynamo Web site:

  1. Write and compile your Java code.

  2. Import the Java class into the database.

  3. Set the required Java VM from within the Sybase Central Configuration folder.

  4. Modify the Dynamo CLASSPATH in the Sybase Central Configuration folder to point to the imported Java class.

  5. Restart PowerDynamo.

  6. Write your PowerDynamo script.

  7. Execute.


Note  

File-hosted Web site
A Web site that is hosted in the file system can use either of these methods but storing Java classes in the file system probably produces more efficient results.

Here is a simple example of a Java class that performs basic string manipulation. Let's assume that the following Java class has been imported into our database-hosted Web site.

public class testclass {
public String str;
public testclass( String str1 ) { str = str1; }
public testclass() { str = "";}
public String getstr() { return str; }
public void setstr( String newstr ) { str = newstr; }
public String Upper() {
return Upper( str );
}
public String Upper( String str1 ) {
return str1.toUpperCase();
}
}

Let's also assume that we have chosen our Java VM and set the CLASSPATH variable using Sybase Central.

In our script we will use the Java.CreateObject method. The Java object manipulates Java classes within PowerDynamo scripts. The CreateObject method is just one of the Java objects methods and is used to instantiate a Java class so that it can be used as an object within your PowerDynamo script. The syntax for this method is:

java.CreateObject( class_name [, list of constructor parameters] )

The Java class we are going to use in our script is called testclass. It does not take any parameters. The proper syntax to instantiate testclass in our script is:

cls1 = java.CreateObject( "testclass" );

The Java class that we are instantiating has properties and methods associated with it. To use those properties within our Dynamo script, the Java class must contain both get and set functions for this property. For example, testclass has a property called str . When this property is used within a Dynamo script to display the value of the str property ( document.WriteLn( cls1.str ); ) in actuality, the getstr function from the Java class is called. The same holds true for setting a property; the setstr function would be called. If you look at the testclass code you will see that the str property has both get and set functions.

Working with methods of an instantiated Java class is the same as working with any DynaScript method.

Note  

Methods that take a Java class parameter
If you have a method that takes another Java class parameter, that class must be instantiated within the script before it can be used.

For further information about the Java object and its methods see "The java object" in PowerDynamo Reference.

The following is a Dynamo script that provided two strings for manipulation by the testclass object:

<!--SCRIPT

string1 = "This is my very long test string ";
string2 = "that I am using"

// The Java class is instantiated
cls1 = java.CreateObject( "testclass" );
if( cls1 == null ) {
document.writeln( site.GetErrorInfo() );
return;
}

// The str property is read - uses the getstr method
document.writeln( "1 " + cls1.str );

// Assigns a property to a Java class - uses the
// setstr method
cls1.str = string1 + string2;
document.writeln( "2 " + cls1.str );

// Instantiates a Java class with parameters
cls2 = java.CreateObject( "testclass", string2 );
if( cls2 == null ) {
document.writeln( site.GetErrorInfo() );
return;
}

// A method is invoked
document.writeln( "3 " + cls1.Upper() );

// A method is invoked that takes parameters
document.writeln( "4 " + cls2.Upper( "this is all upper" ) )
-->

The output for this script would look something like this:

1 
2 This is my very long test string that I am using
3 THIS IS MY VERY LONG TEST STRING THAT I AM USING
4 THIS IS ALL UPPER

As you can see, once you have instantiated the Java class within your script, you work with the Java object in the same manner that you would work with any other object within a Dynamo script.

Arrays in Java

PowerDynamo does not support array indexing in Java. Use the Java class java.lang.reflect.Array if you require array manipulation. For example:

<!--SCRIPT   
// First create the class we want an array of
String_Class = java.CallStaticMethod( "java/lang/Class", "forName", "java.lang.String" );
// Create the array with the class and a length
strarray = java.CallStaticMethod( "java/lang/reflect/Array","newInstance", String_Class, 10 );
// Use the various set and get methods to access the
// created array
java.CallStaticMethod( "java/lang/reflect/Array", "set", strarray, 1, "hello" );
y = java.CallStaticMethod( "java/lang/reflect/Array", "get", strarray, 1 );
document.writeln(y.toString());
-->

Note  

CallStaticMethod
This example uses the CallStaticMethod method of the Java object, which enables users to access static methods without creating (java.CreateObject) an instance of the object within the script.

Method overloading and type conversion

Method overloading in Java may occasionally cause problems in Dynamo.

In most cases, PowerDynamo can handle overloading. There are a few cases, however, where this is not the case. For example, take the following methods:

My_method( long, string, bool, int );
My_method( int, string, bool, int );

Lets assume that the following method call is being made:

Obj.My_method( 4, "hello", true, 7 );

Although Dynamo strings and Java strings are not exactly the same, Dynamo is able to recognize the difference and make the conversion. The same is true for converting Java Booleans to Dynamo Booleans and Java ints to Dynamo ints.

The problem arises for the above example because Dynamo does not know which method to use. The variables hello, true, and 7 are all easily identified. The variable 4, however, could be either an integer or a long so either method could be the correct one to use. At this point an error message indicating an ambiguous method call displays.

If the My_method was instead overloaded in the following manner:

My_method( string, string, bool, int );
My_method( int, string, bool, int );

There would be no problem with the method call, Dynamo can discern that 4 is an int and not a string.

Calling Jaguar components

You can use PowerDynamo to call Java components through Jaguar CTS. This process is explained in "Calling Jaguar Component Methods from PowerDynamo". For complete information on Jaguar CTS, see the Jaguar documentation.

 


Copyright © 1999 Sybase, Inc. All rights reserved.