JMP207 - NetNotes Solutions Unlimited

Download Report

Transcript JMP207 - NetNotes Solutions Unlimited

JMP207
It Takes Two to Tango – Java and Eclipse
John Kidd – Kiddcorp, L.P.
Paul T. Calhoun – NetNotes Solutions Unlimited, Inc
®
Your Presenters – This Is Us!
John Kidd
President
Kiddcorp, L.P.
John Kidd is a recognized
strategist, as well as a frequent speaker and author in the area of
Java and J2EE technologies. John has more than 16 years
experience in leading teams to deliver line of business applications
for companies such as Belo, IBM/Lotus, Texas Instruments, and
Advanced PCS.
John has been an early adopter in delivering key technologies and
delivery methods. He is a frequent speaker on IT strategies and a
consultant for organizations developing open source strategies.
Your Presenters – This Is Us!
Paul T. Calhoun
Chief Technology Officer
NetNotes Solutions Unlimited
Paul Calhoun, ND 6,7 and 8 PCLI and PCLP,
is a highly rated speaker who provides
customer-focused knowledge transfer and consulting to Fortune 100
and 500 companies, as well as many SMBs. Paul currently builds
Domino, Web, Java, and XML applications for his customers using
Domino, Portlet Factory and WebSphere.
He co-authored the IBM Redbook “XML Powered by Domino,” and
has developed several online XML and Java programming courses.
He has written articles for both “The View” and “The Sphere”
technical publications.
The Great Book Give Away
 Compliments of O’Reilly Publishing
 Please thank them by buying their books !!!!
 If you want to participate in the book drawings
 Place your Business card (or first and last name) in the container being passed
around
 We WILL NOT harvest contact information for ANY marketing purposes
 We will draw the names during the Session
Agenda
 Roadmap
 Eclipse, The Tool
 Java language Fundamentals
 Developing Java code In Eclipse
 Develop and Deploy Lotus Domino Java code using the Eclipse
development platform
 Wrap Up
Roadmap
 I’ve said it before, so I’ll say it again !!
 Learning Java™ is a lot like eating an Elephant !!
 It’s a big job and there’s no clear place where to start !!
Roadmap
J2EE, XML
Servlets, JSPs &
JSFs
Start Here! J2SE
Roadmap
 Start with J2SE (Java 2 STANDARD Edition)
 This covers core Java functionality
 Syntax
 Data Types
 Constructs
 Core Classes
– java.lang
– java.io
– java.net
– etc.
 Allow 3-6 Months
http://java.sun.com/javase/index.jsp
Roadmap
 Learn XML (not part of Sun Certification)
 XML Syntax
 DTD/XML Schema
 XSL
 XSLT
 XSL:FO
 Finally jump on the J2EE bandwagon (In this order)
 Servlets
 JSPs
 JSFs
 Allow another 3-6 months
 Then the rest of the J2EE specification
 Allow another 3-6 months
Roadmap
 Start small
 Java Agents in Domino
 Graduate to Servlets
 Agent Code transitioned to Servlets running on Domino
 Incorporate an external Servlet/JSP engine
 Apache w/Tomcat add-in (DSAPI Filters available)
 JBOSS (DSAPI Filters available)
 WebSphere Application Server (DSAPI Filters available)
 Write JSP/JSF applications that access Domino data
 Using Domino Custom JSP tags (Available since ND 6)
Roadblocks
 Road Blocks on your Journey to Learning Java
 “Linear” thinking instead of thinking in “Objects”
 Starting to learn Java with J2EE applications (Servlets, JSPs)
 Trying to start with the Java Enterprise Technologies
 Try to learn Java in conjunction with a HUGE Mission critical project
 Not applying what you learn EVERYDAY !!
Agenda
 Roadmap
 Eclipse, The Tool
 Java language Fundamentals
 Developing Java code In Eclipse
 Develop and Deploy Lotus Domino Java code using the Eclipse
development platform
 Wrap Up
Eclipse, The Tool
 Installing Eclipse
 Updating Eclipse
 Configuring Eclipse
 Navigating Eclipse
 Creating Projects
Ok, let’s get this out of the way
We Don’t Hate LotusScript !!!!!!!!!!!
We Don’t Hate LotusScript !!!!!!!!!!!
We Don’t Hate LotusScript !!!!!!!!!!!
We Don’t Hate LotusScript !!!!!!!!!!!
We Don’t Hate LotusScript !!!!!!!!!!!
We Don’t Hate LotusScript !!!!!!!!!!!
We Don’t Hate LotusScript !!!!!!!!!!!
We Don’t Hate LotusScript !!!!!!!!!!!
Very Wise Saying
If the only tool you have is a hammer,
you tend to see every problem as a nail
Abraham Maslow
Your Domino Development Toolbox
Formula Language
LotusScript
Java
JavaScript
Installing Eclipse
 Steps to Install Eclipse
 Download a Java SDK FIRST
 Level 1.4.2 or higher
– Java 5 is recommended
 Install Java SDK
 Download Eclipse 3.3.x (Europa Build)
 http://www.eclipse.org/downloads/
 Supported Platforms
– Windows
– Linux
– Solaris 8
– AIX
– HP-UX
– Max OS X
Installing Eclipse
 You must have at least one Java SDK installed on your machine
