Working with Java Objects

Variables and Objects

wlshell scripts can use Java objects in different ways. In fact, all wlshell values are Java objects. Objects can be created and stored in variables by specifying their literal value.

server = localhost:7001 # String
max = 10 # Integer
millis = 10000L # Long

Variables in wlshell don't need to be declared. A variable can store objects of any type. See the $variable, descr, and set commands for more info about variables.

New objects can be created with the "new" operator, specifying the class name and any arguments that the constructor may require. New objects are stored in a variable.

x = new Float 10.2
props = new java.util.Properties

Class names should include their package, except for classes belonging to the "java.lang" package, which can be omitted.

The "get" and "invoke" commands return Java objects (or null), which can be stored in a variable for latter use.

cd /Server/$SERVER
version = get ServerVersion
wlsh integration:/ServerRuntime/cgServer> activation = get ActivationTime
variable activation set to 1077750042921 (java.lang.Long)

wlsh integration:/ServerRuntime/cgServer> dateStarted = new java.util.Date $activation
variable dateStarted set to Wed Feb 25 17:00:42 GMT-06:00 2004 (java.util.Date)

Variables can store the Java "null" value.

Accessing Fields

wlshell scripts can access the public fields of an object. The descr command provides the description of an object, including its current String representation ("toString"), class name, public constructors, public fields and public methods. The "descr" command expects the name of a variable with the object to describe.

Public fields of an object (static and non-static) can be accessed using the following syntax:

${variable.field}

The value returned can be stored in a variable.

wlsh [not connected]> n = 1
variable n set to 1 (java.lang.Integer)

wlsh [not connected]> ${n.MAX_VALUE}
2147483647

wlsh [not connected]> max = ${n.MAX_VALUE}
variable max set to 2147483647 (java.lang.Integer)

The following example illustrates how to describe an object and access a public field. The class used in the example ("Simple") is included in the wlshell distribution and defined as:

package wlshell.samples.lang;

import java.util.Date;

public class Simple {

  public static String name = "MyName";
  public Date created;

  public Simple() {
    created = new Date(System.currentTimeMillis());
  }

  public static String toUpper(String message) {
    return message.toUpperCase();
  }

  public Date getCreated() {
    return created;
  }

}
An object of this class is created and described with the descr command. Instance and static fields values are displayed:
wlsh [not connected]> simple = new wlshell.samples.lang.Simple
variable simple set to wlshell.samples.lang.Simple@50b895 (wlshell.samples.lang.Simple)

wlsh [not connected]> descr $simple
description of $simple:

wlshell.samples.lang.Simple@50b895 (wlshell.samples.lang.Simple)

the class doesn't implement any interface

public constructors:
public wlshell.samples.lang.Simple()

public fields:
public static java.lang.String wlshell.samples.lang.Simple.name = MyName
public java.util.Date wlshell.samples.lang.Simple.created = Wed Dec 21 13:41:56 CST 2005

public methods:
public final native void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final native void java.lang.Object.notifyAll()
public final native void java.lang.Object.notify()
public java.lang.String java.lang.Object.toString()
public boolean java.lang.Object.equals(java.lang.Object)
public int java.lang.Object.hashCode()
public final java.lang.Class java.lang.Object.getClass()
public java.util.Date wlshell.samples.lang.Simple.getCreated()
public static java.lang.String wlshell.samples.lang.Simple.toUpper(java.lang.String)


wlsh [not connected]> ${simple.name}
MyName

wlsh [not connected]> ${simple.created}
Wed Dec 21 13:41:56 CST 2005

Invoking Methods

wlshell scripts can invoke the public methods of an object. The descr command can be used to list the signatures of the public methods of an object. If the method expects one or more parameters, values for those can be appended to the method name. The syntax for invoking methods is similar to Tcl.

Public methods of an object (static and non-static) can be invoked using the following syntax:

${variable} <method-name> (<parameter>)*

The value returned by the method invocation can be stored in a variable. The following examples illustrates how to call a method of an object.

wlsh [not connected]> $simple toUpper hello
HELLO

wlsh [not connected]> $simple getCreated
Wed Dec 21 13:41:56 CST 2005
props = new java.util.Properties
$props setProperty user weblogic
dbuser = $props getProperty user
wlsh [not connected]> s = "hello world!"
variable s set to hello world! (java.lang.String)

wlsh [not connected]> u = $s toUpperCase
variable u set to HELLO WORLD! (java.lang.String)

wlsh [not connected]> i = $u indexOf 'W'
variable i set to 6 (java.lang.Integer)

wlsh [not connected]> m = $s length
variable m set to 12 (java.lang.Integer)

