Debugging utility in XPages

Time to re-open an previous XPages project and add some new functionalities for the users. When I did this today I noticed some programming that I never wrote about before, so I do this now 🙂

In an application I wanted to monitor individual classes and write the findings to the console or OpenLog. All managed from a web UI 🙂

The printing method in this app is pretty simple:

public void printToConsole(String msg){
String callerClassName = getCallerCallerClassName();
if (debugClasses.containsKey(callerClassName)){
if (false != debugClasses.get(callerClassName)){
if (true == this.openLog){
OpenLogUtil.logEvent(new Throwable(), msg, Level.INFO, null);

The getCallerClass method identifies which class is calling this method:

public static String getCallerCallerClassName() {
StackTraceElement[] stElements = Thread.currentThread().getStackTrace();
String callerClassName = null;
for (int i=1; i<stElements.length; i++) {
StackTraceElement ste = stElements[i];
if (!ste.getClassName().equals(KDebug.class.getName())&& ste.getClassName().indexOf(“java.lang.Thread”)!=0) {
if (callerClassName==null) {
callerClassName = ste.getClassName();
} else if (!callerClassName.equals(ste.getClassName())) {
return ste.getClassName();
return null;

Here is the KDebug class :

package com.wordpress.kwintessential.myproject.utils;

public class KDebug {
public static String getCallerClassName() {
StackTraceElement[] stElements = Thread.currentThread().getStackTrace();
for (int i=1; i<stElements.length; i++) {
StackTraceElement ste = stElements[i];
if (!ste.getClassName().equals(KDebug.class.getName()) && ste.getClassName().indexOf(“java.lang.Thread”)!=0) {
return ste.getClassName();
return null;

debugClasses is declared as followed:

private HashMap<String,Boolean> debugClasses = new HashMap<String,Boolean>();

And it is set when I initiate my utility class that contains the printing method:

public void setDebugClasses(){

try {
Properties debugs = this.getDebugProperties();
Enumeration<String> enums = (Enumeration<String>) debugs.propertyNames();

while (enums.hasMoreElements()) {
Boolean boolean1 = false;
String key = enums.nextElement();
String value = debugs.getProperty(key);
boolean1 = Boolean.valueOf(value);
debugClasses.put(key, boolean1);
} catch (IOException e) {


method getDebugProperties reads a properties file that is stored in the WebContent directory:


public Properties getDebugProperties() throws IOException {
InputStream input = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream(“”);
Properties debugs = new Properties();
return debugs;

Note that I have to place the file in the WebContent folder and I could not place it as a Resource design element.

The file contains key values pairs e.g.:


So with this code above I can:

  • check which class is calling the print method
  • check if this class should be monitored (read: print data to the console or OpenLog)

So all great! But in my case I wanted to prevent that the design had to be opened and rebuild every time the monitor settings should be altered.

So in this project I created a web UI (XPage) to read and change the file:

<?xml version=”1.0″ encoding=”UTF-8″?>
<xp:view xmlns:xp=””&gt;

<xp:this.beforePageLoad><![CDATA[#{javascript:var file = param.get(“filename”)
if (null == file || file == “”){
ctx = facesContext.getExternalContext();
ctx.redirect(facesContext.getExternalContext().getRequest().getRequestURL() + “?”);
<xp:panel styleClass=”container”>

<h1>Debug <small> Monitor Java classes</small></h1>

disabled=”true” />
<xp:br />

cols=”80″ />
<xp:br />
<xp:panel styleClass=”btnBar”>
<xp:button value=”Load” id=”buttonLoad”
<xp:eventHandler event=”onclick” submit=”true”
refreshMode=”complete” action=”#{fileBean.loadData}”>
<xp:button value=”Save” id=”buttonSave”>
<xp:eventHandler event=”onclick” submit=”true”
refreshMode=”complete” action=”#{fileBean.saveData}”>

Here is how it looks in a browser:

web ui initial.JPG

When I press the Load button the content of the file get’s loaded:

web ui loaded

And of course when I press the Save button the file get’s updated 🙂

Now where is the magic in this? I got two methods fileBean.loadData and fileBean.saveData.

Here is what the fileDataBean class looks like:

import java.util.Properties;

import lotus.domino.NotesException;

import com.paulwithers.openLog.OpenLogUtil;

public class FileDataBean implements Serializable {

private static final long serialVersionUID = 1L;
private String fileName;
private String fileData;
private String dbPath;
private String dbServer;

private Utils utils;
Properties props;

public FileDataBean() throws NotesException{
utils = new Utils();
utils.printToConsole(this.getClass().getSimpleName().toString() + ” – FileDataBean() // constructor”);
try {
props = utils.getDataSourceProperties();
dbServer = props.getProperty(“server_namis_notesname”);
dbPath = utils.getSession().getCurrentDatabase().getFilePath();
} catch (IOException e) {

public String getDbPath() {
return dbPath;

public void setDbPath(String dbPath) {
this.dbPath = dbPath;

public String getDbServer() {
return dbServer;

public void setDbServer(String dbServer) {
this.dbServer = dbServer;

public void setFileData(String fileData) {
this.fileData = fileData;

public String getFileData() {
return fileData;

public void setFileName(String fileName) {
this.fileName = fileName;

public String getFileName() {
return fileName;

public void loadData() {
this.fileData = NAPIUtils.loadFile(this.dbServer, this.dbPath, this.fileName);

public void saveData() {
NAPIUtils.saveFile(this.dbServer, this.dbPath, this.fileName, this.fileData);

The NAPIUtils looks as followed:

import com.paulwithers.openLog.OpenLogUtil;

public class NAPIUtils {

* loads a given WebContent file and returns the result as String
* @param serverName
* the server to use
* @param dbPath
* the database path
* @param fileName
* the file to load
* @return the file data as String
static public String loadFile(final String serverName, final String dbPath, final String fileName) {
NotesSession nSession = null;
NotesDatabase nDatabase = null;
NotesNote nNote = null;
try {
nSession = new NotesSession();
// open database
nDatabase = nSession.getDatabaseByPath(serverName + “!!” + dbPath);;
// load existing data
nNote = FileAccess.getFileByPath(nDatabase, fileName);
// get Filedate and return String
InputStream is = FileAccess.readFileContentAsInputStream(nNote);

return convertStreamToString(is);
} catch (NotesAPIException e) {
} finally {
// recycle NAPI objects
recycleNAPIObject(nNote, nDatabase, nSession);

return fileName;

* loads a given WebContent file and returns the result as String
* @param serverName
* the server to use
* @param dbPath
* the database path
* @param fileName
* the file to load
* @param fileData
* the data of the file
static public void saveFile(final String serverName, final String dbPath, final String fileName, final String fileData) {

NotesSession nSession = null;
NotesDatabase nDatabase = null;
NotesNote nNote = null;

try {
nSession = new NotesSession();
// open database
nDatabase = nSession.getDatabaseByPath(serverName + “!!” + dbPath);;
// load existing data
nNote = FileAccess.getFileByPath(nDatabase, fileName);
// store them to note
FileAccess.saveData(nNote, fileName, fileData.getBytes());
} catch (NotesAPIException e) {
} finally {
// recycle NAPI objects
recycleNAPIObject(nNote, nDatabase, nSession);

* converts an input stream to a string
* @param is
* the input stream to convert
* @return String
static String convertStreamToString( is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter(“\\A”);
return s.hasNext() ? : “”;

* recycleNAPIObject helper method for recycling NAPI objects
* @param nObjects
* the NAPI objects to recycle
static void recycleNAPIObject(NotesObject… nObjects) {
for (NotesObject nObject : nObjects) {
if (nObject != null) {
try {
} catch (NotesAPIException ne) {

From this class I use the loadFile and saveFile methods. Look at the import of the classes which enables me to access files within the NSF!

Hopefully this writing gives you some new ideas for your development projects. Happy coding 🙂

Java and XPages

I still meet Domino developers who want to use XPages in the way IBM told them how to use it. Completely wrong I would say. Now that you have to chance to learn Java skills – EMBRACE IT!

Okay the other way around is sometimes a pain in the ass and complete examples are scarce but it gives you a better way to control your application and it’s behavior. After a while you get better understanding of the underlying technique and there is nothing wrong with that.

At the end it is mostly about creating, reading, updating and deleting stuff.

So for those developers I have setup a simple presentation to get a better understanding of JSF, Java and XPages:

Threads and Jobs on release 9

On OpenNTF you can find a project called Thread and Jobs:

This project contains samples showing how to create threads and Eclipse jobs from XPages to run longer taking operations asynchronously without blocking the XPages user interface or to run scheduled tasks.

It turns out that there have been updates in Release 9 in the ThreadSessionExecutor class and no longer a Status object is returned.

So if you want to have the example application working on your Domino 9 server you should remove the Status/iStatus library import and change the return type from Status to Object as followed:

* © Copyright IBM Corp. 2012
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* implied. See the License for the specific language governing
* permissions and limitations under the License.
package org.openntf.samples.thread;

import java.util.Date;
import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.NotesException;
import lotus.domino.Session;

* Thread sample
* @author priand
* @author Niklas Heidloff (simplified sample)
* Updated 2016-10-18 Patrick Kwinten

public class ThreadSample {

private static MyThread myThread;

public static boolean isRunning() {
return myThread != null;

public static void startThread() throws NotesException {
if (myThread != null) {
try {
synchronized(ThreadSample.class) {
if (myThread == null) {
myThread = new MyThread();
System.out.println(“Thread started”);
} catch (Throwable t) {

public static void stopThread() {
if (myThread != null) {
synchronized(ThreadSample.class) {
if (myThread != null) {
myThread.stopRequest = true;
myThread = null;
System.out.println(” >> Thread stopping”);

private static class MyThread extends Thread {
boolean stopRequest;
private ThreadSessionExecutor < Object > executor;
MyThread() throws NotesException {
this.executor = new ThreadSessionExecutor < Object > () {
protected Object run(Session session) throws NotesException {
try {
System.out.println(” >> Thread running”);
Database db = session.getDatabase(null, “ThreadsJobs.nsf”);
if (db != null) {
if (!db.isOpen());
if (db.isOpen()) {
System.out.println(” >> Database opened: ” + db.getTitle());
Document doc = db.createDocument();
try {
doc.replaceItemValue(“Form”, “ThreadTest”);
doc.replaceItemValue(“DateTime”, session.createDateTime(new Date()));;
} finally {
} catch (Throwable ex) {
if (!stopRequest) {
try {
Thread.sleep(5 * 1000);
} catch (InterruptedException ex) {
//return Status.OK_STATUS;
return “probably OK”;

public void run() {
while (!stopRequest) {
try {;
} catch (Exception ex) {}
System.out.println(“Thread left”);

I have not yet managed how to fix the JobScheduler class…

User Information class

For a project I needed to check which rights a user has in a database and on documents. The UserBean in the Extension Library did not seem to match my needs since that the rights of the current effective user against the database from which the code is called. In my case the documents reside in different databases than the one serving the Xpages.

Second stop was the UserBean class written by Oliver Busse. This allows me to change the effective username againt a given name.

Name name = session.createName(“a string here”);

The same principle goes for the database.

this.aclLevel = session.getDatabase(“a string here”,”a string here”).queryAccess(this.userNameCanonical);

The aclPriviliges are no part of the original code so I added something as followed:

public final List<String> aclPriviliges = new ArrayList<String>();

if ((accPriv & db.DBACL_CREATE_DOCS) > 0){
if (!aclPriviliges.contains(“DBACL_CREATE_DOCS”)){
if ((accPriv & db.DBACL_DELETE_DOCS) > 0){
if (!aclPriviliges.contains(“DBACL_DELETE_DOCS”)){

I registered my class as a Managed Bean and I invoke it from the beforepageload event:

<xp:this.beforePageLoad><![CDATA[#{javascript:UserInfo.init(“location of my nsf”,”Ja user name here”);}]]></xp:this.beforePageLoad>

With this I can use it in the same way Oliver demonstrated in his snippet. But I can also check the acl Priviliges:

<xp:label value=”ACL Priviliges:”></xp:label> 
<xp:label value=”#{javascript:UserInfo.aclPriviliges}”></xp:label>

Next I wanted to check if a user can edit a document or not. This is not that easy as it might seem. This code is what I took as starting-point. It became this piece of code:

public boolean canEdit(String docId){
boolean canEdit = false;
NotesContext ctx = new NotesContext(null).getCurrent();
Session session = null;
session = getCurrentSession();
try {
Database db = session.getDatabase(“”, this.activeDb);
Document doc = db.getDocumentByUNID(docId);
if(null != doc){
canEdit = ctx.isDocEditable(doc);
} catch (NotesException e) {
// TODO Auto-generated catch block
return canEdit;

Unfortunately I cannot set the username, so it runs against against the effective username 😩

I tried to achieve the same functionality with the lock function for documents (which you have to enable on database level) but it fails. Even a given user with Reader access was able to lock a document (name added to $Writers field)

public boolean canWriteDocument(String docId, String userName) {
boolean canWrite = false;
Session session = null;
session = getCurrentSession();
try {
Database db = session.getDatabase(“”, this.activeDb);
if (db.isDocumentLockingEnabled()) {
//Document locking is enabled
Document doc = db.getDocumentByUNID(docId);
if (null != doc){
if (doc.lock(userName)) {
canWrite = true;

} else {
//Document locking is NOT enabled

} catch (NotesException e) {
// fail silently
return canWrite;

I assume I am doing something wrong but I am not sure what. If you happen to know what then drop a comment.

Happy development 🙂

Add 20 years of experience to your workforce

You can 20 years of experience within IBM Notes and Web development to your workforce by hiring me.

Interested? Read my curriculum vitae on LinkedIn: and get in contact.

I am happy to work WITH you !

Lotuscript to Java Beans

It seems that video tutorials / webinars have become the standard in education. Probably because video seems to be considered as one of the most trustworthy source of information. The less positive effect of video tutorials is that they (still) take time.

During a trip lately I was able to watch the excellent Lotuscript to Java Beans webinar, presented by Tim Tripcony.

Below you find screenshots of moments I thought were most interesting:









Go watch that show if you have not already!

Job Wanted

Looking for a creative brain? Choose me!


ZĂ€hme den Tiger – Java entwicklung in Notes & Domino


Today I read this presentation by Bernd Hort held at EntwicklerCamp 2012. Judging fromthe different presentations this sounds like a excellent conference!

This was a nice opportunity to rehearse my German. In case your German is not so good I recommend to listen to a couple of albums of these guys and you will do just fine =)

The presentation can be divided into 2 parts:

  • A general introduction to the Java language.
  • Java in Notes & Domino in details.

Introduction Java language

A nice introduction to the language. More details I found in the jumpstart Eating the elephant.

Java in Lotus Notes & Domino

This section is divided into:

  • Applets
  • Agents
  • Servlets
  • Standalone applications
  • XPages
  • Plugins
  • Other


If you are interested in writing applets The View has published plenty of articles about this.


  • Similar to LotusScript agents.
  • Only back-end documents.
  • Notes:
    • Scheduled agents.
  • Web:
    • WebQueryOpen / WebQuerySave.

Also here check The View knowledge database to find articles on Java agents.


Also a walk through memory lane…

  • Advantage in comparison to Java agents is than once the servlet is initiated the second call is much faster.

Standalone applications

Read: Java access to the Domino Objects (DeveloperWorks)


Lots of examples of Java with XPages here (setup folder structure, import jar files, notes.ini variables, writing Java classes from Package explorer…)


  • Web services
  • OSGI tasklets ( Java based server tasks)


Another great presentation on Java in Notes & Domino. However most parts are also discovered in the other presentations I described earlier. Nevertheless recommended to read (if you like German).

AD104 Intro to Managed Beans (Russel Maher, MWLUG 2012)


This presentation was not on my initial ‘to-study list’ so I will add it there. The presentation was held by Russel Maher at the MWLUG 2012.

Not surprisingly this presentation has quiet some familiarities with the presentation Improving XPages Application Performance with Managed Beans (Russell Maher, The View Advanced Xpages 2012).

Same presenter, similar topic.


Why use beans, managed beans and Java in your XPage projects:

  • Makes your applications run faster.
  • Reduces app maintenance efforts.–
  • Improves reliability.
  • Makes you a stronger developer.
  • Enhances your professional skills.


Arguments why objects are sometimes better:

  • A user can have more metadata than just their name and roles.
  • A “document” might actually be comprised of multiple records.
  • An “environment” might be comprised of multiple databases.

Reduce complexity by embracing complexity:

  • Managing a large number of scoped variables in your app.
  • Dynamic visibility of scoped variables.
  • Progressive disclosure of scoped variables.

Requirements of a Managed:

  • Java class.
  • No-argument constructor.
  • Expose properties/fields through getters/setters.
  • Serializable.


Sometimes data needs to stick around for a while:

  • “Data” can be a number or a complex object.
    • Session scoped.
    • A session is generally there until the user shuts down the browser.

Persistence allows you to know what the user did “before”

  • They saved a document.
  • They saved as draft/complete.

Beans can be scoped to application, session, view, request or none.

Managed Properties

Sharing data between beans:

  • Representing complex objects typically results in a “logical normalization” of data.
    • You generally will include all of the data about an object with that object.
    • You generally will not duplicate that data into other objects.
  • You want to pass data from one bean to another.
  • You need to read some existing data in another bean from your current bean.

Options for sharing data across beans:

  • Using variable resolvers:
    • Accessing other beans through the session map.
    • See: Persistence
  • Using managed properties:
    • One bean is a property of another bean.
    • Configured in faces-config.xml.
    • Object declared within “container” bean.
    • At run time the managed property is “injected” into the container bean.

Defined in Faces-config.xml:


How to use the managed property:



Managed property setter:



Use proper scopes and load order

  • Use the longest scope you need that will work:
    • Issues with viewScope serialization?
      • -> Use session scope.
    • Only need the bean for a single request?
      • -> Request scope.
    • Don’t need the bean to persist at all?
      • -> None scope.
  • Every bean must be called before it can be used.
    • If you try to inject Bean B into Bean as a managed property and Bean A has not been instantiated yet
you got problems.
    • If Bean A errors out but is a managed property of Bean B
you got problems.

Use Java UUID to get unique numbers

  • Designed to give universally unique numbers.
  • Beats inventing your own mechanism if all you need is a unique number.




Another great introduction on managed beans, especially if you didn’t get a hold on the Improving XPages Application Performance with Managed Beans from The View conference.

I haven’t taken a look at the example database, which was demonstrated during this presentation but that is something I will surely will when returning back to the office on Monday.

That’s it for this week. It has been a great 5 days of Java in Xpages. For next Monday I schedule ‘ZĂ€hme den Tiger – Java-Entwicklung in Notes und Domino‘. Looking forward to rehearse my German skills!

AD102 – Caffeinate your XPages with Java (Jeremy Hodge, MWLUG 2012)

Time for another step in my road ahead. This time I have studied Jeremy Hodge’s presentation at MWLUG 2012. As promised Jeremy will demonstrate how to inject Java in XPages application.

Getting started with Java in Designer & XPages

Java Design element in DDE 8.5.3:

Roll out your own project structure via Package Explorer Eclipse view:

You can customize your Java perspective:

  • Should reside in the WebContent folder.

What is a package?

  • A way to organize source code modules by using a namespace to prevent class collisions.
  • Provides some access control on what is accessible between classes.


In the Package Explorer right click your source folder, select New > Package.

Naming conventions for a Package:

  • Everything lower-case.
  • Reverse domain name; Project . Sub-Project
    • e.g. com.zetaone.mwlug.ad102

Java class

In the Package Explorer right click the Package, select New > Class.

Naming conventions for a Java class:

  • UpperCamelCase class name.

The new empty class is created & opened in the workspace.


Access levels to methods:

  • Public:
    • Any other class, in any package can access the method or member field.
  • Private:
    • Can only be accessed within the same class.
  • Protected:
    • Can be access by the current class, sub-classes, or any other class in the same package.
  • Blank:
    • Can be access by the current class, and any other class in the package, but not any sub-classes (unless they are in the same package).

Access Java packages via import e.g. import java.util.Date

Managed beans


Nothing more than a simple Java class.

Managed bean

A Java Class that conforms to a specific set of requirements so that the XPages runtime can create / discard / etcetera our beans for us when we want to use them. In other words it manages our beans for us.

Three things required for a managed bean:

  1. Argument-less constructor.
  2. Serializable.
  3. Uses Getters / Setters to retrieve / store information.


A method:

  • With no return type.
  • Has the same name as the class itself.
  • Does not take any arguments.

public class HelloWorld {

public HelloWorld(/*SEE NO ARGUMENTS HERE*/) {
// Do initialization here if you need to…


The process by which an instance of a class is stored in a form that can be written to disk, the network, etcetera and then converted back into a full in-memory instance (save/restore state).

In most cases basic classes can be serialized by implementing Serializable and adding serialVersionUID.


implements Serializable

This is called an interface. An interface is a predefined set of procedures for transmitting data between different objects.

Serializable defines 2 Methods:

  • writeObject
  • readObject

serialVersionUID tracks what version of this class is so that objects serialized with one version are not de-serialized into a different, incompatible version.

private static final long serialVersionUID = 1L;

  • static:
    • The declaration belongs to the class, not the instance, and all instances of the class share the same serialVersionUID instance (not value) and it is stored with the class.
  • final:
    • Once it is assigned, it can’t be assigned again – Must be assigned when its declared, or in the constructor.
  • 1L:
    • The “L” makes the 1 and long integer (64bit), rather than the default 32bit integer.

Getters / Setters

Use getters / setters to retrieve or set values inside your class instance.

Declaring a managed bean

  • Modify faces-config.xml located in WebContent/WEB-INF.
  • Set the Name, Class, and Scope of the Managed bean.

Usage within Xpages

Use the managed-bean-name to reference the class. Expression Language (EL) converts “helloWorld” to getHelloWorld() and “helloWorldDateTime” to getHelloWorldDateTime() when reading the value from the bean.


But we also defined a setHelloWorld() method. How do we use that to store a value back into our instance?

  • If the component that you bind to your class member is read/write (ie a text box), it will call the setter to update the new value automatically. You don’t have to do anything else!

EXCEPT: Since we are now storing data in the instance that we want to stick around between requests, we need to update our faces-config.xml to set a different managed-bean-scope, such as view.














After refresh:


Reading & Writing Notes Data in Java

Similarity between reading / writing Notes data in LotusScript, Server-Side JavaScript and Java.


Tips, Tricks, & Gotchas

Compare objects

If you want to compare two objects, the objects need to have a .equals() or .compareTo() method.

If (string1 == string2) {
// This is (almost) never true

For objects, the == checks to see if the left and right objects are the same object (same address in memory), not if the string has the same content.

If (string1.equals(string2)) {
// Now we’re cooking


There is also an .equalsIgnoreCase()

Getting SSJS Common Instances in Java

SSJS is a scripting language, so several objects are pre-defined and immediately available to you (ie database, session, sessionAsSigner).

In Java, you need to get the variable resolver, and resolve those variables to get at those objects.


There is a set of helper functions in a JSFUtil class that Jeremy as posted on OpenNTF In a project called mypic (


At some point you are likely to want to use a 3rd party class. You’ll put the JAR file in your NSF, add it to the build path, and you’ll think life is grand, until.


Two main reasons you will get this error

  • Unresolved Dependencies.
    • You didn’t add all the JARs you needed to.
  • The Security Manager is telling you to take a hike, it doesn’t trust that “type” of code in an NSF.

Edit your $INSTALL\jvm\lib\security\java.policy file and grant all permissions to all code.

Serialization, Recycle, and Notes objects

You can not serialize lotus.domino.* classes, ever, AND they WILL get recycled on you – so having them as class members is dangerous!

Biggest reason is recycle(). Recycling is unique to the Notes/Domino objects because the java objects are a wrapper around the C API objects.

Two ways to get around it.

  1. Only deal with the Notes objects when you have to “go to disk” – i.e. load document, save document, read from a view, etc.
    1. Do it all locally within your method, store the contents you read in class members, then discard all Notes objects immediately.
  2. Mark your Notes objects as “transient”, then implement a method to return the cached object.
    1. In that method, check to see if the object is valid, and if so, return it, otherwise, get it again.


General rules:

  • You should recycle anything you open when you no longer use it (but one-off documents or databases won’t hurt you too bad) 
 improves scalability.
  • Anything you open in a loop, recycle before the next loop – or you’re prone to crash w/ “Out of Memory” errors – DON’T Recycle a session, or anything you get from a resolveVariable() call


This presentation is another great step learning Java especially in the XPages environment. In the last section of the presentation, “The Fun Stuff”, Jeremy goes into detail describing an example of what you are capable of using Java in XPages. This section is a bit out of my head, right now.

I thank Jeremy for sharing the presentation! I noticed also a second presentation Jeremy held at the MWLUG “Introduction to managed beans“. I add this also to my ‘to-study’-list.



Improving XPages Application Performance with Managed Beans (Russell Maher, The View Advanced Xpages 2012)


This post is part of my road to more Java in my XPages projects. The next presentation was held at The View – Advanced XPages workshop early 2012 in Amsterdam. I am a bit reserved what I can and cannot share. The presentation is not on-line available so I will re-create all screenshots. Hopefully this abstract won’t trigger someone from their legal department to contact me =)

Main topics:

  • Java Design element.
  • Managed beans.

Java design element

This design element is new in 8.5.3 and makes using custom Java much easier in XPage application:

  • Your class will be added automatically to the project build path.
  • It allows you to work with Java without going via the Package Explorer view in DDE.

What allows this Java design element? You can create:

  • A class or package of classes.
  • Interface; a set of required methods in order to “implement”.
  • Enum; set of constants in Java.

Java class

  • A POJO (plain old java object) is created.
  • Representing a single Java class.
  • Multiple Java classes can be packaged together.

Some rules:

  • Package name and class name need to be defined during creation.
    • Package name typically are domain names in reverse:
      • e.g. com.wordpress.bildr
        • Will be displayed in DDE as com/wordpress/bildr/

The result in DDE (Code\Java view):

The result in the Java editor (DDE):

The result in the Package Explorer Eclipse view:

How do you call the Java class?

  • From any server-side JavaScript.
  • From other Java classes within your application.

Example calling one class from another

SecondClass that returns a string:


public class SecondClass {

public String printMe(){
return “hello world”;

FirstClass that creates a SecondClass object and calls the printMe() method in it:


public class FirstClass {

public String callMe(){
SecondClass sc = new SecondClass();
return sc.printMe();


Server-side JavaScript that creates and uses a FirstClass object:


You can create output to log.nsf via:

  • print(“”)
  • System.out.println(“”)

Java Interface

An interface defines a set of methods that can be “implemented” by Java classes. You can structure your code more via an interface.

Java Enum

An Enum defines one or more constants that you can use in your Java code.

  • declared public
  • defines constants
  • declares a private constructor
  • declares any public methods

Java Bean

A standalone piece of Java code constructed in a way that it can easily be consumed by other Java code.


  • A bean is a Java class with a no-argument constructor:
    • The constructor is the method with the same name as the class.
  • A bean usually has some properties for the class itself.
  • “getters/setters” are used to access bean properties.
  • Getters/setters are named to match their associated properties
    • These methods must match.
      • Example: If the property called myAge, the method must be called getMyAge.

Managed Beans

A managed bean is a Java bean but is not associated with a visual control.

A managed bean follows the standard bean requirements:

  • No-argument constructor.
  • Properties exposed via public getter / setters.

A managed bean needs to be on the build path of your application. The Java design element takes care of this. Alternatively go via the Package Explorer and create a source
folder in the project to add code there.

You need to register a managed bean to make it well-known within your application. You do this in the faces-config.xml file.

Configuration settings

  • Name:
    • Reference to the bean from other code.
  • Class:
    • Java class of the bean.
  • Scope:
    • Application, session, view, request or none.


When the bean has been registered you can use it from different places.

From other Java code

myCar mc=new carBean();

From server-side JavaScript


Via Expression Language



The ability to store the state of a Java object for later use. Serialization can improve the performance of Java. It is therefore recommended that beans implement Serialization.



The sessionMap object can be used to pass data between managed beans.  You can put data into and read out the sessionMap object.

Typical scenario:

  • Bean X puts data into the map.
  • Bean Y reads the data from the map.


Bean injection

When one managed bean is a managed property value of another managed bean, the managed bean instantiates itself.



You need to make sure the injected bean lasts at least as long as the dependent bean so they accommodate each other!


This post is just a small abstract of the presentation so if you want a clear understanding of the topic I recommend everyone to participate in an XPages bootcamp organized by The View. I am sure that in 2013 they will visit the old continent another time.

The presentation brings me a step further into using Java in XPages. Especially the managed beans section is well explained and some items are beyond the first steps I will take implementing Java.

You can start using Per Hendrik Lausten’s blog post as a starter to get more into managed beans. Jeremy Hodge also covers the topic here.

So far the presentations learned me about Java fundamentals, the Java design element, the Package Explorer view in DDE, working with Managed Beans and importing third party libraries.

The next step for me would be either to look more into the technology or to start looking at some real world code examples. I found one example I like: Log data changes using beans and the DataObject interface.

BP118 Using Java to build applications fit for the enterprise (Chris Connor, Lotusphere 2012)


Last evening I enjoyed another opportunity to learn Java in Xpages a bit more. This time I took the BP118 presentation because it jumps directly in the area I am working daily in: XPages! After reading the presentation one word was on my mind: Managed Beans.

Managed Beans

  • POJO – configured with getters / setters.
  • Implements “Serializable” (so XSP can read / write from disk).
  • Can be scoped:
    • Request.
    • View.
    • Session.
    • Application.
  • Very useful for:
    • Providing universal access to common lookups.
    • Providing “states” in an application.
      • e.g shopping cart / “one to many” tables.
    • Calling your Java libaries easily
      • BeanName.doMethod() or BeanName.someproperty
        • e.g.


Do Not Try to scope Domino Objects. They are C++ Objects and are subject to unpredictable garbage collection (eg NotesDocument, NotesDatabase).

Instead work with primitive types and objects such as String, ArrayLists, HashMaps etcetera. E.g. where a document is required use the UNID instead (String).

Use cases

The presentation describes a couple of use cases where managed beans are used.

Use Case 1 – Lookups

Most databases have lookup lists (e.g. keywords). You should decide on which ones are common:

  • Keep the common lookups in a library for sharing across all databases.
  • Application specific lookups can reside in a class/classes.

Goal from Expression Language / SSJS:

  • Simple call syntax e.g. “Lookup.Locations”.
  • Cached for the appropriate Life Cycle (good for performance):
    • Session.
    • Application.
    • Request.
    • View.

Steps to define a managed bean:

  • Find and Edit the Faces-Config.xml file.
  • Go to the Java Perspective in DDE.
  • Package Explorer.
  • Register your bean by adding XML Contents as shown in the image:


Personal comment: Too bad that the code for the class is not included. I am not sure if a sample database is available somewhere???

A simple call to the bean will get you a cached version of whichever property we want:

  • ArrayLists.
  • Strings.
  • Objects.
    • Not Domino Objects!

Example of a call:

  • More efficient than lots of @DbLookups everywhere.
  • Can deploy in an OSGI library for sharing over all your databases.

Use Case 2 – Line Items

Many applications have 1-to-many tables:

  • Order line items.
  • Expenses line items.

Managed Beans can help with this:

  • Provide a flexible structure for modelling.
  • Abstract out data.
  • No need to worry at early development:
    • Could be response documents.
    • Related documents by key.
    • Relational database table(s) data.

Use a classic MVC

Create a controller class for managing the POJO objects.

  • Adding.
  • Removing.
  • Updating.


  • POJO objects are real life objects (the line items).
  • Controlled by the controlling class.
  • The objects are then “written to disk” as appropriate by classes performing data operations.

Steps to define a managed bean (similar as in Use Case 1):

  • Find and Edit the Faces-Config.xml file.
  • Go to the Java Perspective.
  • Package Explorer.
  • Register your bean by adding XML Contents as shown.


Example: Code for the PersonController class:


What we are doing:

  • ArrayLists for containing Person Objects.
    • An arraylist is bound to a repeat control.
    • Methods for adding / removing Person Objects.
  • When the save button is pressed these objects are “written to disk”.

Use Case 3 – Export to Excel

Personal comment: I guess there are 3rd party libraries available that do better in creating Excel files…

Use Case 4 – Generic workflow process

Nice real world scenario however the code is cut into many pieces and therefore it is hard to follow / see the whole picture. Sample code would do magic here.


This presentation covers more the aspects I am working  daily with XPages. Too bad this presentation has only 51 slides and is therefore very compact. It would be great if a “Jumpstart” session will be made from this presentation at Connect13.

Getting hold on the example code would also be beneficial for learning Java and testing Managed Beans in real world XPages application.