Monday, December 24, 2007

Working with Twitter

I have been playing with Twitter a little bit recently. It is a service that allows you share what you are doing with people via little messages. I can just be used for fun, but it can also be a valuable way to keep up with coworkers and colleagues. Updates can be sent via the website, IM, or even SMS texts from your phone.
I have been experimenting with the facebook integration and it is pretty good (via the TwitterSync app). This is the best way I have found to keep your facebook status synced up with Twitter.
I am most curious to see how use of twitter in a professional setting evolves. I think it could be a great way for split project team members to keep up with each other.

Saturday, December 22, 2007

Strings and StringBuilder optimizations

I keep running across code like this:
public String THIS = "this ";
...
StringBuilder sb = new StringBuilder();
sb.append(THIS);
sb.append("is ").append("a ");
sb.append("test: ");
sb.append(5);

It is hard to read and maintain, and wastes the time of the developer who wrote it because there is basically no benefit to writing it. The (Sun) Java compiler will do some optimizations which end up allowing coders to write nicer looking code and avoid lots of extra typing or copy and paste. Let's rewrite the same code the way we would like to if we were not trying to over optimize.
  String test = THIS + "is " + "a " + "test: " + 5;
That's easier to read and the compiler turns this into a StringBuilder anyway. It is easily prove n by runing this code through a debugger and tracing it.
There is another related trick that the compiler will do for Strings.
  String test = "this " + "is " + "a " + "test: " + 5;
That becomes "this is a test: 5" in the bytecode (this is in the language spec). Very easy to read and zero cost at runtime.

As always, the old rule of thumb applies. Always optimize last (or never). Readable and maintainable code is far more important that a minor optimization which possibly saves a few nanoseconds (and in this case saves none).

Before anyone gets the wrong impression, there are perfectly viable cases for writing StringBuilders in your code. Creating a string from a large number of smaller strings within a loop is the most common use case. For example:
StringBuilder sb = new StringBuilder();
sb.append("Counting: ");
for (int i = 0; i <>
sb.append(" ").append(i);
}
String test = sb.toString();

In this case, using a single string and writing += would be more costly and should be avoided for any code that will run often.

NOTE: There is a pitfall here that one can fall into fairly easily. This example of what NOT to do shows how easy this is:
StringBuilder sb = new StringBuilder();
sb.append("Counting: ");
for (int i = 0; i <>
sb.append(" " + i + " ");
}
String test = sb.toString();

In this case, there will be a new StringBuilder created for each iteration through the loop. Do not use "+" concatenation with variables inside the loop (as shown inside the inner append above). This will end up producing another StringBuilder if there is a variable (it is fine if you are using constants). The loss from the creation of many additional StringBuilder objects will offset the gains from using the outer StringBuilder.
sb.append(" " + i + " ");
is efficiently written (when inside a loop) as:
sb.append(" ").append(i).append(" ");

The rule of thumb I use is that if I am concatenating strings on a single line of code then I use "+", otherwise I use a StringBuilder. I sometimes break that rule if I am just concatenating on 2 or 3 lines and the code would be more readable if I used "+" (since the savings are so small in that case).

For those of you runing Java 1.4, the optimization is still there but uses StringBuffer instead.

The key takeaway here is that writing readable and maintainable code should always be a priority over writing optimized code and this is one case where the compiler makes that a little bit easier on the developer.

A few helpful related links for more in depth study:
http://nicklothian.com/blog/2005/06/09/on-java-string-concatenation/
http://java.sun.com/developer/JDCTechTips/2002/tt0305.html
http://www.precisejava.com/javaperf/j2se/StringAndStringBuffer.htm

Saturday, December 15, 2007

Scaling Java with Grids and Clouds

There has been a lot of talk about using grids and spaces to handle large scale java applications at The Spring Experience 2007. The compelling thing about this for me is the idea that these applications allow architects to abstract the concept of clustering. Developers can effectively program as if they are running on a single server (with some minor APIs here and there depending on the solution). Most solutions scale up very well and provide failover support.

