Developing Software in J2EE

Developing Software in J2EE

Environment Consistency – MySQL (Apple Macintosh)

We need to make our life easier by creating consistency for every machine. What this means is every machine you try to use, whether it be yours or your colleagues, the environment will be the same when you sit down to help them with something. This is very important if you’re a Development Manager and you’re trying to help one of your staff.

This is one of several posts in regards to this subject.

I really don’t use MySQL too much.  At work, I have Oracle and Sybase.  But I think I’m going to use it in the example code that I post later.  Because we’ll be using Hibernate, for the most part, it really doesn’t matter.  Hibernate hides the nuances of each type of database from us (for the most part).  Anyway, the real reason has to do with our earlier PHP post and a post about WordPress that I’ll be making later.  ]

Here we’re going to install MySQL for the Macintosh.  We’ll go back and do it for Windows in a later post.

First off, we need the binary.  We’ll go to the MySQL website to download the 5.1 MySQL version file called “mysql-5.1.41-osx10.5-x86.dmg”.  After we’ve gotten that downloaded double click it and we’ll see this window after the image has been mounted:

Mounted MySQL Package

Mounted MySQL Package

Now click on the icon for “mysql-5.1.41-osx10.5-x86.pkg” and you should see this window:

MySQL 5.1.41 Installer Welcome Screen

MySQL 5.1.41 Installer Welcome Screen

click “Continue”

MySQL 5.1.41 Installer Important Information

MySQL 5.1.41 Installer Important Information

click “Continue”

MySQL 5.1.41 Installer Software License Agreement

MySQL 5.1.41 Installer Software License Agreement

click “Continue” and a window will drop down

MySQL 5.1.41 Installer Agree to Software

MySQL 5.1.41 Installer Agree to Software

click “Agree”

MySQL 5.1.41 Installer Select A Destination

MySQL 5.1.41 Installer Select A Destination

click “Continue”

MySQL 5.1.41 Installer Confirm Install Location

MySQL 5.1.41 Installer Confirm Install Location

click “Install”

MySQL 5.1.41 Installer Enter Password

MySQL 5.1.41 Installer Enter Password

you’ll need to type in your password so that you’ll have permission to install the software and after you’ve done that, the software should start to install

MySQL 5.1.41 Installer Installing

MySQL 5.1.41 Installer Installing

and when it’s done you’ll see this window:

MySQL 5.1.41 Installer Successful Install

MySQL 5.1.41 Installer Successful Install

Now you have the MySQL database installed.  But you don’t want to have to start it by hand every time your system boots.  You’d rather it start automatically for you.  We have an app for that. :)   Go back to this window

Mounted MySQL Package

Mounted MySQL Package

and this time click on “MySqlStartupItem.pkg” and you’ll see this window

MySQL 5.1.41 Startup Item Installer Welcome Screen

MySQL 5.1.41 Startup Item Installer Welcome Screen

click Continue

MySQL 5.1.41 Startup Item Installer Welcome Screen

MySQL 5.1.41 Startup Item Installer Welcome Screen

click Continue

MySQL 5.1.41 Startup Item Installer Select Destination

MySQL 5.1.41 Startup Item Installer Select Destination

click Continue

MySQL 5.1.41 Startup Item Installer Confirm Destination

MySQL 5.1.41 Startup Item Installer Confirm Destination

click Install

MySQL 5.1.41 Startup Item Installer Successful Install

MySQL 5.1.41 Startup Item Installer Successful Install

and you’re done.  Click Close.

We could go through the process of starting MySQL by hand but it’s just as easy to reboot our machine.  That way we’ll know it starts automatically for us as well.  So, reboot!

After you’ve rebooted, let’s start up a “Terminal” window.  Once that’s done, type in the following command:

ps -aef | grep mysql

and you should see results that look similar to this:

MySQL 5.1.41 Confirm Install

MySQL 5.1.41 Confirm Install

MySQL doesn’t have a Graphical User Interface so a lot of what we’ll be doing is via the command line.  Because of this, we need to put the /usr/local/mysql/bin in our PATH environment to make life easier for us.  We do this by cd-ing to the /etc directory and then editing the profile file using vi (via sudo).  We’ll be executing these commands:

