Quick-n-Dirty: A simple isRecycled() method

Update: Please read the second article – there you will find a better version.

You will find that the domino java API does not have a suitable method to test if a domino object was already recycled or not. And because of the internal caching of some method calls it is not reliable to check for example for a specific property with a try/catch block.

But domino objects have a private property isdeleted which is transient and gives us the required information. With Java Reflection it is possible to access this property:

package ch.hasselba.domino;

import java.lang.reflect.Field;

import lotus.domino.Base;
import lotus.domino.local.NotesBase;

/**
 * DominoUtil a library containing usefull Tools / Methods for domino
 * 
 * @author Sven Hasselbach
 * @category Domino
 * @category Tools
 * @version 1.2
 */
public class DominoUtil {

    private static Field isDeleted;    

    /**
     * checks if a domino object is already recycled
     * 
     * @param lotus.domino.local.NotesBase
     *           obj to check
     * @author Sven Hasselbach
     * @category Domino
     * @category Tools
     * @version 1.1
     */
    public static boolean isRecycled(NotesBase obj) {

        if( isDeleted == null )
            initIsDeleted();

        try {
            return isDeleted.getBoolean(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * checks if a domino object is already recycled
     * 
     * @param lotus.domino.Base
     *           obj to check
     * @author Sven Hasselbach
     * @category Domino
     * @category Tools
     * @version 1.1
     */
    public static boolean isRecycled(Base obj) {
        return isRecycled( (NotesBase) obj );
    }

    /**
     * inits Reflection of isDeleted field
     */
    private static void initIsDeleted(){
        try {
            isDeleted = lotus.domino.local.NotesBase.class
                    .getDeclaredField("isdeleted");
            isDeleted.setAccessible(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

Here is a short demonstration of the usage in an agent (it works in XPages / SSJS too):

import lotus.domino.*;
import ch.hasselba.domino.DominoUtil;

public class JavaAgent extends AgentBase {

    public void NotesMain() {

      try {
          Session session = getSession();
          AgentContext agentContext = session.getAgentContext();
          Database db = session.getCurrentDatabase();

          View v1 =  db.getView( "AllDocuments" );
          View v2 =  db.getView( "AllDocuments" );

          System.out.println( "Recycled: " + 
             DominoUtil.isRecycled( v1 ) );

          v2.recycle();

          System.out.println( "Recycled: " + 
            DominoUtil.isRecycled( v1 ) );

      } catch(Exception e) {
          e.printStackTrace();
       }
   }
}
Dieser Beitrag wurde unter Java, ServerSide JavaScript abgelegt und mit , , , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

7 Kommentare zu Quick-n-Dirty: A simple isRecycled() method

  1. Nice trick, but it is really dangerous to use suchs tricks in real world applications, because private implementation details of java classes can be changed in the future without documentation. So if IBM change the internal details of the java classes your application breaks and you can not blame support for this change. You have been warned.

    • Thanks for your warning, but this is normal behaviour if you are upgrading a system to a newer version. If the API changes, it changes, and your applications will break. Public or private, hack/trick or „standard“ functions.

      Don’t upgrade without tests…

      • No this is not the same. I am a java programmer since Version 1.0 and i have not found any changes in the existing public API’s, but there were many implementation changes over the years wich will break applications that depends on internal details.

        • For me and my customers it is not relevant if an application doesn’t work anymore because the IBM has changes this or that, or the underlying OS has changed or whatever. Apllication runs: OK. Application doesn’t run: Not OK.

          Problems with IE 9 and Dojo? After upgrading to 8.5.3 partial refreshs are not working anymore? I know for at least three things in Lotus Notes that have changed and have broken existing applications, and this was only for versions 8.5. Here is a link with other examples, just to give you an idea that this happens in the domino world:
          http://stackoverflow.com/questions/9340733/what-are-the-pitfalls-in-when-moving-a-8-5-2-xpages-application-to-8-5-3

          In the Extensibility API f.e. there are some public functions which should be made private in future (some comments in the documentation are saying this).

          If I understand you correctly you will give me a guarantee that the Domino Java API will never change. This is good to know 🙂

          P.S.
          Didn’t know that Java was available in Notes 1.0 😉

        • CEO: (Fevered) Our application is not running anymore!
          Sven: (Pacifying) Yes, I know. There is a bug. I have already created an SPR.
          CEO: (Baffled) A what?
          Sven: (Descriptive) I have informed the IBM that something is not working as it should. They will fix this in a future release.
          CEO: (Perplexed) And the future is… Yesterday or Today or tomorrow?
          Sven: (…) …
          CEO: (Angry) Every day the application is not running we are loosing money.
          Sven: (Sheepish) …
          CEO: (Very Angry) A LOT OF MONEY!
          Sven: (Awed) I have a workaround…
          CEO: (Smiling) Do it! Fix it! NOW!
          Sven: (Fending) No, I can’t! It would be possible that there is a incompatibility with a future release of Domino.
          CEO: (NOT Smiling): What the f..k!?

  2. Pingback: Quick-n-Dirty: A simple isRecycled() method (2) | blog@hasselba.ch

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.


*