Data Base

Data Base systems are intended to organize, store, and retrieve large amounts of data easily. Whether it is a collections of files, has as SQL language interface, key-value pairs, clustered, or a single file, it should make our lives easier.

Coupled with our Web Server from my last article, we will have the foundation of dynamic shared information over the network of interconnected hosts; our be-loved Web.

Keeping with the theme of portability and Java, JavaDB is a good choice. We want a SQL interface and JDBC because they are well known and the DB engine can be switched out later if desired without core code changes. Produced by the Apache foundation under the name Derby, the latest release can be found here:

Apache Derby, an Apache DB subproject, is an open source relational database implemented entirely in Java and available under the Apache License, Version 2.0. Some key advantages include:

* Derby has a small footprint — about 2.6 megabytes for the base engine and embedded JDBC driver.
* Derby is based on the Java, JDBC, and SQL standards.
* Derby provides an embedded JDBC driver that lets you embed Derby in any Java-based solution.
* Derby also supports the more familiar client/server mode with the Derby Network Client JDBC driver and Derby Network Server.
* Derby is easy to install, deploy, and use.

Download it here: Derby Download

With our database comes the responsibility of a rational file system structure for our code base deployment. I chose the name trutree as my project name, so let us start simple:

Create a workspace:

$ mkdir -p ~/Documents/workspace/trutree
$ cd ~/Documents/workspace/trutree

Copy the derby library:

$ mkdir lib
$ cd lib
$ tar xzvf ~/Downloads/db-derby-10.7.1.1-bin.tar.gz
$ ls 
KEYS                    bin                     javadoc
LICENSE                 demo                    lib
NOTICE                  docs                    test
RELEASE-NOTES.html      index.html

Create an environment setting file (notice the difference between Linux and Mac OS X ‘Darwin’):

$ cd ~/Documents/workspace/trutree
$ vi derby.env
~
if [[ $(uname) == "Linux" ]]; then
  export DERBY_HOME=/home/don/workspace/trutree/lib/db-derby-10.7.1.1-bin
  export TRUTREE_HOME=/home/don/workspace/trutree
elif [[ $(uname) == "Darwin" ]]; then
  export DERBY_HOME=/Users/don/Documents/workspace/trutree/lib/db-derby-10.7.1.1-bin
  export TRUTREE_HOME=/Users/don/Documents/workspace/trutree
fi
export PATH=$PATH:$DERBY_HOME/bin
export CLASSPATH=$DERBY_HOME/lib/derby.jar:$DERBY_HOME/lib/derbyclient.jar:$TRUTREE_HOME/bin:.
~

Now create a Java environment file (I am using Java 1.6):

$ vi java.env
~
if [[ $(uname) == "Linux" ]]; then
  export JAVA_HOME=/usr
elif [[ $(uname) == "Darwin" ]]; then
  export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home
fi
~

Finally create a script to start and stop the Derby database server (we will run it in client/server mode to enable multi-user/program access, through port 1527):

$ vi derby.sh
~
. derby.env
. java.env
export PATH=$PATH:$DERBY_HOME/bin
export CLASSPATH=$DERBY_HOME/lib/derby.jar:$DERBY_HOME/lib/derbyclient.jar:.

if [[ $1 = 'start' ]]
then
  $JAVA_HOME/bin/java -jar $DERBY_HOME/lib/derbyrun.jar server start
fi
if [[ $1 = 'stop' ]]
then
  $JAVA_HOME/bin/java -jar $DERBY_HOME/lib/derbyrun.jar server shutdown
fi
~

Verify Derby

Run the sysinfo command, as shown below, to output Derby system information:

java org.apache.derby.tools.sysinfo

Successful output will look something like this:

