Archive for the ‘Tech Tips’ Category

Interface translation in symfony - tool for teams

Wednesday, July 16th, 2008

I mentioned in my previous post how we decouple the translation dictionary completely from the source so that a change in any text does not affect the templates. That has helped us a lot in several large projects.

Another issue which we frequently encountered when multiple people worked on the same project is conflicting and merging of the dictionary files. Since these files have sequential numbers in each <trans-unit> block, if multiple people want to add translations, these suquential numbers almost overlap and have to be rewritten manually while checking in.

So we decided to generate these symfony interface translation dictionary files using a script. So here’s what we do:

  •  put the translations in a properties file: Create one properties file for each language. So for English we create “translations.en.properties”. This has translations like:

USERNAME_LABEL=Username
PASSWORD_LABEL=Password

This ensures that even when multiple people add translations to these files (using a source control like CVS or SVN) there are no issues.

  • Also put the following file createXML.php in the same i18b folder.   
  • Now you can simply call this script from the command prompt to generate the xml dictionary.

php createXML.php > messages.en.xml

That was pretty simple to do. There are some enhancements this requires, like it should by itself generate all translation files based on the properties files available. But thats for when i get more time to work on it.

Passwords in clear text

Friday, July 4th, 2008

I just received a newsletter from a social network site (brijj.com) and was surprised how they had embedded my username and password in clear text. It speaks volumes on how seriously they take security. I think when storing passwords all sites should go by few rules:

  • Do not store passwords in cleartext. Never ever.
  • Use any encryption technology, preferable a 1-way hash so that nobody can decipher the password.
  • If you break the above rules, which several sites do, at least do not send out passwords in emails without being asked for. That’s the worst you can do.

Everyone should realize that people tend to use same passwords for several sites and accounts so sending out passwords in clear text not only compromises the user’s account at your site but possibly at other places as well.

Date manipulation using GNU date

Saturday, February 9th, 2008

GNU date command lets you do just about everything related to date and time but date manipulation is something not well documented.

# get date 2 weeks from now
date –date “now 2 week”

# 2 months earlier
date –date “now -2 month”

you can give any date in place of ‘now’ or use a variable.

# what day is it after 10 days?
date –date “now 10 day” +%A

GlassFish Auto Deployment and Hot Deployment

Tuesday, October 23rd, 2007

There are several ways to deploy a GlassFish application. To speed up development/debuging we need a way to instantly deploy web applications. One of the ways is to use hot deployment feature, another lesser known feature is ‘directory deployment’. You simply point GlassFish to your development directory and let it pick up and deploy application from there. No packaging and re-deoploying hassles. The catch is whenever you want to re-deploy your application you just need to ‘touch’ a file called ‘.reload’ which should be present in your web folder.
Following is the command and directory structure you can use.
–|myproj
–|–|src
–|–|web
–|–|–|WEB-INF
–|–|–|–lib
–|–|–|–classes
–|–|–|–web.xml
–|–|.reload

GLASS_FISH_HOME/bin/asadmin deploydir full_path_to_you_web_folder

If you are using Eclipse or NetBeans make sure that they put the class files in web-inf/class folder after compiling.

Recovering database from MySQL 4.x

Friday, August 31st, 2007

Recently I was involved with a task to recover a database on MySQL 4.x that crashed. Below are the steps that I did to recover the database. The commands in this post are available on MySQL 4.x, if you are on MySQL version higher than this then check MySQL 5.x

1. The MYI file of the database was corrupted and I could find out that by trying to select rows from one of the tables. The error message returned for the select query - ERROR 1016 (HY000): Can’t open file: ‘TABLE.MYI’ (errno: 144).

2. Next step was to run myisamchk for which I should shut down the database first by issuing command “mysqladmin -u root shutdown” at the command prompt

