Tuesday, April 02, 2013

Releasing resource in Java (no destructor)

I needed to release resource in Java class and thinking about C++ style of resource management in class (open in constructor and release it in destructor). It turns out that there is no destructor in Java (finally doesn't cut it). So instead of open the resource in constructor and release it in destructor I should open it locally in the function and use try finally construct. Something like below (quoted from StackOverflow link on references list below) :

Resource r = new Resource();
try {
    //work
} finally {
    r.dispose();
}

Not as elegant as having the real destuctor (I'll need to use the above locally in every functions that needed it) but it gets the job done.

References :

http://stackoverflow.com/questions/171952/is-there-a-destructor-for-java
http://c2.com/cgi/wiki?FinalizeInsteadOfProperDestructor

Wednesday, March 13, 2013

JodaTime, for easier DateTime handling in Java

Having tasted the DateTime classes in .Net made you wish that there is something like it on the Java World. Compared to .Net's DateTime, what's available on Java world regarding the date and time handling feelds a little dated. It turns out that there is even  a better stuff on Java, JodaTime. It has more detailed and useful abstraction regarding date time matters. It even has a sister library NodaTime for .Net which seems shows that the abstraction choices adopted to significant number of developers.

You can head out to it's front page and read "Why Joda Time?" section on overview of the relevance of the library. For me personally, it makes the bridging from .Net world of DateTime to Java world easier

Friday, February 15, 2013

Tunnel Internet traffic through OpenVPN per client only

We have been using OpenVPN at the office to access development resources only. So, as long as we have LAN connection to the VPN host server all is good. However, there is the need to direct all traffic including internet through it. It can be useful on public network to have it as a secure tunnel for all traffic to have more peace of mind. I don't want it to apply to every connection, I still prefer using internet non-tunneled on secure local network for speed reason.

Some references assume the setting is to be applied to all connected clients so the setup is set on server config. The notes below move the server-side setting to client so it can be set per client/case only.

Setting things up on the server side

The server is Ubuntu using Shorewall as a Firewall. What is needed on the server side is setting NAT from OpenVPN interface to the internet so the internet traffic from vpn can be forwarded to the outside world. Here's what to do

Allow IP Forwarding in /etc/shorewall/shorewall.conf :

IP_FORWARDING=Yes

add entry to /etc/shorewall/masq :

eth0    [vpn network IP]

allow traffic from vpn to internet on /etc/shorewall/policy :

vpn net ACCEPT

Client Side 


Here's the lines to be added on the client configuration :

redirect-gateway def1
dhcp-option DNS 8.8.8.8
dhcp-option DNS 8.8.4.4


Google DNS used to override whatever exist on client system in case it cannot be accessed through the tunnel.

I make two different .ovpn file with the same keys setting but one has the above setting. This makes it easy to switch between the one for tunneling internet traffic and vpn-lan-only.

That's about it. Below is some source references :

  • http://openvpn.net/index.php/open-source/documentation/howto.html#redirect
  • http://wiki.debian.org/HowTo/shorewall (part about PAT and NAT)


Tuesday, January 08, 2013

Utilizing Springpad with a little help from Evernote

I stumble upon Springpad sometime ago. It was on some discussion on some forum (probably stackoverflow, it's a bit hazy now) about programmer's notebook. It was hard to really pinpoint the usage of it for me initially since it's feature is all over the place. I can't really describe what it is personally, you just have to try it yourself to really get the feel of it. It's a mix of Evernote, Pinterest and probably several other cloud-services.

I tried to use it for several purpose some which does not seems to fit well. In the end I settle with using it for more collecting-related purposes. It is a great tool to collect things on the fly. On GTD space it is great for Inbox and Someday/Maybe stuff. I migrated my Waiting-For list to it also since it has a good list item support. Here's the highlight of it's features that relevant for my usage :

  • Flexible views. You can see the items using several different layout : cards, list, custom. If you are an Evernote's user, you might find some visuals quite interesting compare to it, at least visually
  • Notebook and Tagging organization
  • Great iOS mobile client feature-wise although still quite buggy in term of stability
  •  Interesting collecting workflow. Saving books, movies and other stuff is actually fun
The noticeable part where Springpad is still lacking is on the note part. It's mobile implemention is quite out-of-sync with the web one. I tried to copy some materials through it's web app which end up uneditable on the mobile. Some that is editable contains various character-encoding artifact. You have to be really careful to get the notes in-sync with mobile experience which is quite a deal-breaker for me. I'd rather focus on my work than spending time on hand-fixing the notes.

Comes Evernote to the rescue. I am not a newcomer with Evernote, it's just that somehow I prefer OneNote style of note-taking (on iMac I settle with Growly that has the closest workflow with OneNote) so I am not actively using it. I have been thinking about porting the note-taking business to the cloud though and it seems a good time to do so. Not much needed to say when related to cloud-based note app since currently Evernote is the de-facto standard. I need to adjust my system though since it only has limited notebook number allowed and the organization seems to center around tagging (I think I am gonna miss OneNote a lot due to this).

So, the serious note matters are on Evernote while Springpad note system currently only useful for basic notes for me. I might switch fully to Springpad if it has a comparable note feature (accross multiple device that is). Until then, having to switch between the two is bearable considering the usefulness.

Thursday, December 06, 2012

Setup Cyrus IMAP account on Thunderbird

I connected to Cyrus-IMAP server using Thunderbird. There's some problems on the first try :

  • All the folder reside under INBOX folder. Trying to move them up fails
  • The sending does not work, always hang with the message box informing that it can not save to Sent folder
I was in the middle of following some troubleshooting direction from this discussion when I see there's textbox labeled "IMAP Server Directory" in "Account Advanced Setting" dialog (Account Management > Server Setting > Advanced) . I input the annoying "INBOX" root folder mentioned above and surprise surprise, it makes the root folder go away. The sending is acting normally after it too.

Below is how the dialog looks like :


Wednesday, December 05, 2012

Setup postfixadmin on top of existing postfix installation

My office mailserver was a result of migration sometime ago. It was done in a limited time at that time so I only migrated the needed data and setup to keep it running as soon as possible. So, stuff like postfixadmin was left out (the system is postfix+courier+sasl setup).

Until this recent time that I need to add the account that it started to bite me. Most of the setup guide for postfixadmin assume a pristine install where it generate the database from scratch which could possibly wipe out my existing installation. So, I need to follow them less linearly.

After some searching around, I settled using this article as a starting point. However, it's quite outdated (to the point the writer marked it oudated on a big font at the top of the page) but it prove useful in my case nonetheless. It also assume fresh install but it define the steps more clearly including some behind the scene matters that happen which help me make some workaround for my specific case.

So, here's the summary (some silly-but-useful trial and errors and mistakes omitted) on what I did :

  • Backup like crazy. It's a live system after all and one can not be too careful
  • Download and install the .deb package from  postfixadmin site : dpkg -i postfixadmin*.deb.  Saying no when asked for auto setup apache and database.
  • Add apache setting : make symbolic link on /etc/apache2/conf.d/postfixadmin to /etc/postfixadmin/apache.conf
  • Add setting for the existing postfix database to /etc/postfixadmin/config.inc.php along with other settings there
  • I couldn't find the admin account for it and find the solution to reset it here. Here's what I did on my case : 
    • Installed dovecot-common package since I used courier instead of dovecot. It is needed to get access to dovecotpw utility
    • run : dovecotpw -s MD5-CRYPT -p password | sed 's/{MD5-CRYPT}//'
    • update the admin account (it's on admin table of postfix db) with the above result  
After that things went quite smoothly. I get access to user management again and adding the user is a snap.


Wednesday, November 14, 2012

Multiple Monitor Setup : 2 +1

It is quite the norm now to use multiple monitor setup for software development. It used to be that dual monitor setup is quite hot but now three monitor is already quite common. However, I find it better to have 2 + 1 setup instead of going to 3 monitor setup (with "+ 1" being a screen of another device ).

I have only certain space at one time (not to mention attention) and with several devices and gadgets I use, they all have to battle for the space in my workspace. So, I set aside one side for secondary device instead of third monitor. I find it more useful this way since I have more flexibility with the third space which I can use for Laptop, Tablet or even Books when needed. Also, if I want to breath a little I can just clear it up (or have there a snack, tea or whatever).

Below is my current workspace following the above setup.


With the above setup, I have quite generous leftover space as below.


 As you might see, there is still some space free for support stuff.  That is PS3 on the far corner for those stressful times :), connected to the secondary monitor (Dell monitor) that has comfortable source auto-switch.

And here's my Windows workspace which has the blank right space to be used for the +1  as the above setup i.e: for non-desktop devices.

As you might notice, I am generally a desktop guy. I don't like working on laptop so much (at least for major development work) unless I really need to.

I find the +1 screen can function as third monitor quite easily e.g: opening references, utilities and even with more flexibility. The downside is that it has no direct computing connection to the main machine which break a flow a little compare to having the keyboard and mouse interacting with the integrated three screen. This can be helped a little using stuff like cloud, file sharing,  bonjour protocol, etc.