$ . java.env
$ . derby.env
$ java org.apache.derby.tools.sysinfo
------------------ Java Information ------------------
Java Version:    1.6.0_24
Java Vendor:     Apple Inc.
Java home:       /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Java classpath:  /Users/don/Documents/workspace/trutree/lib/db-derby-10.7.1.1-bin/lib/derby.jar:/Users/don/Documents/workspace/trutree/lib/db-derby-10.7.1.1-bin/lib/derbyclient.jar:/Users/don/Documents/workspace/trutree/bin:.
OS name:         Mac OS X
OS architecture: x86_64
OS version:      10.5.8
Java user name:  don
Java user home:  /Users/don
Java user dir:   /Users/don/Documents/workspace/trutree
java.specification.name: Java Platform API Specification
java.specification.version: 1.6
java.runtime.version: 1.6.0_24-b07-334-9M3326
--------- Derby Information --------
JRE - JDBC: Java SE 6 - JDBC 4.0
[/Users/don/Documents/workspace/trutree/lib/db-derby-10.7.1.1-bin/lib/derby.jar] 10.7.1.1 - (1040133)
[/Users/don/Documents/workspace/trutree/lib/db-derby-10.7.1.1-bin/lib/derbyclient.jar] 10.7.1.1 - (1040133)
------------------------------------------------------
----------------- Locale Information -----------------
Current Locale :  [English/United States [en_US]]
Found support for locale: [cs]
         version: 10.7.1.1 - (1040133)

Now we start the server in a new command window (Use /Applications/Utilities/Terminal on Mac, or Accessories > Terminal on Linux). Make sure you cd to the correct directory first!

$ cd ~/Documents/workspace/trutree
$ derby.sh start
Sun Apr 10 14:58:29 EDT 2011 : Security manager installed using the Basic server security policy.
Sun Apr 10 14:58:30 EDT 2011 : Apache Derby Network Server - 10.7.1.1 - (1040133) started and ready to accept connections on port 1527

Then we create the database using Derby’s command line tool in another Terminal window, ij:

$ cd ~/Documents/workspace/trutree
$ ij 
ij version 10.7
ij> connect 'jdbc:derby://localhost:1527/trutree;user=app;create=true';
ij> exit;

Check for your database files (notice there are in the directory where you started the DB server, i.e.: ~/Documents/workspace/trutree/trutree):

$ ls -l trutree
total 8
drwxrwxrwx   3 don  don   102 Dec 22 05:29 jar
drwxrwxrwx   5 don  don   170 Mar 26 18:25 log
drwxrwxrwx  84 don  don  2856 Dec 22 05:29 seg0
-rwxrwxrwx   1 don  don   870 Nov  6 18:01 service.properties

Good deal! Now we have a database server and a new database.

To browse around the new database I recommend SQL Squirrel.
Download and install the package for your system, it supports Linux, Mac OS X, and Windows.
Configure the driver for Derby, then connect to your new database:

Alias: trutree
Driver: Apache Derby Client
URL: jdbc:derby://localhost:1527/trutree
User: app
Password: app

If all is good, here is what you will see:

SQL SQuirreL

trutree database initial connection using SQL Squirrel

Next

Web Server

First we need something to pick up files and send them over the wire. NanoHTTPD fits the bill for a small starter application. Created in Java using a single file and only a port as command-line options, you can’t get simpler than this. With a generous BSD license it is easily extendable to suit my purpose at the low end of web site resource creation.

As we add modules to Nano it is best to keep in mind how larger web servers work to keep the code reusable. For instance Servlets are very popular on large web sites using J2EE. Popular ones are Tomcat, Glassfish, and Jboss. Staying with our open-source theme, Glassfish from Sun, now Oracle, seems to be a good choice, but as they all support J2EE standards it should be easy to keep our code generic to work across the board.

As for databases, JavaDB is a good candidate for the low end, Sqllite is the preferred method for Smartphones and HTML5, while Oracle is a winner at the large scale for Relational Management. The are many other NOSQL alternatives such as CouchDB and Cassandra, but again if we keep the DB abstract enough, it should be easy to switch around.

In fact that is my whole theme, to be able to switch components in and out in true Object Oriented fashion. So we will start with a generic framework and start plugging in components.

There are four modes of operation:

1) Tiny (smartphone)

2) Medium (Laptop)

3) Large (Desktop)

4) Huge (Clustered servers)

I will start at medium, move to tiny, and then large.

