Joe Lennon

Rants, Raves & Recommendations

Archive for March, 2010

Oracle XE and mod_plsql

with 6 comments

In this post you will learn how to get a sandboxed PL/SQL web development environment up and running on a Windows machine. You will use the VMware Player virtualization system to run an Ubuntu 9.10 server image, on which you will install Oracle XE. You will then configure a DAD using DBMS_EPG which basically acts in the same way as mod_plsql does in Oracle HTTP Server. This will allow you to create Web applications using PL/SQL. Finally, you will learn how to work with virtual directories so you can store things like images, external stylesheets and external JavaScript files.

The first step you need to follow is to download and install the free VMware Player application on your system. This can be downloaded from https://www.vmware.com/tryvmware/?p=player&lp=1. Once this has downloaded, follow the simple instructions to install VMware Player. This application allows you to run other operating systems that are packaged in virtual machine images. Head over to http://www.thoughtpolice.co.uk/vmware/#ubuntu9.10 and download the appropriate image for Ubuntu 9.10 Karmic Koala. When this has finished downloading, extract the files to a convenient location on your file system, and find the file ubuntu-server-9.10-i386.vmx, and double-click it to start the virtual machine.

At this point, VMware Player should open and your Ubuntu VM will launch and boot up. After a few moments, you should see a prompt like the one shown in Figure 1:

Figure 1 - Ubuntu Login Prompt

Figure 1 - Ubuntu Login Prompt

To login, enter the login notroot and the password thoughtpolice. You will now be logged in and should see the Ubuntu shell prompt.

By default, the VM image you are using has 905208 bytes of swap space allocated. Unfortunately, this will cause the Oracle XE installer to fail, as its minimum is 1024MB. To prevent this from happening, you first need to assign some more swap space. Use the following commands to bump up the swap space to 1058800 bytes. If you are asked for your [sudo] password for notroot (you will be), enter thoughtpolice.

$ sudo dd if=/dev/zero of=/tmp/swap bs=1M count=150
$ sudo mkswap /tmp/swap
$ sudo swapon /tmp/swap
$ free

If the commands worked correctly, the output from the free command should look similar to the results shown in Figure 2 below:

Figure 2 - Configuring swap space

Figure 2 - Configuring swap space

With the swap space issue resolved, you can now move on to the steps required to download, install and configure Oracle XE. First, you’ll need to add the Oracle repository to your apt sources list. Issue the following command to open the list in the nano text editor:

$ sudo nano /etc/apt/sources.list

Navigate to the end of this file (use Ctrl+V to scroll down page by page) and at the bottom add the following line:

deb http://oss.oracle.com/debian unstable main non-free

Save the file by pressing Ctrl+O, and then exit using Ctrl+X.

Now you can go ahead and download Oracle XE using the following commands:

$ wget http://oss.oracle.com/el4/RPM-GPG-KEY-oracle -O- | sudo apt-key add -
$ sudo apt-get update
$ sudo apt-get install oracle-xe

This process may take some time (XE is a 221MB package) and you may need to answer Y when asked if you wish to continue.

It’s worth pointing out that when I ran the above commands, I found that the download would sometimes freeze at a certain point. If this happens to you, press Ctrl+C to force exit the process and then re-issue the command to pick up from where you left off (you can press the up arrow key to quickly bring up the last command you entered).

When the process has finished, Oracle XE is now installed. Before you can use it, however, you will need to configure it. Issue the following command to start the Oracle Database configuration tool:

$ sudo /etc/init.d/oracle-xe configure

When asked for a port number for Application Express, accept the default option of 8080 by pressing Enter. Do the same when offered port 1521 for the database listener. Next, enter a system password, and confirm it when requested. Finally, accept the default setting of y when asked if you want XE to be started on boot. Oracle will now configure the Net Listener and the Database – this may take a few minutes. When it is done you should see a message like the one shown in Figure 3.

Figure 3 - Oracle install complete

Figure 3 - Oracle install complete

The next step you need to follow is to set up the ORACLE_HOME and PATH variables on your system, which will make it easier to run the SQL*Plus client. Create your user .bash_profile file by issuing the following commands:

$ cd ~
$ nano .bash_profile

In the nano editor, insert the following two lines:

export ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server
export PATH=${PATH}:${ORACLE_HOME}/bin

Press Ctrl+O to save and then Ctrl+X to quit nano. Now, log out of Ubuntu using the following command:

$ logout

You’ll be returned to the login prompt. Login once again using the username notroot and password thoughtpolice. Now, issue the following command to start the SQL*Plus client as the SYSTEM user:

$ sqlplus system@xe

Enter the password you chose when configuring Oracle when requested at the prompt. You should then see a message informing you that you are connected to Oracle. Now that you are logged in to the database, it is a good time to allow remote access to the Oracle XE web server, which runs Apex and which we will be using to run our PL/SQL web applications. To do this, issue the following command:

SQL> exec dbms_xdb.setlistenerlocalaccess(false);

You should see the message “The PL/SQL procedure successfully completed.” before being returned to the SQL prompt. Next, exit SQL*Plus by entering the following command:

SQL> exit

You will now be back at the Ubuntu shell prompt. The next thing you’ll need to do  is determine the IP address of your virtual machine. To do this, use the command:

$ ifconfig

You should see output similar to the one shown below. You need to look for the entry for eth0, specifically for the value next to inet addr: – in my case it is 192.168.133.131, as shown in the screenshot in Figure 4.

