Sunday, August 12, 2007

Close resource inside and only inside finally block

Always close any resource (network connection, database connection, file stream) inside and only inside finally block. Don't close it inside try block. What is the problem of closing it inside try block? consider the following example:

try {
String fileName = ...;
InputStream input = new FileInputStream(fileName);
// read theContentOfTheFile
input.close();
retrun theContentOfTheFile;
} catch (IOException e) {
// log the exception
return null;
}

If an exception is thrown while reading theContentOfTheFile, the FileInputStream will never be closed.

A better solution but not totally perfect:
InputStream input = null;
try {
String fileName = ...;
input = new FileInputStream(fileName);
// read theContentOfTheFile
input.close();
retrun theContentOfTheFile;
} catch (IOException e) {
// log the exception
return null;
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
// do nothing
}
}
}

If no exception is thrown inside the try block, then before returning from the method the finally block will also be executed. That means we are trying to close the same stream twice. So, an exception will always be thrown form the input.close() inside the finally block.

The best solution:
InputStream input = null;
try {
String fileName = ...;
input = new FileInputStream(fileName);
// read theContentOfTheFile
retrun theContentOfTheFile;
} catch (IOException e) {
// log the exception
return null;
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
// do nothing
}
}
}

In this example the input.close() is granted to be executed once and only once.

Tuesday, July 24, 2007

Use System.currentTimeMillis() instead of Date or Calendar

Always try to use System.currentTimeMillis() instead of java.util.Date or java.util.Calendar.

Why?

Because of performance. Date and Calendar internally calls System.currentTimeMillis(). So, why not use it directly?

A BAD example:

long currentTime = Calendar.getInstance().getTimeInMillis();
So, what does Calendar.getInstance() actually do? Following lines are just copied from the source code of jdk1.5:
public static Calendar getInstance()
{
Calendar cal = createCalendar(TimeZone.getDefaultRef(),
Locale.getDefault());
cal.sharedZone = true;
return cal;
}

private static Calendar createCalendar(TimeZone zone,
Locale aLocale)
{
if ("th".equals(aLocale.getLanguage()) &&
("TH".equals(aLocale.getCountry()))) {
return new sun.util.BuddhistCalendar(zone, aLocale);
}
return new GregorianCalendar(zone, aLocale);
}

public GregorianCalendar(TimeZone zone, Locale aLocale) {
super(zone, aLocale);
gdate = (BaseCalendar.Date) gcal.newCalendarDate(zone);
setTimeInMillis(System.currentTimeMillis());
}
Have you got it? Calendar.getInstance() checks TimeZone, Locale and of course System.currentTimeMillis(). So, it's an expensive operation.

Another bad example:
Date date = new Date(System.currentTimeMillis());
Why is it bad? Because, it should be as simple as:
Date date = new Date();
And, here is the apidoc of default constructor of Date:
Allocates a Date object and initializes it so that
it represents the time at which it was allocated,
measured to the nearest millisecond.
Yet, another bad example:
Date date = Calendar.getInstance().getTime();
Now, when should we use System.currentTimeMillis():
When we need only the millisecond representation of the current time.

When should we use Date: When we need a date object representing the current time.

When should we use Calendar.getInstance(): When we need TimeZone or Locale specific information. Another use of Calender can be for creating constant Date object:
public static final Date INDEPENDENCE_DAY;

static {
Calendar calendar = Calendar.getInstance();
calendar.set(1971, Calendar.MARCH, 26);
INDEPENDENCE_DAY = calendar.getTime();
}

Saturday, July 14, 2007

Conncting to Grameen Phone EDGE/GPRS in ubuntu linux using Nokia 6300 USB modem

This is the first time I am using ubuntu linux. I am writing this post from the newly installed ubuntu in my home PC. So, this is how I have connected to Grameen Phone EDGE using my Nokia 6300 USB modem.

Check the vendor and product id of your phone, To do this simply connect your phone in Nokia mode through the USB cable.

