Thursday, February 26, 2009

yesterday's date in linux

To get yesterday's date in linux, simply use the following command:
$ date --date='1 day ago'

Sunday, February 22, 2009

Servlet unit testing using Mockrunner

Servlet unit testing using Mockrunner (container free)

I had to write a servlet earlier this month and was looking for a way to do unit test for it. I was hoping that there is a way to do unit test for servlet. Right away, I knew it would not be straight forward as unit testing a regular simple java class. If you are familiar with servlet, of course you know that it depends on some other classes, such as HttpServletRequest, HttpServletResponse, etc.

So, one way of testing it, we need mock object. There is an excellent information about what mock object is all about by Martin Fowler, in this link.
While there are many other framework for JEE unit testing, I chose Mockrunner as the framework for my unit testing.

Download Mockrunner from the site, and include the library in your project. After that, create a test class which extends BasicServletTestCaseAdapter, so your test class would be something like the following:


public class MyServletTest extends BasicServletTestCaseAdapter {
// ...
}

then create an instance of the servlet you want to test, in this example would be MyServletTest:
protected void setUp() throws Exception
{
super.setUp();
createServlet(MyServletTest.class);
}



if your servlet requires parameter(s) then use addRequestParameter() method to set each one:
addRequestParameter("id", 1234);

The servlet that I created need to check the requested URL, so I had to find a way how to set it up. Fortunately, it is possible to do. Here's an example:
getWebMockObjectFactory().getMockRequest().setRequestURL("subscribe");

After all the setup done, it's time to test the servlet, by calling doPost() method, and check all actions that have been done.
I needed to check whether my servlet redirecting to the proper page afterwards. Here's how to do it:


MockHttpServletResponse response = getWebMockObjectFactory().getMockResponse();
assertEquals("redirect_location", response.getHeader("Location"));



I am glad I found this neat testing tool.

Saturday, February 21, 2009

ACT!2009 SDK

ACT!2009 SDK

Nice thing about the recent version of ACT is that they provide a way to extend the functionality using either C# or VB.NET.

You can either create a plugin (dll) to extend the existing functionality of ACT, or create a separate stand alone application that uses the ACT Framework.

I haven't done much with the SDK, still trying to get a feel with the whole Framework thing. Besides, I am also not so sure it's a good idea to create many plugins and applications, since there are some nasty bugs in ACT!2009, like I mentioned in my earlier post.

In order to use the SDK, you need to have the original Installer CD. Go to the following folder to find all necessary dll's ACT!2009\PremiumEX\ACTWG\GlobalAssemblyCache. This the case where you don't have the ACT!2009 installed in your system.
If you have ACT installed in your system, you can go to C:\WINDOWS\assembly\GAC_MSIL and find all the required dll's. Notice that you can not see GAC_MSIL folder from explorer, but instead you can see it from CLI.

ACT!2009 Backup

ACT!2009 Backup

Do you backup your ACT!2009 DB regularly? If not, you should configure automatic backup.
Use ACT Scheduler to schedule automatic backup. Refer to this link on how to set it up.

I really suggest to store the backup in different place then where the ACT!2009 data is located. You might face some problems if you put in the default backup folder. Refer to this link for more information about this.

If somehow you decided to store the backup in other computer/server that needed login validation, you need to configure the ACT Scheduler Service to use proper login account. Click here to see how to do it.

List of problem with ACT!2009 + Outlook 2007

List of problem with ACT!2009 + Outlook 2007

List of ACT!2009 and Outlook 2007 known problem (so far) as per 20090221

1. After performing lookup by example in ACT, the information shown in the fields are from the previous contact. This can be quite confusing for user. Until now, there is no solution for this issue. The only thing we can do is, pressing "F5", to refresh the page, after performing any lookup.

2. Several fields are not shown in import/export wizard in ACT. I consider this issue as the worst one. I was facing a lot of trouble when trying to import some contacts from other database. Too many missing data, which really makes the import/export process is useless. True, we can always use the quick export to excel, but the number of the fields is then limited to what is supported by excel. No good.
Click here to get more information about the problem (but not the solution).

3. Again in ACT. If you happened to have date or annual event field type in your layout, be very careful. Usually, after I make any change to the date field, I click outside, such as the layout background, so when I use the scroll wheel in my mouse, the date will not get changed. Surprisingly, the date still get changed. So, either use the scroll bar, or click on text field then use the scroll wheel.

4. When performing mail merge to a lot of contacts, for example a thousand, outlook crashes. Not good. Solution? Use different email client that can handle big number of emails.