cd /etc
sudo vi profile

and our window should look like this:

MySQL 5.1.41 Edit /etc/profile

MySQL 5.1.41 Edit /etc/profile

In this file, you should see a line that says PATH.  You want to put the line “/usr/local/mysql/bin:” right after the = sign of that line.  You should see a window that looks like this:

MySQL 5.1.41 Change PATH

MySQL 5.1.41 Change PATH

Save it and end your Terminal session and start a new one so that this will take effect for you.

Now that the MySQL server is started, we need to get in and do some things for security reasons.  The server starts with no password for the “root” user and it also has an anonymous account.  We need to fix those issues.  So we start up a “Terminal” window again and we’ll be executing the following command to get us into the MySQL interface:

mysql -u root

and the first thing we’re going to do is delete the anonymous users by running this command:

DELETE FROM mysql.user WHERE user=”;

your window will look like this:

MySQL 5.1.41 Delete Anonymous Users

MySQL 5.1.41 Delete Anonymous Users

And finally we need to get in and change the password for our root user.  Start up a “Terminal” window and get in MySQL by this command:

mysql -u root

then run the following command

UPDATE mysql.user SET password = PASSWORD=(‘xxxxxx’);

Be aware here though.  First off, you’re changing ALL the passwords of ALL the users in your database.  This is a fresh install so that’s OK but if for some reason you’re following this and it’s not a fresh install then be aware.  Secondly, I’m changing the password to ‘xxxxxx’, you’ll want to change it to something real.

How will we know that our password has been changed?  We now have to add the “-p” argument when we try to get into MySQL from now on else we can’t get in.  So we’ll be executing a command like this

mysql -u root -p

and the server will now ask you for the password when you try to get into your server.

The window for this will look like the following for these commands:

MySQL 5.1.41 Change root Password

MySQL 5.1.41 Change root Password

We now have MySQL installed on our Apple Macintosh.

November 22, 2009 Posted by | Development Environment, MySQL | , , | Leave a Comment

Environment Consistency – PHP (Apple Macintosh)

We need to make our life easier by creating consistency for every machine. What this means is every machine you try to use, whether it be yours or your colleagues, the environment will be the same when you sit down to help them with something. This is very important if you’re a Development Manager and you’re trying to help one of your staff.

This is one of several posts in regards to this subject.

Where I work, I’m asked to work on other technologies all the time.  My main focus is to work in JAVA/J2EE but sometimes I have to work in other programming languages.  PHP is one of those languages.

Today we’re going to set up our Apple Macintosh for working and debugging PHP in NetBeans.  I’ll show you how to set up PHP for Windows use in a later post.

In this file I’m going to refer to “<username>”.  This will be your username on your laptop similar to how mine is “thcampbell”

If you didn’t know it already, your Apple Macintosh already has an Apache HTTP web server on it.  You can get to it by going to “System Preferences” and then clicking on “Sharing”.  From there you’ll see something that says “Web Sharing” and it is probably off.

Web Sharing on Apple

Web Sharing on Apple

Click on the check box next to the Web Sharing line to turn it on:

Web Sharing on Apple

Web Sharing on Apple

After you’ve done that, click on the link that’s presented on this page or go to “http://localhost/~<username>” in your favorite browser.  You should see a screen like this:

Apache HTTP Web Server on Apple

Apache HTTP Web Server on Apple

Ok, we have the server running and believe it or not, PHP is also on your Apple Macintosh already.  We just have to enable it.  So start up a “Terminal” session and cd to the “/etc/apache2″ directory.  Then execute (via sudo) the vi command to edit the file “httpd.conf”:

Edit httpd.conf using sudo/vi

Edit httpd.conf using sudo/vi

After you’re editing the file, find the line that looks like the following:

#LoadModule php5_module libexec_apache2/libphp5.so

