Category Archives: JAVA

There is a better way to implement X

One of the code reviews comments that I’ve been seeing recently is “There is a better way to do this” and without further explanation.

By far this is one of the killer comments you can leave to someone. It is not only telling the coder that his code is not the best. but also leaving him with two challenging questions.

  • What is wrong about this implementation?
  • What are those better ways?

My advice to any reviewer who writes such comment, Try to write a more constructive review. One of the goals of code review is to identify the problems in the code and propose better ideas for implementation. so If you see something that is not at its best. Identify why it is not. and what are the better ways to implements it? always give the requestor something to learn, and go home with.

And If you are requesting a review then received this comment, don’t be defensive about your code. just take a deep breath, and ask the 2 questions above.

Virtualbox hidden gems, VBoxManage

Virtual box has this little secret, the Command Line interface (CLI), this is very useful for users who wants to manage their virtual boxes on headless servers.

Question is why using command line while you have a nice easy to use GUI, the answer is POWER. you can do anything with the virtual box command line, you can create virtual machines, modify their settings, take snapshots, start and shutdown them.

VBoxManage supports everything the graphical user interface does, and much more.

VBoxManage has its share of Virtual Box documentation, you can find it here http://www.virtualbox.org/manual/ch08.html

Installing Java plugin for Firefox on Ubuntu 10.10 (Maverick)

Again after upgrading from Ubuntu 9.10 to 10.4, I had some issues getting Java to work with Firefox, after checking the software center, I found that the Java plugin for firefox is not working properly which broke my vpn connection.

To get this fixed, I simply followed the steps :

1- Backup your repositories files.

sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup

2- Open your repositories file:

sudo gedit /etc/apt/sources.list

3- Add the following line then close the sources.list file

deb http://archive.canonical.com/ubuntu maverick partner

4- Uninstall both icedtea6-plugin and openjdk-6-jre.

sudo apt-get remove icedtea6-plugin openjdk-6-jre

5- Install sun-java6-plugin

sudo apt-get update
sudo apt-get install sun-java6-plugin
sudo update-alternatives --install /usr/lib/mozilla/plugins/mozilla-javaplugin.so mozilla-javaplugin.so /usr/lib/jvm/java-6-sun/jre/lib/i386/libnpjp2.so 1

6- Close your firefox and reopen it again, it should be working smoothly.

Installing Java plugin for Firefox on Ubuntu 10.4 (Lucid)

After upgrading from Ubuntu 9.10 to 10.4, I had some issues getting Java to work with Firefox, after checking the software center, I found that the Java plugin has been removed.

To get this fixed, I simply followed the steps in here http://ubuntuforums.org/showpost.php?p=9008918&postcount=32 and it worked well after that.

Manage your notes / Code snippets with Snippely

It has been a challenge for me to keep tracking code snippets I want to remember, I have always used text files, or sending my self email with the snippet that I feel I am gonna need to look at again in the future, same for notes also.

Today I have found this nice Air App called Snippely, it allow you to manage your notes / code snippets, It is enough for me and better than text files and emails, I believe I am gonna stick to it for now.

Snippely

Only Two options I wish to see in the coming version, to backup / restore the data & to sync the data across many machines, but like I said, I am happy with what I have now.

You can get Snippely from their Google Code page : http://code.google.com/p/snippely/

I hope you will find it as much useful as I did.

Zend Whitepapers – PHP Leads the way

I have been browsing Zend website, and i have been going through its white papers, and it is really interesting, so i thought about sharing it with my blog visitors

Zend White Papers

An overview on PHP (PDF)

This paper gives a clear and concise overview of PHP. The paper covers many aspects of PHP, it goes through the history of PHP, the people behind PHP, the PHP market, the future of PHP, and more.

PHP Leads Web 2.0 (PDF)
This paper defines and applies Web 2.0 for today’s users. Though it is difficult to define Web 2.0 there are usually a few common denominators such as, rich web applications, SOA (service oriented architecture), and social Web elements. This paper also explains what Web 2.0 means for the Enterprise and what it means for technology today.

PHP 4 to PHP 5 Migration (PDF)
A little over three years ago, the PHP community released version 5 of its popular programming language. PHP5 has now become the version of choice for many new PHP projects. PHP4 however remains popular and is still widely used. This whitepaper is aimed at developers who are still using PHP4 and considering switching to PHP5. It is also intended to provide IT managers responsible for PHP applications additional information for their decision on whether to stick with their current version or migrate. This paper seeks to answer the questions, “What are the benefits of migrating to PHP5? And, equally importantly, what are the risks?” The information below will put you in a better position to make an informed decision between PHP4 and PHP5.

