Tech Tidbits - Ruby, Ruby On Rails, Merb, .Net, Javascript, jQuery, Ajax, CSS...and other random bits and pieces.

Thursday, June 7, 2007

Javascript/Ajax Libraries

I've been working on a few Ajax and DOM scripting projects at work using prototype.js and scriptaculous. Now we're looking at a "Web 2.0" upgrade for some of our sites, so I'm looking at several libraries:

Prototype JavaScript Framework
script.aculo.us
The Dojo Toolkit
Rico
Yahoo! UI Library (YUI)
mootools framework
MochiKit
JQuery

There are many, many more...
OSA Foundation - Survey of Ajax/JavaScript Libraries

Here's an accoridion demo I created with Rico:
My Rico Accordion Demo

Monday, June 4, 2007

DocBook

I've been tackling some documentation at work and decided to take a look at docbook.

There are a few good resources to start with:


With DocBook, you can use SGML (Dsssl styesheets) or XML (XSL stylesheets). I've chosen to work with XML. "DocBook XSL" provides the details of all the tools that are needed. At the moment I'm working on a Mac laptop, using the following:



I installed everything in my home directory and worked from there, but LSB proposal is:

  • DTD: /usr/share/sgml/docbook/xml-dtd-4.5

  • XSL: /usr/share/sgml/docbook/docbook-xsl-1.72.0/



Create a DocBook XML file (book.xml, for example)

<?xml version="1.0"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.5/EN"
"/path/to/dtd/docbook-xml-4.5/docbookx.dtd">
<book>
<bookinfo>
<title>My Book Title</title>
</bookinfo>

<chapter>
<title>Chapter Name</title>

<para>Some text</para>
</chapter>
</book>


Procss to generate html:

$ xsltproc --output book.html /path/to/docbook-xsl-1.72.0/html/docbook.xsl book.xml