These solutions are being recommended as production ready solutions and apparently large companies (like banks) are already using them in massive scale production. It will be interesting to see how these impact Java enterprise development in the future.

Here are some of the solutions I heard mentioned at TSE 2007:
Terracota
OpenSpaces (API/aggregation site)
GigaSpaces XAP
Coherence
ObjectGrid

Friday, December 14, 2007

Spring Dynamic Modules

Adrian Colyer did an incredible demo of Spring OSGi at The Spring Experience on Friday, 14 Dec, 2007 to show the things that are up and coming. The demo included a 3 layer app (web, logic, dao) running with each layer as a separate module in OSGi. During the demo he intentionally shutdown the logic module, clicked a link on the web page, restarted the logic module, and then went back to the page which had magically worked without failing. Apparently Spring OSGi will proxy the layers until a timeout is reached or the module can restart. This allows some amazing things (like upgrades without server shutown). It can also control access to the modules so they can effectively operate in a hierarchy. I think they said it should be released in a few months.

On a side note, Adrian and Rob did a great keynote with some singing and roleplaying. It was easily the best keynote I have ever seen. Cheers to all who planned and executed it.

Thursday, December 13, 2007

The Spring Experience and RSF


I am attending the Spring Expereience in Hollywood, Florida this week (Dec 14, 2007) along with Antranig. It is exciting and scary to be around so many really great developers. It feels like everyone we talk to is doing something impressive and advanced with Spring. It is pretty intimidating to be around and talk to all these really great developers.

The sesssions have been really great so far. It is really hard to decide which sessions to attend because there have been a few slots where I wanted to attend all five talks. The presenters have actually written code in front of people which is great to see. I have had a chance to learn about the new Spring annotations, AspectJ, @MVC, and Testing. The material is advanced but that is what I want.

Antranig and I presented a rapid fire session on RSF. It was a one hour introduction to the goals and basics of RSF. We had a bit of technical trouble and the talk before us ran long but overall I felt really positive about the session. There were lots of good questions and we had about 25 people in the session. We even got a compliment on the wiki and documentation. I hope we will have a chance to talk with more people about RSF during the remainder of the conference.

Tuesday, October 16, 2007

Validating CSS online

I recently had a problem with a webapp where things were showing up but I could not figure out why. The CSS seemed correct at a glance but someone else had written it so I was not very familiar with it. After struggling for awhile I did the smart thing and asked Gonzalo Silverio if he had any ideas (which of course, he did). He pointed me to this awesome CSS validator at http://jigsaw.w3.org/css-validator/. You can upload a css file or simply provide a URL to the file and it will give you a nice little report that tells you what is wrong.
A similar service for validating your markup is available at http://validator.w3.org/.

Sunday, October 07, 2007

Roadmaps, Kernels, and Community?

There has been a bit of traffic on the mailing lists about Kernels and Sakai community processes recently. I personally hope that it will encourage more open communication in the community and more transparency around decision making. I have high hopes that some of the proposals I have posted in the Sakai Dev space on Sakai Confluence will spark conversations and encourage people to get involved in various framework improvement efforts.

In particular, I am hoping the following things are worked out BEFORE or AT the Sakai conference in December.
1) What does the Sakai Roadmap look like?
2) What is a framework team? What does it do? Who are the initial members?
3) How does the voting process work in Sakai and what kinds of things should be voted on?
4) Do we have a Sakai Kernel and if so, what does that mean (what is it)?
5) Should we have a code approval process for the Sakai framework and what should it look like?

What are the right answers to these questions? Frankly, that is outside my core area of expertise which is mostly designing and writing software. I have some proposals out there in the hopes that someone smarter will come along and rip them apart and replace them with something that is better. Even if that doesn't happen, I hope they will at least encourage discussion. Isn't that how we arrive at decisions that will benefit us all as a community? I mean, we don't really need someone to make all the decisions for us and tell us what to do... do we?

Saturday, August 25, 2007

