Posted by & filed under Java, Uncategorized.

We often discuss Java limitations on IRC and try to come up with (sometimes silly) workarounds. Unfortunately after time passes it’s often easy to forget the outcome, and lose code snippets. So I thought I’d start blogging some of them so I don’t lose them, and other people might suggest other ways of doing things that we’ve overlooked.

This particular problem occurs when you want to assign the result of a method that can throw an exception to a final variable. For example:

final Customer c;
try
{
	c = getCustomer(id);
} catch (CustomerNotFoundException e)
{
	c = createNewCustomer();
}

This will fail to compile with “variable c might already have been assigned”. Of course making c not final would solve the problem, but that’s no fun.

If we were not using Exceptions, Java provides a useful ternary operator “?:” that lets us do things like:

final Customer c = customerExists(id) ? getCustomer(id) : createNewCustomer();

Which is nice and clean, but means that getCustomer is going to have to return null or throw a RuntimeException in the case that there is no matching customer, which is undesirable. Also customerExists() may be expensive.

We could also possibly use something along the lines of

final Option<Customer> c = getCustomer();

Both of these alternatives, however, require changing the API you’re consuming, and avoiding Exceptions. It would be nice if there was an equivalent of “?:” for try/catch so that you could assign the result to a final variable. The best I can manage in Java is below, can anyone do better?

import java.lang.reflect.ParameterizedType;
 
abstract class TryCatch<T, U extends Exception>
{
	public T value()
	{
		try
		{
			return Try();
		} catch (Exception e)
		{
			if (getTypeOfU().isAssignableFrom(e.getClass()))
			{
				return Catch();
			} else
			{
				throw new RuntimeException(e);
			}
		}
	}
 
	@SuppressWarnings("unchecked")
	private Class<U> getTypeOfU()
	{
		return (Class<U>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1];
	}
 
	public abstract T Try() throws U;
 
	public abstract T Catch();
}
 
//Example
public class Main
{
	private static CustomerRepo repo = new CustomerRepo();
 
	public static void main(String[] args) 
	{
		final Customer c = new TryCatch<Customer, CustomerNotFoundException>()
		{
			public Customer Try() throws CustomerNotFoundException
			{
				System.out.println("in try");
				return repo.getCustomer(1);
			}
 
			public Customer Catch()
			{
				System.out.println("in catch");
				return repo.createCustomer();
			}
		}.value();
	}
 
}
 
class CustomerRepo
{
 
	public Customer getCustomer(int id) throws CustomerNotFoundException
	{
		throw new CustomerNotFoundException();
	}
 
	public Customer createCustomer()
	{
		return new Customer();
	}
}
 
class Customer
{
}
 
class CustomerNotFoundException extends Exception
{
}

In C# we don’t run into the same problem since readonly is much less useful than Java’s final. However, if we wanted to try/catch at the same time we can do a bit better. Here’s an alternative in C#:

using System;
 
public class Example
{
	public static void Main()
	{
		Example t = new Example();
		t.Foo();
	}
 
	public void Foo()
	{
		String result1 = this.Try(() => GetBar(true)).Catch<BarException>(() => "Caught a BarException");
		String result2 = this.Try(() => GetBar(false)).Catch<BarException>(() => "Caught a BarException");
		Console.WriteLine(result1);
		Console.WriteLine(result2);
	}
 
	public String GetBar(bool succeed)
	{
		if (succeed)
			return "Success!";
		else
			throw new BarException();
	}
}
 
public class BarException : Exception {}
 
public class Tryer<TResult>
{
	private readonly Func<TResult> toTry;
	internal Tryer(Func<TResult> toTry)
	{
		this.toTry = toTry;
	}
 
	public TResult Catch<TException>(Func<TResult> whenCaught)
		where TException : Exception
	{
		try
		{
			return toTry();
		} catch (TException)
		{
			return whenCaught();
		}
	}
}
 
namespace System
{
	public static class ProvidesTry
	{
		public static Tryer<TResult> Try<T,TResult>(this T other, Func<TResult> toTry)
		{
			return new Tryer<TResult>(toTry);
		} 
	}
}

Posted by & filed under Uncategorized.

In the last couple of weeks I’ve had some time to work on the software portal project for the first time for a while.

There is now a test snapshot that you can play with if you wish, and help find bugs.

Please do have a play and leave comments here or file a bug report under the Software Portal component.

You’ll need to register to add comments/screenshots/tags etc. Unfortunately since the account you use to login to other openSUSE.org services is controlled by Novell and they provide no openid/saml facility you cannot use the same user account. If you want permissions to edit application details just send me an email (benji at opensuse dot org).