and uncomment it (remove the # sign).  When you’re done the file will look like the following:

Editing LoadModule line

Editing LoadModule line

Save it!

Now we need to restart our Apache HTTP web server.  Easy enough, go back to “System Preferences” and then “Sharing”, from there uncheck the check box for “Web Sharing” and then check it again.  This will stop and restart your server.

But how do we know PHP is running properly?

Go to your “Terminal” session and “cd ${HOME}/Sites”.  This will be the “Sites” directory under your username on the system.  In this directory create a file (using vi) called “phpinfo.php”.  In this file put the following line

<?php phpinfo(); ?>

Your file will look like this:

phpinfo.php

phpinfo.php

Save it.

Now go to your browser at “http://localhost/~<username>/phpinfo.php” and you should see a screen that looks like the following:

http://localhost/~<username>/phpinfo.php

http://localhost/~/phpinfo.php

The next thing we need to do is set up PHP so that we can debug our software easily.  We do this by using Xdebug.  Get the Xdebug source files from http://www.xdebug.org/download.php.  You want to get the latest source and download it to your machine.  After you’ve done that, go to a “Terminal” session and go to the directory where you downloaded the files.  (More than likely ${HOME}/Downloads)  Now run gunzip and tar on the file like so:

gunzip and tar xvf xdebug

gunzip and tar xvf xdebug

After you’ve done that, you need to cd into the directory where the sources are kept.  In the case of the file I downloaded they are in “xdebug-2.0.5″ and from here I’m going to run “sudo phpize” and “./configure –enable-Xdebug” like so:

phpize and configure of xdebug

phpize and configure of xdebug

and finally I’m going to run make:

make xdebug

make xdebug

Now we should have a file in the modules subdirectory called xdebug.so:

xdebug.so

xdebug.so

We need to copy this file to our extensions directory.  Where is that at?  We go back to our phpinfo program that we ran in the browser at “http://localhost/~<username>/phpinfo.php”.  The information displayed in the browser should have a section labeled Core which should have an entry for “extension_dir”.

Core and extension_dir in phpinfo.php

Core and extension_dir in phpinfo.php

We want to put the “xdebug.so” file in this directory.  We’ll do this using “sudo” like so:

Move xdebug.so into extension_dir

Move xdebug.so into extension_dir

We now need to edit our “php.ini” file.  But where is it?  Well PHP doesn’t have to have a php.ini file to run and on our Apple Macintosh there’s not one.  If you again, look at the phpinfo program that we ran in the browser at “http://localhost/~username/phpinfo.php” you’ll notice a line that says “Configuration File (php.ini) Path”.  Mine says “/etc” and if you look in “/etc” for “php.ini” the file is not there.  But there is a file called “php.ini.default”.  Just copy it (using “sudo”) and then we’ll edit it (using “sudo vi”) like so:

cp php.ini.default to php.ini

cp php.ini.default to php.ini

At the bottom of this file we want to put the following lines:

; Xdebug config for Mac OS X and NetBeans IDE
zend_extension=/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
; xdebug.remote_mode=req
xdebug.remote_host=localhost
xdebug.remote_port=9000
; xdebug.idekey=

and our file will look like this (the lines we added are highlighted):

php.ini file edited with xdebug information

php.ini file edited with xdebug information

Now, we need to stop and restart our server again.  Again, this is done by going to “System Preferences”, “Sharing” and unchecking and checking the box next to “Web Sharing”.

Now, how do we know if our debug mode is ready?  Again, we go to “http://localhost/~<username>/phpinfo.php” and in this file there should be a section title “XDebug” like so:

XDebug in phpinfo.php

XDebug in phpinfo.php

Ok, now we’ve got PHP running, in debug mode, but we want to be able to create and debug programs in NetBeans.  This is pretty simple to do as well, let’s do a test.

Click on File and New Project and the following dialog box should come up

New PHP Project

New PHP Project

Click Next and you may see the following dialog box as well:

Activating PHP

Activating PHP

I had some problems with this on the Apple Macintosh (it just stayed on this screen).  Give it a few minutes and then hit the “< Back” button.  Everything seemed to be OK for me when I did this.

So, now (if we had to, we clicked on File and New Project again) we should see the following dialog box where we will enter “TestPHPProject” in as the “Project Name” and “/NetBeansProjects/TestPHPProject” as the “Sources Folder:”

TestPHPProject

TestPHPProject

Now, click “Next >” and you’ll see this dialog box:

PHP Project Configuration

PHP Project Configuration

Change the Project URL to be “http://localhost/~<username>/TestPHPProject”

Click on the “Copy files from Sources Folder to another location” button and then click the “Browse” button.  You want to browse to “/Users/<username>/Sites”.  The resulting dialog box will look like the following:

TestPHPProject Edited Configuration

TestPHPProject Edited Configuration

After that’s done, NetBeans will open up the “index.php” file for your Project.  Put the following line

phpinfo();

in this file so that it looks like this:

index.php in NetBeans with phpinfo();

index.php in NetBeans with phpinfo();

Now, you’re ready to go.  In NetBeans, click on “Debug” and then “Debug Project (TestPHPProject)” and the program should start and it will stop at the first line like so:

NetBeans index.php file being debugged

NetBeans index.php file being debugged

You can now step through this file or any other PHP file you create.

I’d like to credit the following articles that I used for research in regards to creating this post:

Configuring PHP, Apache, MySQL, and Xdebug for PHP development in MAC OS X

Debugging PHP Source Code in the NetBeans IDE for PHP Editor

Using Apache and PHP on Mac OS X

Working with PHP 5 in Mac OS X 10.5 (Leopard)

Where is php.ini under MAC OSX

November 18, 2009 Posted by | Development Environment, PHP | , , , | Leave a Comment

Environment Consistency – Sync Directories

We need to make our life easier by creating consistency for every machine. What this means is every machine you try to use, whether it be yours or your colleagues, the environment will be the same when you sit down to help them with something. This is very important if you’re a Development Manager and you’re trying to help one of your staff.

This is one of several posts in regards to this subject.

OK, this week we’re going to sync up our directories.  There are two directories that we care about (actually 3 if you count the “NetBeansProjects” directory separately).

On our Windows machine, we’re already done.  We mapped in a previous post the “\\snake\developers$” directory to our Q: drive.   This is where our third party libraries will be stored on Windows.  We also have the “My Documents” directory that resides on our Windows file server and under that directory is the “NetBeansProjects” directory.  So we’re set on the Windows side of things.

On the Apple Macintosh though, it’s not that easy.  We need to use a utility called rsync to sync up our Libraries directory from the file server.  The way that we do that is to first set up a rsync directory for our rsync scripts.  We do this by starting up a terminal window which will start in the ${HOME} directory and execute the command “mkdir rsync” like so:

mkdir rsync

mkdir rsync

Now I’m going to create a script in that directory so I cd into it and using vi create a file called “syncLibrary.sh” like so:

vi syncLibrary.sh

vi syncLibrary.sh

We’re going to do a couple of things here.  The first thing we’re going to do is mount our Windows file server “snake”.  That is done using the mount command like so:

mount -t smbfs //thcampbell:<mypassword>@snake.mydomain.com:/Departments /nfs/Departments

This command will mount, using the smb protocol, the “Departments” directory on the Windows file server “snake.mydomain.com” using my username “thcampbell” and my password “<mypassword>”.  It will mount that at the “/nfs/Departments” mount point on my Apple Macintosh.  The “Departments” directory on snake.mydomain.com is a file shared created by my Windows Administrator.

Ok, after that’s done, I want to do my rsync.  Here’s my rsync command:

rsync -auv –delete /nfs/Departments/IT/Developers/ /NetBeansLibraries

This executes the rsync command with the options “-auv” and “-delete” while will sync the files from the directory “/nfs/Departments/IT/Developers” to the “/NetBeansLibraries” directory that we created in a previous post.

I’m not going to go into options “-auv” and “-delete” in detail but basically what’s going on is that the directory “/NetBeansLibraries” will be an exact duplicate of the “/nfs/Departments/IT/Developers” directory.  It will delete files and make changes to the “/NetBeansLibraries” directory each time you run it; so don’t make changes to the “/NetBeansLibrary” directory.  If a change needs to be made, make it in the “/nfs/Departments/IT/Developers” directory and then run your script “syncLibrary.sh”.

So after the rsync is done we have to umount our mount point by doing the following:

umount /nfs/Departments

Ok, so here’s what we have in our file:

syncLibrary.sh

syncLibrary.sh

Save it!  And set the permissions on it to be an executable using

chmod 755 syncLibrary.sh

and run it

./syncLibrary.sh

Execute syncLibrary.sh

Execute syncLibrary.sh

Depending on how many changes you have to your Libraries directory this could take a while to run.  I store other things in the “/NetBeansLibraries” directory such as Software that I need (like tomcat) and some Documentation, and you can do this too if you’d like.  My third party libraries are in a sub directory called Libraries and you’ll see me reference it when I talk about managing Libraries in a later post.

But we’re not done yet.  We have another couple of directories we want to sync.

We have to rsync the “${HOME}/NetBeansProjects” directory.  This directory is where we store our projects and we’ll want to rsync it to the Windows file server so that a) we have access to it from our Windows machine and b) even though we’ll be using Subversion, it never hurts to have a backup of our projects.