3. First I tried using the –quick option on the database to rebuild the index only
/pathtomysqlexecutablefiles/myisamchk –quick /pathtodatabase/*.MYI [ to repair all the tables use wildcard (*) ], the command executed successfully, and displayed the repair information.

4. I again started mysql server using command “mysqld &”

5. Logged in to mysql prompt and selected the repaired database. To know the status of the tables after repair I executed “show table status” on the mysql prompt.

6. The results displayed error “Incorrect information in file: ‘./database/TABLE.frm” which means the frm file of the database got corrupted. Searching for the same on the web I found if we run an extended check using myisamchk may solve this problem for which I had to go to step 2.

7. There are few checks one should do if step 3 cannot help you recover the database, and after every check repeat steps 4 - 6 in the same order. You would know your table is recovered when “show table status” shows no errors, and to confirm this further you should be able to select rows from the table.

a) /pathtomysqlexecutablefiles/myisamchk –force –fast -c -i -r /pathtodatabase/*.MYI
b) /pathtomysqlexecutablefiles/myisamchk –force –fast -c -i -o /pathtodatabase/*.MYI
c) /pathtomysqlexecutablefiles/myisamchk –force –fast –update-state -i -r -e /pathtodatabase/*.MYI

if it gives error - myisamchk: error: Not enough memory for blob at 1940 (need 1890777748) (1.7GB thats wierd)
but if you think your table may be holding that much of data and your server has huge memory you may use
d) /pathtomysqlexecutablefiles/myisamchk –fast –update-state -i -o -e -O key_buffer_size=2048M -O read_buffer_size=2048M -O write_buffer_size=2048M /pathtodatabase/*.MYI
Thats giving 2 GB of memory to myisamchk for processing!
If you want to know more about visit : myisamchk Memory Usage

If the error about memory persists, it means there has been serious damage to the database. Same was the case with my database, these are the steps for recovering in such scenario:
a) Recreate a database on local MySQL server.
b) Recreate all the tables with similar schema as it is on the web server
c) Stop local MySQL server
d) Copy frm and MYD to the local computer at the same place where your newly created database exists with its (frm, MYI and, MYD) files.
e) Execute myisamchk –quick /path/to/datadir/*.MYI (One can also use REPAIR TABLE command from mysql prompt)
f) Restart MySQL Server and try selecting rows from the tables.

Free Photo Editing Softwares

Monday, August 20th, 2007

Wanting a powerful and free photo editing software is a common thing. I mean when you are trying it as a hobby or for some small time fun, why would you pay $649 for Photoshop ? This is where free softwares come to your aid. Of course if you are reading this, you have looked around for them and have come across the same old lists. So which one to use ? That question is very common - and very hard to answer too. You could go for an MS Paint-Style editor with a few extra functions or yield the real power from a Photoshop-Style editor. So I am here to help you in your quest for the right editor.

  • The GIMP : You will hear this name very often. GIMP ( GNU Image Manipulation Program ) is one of the most famous names out there. GIMP started as a free open-source replacement for Photoshop and has become what it wanted to be. It is getting better with each release and the number of users is increasing rapidly. It works on Linux, Unix, Windows and Macintosh and is Multilingual. It is the idle Photoshop replacement for many people. Here are a few screenshots. If you want to ask me, this is what I recommend to anyone who is looking for a free image manipulation software/image editor.
    • GIMP on Linux
    • GIMP on Unix
    • GIMP on Windows
    • GIMP on Mac
  • GIMPshop : GIMPshop is a GIMP hack to make it feel like Photoshop. It does not sacrifice or add any features but the interface is modified to make Photoshop users feel comfortable with it. So if you are switching from Photoshop to GIMP, you should probably go for this.
  • : Another great windows-only alternative. Considered by some as “Free Photoshop”. According to their site, Paint.NET started as a undergraduate college senior design project mentored my Microsoft and was originally intended to be used as a free replacement for Microsoft Paint. It kept getting more features and became what it is now. Personally, I don’t like it much. But then that is just my opinion. You should go and try and decide for yourself. It is very powerful, but I find GIMP better. Heres a screenshot of it.
  • Pixen : Pixen is a mac-only pixel art designer. Its made for pixel artists who do their work pixel by pixel. If you are looking for big sized realistic pictures, you better avoid this one. But if you are into pixel art or want to try it, and have a mac or a mac emulator on your Windows/Linux/Unix, then you can go for it. But keep in mind that this doesn’t support layers. If you need layers, try the next one.
  • mtPaint : Another one made for pixel art. This one comes for Linux and Windows. This has support for layers so you might want to go for it if you need layers ( Pixen doesn’t support layers ). Some useful features are present in there but don’t expect something really big.
  • : This ones not really that famous. Based on GIMP and for Mac only. And its not there to replace Photoshop. A simple interface for basic home users. So you are better off with GIMP.

Interface translation in symfony

Thursday, July 26th, 2007

Symfony provides interface translation using the XLIFF standard. While using the XLIFF standard here is good, it has one pain point. The pain point becomes apparent when the need arises for changing/editing the phrases. If the phrase is used in several templates, you need to search all the templates and change there as well as in the dictionary.

For example, lets say we define a simple label like “Username” in the dictionary and in several templates. The dictionary will look like:

<?xml version="1.0" ?>
<xliff version="1.0">
  <file orginal="global" source-language="en_US"
    datatype="plaintext">
    <body>
      <trans-unit id="1">
        <source>Username</source>
        <target>ユーザー名</target>
      </trans-unit>
    </body>
  </file>
</xliff>

And in all templates that use the label “Username” we will use the following to translate it based on user’s culture:

echo __('Username');

This is good, but lets say down the line we decide to use the label “User Id” instead of “Username”. To do that we need to update all the templates and the dictionary. This becomes cumbersome if you have a large number of templates that reference this label. If you notice, the cause of the problem lies in the fact that we have used the actual text itself to identify the i18n phrase within all templates.

To get around this problem, what we did was to use a code to identify the phrase and use that in the template. And we provide a dictionary for each of the supported language. So in this case we create an English and Japanese dictionary both. But what about the source language, you would ask. Well, thats a hack: we ended up providing a non existent ISO code. Based on the comment from Dennis, we use English as the source since translation happens even if source and target languages are same.

So here is what our English and Japanese dictionary look like:

<?xml version="1.0" ?>
<xliff version="1.0">
  <file orginal="global" source-language="en_US"
    datatype="plaintext">
    <body>
      <trans-unit id="1">
        <source>LABEL_USERNAME</source>
        <target>Username</target>
      </trans-unit>
    </body>
  </file>
</xliff>

<?xml version="1.0" ?>
<xliff version="1.0">
  <file orginal="global" source-language="en_US"
    datatype="plaintext">
    <body>
      <trans-unit id="1">
        <source>LABEL_USERNAME</source>
        <target>ユーザー名</target>
      </trans-unit>
    </body>
  </file>
</xliff>

And the phrase in the template looks like:

echo __('LABEL_USERNAME')

So now if you want to change the English version of the phrase, you simply update the dictionary and no need to change the templates.

FireFox extensions I cannot work without

Tuesday, January 23rd, 2007

Firefox, Eclipse, Thunderbird, Kopete/GAIM, OpenOffice, konsole, tomboy this is where I spent over 95% of my computer time.

Best thing about all these applications/products is that you can install additional plugin/extension/addons to enhance their usage. Following is the list of firefox plugins I use almost daily.

Firebug - A must have, very helpful js/html debugging tool.

Web Developer - Very helpful for finding html/form related problems. Also a good place to learn how to build firefox extensions.

JSView - Lets you view source of externs js, css files. Very handy.

Load Time Analyzer allows developers to measure and graph how long web pages take to load

Server Switcher It allows you to easily switch between sites on your development and live servers Very helpful.

SearchStatus Display the Google PageRank and Alexa popularity score

del.icio.us
Next: Eclipse plugins I cannot work without :)

Why do I use Frameworks and Design Patterns?

Sunday, August 13th, 2006

I have been designing and developing for over 10 years now. I have never worked on same technology for more than 2 years. This gave me oppurtunity to learn new stuff but at the same time it made my job very challenging. Time to market is still a key differentiating factor and in this competitive world where for once money and skills are available in abundance, speed is something which can make you stand tall among the crowd. So how do you develop FAST making sure your design is extensible, your application can handle unlimited change requests, you can scale to increasing demand, you can modify look and feel every x months and lot more :) . How do you design a critical system in less time and still be confident that you are moving in the right direction?

Nothing can replace experience, now if you do not have experience yourself how can you take advantage of other people’s experience? This is where frameworks and Design patterns come.

Framework helps you start fast and Design patterns help you design right and fast. You are confident because you are using a tested and proven solution. When you have to do more in less time you have to pay more attention to issues that can become big later. Design patterns help you identify those too, it lets you identify those issues. Another side-effect of using frameworks / patterns is that it helps you communicate better. This is really helpful when you are working in a distributed enviornment. If both teams have some knowledge of both lots of times its very easy to communicate/discuss a technical problem and their solutions. Frameworks provide you some common set of features that you need in every application like session handling, logging, error/exception handling, i18n, templating etc. Most of the frameworks implement common design patterns.

more on some of the frameworks we use Struts, symfony, RCP

Enabling Java support in PHP

Saturday, March 11th, 2006

Note: This article was written after implementing Java (1.4.2_06) support on PHP 5.1.2 (cli) (built: Feb 28 2006 06:21:15)

As written on php.net there are two ways of integrating:

  1. Integrate PHP into Java
  2. Integrate Java into PHP

We will be talking about “Integrating Java into PHP”. This again can be done in two ways

  1. Re-compile php using –with-java=$JAVA_HOME (Ample of documentation availbale on net on how to compile php)
  2. Use php-java-bridge.

Enabling php-java-bridge is fairly easy and recommended as you wont have to compile php which might be already customised.

  • Get source for php-java-bridge from sourceforge
  • Decompress
  • cd to “php-java-bridge” source folder
  • phpize
  • ./configure –with-java=$JAVA_HOME
  • make
  • make install

This will install libraries in some folder eg /usr/lib/20050922 copy JavaBridge.jar java.so libnatcJavaBridge.so to php modules folder eg. /usr/lib/php/modules restart apache (service httpd restart) Look at out of phpinfo() and confirm that you see “java running” You can either write a php file and access it from your browser or quickly type echo ” | php | fgrep java at command prompt