Tuesday, April 28, 2009

How to build Hibernate Core...I think?

Checkout the lastest SVN trunk from http://anonsvn.jboss.org/repos/hibernate/core/trunk.
I got rev 16450

Set your JAVA_HOME environment variable to a Java 5 JDK (it fails with Java 6).
Mine is set to C:\Program Files\Java\jdk1.5.0_17

Edit parent/pom.xml to add these lines in bold.

...
         </mailingList>
     </mailingLists>


    <repositories>
        <repository>
            <id>jboss</id>
            <url>http://repository.jboss.com/maven2/</url>
        </repository>

        <repository>
            <id>jboss-snapshot</id>
            <url>http://snapshots.jboss.org/maven2</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>jboss</id>
            <url>http://repository.jboss.com/maven2/</url>
        </pluginRepository>
    </pluginRepositories>


     <build>
         <plugins>
             <plugin>
...

Download Maven.
I got apache-maven-2.1.0-bin.zip

Unpack Maven somewhere, cd to the hibernate-core trunk directory and run this command.

<path to apache-maven>/bin/mvn package

This should automatically download everything, build the whole thing, and run the tests.

My test results were

Tests run: 117, Failures: 0, Errors: 56, Skipped: 0

At this point I have no idea what to do…

Friday, April 24, 2009

Highlighting Code Snippets

Windows Live Writer seems to be a popular blogging client, especially with the Code Formatting plugin. After the last post however, its shortcomings became very apparent. The writer’s formatting is fickle and limited. The code formatter is messy and unreliable (it refused to highlight the code in the last post).

It’s sad that with all our technology, we still have yet to make very reliable WYSIWYG text editors. That means writing HTML directly, or finding an intermediate markup language to do the job. So I setup and tried two lightweight markup language for writing blog posts.

reStructuredText

reStructuredText has been officially accepted as the Python inline code documentation formatter. It currently doesn’t handle code formatting, but it’s in the works. This fellow notes that Pygments already has a way to integrate with reStructuredText. It took a lot of finagling (and realizing my PATH was wrong), but I got it working.

Here’s what I did (perhaps not the best way)

  • Download Docutils and unpack it.
  • Run setup.py build which creates a build directory
  • Move all the files in the build/scripts-2.5 directory to the build/lib directory
  • Running rst2html.py will now work, but it can’t do highlighting yet
  • Download the Pygments source and .egg file for the proper Python version
  • Randomly run a bunch of commands on the Pygments source and/or .egg until it somehow gets installed as a Python lib (yes, that’s pretty much what happened)
  • Find the rst-directive.py file amidst the Pygments source, copy its contents, and paste it into rst2html.py right before the last line of that file.

reStructuredText will now recognize the sourcecode directive. There are other options you can set in that Pygments directive code (such as enabling the :linenos: option).

asciidoc

asciidoc works right out of the box, but needs GNU source-highlight for code highlighting (although I guess you could hack it to use whatever else you want). You can download a very old Windows binary, but the latest version has many more highlighters that aren’t compatible with older versions.

Since source-highlight needs some of the boost libraries. I didn’t have much hope in building it with MinGW, so I just went with the Cygwin build. The build process is pretty painless:

  • Using the Cygwin setup, download the boost libraries under the Devel category.
  • Downloaded the latest release of source-highlight
  • Unpack it into a path without spaces (the build with fail otherwise)
  • Fire up the Cygwin shell and cd into the directory
  • 10+ minutes of ./configure, make, and make install, and you’re done

Running asciidoc.py with the -s option removes the HTML header/footers, and source-highlight inlines all its CSS with the code. After getting it working, I recreated the last post in asciidoc format, and replaced the post with the generated output.

It looks really nice.

Blogging with asciidoc

All-in-all, asciidoc is very usable and powerful, but it does a lot more than I wanted, and the syntax isn’t as natural as I would like.

By default asciidoc and the blogspot styling don’t completely get along. The listingblocks don’t have the overflow: auto style, and it takes some tweaking to fix it for both Firefox and IE. There is also a lot of extra spacing added due to several layers of tags, but that’s not hard to fix.

