Chapter 3 DynaScript Predefined Objects


java object

Object

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.

Syntax

To use a java method:

java.MethodName( parameter ) 

Description

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:

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.

Note  

Accessing Java methods and properties
You can access methods from any Java class. Properties, however, can be accessed only from JavaBeans.

Example

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.

CreateComponent method

Syntax

java.CreateComponent( componentName [, managerUrl, id, password, narrowingComponent] )

Description

Creates an instance of an EAServer component, using CORBA naming services, through Java. The parameters are:

If however, you used the default value when generating stubs for the SVU package, your syntax would look like this:

java.CreateComponent( "SVU/SVUMetaData" );

Note  

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.

Return

A EAServer component object or null if the component cannot be created. This is a DynaScript Java object.

Example

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.

Note  

site.GetErrorInfo( )
The site.GetErrorInfo( ) method may be used for retrieving error information.

See also

"Calling EAServer Component Methods from PowerDynamo" in the PowerDynamo User's Guide.

CreateObject method

Syntax

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

Description

Instantiates a Java class object. The parameters are:

Note  

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.

Return

A DynaScript object representing the Java class. Returns null if the Java class cannot be created.

Example

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 );
}
--> 

See also

"PowerDynamo and Java" in the PowerDynamo User's Guide.

CallStaticMethod method

Syntax

java.CallStaticMethod( className, staticMethod [, list of method parameters] )

Description

Calls a static Java method. You need not create a Java object (java.CreateObject) to call static methods of that object. The parameters are:

Return

The return type is dependent on the type of return value of the static method.

Example

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 );

--> 

See also

"PowerDynamo and Java" in PowerDynamo User's Guide

GetHomeInterface method

Syntax

java.GetHomeInterface(component_name [, provider_url, user_id, password] )

Description

Allows access to the EJBHome interface for an EAServer component. The parameters are:

Note   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.

Example

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(...)

Note   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.

See also

"PowerDynamo and Java" in the PowerDynamo User's Guide

GetUserTransaction method

Syntax

java.GetUserTransaction(  [ <provider_url>, <user_id>, <password>] )

Description

Allows access to the EJB UserTransaction object. The parameters are:

Note   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.

Comments

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:

  1. Use the same name server to create the UserTransaction object that was used when creating the component instances. That is, supply the same provider_url to GetUserTransaction() and GetHomeInterface() , CreateComponent() . The initial-context values of the URLs can differ.
  2. Get the UserTransaction object and begin the transaction before creating any component instances.
  3. In addition, follow the same restrictions as other Java clients when using the UserTransaction object. See to the EAServer Programmer's Guide and EAServer Reference Manual for details.

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 */    }
    ...

Example

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
    }

See also

"PowerDynamo and Java" in the PowerDynamo User's Guide.

 


Copyright © 2001 Sybase, Inc. All rights reserved.