Tuesday, August 14, 2012

SmartGWT Calendar widget scroll to Current Time in month view

The calendar widget of smartgwt is quite powerful. But it lacks one important functionality, that is, scroll to the current time in case of month view.
But this feature can easily be enabled by working on the Workday feature which smartGWT calendar widget supports.

I wrote a simple function to achieve the same:

private void scrollToCurrentTime() {
int[] workDays = new int[1];
Date now = new Date();
workDays[0] = now.getDay();
int currentHour = now.getHours();
now.setHours(currentHour-1);
String start = DateTimeFormat.getFormat(
PredefinedFormat.TIME_SHORT).format(now);
now.setHours(currentHour+7);
String end = DateTimeFormat.getFormat(
PredefinedFormat.TIME_SHORT).format(now);
calendar.setWorkdays(workDays);
calendar.setWorkdayStart(start);
calendar.setWorkdayEnd(end);
calendar.setScrollToWorkday(true);
calendar.setShowWorkday(true);
}

Correct way to use SNMP4J in concurrent environment.

SNMP4J is one of the most popular Java library for SNMP (Simple Network Management Protocol) operartions. Its quite powerful and easy to use. But it seriously lacks a good documentation.  In my project at work, which is a concurrent environment, I faced a funny problem.

I was using a simple snmp trap listener code as shared in the java docs and in numerous amount of blogs. But, the listener use to stop working after a while. Little more debugging and I figured out that it use to stop working only after somewhere else in the code someone has used the same library for SNMP get operation.

Then, i went ahead googling to see if anyone else has ever faced a similar problem. But in vain! Checked the bug list on SNMP4J to see if any such similar issue exists. No luck again! Then I decided to understant the scenario properly and started reading the SNMP4J java source code and Bazinga!!

The very basic source code share with the documentation gives us a wrong implementation of the trap listener. The SecurityModels class is a static class. And when we add user to the ids given, it overrides the previous user. So the right way to add USM user is:

USM usm = (USM) SecurityModels.getInstance().getSecurityModel(
new Integer32(USM.SECURITY_MODEL_USM));
if (usm == null) {
usm = new USM(SecurityProtocols.getInstance(), new OctetString(
MPv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
}

UsmUser myuser = new UsmUser(new OctetString(USERNAME), AuthMD5.ID,
new OctetString(PASSWORD), null, null);
usm.addUser(new OctetString(USERNAME), myuser);


With this we way don't override the USMUser and our code works fine. Woila! This discovery took a huge amount of my time. Hope it helps someone.

Building Successful Products in the Maze of a Large Organization

  *Image is generated using AI Large organizations offer a treasure trove of resources and stability for product development. However, navig...