We do pretty much the same thing as we did above.  We are going to edit a file using vi called “syncNetBeans.sh”.

In this file, we first mount “snake” but this time we’re mounting the “Home” directory instead of the “Departments” directory.  We’ll be mounting this directory at “/nfs/Home” on our Apple Macintosh.  Again, the “Home” directory on snake, is a share that our Windows Administrator set up for us:

mount -t smbfs //thcampbell:<mypassword>@snake.mydomain.com:/Home /nfs/Home

Next we’re going to rsync our “${HOME}/NetBeansProjects” directory to “/nfs/Home/My Documents/”.  This directory “/nfs/Home/My Documents” will correspond to the “My Documents” directory on our Windows machine so that when we use that machine, we’ll have access to our files without any effort.

rsync -auv –delete /Users/thcampbell/NetBeansProjects /nfs/home/thcampbell/My\ Documents

And just like before, we’re going to umount snake:

umount /nfs/Home

This is our finished file.

syncNetBeans.sh

syncNetBeans.sh

It’s important to note that we’re syncing to snake in this script instead of from snake like we did in the previous script.

Just like before, save it, chmod it and execute it.

Execute syncNetBeans.sh

Execute syncNetBeans.sh

Finally I have one more directory that I want to sync.  As I’m using my Apple Macintosh as my master computer, I want to edit my files in my “${HOME}/Documents” directory but have them show up in my “My Documents” directory on my Windows machine.  So, we edit a file called “syncHome.sh” using vi.