BEFORE installing the Eclipse workbench
 Must be version 1.4.2 or above (J2SE 5 or 6)
 Java 5 is the recommended !!
Tip
 If supporting multiple installed SDKs
 Install oldest to newest
 Installing Java 5 or 6 will allow you to write code that conforms to
older JRE
http://java.sun.com/javase/downloads/index.jsp
Installing Eclipse
 Download Eclipse 3.3.x
 This is the Europa build
 It will be .zip file
 Unzip the contents to a directory on the hard drive
 That’s it. You just installed Eclipse !!!
 Start Eclipse
 Eclipse.exe (Windows)
 ./Eclipse (Linux)
 First time Eclipse starts it will search for an installed SDK and add it to the
workbench configuration
 Only the currently configured JRE will be added to the workbench
configuration
 Others can be added (See configuration slides)
Installing Eclipse
 Eclipse start up screen
Installing Eclipse
 The Eclipse Workbench with the default Java Perspective
Updating Eclipse
 The first task to complete after the Eclipse workbench is started is
to check for software updates
 From the “Help” menu choose “Software Updates > Find and Install…”
Updating Eclipse
 In the Feature Updates Dialog box there are two options
 Search for updates of the currently installed features
 Allows you to update all currently installed feature functionality
 Search for new features to install
 Allows you to add additional functionality to the Eclipse Workbench
– Like the Web Tools Plug-in
Updating Eclipse
 Search for updates to the currently installed features FIRST
 This will ensure that you have the most up to date “Core” install
 If you have just downloaded Eclipse, there may be not updates to install
 After the installed feature update is complete, then search for new
features to install
 This will allow you to add additional functionality to your installation of Eclipse
Updating Eclipse
 If “Search for new features to install” is selected you will see the
following dialog box
Updating Eclipse
 The Europa Discovery Site is a combination of Eclipse projects in
one installation site
 Check the values for the functionality you want to install and click the Next
button
 If there are errors after you make your selections, click the “Select Required”
button to resolve any dependency issues
Updating Eclipse
 When first starting out I recommend including the following
 Graphical Editors and Frameworks
 Java Development
 Web and JEE Development
 You will need to run Software update
 After every newly installed feature
 At least once a Quarter
 Preferably once a month
Tip
Configuring Eclipse
 Configuration changes that affect the entire workbench are done
via the “Preferences” dialog box
 From the “Window” menu choose “Preferences”
Tip
Configuring Eclipse
 Expand the preferences in the column on the left to access the
preferences for that category
Configuring Eclipse
 Common preference changes
 Change Font for editors and dialogs
 General > Appearance > Colors and Fonts
– Expand “Basic” select “Text Font”
 Prompt for workspace on startup
 General > Startup and Shutdown
 Default Web Browser to use
 General > Web Browser
 Enable HTTP Proxy Connection
 General > Network Connections
Configuring Eclipse
 Common preference changes (Cont)
 Configure additional JREs
 Java > Installed JREs
– Click the Add button and navigate to install directory
– You can add the Domino 6.x, 7.x and 8.x JVM to the Installed JREs in
Eclipse to use on Domino only projects
Configuring Eclipse
 Common preference changes (Cont)
 Some changes are specific to the current workspace (Like the JVM’s we just
added)
 Preferences can be exported from one workspace and then imported into
another
 Once you have configured your preferences the way you want them
 Export the Preferences
– File > Export…
– Expand General and choose “Preferences”
Tip
Configuring Eclipse
 Common preference changes (Cont)
 You can choose to export all or just the specific changes
 Provide a file name that has an .epf extension
 This is just an XML file
Configuring Eclipse
 Common preference changes (Cont)
 Preferences can be imported into a new workspace from the import menu
 File > Import…
– Expand General and choose “Preferences”
Configuring Eclipse
 Common preference changes (Cont)
 Choose the .epf file to be imported
 You can import all of the preferences or choose a sub-set
Configuring Eclipse
 Common preference changes (Cont)
 In order to test Web Applications (Servlets, JSP’s) you will need an installed Web
Application Server
 Add Runtime Test Server
– Server > Installed Runtimes
– Click “Add” and browse to installation of runtime test server
– IBM WebSphere Application Server
– Apache w/Tomcat add-in
– JBOSS
– Oracle
Navigating Eclipse
 Eclipse is made up of
 Perspectives
 Allows developer to focus on particular development type
– Java
– J2EE
– Debug
– Plug-in
– Resource
 Views
 Displays functionality with-in a perspective
– Package Explorer
– Problems
 Editors
 Allows for development of a particular code type
– Java
– HTML, CSS, JavaScript, XML, etc.
Navigating Eclipse
 Switch between perspectives using the Perspective bar in the upper
right hand corner of the workbench
 Views in Perspectives can be dragged/dropped to any area of the
perspective
 Additional views can be added via the menu
 “Window > Show View”
Navigating Eclipse
 Views can be removed from a perspective by click on the “X” in the
upper right corner of a view
 Any View (Including Editor Windows) can be Maximized/Restored
by double clicking on the tab that displays the view/editor title
 If you totally FUBAR a perspective you can always restore the view
to the original layout via the menu
 “Window > Reset Perspective”
Tip
Navigating Eclipse
 The default Java Perspective
Perspective
Bar
Editor
Views
Creating Projects
 The type of Project that can be created in Eclipse is based upon the