A Practical Guide to Data Caching with Zend Server
When asked about the most significant methods to speed up PHP applications, most experts would say “Cache, Cache and Cache”. There are a lot of optimizations that could be done to PHP code that would make it run faster, but the most effective method to speed up PHP execution is to simply execute less, and one of the best ways to achieve that is through caching.

Team Development with Zend Studio for Eclipse
Today’s development organizations are under ever increasing pressure to be more productive. One key factor for productivity is effective coordination across development teams. Learn how Zend Studio for Eclipse can help improve productivity and team coordination with capabilities to allow for effective use of Source Code Management, Code Refactoring, integration between development and deployment environments and more.
PHP is Not Java: Session Management White Paper (PDF)
PHP is used by developers that have a wide range of backgrounds and skill levels. When writing scalable PHP applications, it is crucial to understand the “shared nothing“ architecture of PHP. The most misunderstood component in this architecture is the session handling. PHP sessions are different from the ones used in other languages. Due to the “shared nothing“ architecture of PHP, objects cannot survive a request. Session data is therefore serialized to external storage for each request, meaning that only serializable data can be stored, not resources such as DB handles. This is a very powerful and scalable approach for session data averaging around 10-100kb, as serialized session storage can easily be spread over a cluster and serialization does not introduce a great deal of overhead.

Zend Download Server (PDF)
This paper will explain how the Apache web server works. The paper will answer key question such as: How do large file downloads affect your server’s performance? What is the difference between web page and file download serving? What do you do when your web server gets saturation? Additionally, this paper clearly defines and explains what Zend Download Server does.

Scalability and Responsiveness with Zend Platform’s Job Queue (PDF)
This paper defines Job Queue for users. The paper also details as well as explains how Job Queue is used and how it works.

Integrating PHP with System I using Web Services (PDF)
The following paper examines the ways in which PHP and Zend helps System I developers. The paper demonstrates how quick and simple it is to create a Web Service wrapper for an existing RPG program.

PHP and Oracle (PDF)
The following paper explains how to install and utilize Zend Framework to develop applications in PHP.

Appcelerator :: Building RIA Apps and Use Cases with ease

Yesterday i was browsing Zend Framework Webinars, and i stopped by the title :

Get “Rich” Quick : Building Ajax-based RIAs with the Zend Framework and Appcelerator

i didn’t hear about the Appcelerator thing before, but the title was really interesting as i use Zend framework extensively and it is a great idea to add some interactivity and UI to my apps.

after watching the webinars, i really liked the idea of being able to build the full prototype and have it working with no server side code, and then in the development phase with some slight changes to the prototype, it becomes the working front end interacting with the server side code with no problems @ all.

i went to try.appcelerator.org and gave it a try on the fly, that got me more interested. so i downloaded the sdk installer for MAC and installed it, the installation was at ease, even creating the projects and testing.

Appcelerator supports what we call “technology driven agile development” and we call that process Interactive Use Cases.

Completion of IBM Java Programming Diploma

I have got my certification of completion of Java Programming Diploma last week.

The most usefull thing i got from this course is the stong concept of OOP in java which helped me a lot in PHP especially after working with PHP5 which is diversed to the OOP conecpt. and also using Zend Framework.

i would like to thank the one who encouraged me to take it. ; i am satisfied with it and it was something i must have taken.

i have written some Java tutorials here, i will right more when i am done with the couple of projects i am taking care of now.

The future is PHP

With PHP continuing to gain massive support from programmers looking to expand their programming horizon, new versions of the language are likely to be geared towards solidifying its position as opposed to revolutionising.


The job market is telling nothing but good news

When Perl 6 was being designed, many huge changes were brought that alienated a lot of developers – hopefully PHP will not go the same way.In the meantime, be happy that you have chosen such a popular language that is advancing so quickly – PHP is here to stay, and things are only going to get better.The simplicity of scripting language PHP means it will be more popular than Java for building Web-based applications, Internet browser pioneer Marc Andreessen predicted in a speech in Burlingame, California, at the Zend/PHP Conference.

Action Event Handling in JAVA

Throughout AWT, and all Java APIs for that matter, event handling has been consistent:

  1. Implement a listener interface (such as java.awt.event.ActionListener)
  2. Provide an implementation of all listener interface methods (such as actionPerformed( ActionEvent e ))
  3. Add your class as a listener to a component, which generates events by calling its addXXXListener() method (such as calling javax.swing.JButton‘s addActionListener() method)

i will go through 3 methods to perform Action Event Handling in Java

First method:

1- create a Handler Class in a separate file that implements from ActionListener interface.

2- overwrite the abstract method actionPerformed of the ActionListener interface, with the code we want to be executed upon the action on our object.

3- create instance of the Handler class, and add it as an action listener on the object we want like button for example.

Example :

in the following example we will create a button from the swing library, and apply the action that when a user click the button, we will show a message dialog.

