Archive for the ‘Java’ Category

Agent Smith: scripting languages may not be the answer

Saturday, March 10th, 2007

This is the first of a series of articles about Agent Smith. I will begin with an introduction, followed by a “try it, test it how-to”, ending with some examples together with Echo2, Struts and Spring.

As you may know, I am a bit involved with Echo2. Really, I’m a fan of it, I love the way I create ajax UIs coding the swing way. And forgetting the page thing.

But Echo2 has two problems: it consumes a bit of memory (but I don’t really care, since my apps are not intended to be delivered to the world “wild” web) and, if you don’t use EchoStudio, it needs you to reload the webapp to check even small UI changes (obliging you to a constant code-restart-code).

Well, the second problem has been solved. Its solution is called Agent Smith.

Why “agent” ? In java, there are many meanings of the word “agent”. One meaning is a class that has access to the Instrumentation interface, introduced since java 5. With Instrumentation you can call a wonderful method:

void redefineClasses(ClassDefinition[] definitions)

With such method you can change the definition of an entire class, while your program is running. If you have ever played with ClassLoader (creating your own implementations) you know what the method “defineClass” does and you know it is final.
Instrumentation can be used as a workaround to the problem of class redefinition, meaning you can actually use java as a scripting language.

So why not just use Groovy or some other scripting language ?
Because Groovy is slow. Because scripting languages are usually slow. Test Groovy. I found it 18 to 20 times slower that “native” java code.

Java has been criticized by every Microsoft developer for being slow. And they were right. Were. They are no more. Why the heck should I use Groovy to code? Would you shoot at your feet?

But don’t take my humble opinion as a “scripting languages are the wrong way” opinion. Groovy in particular has a lot of nice features (especially the syntax) but, since I’ve read articles about using groovy to speed up Struts actions building, you may find an agent a smarter and cleaner solution.

I think there are times when we really need to avoid app server restart. Think about your JEE application running into JBoss, about your JSF backing bean…
Scripting languages allow us to avoid server (or app) restart, but they also add weight to your code, making it slower.

An agent is a bit hard to set up and so it may not be a good solution. But if you are planning to work on the same project for the following two weeks, then it is.

In the next article I will explain how to compile and set up Smith.

See you soon.

It’s over, or it’s just begun

Friday, March 2nd, 2007

Today was my last day in Concept. After 1 year and a half, my very first java experience, I’ve stopped being part of the staff.
I hope I’ve done well, but I’m quite sure I’ve chosen the right way. Not really because I’ve brought (learned and taught) Echo2.
Because I’ve had the opportunity to learn and share a lot of things, things that made the company switch to AJAX and TDD.

After almost 6 years spent in Milan, tomorrow I’ll go back home, Turin, where I have a couple of things to do.

First, a flat. I’ll sleep in my parents’ house for some weeks but I need to get a flat.
Second, a new job. Something that could allow me to start being a one-man-company. I want to keep on working together with Concept, I want to closely follow some open source projects I’m interested in and I need a job for a living. Want, want, need. Uhmm… :/
Third, helping my friends switch to GNU/Linux.
Fourth, getting a life. It’s been quite hard since today. Things have to change.

Wish me luck, something new is going to start here.

Echo2: JavaJournal interview, translated

Monday, January 15th, 2007

As I’ve promised, here is the translation of my interview by JavaJournal, the italian java magazine.