5. I created a simple vba script to perform mail merge from outlook. I was hoping that using vba script, I could perform the mail merge without outlook crashing, and the email would be attached in history. Emails went through just fine, but this time ACT.Outlook.Service.exe crashed.

vnc over ssh

For those who like to use vnc to work with remote computer, you should be aware that the traffic is not encrypted. Please refer to tightvnc faq page for information about this. Just like what recommended in the faq, ssh is required to secure the traffic.

Create a tunnel between your local and remote computer, and one way to do this is:
$ ssh -CNT username@remote_computer -L 5901:127.0.0.1:5900

Now, simply set your vnc viewer to access localhost with port 5901.

An example of using vncviewer is:
$ vncviewer localhost:5901

Friday, February 20, 2009

ecryptfs == privacy

ecryptfs == privacy

Simple and nice way to protect your documents in linux.
Install ecryptfs by executing the following command:
$ sudo apt-get install ecryptfs-utils

Create a directory where you want to put those private documents. For example I will create a folder in my home, named "Personal", and set the permission only to owner.
$ mkdir ~/Personal
$ chmod 700 ~/Personal

Mount that directory as eCryptfs filesystem.

$ sudo mount -t ecryptfs ~/Personal ~/Personal
I chose passphrase, aes, and 16-bit. For plaintext passthrough I chose "no" (no non-ecrypted files allowed in the mount).

Move, copy, create all private documents into ~/Personal directory.

When done, umount ~/Personal by executing:
$ sudo umount ~/Personal

All the files inside that directory can not be opened unless you mount it as ecryptfs filesystem.

Next time you mount, use the following command, so it will only ask for passphrase.
$ sudo mount -t ecryptfs ~/Personal ~/Personal -o key=passphrase,ecryptfs_cipher=aes,ecryptfs_key_bytes=16,ecryptfs_passthrough=n

Thursday, February 19, 2009

ACT + Outlook 2007 + PST over the LAN = Huge Mess

ACT + Outlook 2007 + PST over the LAN = Huge Mess

I had some problems with ACT!2009 and Outlook 2007. From Outlook, I kept getting pop-up error message saying that the pst file was locked, probably used in some other terminal. ACT!2009 kept giving me pop-up error message saying something like Act! Value cannot be null. Parameter name:pUnk. What an error message, and who is this pUnk guy, anyway.

So, initially the pst file was stored in the server, so different user can take care the incoming file at a time. True, that Exchange server should be used in this particular case, but somehow they don't want to use it, with many different reason, which I don't agree. The user insisted that they want to have convenience of not having to switch back and forth using different computer. Therefore, pst file stored in server.

When outlook showed error saying that the pst file was locked and being used in other terminal, one thing I had in my mind was, probably that's true. So, I made sure no one else is using it. Still, the error occurred many times.
The error message from ACT was really gave me no clue on what's going on.

After spending a lot of time searching here and there, I came across the following information: Personal folder files are unsupported over a LAN or over a WAN link, http://support.microsoft.com/kb/297019

My intuition told me that this must have something to do with the problems we were having. Well, it turned out to be that storing pst file in the server and accessed over the network is a terrible idea.

Once I convinced users that since they don't have Exchange server and pst file is not designed to be used over network, they agree to move the pst files locally. Problem solved.

ACT!2000 to ACT!2009 Migration

ACT!2000 to ACT!2009 Migration

Currently, I work a lot with ACT!2009, Contact and Customer Relationship Management Software. This is pretty new thing for me. Interesting and troublesome at the same time.

I had to migrate all data from ACT!2000 to ACT!2009. According to the ACT Knowledge Base, the procedure should be simple, and straight forward. The only problem is, the database in ACT!2000 was corrupted, quite badly. ACT diagnostic tool could not fix the problem, and the only way to fix this is by using import/export features. Please go here to get detailed information on how to use import and export to repair ACT!2000 database.

The process was time consuming and when there were duplicate contact records found during import, user interaction was needed. Apparently the there were a lot of duplicate contact found in the database.
One lesson learned here, which suppose to be done by previous user, is to resolve duplicate contact record.

Once the repair process done, I simply opened the previous version of ACT database in the new ACT, and the conversion process begins.
After the conversion done, there are many fields that are not converted to the properly type. For example, date field become annual event. Memo files become regular text fields. No good.

ACT Knowledge Base is a place to go for ACT users.

ssh tunneling

ssh tunneling

In the client execute:
$ ssh -N -D user@CompB

where CompB is the server

To use this ssh tunneling with internet browser, for example firefox, change the "Connection Settings", under "Manual proxy configuration", update the "SOCKS Host:" to localhost:any_free_port. Notice that any_free_port here must be the same one as the one used in ssh.

