Posts Tagged Java

Oracle-Sun acquisition: update

Logo Sun/OracleI have already commented on this subject, and nothing much has changed since then… the acquisition is still pending regulatory issues, and Larry Ellison is trying to put pressure on the EU by saying everywhere that every day until the deal closes, he loses 100.000 dollars, a figure that can’t be confirmed of course.

Anyway, a few days ago Oracle updated its web page dedicated to the acquisition, and we can now find there a document named “Oracle and Sun overview and FAQ“. In summary, this is what we can retain from this document regarding Java-related issues:

  • Java: Oracle plans to “accelerate investment on the Java platform”.
  • DBs: Oracle claims it will spend more money then Sun does now developing MySQL.
  • IDEs: JDeveloper, NetBeans and Oracle’s own flavor of Eclipse will coexist… for now.
  • Application servers: Oracle will continue evolving Glassfish as JEE reference implementation, and also work towards “aligning common infrastructure components“ between Glassfish and WebLogic. Does this sound like a step to merging both products?

Other announcements include increased funding for Solaris and SPARC, continued support for virtualization products, and a few other things.

Overall this is obviously meant to be reassuring to everyone: there is no intention to drop any product, most of them will get increased investment, Oracle now has the best hardware and software combination, etc. But until we know what “accelerate investment on the Java platform” exactly means, there’s still a lot of room for speculations.

, ,

No Comments

Snow Leopard, Java 6 and GWT

mac-logoRunning GWT’s hosted mode on the Mac requires a 32 bit JVM, which under OS X 10.5 (Leopard) forces us to use Java 5, because Java 6 only has 64 bit libraries.

Problem: OS X 10.6 (Snow Leopard) removes all versions of Java but version 6. Fortunately, Java 6 now has a 32 bit mode, but it can’t be used straightforwardly for hosted mode because GWT insists on having Java 5…

So how do we get GWT’s hosted mode to run? Alex Moffat suggests an elegant solution:

Lombardi Development Blog » Blog Archive » Snow Leopard, Java 6 and GWT.

, , ,

No Comments

Rant: don’t name interfaces like ISomething

I see a trend lately in Java libraries and code that I thought was only plaguing Microsoft developers: naming interfaces like ISomething. I find this very disturbing so I thought I had to react.

First thing: an interface is a type just like a class. What a type does is mainly to define the set of operations (methods) that are applicable to an instance of a class that implements this type. When dealing with references, the only thing the Java compiler cares about is the type of the reference, so that is knows if a method call is licit or not. It does not care if the type is a class or an instance. Think about the instanceof operator: despite what the awkward name suggests, it works with interfaces as well as classes, because what it actually tests is whether a given reference is of a particular type.

So, why shouldn’t you name your interfaces beginning with the letter I?

  • it breaks a fundamental OO rule, which is that you should let the users of your code know as little as possible about the implementation. If a method returns a object of type Foo, it doesn’t make any difference to the caller of this method if Foo is actually a class or en interface.
  • it is redundant. If you really need to know, the documentation (written or Javadoc), or your IDE’s lookup mechanism, will tell you if it’s an interface or not
  • it makes code harder to read. Which one do you prefer:


IFoo foo = getFoo();
IBar bar = (IBar) foo.getStuff();

or

Foo foo = getFoo();
Bar bar = (Bar) foo.getStuff();

I understand that developpers are often tempted to name interfaces like that because they know there will likely ever be only one implementation of this interface. So they have a Foo class, and it needs to implement an interface, why not name it IFoo?

Two points here:

  • You don’t always need to define an interface for such a class. If you are configuring Spring beans for example, your bean references don’t need to be an interface type. Even if it is good practice to do so, there are cases (such as the one above) where it’s a perfectly valid case to use the class directly.
  • Why not name your interface Foo, and your implementation something else, like DefaultFoo or SimpleFoo or BaseFoo or whatever. Or maybe just Foo, provided it’s not in the same package! If you are using a factory, no-one will ever know or care how the concrete class is named anyway.

