Wednesday, February 20, 2008

Gaining through Grids

Tom's got a blurb on the excellent Grid presentationNick and he gave at the recent QJUG. Some pictures follow:




Nick and Glen conspire




Good questions as always



Interesting punch-lines! :)

Good work guys! :)

Many thanx to the wonderful folks @ VLC for hosting and giving us the opportunity to work on such an interesting project.

Tuesday, February 19, 2008

Aaaargclipse!

Oh how I love installing and using wondrous Eclipse. It is a tool of fun and frolic not to mention of much utility.

No. Not really. A while back I was trying to install Eclipse on 64-bit Ubuntu 7.10. Easy enough. I had done it several times before. So this should be a cinch right? Well not exactly. When I ran eclipse after installation I kept getting weird errors such as:

error while loading shared libraries: libgtk-x11-2.0.so.0: cannot open shared object file: No such file or directory

Yes, brilliant! So what does that tell me? Oh so I need libgtk-x11-2.0.so.0. So why don't I have it? What? I do? Ok....?

Or the equally self-explanatory:



Superb! Could I ask for a more meaningful error message? Possibly. Definitely. Maybe. (Isn't that a movie?)

After hours of trying to fix this problem by installing libraries I didn't need, I found out that I had installed the 32-bit version of Eclipse on my 64-bit OS. An error message of the sort "I think you are trying to install a 32-bit version of Eclipse on a 64-bit platform. Are you sure you want to do this?" might have been nice. It would have saved me hours of what I like to call "wasting time with Eclipse".

So why did I install the wrong version of Eclipse? The eclipse website detects the version of the OS you are running and guides you to the appropriate download. So when I went to the site from 64-bit Ubuntu it took me to:



Now if I click on the "linux" link, it takes me to the 32-bit download, which is totally wrong for my platform and leads to hours of fun, installing superfluous libraries! Yay!



But if I click on the "Eclipse Classic 3.3.1.1" link it takes me to the correct 64-bit download:



So why is the eclipse web site so confusing? It's probably the same the reason why eclipse is so hard to use! :)

If you are installing eclipse on Ubuntu 64-bit, make sure you have the
xxxx-gtk-x86_64.tar.gz version as opposed to the xxxx--gtk.tar.gz version.

Enough Eclipse-bashing. Have you tried Intellij btw? ;)

Tuesday, February 5, 2008

Partial Implementations with Traits

A trait in Scala is similar to an interface in Java. It's similar in the sense that it defines a contract (through methods signatures) as a Java interface would. It also has the capability of implementing the methods it defines, similar to an abstract class in Java. This is known as "partial implementation" as it implements some but not all of the methods on the trait. Traits can also be used as mixins to given functionality similar to multiple inheritance. In Java we implement interfaces and in Scala we extend traits.

The examples below use the trait's partial implementation similar to that of abstract classes in Java. I know many people hate using abstract classes and inheritance for that matter, due to one reason or another (hard to test, complex, inflexible take your pick - or have a look @ Tom's article The practical problems of inheritance). But everything has a time and a place. We are going to ignore the above (valid) arguments for the moment and see what traits can do for us by providing partial implementations.

So let's see this partial implementation jazz already.

Suppose we had an interface similar to the java.awt.event.MouseListener interface. Most of the time you are only interested in 1 or 2 of the methods on this interface and providing implementations for the other methods is a pain in the rear. The java.awt.event.MouseAdaptor class provides default implementations of all methods on the MouseListener interface, allowing the developer to override only the method(s) of interest. Let's create a similar interface to the MouseListener interface in Java:

public interface JavaMouseEvent {}

public interface JavaMouseListener {
void mouseClicked(JavaMouseEvent e);
void mousePressed(JavaMouseEvent e);
void mouseReleased(JavaMouseEvent e);
void mouseEntered(JavaMouseEvent e);
void mouseExited(JavaMouseEvent e);
}