Just like in the previous script, we mount “/Home” on snake to our “/nfs/Home”:

mount -t smbfs //thcampbell:<mypassword>@snake.mydomain.com:/Home /nfs/Home

Our rsync command is a little different this time though:

rsync -auv –delete –exclude=NetBeansProjects /Users/thcampbell/Documents/ /nfs/home/thcampbell

This time you’ll notice a –exclude=NetBeansProjects.  What this tells rsync is that it should not do anything with the “NetBeansProjects” directory.  We manage this directory using the script up above so we don’t want to mess with it here.

Then, just like before we umount snake:

umount /nfs/Home

Here’s what our finished script looks like

syncHome.sh

syncHome.sh

Save it, chmod it and run it!

Execute syncHome.sh

Execute syncHome.sh

Now that you have your scripts created, you can run them by hand or you could run them using a cron job.  It’s up to you but I’ll leave figuring out how to set up cron to you.

November 9, 2009 Posted by | Development Environment | , , , , | Leave a Comment

Environment Consistency – Directories (Windows)

We need to make our life easier by creating consistency for every machine. What this means is every machine you try to use, whether it be yours or your colleagues, the environment will be the same when you sit down to help them with something. This is very important if you’re a Development Manager and you’re trying to help one of your staff.

This is one of several posts in regards to this subject.

As I mentioned, I support both a Windows environment and an Apple Mac environment and we have a different way of handling our “directories” for each environment.  We’re going to talk about Windows here.

There are two “directories” that are important when developing our software.  There is the “NetBeansProjects” directory and the “NetBeansLibraries” directory.

When NetBeans installs for Windows, it creates a directory called “NetBeansProjects” in your “My Documents” directory.  Now to keep things consistent we need to do something extra on each machine.

