import lotus.domino.*;
public class NotesJavaShell extends AgentBase{
// Public fields.
// Local vars
private Session session;
private Database database;
// Constructors
public NotesJavaShell(){ // default constructor, without run context
}
public NotesJavaShell(Session s, Database d) { // when we know the run context
this.session = s;
this.database = d;
}
// Start here when run from Eclipse.
public static void main(String[] args) {
//System.out.println(System.getProperty("java.library.path"));
Session sess=null;
Database db=null;
NotesJavaShell ag;
// Initialize Notes
NotesThread.sinitThread();
try {
sess = NotesFactory.createSession(); // start a Notes session
db = sess.getDatabase("", Constants.DEFAULT_HOME_DB); // simulate home database for agent
ag = new NotesJavaShell(sess, db); // simulate agent object with session and database
ag.NotesMain(); // call main routine of agent
}
catch(NotesException ne) {
System.out.println(ne.id + " " + ne.text);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if (db != null) db.recycle();
if (sess != null) sess.recycle();
}
catch (Exception x) {}
NotesThread.stermThread();
}
} // end main
// This routine is called however the code is run: from Eclipse or agent launch within Notes/Domino.
public void NotesMain()
{
Log notesLog=null;
Session sess=null;
Database db=null;
AgentContext ac=null;
Utilities util;
int logLevel;
try {
if (this.session != null) // Already have an agent context.
{
sess = this.session;
db = this.database;
}
else { // Need to get agent context.
sess = this.getSession();
if (sess==null) throw new NotesException(NotesError.NOTES_ERR_ERROR, "Could not get current session at start of agent.");
ac = sess.getAgentContext();
if (ac==null) throw new NotesException(NotesError.NOTES_ERR_ERROR, "Could not get agent context at start of agent.");
db = ac.getCurrentDatabase();
if (db==null) throw new NotesException(NotesError.NOTES_ERR_ERROR, "Could not get current database at start of agent.");
}
}
catch(NotesException ne) {
System.out.println(ne.id + " " + ne.text);
return;
}
catch (Exception e) {
e.printStackTrace();
return;
}
finally {};
// The truly common code, however we are invoked.
try {
// Get utilities.
util = new Utilities();
// Record the session so other objects can use it.
util.setSession(sess);
// Find all the runtime parameters.
util.readParameters();
// Start a Notes log.
notesLog = util.getNotesLog();
logLevel = util.getLogLevel();
if (Constants.CONSOLE_OUTPUT) System.out.println("Agent started. " + Constants.BUILD_STAMP);
if (logLevel >= Constants.LOG_LEVEL_BRIEF) notesLog.logAction ("Agent started. " + Constants.BUILD_STAMP);
// ******* Do the real work here ********
System.out.println (db.getTitle()); // print title of home database, to prove we got the agent context correctly
// ******* Do the real work here ********
// All done with no errors.
if (logLevel >= Constants.LOG_LEVEL_BRIEF) notesLog.logAction ("Agent done with no errors.");
if (Constants.CONSOLE_OUTPUT) System.out.println("Agent done with no errors.");
} // try
// Catch Notes exceptions from main code.
catch(NotesException ne) {
if (Constants.CONSOLE_OUTPUT) System.out.println("Notes error code " + ne.id + ". " + ne.text);
try {
notesLog.logError (ne.id, ne.text); // Notes log may not be valid, but give it a try
}
catch (Exception x) {}
}
// Catch non-Notes exceptions from main code.
catch (Exception x) {
if (Constants.CONSOLE_OUTPUT) System.out.println("Error: " + x.getMessage());
if (Constants.CONSOLE_OUTPUT) x.printStackTrace();
try {
notesLog.logError (NotesError.NOTES_ERR_ERROR, x.getMessage()); // Notes log may not be valid, but give it a try
}
catch (Exception x2) {}
}
// Do cleanup from main code block.
finally {
try {
if (notesLog!=null) notesLog.close();
if (notesLog!=null) notesLog.recycle();
// cannot recycle the AgentContext, Session or Database, since they may have been passed in by Notes.
}
catch (Exception x){}
} // finally
} // NotesMain
} // main class
import lotus.domino.*;
public class Utilities {
// Class fields, shared by all objects.
private static Session session;
private static Log notesLog = null;
private static String logDbPath = "";
private static String logName = "";
private static int logLevel;
// Constructor
Utilities () {
}
// Hold Notes session info so all objects can share it.
public void setSession (Session s) {
session = s;
}
public Session getSession () {
return(session);
}
// Return the Notes log, or start a new one.
public Log getNotesLog () throws NotesException {
if (notesLog != null)
return(notesLog);
else {
notesLog = session.createLog(logName);
if (notesLog==null) throw new NotesException(NotesError.NOTES_ERR_ERROR, "Could not create NotesLog object in NotesSession.");
notesLog.openNotesLog(Constants.LOG_SERVER, logDbPath);
return(notesLog);
}
}
// Get all program parameters, storing them in class variables here.
public void readParameters() throws NotesException{
Database configDb;
View configView = null;
Document configDoc = null;
String configDbPath = "";
String logLevelString = "";
// Get the Notes.ini variable that holds the filename of the configuration database.
configDbPath = session.getEnvironmentString(Constants.CONFIG_DB_INI, false); // not system var, add $
// If the INI is not found, use the default path for the config db.
if (configDbPath.compareTo("")==0) configDbPath = Constants.CONFIG_DB_DEFAULT;
// Open the config db.
configDb = session.getDatabase(Constants.CONFIG_SERVER, configDbPath);
if (!(configDb.isOpen())) throw new NotesException(NotesError.NOTES_ERR_ERROR, "Could not open configuration database " + configDbPath + ".");
// Open the view of configuration documents.
configView = configDb.getView(Constants.CONFIG_VIEW);
if (configView == null) throw new NotesException(NotesError.NOTES_ERR_ERROR, "Could not open view " + Constants.CONFIG_VIEW + " in the configuration database " + configDbPath + ".");
// Get the configurations that relates to logging.
configDoc = configView.getDocumentByKey(Constants.CONFIG_LOOKUP_LOGDB);
if (configDoc == null) throw new NotesException(NotesError.NOTES_ERR_ERROR, "Could not find the setting " + Constants.CONFIG_LOOKUP_LOGDB + " in the configuration database " + configDbPath + ".");
logDbPath = configDoc.getItemValueString(Constants.CONFIG_VALUE_FIELD);
if (logDbPath.compareTo("")==0) throw new NotesException(NotesError.NOTES_ERR_ERROR, "Could not find the setting " + Constants.CONFIG_LOOKUP_LOGDB + " in the configuration database " + configDbPath + ".");
if (configDoc!=null) configDoc.recycle();
configDoc = configView.getDocumentByKey(Constants.CONFIG_LOOKUP_LOGNAME);
if (configDoc == null) throw new NotesException(NotesError.NOTES_ERR_ERROR, "Could not find the setting " + Constants.CONFIG_LOOKUP_LOGNAME + " in the configuration database " + configDbPath + ".");
logName = configDoc.getItemValueString(Constants.CONFIG_VALUE_FIELD);
if (logName.compareTo("")==0) throw new NotesException(NotesError.NOTES_ERR_ERROR, "Could not find the setting " + Constants.CONFIG_LOOKUP_LOGNAME + " in the configuration database " + configDbPath + ".");
if (configDoc!=null) configDoc.recycle();
configDoc = configView.getDocumentByKey(Constants.CONFIG_LOOKUP_LOGLEVEL);
if (configDoc == null) throw new NotesException(NotesError.NOTES_ERR_ERROR, "Could not find the setting " + Constants.CONFIG_LOOKUP_LOGLEVEL + " in the configuration database " + configDbPath + ".");
logLevelString = configDoc.getItemValueString(Constants.CONFIG_VALUE_FIELD);
if (logLevelString.compareTo("")==0) throw new NotesException(NotesError.NOTES_ERR_ERROR, "Could not find the setting " + Constants.CONFIG_LOOKUP_LOGLEVEL + " in the configuration database " + configDbPath + ".");
if (configDoc!=null) configDoc.recycle();
// Change the logging level to a number.
if (logLevelString.compareToIgnoreCase(Constants.LOG_LEVEL_ERROR_STRING)==0)
logLevel = Constants.LOG_LEVEL_ERROR;
else if (logLevelString.compareToIgnoreCase(Constants.LOG_LEVEL_BRIEF_STRING)==0)
logLevel = Constants.LOG_LEVEL_BRIEF;
else if (logLevelString.compareToIgnoreCase(Constants.LOG_LEVEL_FULL_STRING)==0)
logLevel = Constants.LOG_LEVEL_FULL;
else
logLevel = Constants.LOG_LEVEL_ERROR;
// ***** Get other setup parameters here. *********
// Clean up.
if (configDoc!=null) configDoc.recycle();
if (configView!=null) configView.recycle();
if (configDb!=null) configDb.recycle();
} // readParameters()
// Simple getItems() for configuration. Some settings don't need a get(), if it is only used
// within this class to define a larger parameter.
public int getLogLevel () {
return(logLevel);
}
// ***** Add getItems() for other setup parameters here. *********
}
public class Constants {
// Version stamp.
final static String BUILD_STAMP = "Build 5, dated 04 June 2010.";
/*
* For test setup.
*/
final static boolean CONSOLE_OUTPUT = true;
final static String DEFAULT_HOME_DB = "names.nsf"; // when running from Eclipse, the "home" db of the agent
/*
* Parameters that are NOT user configurable.
*/
// Assume all databases are on current machine.
final static String CONFIG_SERVER = "";
final static String LOG_SERVER = "";
final static String SOURCE_SERVER = "";
// Logging control
final static String LOG_LEVEL_NONE_STRING = "0"; // not implemented, we always do at least ERROR logging
final static String LOG_LEVEL_ERROR_STRING = "1";
final static String LOG_LEVEL_BRIEF_STRING = "2";
final static String LOG_LEVEL_FULL_STRING = "3";
final static int LOG_LEVEL_NONE = 0;
final static int LOG_LEVEL_ERROR = 1;
final static int LOG_LEVEL_BRIEF = 2;
final static int LOG_LEVEL_FULL = 3;
// ********** Add other non-user-configurable parameters here. **********
/*
* To get parameters that ARE user configurable.
*/
final static String CONFIG_DB_INI = "NotesJavaSetup";
final static String CONFIG_DB_DEFAULT = "notes_java_setup.nsf";
final static String CONFIG_VIEW = "SettingsByName";
final static String CONFIG_VALUE_FIELD = "Values";
final static String CONFIG_LOOKUP_LOGDB = "Log Database";
final static String CONFIG_LOOKUP_LOGNAME = "Log Name";
final static String CONFIG_LOOKUP_LOGLEVEL = "Log Level";
// ********** Add the names of other user-configurable parameters here. These are the parameter names
// ********** as they appear in the configuration database. *********
// Constructor
Constants () {}
}