File Handler.class

import java.awt.event.*;import java.awt.*;

import javax.swing.*;/**

*

* @author ahmedabdelaliem

*/

//we create the Handler class and implement from ActionListener interface

public class Handler implements ActionListener{

//now we overwrite the abstract method of ActionListener interface

public void actionPerformed (ActionEvent event){

// here goes the code we want to execute when the action happen, for example show a message dialog with the below text

JOptionPane.showMessageDialog(null, "You Have just clicked the button");

}

}

file ButtonTest.java

/** *

* @author ahmedabdelaliem

*/

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

import java.awt.*;

import javax.swing.*;// we create our class and inherit from JFrame to create our GUI

public class ButtonTest extends JFrame{

// we create an empty reference with JButton type

private JButton plainButton,;

// we create the constructor of the class

public ButtonTest(){

//set up the container

Container con = getContentPane();

con.setLayout(new FlowLayout());

// create an object of the JButton

plainButton = new JButton();

plainButton.setText("Regular Button"); // set the text on the button

//create an object of our Handler

Handler h = new Handler();

// now we add the Handler object we created as Action Listener for the button

plainButton.addActionListener(h);

//now we add the button to the container and show the frame.

con.add(plainButton);

setSize(400,300);

setVisible(true);

}

}

file Main.java

/** *

* @author ahmedabdelaliem

*/

public class Main {

public static void main(String[] args) {

// we create an object of our ButtonTest Class

ButtonTest b1 = new ButtonTest();

b1.setDefaultCloseOperation(b1.EXIT_ON_CLOSE);    }

}

now the output of this simple application : a frame will show with a button on it, and when we click the button a message dialog will appear.

the disadvantage of this method is that we need to create a separate handler for each object we want a handler for,

for example if we have a form with 10 buttons with different actions when clicked, then we need to create 10 Handlers in 10 files with 10 different actions

that doesn’t make sense at all.

so this is why we follow one of the next 2 methods,

Method 2 : create the Handler class as inner class of our class to handle the actions on the components in our class

Example :

File : CheckButtonTest.java

import javax.swing.*;import java.awt.*;

import java.awt.event.*;

/**

*

* @author ahmedabdelaliem

*/

public class CheckBoxTest extends JFrame{

JCheckBox b1,b2; // we create 2 referneces with JCheckBox type

JTextField t1; // we create 1 reference with JTextField type    // we create the constructor of the class

public CheckBoxTest() {

//we call the constructor of our super class and give it the title of the frame

super ("Check Box Test");

//now we setup the container

Container con = getContentPane();

con.setLayout(new FlowLayout());

// we create the text field object with pre entered text

t1 = new JTextField("This is the test text");

// we set the font for the text field

t1.setFont( new Font("serif", Font.PLAIN, 14));

// now we add the text box to the container

con.add(t1);

// we create object for the checkbox and give it Bold value

b1 = new JCheckBox("Bold");

//we add the checkbox to the container

con.add(b1);

// we create another object for the second box and give it italic value

b2 = new JCheckBox("Italic");

// we add the checkbox to the container

con.add(b2);

// now we create the handler object

Handler h = new Handler();

// we add the handler as item listener for the 2 checkboxes

b1.addItemListener(h);

b2.addItemListener(h);

// now we view the frame

setSize(400,300);

setVisible(true);

}

// here we create the handler class   as inner class

private class Handler implements ItemListener{

private int bold = Font.PLAIN; // we setup initial values for the bold, italic variables

private int italic = Font.PLAIN;

// we overwrite the abstract method of the ItemListener interface

public void itemStateChanged(ItemEvent event){

if (event.getSource() == b1){ // here we check which checkbox is checked

if (event.getStateChange() == event.SELECTED){ // here we check if it is selected or the selection is removed

bold = Font.BOLD; // if conditions apply, we set the bold variable as BOLD

//t1.set;

}else{

bold = Font.PLAIN;

}

}else if (event.getSource() == b2){

if (event.getStateChange() == event.SELECTED){

italic = Font.ITALIC;

}else{

italic = Font.PLAIN;

}

}

// here we add the 2 variables and apply the format to our font

t1.setFont(new Font("Serif", bold + italic, 14));

}

}

}

File : Main.java

/**

 *

 * @author ahmedabdelaliem

 */

public class Main {    /**

     * @param args the command line arguments

     */

        // TODO code application logic here

        public static void main(String[] args) {

        // TODO code application logic here

       CheckBoxTest checkBox = new CheckBoxTest();

       checkBox.setDefaultCloseOperation(CheckBoxTest.EXIT_ON_CLOSE);

    }

}

This method is better than the first one, cause it allow us to create one handler for many objects and the handler can reach and compare the variables of our main class.

The third method is to create an instance of