Archive for the ‘Java’ Category

Java 1.6: Easier Thread Dumps For All

Tuesday, February 13th, 2007

Thread dumps are an invaluable form of debugging, especially something running at a remote site. When you have no idea what is going on inside your application, a thread dump can be a life saver. However, depending on the operating system and method of launching the application, it can be difficult to get a thread dump by the traditional method (SIGQUIT on Unix and Control+Break in the console on Windows). Either it is difficult to find the right Java process (pre-NPTL Linux), it is difficult to find where stdout is going (when running as a service) or it is impossible to hit Control-Break (no console!).

Well, a couple of tools that first arrived in Unix 1.5 JDKs solve this problem. And now, in Java 1.6, they are available on Windows too. Those tools are jps (a ps for Java processes) and jstack (a tool to dump the threads of a specified Java process). Shipped with the JDK, these tools make getting a thread dump child’s play:

jsankey@pal:~$ jps -l
8328 com.zutubi.pulse.command.PulseCtl
5310 org.tanukisoftware.wrapper.WrapperStartStopApp
4306 org.apache.tools.ant.launch.Launcher
8729 sun.tools.jps.Jps
8350 org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner
jsankey@pal:~$

There you have the PIDs for all Java processes running as the current user, with the name of the main class. To dump the threads, pass the PID to jstack:

jsankey@pal:~$ jstack 8328
Attaching to process ID 8328, please wait…
Debugger attached successfully.
Client compiler detected.
JVM version is 1.5.0_08-b03
Thread 8581: (state = BLOCKED)
– sun.misc.Unsafe.park(boolean, long) @bci=0 (Interpreted frame)

Voila! Of course, if you have a graphical environment available, you can get all the goodness of jconsole too.

java.blogs Updates

Friday, February 9th, 2007

Well, if I’m going to whinge when I don’t like changes to java.blogs, then I feel it’s right to also give credit when it’s due. The latest round of updates have addressed the usability issues, and I personally much prefer the new look. So, to the devs at Atlassian: well done, and thanks.

The Rise of OSGi

Friday, January 19th, 2007

There is a lot of talk buzzing around about OSGi, the “dynamic module system for Java”. Most famously, an implementation of OSGi forms the basis of the plugin system in recent versions of Eclipse.

So what is OSGi? If you head to the official website for the answer, you could be forgiven for thinking you had stumbled on a game of buzzword bingo. The terms “universal middleware”, “service oriented” and “component based” show up in the first couple of sentences! Putting marketing-speak aside, however, OSGi turns out to be a mature standard for dynamic modules (read: plugins) for Java. Despite the buzzwords, it is also pleasantly simple at its core (plugins are just Jar files with metadata in the manifest). If you’re looking to create a plugin-based application, or to add a plugin system to your existing application, then you should look into OSGi.

In our case, we are adding a plugin system to the next major version of Pulse. We have decided to go with OSGi for a variety of reasons:

  • A robust standard for the basic requirements of a plugin system: such as plugin metadata, dependencies, classloading, service management, dynamic loading/unloading and so on. Why reinvent a less complete system?
  • A simple core: OSGi plugins (known as bundles) are just plain old Jar files with some metadata. This simplicity will make our life much easier.
  • Existing open source implementations: we have chosen the Eclipse foundation’s Equinox implementation, but there are others such as Felix, Knopflerfish and Oscar.
  • Eclipse tooling support: as Eclipse uses OSGi under the covers, the Eclipse Plugin Development Environment has tool support for creating and managing OSGi bundles. One of our goals is to make the lives of plugin authors as simple as possible: tool support helps achieve this.

Of course, it is not all good all of the time. There are some challenges we face along the way:

  • Lack of documentation: it is seriously difficult to find good documentation about OSGi and the implementations, apart from the standard itself (which happily is reasonably readable). The saving grace is having the Equinox code and a helpful development community.
  • Embedding Equinox: the Equinox framework, as part of the Eclipse project, has a certain bias towards being used as part of the larger Eclipse platform (and Rich Client Platform). This is changing over time, but we are finding some interesting challenges embedding Equinox in Pulse.
  • Impedence mismatches between Pulse and OSGi: at times the default OSGi behaviour is at odds with how we would like plugins to act in Pulse. For example, OSGi mandates that the state of plugins is persisted by the framework, whereas we want Pulse itself to have control over their state.