Opera browser does not have socks proxy configuration. One way to do it is by using "tsocks."
Install tsocks:
$ sudo apt-get install tsocks

edit /etc/tsocks.conf, and change
server = 127.0.0.1 # this is localhost
server_port = # again, this must match with the port used in ssh.

screen

I work remotely a lot, and mostly using ssh, since it's more secure than telnet. There's a time when the process takes very long time, and I don't want to always be connected the whole time.
The problem is, when I put the process in the background (using ctrl-z and bg, or using "&" when executing), when I exited the remote shell, all processes belong to that remote shell will be terminated.

One way to resolve this issue is using "nohup". This is very useful when the process does not require any interaction. Still I'm not satisfied with "nohup."

Then I came across this very neat tool, "screen." Very elegant way to do things remotely.

Do the following:

$ screen

it will give you the session, and start to do any activity. So, when I want to leave the remote shell. Press --> ctrl-a, d. That command will detach the screen session, and it's save to exit the remote shell.

To resume that particular session. Login again to the remote computer, then look at the detached session, by:
$ screen -ls
$ screen -r <session_id>

rsync + ssh => resumable download

rsync + ssh => resumable download

Sometimes ago, I felt the necessity of being able to resume my download from another computer, specially if the file is quite big. In this case, it's time to use rsync. Since mostly I use ssh to access remote computer, so I combine rsync with ssh.

Here's how to do it:
$ rsync --partial --progress --rsh=ssh user@server:/serverpath/to/file /localpath/to/file

Now, what if you need to use non-standard ssh port? Let's say you need to connect to ssh via port 7890. The command will be like the following:
$ rsync --partial --progress --rsh='ssh -p7890' user@server:/serverpath/to/file /localpath/to/file

The neat thing is that there is also progress indicator shown.

Tuesday, February 17, 2009

Resolve Windows hostname in Linux

Resolve Windows hostname in Linux

I work a lot in heterogeneous environment, and ofcourse, windows is one of the system in that network. So, I would like to be able to resolve windows hostname (netbios) and access it's resource. In order to do that, there are packages needed to be installed first.

$ sudo apt-get install winbind

after that, edit /etc/nsswitch.conf configuration file.
Search for word "hosts", and add "wins" at the end of the line. Below is an example of "hosts" configuration after update.

# /etc/nsswitch.conf
#
# ....

hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4 wins

#....

after that, restart winbind service (if necessary). Now, windows computers should be able to be accessed using it's windows hostname. Pretty convenient way to access those windows computer in the network.

If you are still not able to resolve windows hostname, perhaps there is a firewall running in your computer. In that case you need to add extra rule to allow udp.

I use firestarter for my personal firewall, and what I did was edit the /etc/firestarter/inbound/setup configuration file.
Search for:
$IPT -A INBOUND -p udp -m state --state ESTABLISHED,RELATED -j ACCEPT
and put the following rule right after it:
$IPT -A INBOUND -s $NET -p udp -m state --state NEW -j ACCEPT

restart firestarter, to load the new config. Try to access windows computer using it's hostname again.


I just found out a way to configure UFW for this matter.
First edit: /etc/default/ufw
Search for:

IPT_MODULES="nf_conntrack_ftp nf_nat_ftp nf_conntrack_irc nf_nat_irc"

and change it to:
IPT_MODULES="nf_conntrack_ftp nf_nat_ftp nf_conntrack_irc nf_nat_irc nf_conntrack_netbios_ns"


Then: reload ufw
# sudo ufw reload

Next step: Configure UFW Rules
Allow outgoing UDP to port: 137, 138
Allow outgoing TCP to port: 139, 445


Sunday, February 15, 2009

convert bin to iso (linux)

convert bin to iso (linux)

To convert bin file to iso format, simply use bchunk tool in linux.
Check whether it's already in your system (ex. $ which bchunck).
If not, install the tool first by executing:
$ sudo apt-get bchunk


after that, let's convert bin to iso, by executing:
$ bchunk binimage.bin binimage.cue isoimage


Notice that bchucnk needs cue file (as second argument).
If there is no cue file, do not panic, since it's easy to create the cue file.

Below is a simple shell script to create cue file and convert bin to iso:
#!/bin/bash
printf "FILE $1.bin BINARY\nTRACK 01 MODE1/2352\nINDEX 01 00:00:00\n" > $1.cue
bchunk $1.bin $1.cue $1_
rm $1.cue


save the script to a file, don't forget to chmod +x, and run it.