$ lsusb
Bus 004 Device 001: ID 0000:0000
Bus 005 Device 003: ID 0421:04f9 Nokia Mobile Phones
Bus 005 Device 001: ID 0000:0000
Bus 002 Device 001: ID 0000:0000
Notice the ID 0421:04f9 Nokia Mobile Phones. Here 0421 is the vendor id and 04f9 is the product id.
$ sudo modprobe usbserial vendor=0x0421 product=0x04f9
$ wvdialconf create
Editing `create'.

Scanning your serial ports for a modem.

WvModem<*1>: Cannot set information for serial port.
ttyS0<*1>: ATQ0 V1 E1 -- failed with 2400 baud, next try: 9600 baud
ttyS0<*1>: ATQ0 V1 E1 -- failed with 9600 baud, next try: 115200 baud
ttyS0<*1>: ATQ0 V1 E1 -- and failed too at 115200, giving up.
Modem Port Scan<*1>: S1 S2 S3
WvModem<*1>: Cannot get information for serial port.
ttyACM0<*1>: ATQ0 V1 E1 -- OK
ttyACM0<*1>: ATQ0 V1 E1 Z -- OK
ttyACM0<*1>: ATQ0 V1 E1 S0=0 -- OK
ttyACM0<*1>: ATQ0 V1 E1 S0=0 &C1 -- OK
ttyACM0<*1>: ATQ0 V1 E1 S0=0 &C1 &D2 -- OK
ttyACM0<*1>: ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0 -- OK
ttyACM0<*1>: Modem Identifier: ATI -- Nokia
ttyACM0<*1>: Speed 4800: AT -- OK
ttyACM0<*1>: Speed 9600: AT -- OK
ttyACM0<*1>: Speed 19200: AT -- OK
ttyACM0<*1>: Speed 38400: AT -- OK
ttyACM0<*1>: Speed 57600: AT -- OK
ttyACM0<*1>: Speed 115200: AT -- OK
ttyACM0<*1>: Speed 230400: AT -- OK
ttyACM0<*1>: Speed 460800: AT -- OK
ttyACM0<*1>: Max speed is 460800; that should be safe.
ttyACM0<*1>: ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0 -- OK

Found an USB modem on /dev/ttyACM0.
create: Can't open 'create' for reading: No such file or directory
create: ...starting with blank configuration.
Modem configuration written to create.
ttyACM0: Speed 460800; init "ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0"
This shows that a modem is connected at /dev/ttyACM0
$ sudo cat > /etc/wvdial.conf
[Dialer Defaults]
Modem = /dev/ttyACM0
Baud = 460800
Init1 = AT&F
Init2 = AT E0 V1 &D2 &C1 &S0 S0=0 +dr=1
Init3 = AT+IFC=2,2;+CVHU=1
Init4 = ATS7=60+DS=3,0;&K3
Init5 = AT+CGDCONT=,,"gpinternet"
Init6 = ATS0=0
ISDN = 0
Modem Type = Nokia GSM Phone USB Modem
Phone = *99#
Username = any
Password = any
Stupid Mode = 1
Now just dial your connection.
$ sudo wvdial
--> WvDial: Internet dialer version 1.56
--> Cannot get information for serial port.
--> Initializing modem.
--> Sending: AT&F
OK
--> Sending: AT E0 V1 &D2 &C1 &S0 S0=0 +dr=1
AT E0 V1 &D2 &C1 &S0 S0=0 +dr=1
OK
--> Sending: AT+IFC=2,2;+CVHU=1
OK
--> Sending: ATS7=60+DS=3,0;&K3
OK
--> Sending: AT+CGDCONT=,,"gpinternet"
OK
--> Sending: ATS0=0
OK
--> Modem initialized.
--> Sending: ATDT*99#
--> Waiting for carrier.
CONNECT
~[7f]}#@!}!} } }2}#}$@#}!}$}%\}"}&} }*} } g}%~
--> Carrier detected. Starting PPP immediately.
--> Starting pppd at Sat Jul 14 10:37:55 2007
--> Pid of pppd: 8383
--> Using interface ppp0
--> pppd: H[18]
--> [06][08][10][12][06][08]
--> pppd: H[18]
--> [06][08][10][12][06][08]
--> pppd: H[18]
--> [06][08][10][12][06][08]
--> pppd: H[18]
--> [06][08][10][12][06][08]
--> local IP address 10.130.9.55
--> pppd: H[18]
--> [06][08][10][12][06][08]
--> remote IP address 10.6.6.6
--> pppd: H[18]
--> [06][08][10][12][06][08]
--> primary DNS address 202.56.4.120
--> pppd: H[18]
--> [06][08][10][12][06][08]
--> secondary DNS address 202.56.4.121
--> pppd: H[18]
--> [06][08][10][12][06][08]
To disconnect press Ctrl+C.

Enjoy

References:
http://warofwords.wordpress.com/2007/05/27/using-gprs-with-ubuntu-without-bluetooth-ankur-shrivastava-linux-usb-nokia-modem-internet/
http://gentoo-wiki.com/Nokia_N60_series
http://www.marzocca.net/linux/ubuntux31.html#gprs

Friday, June 8, 2007

adding all jar files of a directory to classpath

how do you add all the jar files of your application's lib directory to classpath? probably as follows:

set CLASSPATH=%CLASSPATH%;%LIB_DIR%/activation.jar
set CLASSPATH=%CLASSPATH%;%LIB_DIR%/FTPProtocol.jar
set CLASSPATH=%CLASSPATH%;%LIB_DIR%/jai.jar
set CLASSPATH=%CLASSPATH%;%LIB_DIR%/jakarta-oro.jar
set CLASSPATH=%CLASSPATH%;%LIB_DIR%/jaas.jar
set CLASSPATH=%CLASSPATH%;%LIB_DIR%/log4j.jar
set CLASSPATH=%CLASSPATH%;%LIB_DIR%/mail.jar
set CLASSPATH=%CLASSPATH%;%LIB_DIR%/rmicb.jar
set CLASSPATH=%CLASSPATH%;%LIB_DIR%/Tidy.jar
set CLASSPATH=%CLASSPATH%;%LIB_DIR%/trove.jar
set CLASSPATH=%CLASSPATH%;%LIB_DIR%/xalan.jar
set CLASSPATH=%CLASSPATH%;%LIB_DIR%/xerces.jar
set CLASSPATH=%CLASSPATH%;%LIB_DIR%/xml-apis.jar
but there is a easier way:
SETLOCAL ENABLEDELAYEDEXPANSION
for %%f in (%LIB_DIR%\*.jar) do set CLASSPATH=!CLASSPATH!;%%f
ENDLOCAL
note the ! sign around CLASSPATH and don't miss SETLOCAL/ENDLOCAL commands.

Saturday, May 19, 2007

jgoodies validation (very easy!!!)

for last couple of days i was looking into jgoodies validation. but i found that it doesn't have enough quick start guide or simple sample code. the sample code provided with jgoodies are too complex. i have searched the net but i failed.
then i asked hasan's help. he said, "give me the complex code, i will simplify it". then i think, why don't i do that. so here is my simple code.

first assume you have a domain object as follow:

public interface User {
public String getUserName();
public void setUserName(final String pUserName);
public String getPassword();
public void setPassword(final String pPassword);
}
and it's implementation UserImpl.

you also have a form with two text fields mUserNameTextField and mPasswordField

now first create a Validator class
public class UserValidator extends AbstractValueModel
implements Validator, User {
private User mUser = new UserImpl();
public Object getValue() {
return this;
}
public void setValue(Object pObject) {
// never used
}
public User getUser() {
return mUser;
}
public void setUser(final User pUser) {
mUser = pUser;
}
public ValidationResult validate() {
ValidationResult result = new ValidationResult();
if (ValidationUtils.isEmpty(mUser.getUserName())) {
result.addError("User name is required");
}
if (ValidationUtils.isEmpty(mUser.getPassword())) {
result.addError("Password is required");
}
return result;
}
public String getUserName() {
return mUser.getUserName();
}
public void setUserName(final String pUserName) {
mUser.setUserName(pUserName);
}
public String getPassword() {
return mUser.getPassword();
}
public void setPassword(final String pPassword) {
mUser.setPassword(pPassword);
}
}
in the form class create there fields
private ValidationResultModel mValidationResultModel =
new DefaultValidationResultModel();
private PresentationModel mPresentationModel =
new PresentationModel(new UserValidator());
private JLabel mMessageLabel = ValidationResultViewFactory.
createReportIconAndTextLabel(mValidationResultModel);
after initializing the form
Bindings.bind(userNameTextField,
mPresentationModel.getBufferedModel("userName"));
Bindings.bind(passwordTextField,
mPresentationModel.getBufferedModel("password"));
here "userName" and "password" are the properties of your domain class.

finally in the action handler for form submit
mPresentationModel.triggerCommit();
UserValidator validator =
(UserValidator) mPresentationModel.getBean();
ValidationResult result = validator.validate();
mValidationResultModel.setResult(result);
if (result.isEmpty()) {
// do your action handling here
}
isn't it very easy!!!

Friday, May 18, 2007

some photos from beitostølen, norway

in 23-25 march 2007 i went to beitostølen for a ski trip.
here are some photos form there.