As we continue the implementation of a plugin system for Pulse, I’ll post more about the particular things we learn along the way. Hopefully this information will help fill the documentation black hole, particularly in a new area for Equinox: embedding in a standalone application.

How Not to Use “AJAX”

Thursday, January 11th, 2007

Once a technology reaches buzzword-compliance, it starts to pop up in the least appropriate of circumstances. The current manifestation of this is the “improvement” of favourites management on javablogs.

Let’s review favourites as they were. You have the ability to add a chosen blog to your “favourites” list. You can also choose to ignore a blog, so that entries from that blog are hidden. To do this previously, you simply clicked on a “+” or “x” icon next to the header of any entry from the blog:

Simple, and effective. Now, swept up in the hype of AJAX, the mechanism for managing favourites has been updated. The same “+” and “x” icons still appear, but they no longer take direct effect. Instead, they pop up a dialog:

Wow, a nice piece of eye candy that serves no purpose whatsoever. The dialog gives exactly the same options as were available before. The text description of the actions could more appropriately be added as tooltips for the icons. To add insult to injury, you can’t even click on the text, but only the icons on the left. I can see the tagline now:

“Favourites management: Now with extra click!” :)

Strictly speaking, the dialog itself is not AJAX at all, just plain old Javascript/DHTML. The adding of favourites, though, does happen without a full refresh, which is an improvement.

Now, javablogs is not alone in this gratuitous use of new technology, it just happened to be right in my face today. Please people, remember: the goodness of AJAX (and more use of Javascript/DHTML) is in improving usability. Think before you code, and keep your AJAX tasteful.

Java 1.6: Finally Integrating With The Underlying Platform?

Friday, November 3rd, 2006

We all know the Java promise, Write Once Run Anywhere. And we all know some things are too good to be true. On the one hand, there are those features that don’t quite work the same on different platforms. That’s just a fact of life in portable programming, which I can deal with. On the other hand, there is the lowest common denominator syndrome. Important features that are only available on some platforms are non-existent in the Java APIs. The excuse for the absence of such APIs is, of course, that they can’t be implemented on all platforms. This I can’t stand1. I don’t expect every platform-specific API to be exposed, but some are just too important to ignore. Unfortunately for Java programmers, Sun has managed to ignore several for quite some time, for example:

  • Unix style file permissions: applications that work with Files (yes, there are a few of them) often need access to the read/write/execute permissions. I have seen (and written) plenty of ugly workarounds to use the stat/chmod commands to achieve the desired result. A request for this functionality can be found in the bug database from as early as 1997. Sure, the permission systems are different on other platforms (notably Windows), but you can cover a huge percentage of use cases with a small amount of work. Finally, in Mustang, we have the ability to get and set the permissions.
  • Free disk space: those applications working with the file system may also want to make sure they don’t fill it. It is a lot easier to prevent disk exhaustion than to recover from it. Looking back, poor Java developers have been hoping for this ability since 1997. Finally, Mustang delivers.
  • Masked command line input: the primitive Java APIs for command line interaction make it near impossible to create a usable command line interface. An example of something simple which you cannot do without crazy workarounds is prompt for masked input, e.g. to get a password. yep, you guessed it, there is a request from way back in 1997. Finally, in Mustang, we have the new java.io.Console class that provides a readPassword method.
  • System tray icons: many applications that wish to integrate with popular desktops on both Windows and Unix need the ability to use the system tray. It’s an expected part of the user’s desktop experience. The earliest request I found for this one is from 2000. The nice part is the Mustang support grew from an external project, so in this case the users were a bit more empowered.

It’s quite sad that Java developers have had to wait a decade for such commonly-required and relatively simple APIs. No wonder Java has had trouble getting a foothold on the desktop when it has been so difficult to make an application that integrates well with the underlying platform. I just hope the arrival of this new functionality in Mustang is a sign of a new attitude towards such features. No more hiding behind the platform independence argument, time to just get on and give people the tools they need to get the job done.