features that have been added
 “Out of the Box” Eclipse can only create the following project types
 Java
 Plug-in
 Resource
 CVS (Concurrent Versioning System – Code Sharing)
 With Additional Features from the Eudora or other Eclipse/Third
Party add ins
 Web
 Static Web Project
 Dynamic Web Project
 J2EE
 Enterprise Application Project
 Application Client Project
Creating Projects
 All projects are created via the New Project Wizard
 File > New Project
Eclipse Workbench - Demonstration
Agenda
 Roadmap
 Eclipse, The Tool
 Java language Fundamentals
 Developing Java code In Eclipse
 Develop and Deploy Lotus Domino Java code using the Eclipse
development platform
 Wrap Up
Java History
 Originally was called the “Oak”
 Was intended to be used in consumer electronics
 Platform independence was one of the requirements
 Based on C++, with influences from other OO languages (Smalltalk, Eiffel …)
 Started gaining popularity in 1995
 Renamed to “Java”
 Was a good fit for the Internet applications
Portability
 Java is a platform independent language
 Java code can run on any platform
 Promotes the idea of writing the code on one platform and running it on any
other
 Java source code is stored in .java files
 Compiler compiles code into .class files
 The compiled code is the bytecode that can run on any platform
 Bytecode is what makes Java platform independent
 Bytecode is not machine code
 Java Virtual Machine (JVM)
 Platform specific - Processes bytecode at runtime by translating bytecode into
machine code
Memory Management
 Automatic garbage collection is built into the language
 No explicit memory management is required
 Occurs whenever memory is required
 Can be forced programmatically
 Garbage collector frees memory from objects that are
no longer in use
Distributed Systems
 Java provides low level networking
 TCP/IP support, HTTP, and sockets
 Java also provides higher level networking
 Remote Method Invocation (RMI) is Java’s distributed protocol
 Used for communication between objects that reside in different Virtual
Machines
 Commonly used in J2EE (Java 2 Enterprise Edition) Application Server
 CORBA could also be used with Java
Identifiers
 Used for naming classes, interfaces, methods, variables, fields, and
parameters
 Can contain letters, digits, underscores, or dollar-signs
 There are some rules that apply:
 First character in the identifier cannot be a digit
 Can be a letter, underscore, or dollar sign
 Literals true, false, and null cannot be used
 Reserved words cannot be used
 And it is Case Sensitive!
Expressions
 Statements are the basic Java expressions
 Semicolon (;) indicates the end of a statement
variable declaration
variable assignment
object creation
message sending
HomePolicy homePolicy;
double premium;
premium = 100.00;
homePolicy = new HomePolicy();
homePolicy.setAnnualPremium(premium);
Comments
/** Javadoc example comment.
* Used for generation of the documentation.
*/
/* Multiple line comment.
*
*/
//
Single line comment.
Literals
 Represent hardcoded values that do not change
 Typical example are string literals
 When used, compiler creates an instance of String class
String one = "One";
String two = "Two";
String one = new String("One");
String two = new String("Two");
Java and Types
 There are two different types in Java
 Primitive data type
 Reference type
 It is said that Java is a strongly typed language
 Fields, variables, method parameters, and returns must
have a type
variable name
variable type
double premium;
HomePolicy homePolicy;
Primitives
 Primitives represent simple data in Java
 Primitives are not objects in Java
 Messages cannot be sent to primitives
 Messages can be sent to other Java objects that
represent primitives
 These are known as wrapper objects in Java (such as Double, Integer, and
Boolean)
Primitive Types
Keyword
Size
Min value
Max value
boolean
true/false
byte
8-bit
-128
127
short
16-bit
-32768
32767
char
16-bit Unicode
int
32-bit
-2147483648
2147483647
float
32-bit
double
64-bit
long
64-bit
- 9223372036854775808
9223372036854775807
Manipulating Numeric Types
 A lesser type is promoted to greater type and then the operation is
performed
12 + 24.56
//int + double = double
 A greater type cannot be promoted to lesser type
 Assigning double value to int type variable would result in a compile error
int i = 12;
double d = 23.4;
i = d;
Type mismatch
Type Casting
 Values of greater precision cannot be assigned to variables declared
as lower precision types
 Type casting makes primitives change their type
 Used to assign values of greater precision to variables declared as lower
precision
 e.g., It’s possible to type cast double to int type
int i = 34.5;
//compiler error - type mismatch
int i = (int)34.5; //explicit type casting
Reference Types
 Reference types in Java are class or interface
 They are also known as object types
 If a variable is declared as a type of class
 An instance of that class can be assigned to it
 An instance of any subclass of that class can be assigned to it
 If a variable is declared as a type of interface
 An instance of any class that implements the interface can be assigned to it
Reference Types (cont.)
 Reference type names are uniquely identified by:
 Name of the package where type is defined (class or interface)
 Type name
 This is full qualifier for the type and it is full qualifier for class or
interface
java.lang.Object
com.kiddcorp.demo.models.Policy
Object Operators
Keyword
Description
instanceof
object type
!=
not identical
==
identical
=
assignment
Creating Objects in Java
 In Java, objects are created by using constructors
 Constructors are methods that have the same name as
the class
 They may accept arguments mainly used for fields initialization
 If constructor is not defined, the default constructor is used
 Use “new” before class name to create an instance
