package com.developi.jnx.utils; import com.hcl.domino.DominoClient; import com.hcl.domino.DominoClientBuilder; import com.hcl.domino.DominoProcess; import java.nio.file.Path; import java.nio.file.Paths; import java.util.function.Consumer; public class DominoRunner { /** * Runs the given action with a new DominoClient instance. * This method initializes the Domino process and thread context. * It will use the existing Notes client ID if available. If the ID is password-protected, keep Notes client running. * * @param action the action to perform with the DominoClient */ public static void runWithDominoClient(Consumer<DominoClient> action) { runWithDominoClient(action, true); } /** * Runs the given action with a new DominoClient instance. * This method initializes the Domino process and thread context. * It can optionally use the existing Notes client ID if available. , if false, environment variables * Notes_IDPath and Notes_IDPassword are used to switch to the ID. * * @param action the action to perform with the DominoClient * @param useExistingNotesClientId whether to use the existing Notes client ID */ public static void runWithDominoClient(Consumer<DominoClient> action, boolean useExistingNotesClientId) { // Set the jnx.skipNotesThread property to true to avoid creating a NotesThread. // Otherwise, we are going to spend precious time to find a non-error exception! System.setProperty("jnx.skipNotesThread", "true"); //System.setProperty("jnx.debuginit", "true"); // Although the documentation suggests a single string argument, we use an array. // The second parameter would be the notes.ini file path, but we don't need it, I guess. String[] initArgs = new String[]{System.getenv("Notes_ExecDirectory")}; final DominoProcess dp = DominoProcess.get(); dp.initializeProcess(initArgs); try (DominoProcess.DominoThreadContext ignored = dp.initializeThread()) { if (!useExistingNotesClientId) { // prevent ID password prompt final String idFilePath = System.getenv("Notes_IDPath"); final String idPassword = System.getenv("Notes_IDPassword"); if (idPassword!=null && ! idPassword.isEmpty()) { Path idPath = (idFilePath==null || idFilePath.isEmpty()) ? null : Paths.get(idFilePath); dp.switchToId(idPath, idPassword, true); } } try (DominoClient dc = DominoClientBuilder.newDominoClient().asIDUser().build()) { action.accept(dc); } } catch (Throwable t) { throw new RuntimeException("Domino Process Failed", t); } finally { dp.terminateProcess(); } } }