1: It’s worth noting that this sort of attitude is not restricted to the Java community. I can’t count the number of times that I have seen people in the C++ community reject ideas for additions to the standard library because “C++ runs on platforms that don’t even have a file system”. Who cares when 90% of C++ code runs on systems that could use this functionality!


Interested in automated builds? Pulse 1.2 is now in Early Access Release. Check it out!

SQL schema upgrades a thing of the past?

Tuesday, October 17th, 2006

I would like to draw your attention to the recent release of the new Java Persistence API for the Berkeley DB. In short, the Berkeley DB is designed to be a very efficient embedded database. With no SQL query support, you instead talk directly to its internal BTree via a very simple API. A very good write up is available at the server side for those interested in the full details.

This style of persistence mechanism is certainly not for everyone. If you want adhoc query support for writing reports and extracting data, look elsewhere.

If you don’t, then Berkeley should be considered when defining the persistence architecture of your next project, just don’t tell your DBAs. Not only is it reportedly very fast, due largely to the lack of SQL style interface, but it also supports transactions, hot backups and hot failover, all of the things that help you sleep at night. However, what has me intrigued is idea of not having to deal with SQL schema migration.

I consider Schema migration to be one of the more tedious and yet non-trivial tasks that is required by any application that employs relational persistence. Yes, Hibernate makes this task somewhat easier to deal with. However, even with Hibernate, you will still need to roll up your sleeves and write some SQL to handle the migration of the data.

Managing schema migration within the Berkeley DB is different. Where as previously you extracted the data via SQL, converted it and then updated the DB via SQL, with Berkeley, you just convert the data in Plain Old Java. They have some examples in there javadoc that gives a reasonable idea of what is involved. Below is one of these examples, a case where the Person object’s address field is split out into a new Address object with 4 fields. The core of the work is done by the convert method:

public Object convert(Object fromValue) {

    // Parse the old address and populate the new address fields
     
    String oldAddress = (String) fromValue;
    Map<String,Object> addressValues =
                                  new HashMap<String,Object>();
    addressValues.put(“street”, parseStreet(oldAddress));
    addressValues.put(“city”, parseCity(oldAddress));
    addressValues.put(“state”, parseState(oldAddress));
    addressValues.put(“zipCode”, parseZipCode(oldAddress));

    // Return new raw Address object
     
    return new RawObject(addressType, addressValues, null);
}

Personally, I think this is a great improvement. Now, if only I had been aware of this at the start of this project, things might be a little different, and faster, and some other good stuff as well.

So are schema upgrades a thing of the past? Maybe not, but they don’t have to be a part of every project.

Annotation Patterns: The Meta Squared Pattern

Monday, October 9th, 2006

Over the past couple of weeks, I have been working on adding plugin support to pulse. One of the primary requirements is to provide as much functionality out of the box as possible, allowing plugin writers to focus on the features, not support code. For example, if a plugin requires configuration, then the plugin developer should define what input is required, what validation rules should be applied, but not have to worry about the details of rendering and validating a web form. But more on that later.

Whilst doing some research into what was already being done, I came across an approach to using annotations that I have since found very useful. For reference, the project was the excellent trails project from Brian Topping.

The approach centres around defining the annotation, and annotating the annotation with a reference to its handler.

@Constraint(RequiredValidator.class)
public @interface Required
{
}

In the code example above, you can see that the Required annotation has a reference to the RequiredValidator, its handler.

By adding a simple annotation processor that searches for annotated annotations and executes the handler, you have the basis of a very flexible meta data processing facility. The core of the flexibility here is that a custom annotation does not need to be registered with the processor. The information normally conveyed by the registration process is embedded in the annotation.

Lets see how this works in practice by applying it to the validation domain by implementing custom Name validation rules.

Firstly, the validator:

public class NameValidator extends RegexValidator
{
  public NameValidator()
  {
    setPattern(“[a-zA-Z0-9][-a-zA-Z0-9_. ]*”);
  }
}