Hope you’ll enjoy it.

  1. Hello Federico, would you like to introduce yourself to JJ readers?
    I’m a posse about information technology and I’ve managed to port the passion to a job. I’ve been working as a programmer since 6 years ago, 1 year and a half coding with Java.
    I’ve done things like airline ticket booking, questionnaire management and gps vehicle tracking.I’m currently working with Concept, a promising Italian company, and the next month I’ll go back to Turin, my birth city, where I’m planning to start a new chapter.
  2. What do you think about the emphasis given to Ajax?
    The buzzword “Ajax” is due to a set of terrific components and applications that suddenly have made the browsers doing things never seen before. Since GMail and Google Suggest, the buzzword is in the mouths of half of the IT world.Too bad many developers are missing the point about Ajax, that is, per se, just a way to communicate with a remote server or service: all the cool components and effects are a matter of DOM and CSS manipulation. Once you understand the reality of things, you also see a lot of troubles, particularly regarding the DOM and JavaScript.So it is necessary, since a certain difficulty level, to switch to a tool that will take care of such troubles, relieving the developer of them. A couple of nice tools are GWT, Openlaszlo, ZK and Echo.
  3. How do you compare it to other solutions such as java applets (that seems to discover a new birth) or flash with Openlaszlo or Flex?
    All these solutions produce the same final result: Rich Internet Applications, client-server applications with minimal requirements on the client computer and with characteristics of reactivity, behavior and interaction much more similar to desktop applications than to web ones.
    So you need to evaluate the importance of the following three aspects:

    • the configuration of the client computers that will host your application
    • the architecture each solution bounds you to
    • the current knowledge of your dev team
  4. What’s your favorite Ajax framework?
    Echo2, to me and to my team, has given the best answers to the questions that the three aspects above have arisen.
  5. What do you like the most about Echo2 compared to other solutions such as Google Web Toolkit?
    The only real difference between GWT and Echo2 is architectural: GWT is client-side, while Echo2 is server-side.With GWT, you implement your GUI using Java, then you translate it to JavaScript with the GWT compiler and finally it is released to each client that connects to your server.
    That means releasing the GUI code to the client and interact with the server through RPC.Echo2 runs on your server: the client receives the DOM updates therefore updating the GUI appearance.
    That means maintaining the application state on the server (session) and be bound to a servlet container (and therefore Java).

    A server-side solution obliges you to keep an eye open on memory consumption but it also gives you a security layer for free, due to the session. Echo2 adds to that a synchronization mechanism that, given a valid session, does not allow you to “press a button” currently invisible to that user. With client-side solutions you need to code such checks manually.

    Finally, Echo2 implements a Swing-like API that can flat down the learning curve.

  6. Is it really like coding with Swing? Can a complex Swing application be mapped to Echo2?
    It is very similar but the API is a subset of the original Swing one. It is like a port of the Swing API, adapted to the web context.
    We’ll find classes with (almost) the same names and (almost) the same interfaces but we’ll miss some fundamentals elements such as the LayoutManager, as Echo2 does not implement it (you’ll code like using the FlowLayout).Therefore it would be difficult to map a Swing application to Echo2: Swing is just too rich.
  7. Is it possible to do the contrary: starting from an Echo2 application, transform it to a standalone client, eventually launching it with WebStart?
    Theoretically, yes. Echo2 starts with this very idea: to keep the widgets jar uncoupled from the jar that contains the info to render them.
    It shouldn’t be hard to implement a mapping, although, as far as I know, there isn’t yet a project with such aim.
  8. Echo2 is open source but there’s a commercial editor for rapid application development. Have you tried it? What do you think?
    EchoStudio is an Eclipse plugin and it wants to help the developer composing the GUI. It is useful for fast GUI prototyping and when you to work together with a GUI designer. Personally, I prefer to write the code.
  9. How is the community? Is it big and active?
    Ouch, this question hurts! Unfortunately Nextapp (Echo creators and maintainers) has set up just a forum to let users and developers keep in touch. I would have left the forum to the newbies and have created a mailing list for developers and contributors along with a bugzilla, to let everyone follow the course of bugs and contributed patches.Between the community, the discontent is arising and probably, on the half of January, some users will set up a project that will have the aim of supporting advanced users and, following the original code base while applying contributed patches, release an experimental version of Echo2, in order to push Nextapp to add the requested features to their code base.
  10. I’ve showed the Echo2 demo to one of my customers, and he said: “Ezio: am I wrong, or the demo you showed me is a bit slow?” What will you answer?
    If that demo was the one of the Nextapp site, yes IT IS slow! It is due mainly to the number of images they have loaded (even if that results in a really eye-candy widgets tour) and to the load the server has to handle for each user.As said, Echo2 stores the application state at the session level. Every user will have its place in the server memory.
    That leads me to suggest to avoid using Echo2 on public applications, for a simple reason: it is difficult to know how many users will use it. If you’ll be linked by Slashdot or Punto-Informatico you’ll probably see your server going down.
    The ideal use of Echo2, for what I’ve said before, particularly regarding security, is for creating backoffices: even if you plan to have tens of thousands of users, you have numbers, so you can react accordingly, avoiding any danger.
  11. How invasive is Echo2? Can it be measured and someway managed?
    A “Hello World” with Echo2 takes about 8k for each user. This is the initial cost, the overhead.
    As long as you add components to your GUI, memory consumptions grows progressively but the growing is slower.Anyway you also have to add the cost of the data the components are containing: if it is true that, for example, the table component will be stored at the session level, the data it is displaying will go to the session too.To have a measure of it, you can serialize the session on a file and check its size.

    As far as I’ve seen till now, an average complex application takes about 30 Kb for each user.

  12. Talking about productivity, how is developing with Echo2?
    I’ve been working with Echo2 for 5 months and I’ve created a couple of support classes to speed the development up: therefore, creating a plain CRUD interface, takes me something like 10 minutes.In the daily activity, writing the code by hand, without an RAD like EchoStudio, obliges you to reload the webapp context at every modification. EchoStudio, that eliminates such issue, actually does the same, by programmatically reloading an embedded version of Jetty.So, at the productivity level, developing with Echo2 is much like developing JSPs, with the great advantage of being able to test our views (the “v” in MVC) and to inherit from our previously created ones.

    Together with the quality of the final result, Echo2 is a tasty solution, surely worth an evaluation.

