The ins and outs of using Java with Domino

原创 2005年01月01日 12:29:00
The ins and outs of using Java with Domino
By Tony Patton

Java has grown from a buzzword, to a language for creating cute applets that run in your browser, to a full-fledged presence in the development community. It's definitely here to stay. The focus is now more on the back-end, leaving the browser/GUI (Graphical User Interface) development to DHTML (Dynamic Hypertext Markup Language), HTML, JavaScript, and CSS (Cascading Style Sheets).

Lotus has placed its full weight (which is rather large considering IBM's presence) behind the Java language. Full support was added in Domino 5.03 with minor enhancements in each incremental release. The latest release (5.05) includes Java libraries for working with XML (eXtensible Markup Language).

Why bother with Java?
The big question, you may ask yourself, is why bother with Java? I can offer four solid reasons.

  • Java is object-oriented. Everything is an object. In Domino, there are database objects, session objects, document objects, and so forth. The result is a componentized application with the end-goal of reusability for individual components.

  • Java offers broader support. There are no other platforms or applications that support the proprietary LotusScript language. On the other hand, the list of Java platforms is seemingly endless.

  • Java has standard network communication features. Java was developed as an Internet language, so network communication functionality is built into the core language with a common protocol of TCP/IP (Transmission Control Protocol/Internet Protocol). You can create network savvy applications in no time.

  • Java is portable. We've touched on this point, but it deserves to be repeated. Java code developed in Domino can survive outside the Domino environment. Try that with LotusScript.

Plus, Java has skill-set transportability. You can transfer your Java skill-set in addition to your Java code. It's always a good idea to make yourself more marketable with a new skill.

Domino Designer
One of the excellent additions to R5 is a separate development environment. This includes a rudimentary Java editor and compiler. The best part of it is syntax checking and error reporting.

You may want or need the more advanced features of third party IDEs (Integrated Development Environments), so you do have that option. That is, an IDE such as Visual Café, Jbuilder, or VisualAge for Java can be used to develop Domino Java code. Actually, VisualAge includes directions for working with Domino Java classes.

The Domino Designer allows Java code to be typed directly into it. It also allows Java classes to be imported from the file system. Basically, it has the same layout as the LotusScript editor. Class files in the current project/agent are organized in the left frame/window under the tab labeled Classes. Notice, our agent uses the standard name assigned to all new classes created: JavaAgent.

Domino provides the standard structure for a Java agent when Java is selected as the Run type for an agent. Figure A shows the structure provided.

FIGURE A
java-a-sm.gif
Here's the Domino Designer Java environment.

It provides the appropriate import statement (import lotus.domino.*) needed to work with the Domino classes. Also, the main entry point for a Domino Java agent is the NotesMain method, and it's provided. A try/catch block is provided along with two objects: Session and AgentContext. The Session object represents the environment for the current program, and AgentContext represents the current environment for the agent.

Additionally, the reference tab next to the classes tab is available for online help during development. Figure B shows the reference tab opened to the Domino classes.

FIGURE B
java-b-sm.gif
The Domino Designer reference tab is opened to the Domino classes.

Core Java and XML classes are available for viewing as well. The reference tab lists the classes by their package names with twisties available to expand to the classes and their associated methods and properties.

I've got a couple of other comments regarding the Designer interface. You may have noticed four buttons at the bottom of the window: Edit Project, New Class, Export, and Compile. The Edit Project button allows classes to be removed and added to the current code. The New Class button adds a new class to the agent. Compile compiles the code into bytecode, and Export allows it to be saved to a local drive. Above the buttons is the error box where errors encountered during compilation are listed with the errant line numbers to the right.

Java Console
All developers should thoroughly debug their code to ensure proper functionality. The Domino client offers the Java Console for working with Domino Java code. All system output via print statements can be viewed in it. All error messages will be displayed in it as well. It's available via the File->Tools->Show Java Debug Console drop-down menu. Figure C shows the drop-down selection

FIGURE C
java-c-sm.gif
The drop-down menu activates the Java Debug Console.

Figure D shows the actual window.

FIGURE D
java-d-sm.gif
Here's the Java Console window.

Class files
Java class files are most often distributed via JAR (Java ARchive) files. This is a compression format unique to Java, much like WinZip or Microsoft CAB files. Well, the Domino Java classes are distributed in a JAR file. They're located in the base directory of your Domino R5 installation.

Table A shows the files that are available.

TABLE A: Java files

File name Description
dservlet.jar Domino Java Servlet classes
i18n.jar Core SUN Java classes
jsdk.jar SUN Java Servlet classes
LotusXSL.jar XSLT Processor (Xalan) classes
Notes.jar Domino Java classes
rt.jar Core SUN Java classes
tools.jar Core SUN Java classes
XML4j.jar XML Parser classes (Xerces)

If you'll be working with Domino Java outside of the Domino environment, you must place the appropriate class files in your CLASSPATH environment variable. You'll need the Notes.jar file for using Domino Java classes.

Our first Domino Java agent
Okay, we've taken a look at the basics of utilizing Java in Domino. Let's turn our attention to a simple Domino Java agent. We will construct an agent that accesses all person records in the name and address book (names.nsf) and displays the first and last name of the person(s).

As an example, I've numbered a piece of code. Following the code, you'll find each line explained next to its corresponding number.

1. import lotus.domino.*;
2. import java.io.PrintWriter;
3. public class JavaExample extends AgentBase
{
4. public void NotesMain()
{
5. try
{
6. Session s = this.getSession();
7. PrintWriter pw = this.getAgentOutput();
8. Database db = s.getDatabase("","names.nsf");
9. if (db != null)
{
10. View vw = db.getView("People");
11. if (vw != null)
{
12. Document doc = vw.getFirstDocument();
13. String fname = null;
String lname = null;
14. int c = 0;
15. while (doc != null)
{
16. c += 1;
17. fname = doc.getItemValueString("FirstName");
lname = doc.getItemValueString("LastName");
18. pw.println("Person #" + c + " : " + fname + " " + lname);
19. doc = vw.getNextDocument(doc);
}
20. vw.recycle();
}
21. db.recycle();
}
22. s.recycle();
} catch(Exception e) {
e.printStackTrace();
}
}
}

Here's the explanation:

  1. Make the Domino Java classes available to the agent;
  2. The PrintWriter class of the Java.io package;
  3. Class declaration; everything in Java is a class/object;
  4. The entry point for a Domino Java agent is the NotesMain method;
  5. Beginning of the try/catch block;
  6. Create a Session object;
  7. PrintWriter object is used for output; we can view the output in the Java console;
  8. Create a Database object; we will use the name and address book;
  9. Continue only if the database was properly instantiated;
  10. Create a View object using the list of people in the NAB;
  11. Continue only if the View object has been properly instantiated;
  12. Retrieve the first document in the view;
  13. Declare String objects to be used for field values;
  14. Declare variable to be used as a counter;
  15. Loop through all documents in the view;
  16. Increment the counter variable;
  17. Retrieve the contents of the FirstName field as a String;
  18. Display the name and counter variable;
  19. Get the next document from the view;
  20. Return the memory used by the View object to the system;
  21. Return the memory used by the Database object to the system;
  22. Return the memory used by the Session object to the system.

Figure E shows the output of the example as seen in the Java Console.

FIGURE E
java-e-sm.gif
Here's the output from the example.

Conclusion
Java is a powerful language that can be used to extend the already powerful Domino environment. In this article, we've looked at the very basic application of Java in Domino: an agent via Domino Designer. Domino Java can be utilized in applets, standalone applications, and servlets as well. The capabilities are only limited by your imagination. For more information on Domino and Java, please refer to my latest book, Domino Development with Java, available through the publisher, Manning Publications, at http://www.manning.com/patton2.

The Ins and Outs of Gradle

http://code.tutsplus.com/tutorials/the-ins-and-outs-of-gradle--cms-22978 As the first IDE (Integra...
  • orgcheng
  • orgcheng
  • 2015年03月19日 22:22
  • 389

The Ins and Outs of Token Based Authentication

Introduction Token based authentication is prominent everywhere on the web nowadays. With most ever...
  • canlets
  • canlets
  • 2016年03月14日 17:34
  • 438

Discover the ins and outs of J2EE application assembly and deployment

We all built EJBs, servlets, and JSPs before J2EE came upon the scene at 1999's JavaOne. In those da...
  • sbtmbj2010
  • sbtmbj2010
  • 2014年05月16日 08:54
  • 466

投奔怒海——一个Domino老程序员眼里的Java开发

这是一个以键盘鼠标为谋生工具已十多年的人初次进行专门的Java开发的体验和感受,对于Java程序员,这些也许早就习以为常,那就把这当成从一个来自不同世界的新人眼里看看他们自己的工作;对于我的Domin...
  • starrow
  • starrow
  • 2014年07月25日 18:25
  • 8202

Domino使用Java代理的注意事项

 Domino中使用JAVA代理一定要小心,特别是R5的版本。1、及时地对所用到Notes Object(session,agentContext,db,view,document. 等等)进行Rec...
  • wangdeq
  • wangdeq
  • 2007年10月10日 13:34
  • 1058

domino java多线程运用

多线程已经在很多应用中使用了,但在domino中很少有相关资料,能否实现多线程应用呢?百度查查了一下java的资料是可以的,直接转移过来使用就试试,结果能正常运行。已经把一些应用转到多线程处理了,速度...
  • weijia3624
  • weijia3624
  • 2015年10月27日 19:49
  • 1157

Java 利用http协议与Domino实现sso单点登录

// 单点登录测试  @RequestMapping(value = "sso")  public String sso(HttpServletRequest request, HttpServlet...
  • pgbiao
  • pgbiao
  • 2015年03月23日 15:35
  • 3521

Lotus Domino Java代理邮件最终

这是之前Lotus邮箱做Java代理最后一帖了,因为这方面确实研究的不是很透彻。 在研究Lotus邮件结构时发现其内部有很多东西,附件,图片,表格等等。因为这些就意味着程序员们需要解析这些东西。邮件...
  • github_34437042
  • github_34437042
  • 2016年11月11日 09:16
  • 278

Java通过DIIOP访问domino的对象实例

1. 背景 看到网上别人写的Java通过DIIOP访问domino对象的实例,自己也试着写了一下,其实挺简单。 2. 开发工具 a) IBM/Lo...
  • Gavid0124
  • Gavid0124
  • 2015年02月06日 14:15
  • 1430

domino的内存溢出问题

domino的内存溢出问题通常是java代理导致的。比如在循环代码中的domino对象没有释放,在代理运行完之后系统也不会自动释放,这样如果循环次数较多,就可能内存耗尽。这种情况下,可以将循环的dom...
  • titanzhi
  • titanzhi
  • 2009年12月24日 13:56
  • 1003
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:The ins and outs of using Java with Domino
举报原因:
原因补充:

(最多只允许输入30个字)