Now say we are only interested in the mouseClicked event. We create our own adaptor class in the form of a trusty abstract class or a base class:


public abstract class JavaMouseAdaptor implements JavaMouseListener {
@Override
public void mouseClicked(JavaMouseEvent e) {}

@Override
public void mouseEntered(JavaMouseEvent e) {}

@Override
public void mouseExited(JavaMouseEvent e) {}

@Override
public void mousePressed(JavaMouseEvent e) {}

@Override
public void mouseReleased(JavaMouseEvent e) {}
}


Now we can handle the mouseClicked event as so:


public class JavaEventHandler extends JavaMouseAdaptor {
@Override
public void mouseClicked(JavaMouseEvent e) {
System.out.println("The mouse has been clicked! Call the mouse brigade");
}
}


In Scala we could do the above with:


trait ScalaMouseEvent

trait ScalaMouseListener {
def mouseClicked(e: ScalaMouseEvent): Unit = {}
def mousePressed(e: ScalaMouseEvent): Unit = {}
def mouseReleased(e: ScalaMouseEvent): Unit = {}
def mouseEntered(e: ScalaMouseEvent): Unit = {}
def mouseExited(e: ScalaMouseEvent): Unit = {}
}

final class ScalaEventHandler extends ScalaMouseListener {
override def mouseClicked(e: ScalaMouseEvent): Unit = {
println("The mouse has been clicked! Call the mouse brigade")
}
}


We essentially, have cut out the adaptor class - which we didn't need thanks to partial implementation of traits in Scala. This is also easier to maintain because the implementation is in the same location/File as the definition of the contract.

If we wanted to look beyond default implementations of a trait, we could implement a pattern such as the Template pattern, through a trait with partial implementation. Say we had a process, and every process had a start, step1, step2, step3 and end stages. And further assume that each process has a processName. We could model the process through the template pattern in Java like so:

public interface Process {
void start();
void step1();
void step2();
void step3();
void process();
void end();
String getProcessName();
}

public abstract class AbstractProcess implements Process {
public void start() {
System.out.println("Starting process: " + getProcessName());
}

public abstract void step1();
public abstract void step2();
public abstract void step3();

public void process() {
start();
step1();
step2();
step3();
end();
}

public void end() {
System.out.println("ending process: " + getProcessName());
}

public abstract String getProcessName();
}

public final class ReviewProcess extends AbstractProcess {
@Override
public String getProcessName() {
return "Review Process";
}

@Override
public void step1() {
System.out.println("processing step1");
}

@Override
public void step2() {
System.out.println("processing step2");
}

@Override
public void step3() {
System.out.println("processing step3");
}
}


We run the code with:


new ReviewProcess().process();


The output we receive is:


Starting process: Review Process
processing step1
processing step2
processing step3
ending process: Review Process


In Scala we could do the same thing through a trait:


trait Process {
def start:Unit = println("starting process: " + processName)

def step1:Unit
def step2:Unit
def step3:Unit

def end:Unit = println("ending process: " + processName)

def process():Unit = {
start
step1
step2
step3
end
}

def processName: String
}

final class ReviewProcess extends Process {
override def step1(): Unit = println("processing step1")
override def step2(): Unit = println("processing step2")
override def step3(): Unit = println("processing step3")
override def processName(): String = "Review Process"
}


If we run the above code with:


new ReviewProcess().process


The output we receive is:


starting process: Review Process
processing step1
processing step2
processing step3
ending process: Review Process

The template pattern implementation is much more terse in Scala. Just as you could override methods on base class via the subclass in Java, you can override methods implemented on the trait with the override keyword from the extensions of the trait:



final class AutomationProcess extends Process {
override def step1(): Unit = println("automating step1")
override def step2(): Unit = println("automating step2")
override def step3(): Unit = println("automating step3")
override def processName(): String = "Automation Process"
override def start = println("Commencing the following process: " + processName)
}