Then the annotation:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(NameValidator.class)
public @interface Name
{
}

And finally, we apply it to our business object:

@Name
  public void setMyName(String name)
  {
    this.name = name;
  }

The core of the annotation processor would look like:

private void processConstraints(Method method, Object obj)
{
  for (Annotation annotation : method.getAnnotations())
  {
    Constraint constraint = annotation.annotationType().
                     getAnnotation(Constraint.class);
    if (constraint != null)
    {
      Class constraintHandlerClass = constraint.value()
      // we now have the handler, now we just need to
      // apply it to the instance being validated.
    }
  }
}

private boolean isConstraint(Annotation annotation)
{
  return annotation.annotationType().
           getAnnotation(Constraint.class) != null;
}

We are also applying this same technique to the plugin frameworks’ automatic form generation, and anywhere else where we want plugin authors to be able to use annotations to customise the default behaviour of the framework.

JUnit V TestNG: Managing external dependencies

Wednesday, August 30th, 2006

We have many components in our product that rely on external packages being available before tests will pass. For example, our make integration requires that make is installed, and our cvs integration tests require the cvs test server to be reachable.

The problem is that some of these resources will not always available on the box running the tests. This is often the case when we are running a build on a new box / environment for the first time. What would be very nice is if the test framework could warn me if a resource is not available, and only run the tests that have a chance of succeeding, ie, those where the required resources were available.

JUnit

Since we use JUnit for testing, I had a look to see what I could do to solve this issue.

The simplest solution is to Assert.fail during the setup/before method if the resource is not available:

@Before public void checkResource()
{
  if (isResourceNotAvailable())
  {
    Assert.fail(“resource not available.”);
  }
}

This results in a:

java.lang.AssertionError: resource not available.
        at org.junit.Assert.fail(Assert.java:69)
        at junit4.DependencyTest.checkResource(
        at sun.reflect.NativeMethodAccessorImpl.i
        at sun.reflect.NativeMethodAccessorImpl.i
        at sun.reflect.DelegatingMethodAccessorI
        at org.junit.internal.runners.BeforeAndAfte
        at org.junit.internal.runners.BeforeAndAfte
        at org.junit.internal.runners.BeforeAndAfte
        at org.junit.internal.runners.TestMethodRu
        at org.junit.internal.runners.TestMethodRu
        at org.junit.internal.runners.TestClassMeth
        at org.junit.internal.runners.TestClassMeth
        at org.junit.internal.runners.TestClassRunn
        at org.junit.internal.runners.BeforeAndAfte
        at org.junit.internal.runners.TestClassRunn
        at junit.framework.JUnit4TestAdapter.run(

for each test that requires the unavailable resource. This is certainly effective, but a little crude, resulting in a lot of meaningless strack traces when all I need to know is that X is not available.

An alternative, in JUnit3 at least, would be to implement a custom TestDecorator that would only execute the tests it wraps if the required resources are available.

pulbic class CheckResourceDecorator extends TestDecorator
{
  public CheckResourceDecorator(ResourceAwareTest test)
  {
    super(test);
  }

  public void run(final TestResult result) {
    Protectable p= new Protectable() {
      public void protect() throws Exception {
        if (isResourceAvailable())
        {
          basicRun(result);
        }
        else
        {
          System.err.println(“resource not available.”);
        }
      }
    };
    result.runProtected(this, p);
  }

  private boolean isResourceAvailable()
  {
    return ((ResourceAwareTest)getTest()).isResourceAvailable();
  }
}

public interface ResourceAwareTest extends Test
{
  public boolean isResourceAvailable();
}

This has the advantage of avoiding the stack traces, and allows us to control the number of error messages printed. This is certainly not as noisy as the first approach, but now we need to wrap all of our tests in this decorator, and does not seem to work well with the JUnit 4 suites.

TestNG

I have heard some good things about TestNG lately, so how does it handle this problem?

TestNG supports the concept of test dependencies. That is, if testB depends on testA, then testB will not run until testA has run successfully.

So, what we can do is define a method to check for a resource, and make tests that require that resource dependent on that method.

@Test public void checkResourceX()
{
  // if resource x is not available, then fail.
  Assert.fail(“resource X not found.”);
}

@Test(dependsOnMethods = {“checkResourceX”})
public void testThatRequiresResourceX()
{
    // run the tests.
}

The result is that if the resource is not available, you get a single failure with details “resource X not found”, with tests that depend on that resource will be marked as skipped. That is pretty clean outcome. We get no duplicate failures and a short error message:

java.lang.AssertionError: resource X not found.
        at org.testng.Assert.fail(Assert.java:76)
        at testng.dependency.DependencyTest

Short and definately Sweet.

Conclusion

Given these results, I certainly prefer the TestNG solution. The output is clear and the association of dependencies to tests is defined with each test case and on a per test basis, not on the per class basis as with JUnit. Now if only I was using TestNG :) . Maybe it is time to run the JUnitToTestNGConverter?

I am curious to know if other people run into this issue? And if so, how do you solve it? Maybe you do one of the above, or have written custom annotations or extended JUnit. Please let me know, I would love to hear about it.

Incremental schema upgrades using Hibernate

Monday, August 28th, 2006

I have been inspired by recent discussions on upgrade frameworks to show how hibernate can be used to provide simple incremental database schema maintenance. Database schema maintenance is one of the more difficult aspects of upgrading applications, particularly when the application supports multiple databases, so I am very happy that hibernate helps out during upgrades.

SchemaUpdate

Hibernate provides a class called SchemaUpdate that is able to synchronise a set of hibernate mappings with a database schema. The following code snippet shows how easy it is:

// manually setup the hibernate configuration
Configuration config = new Configuration();

Properties props = new Properties();
props.put(“hibernate.dialect”, “org.hibernate.dialect.HSQLDialect”);
props.put(“hibernate.connection.provider_class”,
 “com.zutubi.pulse.upgrade.tasks.UpgradeTaskConnectionProvider”);

// slight hack to provide hibernate with access to
// the configured datasource via a static variable
// on our ConnectionProvider implementation.
UpgradeTaskConnectionProvider.dataSource = dataSource;

// use spring to help load the classpath resources.
for (String mapping : mappings)
{
  ClassPathResource resource =
                new ClassPathResource(mapping);
  config.addInputStream(resource.getInputStream());
}

// run the schema update.
new SchemaUpdate(config, props).execute(true, true);

This example uses the spring ClassPathResource to load the mappings file from the classpath, and the UpgradeTaskConnectionProvider to inject a datasource into the process.

.hbm.xml fragments

This by itself is not overly interesting. What people usually do not realise is that the mappings files do not need to hold your entire schema. When making incremental changes to your schema, all you need in the mappings are those incremental changes. This comes in very handy when you have lots of mappings to manage.

For example. You have the following mapping of a user:

<class name=“com.zutubi.pulse.model.User” table=“USER”>

    <id name=“id” type=“java.lang.Long” column=“ID”>
        <generator class=“hilo”/>
    </id>

    <property name=“login” column=“LOGIN” type=“string”/>
    <property name=“name” column=“NAME” type=“string”/>

</class>

Some time later, you want to store a password field with this user. By passing the following mapping to the SchemaUpdate, it will add that column to your existing table, leaving the existing schema as it is.

<class name=“com.zutubi.pulse.model.User” table=“USER”>

  <id name=“id” type=“java.lang.Long” column=“ID”>
    <generator class=“hilo”/>
  </id>
       
  <property name=“pass” column=“PASS” type=“string”/>

</class>

You still need to ensure that the mapping file is valid, hence the inclusion of the ID field in the second mapping.

Versioning

So, to support incremental schema upgrades within your application, you will need to keep two sets of hibernate mapping files. The first will be the latest version of your mappings. This is what is used for new installations. The second will be a set of versioned mapping fragments as described above.

You will need to version them so that you can track which fragments you need to apply and in which order, based on the version of the schema you are upgrading from. I use directory names like build_010101 to store my schema fragments and a properties file to store the current schema version. Other people use a special table in the database to hold the current schema version. Use which ever is most appropriate to your situation.

Generating upgrade SQL

For those of you that do not want or can not allow Hibernate to run the schema update, you can use the following code to generate the SQL that Hibernate would otherwise execute:

Dialect dialect = Dialect.getDialect(props);
Connection connection = dataSource.getConnection();
DatabaseMetadata meta =
    new DatabaseMetadata(connection, dialect);
String[] createSQL =
    config.generateSchemaUpdateScript(dialect, meta);

This code would replace the last line in the first example.

Things to remember about SchemaUpdate

Okay, so just a couple of final things to be aware of with hibernates schema update.

The hibernate schema update will:

  • create a new table
  • add a new column

The hibernate schema update will not:

  • drop a table
  • drop a column
  • change a constraint on a column
  • add a column with a not-null constraint to an existing table

Final tip

Oh, and the class name that you provide in the update mapping can be anything you want. It is not checked, which is great, otherwise you would need to handle versioning of your class files as well.

Happy upgrading!

If Java Could Have Just One C++ Feature…

Thursday, August 3rd, 2006

I have been immersed in Java for a while now, but having worked in C++ for years before, there is one big thing I miss: destructors. Especially in a language with exceptions, destructors are a massive time and error saver for resource management.

Having garbage collection is nice and all, but the fact is that we deal a multitude of resources and need to collect them all. How do we do this in Java? The Hard Way: we need to know that streams, database connections etc need to be closed, and we need to explicitly close them:

FileInputStream f = new FileInputStream(“somefile”);
// Do some stuff.
f.close();

Of course, with exceptions it gets worse. We need to guarantee that the stream is closed even if an exception is thrown, leading to the oft-seen pattern:

FileInputStream f = null;
try
{
    // Do some stuff
}
finally
{
    if(f != null)
    {
        try
        {
            f.close();
        }
        catch(IOException e)
        {
            // Frankly, my dear…
        }
    }
}

The noise is just incredible. A common way to reduce the noise is to use a utility function to do the null check and close, but noise still remains. Repeating the same try/finally pattern everywhere is also mind-numbing, and it can be easily forgotten leading to incorrect code.

In C++, this problem is solved elegantly using the Resource Acquisition Is Initialisation (RAII) pattern. This pattern dictates that resources should be acquired in a constructor and disposed of in the corresponding destructor. Combined with the deterministic destruction semantics for objects placed on the stack, this pattern removes the need for manual cleanup and with it the possbility of mistakes:

{
    std::ifstream f(“somefile”);
    // Do some stuff
}

Where has all the cleanup gone? It is where it should be: in the destructor for std::ifstream. The destructor is called automatically when the object goes out of scope (even if the block is exited due to an uncaught exception). The ability to create value types and place them on the stack is a more general advantage of C++, but Java can close the gap with smarter compilers1.

Interestingly, C# comes in half way between Java and C++ on this matter. In C#, you can employ a using statement to ensure cleanup occurs:

using (TextReader r = File.OpenText(“log.txt”)) {
    // Do some stuff
}

In this case the resource type must implement System.IDisposable, and IDispose is guaranteed to be called on the object at the end of the using statement. The using statement in C# is pure syntactic sugar for the try/finally pattern we bash out in Java every day.

What’s the answer for Java?2 Well, something similar to using would be a good start, but I do feel like we should be able to do better. If we’re going to add sugar why not let us define our own with a full-blown macro system? Difficult yes, but perhaps easier than always playing catch up? An alternative is to try and retrofit destructors into the language3. It is possible to mix both garbage collection and destructors, as shown in C++/CLI4. However, I don’t see an elegant way to do so that improves upon what using brings. If you do, then let us all know!


1 it appears that Mustang already has some of the smarts such as escape analysis.
2 if you’re the one down the back who shouted “finalizers”: you can leave anytime you want as long as it’s now!
3 I said NOW!
4See also Herb Suttor’s excellent post on the topic Destructors vs. GC? Destructors + GC!.