I wonder if there’s a way to create a tool like this for blogspot.

Monday, April 20, 2009

Comparison of Hibernate with Netbeans and Eclipse

Netbeans 6.5.1 with Hibernate Support plugin 1.0.3

Update: Looks like the new Netbeans 6.7 has really improved Hibernate reverse-engineering support.

Netbeans’s Hibernate implementation is pleasantly simple, however its reverse-engineering is currently incomplete. If you want a straight-forward introduction to Java and Hibernate, Netbeans is the perfect way to go. But as soon as you want more powerful visual reverse-engineering tools, you’d best switch to Eclipse.

Steps to use Hibernate in Netbeans

  • Add Hibernate Support plugin
  • Setup a database connection in the Services window
  • Create a new Java Application project
  • Create a new Hibernate Configuration File (hibernate.cfg.xml)
    • Select the database connection
    • press Finish
  • Edit the hibernate.cfg.xml and add these properties
    hibernate.show_sql = true # under Configuration Properties (optional)
    hibernate.current_session_context_class = thread # under Miscellaneous Properties
  • Create new “Hibernate Mapping Files and POJOs from Database”
    • select the configuration file
    • choose the tables to reverse engineer
    • check “JDK 5 Language Features” to get type-safe generics
    • select a package where reverse engineered classes will go
    • press Finish
  • Create new “HibernateUtil.java”
    • So Hibernate can find the configuration file even if it’s in the jar, I recommend changing the line
      sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();

      to

      URL cfgurl = HibernateUtil.class.getResource("hibernate.cfg.xml");
      sessionFactory = new AnnotationConfiguration().configure(cfgurl).buildSessionFactory();