Most of the recent work has been under the hood. Significant speed improvements to the repository indexer and a Webpin compatible webservice that will enable the various clients to continue to work against software portal as they did with webpin until they can be ported to the new api.

There are also some visible changes. The homepage now displays recent comments & top rated applications, and includes the tag cloud.

Software Portal Home Page

Users can now choose a specific vendor for an application for their distribution, as well as installing from the preferred vendor.

Editors can now customise which source packages map to which application.

and which binary packages should be installed by default when a user clicks install.

Posted by & filed under Uncategorized.

This morning we had an unusual amount of snow here, more than we’ve had in my lifetime. Most public transport was suspended and the people attempting to drive were quickly getting stuck and sliding all over the place.

Motorists were making the classic motorist error. They’ve got too much power. I’ve got half a horsepower on this – you don’t want any more than that on snow.

Even Boris Johnson mayor of London apparently cycled to work today.

Also see this comic.

Posted by & filed under openSUSE.

There is a new mailing list for operators on the openSUSE IRC channels – ircops at opensuse dot org.

If you have any queries, suggestions, or complaints regarding an openSUSE IRC channel, please email the list, supplying relevant log excerpts.

For reference, the IRC rules are located on the opensuse-community wiki..

If you are an operator on any openSUSE IRC channel, and have not yet done so – please join the list. The method of subscribing is the same as for any other openSUSE mailing list.

In other IRC related news, the SUSEhelp bot sitting in many openSUSE channels has for some time now supported localisable factoids. Some channels are already using this to provide localised answers to queries such as “SUSEhelp: mp3” and “SUSEhelp: kde4”. If you help in a non-English IRC channel and would find this useful feel free to ask me (benJIman on freenode) how to use it.

Posted by & filed under openSUSE.

Most of the time that I was able to spend actually hacking during hackweek I spent working on the software portal. It was very helpful to be able to discuss issues with Pascal in person.

The software portal project aims to expose the software available for openSUSE and other linux distributions to users as “Applications” rather than “Packages”. The differences between applications and packages are:

  • Applications can be made up of more than one package. e.g. Amarok is split into several packages on openSUSE.
  • There can be several applications in one package. e.g. kdenetwork3 contains both kget and feedbrowser on openSUSE.
  • Applications are not tied to a particular distribution.

The idea is that the user can locate available software by searching or browsing then add screenshots, comments, ratings, tags, translations, etc. The user should also be able to install an application he/she has found without having to know what operating system he/she is running.

Software Portal automates the import of package information from package repositories (rpm-md,deb) and in the future other sources. The import process also maps packages to applications automatically based on the available package metadata a process that can be refined and guided with rules.

Package information is imported from package repositories and mapped to applications. Users can also contribute to applications.

I spent quite some time working on providing a mechanism for users to guide how the repository indexer maps packages to applications, which is quite crucial to allow users to add their own applications and specify which packages make up the application.

I was also very pleased to achieve the goal I set myself before hackweek – to get an instance of the software portal running on a server where people could access it and test it. This has already led to many defects being found and some fixed. I will not publish the URL publicly yet as there are still major problems to fix, we are breaking it fairly regularly, and having lots of people playing with it at this stage would cause problems. Hopefully we can make a test version publicly available before too long.

Some current screenshots:

Software Portal Front Page
Listing applications
Viewing an application

More screenshots are available.

At present there are only two people working on the software portal project. If you are interested in helping either with java development, or with web design, or in any other area, please do join our mailing list, or drop into #opensuse-project on freenode with any questions.

Posted by & filed under openSUSE.

Having now left the University of Warwick, I have lost access to my Warwick hosted blog. So I will now be hosting it myself at http://benjiweber.co.uk/blog.

Last week was hackweek at Novell – during which Novell developers get to work full time on their own personal projects. Novell were kind enough to sponsor several openSUSE community members (including myself) to travel to N├╝rnberg to meet openSUSE community members (some of whom are employed by Novell) in person and participate in hackweek. It was great that so many people from outside Novell were able to take part this time.

My week was a mixture of some useful discussion about openSUSE (I’m not sure who came up with the name “chatweek”, but Andrew seems to have blogged it first at least.) and work on the software portal project (which I will blog about separately). I think it was productive to discuss some of the weaknesses of the openSUSE project, and what can be done to address them. I hope someone else will publish the outcomes of these discussions as I did not take very good notes.

It was also good to talk to people in person about the projects I am involved in (Software Portal and One Click Install) and give and be given suggestions and requests. I was able to talk to Klass about what we would need from the buildservice hermes messaging framework to allow the buildservice to push information about new packages directly to the software portal. It was also good to talk to Garret who has designed a vastly improved (if challenging to realise) user interface for the One Click Install handler.