Quantcast
Channel: None – Dec's Dom Blog
Viewing all articles
Browse latest Browse all 30

Getting To The Java Roots of XPages – Part 9

$
0
0

In the last part we looked at recycling domino handles in java but I mentioned that the class has some other serious issues. The biggest problem that I see now is that every time the getMyInternetAddress method is called it has to access the nab and get the document and return the value.

If I was using this method multiple times on the same page then that would mean multiple calls to the nab and multiple calls to the document and multiple calls to the getItemValueString etc. Unless your internet email address changes a lot then there really is no need to hit the nab multiple times.

Fixing this is very easy but needs a bit of a basic explanation first. When an object is created a space in memory is assigned for the entire object that exists for the life of the object. If I have a viewScope bean that is created from the scary.java.demo.AboutMe class then there is an AboutMe object created that exists for the life of the viewScope. If I set a value of a variable in that bean I can then read that value back later as long as I’m still in the same viewScope session.

So to make my class much more efficient I can store the internet address in a variable, when the getMyInternetAddress method is called the first time it will know that I haven’t set the variable yet and it will go and get the value and put it into the variable before sending it back to the xpage, if I call the method a second time it will know it has the variable and just send it back with no need to hit the nab and find the document which will be much faster.

So how do i do this? Well first I need a variable to store the value in. I’ll create this outside of the method.

private String myInternetAddress = null;

You will notice that I gave it a name that matches the method name but without the get bit at the front, in fact it matches the Expression Language version of the method name. This has been done on purpose and if you follow the series more you’ll find out why. I’ve set this as private which means that only this class can see it, it is a String and I’ve set it to null to begin with.

Now inside my getMyInternetAddress method I just have to add a very simple check, if the variable is null then I get the value from the nab, if it is not null then I know I’ve already gotten it and I can just pass back the value I already have.

Here is the new version of the method

public String getMyInternetAddress() {
		if (myInternetAddress == null) {

			Session session =null;
			Database thisDB =null;
			Database nabDB =null;
			View nabView =null;
			Document nabDoc =null;

			try {
				session = ExtLibUtil.getCurrentSession();
				thisDB = session.getCurrentDatabase();
				nabDB = session.getDatabase(thisDB.getServer(),"names.nsf", false);
				nabView = nabDB.getView("($Users)");
				nabDoc = nabView.getDocumentByKey(session.getEffectiveUserName());
				myInternetAddress = nabDoc.getItemValueString("InternetAddress");
			} catch (NotesException e) {
				System.out.print(e);
				myInternetAddress = "No NAB Doc";
			} finally {
				recycleDominoObjects(nabDoc,nabView,nabDB,thisDB,session);
			}
		}
		return myInternetAddress;
	}

It is still not perfect but it is getting there. In the next part I’m going to add two more methods to get a person’s first and last names.


Viewing all articles
Browse latest Browse all 30

Trending Articles