There’s currently no way to regenerate the reverse-engineered code once it’s been generated. This thread says it is “targeted as part of the next release” (Netbeans 6.7 in June '09).

Eclipse 3.4.2 with Hibernate Tools addon 3.2.4

Is it just me, or is it really hard to find up-to-date information on the web about setting up Hibernate in Eclipse? There is one tutorial that doesn’t seem too old, and covers most of my steps listed below.

Being new to Eclipse, it might be difficult to figure out there are actually 2 different addons you can download: Hibernate Tools, and Hibernate Synchronizer (last release 3 years ago?). There’s also MyEclipse that costs money but probably makes the whole process more seamless. To get started in Eclipse, it might be useful to understand how Eclipse does Java.

There are 4 different configurations to be aware of for Eclipse Hibernate Tools:

  1. The Configuration file (hibernate.cfg.xml), used by the Console Configuration (technically optional because most if not all of these settings can be made in the Console Configuration)

  2. The Console Configuration, used when creating the Reverse Engineering file, and by the Hibernate Code Generation configuration

  3. The Reverse Engineering file (hibernate.regeng.xml) used by the Hibernate Code Generation configuration

  4. The Hibernate Code Generation configuration

So with plain Eclipse, here’s how you setup Hibernate Tools.

  • Find the URL to the Eclipse Hibernate Tool addon
  • Add the site to the Software Updates and install the addon
  • Find all the libraries necessary to use Hibernate in your program and download them
  • Create a new Java Project
    • under the Libraries tab, add all those Hibernate libraries
  • Open up the project properties
    • in Hibernate Settings, check “Enable Hibernate 3 support”
  • Create a new Hibernate Configuration file (hibernate.cfg.xml)
    Note that this configuration file is only to be used by the Console Configuration
    • select database dialect
    • select driver class
    • enter the connection URL
    • check “Create a console configuration”
    • press Finish
  • Edit the hibernate.cfg.xml and add these properties
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.show_sql">true</property>  <!-- optional -->
  • Create a new Hibernate Reverse Engineering file
    • select the Console Configuration you just made and press Refresh
    • select which tables to reverse engineer
  • Create a new Hibernate Code Generation configuration
    • select the Console Configuration
    • select the Output Directory (if the output directory contains your hibernate.cfg.xml, it will be overwritten by this task)
    • check “Reverse Engineer from JDBC Connection”
    • enter the regeng.xml file you created
    • under Exporters tab
      • check “Use Java 5 syntax” to get type-safe generics
      • check the following Exporters: Domain code, Hibernate XML Mappings, Hibernate XML Configuration
    • press Run
  • At this point your original Hibernate Configuration file could be replaced by the new one created by the Code Generation, or you can keep them separate
  • There’s no HibernateUtil generator, but all you really need is this
    URL cfgurl = this.getClass().getResource("hibernate.cfg.xml");
    SessionFactory sf = new AnnotationConfiguration().configure(cfgurl).buildSessionFactory();

In my specific case, I’m using Oracle 10g and the tables are in a schema named anotherschema. There are some things to watch out for.

  • Add the Oracle jdbc driver jar to your project libraries.
  • Case sensitivity!

Netbeans:

  • Add the schema to the database connection properties after it’s created (this also means each schema needs a separate connection).
  • Has a problem that prevents me from reverse-engineering a database more than once without restarting the IDE (error says table “cannot be added because it does not have a primary key”).

Eclipse:

  • Most places that need to read or refresh the database schema will often freeze Eclipse.

Friday, April 10, 2009

Installing Linux on a Toshiba Satellite T1960CS

Some time back in 2007 I had the notion to install Linux on an ancient laptop I had sitting around. I figured it might be useful as a resident machine for torrents, a small web server, or even a router.

This was originally posted on a page at atwiki.com, but somehow got deleted. Thankfully archive.org had picked it up, so here it is again, all cleaned up.

The Computer

Toshiba Satellite T1960CS

  • 486, 66Mhz
  • 20MB RAM
  • 600MB HD
  • VGA display
  • PS/2 Keyboard and Mouse ports
  • 3.5'' floppy drive
  • Serial Port
  • Parallel Port
  • PCMCIA Ethernet IC-Card+

No CD drive. No USB port.

This computer also has a known habit of not liking certain types of boot floppies. The floppy will be perfectly fine, and will boot on any other computer. Distasteful floppies make the Toshiba respond with the very detailed message of: “Boot failed”. The Goal

Install some Linux onto the Toshiba T1960CS

The approach suggested in the Damn Small Linux page:

  • Find a tiny distro that can boot from floppy disk
  • Ensure the tiny distro can enable interweb abilities
  • Download and install the real distro of choice to the lappy

Tiny Distros

DSL suggests TOMSRTBT. I never figured out how to enable PCMCIA with that distro (maybe some guru knows how). DSL also suggests BGRescue, but that floppy gave a “Boot failed”.

Other tiny distros tested:

Boots! detects network card (with some tweaking)!

BasicLinux

Boots from 2 floppies. First floppy can be made with simple dd command. Second floppy needs to be formatted MS-DOS, and you copy the tar.gz file onto it.

This thing actually has an X server, with some very basic applications.

After booting with the first disk, do the following:

1) Partition and format the hard drive

I chose a main partition, and a 64mb swap partition.

$ fdisk /dev/hda
: d (delete existing partitions)

: n (new partition)
: p (primary partition)
: 1 (partition 1)
: <Return> (first cylinder)
: +636M (last cylinder)

: n (new partition)
: p (primary partition)
: 2 (partition 2)
: <Return> (start where partition 1 left off)
: <Return> (to the end of the disk)

: a (make a partition bootable)
: 1 (partition 1)

: t (type of partition)
: 2 (partition 2)
: 82 (hex code for swap partition)

: p (check that the partitions are correct)

: w (write the partitions)
2) Format the partitions
$ mke2fs -c /dev/hda1
$ mkswap -c /dev/hda2
3) Mount the drive
$ mount /dev/hda1 /hd
4) BasicLinux to HD

install-to-hd

  1. reboot the system with floppy 1 in the drive

  2. press CTRL when you see LILO

  3. enter this: hd root=/dev/hda1

  4. execute: lilo -v

  5. get the bas2hd package

  6. insert the disk, copy bas-hd.tgz to /tmp, and run go

  7. Edit /etc/rc.hd

Now that I have a drive ready and waiting, I need to get to the interweb.

$ edit /etc/rc.hd

Comment out these two lines

e2fsck -pf /dev/xxxx 2>/dev/null
mount -avt nonfs