Figure 4 - VM IP address

Figure 4 - VM IP address

Now, back in Windows, fire up your favourite Web browser and enter the following address into the Address box, replacing my IP address with the one for your virtual machine:

http://192.168.133.131:8080/apex

You should see a screen like the one shown in Figure 5.

Figure 5 - Oracle Application Express Home Page

Figure 5 - Oracle Application Express Home Page

This interface is actually very useful, so let’s login and create a new database user, which you will later use as the schema for your PL/SQL web application. Enter the username SYSTEM and the password you entered during the Oracle configuration. From the next screen, click the arrow next to the Administration icon, and from the Database Users menu, select “Create User”. This will bring you to a form where you can enter the details for the new database user. Enter the username embosa and whatever password you like (for the sake of simplicity it would be wise to use the same password as you chose for the SYSTEM user – of course you would never do this on a production system!). Make sure that the roles CONNECT, RESOURCE and DBA are all checked, and press the “Create” button at the top right hand side of the form to create the user.

Next, hop back to your Ubuntu VM and enter the following command to login to Oracle using your new user:

$ sqlplus embosa@xe

Enter the password when asked, and hey presto, you’re logged in as the user embosa! Now, let’s go about creating a DAD so that we can publish HTML using PL/SQL. The following block of code will create, authorize and configure a DAD named embosa which will be accessible at the URL http://192.168.133.131:8080/embosa (again, replace the IP address with your own VM’s IP).

begin
dbms_epg.create_dad(dad_name=>'embosa', path=>'/embosa/*');
dbms_epg.authorize_dad(dad_name=>'embosa', user=>'EMBOSA');
dbms_epg.set_dad_attribute(dad_name=>'embosa', attr_name=>'default-page', attr_value=>'home');
dbms_epg.set_dad_attribute(dad_name=>'embosa', attr_name=>'database-username', attr_value=>'EMBOSA');
end;
/

This will create the DAD, next you need to create the “default page” by creating a procedure named home. To do so, use the following:

create or replace procedure home as
begin
htp.p('Hello, world!');
end home;
/

Now, hop back over to Windows and in your Web browser, navigate to the URL http://192.168.133.131:8080/embosa, replacing the IP address accordingly. You should see something like that shown in Figure 6.

Figure 6 - A basic PL/SQL Web Application

Figure 6 - A basic PL/SQL Web Application

The final piece of configuration to do is to configure a virtual directory where you can store images, external styles and scripts and so on. Back in your Ubuntu VM, you should still be logged into SQL*Plus. In here, run the following command to open port 2100 for FTP connections:

SQL> exec dbms_xdb.setftpport('2100');

Now, enter exit to leave SQL*Plus, and create a test text file by issuing the following command:

$ echo testing! > test.txt

Next enter the command ftp to open the FTP client. At the ftp prompt, issue the following commands (you will be prompted for your username and password, these are your database username and password, so if you followed the instructions above your username will be embosa):

ftp> open localhost 2100
ftp> ls
ftp> mkdir images
ftp> cd images
ftp> send test.txt

Now, go back to your web browser, and this time enter the URL http://192.168.133.131:8080/images/test.txt (again, change the IP as required) -  you should see the message “testing!”. That’s it, your virtual directory is now created and you can use your favourite FTP client to upload images and other files to this directory. To connect to this folder from Windows, just enter the Ubuntu VM’s IP as the hostname, be sure to enter 2100 as the port number, and use the database username and password to login.

You should now have a usable alternative to mod_plsql up and running, which will allow you to create PL/SQL web applications on Oracle XE alone. I hope to post some more tutorials in the near future that will capitalise on this setup, showing you how to create useful PL/SQL Web applications. If you have any questions, comments or suggestions, feel free to leave a comment on this post.

Written by Joe Lennon

March 9th, 2010 at 6:50 pm

Tutorial: HTML5 & CSS3

with 4 comments

Since the World Wide Web emerged in the early 1990s, HTML has evolved to become a relatively powerful markup language, which, when backed up by its close partners JavaScript and CSS, can be used to create visually stunning and interactive Web sites and applications. This tutorial serves as a hands-on introduction to HTML5 and CSS3. It provides information about the functionality and syntax for many of the new elements and APIs that HTML5 has to offer, as well as the new selectors, effects, and features that CSS3 brings to the table. Finally, it will show you how to develop a sample Web page that harnesses many of these new features. By the time you have finished this tutorial, you will be ready to build Web sites or applications of your own that are powered by HTML5 and CSS3.

Over the past ten years or so, concepts such as Web 2.0, Rich Internet Applications (RIAs), and the Semantic Web have all pushed HTML, CSS, and JavaScript to and beyond their limits, often relying on plug-ins such as Adobe® Flash to power components such as video and audio, as well as highly graphical and interactive applications. The Adobe Flex development framework, Microsoft®’s Silverlight platform, and JavaFX have all looked to provide support where HTML’s weaknesses made developers’ lives difficult. With HTML5, however, the markup language is striking back, with full multimedia support, local storage and offline application support, a native 2D drawing API, and a host of new application development APIs, all provided with the intent of proving that HTML, CSS, and JavaScript can provide a rich front end to your Web sites and applications.

Read the tutorial at http://www.ibm.com/developerworks/web/tutorials/wa-html5/

Written by Joe Lennon

March 4th, 2010 at 4:50 pm