Sakai AppBuilder 0.82 available

Version 0.82 of the Sakai AppBuilder is now available on the Sakai eclipse update site (http://source.sakaiproject.org/appbuilder/update/). This version will now generate maven 1 project files and maven 2 pom files and will create the links to the local repository for the eclipse classpath for either maven 1 or 2. This also includes updates to the most recent jars being used with Sakai including the recent release of RSF 0.7.2RC1.

Friday, August 24, 2007

Cafe trunk using Maven 2

The cafe trunk distribution for Sakai is finally working and available for download. The cafe distribution is part of the Sakai programmers cafe and the primary goal is to create a smaller distribution which makes development in Sakai easy and reduces the memory footprint. This is the first cafe distro available which uses Maven 2 (like the rest of Sakai). The really notable thing about this cafe is that it is the smallest one yet (in terms of projects) and probably the smallest distribution of Sakai currently available.

Ian Boston and I have been tracking down intra-project dependencies in Sakai and removing them in order to create a small and clean kernel for Sakai. This cleanup has resulted in a better kernel and will eventually lead to a more solid service foundation in Sakai. The cafe distribution represents that kernel distribution plus the standard Sakai portal (charon). It should make development in Sakai easier and faster since restarts should take less time and there are less projects to checkout and keep up to date.

In order to make this distribution for Sakai with Maven 2 we had to create a special base pom that lists only the projects in the distribution. We are use svn externals to setup the download of the correct projects from the main Sakai source tree. There is one bit of hackery involved with getting this to work which is needed if only part of the project is desired (for example, we can only use parts of the user project since some parts pull in undesirable dependencies). This involves creating a new base pom for the main project (user) and then removing the modules from that pom file which are not included in the externals. We did this for 4 projects in the cafe distribution.

Saturday, July 28, 2007

RSF Mailing List

Reasonable Server Faces (RSF) now has a mailing list for users! Please sign up for this list if you are using RSF or thinking about using it. More info on joining the list is available on the RSF Wiki but ideally we will use this list to answer questions, send out announcements, and all the normal stuff email lists are good for.

This is not meant to replace the RSF forums but to just supplement them.

Along with this, I am happy to announce the second anniversary of RSF is today (27 July 2007)! Please celebrate in whatever way is appropriate.

Tuesday, July 24, 2007

RSF Wiki refactor and AJAX samples

I have spent many hours over the past couple weeks working on the RSF Wiki. For those who are not familiar with Reasonable Server Faces (RSF), it is an open source Java web framework which is based on the Spring framework and supports pure XHTML templating. The Wiki is the canonical source of documentation for RSF and needed a lot of updates and additions to be more developer friendly. After comparing the websites and Wikis for a lot of open source projects I settled on a set of left hand navigation topic areas and links and then started editing. At this point, the majority of the layout changes are complete and it should be a lot easier for developers to get started with RSF.

Along with the refactoring, I also added pages to describe how to use AJAX in RSF. RSF includes the UVB (or Universal View Bus) which makes calling a method in your application code from AJAX and getting back the results easy. Developers will now find detailed notes about how UVB works and the use of the RSF Javascript library. There is also a new RSF sample app which demonstrate a simple autocomplete using RSF and UVB (this is also available as a sample Sakai tool). I encourage anyone using RSF (and even those who aren't) to check out the Wiki and post feedback.

Monday, July 23, 2007

Using patch (as in .patch files) in WinXP

I had to apply a large set of patch files (and I mean .patch files) to a project today using a Windows XP machine. It was a lot more painful than I thought it would be so I am recording it here in case it helps anyone else avoid the pain.

You should have all your patch files in a directory (I am assuming one called patches but use anything you like, just remember to tweak the commands). You will need the patch program for windows installed (which proved to be fairly hard to find) and on your Path.

How to apply a directory full of patches with a single command in windows XP.
  1. Open a command line (Start ->Run... -> cmd) and go to the directory where you are applying the patches (should have a patches directory here also)
  2. Run the following command to test your patch syntax:
    for %f in (patches\*.patch) do type %f patch -p0 --dry-run
  3. If you got no errors then just remove the --dry-run from the end and run it again to actually apply the patches, for example:
    for %f in (patches\*.patch) do type %f patch -p0

You would not think that would have taken hours to figure out would you?

For the unix folks out there, here is the command (though I suspect most everyone knows this one already):
for patch in patches/*.patch ; do patch --dry-run -p0 < $patch ; done


You might want to go ahead and get the core GnuWin32 utilities if you want to run more unix type commands in winXP.

Sunday, July 22, 2007

Maven2, Continuum, and Sakai

I have been doing a lot with Maven2 these days. I have been adding Maven2 builds to various Sakai tools I am responsible for like Entity Broker and Evaluation. I have also been updating the RSF wiki to include the CARET Maven2 Repository and sample RSF projects that use Maven2. The RSF poms are now updated to run various reports and generate javadocs and Maven 2 sites which are linked to (or will be soon) on the RSF Developer page. I am currently running into an issue with Maven 2 where I cannot generate a site for a project which has a base pom with no code in it and modules which have the code (I get failures in the build when I try to build the site from the base). I'm hoping to get some help from the Maven users list.

Continuum has been an interesting experience in pleasure and pain. The CARET Continuum server (version 1.0.3) is building Sakai trunk, RSF, and all the CARET related tools nightly. It is allowing us to keep track of our projects and know if there are any build problems. Unfortunately, I have managed to create 5 continuum projects which were corrupted and had to be removed by our system admin. On the plus side, I am learning a lot about how Continuum integrates with Maven2 and most of the poms are getting nice updates as a result. Hopefully we will soon have full code reports being generated nightly that we can review as needed.

Monday, May 07, 2007

Tetra Skye meeting

The Tetra group technical meeting was held last week on the Isle of Skye in Scotland. Tetra currently consists of the following UK higher education institutions: UHI Millenium Institute (hosting), University of Cambridge, University of Oxford, and University of Hull. The meeting took place on the Sabhal Mòr Ostaig campus on the Isle of Skye.

The team discussed various Sakai related issues including hierarchical groups, entity system updates, and local projects such as evaluation system and breeze link tool. Possible JISC funded projects and future plans were also discussed.

Evening activities included boating to pubs, skimming stones on the Sound of Sleat, and debating whether wheat is corn. Pictures are available of the event under the flickr tag: tetraskyemay07. There is also a flickr image set created by myself that you are welcome to view. Thanks and Kudos to Sean Mehan of UHI for doing and excellent job of organizing the meeting and evening events.

Thursday, April 19, 2007

South Africa Programmers Cafe

We are currently running the South Africa Programmers Cafe Bootcamp in Potchefstroom, South Africa. The cafe is running from April 16th - April 20th, 2007. There are about 22 people from various South African institutions in attendance. Aaron Zeckoski (me), Antranig Basman, and Tony Atkins are presenting on various topics ranging from Sakai development and RSF to Server configuration and deployment. We have pictures from the cafe available in Flickr if anyone wants to take a look.
The weather has been fairly nice the whole time we have been here. Our hosts from NWU have taken excellent care of everyone and arrangements have been fantastic. Many thanks go to Boeta and Adell at NWU in Potchefstroom for all their hard work in getting things running smoothly and taking care of the roudy and jetlagged presenters. A special thanks and mention here goes to David Horwitz for helping get the funds and generating interest so we could have this event.
Being here in South Africa has been a great experience for me so far and I feel like I have learned a lot about this country in tha past few days here. They have limited internet options available and we currently have to pay for all the data going in and out. The exchange rate has been a nice plus since we get 14 Rand for 1 British Pound (things are quite affordable). I love the South African accent and don't understand a single word of Afrikans but I am having a great time talking to the bootcamp participants and seeing the local area. We will have a chance to see the local area a bit after the cafe ends on friday and I am looking forward to it.