Echo2: interviewed

Monday, January 15th, 2007

I’ve been interviewed about Echo2 by Michele Sciabarrà from JavaJournal.

If you understand italian, you can read it here; otherwise, please wait until tomorrow, when I will post the translation on my blog.

Quite excited, I must admit; even a bit embarrassed ;)

Tod is alive!

Wednesday, January 3rd, 2007

Some users from the Echo2 community forums have been complaining about the lack of presence from Tod Liebeck, developer and maintainer of Echo.

As I’ve supposed, Tod have been completely caught by his company projects.

Now Tod has posted a status update about Echo 2.1 (the RC is coming! at last) and 2.2, that promises to be faster than the current 2.1.

We are all looking forward to them, Tod! Welcome back!!

Echo2: how to log users out and close their session

Tuesday, December 26th, 2006

The nicest thing about Echo is that is abstracts you from the “page” thing and makes you quickly forgot that you are actually inside a web environment.

Anyway you can’t forget your environment as it influences your design and code decisions.

This is the case of session invalidation and logout actions. I’ve written a small page on the nextapp wiki and I want to propose it here for the search engines’ sake :)
So: how do you log your users out and invalidate their sessions? As always, it depends on what you need to achieve.

The easy and quick way
As you know, Echo2 applications state is synchronized with the model stored into each user’s session. So, if you just want to force the user to start from the beginning, changing the screen will be sufficient

ApplicationInstance.getActive().getDefaultWindow().setContent(new WelcomeMessageScreen());

In no way the user will be able to interact with previously rendered components, so security is safe.

Advantage:

  • quick: you could already have a “welcome” screen, so “closing” your application is just the line above

Disadvantage:

  • the session is still open

The servlet way
If the easy way is not enough because you want to invalidate user’s session as soon as the user logs out, you may implement a short servlet.

