Aspect-oriented programming with AspectJ

In one of the courses I took this session I was introduced to aspect-oriented programming. For those who are not familiar with it, an aspect can be used to implement a concern that is crosscutting among different components. (There are a few such concerns that are commonly affected by crosscutting, namely: authentication, persistence, logging and contract checking.) It is argued that with object-oriented programming, even after proper refactoring, it is not always possible to map a requirement to a single component. When a requirement is implemented by more than one component, scattering occurs. AOP allows you to localize the scattered requirement into an aspect.

AspectJ is an extension to the Java language that adds support to AOP. An aspect in AspectJ is composed of pointcuts and advices. A pointcut defines the condition that needs to be met in order for the logic in an advice to be executed. A pointcut could be, for instance, the execution of a method in a particular class.

How AspectJ can be useful

Let’s look at an example. Say we want to log when horses drink water and when cows eat grass. Assume we have a class Logger with a static method log(). Here’s what the implementation could look like:

public class Horse {
	private int consumedWaterInLitres;

	public void drink() {
		consumedWaterInLitres++;
		Logger.log("A horse is drinking water.");
	}
}

public class Cow {
	private int consumedGrassInGrams;

	public void eat() {
		consumedGrassInGrams++;
		Logger.log("A cow is eating grass.");
	}
}

The argument is that the concern of logging is now crosscut among the Horse and Cow components. In addition, it can be said that Horse.drink() and Cow.eat() are responsible for more core logic (eating and drinking) than they should have been. Let’s try to fix this problem with an aspect:

public aspect Logging {
	pointcut logHorseDrinking() : call(public void Horse.drink());
	pointcut logCowEating() : call(public void Cow.eat());

	void after() : logHorseDrinking() {
		Logger.log("A horse is drinking water.");
	}

	void after() : logCowEating() {
		Logger.log("A cow is eating grass.");
	}
}

Here we defined two pointcuts that capture calls to Horse.drink() and to Cow.eat(). We’ve attached these pointcuts to two advices that will run after these methods are executed. With the Logging aspect in place, we can now remove all calls to Logger.log() from Horse and Cow. Isn’t that great?

How AspectJ can be dangerous

Dog before DogAspect

Dog before DogAspect

Aspects can also add state, behaviour and inheritance to a component. Privileged aspects have access to all features in a system—including private ones. Does that raise a red flag? Let’s look at the class Dog below.

public class Dog {
}

It has no features—no attributes nor methods—and doesn’t extend from any class, right? Wrong:

public aspect DogAspect {
	declare parents: Dog extends Animal;
	private String Dog.ownersName = "Bobert";

	public String Dog.getOwnwersName() {
		return ownersName;
	}
}
Dog after DogAspect

Dog after DogAspect

The aspect above has completely changed the class Dog. It has made it an Animal, it has added an attribute ownersName and a getter for it. The worst part is that Dog is completely oblivious to the aspect—it will never know about it. In fact, unless you as a developer look at all aspects on the system, you will never know about it either.

That is not completely true because some IDEs will provide a visual clue as to when a class is affected by an aspect. The AspectJ Developement Tools add-on for Eclipse (a great IDE by the way) is supposed to show a marker on the editor margin whenever a component is being advised by an aspect. I have the latest version installed (ADJT 1.6.4) but for some reason the marker is not showing up, unfortunately.

There are also other issues one might encounter when using AspectJ. It is not an easy task to document the impact aspects have on a system—UML has no support for aspects at this moment, and how are you going to show that on a sequence diagram? Also, the debugging and tracing of execution of a class that is being advised by an aspect can get pretty tricky.

In conclusion

I believe that the application of AOP could indeed improve the quality of a system through the localization of crosscutting concerns. However, its Java implementation—AspectJ—provides a level of control that is too risky to be used in industrial medium- to large-scale projects. Its supporting technologies and documenting tools have not yet reached the desired maturity level.

Mediawiki Database Schema

Since last summer I’ve been working with three other Software Engineering undergraduates on a web enterprise application that will eventually replace a legacy ERP system. We used patterns from Martin Fowler’s Patterns of Enterprise Application Architecture (a great book, by the way), and the well-known three-layered architecture.

One day we had a discussion related to the persistence of Domain Models and whether we should enforce foreign-key constraints at the database level. My first reaction was that the very use of a relational database implied the enforcement of such constraints, but some of my colleagues argued that the database should be seen as nothing but a persistence mechanism and therefore we should avoid placing any business logic in it. We ended up by not using foreign-key constraints.

