AD409: New! How to Call Java Classes From Your LotusScript Programs

AD409: New! How to Call Java
Classes From Your LotusScript
Programs
Gary Devendorf
Keith Kimball
M&C Development
M&C Development
X
Agenda
Introduction
Why call Java?
Basic "How to"
demos
Advanced "How to"
demos
Conclusion
Y
What is LotusScript
A "BASIC" structured language
Part of a hosting application (VBA)
Cross platform
Win32, MAC, various UNIX's, Linux, AS/400,
OS390
Gluing together Objects/Applications
now even Java objects
Z
What is Java?
Both programming language and platform
Developed by Sun MicroSystems
Write once, run anywhere
Runs on popular platforms - Windows, Linux, AIX,
Solaris, MacOS
Smart cards to mainframes
Rich set of APIs with network focus
[
Why call Java?
Reuse existing software
Extensive API
beyond LotusScript (e.g., Network and UI)
Easy to call from LotusScript
\
Classes you can call from LotusScript
LotusScript classes
written in LotusScript
OLE/COM
Calling Windows application
MS Office, AutoCAD, ..
LSX
C++ classes
Compiled for each platform
Lotus Notes/Domino 6 - Java!
]
Demo
FruitReport
reuse existing software
^
Using Java Classes from LotusScript
LotusScript to Java ( LS2J )
........Easy
_
5 Easy Steps
Initialize Java Virtual Machine (JVM)
Establish JVM connection (JavaSession)
Find desired class (JavaClass)
Get desired object (JavaObject)
Access object's methods and properties
`
java/net/InetAddress
XW
1. Initialize Java Virtual Machine
Starts Java Virtual Machine (JVM)
Loads the JavaSession ADT
Example
Uselsx "*javacon"
XX
2. Establish JVM connection
JavaSession represents a JVM connection
Multiple sessions allowed
Starting point for access to Java objects
Example
Dim jsession as New JavaSession
XY
3. Find Desired Class
JavaClass represents a Java class
Terminology: field versus property
Example
Dim jsession as New JavaSession
Dim jclass as JavaClass
Set jclass = jsession.GetClass("java/net/InetAddress")
XZ
4. Get Desired Object
Obtain JavaObject
create a new one
call static method
access static property
Example
Dim jsession as New JavaSession
Dim jclass as JavaClass
Dim localHost As JavaObject
Set jclass = jsession.GetClass("java/net/InetAddresss")
Set localHost = jclass.getLocalHost()
X[
5. Access Methods and Properties
Only public fields and methods
Dot notation usually suffices
Example
Dim jsession as New JavaSession
Dim jclass as JavaClass
Dim localHost As JavaObject
Set jclass = jsession.GetClass("java/net/InetAddresss")
Set localHost = jclass.getLocalHost()
Msgbox "Name: " & localHost.getHostName()
X\
LocalHost Example
Option Public
Uselsx "*javacon"
Sub Initialize
Dim jsession As JavaSession
Dim jclass As JavaClass
Dim localHost As JavaObject
Set jsession = New JavaSession
Set jclass = jsession.GetClass("java/net/InetAddress")
Set localHost = jclass.getLocalHost()
Msgbox "Name: " & localHost.getHostName()
Msgbox "Address: " & localHost.getHostAddress()
End Sub
X]
LocalHost Demo
Java
X^
Quick Reference
Action
Syntax
1. Initialize JVM
2. JVM Connection
3. Get desired class
4. Get desired object
Uselsx "*javacon"
jsession = new JavaSession
jclass = jsession.GetClass(....)
jobject = jclass.CreateObject(...)
jobject = jclass.<method>(...)
jobject = jclass.<property>
5. Manipulate object Invoke methods
Access properties
X_
Beyond the Basics
Script Libraries
Java API Documentation
Datatype Mappings
Java Signatures
New Classes
Error Handling
Dot Syntax Limitations
Methods and Properties
Collections
X`
Script Libraries
LotusScript, JavaScript or Java
Replicated as part of database
Include with USE statement
Tip: put all Java user classes in library!!
My Java
YW
Creating a Java Library
YX
Add Java Classes to Library
YY
Select Files from File System
YZ
Demo
Bouncing Heads
found in on-site lab
Y[
Java API Documentation
Easy to Use
Available in HTML format
Online
http://java.sun.com/products/jdk/1.2/docs/api/index.html
Download
http://java.sun.com/j2se/1.3/docs.html
Y\
Datatype Mapping
Java
# bytes
LotusScript (* new)
byte
1
short
int
long
float
double
boolean
2
4
8
4
8
undefined
Byte *
Integer
Long
Double
Single
Double
char
java.lang.String
other objects
16
varies
varies
Boolean
String
String
*
JavaObject
*
Y]
Java Signatures
Signature
Descript Example
Arguments Result
B
byte
(B)I
byte
integer
C
D
char
double
(C)I
(D)F
char
double
integer
float
F
float
(FF)D
float,float
double
I
int
(I)C
int
char
J
long
(J)I
long
int
S
short
(S)I
short
int
V
void
(I)V
int
void
boolean
string
int[]
boolean
void
int
Z
boolean (Z)Z
L<name>; class
(Ljava/lang/String;)V
[
array of ([I)I
Y^
LS2J Classes
Class
Properties
Methods
ClassName
ClearJavaError
GetClass
GetLastJavaError
CreateObject
GetClassMethods
GetClassProperties
GetMethod
GetProperty
JavaSession
JavaClass
JavaObject
Y_
LS2J Classes (continued)
Class
Properties
JavaError
Methods
ErrorMsg
Stacktrace
Invoke
JavaMethod
JClass
MethodName
Modifier
Signature
JavaProperty
JClass
GetValue
PropertyName SetValue
Modifier
Type
JavaMethodCollection Count
JavaPropertyCollection Current
GetFirst
GetNext
GetNth
Y`
JavaError Class
Contains information about Java Errors
Used in conjunction with GetLastJavaError and
ClearJavaError from JavaSession
LotusScript "Error" usually suffices
Properties
ErrorMsg
StackTrace
ZW
Error Handling Example
Dim jsession as New JavaSession
Dim jclass As JavaClass
Dim err As JavaError
On Error Goto Catch
Set jclass = jsession.GetClass("InvalidClassName")
Exit Sub
Catch:
Messagebox "LS Error: " & Error
Set jerr = jsession.GetLastJavaError()
Messagebox "Java Error: " & jerr.ErrorMsg()
Messagebox " Stack trace: " & jerr.Stacktrace()
jsession.Clearjavaerror
ZX
Dot Notation Limitations
Limitations
Overloaded methods
Case sensitive names
Names over 40 characters
Solution
JavaMethod
JavaProperty
ZY
JavaMethod & JavaProperty Classes
Solution to dot notation limitations
JavaMethod
Invoke
JavaProperty
GetValue
SetValue
ZZ
JavaMethod Example
Dim demo as JavaObject
Dim lower, upper as JavaMethod
...
Set lower = jclass.Getmethod("foo","()I")
Set upper = jclass.Getmethod("FOO","()I")
Msgbox demo.foo()
Msgbox lower.invoke(demo)
Msgbox upper.invoke(demo)
Z[
JavaProperty Example
Dim lower, upper As JavaProperty
Dim demo As JavaObject
...
Set lower = jclass.GetProperty("bar")
Set upper = jclass.GetProperty("BAR")
Msgbox demo.BAR
Msgbox lower.getValue(demo)
Msgbox upper.getValue(demo)
Z\
Java Collections
Of JavaMethods or JavaProperties
Similar to Notes document collections
Properties
Count
Current
Methods
GetFirst
GetNext
GetNth
Z]
Demo of Web Services
LotusScript calling Java calling Web Services
Z^
Things to Remember
Java can do useful things
Calling Java from LotusScript is easy
Java classes in Libraries
replicate with the database
stay out of the file system
run across platforms
LotusScript rules!
Z_
Questions
Z`
Resources
www.lotus.com
www.notes.net
java.sun.com
www.groupcomputing.com/garyspage
Please fill out evaluations
AD 409: New! How to Call Java Classes From
Your LotusScript Programs
[W