Running the above through
new AutomationProcess().process
yeilds:


Commencing the following process: Automation Process
automating step1
automating step2
automating step3
ending process: Automation Process


If we don't want an implementation to be overridden, we declare the method final, with def final. For example to prevent the start method being overridden, we could use the following on the Process trait:


final def start:Unit = println("starting process: " + processName)


Making the template methods final is a good practice when implemented using inheritance.

So the question is whether implementations on the trait are a good thing. Sure, it's flexible but it goes against some "standard" practices in Java. I find traits useful for instances like the MouseListener example, where I only ever have one use for the extension and where I don't really care about the other implementations. For the template pattern a delegation solution would be easier to use and maintain without partial implementations.

Sunday, February 3, 2008

Gmail Vulnerable to Sidejacking

"Last August, security researcher and CEO of Errata Security Robert Graham demonstrated just how easy it could be access potentially serious user information. His technique (nicknamed sidejacking), intercepts session ID cookies from the WiFi signal and used for a number of purposes, including sending and receiving e-mail. This type of attack takes place after the end-user has securely logged on to a service. Virtually all companies provide a secure login portal, but many do not secure the connection thereafter, which exposes the end-user to potential hacking as described above. During his demonstration at the time, Graham said that Google Mail users could switch to https://mail.google.com and secure their session from such snooping—but he's now backed away from and qualified that statement."

Check out the full article here.

Ubuntu: What's Under the Hood

I recently stumbled upon the rather useful lshw command. The lshw command provides you with hardware configuration information of your machine. Its man page states:

"lshw is a small tool to extract detailed information on the hardware configuration of the machine. It can report exact memory configuration, firmware version, mainboard configuration, CPU version and speed, cache configuration, bus speed, etc. on DMI-capable x86 or IA-64 systems and on some PowerPC machines (PowerMac G4 is known to work).

It currently supports DMI (x86 and IA-64 only), OpenFirmware device tree (PowerPC only), PCI/AGP, CPUID (x86), IDE/ATA/ATAPI, PCMCIA (only tested on x86), SCSI and USB."

Some sample usages:

For a summary of devices on your system:

sudo lshw -short