NanoHTTPD can be:

Run as a standalone app, serves files from current directory and shows requests
Subclass serve() and embed to your own program
Call serveFile() from serve() with your own base directory

So we download NanoHTTPD.java, and compile it:

$ javac NanoHTTPD.java

Then run it:

$ java NanoHTTPD 8080
NanoHTTPD 1.21 (C) 2001,2005-2011 Jarno Elonen and (C) 2010 Konstantinos Togias
(Command line options: [port] [–licence])

 

Now serving files in port 8080 from “/Users/don/Documents/workspace/trutree/src/com/doncohoon/server”
Hit Enter to stop.

Now fire up your browser and enter URL => http://localhost:8080. You should see at least the source for NanoHTTPD.java and some class files. To run the web server on port 80, you will need to run as a privileged user (sudo java NanoHTTPD). This works on Mac OS X, Linux and Windows, except for Windows privileged user access require logging on as Administrator or granting your user Admin rights.

Congratulations! You now have a web server. Next we will build a database to house our dynamic content. Same Bat Channel next time, in Build-a-Website.

Next

Build a Website

Be it ever so humble, there’s no place like home. Home, home on the internet.

One hundred and one ways exist to construct a socket-server collection of your own making. This post is a beginning to describe a method I created after making pages since 1997. The following pages are code heavy. If you want to see the guts of some code then these posts are for you. It all started with a design and a goal.

Goal number one was to make something that did not rely on spending much money. Many people pay big bucks to create and maintain their web sites, and many choices rust after the ages of just a few years, requiring expensive upgrades.

Second on my list was longevity. This site should be able to morph into unknown technologies never heard of today, on several operating systems and very small to very large hardware.

Third was flexibility and ease of use. I want to keep it current, and it should be simple to add new features, such as HTML5, video, RSS, Wiki, Semantic Web, and anything else not invented yet.

With my three main goals in mind I chose to build a Java based framework with all the source code, making it a true Free Software technology. Free as in free from patents, licenses, and proprietary binaries.

As with any quest of substantial undertaking it was readily apparent it would take time, and that was OK since it is a quest of mine. I am master of my domain. King of the code. Ruler of design.

I shall attempt to document and summarize the thought process, building, and pretty pictures of this quest in forthcoming posts tagged with Build-a-Website.
Next

Make Music

Keyboard instruments are a great starting point to discovery of magic within music. Harmonic chords, rhythmic beats, weaving melody. A white and black row of wonder and joy.

Its easy to get started if you follow some simple steps, particularly from an enthusiastic teacher like Scott Houston. As seen on TV there are four steps:

  1. Think of a song you like (hopefully not to hard)
  2. Find and learn the chords
  3. Learn the melody
  4. Play with both hands

I found a song that was simple yet had great meaning to me. It was not too hard finding the lyrics, but chords and melody are proving more difficult. Perhaps I should try another before getting too frustrated, but I have a plan now that I found the chords.

Here is a chord chart that helps me:

Piano Chords

Piano Chord Chart

I have five chords in my song and have practiced switching and counting along with the words. It helped to write the pattern on a piece of lined paper with the chords stacked vertically. This way I can see how far to jump right, then left, and notice patterns. My chords are: C, Am, G7, and F. I practiced these until I was comfortable with the order, switching, and timings.

Next the melody. Since I do not have the music, but I know the song, my plan is to use the following scale chart:

Piano Scales

Piano Scales

I will play with the scale notes that match the chord and when it sounds good write them on some lined paper in the treble cleft notation using the following keyboard-staff guide:

Keyboard Staff

Keyboard Staff

So that’s the Hacker’s Guide to Piano. Many thanks for the inspiration of The Piano Guy to dust off the old Yamaha and enjoy the stress relief of making my own music.

Listen

Do you hear it?
Do they hear you?

The world is speaking in many tongues
and dialects of the senses.

Directional space laser focus
into a void,
is heard by no one.

Toss out a dream
splash on a chance,
watch and learn
how human-kind reacts.

Take some time
to give a little.
Peace
Show respect
to problems.