wlsh [not connected]> w = $u substring $i expr($m - 1)
variable w set to WORLD (java.lang.String)

Static Fields and Methods

wlshell supports working with Java classes, to read static fields and invoke static methods. In Java, classes are represented themselves as objects, therefore static members can be used with the same syntax. Here are the steps:

  1. get an object, any object of any class
  2. obtain the object that represents its class, using the "getClass" method
  3. obtain the object that represents the class with the static fields or methods, using the "forName" method
  4. using the object, access the static field or invoke the static method
Steps 1 and 2:
wlsh [not connected]> object = new Object
variable object set to java.lang.Object@40abf4 (java.lang.Object)

wlsh [not connected]> class = $object getClass
variable class set to class java.lang.Object (java.lang.Class)
Step 3, obtain the object representing the class:
wlsh [not connected]> simpleClass = $class forName wlshell.samples.lang.Simple
variable simpleClass set to class wlshell.samples.lang.Simple (java.lang.Class)
Step 4, access the static field and invoke the static method:
wlsh [not connected]> ${simpleClass.name}
MyName

wlsh [not connected]> $simpleClass toUpper hello
HELLO

Attempts to access the non-static fields or invoke non-static methods, using the object representing the class, are not allowed:

wlsh [not connected]> ${simpleClass.created}
couldn't execute all the script, there was an error at line 1: Cannot access a non static field: "created"
type "help" for a list of commands

wlsh [not connected]> $simpleClass getCreated
couldn't execute all the script, there was an error at line 1: Cannot invoke a non static method: "getCreated"
type "help" for a list of commands
The object representing the class can be described with the descr command:
wlsh [not connected]> descr $simpleClass
description of $simpleClass:

class wlshell.samples.lang.Simple (java.lang.Class)

implements:
interface java.io.Serializable

public constructors:

public fields:
public static java.lang.String wlshell.samples.lang.Simple.name = MyName
public java.util.Date wlshell.samples.lang.Simple.created

public methods:
public final native void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final native void java.lang.Object.notifyAll()
public final native void java.lang.Object.notify()
public java.lang.String java.lang.Object.toString()
public boolean java.lang.Object.equals(java.lang.Object)
public int java.lang.Object.hashCode()
public final java.lang.Class java.lang.Object.getClass()
public java.util.Date wlshell.samples.lang.Simple.getCreated()
public static java.lang.String wlshell.samples.lang.Simple.toUpper(java.lang.String)

Inner classes can also be specified using the "$" notation. For example:

factory = $class forName "com.bea.xml.XmlObject$Factory"
xmlObject = $factory newInstance

Arrays

Arrays of objects can also be specified in wlshell scripts.

Arrays can be created using the following syntax:

<variable-name> = new <class-name> [ <size> ]

The size of the array can be specified with a constant or a variable containing a non-negative Integer.

Use the following syntax to access an array position:

$<variable-name> [ <index> ]

Use the following syntax to store a value on an array position:

<variable-name> [ <index> ] = <value>

Examples:

n = 3
a = new String[$n]
a[0] = cero
a[1] = one
a[2] = two
uno = $a[1]

for $i in 0 to 2 do
  a[$i] = queue$i
end

Arrays can be used in the for command.

for $i in $a
  echo $i
end

ArrayList

The Java class "java.util.ArrayList" can also be used in wlshell scripts. The syntax for accessing elements in the ArrayList is the same as in the regular arrays. ArrayLists provide some advantages over arrays, like dynamic resizing, ability to contain objects of different classes and more compact declaration and initialization. Many wlshell commands return ArrayList as results.

ArrayLists can be created using the following syntax:

<variable-name> = [ <element> (, <element>)* ]
For example:
myPath = [Type, Name]
This is equivalent to the following commands:
myPath = new java.util.ArrayList
$myPath add Type
$myPath add Name

Use the following syntax to access an element in the ArrayList:

$<variable-name> [ <index> ]

Use the following syntax to store a value on an ArrayList position:

<variable-name> [ <index> ] = <value>

Use the "add" method to add a new element. See the javadoc for java.util.ArrayList for a complete description, or use the descr command.

Examples:

wlsh runtime.com.bea:/> myPath = [Type, name]
variable myPath set to [Type, name] (java.util.ArrayList)

wlsh runtime.com.bea:/> myPath[1] = Name
variable myPath[1] set to Name (java.lang.String)

wlsh runtime.com.bea:/> $myPath add ServerRuntime
true

wlsh runtime.com.bea:/> $myPath
[Type, Name, ServerRuntime]

ArrayLists can be used in the for command.

for $i in $a
  echo $i
end