of a class
HomePolicy firstPolicy = new HomePolicy();
HomePolicy secondPolicy = new HomePolicy(1200);
Assignment
 Assigning an object to a variable binds the variable to the object
aHomePolicy
HomePolicy firstPolicy =
new HomePolicy(1200);
1200
aHomePolicy
HomePolicy firstPolicy =
new HomePolicy(1200);
HomePolicy secondPolicy =
new HomePolicy(1200);
firstPolicy = secondPolicy;
aHomePolicy
1200
1200
aHomePolicy
1200
Identical Objects
 Operand == is used for checking if two objects
are identical
 Objects are identical if they occupy the same memory space
int x = 3;
int y = 3;
x == y; //true
3
HomePolicy firstPolicy =
new HomePolicy(1200);
HomePolicy secondPolicy =
new HomePolicy(1200);
firstPolicy == secondPolicy; //false
aHomePolicy
1200
aHomePolicy
1200
Identical Objects (cont.)
 Variables that reference objects are compared by value
 Objects are identical if their memory addresses are the same
 Variables are identical if they refer to the exact same instance of
the class
HomePolicy firstPolicy = new HomePolicy(1200);
HomePolicy secondPolicy = firstPolicy;
firstPolicy == secondPolicy;
//true
aHomePolicy
1200
Equal Objects
 Determined by implementation of the equals() method
 Default implementation is in the Object class and
uses == (identity)
 Usually overridden in subclasses to provide criteria
for equality
HomePolicy firstPolicy =
new HomePolicy(1200,1);
HomePolicy secondPolicy =
new HomePolicy(1200,1);
firstPolicy.equals(secondPolicy);
//false
Null
 Used to un-assign object from a variable
 Object is automatically garbage collected if it does not
have references
 When a variable of object type is declared, it is assigned “null” as a
value
String one = "One";
one = null;
one = "1";
HomePolicy policy;
policy = new HomePolicy(1200);
…
if (policy != null)
System.out.println(policy.toString());
Arrays
 Arrays store objects of specific type
 One array cannot store objects of different types – String and int, for example
 When creating an array explicitly, its size must
be specified
 This indicates the desired number of elements in the array
 Elements in the array are initialized to default values
int arrayOfIntegers[];
arrayOfIntegers = new int[5];
 Creating and initializing arrays at declaration
int[] arrayOfIntegers = {1,2,3,4,5};
 The first element in the array is at the zero index
Arrays (cont:)
 If not using initializer, an array can be initialized by storing elements
at the proper index
int[] arrayOfIntegers;
arrayOfIntegers = new int[3];
arrayOfIntegers[0] = 1;
arrayOfIntegers[1] = 2;
arrayOfIntegers[2] = 3;
 An element of the array is accessed by accessing the index where
the element is stored
Console
int[] arrayOfIntegers = {1,2,3,4,5};
System.out.println(arrayOfIntegers[2]);
 The length property will return the number of
elements in the array
3
Conditional Statements
 x?y:z
 x must be type boolean, y and z can be of any type
 Evaluates to y if x is true, otherwise evaluates to z
(1 == 1) ? 7 : 4 //evaluates to 7
 if, if-else
 Can be nested
 Can omit braces if only one statment
if(condition){
statement1;
}else{
statement2;
}
Conditional Statements (cont:)
 Switch Statement
 Break Allows you to stop after executing an expression
switch(expression){
case value1:
expression1;
case value2:
expression2;
expression3;
default:
expression4;
}
switch(expression){
case value1:
expression1;
break;
case value2:
expression2;
break;
default:
expression3;
}
Looping
 for loop