Naming interfaces like ISomething is similar to including type name or scope in your variables names. Yuck… Please don’t bring this to Java.

, ,

2 Comments

GWT: keep thinking in Java!

Logo GWTAs a reply to a post on ongwt.com asking about tips for GWT newcomers, someone answered “think in JavaScript, not in Java”. I felt I had to respond, because my experience is totally the opposite, and that’s why I would recommend GWT to anyone who wishes to write AJAX applications while continuing to think in Java (not in JavaScript).

For the record here’s my reply:

“think in JavaScript, not in Java”: I strongly disagree with that!
JavaScript plays the same role for GWT as bytecode does for plain Java: an intermediate language, generated by the compiler, and interpreted by the runtime platform.
As for HTML and CSS, it is true that it helps a lot to know them, especially when the time has come to add styling to your application. This is by design, as explained in the developer’s guide: “instead of attempting to encapsulate UI styling behind a wall of least-common-denominator APIs, GWT provides very few methods directly related to style. Rather, developers are encouraged to define styles in stylesheets that are linked to application code using style names”.
The whole point of GWT is to shield the programmer from JavaScript and let him use the Java idiomatisms and tools he is used to. I’ve been working with GWT for almost 3 years now and I have never ever felt the need to learn JavaScript. And I thank GWT for that!

JavaScript is just a means to achieve the goal, which is: to build a rich web UI. Knowing JavaScript will not help you build a good client, instead you should concentrate on writing clean and robust client code using proven techniques such as MVC, which GWT makes really easy to use (unless you’re using GXT, but that’s another story…)

, ,

4 Comments

Larry Ellison talks about the Sun acquisition…

Logo Sun/Oracle…and does not even mention Java.

I know it’s only part I, but it’s a little scary anyway.

http://www.oracle.com/sun/lje-oracle-sun-faq.pdf

, ,

No Comments

Why Ext-GWT MVC is broken

Introducing Ext-GWT

Our current project is a GWT project, and we’re using Ext-GWT as a widget library and framework.

Ext-GWT (formerly MyGWT) is a “pure” Java/GWT port of the well known Ext-JS JavaScript library, not to be mistaken for gwt-ext, which is a Java/GWT wrapper around Ext-JS… Still following?

In short, gwt-ext requires Ext-JS, while Ext-GWT doesn’t. Ext-GWT is from the same company that makes Ext-JS (appropriatley called Ext), while gwt-ext is an independant work. Also, since version 2.1, Ext-JS removed LGPL from its licensing options, which effectively prevents software like gwt-ext from using it, so it’s stuck with v 2.0.x.

Anyway, this isn’t my point. Ext-GWT in its version 1.2 features a so-called “lightweight” MVC implementation, which we use on our project. Unfortunately, this implementation is quite flawed, and this isn’t harmless because it allows all sorts of problems to arise, that a clean MVC should prevent.

YAAADMVC (Yet another attempt at defining MVC)

First, it’s hard to come by a good explanation of MVC.  according to Wikipedia, it was first described in 1979 by Trygve Reenskaug, and since then it has seen innumerous variations and implementations, some bright, some less bright… Judging by the number of incorrect or plainly wrong explanations you can find on the web, there must be something inherently hard to get in it. However, I’ll have my shot at explaining.

MVC is about roles. It defines three roles, namely Model, View and Controller, and how they interact. Some have interpreted these roles to be software components, which led to MVC frameworks, and some as architectural components, which led for example to so-called MVC web frameworks. Here we concentrate on the software component approach.

So what are those three roles?

  • The model holds the data and provides an interface to manipulate the data.
  • The view is a graphical representation of the model, and possibly also user controls (such as buttons, etc.). It is also able to generate events to any interested listeners, for example to signal a user action (button pressed, etc.)
  • The controller pilots both the view and the model. It reacts to user interaction in the appropriate manner, including possibly updating the model.

This definition might not be universal, and some might disagree on some points, but it is viable.

Some important points:

  • The model does not know anything of views or controllers. Ideally, it is observable and notifies its changes to any interested listeners.
  • The view only has to know its model, at least enough of it to build the display. If the model is observable, the view will register as a change listener and update the display whenever the model changes. The view does not need to know its controller (and indeed you could use a view without a controller if you didn’t need user interaction)
  • The controller listens to events sent by the view, and only the controller should have the intelligence to know what to do when something happens, like a user pressing a button.

Why is this important? because it cleanly separates the responsibility of each component, and we know that leads to code that is more readable, more testable and more maintainable. Less coupling, more consistency: the basis of a good architecture.

Here’s the best diagram I’ve found describing the relationships between MVC components (from http://best-practice-software-engineering.ifs.tuwien.ac.at/patterns/mvc.html):

Relationships between components in a MVC architecture

Relationships between components in a MVC architecture

MVC in Ext-GWT

So let’s have a look at Ext-GWT’s MVC implementation. This is the constructor of class View:

  /**
   * Creates a new view instance.
   *
   * @param controller the parent controller
   */
  public View(Controller controller) {
    this.controller = controller;
  }

Yes, the view knows its controller… Why is it so? Looking further into the source code, we can find the explanation:

  /**
   * Called when a view needs to pass an event to it's controller.
   *
   * @param event the app event
   */
  protected void fireEvent(AppEvent event) {
    Controller c = controller;
    while (c != null) {
      if (c.canHandle(event)) {
        c.handleEvent(event);
      }
      c = c.parent;
    }
  }

So apparently, the view knows its controller only to be able to send events to it… In a correctly designed system, the view should allow the controller to register itself as an event listener, thus eliminating an unnecessary dependency. We can also notice that the knowledge that controllers have a parent, and that the event should be propagated to all ancestor controllers, is all built right there into the view… ouch.

But there’s more. The handleEvent() method in class Controller is public, making it possible for anyone to call it. The problem is that, as we can see above, the handleEvent() method should only be called if canHandle() is true. Making handleEvent() a public method allows anyone to bypass this rule.

We find a different flavour of the same design flaw in method Controller.forwardToView():

  /**
   * Forward an event to a view. Ensures the view is initialized before
   * forwarding the event.
   *
   * @param view the view to forward the event
   * @param event the event to be forwarded
   */
  public static void forwardToView(View view, AppEvent event) {
    if (!view.initialized) {
      view.initialize();
      view.initialized = true;
    }
    view.handleEvent(event);
  }

Here, we can see that the method View.initialize() must be called once before the first call to handleEvent(). But this is enforced only here, so it’s easy to call View.handleEvent() directly and break this rule. If handleEvent() depends on something that is done in initialize(), you can imagine what can happen. You could also call initialize() and not set initialized to true, then it would be called again the next time…

You will also notice that the existence of the Controller.forwardToView() method implies that the controller can forward events to a view. This is clearly not appropriate because what the controller really does is send orders to the view. And since the controller must know its view, it’s best to simply call a method of the view when it needs to alter it.

Of course you could send orders disguised as events, but it has two major drawbacks:

  • it complicates the control flow and makes it very hard to understand
  • it prevents static code analysis and refactoring that is offered by modern IDEs.

In addition, for some reason Ext-GWT event types are not defined as Java enum members, but mere integers. This makes it very difficult to identify an event from just its code, for example when stepping through code.

Bottom line: don’t use Ext-GWT’s MVC

Ext-GWT has a lot of nice things to offer, but clearly its MVC implementation is not one of them. More could be said about it, but I think you get the picture: don’t use it. Write your own clean MVC implementation from scratch, you’ll save yourself a lot of trouble.

, , , , , ,

9 Comments