Chapter 3 DynaScript Predefined Objects
Allows for manipulation of Java classes within PowerDynamo scripts.
The java object represents an object used for working with a Java class within your Web site.
To use a java method:
java.MethodName( parameter )
To use Java class objects within your Web site, your Web site must be configured to enable Java. Sybase Central allows for easy Java class setup for PowerDynamo Web sites. You should be aware of the following configuration options in the Configuration folder of Sybase Central if you want to create and use instances of Java class objects within your Dynamo scripts:
Java VM
PowerDynamo
supports the Sun Java VM and the Microsoft VM. You must select one
of these VMs for PowerDynamo to support Java.
Java
class cache size
To improve
performance, information about the methods and properties of Java
classes is cached. This option enables you to set the cache size.
Class
path for the mapping
You
must specify a path for class files that are stored in a Dynamo
Web site.
Executing from within Sybase Central
To execute scripts that use the java
object from within Sybase Central, the mapping and Dynamo site name
must be the same.
For more information on PowerDynamo configuration settings in Sybase Central, see "Changing Dynamo configuration settings" in the PowerDynamo User's Guide.
You must specify all parameters when you are working with the java object. Unlike other DynaScript objects, an error occurs if parameters are excluded when calling a Java class method or property.
Accessing Java methods and properties
You can access methods from any Java
class. Properties, however, can be accessed only from JavaBeans.
Let's assume we are working with this class:
class Fibonacchi { public long prev1; public long prev; public long next; public long getPrev() { return prev; } public long getNext() { return next; } public Fibonacchi() { prev1 = 1; prev = 0; next = 0; } public void generateNext() { next = prev1 + prev; prev = prev1; prev1 = next; } static public double doubleme( double t ) { return t*2; } }
To call this class, your script might look something like this:
// call a static method on the class result = java.CallStaticMethod( "Fibonacchi", "doubleme", 2.5 ); document.writeln( result ); // create a DynaScript object representing a Java class fib = java.CreateObject( "Fibonacchi" ); // retrieve a property on the class document.writeln( "The value of next is " + fib.next + " The value of prev is " + fib.prev ); for( i = 0; i < 20; i++ ) { // invoke a method on the class fib.generateNext(); document.writeln( "The value of next is " + fib.next + " The value of prev is " + fib.prev ); }
For more information on using Java in your PowerDynamo scripts see "PowerDynamo and Java" in PowerDynamo User's Guide.
java.CreateComponent( componentName [, managerUrl, id, password, narrowingComponent] )
Creates an instance of an EAServer component, using CORBA naming services, through Java. The parameters are:
narrowing_component
parameter
of the CreateComponent method should be used. For example, if you
have stored your Java stubs for the SVU Jaguar package in com/sybase/jaguar/sample/svu/SVUMetaData then
you would use the following syntax to create an instance of a SVUMetaData
component that resides in the SVU Jaguar package:
java.CreateComponent( "SVU/SVUMetaData", "iiop://localhost:9000", "Jagadmin", "", " com/sybase/jaguar/sample/svu/SVUMetaData" );
If however, you used the default value when generating stubs for the SVU package, your syntax would look like this:
java.CreateComponent( "SVU/SVUMetaData" );
Setting the Java VM
The Java VM configuration option
must be set to the Sun VM for scripts with this method to execute.
Change the VM option using the Configuration\Default
General Settings\Java VM folder in Sybase Central.
A EAServer component object or null if the component cannot be created. This is a DynaScript Java object.
This example creates an instance of a component
called comp
. Once the object is
created, its method getMajors
is
called and the Record Set
is
displayed:
<HTML> <H1>A CreateComponent example</H1> <!--SCRIPT document.writeln( "<H3>Testing SVUEnrollment getMajors</H3>" ); comp = java.CreateComponent( "SVU/SVUEnrollment" ); RecordSet = comp.getMajors(); query = java.CallStaticMethod( "com.sybase.CORBA.jdbc11.SQL", "getResultSet", RecordSet); received = query.next(); i = 0; while( received ) { metadata = query.getMetaData(); document.writeln( "*****" ); columns = metadata.getColumnCount(); for( j = 1; j<= columns; j++ ) { value = query.getString( j ); document.writeln( value ); } received = query.next(); i++; } --> </HTML>
For information on result sets and the getResultSet method, see the EAServer Programmer's Guide.
site.GetErrorInfo( )
The site.GetErrorInfo( ) method
may be used for retrieving error information.
"Calling EAServer Component Methods from PowerDynamo" in the PowerDynamo User's Guide.
java.CreateObject( className [, list of constructor parameters ] )
Instantiates a Java class object. The parameters are:
Setting the Java VM
The Java VM configuration option
must be set to the Sun VM for scripts with this method to execute.
Change the VM option using the Configuration\Default
General Settings\Java VM folder in Sybase Central.
A DynaScript object representing the Java class. Returns null if the Java class cannot be created.
This example creates an instance of Java class
that performs the Fibonacchi mathematical procedure. It involves
the GenerateNext
method
and returns results.
<!--SCRIPT fib = java.CreateObject( "Fibonacchi" ); // retrieve a property on the class document.writeln( "The value of next is " + fib.next + " The value of prev is " + fib.prev ); // set a property on the class fib.next = 5; for( i = 0; i < 20; i++ ) { // invoke a method on the class fib.generateNext(); document.writeln( "The value of next is " + fib.next + " The value of prev is " + fib.prev ); } -->
"PowerDynamo and Java" in the PowerDynamo User's Guide.
java.CallStaticMethod( className, staticMethod [, list of method parameters] )
Calls a static Java method. You need not create a Java object (java.CreateObject) to call static methods of that object. The parameters are:
The return type is dependent on the type of return value of the static method.
This example executes the static method doubleme
of
the class Fibonacchi
. Calling
a static method does not require that an object representing a Java
class instance be created.
<!--SCRIPT result = java.CallStaticMethod("Fibonacchi", "doubleme", 2.5); document.writeln( result ); -->
"PowerDynamo and Java" in PowerDynamo User's Guide
java.GetHomeInterface(component_name [, provider_url, user_id, password] )
Allows access to the EJBHome interface for an EAServer component. The parameters are:
The Dynamo methods GetHomeInterface and GetUserTransaction enable access
to EJB interfaces of EAServer components. To access EJB interfaces of EAServer components, Dynamo requires
an EAServer client jar file, easclient.jar,
from your EAServer installation. Refer to PowerDynamo installation
instructions for your platform and ensure that this file is included
in your JAGUARCLASSES (Solaris) or CLASSPATH (NT) variable.
Use java.GetHomeInterface
to
create an instance of the EJBHome interface for the component Test/Cart
.
Then use the Home interface to create an actual instance of Test/Cart
.
You need to use the JNDI name of the Object, for instance Cart,
if the JNDI name of the component is Cart:
carthomeObj = java.GetHomeInterface("Cart") cartObj = carthomeObj.create("John", "7506"); cartObj.addItem(66) cartObj.addItem(22) cartObj.purchase() carthomeObj.remove(...)
If you create a EJB from a tool like PowerJ, the wizard
prompts you for a unique name ( this is the JNDI name used for the
lookup ). Normally the name for the remote interface ( the code
running on the client side to invoke the remote method ) and the
JNDI name are the same.
"PowerDynamo and Java" in the PowerDynamo User's Guide
java.GetUserTransaction( [ <provider_url>, <user_id>, <password>] )
Allows access to the EJB UserTransaction object. The parameters are:
The Dynamo methods GetHomeInterface and GetUserTransaction enable access
to EJB interfaces of EAServer components. To access EJB interfaces of EAServer components, Dynamo requires
an EAServer client jar file, easclient.jar,
from your EAServer installation. Refer to PowerDynamo installation
instructions for your platform and ensure that this file is included
in your JAGUARCLASSES (Solaris) or CLASSPATH (NT) variable.
The components that participate in the transaction managed with the UserTransaction object must be in the same server as the UserTransaction object, which cannot be assumed if an EAServer cluster is used to ensure that this requirement is met:
GetUserTransaction()
and GetHomeInterface()
, CreateComponent()
.
The initial-context values of the URLs can differ.
The UserTransaction interface defines exceptions that are
thrown when errors occur. For example, TransactionRolledbackException
is thrown when commit()
is
called after setRollbackOnly()
has
already been called for a transaction.
As many of the methods in the UserTransaction interface for
example, return void, begin()
, commit()
, rollback()
,
these exceptions are the only way to detect errors.
In DynaScript, exceptions are passed to the user through the site.GetErrorInfo()
method.
That is, the exception is converted to an error string. For example:
ut.begin() ... ut.commit() errMsg = site.GetErrorInfo(); if ((errMsg != null) && (errMsg != "")) { /* the transaction was not committed */ }
The UserTransaction.getStatus()
method
returns an integer value indicating the status of the current transaction.
Dynamo users can include the script ~/system/utils/usertran.ssc to
get the definitions of variables that map to the integer values
returned by getStatus()
.
import "~/system/utils/usertran.ssc"
... ut = java.GetUserTransaction(...); ut.begin() status = ut.getStatus(); if (status != UserTran.STATUS_ACTIVE) { /* the transaction was not started */ } ...
Use java.GetUserTransaction
to
get the UserTransaction object, instantiate the components that
will participate in the transaction, begin the transaction, perform
the operations required in the transaction, and commit the transaction.
ut = java.GetUserTransaction("iiop://my-host:9000") acctHome = java.GetHomeInterface("Account", "iiop://my-host:9000") acct1 = acctHome.create("John", "Doe", "111-11-1111") acct2 = acctHome.create("Jane", "Doe", "222-22-2222") acct3 = = java.CreateComponent("Bank/Account", "iiop://my-host:9000") ut.begin(); errMsg = site.GetErrorInfo(); if ((errMsg != null) && (errMsg != "")) { /* handle error, the transaction did not begin return } acct1.withdraw(100); acct2.deposit(50); acct3.deposit(50); ut.commit(); errMsg = site.GetErrorInfo(); if ((errMsg != null) && (errMsg != "")) { /* handle error, the transaction did not commit return }
"PowerDynamo and Java" in the PowerDynamo User's Guide.
Copyright © 2001 Sybase, Inc. All rights reserved. |
![]() |