Uncomment this line

# /etc/pcmcia/start

Add the following lines at the end (self assigned IP and netmask, default gateway, your DNS servers)

ifconfig eth0 192.168.###.### netmask 255.255.255.0
route add default gw 192.168.###.###
echo "nameserver ###.###.###.###" > /etc/resolv.conf
echo "nameserver ###.###.###.###" >> /etc/resolv.conf

Exit and save with Ctrl+X and y

Copy this new file to the hard drive or floppy to save it

$ cp /etc/rc.hd /hd

Then run the script

$ sh /etc/rc.hd

And make sure the internet is actually working

$ ping google.com

I had to change my pants when it worked the first time.

Get the real distro: DSL…or not

My first attempt at a distro was DSL. I followed their instructions for the “Poorman’s install”.

Unfortunately BasicLinux doesn’t have ash installed, so it can’t run the frugal_lite.sh script. But looking at the script code, all it does is this:

  • download the current DSL iso
  • mount it
  • copy everything out of it, onto the harddisk
  • download the floppy boot image
  • dd the floppy image to a floppy
  • reboot the computer

More unfortunately, I never could get BasicLinux to mount the iso.

Sooo…on another computer, I had to take all the files out of iso and put them into a tar.gz, then run a quick web server with newlisp (newlisp -c -d 8080 -w), so the lappy could wget the file (there’s gotta be a better way to do this).

Finally I got all of the DSL iso files copied onto the lappy harddrive.

I made a boot disk with dd from the ``bootfloppy.img'' file. Unfortunately the Toshiba didn't like the boot disk (“Boot failed”). After confirming that it just wasn’t going to take that image, I instead tried bootfloppy-grub.img. I had to change my pants again when it actually did boot.

Unfortunately, neither of the two Grub options worked. So I had to manually tell it. Press 'c' for the prompt, and type these commands (from here)

root (hd0,0)
kernel /boot/isolinux/linux24 root=/dev/hda1
initrd /boot/isolinux/minirt24.gz
makeactive
boot

(the command "title DSL" just gave me an error)

One last pair of pants for when this thing actually started booting up…and I had plenty of time to change because the bootup took forever.

DSL saw I had PCMCIA, and even the network card, unfortunately it said it didn’t have the drivers for any of it. Also my mouse wasn’t working.

At this point I give up on DSL because I didn’t want to figure out how to install the drivers for my network card without having a network card to download them (I’d have to find them on another computer and copy them onto a floppy?).

Another distro?

I thought I might also try Feather Linux. I have plenty of disk space, and this slightly bigger distro might have my network card drivers.

Need to download the USB distro zip, and unpack to the drive. Make the boot floppy. Once booted, I should be able to install it.

Not to be continued

Got burned out with all this madness. If I ever resume trying to get Linux installed, I plan to just use BasicLinux-at least I know it works. Plus I found X detects the mouse so long as it’s plugged in during bootup and never removed.

Unfortunately BasicLinux doesn’t come with gcc, so adding that would be the first priority.

Miscellaneous

Windows driver for the PCMCIA Ethernet card
http://members.driverguide.com/driver/detail.php?driverid=31708

A fine transform

Spent a day trying to figure out why this function worked…

void transformDraw(BufferedImage bi,
                   Stroke st, Color c,
                   AffineTransform at, Shape sh)
{
    Graphics2D g2d = bi.createGraphics();
    g2d.setStroke(st);
    g2d.seteColor(c);
    g2d.draw(at.createTransformedShape(sh));
}

…but this one…

void transformDraw(BufferedImage bi,
                   Stroke st, Color c,
                   AffineTransform at, Shape sh)
{
    Graphics2D g2d = bi.createGraphics();
    g2d.setStroke(st);
    g2d.seteColor(c);
    g2d.transform(at);
    g2d.draw(sh);
}

…always resulted in the BufferedImage being filled entirely with the stroke color when I called the function like this…

transformDraw(myBufferedImg,
              new BasicStroke(3), Color.RED,
              AffineTransform.getScaleInstance(100000, 100000),
              myShape);

It was this pdf that helped me finally understand why those two functions are different.