for(int i=1; i<5; i++){
System.out.println("Index is equal to
" + i);}
Console
Index
Index
Index
Index
is
is
is
is
equal
equal
equal
equal
 while() and doWhile()
int i=1;
while(i < 5){
System.out.println(i)
;
i++;}
int i=1;
do{
System.out.println(i);
i++;
}while(i < 5);
to
to
to
to
1
2
3
4
Java Fundamentals
How to Define a Java Class?
 Java class is defined by using the “class” keyword
 Class name follows the keyword, and by convention starts with capital letter
 For example Policy, Client, House, etc.
 Class access level must be specified before the
class keyword
 Public
 Identifies that any other class can reference defined class
 Not specified
 Identifies that only classes defined in the same package can reference
defined class
 It is the default access level modifier
public class Policy{
…
}
.java Files
 Java classes are contained in .java files
 One file can contain one public class
 One file can contain more than one non-public class
 The file name is the same as the class name contained in the file
package com.kiddcorp.demo.models;
public class Policy{
…
}
Policy.java
Package
 Package groups related classes
 Classes are usually related by their functionality, for example, domain classes, testing
classes, etc.
 Package is identified using “package” keyword
 Package is a unique identifier for a class
 Two classes with a same name cannot be in the same package
 Different packages can contain the same class names
 Compiled Java classes from the same package are compiled into the
same directory
 The directory name matched the package name
package com.kiddcorp.demo.models;
Referencing Classes
 A class must be fully referenced every time when used outside of its
package
 Full qualifier for a class is used
 package name + class name
package com.kiddcorp.demo.tests;
public class PolicyTester{
com.kiddcorp.demo.models.Policy policy;
…
policy = new com.kiddcorp.demo.models.Policy();
}
Import Statement
 Used to identify which classes can be referenced without fully
identifying them
 Specified with “import” keyword
 Can specify a class, or all classes from a package
package com.kiddcorp.demo.tests;
import com.kiddcorp.demo.models.Policy;
public class PolicyTester{
Policy policy;
…
policy = new Policy();
}
Classpath and .jar Files
 Classpath allows Java Virtual Machine to find the code
 CLASSPATH environment variable is used to indicate the root of where packages
are
 Packages are subdirectories under the root
 Compiled Java classes can be packaged and distributed in Java
Archive (.jar) files
 Packages in the .jar file are replaced with directories
 Essentially a zip file with a different extension
Defining Fields
 A field definition consists of:
 Access modifier
 Field type
 Field name
package com.kiddcorp.demo.models;
public class Policy {
private Client client;
private String policyNumber;
private double premium;
}
Initializing Fields
 Fields are initialized when a new instance of a class
is created
 Primitive type fields get a default value
 Numeric primitives are initialized to 0 or 0.0
 Boolean primitives are initialized to false
 Reference type fields are initialized to null, as they do not yet reference any
object
 Fields can also be explicitly initialized when declared
Initializing Fields Explicitly
 Possible when declaring fields
 Not commonly used
 Constructors are generally used for initializing fields
package com.kiddcorp.demo.models;
public class Policy {
private Client client = new Client();
private String policyNumber = "PN123";
private double premium = 1200.00;
}
Field Access Modifier
 There are four different modifiers:
 Public
 Allows direct access to fields from outside the package where the class is
defined
 Protected
 Allows direct access to fields from within the package where the class is
defined
 Default
 Allows direct access to fields from within the package where class the is
defined and all subclasses of the class
 Private
 Allows direct access to fields from the class only
Defining Methods
 Methods are defined with:
 Access modifier – same as for fields
 Return type
 Method name
 Parameters – identified with type and name
 To allow access to private fields, getter and setter methods are
commonly used
package com.kiddcorp.demo.models;
public class Policy {
…
public void setClient(Client aClient){
…
}
}
Constructors
 Special methods used for creating instances of a class:
 Access modifier
 Same name as the class
 Manipulate new instance
package com.kiddcorp.demo.models;
public class Policy {
…
public Policy(){
setClient(new Client());
setPolicyNumber("PN123");
setPremium(1200.00);
}
}
What Are Static Fields?
 Static fields represent data shared across all instances of a class
 There is only one copy of the field for the class
 Static fields are also known as class variables
 Java constants are declared as static final fields
 Modifier final indicates that field value cannot be changed
public class Count{
public static String INFO = "Sample Count Class";
public final static int ONE = 1;
public final static int TWO = 2;
}
Console
System.out.println(Count.ONE);
1
Static Methods
 Define behavior related to the class, not individual instances
 Defined by using the “static” keyword
 Commonly utilitarian in nature (See the Wrapper Classes)
public class Count{
private static String INFO = "Sample Count Class";
public final static int ONE = 1;
public final static int TWO = 2;
public final static int THREE = 3;
public static String getInfo(){
return INFO;
}
}
System.out.println(Count.getInfo());
Console
Sample Count
Class
Defining Inheritance
 In Java, inheritance is supported by using
keyword “extends”
 It is said that subclass extends superclass
 If class definition does not specify explicit superclass,
its superclass is Object class
 Multiple inheritance is not supported in java
public
public
public
public
class
class
class
class
public class Policy{…
Policy{…
HomePolicy extends Policy{…
AutoPolicy extends Policy{…
LifePolicy extends Policy{…
public class Policy extends Object{…
Specialization and Generalization
Item
Text
DateTimeValue
RichTextItem
EmbeddedObjects
embedObject
endSection
…
Specialization
parseXML
containsValue
…
What Is Inherited?
 In general, all subclasses inherit from superclass:
 Data
 Behavior
 When we map these to Java, it means that subclasses inherit:
 Fields (instance variables)
 Methods
Method Overriding
 If a class defines the same method as its superclass, it is said that
the method is overridden
 Method signatures must match
Animal
 Consider Animal class:
talk()
 Dog and Cat as subclasses
 All Animal objects should know how to talk
Animal aCat;
aCat.talk();
Animal aDog;
aDog.talk();
meows
barks
Dog
Cat
talk()
talk()
Interfaces
 Interfaces define a type and protocol, but do not provide
implementation of that protocol
 Classes that implement interfaces must provide implementation for defined
protocol
 Interfaces capture common behavior of the classes that implement them
 Commonly used to:
 Impose protocol on set of classes that implement interface
 Indicate certain Java type (marker interfaces)
 Interfaces are also stored in .java files
public interface Animal{
public void speak();
}
public interface Mammal{
}
Interface Rules
 Interfaces can contain
 Only method signatures
 Only final static fields
 Interfaces cannot contain




Any
Any
Any
Any
fields other than final static fields
static methods
method implementation
constructors
 It is possible that one interface extends other interfaces
 Multiple inheritance is allowed with interfaces
 Inheritance works the same as with classes
Implementing Interfaces
 Classes implement interfaces
 Keyword “implements” is used
 They must define all methods that the interface they implement declares
public class House extends Building implements Policyable{
public Policy createPolicy(){
HomePolicy policy = new HomePolicy(this);
return policy;
}
}
What Are Collections?
 Collections are Java objects used to store, retrieve, and manipulate
other Java objects
 Any Java object may be part of a collection, so collection can contain other
collections
 Unlike arrays, collections do not store primitives
 Unlike arrays, collections can store different objects
 Not all objects in a collection must be the same Java type
Most Commonly Used Collections
 The most commonly used collections include:
 Collection framework collections:
 ArrayList
 HashMap
 HashSet
 Legacy collections re-implemented with the collection framework:
 Vector
 Hashtable
What Is an Exception?
 Exceptions are unexpected conditions in a program
 Like Opening a file that does not exist
 Or Sending a message to an object that object does
not understand
 In Java, there are two different types of exceptions
 Unchecked exceptions
 Runtime exceptions and errors
 Checked exceptions
 All others exceptions
 Exceptions are objects in Java, and they are all of the Throwable
type
Generics
 New Java 1.5 is the concept of the Generic
 In the old days, anything put into a collection was stored as a
generic Object
 Thus when you retrieved it from the collection you had to cast it to
the appropriate type
ArrayList al= new ArrayList();
al.add(new Document());
/*Later in the code we retrieve the value */
Object obj=al.get(0); //Value is retrieved as an object
Document doc=(Document)obj; //Cast to the appropriate type
Generics Save the Day
 No longer is it necessary to cast the value retrieved from a
collection to it’s real type
 This provides significant improvements in code style and type safety
ArrayList<Document> al= new ArrayList<Document>();
al.add(new Document());
/*Later in the code we retrieve the value */
Document doc=al.get(0); //No need to cast
Try – Catch and Finally Blocks
 Exceptions are handled in a try-catch block
 Finally Always executes at the end after the last catch block
 Commonly used for cleaning up resources (closing files, streams, etc.)
public void myMethod(){
try{
//code that throws exception e1
//code that throws exception e2
}catch(MyException e1){
//code that handles exception e1
}catch(Exception e2){
//code that handles exception e2
}finally{
//clean up code, close resources
}
}
Java OO Fundamentals
Agenda
 Roadmap
 Eclipse, The Tool
 Java language Fundamentals
 Developing Java code In Eclipse
 Develop and Deploy Lotus Domino Java code using the Eclipse
development platform
Wrap Up
Java Development Tooling – JDT
 Eclipse’s Java Development Environment is often referred to as Java
Development Tooling (JDT)
 Using the JDT you can do the following things with
Java programs:
 Write
 Compile
 Test
 Debug
 Remember the right click menu!
Perspectives
 When developing Java code, commonly used perspectives include:
 Java Perspective
 Designed for working with Java projects
 Java Browsing Perspective
 Designed for browsing the structure of Java projects
 Java Type Hierarchy Perspective
 Designed for exploring type hierarchy
 Debug Perspective
 Designed for debugging Java programs
Java Perspective
 Contains:
 Outline View
 Editor area
 Package Explorer View
 Hierarchy View
 Tasks View
Java Browsing Perspective
 Contains:
 Editor area
 Projects View
 Packages View
 Types View
 Members View
Java Type Hierarchy Perspective
 Contains editor area and Hierarchy View
Build Path Project Preferences
 You can set global preferences for
a project
 Select Window → Preferences to get
Preferences View
 Good idea to separate your Java files
into source and compiled directories
(src and bin)
 This action only needs
to be done once
 Done for all
subsequent projects
Tip
Creating a Java Project
 Projects are used to organize
resources (source, class, icons) for a
project
 To create a Java project:
 Select Window → New → Project … from
the menu
 The New Project wizard
comes up
 Select Java → Java Project
 Click Next
New Project Wizard

Specify a Project name

Click Next
New Project Wizard: Java Settings
 Specify Java Settings
 Output folder (where compiled files will
be stored)
 Any external JAR files the project
depends on
 Classes from other projects that are
referenced in
the project
 Click Finish
Project Properties
 You can change the Java build path at any time
 Choose Properties from the context menu on the project
Creating Packages
 A package contains
Java class files
 To create a package for
a project:
 Select the project
 Choose New → Package from the
context menu
 Specify package Name
 Click Finish
Creating Classes
 To create a class in a package:
 Select the package
 Choose New → Class from the context
menu
 The Class wizard comes up
 Specify class details
 Click Finish
Using Code Assist
 When activated, code assist opens a list of available code
completions
 Code Assist activates by Crtl+Space
 Activates automatically when a message needs to be sent to an object (after the
dot is typed)
Tip
Using Quick Fix (Ctrl + 1)
 Useful if Java compiler shows errors
 Gives options for fixing the errors
 Activated through Edit → Quick Fix menu option
Error indication
Searching for Java Classes
 When developing Java applications, a good search mechanism is
very important
 You often search for class, method declarations,
and references
 It is important to be able to find things quickly
 Eclipse Java Search mechanism is very extensive
 It allows you to search for:
 Types, methods, constructors, packages, and fields
 Declarations, Implementers, References
 In the scope of Workspace, Working Set, or
Selected Resources
Organizing Java Code
 Eclipse comes with extensive support for organizing and refactoring
Java code
 It is possible to:
 Generate getters and setters for the fields
 Organize missing import statements
 Move fields, methods, classes
 Rename methods, classes, packages
Generating Getters and Setters
 Available for creating get and set methods on the fields
 It supports encapsulation activated by choosing Source → Generate Getter and
Setter from the editor’s context menu
Source Navigation
 F3
 Navigate to a type, method, or field at the current
cursor location
 Ctrl+Shift+T
 Open an editor for any Java type that is available in
your workspace. Also via menu Navigate → Open
Type … or using a toolbar icon
 Ctrl+O
 Open a lookup dialog containing a list of members in
the current Java editor. Start typing characters to limit
the list and press Enter to go to the selected member.
A quick alternative to the Outline view.
 Ctrl+F3
 Open a member lookup dialog for the class name at
the current cursor location, e.g., position on Date and
press Ctrl+F3 to review and lookup
one of its methods
Source Code Templates
Provides a “shorthand” for common programming tasks
Eclipse JDT
Agenda
 Roadmap
 Eclipse, The Tool
 Java language Fundamentals
 Developing Java code In Eclipse
 Develop and Deploy Lotus Domino Java code using the Eclipse
development platform
 Wrap Up
Develop and Deploy Lotus Domino Java Code
 Java versions by Domino release
 ND 8
 Java 5 (1.5) / with notes.ini variable set
– JavaCompilerTarget=1.5
– Notes/Domino will compile agents using
– javac -source 1.5 -target 1.5
 Java 5 (1.5) / without notes.ini varibale set (Default)
– Notes/Domino will compile agents using
– javac -source 1.3 -target 1.2
Develop and Deploy Lotus Domino Java Code
 Java versions by Domino release (cont)
 ND 7
 Java 1.4.2
 ND 6
 Java 1.3.1
 ND 5
 Java 1.1.8
Develop and Deploy Lotus Domino Java Code
 If you attempt to compile code using Java features not supported
by the runtime you will receive compile errors
 The following is an example of running Java 1.5 specific code in
Notes without the .ini variable set
Develop and Deploy Lotus Domino Java Code
 The JVM in ND 8 is enabled with a Just-in-Time compiler or JIT
 Using JIT compiling will improve performance in the execution of
Java code running on the ND 8 platform
 JIT is enabled by default
 Although it is not recommended you can turn off the JIT using the following .ini
variable
 JavaEnableJIT=0
Develop and Deploy Lotus Domino Java Code
 The Problem
 The Domino Designer client does not include support for Java code completion
or Pop-up syntax help
 The Solution
 Write your Java agents in a Java IDE !!
 Eclipse
 Rational Developer
 Any IDE that lets you access the .java file from the file system
Develop and Deploy Lotus Domino Java Code
 Steps for Developing and Deploying Domino Java Code
 Create a Java Project in Eclipse
 Create Agent source code in Project
 Create a new Agent in the Designer Client
 Import source code from Eclipse into new Agent
Develop and Deploy Lotus Domino Java Code
 Create a New Java Project in Eclipse
 File > New Project > Java Project
 Provide a Project Name
 Select the JRE
– If you have configured Domino as one of your JREs in the workbench
preferences you can select it here
 Project Layout
– Keep separate source and output folders
 Click “Next”
Develop and Deploy Lotus Domino Java Code
 In the Java Settings Dialog
 Click on the “Libraries” tab. This is your projects “Classpath”
 This will allow you to add additional libraries to your project
– If you are NOT using the Domino 7 or 8 JRE you will need to add the
“Notes.jar” file here
 Click “Add External Jars”
– Navigate to the proper Domino Directory
 Domino 6
– C:\installdir\notes
 Domino 7
– C:\installdir\jvm\lib\ext
 Domino 8
– C:\installdir\jvm\lib\ext
 This is also where you would add third party
jar files to your project
 Like JDBC drivers, etc
Develop and Deploy Lotus Domino Java Code
 The majority of Java Agents are created without a package
reference
 Although it’s a good idea to start getting in the habit of using them
 Packages are typically the reverse DNS name of your organization
 com.acme.code
 In the “src” folder of your Java Project
 Create a package or use the “default package”
 Create a new Java Class
 Right click package or folder and choose “New > Class” from context menu
 This will open the “Java Class” wizard
Develop and Deploy Lotus Domino Java Code
 Provide a Class name in the
Name field
Do not use the value of
“JavaAgent” that is used by default
in all new Java Agents in the
Domino Designer Client
Choose a name that is indicative of
the agent functionality
Do not worry about any of the
other settings
Click the “Finish” button to create
the class and open the source code
in the Java Editor
Develop and Deploy Lotus Domino Java Code
 The code created by the wizard will need to be replaced with
template code created from a new agent in the Designer Client
 You can
 Cut / paste the new agent template code from the Designer Client
 Create a “Snippet” in Eclipse that stores the new agent template code
 Use Plug-in provided (See resources at end of presentation)
import lotus.domino.*;
public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
// (Your code goes here)
} catch(Exception e) {
e.printStackTrace();
}
}
}
Develop and Deploy Lotus Domino Java Code
 Every newly created agent in the Designer Client creates a new
source file with a class name of “JavaAgent”
 When the template code is added in eclipse this name will need to
be “re-factored” to the name of the class created in Eclipse
 Click on the “light bulb” and choose “Rename type to…”
Develop and Deploy Lotus Domino Java Code
 Write the Java code in Eclipse to complete the agent functionality
 Save the source code and ensure that there are no compile errors
 Toggle back to the Designer Client
 Create a new Java Agent and give it the same name as the class in Eclipse
 Click the “Edit Project” button on the bottom of the agent IDE in the Notes
designer client
 Change the “Base Directory” to point to the source folder of your eclipse project
 You will only have to do this once per database
 Add the source (.java NOT the .class) file to the Current Agent files
 One time only
 Change the “Base Class” to the new source file name
 Delete “JavaAgent.java” from the file list
 Click “OK” to import the source code
Develop and Deploy Lotus Domino Java Code
Develop and Deploy Lotus Domino Java Code
 The first time the source code is imported the code block will be
collapsed
 Click the “+” to expand it
 When changes are made to the source code in Eclipse
 Click the “Edit Project” button
 Click on the “Refresh All” button
 Click “OK”
 You will be prompted that the source code will be replaced with the
new code
 Click “Yes” to accept the new code changes
Develop and Deploy Lotus Domino Java Code
 Using this procedure
 Make sure you ALWAYS make your code edits in Eclipse
 Import the changes to Existing/New agents in the Notes Designer Client
Develop and Deploy Lotus Domino Java Code - DEMO
Develop and Deploy Lotus Domino Java Code
 Java Domino Objects
 Only implements the “Back End” objects
 Exposed via:
 lotus.domino.*
 No Notes Client UI access
 You (the Domino Developer) already know 98.67 percent of all of the Java class
names for the Domino Back End Objects
 Drop “Notes” from the front of the LotusScript class names and you have the
Java class names
LotusScript Class Name
Java Class Name
NotesSession
Session
NotesDatabase
Database
NotesView
View
NotesDocument
Document
NotesItem
Item
Develop and Deploy Lotus Domino Java Code
 Performance
 Java Agent vs. LotusScript Agents
 Java Agents and LotusScript Agents call the same C++ code base
 On an “Apples to Apples” comparison they will perform equitably
 Classes the don’t exist in LotusScript
 AgentBase
 AppletBase and JAppletBase
 NotesError
 NotesException
 NotesFactory
 NotesThread
Develop and Deploy Lotus Domino Java Code
 AgentBase
 Java Agents MUST extend AgentBase
 Java Agents MUST use the NotesMain() method as the entry point for their
functional code
 The getSession() method creates a Session object
import lotus.domino.*;
public class MyFirstAgent extends AgentBase {
public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
// (Your code goes here)
}catch(NotesException ne){
ne.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
}
Develop and Deploy Lotus Domino Java Code
 Primary problems LotusScript Developers have transitioning to Java
 Compile Errors
 Java is Case Sensitive
– Yes, it’s always going to be case sensitive
 Data typing errors
– Java has no “Variant” data type
 Most common runtime Errors
 Null Pointer exception
 Class not found exception
Develop and Deploy Lotus Domino Java Code
 Domino Java agents have no access to the Notes UI
 System.out.println() commands can be inserted into the code
 Output from agents is written to the “Debug Console” from code executed on
the client
 Output from agents is written to the Server Console from code executed on the
server
 Access the Debug Console via the menu “File > Tools > Show Java
Debug Console” from the Notes/Designer client
Develop and Deploy Lotus Domino Java Code
 Code Walk Through
 Session
 AgentContext
 Database
 View
 Documents – Read
 Help Doc Count Agents
 Documents – Create
 Items – Read
 Items – Create
 Run on Server
 Java Network I/O
 HTML Output
 XML Output
 Web Query Save
Wrap Up
 Follow the “Roadmap”
 J2SE
 Servlets, JSPs, JSFs
 XML
 Rest of J2EE Specification
 Start with Java Agents in Domino
 Transition to Servlets running on the Domino Server
 Transition to external Servlet/JSP – J2EE server
 Avoid the Roadblocks !
 Introduce yourself to Plug-ins and the Eclipse Rich Client Platform
Other Java Related Sessions you should checkout
 AD201 - The Amazing IBM Lotus Notes 8: Extendable with Plug-ins
 AD203 - Developing Secure Java Applications in IBM Lotus Notes 8
 AD301 - IBM Lotus Domino Designer
 AD308 - Java 5.0 for IBM Lotus Notes and Domino
 AD312 - Using Eclipse To Develop, Debug and Test Java Agents
 HND102 – IBM Lotus Domino Designer in Eclipse
 HND103 – Developing Eclipse Plug-ins to Extend IBM Lotus Notes 8
Wrap Up
 References and Resources
 NNSU
 Domino Eclipse/RAD plug-in
 http://www.nnsu.com
 TLCC
 Java Computer based training via Notes Databases
 http://www.tlcc.com
 O’Reilly Publishing
 Head First… Series
 http://www.oreilly.com
 SUN Developer Network
 http://java.sun.com
 Eclipse Project
 http://www.eclipse.org
Wrap Up
 References and Resources (cont)
 Apache Jakarta Project
 http://jakarta.apache.org
 Developer Works
 http://www.ibm.com/developerworks
 http://www.ibm.com/developerworks/java
 http://www.ibm.com/developerworks/lotus
 http://www.ibm.com/developerworks/xml
Remember the On-line Evaluations !!!!!
 Please take a moment after this session to fill out the on-line
evaluations !!!!!!
You got Questions? We got Answers !
[email protected]
[email protected]