Would you have done otherwise? I would like to hear what other software engineers/developers have to say about this.

Google Chrome, a new web browser

Google confirmed tomorrow’s launch of Google Chrome beta, a new open source web browser that borrows the rendering engine from Apple’s WebKit and components from Mozilla Firefox.

An important design aspect behind Google Chrome is that each tab will be assigned an entire process instead of a thread within a process. This means that if a particular website causes the page to hang, only its tab will have to be closed. In addition, a task manager will allow the user to see which page, plug-in or web application is consuming system resources, a feature available in all modern OSs.

Google has published a comic that explains in 38 pages their web browser project.

How will this affect the battle for market share between existing web browsers? Will Google Chrome be adopted by Firefox or IE users?

Google Chrome Comic page 38 A screenshot Google Chrome

Update 1: added a screenshot (found at TechCrunch).
Update 2: The Google Chrome webpage is up.

Yet another article on why IE six sucks

I can give at least two reasons: its PNG rendering and CSS support. When I worked on the new version of Pensador’s Blog and compared the results on both Firefox 2 and Internet Explorer 7, I had the naïve assumption that I was covering most of the browsers, until my girlfriend told me she could not see half of my site on her laptop.

To be honest, at first I had the usual arrogant geek reaction—she must be doing something wrong. She had to send me a screenshot so that I could realize she was using version six of the infamous browser. I am not going into the technical intricacies as to why PNG and CSS are not well supported; all I will and can say is that IE just recently started to comply with the web standards.

Here are the side-by-side screenshots comparing Pensador’s Blog on Firefox 2, IE 7 and IE 6 respectively:

As you see, the PNG transparency was competely ignored and here is why the blog posts do not show up:

I was forced to convert all those PNG files to GIFs and to hack my CSS to get the desired results. For some reason, the “main” div in which I put the blog posts is too wide to fit in the “page” div. However it should fit since its width was 600 px and the menu on the left was 180 px, adding up to the 780 px of the “page” div. Anyway, I had to reduce the size of the “main” div to 585 px for it to show up on IE 6.

iPod + cell phone + web + email = iPhone

Apple did it again. They already redefined the way we listen to music with iPods and now they will revolutionize how we communicate with each other. An iPhone is a combination of an iPod with a cell phone with Internet and email facilities. I’ll probably get one in 2 years, when my contract ends and the price will be more affordable than $600 :)

Firefox 2.0 Released!

The final version of Firefox 2.0 has been released today. Here are the great new features:

  • Spell check for textareas (thank you!)
  • Search engine manager
  • Updated user interface
  • Navigation arrows for when you’ve opened too many tabs
  • Updated Add-ons manager
  • And much more!

Download it now!

Google Buys YouTube, Spends $1.65 Billion

After several offers from many sources, Google finally closed the deal yesterday.

Google has agreed to purchase popular video sharing site YouTube for $1.65 billion in stock, the two companies announced after the close of the stock market Monday. The deal marks the largest acquisition for Google in the company’s eight-year history.

I’m afraid that this acquisition will be just a merger with Google Video and YouTube will lose its unique user experience. Hopefully this won’t happen:

YouTube will retain its brand name and offices in San Bruno, Calif., the companies said, and all YouTube employees will keep their jobs. The acquisition is expected to close before the end of the year.

Source: BetaNews

The W3 Validator doesn’t validate PHPSESSID?

Are you getting the following error from the W3C validator?

cannot generate system identifier for general entityPHPSESSID

This is because you are using sessions on your website and PHP adds the session ID to links on your page. If the URL already has a GET parameter, &PHPSESSID is added at the end. However, the correct code would be &PHPSESSID since for the validator the ampersand means that you are refering to a HTML entity. A quick way to fix this is to turn on the option arg_separator.output in php.ini. If you don’t have access to that file, you can use the following directive in a .htaccess file:

php_value arg_separator.output "&"

AMD to buy ATI

Advanced Micro Devices plans to acquire graphics chipmaker ATI Technologies for $5.4 billion, a move intended to increase AMD’s mobile-computing and consumer electronics capabilities, and help battle archrival Intel.

Source: ZDNet

What’s next? Intel buying nVidia?

Firefox 1.0.1 is out!

Here’s what’s new in Firefox 1.0.2:

  • Improved stability
  • International Domain Names are now displayed as punycode. (To show International Domain Names in Unicode, set the “network.IDN_show_punycode” preference to false.)
  • Several security fixes.

Get the update here