Windows has an old DOS command called SUBST which “Associates a path with a drive letter.”  Using this command we can fix it so that where ever our NetBeansProjects directory is created we can consistently associate it with the driver letter X:.  This is done by executing the following command at the command line:

SUBST X: “H:\My Documents\NetBeansProjects”

TheSUBST Command

The SUBST Command

You’ll notice that my “My Documents” directory is located on the H: drive.  Fortunately for me, this directory is located on the main file server at my work place and is synchronized each time I log on to the network.  This means when I disconnect from the network, I can still work on my projects and then when I connect, everything will be backed up to the file server (which then goes to tape).  I don’t want to go into further detail about this here, talk to your System Administrator about setting this up for you.

If you can’t get them to create this for you, then you’ll be putting your files (probably) on your C drive somewhere.  I’ll show you in a later post how to set up Subversion to manage your source code but even then, you could have a hard drive failure and lose a couple of days of work.  Having your files synced to the file server is the best way to go.

Now, everyone that uses Windows, when they are using NetBeans, should create projects in X:\NetBeansProjects, NOT in H:\My Documents\NetBeansProjects.  Sounds stupid but it helps, trust me.

Now typing in the SUBST command each time you reboot your machine isn’t exactly my idea of fun.  So create a batch file for it (put that line in a file and save the file at c:\substx.bat) and then create a “Windows Shortcut” to c:\substx.bat in your Startup folder.  That way it’ll run each time you reboot.

The NetBeansLibraries directory is going to be associated with our Q: drive.  Our Windows Administrator helped us out again here by creating a special share for us called developers$.  Our file server is called “snake” so our special share is located at \\snake\developers$.  We map this drive using Windows Explorer.  So start Windows Explorer and click on Tools and then click on Map Network Drive.  A Window will come up and the information in it should look similar to the following:

Windows - Map Network Q Drive

Windows - Map Network Q Drive

After you’ve done this, you need to make sure it’s going to synchronize and be available offline.  Again in Windows Explorer, right click on the Q drive and there should be an option for “Make Available Offline”.  Make sure it’s checked like so:

Windows - Make Available Offline

Windows - Make Available Offline

If you don’t see this option go talk to your Windows Network Administrator so, he can “hook you up”.  We’ll talk about managing the Libraries in a later posts.

November 1, 2009 Posted by | Development Environment | , , | Leave a Comment

Environment Consistency – Directories (Apple Mac)

We need to make our life easier by creating consistency for every machine. What this means is every machine you try to use, whether it be yours or your colleagues, the environment will be the same when you sit down to help them with something. This is very important if you’re a Development Manager and you’re trying to help one of your staff.

This is one of several posts in regards to this subject.

As I mentioned, I support both a Windows environment and an Apple Mac environment and we have a different way of handling our “directories” for each environment.  We’re going to talk about the Apple Mac here.

There are two “directories” that are important when developing our software.  There is the “NetBeansProjects” directory and the “NetBeansLibraries” directory.

Our first directory is where NetBeans stores our projects.  When NetBeans is installed it created a directory called “NetBeansProjects” in the “${HOME}/Documents” directory on the Apple Mac.  Now, to keep things consistent, we’re going to do something extra on each Apple Mac.

For the Apple Mac, we need to create a symbolic link.  Start up a “Terminal” session and execute the following command:

ln -sf ${HOME}/Documents/NetBeansProjects /NetBeansProjects

Terminal - Using ln -sf command

Terminal - Using ln -sf command

Now, everyone that uses an Apple Mac, when they are using NetBeans, should create projects in /NetBeansProjects, NOT in ${HOME}/Documents/NetBeansProjects.  Sounds stupid but it helps, trust me.

The NetBeansLibraries directory is just a directory that we’re going to create in /.  We’ll populate it with data in a later post.

mkdir /NetBeansLibraries

Creating /NetBeansLibraries

Creating /NetBeansLibraries

This directory will be populated with third party libraries and other files that we can use while developing our software.  Again, we’ll do this in a later post.

November 1, 2009 Posted by | Development Environment | , , , | Leave a Comment

   

Follow

Get every new post delivered to your Inbox.