public class LogoutServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getSession().invalidate();
response.sendRedirect(request.getContextPath() + “/”);
}
}

Note: you can’t invalidate the session INSIDE the Echo application, because that will cause a “session expired” message; you need to invalidate the session outside the application. Although it may look weird, it’s actually better as your application SHOULD NOT know in any way that it is running inside an “servlet container” (because Echo abstracts you from that). Having a “Logout Servlet” does not solve the problem, but isolates the session invalidation logic into a known, separate place.

Advantage:

  • more control: session is invalidated programmatically.

Disadvantage:

  • the above code may look reusable, but what if you need to dispose other resources as well? Read on

The servlet + session listener way
Suppose you also need to log how long a session lasted and/or to dispose other resources and/or to do something else when a user logs in and out.
Then you need to implement the interface !HttpSessionListener and to register it into your servlet container.
An empty one looks like:

public class MySessionListener implements HttpSessionListener {
private static final Log log = LogFactory.getLog(SessionListener.class);

public void sessionCreated(HttpSessionEvent arg0) {
log.debug(”new session created”);
}

public void sessionDestroyed(HttpSessionEvent arg0) {
log.debug(”a session was destroyed”);
}

}

Code the two methods according to your needs. As you may have noticed, the “logout servlet” may be reused quite easily, while the above listener may be more specific to your application.

Advantage:

  • total control: forget for a while the beauty of Echo abstracting you from the web thing and brush up your servlet knowledge

Disadvantage:

  • more code to write and maintain

I usually go with the third method, leaving the listener empty until I need it. The cold face of the hammer of gold looks at me, but I don’t care about such a tiny piece of code

Echo2: impressive!

Sunday, December 17th, 2006

As you may know, I had a talk at JUGMilano about Echo2.

To show the juggers some of the Echo capabilities, I’ve built Echo2Impress, a simple echo application that shows slides, just as OpenOffice Impress does.

Ah! How cool it was to present Echo with Echo! :)

If you want to give Echo a try or if you are just curious about it, take a look at Echo2Impress and its blank presentation project.
Hope you like it ;)

Echo2: an enterprise app review

Wednesday, November 29th, 2006

Following two posts on the echo2 forums, Matt Brooks has written a nice review about his experience using a stack made of Echo2, JBoss, EJB3 and ServiceMix ESB.

Matt’s app seems really cool. Its loading page is the cooooooolest thing ever seen so far in the ajax multiverse!

Taken from the architectural point of view, it’s a nice reading especially because I’ve heard too many “w00t!”s while I’m not aware of any good enterprise application (but Google’s, of course).

I haven’t looked for them actually, but I think I should have heard of them, at least from the javaposse guys, who are well aware of RIAs (as one of them is from Google and as they’ve interviewed the GWT team).

Am I wrong? Or blind? Or this ajax plane is actually taking too much time to take off?

By the way, even my two apps are going to be released in the next few months. But they won’t be public. Argh! I wanna go public!

Echo2 speech @ JUG Milano

Wednesday, November 22nd, 2006

On December, the 5th, I’ll have my very first public speech!! I’m very excited about that. I’m even worried, actually.

I’ve just completed a draft of the slide show. Although a very busy period, I’ll hope to have time to add some more info about the projects related to Echo2 such as HSE, the Extras and EchoPointNG.

So: do you have a dinner planned for the 5th of December? No? Do you want to learn something more about JSF (by Michele Sciabarrà) and Echo2? Do you like pizza? :)

Hurry up! Get the details!

I’m onto the Gentoo train

Sunday, October 22nd, 2006

With the struts-1.3.5 ebuild, nichoj offered me access to the Gentoo java overlay.

An overlay is an alternative branch of the official portage tree, where experimental ebuilds are made available to testers and people in a hurry to use an application or have a library.

Of course, “happy” is not a word worth describing my pride.

There are still a LOT of things I have to learn. But I got the patience: hope my team mates have enough of it, too.