H/W path Device Class Description
=========================================================
system Desktop Computer
/0 bus K8M800-8237
/0/0 memory 128KB BIOS
/0/4 processor AMD Sempron(tm) Processor 2800+
/0/4/8 memory 128KB L1 cache
/0/4/9 memory 256KB L2 cache
/0/18 memory 1536MB System Memory
/0/18/0 memory 1GB DIMM 400 MHz (2.5 ns)
/0/18/1 memory 512MB DIMM 400 MHz (2.5 ns)
/0/e4000000 bridge K8M800 Host Bridge
/0/e4000000/1 bridge VT8237 PCI bridge [K8T800/K8T890 South]
/0/e4000000/1/0 display S3 Unichrome Pro VGA Adapter
/0/e4000000/9 scsi0 storage VT6421 IDE RAID Controller
/0/e4000000/a eth0 network DGE-530T Gigabit Ethernet Adapter (rev 11)
/0/e4000000/f scsi3 storage VIA VT6420 SATA RAID Controller
/0/e4000000/f/0 /dev/sda disk 186GB WDC WD2000JS-60M
/0/e4000000/f/0/1 /dev/sda1 disk Linux filesystem partition
/0/e4000000/f/0/2 /dev/sda2 disk 166GB Extended partition
/0/e4000000/f/0/2/5 /dev/sda5 disk Linux filesystem partition
/0/e4000000/f/0/2/6 /dev/sda6 disk Linux swap / Solaris partition
/0/e4000000/f/1 /dev/sdb disk 298GB ST3320620AS
/0/e4000000/f/1/1 /dev/sdb1 disk 298GB Extended partition
/0/e4000000/f/1/1/5 /dev/sdb5 disk Linux filesystem partition
/0/e4000000/f.1 storage VT82C586A/B/VT82C686/A/B/VT823x/A/C PIPC Bus Master IDE
/0/e4000000/f.1/0 ide0 bus IDE Channel 0
/0/e4000000/f.1/0/0 /dev/hda disk PIONEER DVD-RW DVR-110
/0/e4000000/f.1/0/0/0 /dev/hda disk
/0/e4000000/10 bus VT82xxxxx UHCI USB 1.1 Controller
/0/e4000000/10/1 usb1 bus UHCI Host Controller
/0/e4000000/10.1 bus VT82xxxxx UHCI USB 1.1 Controller
/0/e4000000/10.1/1 usb2 bus UHCI Host Controller
/0/e4000000/10.2 bus VT82xxxxx UHCI USB 1.1 Controller
/0/e4000000/10.2/1 usb3 bus UHCI Host Controller
/0/e4000000/10.3 bus VT82xxxxx UHCI USB 1.1 Controller
/0/e4000000/10.3/1 usb4 bus UHCI Host Controller
/0/e4000000/10.4 bus USB 2.0
/0/e4000000/10.4/1 usb5 bus EHCI Host Controller
/0/e4000000/11 bridge VT8237 ISA bridge [KT600/K8T800/K8T890 South]
/0/e4000000/11.5 multimedia VT8233/A/8235/8237 AC97 Audio Controller
/0/100 bridge K8M800 Host Bridge
/0/101 bridge K8M800 Host Bridge
/0/102 bridge K8M800 Host Bridge
/0/103 bridge K8M800 Host Bridge
/0/104 bridge K8M800 Host Bridge
/0/105 bridge K8 [Athlon64/Opteron] HyperTransport Technology Configuration
/0/106 bridge K8 [Athlon64/Opteron] Address Map
/0/107 bridge K8 [Athlon64/Opteron] DRAM Controller
/0/108 bridge K8 [Athlon64/Opteron] Miscellaneous Control

For information on a specific class of device (the classes can be retrieved by the above command) such memory:

sudo lshw -C memory


*-firmware

description: BIOS
vendor: Award Software International, Inc.
physical id: 0
version: FB (12/07/2005)
size: 128KB
capacity: 448KB
capabilities: pci pnp apm upgrade shadowing cdboot bootselect socketedrom edd int13floppy360 int13floppy1200 int13floppy720 int13floppy2880 int5printscreen int9keyboard int14serial int17printer int10video acpi usb agp ls120boot zipboot biosbootspecification

*-cache:0

description: L1 cache
physical id: 8
slot: Internal Cache
size: 128KB
capacity: 128KB
capabilities: synchronous internal write-back

*-cache:1

description: L2 cache
physical id: 9
slot: External Cache
size: 256KB
capacity: 256KB
capabilities: synchronous internal write-back

*-memory

description: System Memory
physical id: 18
slot: System board or motherboard
size: 1536MB

*-bank:0

description: DIMM 400 MHz (2.5 ns)
physical id: 0
slot: A0
size: 1GB
width: 64 bits
clock: 400MHz (2.5ns)

*-bank:1

description: DIMM 400 MHz (2.5 ns)
physical id: 1
slot: A1
size: 512MB
width: 64 bits
clock: 400MHz (2.5ns)

Or to get CPU information:

sudo lshw -C processor


*-cpu
description: CPU
product: AMD Sempron(tm) Processor 2800+
vendor: Advanced Micro Devices [AMD]
physical id: 4
bus info: cpu@0
version: AMD Sempron(tm) Processor 2800+
slot: Socket 754
size: 1600MHz
capacity: 4GHz
width: 64 bits
clock: 200MHz
capabilities: fpu fpu_exception wp vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx mmxext fxsr_opt x86-64 3dnowext 3dnow up pni lahf_lm


Remember to run the command with sudo or you will only get a partial list of results.