OpenLogging from an OSGi Servlet

Further to the last post on OpenLog which works nicely for Servlets that are contained within an NSF.  What if you want to use OpenLog in your OSGi Plugin Servlet?

If you follow the instructions in the previous post but your Servlet is an OSGi plugin you will hit a runtime error:

18/02/13 22:23: Exception Thrown java.lang.NoClassDefFoundError: com.ibm.xsp.resource.Resource at java.lang.J9VMInternals.verifyImpl(Native Method) at java.lang.J9VMInternals.verify(J9VMInternals.java:72) at java.lang.J9VMInternals.initialize(J9VMInternals.java:134) at com.jasonhookonline.logging.OpenLogItem.getSession(OpenLogItem.java:635) at com.jasonhookonline.logging.OpenLogItem.getLogDb(OpenLogItem.java:256) at com.jasonhookonline.logging.OpenLogItem.writeToLog(OpenLogItem.java:977) at com.jasonhookonline.logging.OpenLogItem.logMsg(OpenLogItem.java:725) ….

You’ll be tempted, no doubt to fix the missing class by finding the right jar and copying it to /jvm/lib/ext but that’s a slippery slope.

Instead you should follow the instructions in the earlier post before applying the following tweaks.

In your Servlet you need to create a session (I’m going to create, use and get rid of the session inside the doGet or doPost method for the purposes of this example) and pass it to the getOpenLogItem method.

In your Servlet Class

public class MyServlet extends ... {
Session _session = null; // a member variable
    try {
         NotesThread.sinitThread();
         _session = getSession();
    } catch (NotesException ne){

    } catch (Exception e){

    } finally {

         try{
            _session = null; 
         }
         NotesThread.stermThread();
   }
private Session getSession() {
 
     if(_session == null) {
        try {
            _session = NotesFactory.createSession("", "some account", "the internet password");
        } catch (NotesException e) {
          // TODO Auto-generated catch block
            e.printStackTrace();
         }
     } else {
       try {
           @SuppressWarnings("unused")
           boolean pointless = _session.isOnServer();
     } catch (NotesException ne) {
          //TODO TO DO A technical problem has occurred
        try{
          _session = NotesFactory.createSession("", "some account","the internet password");
        } catch(Exception e){
           resp.sendRedirect("/error.php?msg=oops!");
        }
     }
    }
 return _session;
 }

} //EO of the class

In your utility class

You need to modify the getOpenLogItem method so it accepts a session object:

package com.jasonhookonline.utility;
import com.jasonhookonline.logging.OpenLogItem;
 import lotus.domino.Session;
public class Utility {
 public static OpenLogItem _oli;
public static OpenLogItem getOpenLogItem(Session _session){
 if (_oli == null) {
 _oli = new OpenLogItem(_session);
 }
 return _oli;
 }
 }

In the OpenLogItem class

Replace the getSession method:

private Session getSession() {

 if(_session == null) {
 try {
 _session = NotesFactory.createSession("", "some account", "the internet password");
 } catch (NotesException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 } else {
 try {
 @SuppressWarnings("unused")
 boolean pointless = _session.isOnServer();
 } catch (NotesException ne) {
 //TODO TO DO A technical problem has occurred
 try{
 _session = NotesFactory.createSession("", "some account", "the internet password");
 } catch(Exception e){
 //TODO TO DO A technical problem has occurred
 }
 }

 }
 return _session;
}

Back in your Servlet:

Utility.getOpenLogItem(_session).logMsg(“Servlet Running doPost”);
Utility.getOpenLogItem(_session).logError(ne);

Advertisements
Tagged with: , , , ,
Posted in Coding, Java, XPages
One comment on “OpenLogging from an OSGi Servlet
  1. Thanks for this explanation. It solved my problem to get OpenLog working in my OSGi plugin

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: