LendKey

Wednesday, December 24, 2008

Why do we do final in Java?

Noticed lots of nice opensource code and Java style checker suggested to use final and static as much as possible.
I kinda understand why to use static, especially for methods, but not sure why final will help.
After a bit of google, I found the following claim at:

http://www.javaperformancetuning.com/tips/final.shtml#REF1

From: http://www.protomatter.com/nate/java-optimization/
Various tips. (Page last updated 1999?, Added 2000-10-23, Author Nate Sammons, Publisher Sammons). Tips:

Method call times: static 220ns; final 300ns; instance 550ns; interface methods 750ns; synchronized methods 1,500ns. [But times vary enormously depending on the VM and context].
Use static final methods where possible. [And do functional programming too ;-)]


Since the note was added around 1999 and 2000, I'm wondering it still stand true or not.

Now matter what, I will try my best to apply this policy to my code. But if anyone can explain why to me, it will make me feel much better.

Thanks!

Monday, November 10, 2008

Create free ringtune for iPhone

I got my iPhone for over 4 months now, and one of the biggest problem (beside of my love on it) is my favorite Ringtones in my previous phones cannot be installed on it.

Finally I ran into a site today and found the working, easy and free way to upload my ringtones:
http://www.ismashphone.com/2008/08/ringtone-iphone.html

The key is actually to use a free service provided by:
http://audiko.net

Great tool and thank you very much for sharing with us!!!

Thursday, October 23, 2008

Pidgin log file to Adium log converter

Because I'm a business owner, need to keep in touch with many of my clients, partners and employees with all different kinds of IMs(MSN, Yahoo, AIM, GTalk, Jabber, etc.). I have been using Pidgin(both Windows and Linux) to work as my All-in-one IM clients for many many years, and accumulated tons of log files through Pidgin's html log plugin. And those logs are invaluable asset to me and my business.

I switched to Mac in the past year and use Adium (http://www.adiumx.com/) to handle my multiple IM accounts. But I was never able to find a way to convert my Pidgin logs to Adium.

So I finally decided to write one myself. The script is written in PHP, you need to modify some configuration parameters before it is executed:

$srcDir= Where the original Pidgin log files and folders are located
$destDir= Where to save converted Adium log files to
$masterAlias=array("Li","Li Ma","马力");//List all Alias names you ever used in all different IM accounts and in history. Very important to make conversions right


Once the script is configured, you can run it by:

php adium_convert.php


A log file will also be created when error occurs.

You also need to copy converted Adium logs to where your current Adium's log folder, which is normally:

~/Library/Application\ Support/Adium\ 2.0/Users/Default/Logs/


The complete code is:



The script was designed to resolve my issues first. Since we have to convert Pidgin log from html format, which is not very well formatted all the time, this converter might not work 100% for your case. It took me many days to make it work for all my log files. So please make sure you verify the converted log files after you run the script. Also make sure you keep the origina log files just in case.

Enjoy!

Li

Sunday, October 5, 2008

Check object that has been locked in Oracle

select oracle_username,object_id,session_id from v$locked_object;
select object_name from dba_objects where object_id = ;

is the id of object that was returned from the first query.

Or to combine them into one script:
select l.oracle_username,l.object_id, object_name, l.session_id from v$locked_object l, dba_objects where dba_objects.object_id = l.object_id;

Friday, June 20, 2008

Allow Linux box in intranet to access internet via a gateway server

I have three servers in a intranet environment. One(A) can access internet directly. Others(B andC) are connected to A.

In order to allow B and C access internet, do the following settings:
1. vi /etc/resolve.conf
Add the following text in it:
nameserver <a's IP>

2. vi /etc/sysconfig/network
Add
GATEWAY= <a's IP>

3. Run /etc/init.d/network restart

Do the three steps on both B and C. Then they should both be able to access internet.
Try it by ping yahoo.com.

Friday, June 6, 2008

Submit form to a popup window

Sometime, when submit a form, you wanna open a new popup window and show submit result there.

Here's the code that can do the work:

<form action="report.html"
where the form will be submitted to
target="report"
name of the popup window
onsubmit="window.open('about:blank','report','width=300,height=200')">
open a new window that has the target window name and expected window size first.

It was tested in Firefox 3.0 Beta, IE 7, Opera

Tuesday, May 27, 2008

Quicken 2006 on Ubuntu 8.04 X64

Make sure you have IE4Linux (http://www.tatanka.com.br/ies4linux/page/Main_Page) installed.
Then install Quicken 2006 in Ubuntu 8.04.

Everything works smoothly, including auto upgrading patches.

I need to play more, especially test if account downloading from my online accounts still works or not.

Update: June 6th,2008
Quicken cannot do online update for my accounts, such as my credit card or band transaction history. Seams like this is an known issue. Lots of people complaint about it.

Linux driver for Lexmark E120N printer

The networked laser printer from Lexmark works like charm with my Windows machines. After I migrated to Ubuntu Linux 8.04 X64, it took me a while to find the correct driver for it.

After a few days of try, I found the best driver to use for this printer is:
Lexmark Optra E321 Foomatic/lj4dith

Finally I was able to get the first stack of fine printed document sent from my Thinkpad T61P running Ubuntu 8.04 X64!

Friday, April 25, 2008

My experience resolving slowness of PHP/MySQ

A client called me several times complaining random slowness of his site which is running a fairly old Fedora 2 with PHP 4 and MySQL 3.

The problem he had is when he access a particular page, sometime, it goes super fast, sometime, it could take over 5 minutes or more to get the result back. There're only around less than 50 people using the site at the same time. Challenge is there's no fix pattern to replicate the issue. And I personally played with the page for over 20 minutes, and never was able to replicate the issue.

With lots of frustration, I started my journey to resolve the issue.

1. My friend google.com
Searched keywords like: slow php mysql linux
Found some hints on how to optimize mySQL and PHP. Nothing really help. However, I did find a debug tool to log the slow queries.

2. Log slow queries
Then I changed my.cnf and added:

set-variable = long_query_time=3
log-slow-queries=/var/log/mysql/slow-query

Please be advised, if you do not put set-variable = in front of long_query_time=3, it will not work, and mySQL will not start.

So I got a nice report of slow queries. From there, I found out some queries without sufficient index. After adding indices, I'm sure lots of queries got improved, however, the particular issue is still there. The big random slowness still happen.

4. mytop
Found a nice tool to monitor mySQL:
http://jeremy.zawodny.com/mysql/mytop/

Installation was easy. It requires PERL and some extra modlues. CPAN did a nice work to help me install them, although it took around half hours to do the upgrading, downloading and installation. Good things is I got lots of things to do while waiting.

In order to make it easy to run mytop, need to create a config file:
~/.mytop

user=<your user name here, I used admin>
pass=<your password here>
host=localhost
db=<your DB name here>
delay=5
port=3306
socket=
batchmode=0
header=1
color=1
1dle=1

Run mytop helped me verified my optimization of queries were working. Again, the issue is still there.

3. Check error log of PHP
Went to check error log of Apache/PHP. WOW! the error log file and access log reached over 600M each!!!
That might explain why it goes so slow. When multiple requests sent to server, to handle each request, server need to log something. To append text at end of a 600M file cannot be fast, not to mention having multiple threads/process racing for the change.
So backed up the log, restarted apache. Looks like things are getting better!

4. Restart Server
Some how found the server has over 2800 tasks. Most of them are related to rpm, awk, etc.
Something like below:

4 0 13076 12539 16 0 2456 264 pipe_w S ? 0:00 awk -v progname=/etc/cron.daily/rpm progname {????? print progname ":\n"????? progname="";???? }???? { print; }
4 0 13077 13075 16 0 6280 432 - S ? 0:00 /usr/lib/rpm/rpmq -q --all --qf %{name}-%{version}-%{release}.%{arch}.rpm\n
4 0 13078 13075 18 0 28856 336 pipe_w S ? 0:00 sort
5 0 19864 1 15 0 10708 1340 pipe_w S ? 0:00 /usr/libexec/webmin/shell/index.cgi


Not sure what they are. So I decided to reboot server. Task number dropped to less than 100.

After the journey, server is working OK so far. No report of slowness yet. But I will give it a couple of more days to confirm.

Friday, January 25, 2008

Story on nagios setup and my web server

I tried to installed Nagios on my web server today, hoping it could help me monitoring the server and report any problem it can detect.

After I followed instruction on:

http://nagios.sourceforge.net/docs/3_0/quickstart-fedora.html

/usr/sbin/usermod -G nagcmd apache

to change group of apache, my server went completely down! Apache refused me from accessing any pages on mysite.

I initially thought it was related to SELinux setting, but later I figured, my SELinux is not enabled.

After reviewing secure log, I noticed apache was removed from the following two groups:
apache
psaserv


By adding it back to those groups, my sever is running OK again.

Well, be careful when you change any group setting :(

Friday, January 18, 2008

How to keep program running when ssh session is closed or network is disconnected

I guess most of sys admin or unix server had same experience as I had. Very often, I start a long task, such as disk backup, in a remove ssh terminal. After leaving it running for so many hours, suddenly a quick network disconnection will ruin all the work done before completely! A very quick network instability could drop the ssh session, and all the running work in the session simply got dropped! This is too annoying.

Today, I finally found a great tool to resolve the issue: GNU Screen.

I found a bunch of good tutorials about the great tool. One of my favorite is:
http://www.kuro5hin.org/story/2004/3/9/16838/14935

The tool can be found at:
http://www.gnu.org/software/screen/

Most likely, you probably already have it in your installation, or can simply run:

apt-get install screen
or
yum install screen

to get it.

The idea of screen, I guess, is to create a bunch of local session to terminal services. What your SSH will only connect to the local session, and the real work will be done in the local session. So even you lose connection to screen, your work is still running. When your network is back, you simply attach to the screen session, and you can get everything back. The idea is really similar to VNC or other remote control tools.

For more information about the tool, please check the links I provided above. Hopefully it can resolve lots of headache you already had.

Monday, January 14, 2008

How to config apache to set bandwidth limitation

Here's a nice document about how to set bandwidth limitation on Apache 2 with a module called mod_cband.

http://www.howtoforge.com/mod_cband_apache2_bandwidth_quota_throttling

Monday, January 7, 2008

Download older version of Oracle products

Oracle only put the latest supported product on their site for download. If by any chance an older version is required, you can go to the following site to see if you can find the old version you need:

http://edelivery.oracle.com/

I was looking for Oracle client 10.1.0.3.0 when Oracle only had 10.2.0.4.0 for download. After about 1 month of search, I finally found 10.1.xxx from that site.

Tuesday, January 1, 2008

Fix display problem when running Far Manager in Chinese environment

Far Manager is the greatest file manager I have been using over the longest time and cannot live without.

Besides of all the great features and plugins, I do keep running into an annoying issue on my Windows with region set to Chinese. The problem is in Chinese environment, FAR window will be screwed with panel borders shown as garbage characters like below :


I have tried many ways to resolve the issue.
On Win95 through XP, I was able to create a batch file just to start Far, create a shortcut to the batch and set Code Page to English. Although I will not be able to see any Chinese file names, the display looks good.

But in Vista, somehow the code page dropdown turned to be empty. So I had to search for other solutions. Luckily, I do find something very handy which I will share below. Basically you can create a shortcut, and enter the following command into it:

C:\Windows\System32\cmd.exe /c chcp 437&&"C:\Program Files (x86)\Far\far.exe"


The idea is simple, it set current code page to 437 by "chcp 437". 437 is English in Windows.
Then start Far.

Please be advised, I'm running Vista 64bit. If you run in 32bit OS, the path is different. But it does not matter.

I also often change layout of shortcut to: 100 x 50:


Click on the shortcut, you will see a nice Far window:



I think the trick also works on other locales in case you run into the same issue.

Just heard Far goes open source! What a great news! Wow!!!