You can validate XML using xmllint (parser won't necessarily do it).

$ xmllint --noout book.xml

No output will be seen unless there is an error.

I haven't covered other formats (like converting to pdf using FOP), Java parsers, catalogs, makefiles, and Ant.

Speaking of Ant, here's a resource for DocBook and Ant:

DocBook and Ant.

I've already started looking at using Cocoon with DocBook, but that will be material for another post...

WebSphere Application Server V6.1 on Linux

I recently downloaded the full-featured trial download of WebSphere Application Server V6.1 to my old Fedora Core 5 laptop.

The installation was amazingly easy...


$ tar zxvf websphere.tar.gz
$ cd WAS/install
$ java -jar WAS/setup.jar
$ cd /opt/IBM/WebSphere/AppServer/bin
$ ./startServer server1


After starting WebSphere, you can check out the sample application:


http://localhost:9080/WSsamples

Friday, May 25, 2007

Audio file conversion

Here's a collection of small scripts for converting audio file formats using lame, mpg123, and mplayer.

wav2mp3.pl

#!/usr/bin/perl
use strict;
use warnings;

my $in = "/home/doug/music/somedir/";
my $out = $in;

opendir DH_IN, $in || die "Can't open $in: $!";
foreach my $name (sort readdir(DH_IN)) {
next unless $name =~ /\.wav$/;
print "NAME: $name\n";
my($base_name) = ($name =~ /^(.*)\.wav$/);
my $in_file = "${in}$base_name.wav";
my $out_file = "${out}$base_name.mp3";
`lame -b 192 $in_file $out_file`;
}
close DH_IN;


wav2mp3.sh

#!/bin/sh
bitrate="192"
for wavfile in *.wav
do
echo "input: $wavfile"
mp3file=`echo $wavfile | sed s/\\.wav/.mp3/`
echo "output: $mp3file"
lame -b $bitrate $wavfile $mp3file
done


mp32wav.sh

#!/bin/sh
for mp3file in *.mp3
do
echo "input: $mp3file"
wavfile=`echo $mp3file | sed s/\\.mp3/.wav/`
echo "output: $wavfile"
mpg123 -w $wavfile $mp3file
done


wma2mp3.sh

#!/bin/sh

if [ -z "$1" ]
then
echo "Usage: wma2mp3 "
exit 1
fi

name=`echo $1 | tr ' ' '_' | tr '[A-Z]' '[a-z]'`

name="`basename "$name" .wma`.mp3"

cp "$1" $name

mplayer -vo null -vc dummy -af resample=44100 -ao pcm:waveheader $name && lame -m s -h --vbr-new audiodump.wav -o $name


m4a2mp3.sh

#!/bin/sh

MPLAYER=mplayer
LAME=lame

for m4a in *.m4a
do

mp3=`echo "$m4a" | sed -e 's/m4a$/mp3/'`
wav="$m4a.wav"

if [ ! -e "$mp3" ]; then
[ -e "$wav" ] && rm "$wav"
mkfifo "$wav" || exit 255
mplayer "$m4a" -ao pcm:file="$wav" &>/dev/null &
lame "$wav" "$mp3"
[ -e "$wav" ] && rm "$wav"
fi
done

Finding installed Perl modules

I don't recall where I came across this little script, but it's quite handy for finding installed Perl modules along with their versions.


#!/usr/bin/perl
use strict;

use ExtUtils::Installed;
my $instmod = ExtUtils::Installed->new();
foreach my $module ($instmod->modules()) {
my $version = $instmod->version($module) || "???";
print "$module -- $version\n";
}

Wednesday, May 16, 2007

iBATIS - part 1

Very basic and simple iBATIS tutorial for a simple SELECT:

Download iBATIS from http://ibatis.apache.org/

Unpack the tar ball and install the jar file (it's now a single file - something like ibatis-2.3.0.677.jar) somewhere on your classpath...or if using with Tomcat or another servlet container, add it to "/WEB-INF/lib" in your for the webapp.

Database (MySQL)

mysql> describe book;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(50) | NO | | | |
| authors | varchar(50) | YES | | NULL | |
| editor | varchar(50) | YES | | NULL | |
| chapters | text | YES | | NULL | |
| page_count | int(11) | YES | | NULL | |
| status | char(1) | NO | | P | |
| price | decimal(5,2) | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+

mysql> SELECT * FROM book;
+----+----------------------+---------+--------+----------+------------+--------+-------+
| id | title | authors | editor | chapters | page_count | status | price |
+----+----------------------+---------+--------+----------+------------+--------+-------+
| 1 | Learning Perl 3rd Ed | NULL | NULL | NULL | NULL | P | 29.99 |
| 2 | Learning Java 4th Ed | NULL | NULL | NULL | NULL | P | 29.99 |
+----+----------------------+---------+--------+----------+------------+--------+-------+


SqlMapConfig.xml

PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>

<!-- These settings control SqlMap configuration details -->
<settings
useStatementNamespaces="true"
/>

<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost/bookdb"/>
<property name="JDBC.Username" value="yourusername"/>
<property name="JDBC.Password" value="yourpassword"/>
</dataSource>
</transactionManager>

<!-- Identify all SQL Map XML files to be loaded by this SQL map -->
<sqlMap resource="BookMap.xml" />

</sqlMapConfig>


BookMap.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="Book">

<resultMap id="get-Result" class="Book">
<result property="id" column="id"/>
<result property="title" column="title"/>
</resultMap>

<select id="getById" parameterClass="string" resultMap="get-Result">
SELECT * FROM book WHERE (id = #value#)
</select>

<select id="getAll" parameterClass="string" resultMap="get-Result">
SELECT * FROM book WHERE (status = 'P') ORDER BY title
</select>

</sqlMap>


Book.java

import com.ibatis.sqlmap.client.SqlMapClient;
import java.sql.Date;

public class Book {

public int getId()
{
return id;
}

public void setId(int id)
{
this.id = id;
}

public String getTitle()
{
return title;
}

public void setTitle(String title)
{
this.title = title;
}

public char getStatus()
{
return status;
}

public void setStatus(char status)
{
this.status = status;
}

private Integer id;
private String title;
private char status;
}


BookTest.java

import com.ibatis.sqlmap.client.*;
import com.ibatis.common.resources.Resources;

import java.io.Reader;
import java.util.List;
import java.util.Iterator;

public class BookTest {
public static void main(String arg[]) throws Exception {
String resource = "SqlMapConfig.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
List bookList = sqlMap.queryForList("Book.getAll",null);
System.out.println("Selected " + bookList.size() + " records.");

if(bookList.size() == 0){
System.out.println("OOOPS - bookList size is 0\n");
}
Iterator books = bookList.iterator();
StringBuffer bookBuffer = new StringBuffer();
while(books.hasNext()){
Book bookX = (Book)books.next();
bookBuffer.append("ID: " + bookX.getId() + " TITLE: " + bookX.getTitle() + "\n");
}
String bookOptions = bookBuffer.toString();
System.out.println(bookOptions);
}
}



$ java BookTest
Selected 2 records.
ID: 2 TITLE: Learning Java 4th Ed
ID: 1 TITLE: Learning Perl 3rd Ed

Sunday, May 13, 2007

Tomcat - How to run stand alone on port 80

With Apache, all ports below 1024 are privileged ports, so normally to run Tomcat as a stand alone server, you'd have to run as root, which is a Very Bad Idea. Using the jsvc interface, it is possible to bind Tomcat to a privileged port, such as port 80.

First, change server.xml to use port 80:


$ cd $CATALINA_HOME/conf
$ sudo vi server.xml


and make this change:


<!--<Connector port="8080" protocol="HTTP/1.1" -->
<Connector port="80" protocol="HTTP/1.1"


With Tomcat 5 & 6, there will be a jsvc.tar.gz file in the bin directory of your $CATALINA_HOME directory.


$ cd $CATALINA_HOME/bin
$ sudo tar zxvf jsvc.tar.gz
$ cd jsvc-src
$ sudo autoconf
$ sudo ./configure
$ sudo make
$ sudo cp jsvc ..


I had to add a few packages on Ubuntu to compile jsvc...

$ sudo apt-get install libc6-dev g++ gcc
$ sudo apt-get install make
$ sudo apt-get install autoconf


You can start Tomcat as "tomcat" user (use which ever existing non-privileged user you want, or create a new user):


$ cd $CATALINA_HOME
$ sudo ./bin/jsvc -cp ./bin/bootstrap.jar \
-user tomcat \
-outfile ./logs/catalina.out \
-errfile ./logs/catalina.err \
org.apache.catalina.startup.Bootstrap


There is a sample starup script in the $CATALINA_HOME/bin/jsvc-src/native directory, which can be modified to work on your system:


$ cd /etc/init.d
$ sudo cp tomcat tomcat.bak (if needed, and make sure it will start on the run levels you need for startup on server boot)
$ sudo cp $CATALINA_HOME/bin/jsvc-src/native/Tomcat5.sh ./tomcat
$ sudo vi tomcat (edit for your system)


Now you can start Tomcat:


$ sudo /etc/init.d/tomcat start


More info can be found at Apache Jakarta Project - commons daemon (Java Service)

About Me

My photo
Developer (Ruby on Rails, iOS), musician/composer, Buddhist, HSP, Vegan, Aspie.

Labels