Java Harvest SDK (JHSDK) is not just a library of helpful Harvest-related classes, it has a very specific focus. JHSDK is for building a custom Harvest client. JHSDK serves as your proxy for interacting with Harvest. The very first thing your Harvest client has to do is log in to Harvest…duh. Your client can do absolutely nothing until it’s logged in.
The first class that you need to become familiar with is com.ca.harvest.jhsdk.JCaHarvest. This is your application’s key to the wonderful world of Harvest. The Javadoc says “This is the main Harvest object used to set up a session and generate a context. ” The session is your live, authenticated access to Harvest. And if JCaHarvest is the key, then JCaContext is the java representation of the world itself. More about JCaContext later. Let’s login to Harvest.
Import your JCaHarvest class
Create a reference to a JCaHarvest class
private JCaHarvest harvest;
Instantiate JCaHarvest with your broker name
harvest = new JCaHarvest("mybroker");
- Call login method with user name and password returning int to check status of the login
int rtn = harvest.login("myusername", "mypassword");
- If the return code is zero, then login succeeded. If it’s not zero, then the login failed and you get can getLastMessage() to get the error message.
So your login method my look something like this
Once you have an authenticated JCaHarvest object, you have the functionality of Harvest in your java application! Pretty powerful!
In an enterprise application, you want to consider how to manage your JCaHarvest reference(s). Logging in is a pretty expensive call. You probably don’t want to create a new instance or perform a new login with every call to the system. So far, my plan is to
- create a pool of shared Harvest sessions for read-only gestures
- prompt the user for their login when they need to perform some non-read-only operation (ie checkin or a checkout for update)
- cache their Harvest session in the Session scope
Check back with this category for future blogs documenting the success (or failure) of this technique for managing Harvest sessions in an enterprise application. My Harvest administrator and I have been discussing this technique and we believe it will work.
[EDIT: JHSDK will not work well in a Java EE environment because it is not thread safe. We wrap system commands to the CLI (command line interface). We’ve been very successful in numerous application doing this.]