Why being a Rebel is hot and spicy?

I usually write technical articles about what is hot and spicy in software development, or at least what I think is hot and spicy. Well, I will make a little exception this time and write about why it is also hot and spicy being a RebelLabs author. If you’re reading this, you probably know what RebelLabs is: a house of geeky ninjas that like to write technical content about hot topics in our geeky development world. According Oliver White, head of RebelLabs:

Developers enjoy the additional “spice” of RebelLabs–we’ve managed to create a reliable content machine producing challenging, opinionated and humorous technical content for all software engineers to enjoy. Recently I saw this quote about RebelLabs by Rafael Winterhalter, in a recent Developer of the Week interview by DZone, and I think it sums things up very nicely: “I like RebelLabs a lot, especially since they manage to be both competent and humorous which is a rather rare combination.” —

Finding passion in the geeky world of development

As a geek, I’m passionate about software development, especially about Java and more recently, JavaFX. This passion leads me to go to people and say “Hey look, I discovered some pretty awesome new stuff”. But you can’t do this with your wife or your grandma, right? You won’t be understood … So frustrating …

But as passionate geek, I still need to tell somebody what I have discovered, tested and developed because I’m proud of it. Yeah you read right, proud of it. It is like being a kid having the latest trendy toy who shows everybody he has it. It is the same for me with software development. RebelLabs brings me the possibility of doing it, with a lot of fun.

I don’t need to write about old tools, APIs, languages and so on. I need to find a subject, find a way of making it attractive and interesting, and write. And when it’s published it’s like telling the world how proud you are. It is like your work being concrete and not only stored on your computer, right? Haven’t you ever felt that feeling of pride when you’ve developed the next most awesome API and nobody is using it?

The open source writer

We all know open source projects, developed by passionate ninjas to help other ninjas (or not) do their jobs better and make them easier by bringing them tools, APIs and other shiny stuff. I know a couple of those. One of them, Guillaume Scheibel (@g_scheibel and Hibernate OGM contributor) told me some time ago, that in the beginning it was not that easy to code in open source projects because you show the way you code to everyone and you have to accept remarks from others giving you advices of how doing things better. You have to accept remarks and sometimes it is not that easy. Writing is almost the same because you show the world what you’ve done and how you write. Double pressure.

I accept both because what you learn is much greater than this. Why is it? Because I work with guys that are as passionate as me and who like to teach you things about writing, tools and more. They don’t want to take you down in what you’re writing, but raising you up. And remarks are always constructive. It helps you do things better.

I also think this needs a little bit of courage. You, and I, have to be brave before publishing your first article. I needed some to accept the opinions of others, I needed some to accept the remarks and comments about my opinion, way of writing and way of thinking. I needed some to accept that other ninjas will take a look and possibly prove me wrong. But at the end the discussion will bring you a lot of point of view, a lot of ideas and you will always have the chance to be better next time.

Being visible

There is a really nice side effect of writing for RebelLabs: you are visible and you’re getting more and more known. People are reading your articles, looking at your Twitter profile, reading your blog and so on. It is a nice recognition to have people with more experience than you reading your articles, commenting on them and sometimes offering you their point of view.

You start to grow much more, and very quickly. And people can see that you are passionate, up to date, open minded and full of resources. It is a very positive image you’re giving of yourself.

You also benefit of the RebelLabs’ image, so you are more considered as experimented because if you’re not, you’re going to know it very quickly. So it is good for RebelLabs and it is good for you as well.

Hot and spicy instead of glory

To conclude, I write for fun. I write because I like to share my knowledge. I write because I’m passionate. I write because I like to write. I don’t write for any glory. I’m humble enough to admit there are plenty of ninjas out there that are thousands times better than me. But I am always looking for new challenges and I like to share them because maybe it could help other ninjas like you.

It’s a great opportunity to write for RebelLabs, and I like it very much. I can only encourage you to think about it, because being a Rebel is freakin’ awesome, and it’s hot and spicy!

Partial HTML generation from textile using eclipse Mylyn standalone

The title is pretty much explicit. For SlideshowFX, I needed to generate HTML content from textile, in order to define slide’s content. Looking for a lib, I found eclipse Mylyn that can be used in a standalone way. The library is pretty powerful but I have to look a lot at the sources in order to get the things done how I wanted to (#LukeAtTheSource power). Generally to convert a markup string into a HTML one, you create a MarkupLanguage as well as a MarkupParser and do the following:

final MarkupLanguage language = new TextileLanguage();
final MarkupParser parser = new MarkupParser(language);
parser.parseToHTML("h1. My little String");

The thing is that this code will create a whole HTML document (with html, head, body and so on tags). Me needs were to just get the HTML code corresponding to my string, e.g.

<h1>My little String</h1>

So the code should be changed to use a DocumentBuilder, like the following one:

final StringWriter writer = new StringWriter();
final DocumentBuilder builder = new HtmlDocumentBuilder(writer);
final MarkupLanguage language = new TextileLanguage();
final MarkupParser parser = new MarkupParser(language, builder);

// false indicates to not produce a whole HTML document
parser.parse("h1. My little String", false);

writer.flush();
writer.close();

String htmlContent = writer.toString();

This is almost done except that Mylyn generates IDs (which is kind of normal right?) by using the content of the markup. In short, I wanted to avoid IDs’ generation but I could’t find a right and efficient way to do it. So I decided to ensure uniqueness of IDs by always getting the current timestamp. Maybe it’s not a wonderful solution, but for the purpose of this little tutorial it will be perfect. The main idea is to change the ID generation and for doing this, you have to override some classes, because the default ID generation strategy is stored as a static and final variable, and no setters are available. The following example demonstrates how to do it:

final StringWriter writer = new StringWriter();

// The generation strategy generates IDs using the current timestamp
final IdGenerationStrategy idGenerationStrategy = new IdGenerationStrategy() {
  @Override
  public String generateId(String s) {
    return System.currentTimeMillis() + "";
  }
};

final IdGenerator idGenerator = new IdGenerator();
idGenerator.setGenerationStrategy(idGenerationStrategy);

final TextileContentState contentState = new TextileContentState() {
  @Override
  public IdGenerator getIdGenerator() {
    return idGenerator;
  }
};

// Override the language to return the created contentState used for the ID generation
final MarkupLanguage language = new TextileLanguage() {
  @Override
  protected ContentState createState() {
    return contentState;
  }
};

final DocumentBuilder builder = new HtmlDocumentBuilder(writer);

final MarkupParser parser = new MarkupParser(language, builder);

parser.parse(markupString, false);

writer.flush();
writer.close();

final String htmlContent = writer.toString();

This is it. Enjoy.

Get annotation’s field’s value easily

Hello,

Maybe have you ever been confronted to the problem of getting the value of a field of an annotation. You see what I’m talking about? No. Example:

@Foo(myField="Hello there")
public class FooClass implements Serializable {
// ...
}

Let’s imagine you want to retrieve the value of myField (ie Hello there). How could You do that? Let me propose you two ways.

First way
The easier way but absolutely not scalable is to do the following code:

// ...
String myFieldValue = "Hello there";
// ...

Well it’s working, isn’t it? But imagine you have to get this value a lot of times, in different classes and so on … Are you going to use your favorite IDE’s function “Find and replace …”? Why not. But, lets take a look to a different way.

Second way
The other solution, much more scalable, is to use reflexivity. And the great thing about that is that you will be able to use for whatever annotation and whatever field. Lets take a look at this:

public String getClassAnnotationValue(Class classType, Class annotationType, String attributeName) {
        String value = null;

        Annotation annotation = classType.getAnnotation(annotationType);
        if (annotation != null) {
            try {
                value = (String) annotation.annotationType().getMethod(attributeName).invoke(annotation);
            } catch (Exception ex) {
            }
        }

        return value;
    }

How invoke that snippet of code?

// ...
String myFieldValue = getClassAnnotationValue(FooClass.class, Foo.class, "myField");
// ...

As you can see, you can use it for whatever class annotation on a class. You can now imagine doing the same for a field Annotation. Give it a try!

This is a pretty useful when you work in JEE. Imagine a SessionBean with may implementations, or an EntityBean with the @Table annotation, or a simple field with the @Column annotation. And these two last examples, it is useful if you build your own SQL queries. Because with this snippet of code, you won’t have to look everywhere you use your EntityBean and change the table name in your query …

Well, I hope this could help you sometimes.
Enjoy.

Deal with Context-Path in Java EE

Maybe have you ever try to create an Enterprise Application in Java, containing both EJB and Web modules. And maybe have you noticed that the context-path of the web application is the same that the generated WAR file.
So let’s imagine you create an enterprise application named JustMyApp with two modules: JustMyApp-ejb and JustMyApp-war. After deploying your application, you just go to your website using an URL like this one (depending on which port your server is running): http://localhost:8080/JustMyApp-war. Well I have to admit I totally dislike to enter -war in the URL… Why not JustMyApp only?

Context-Path in Java EE is kind of strange. Indeed, if you only have a WAR (not en EAR) changing the CP for the WAR is working fine. But if you are in an EAR, things are little bit different!

So how to do that? You have to create a file called a Standard Deployment Descriptor and put it into your EAR. The name of this file must be application.xml. This file contains the name of all modules you have in the EAR, and you can specify the context-path for your Web module. Let’s see an example:

<?xml version="1.0" encoding="UTF-8"?>
<application version="6" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd">
  <display-name>JustMyApp</display-name>
  <module>
    <ejb>JustMyApp-ejb.jar</ejb>
  </module>
  <module>
    <web>
      <web-uri>JustMyApp-war.war</web-uri>
      <context-root>/JustMyApp</context-root>
    </web>
  </module>
</application>

The URL to enter to access your web application is just specified by the <context-root>…</context-root> markup. So put there whatever you want and just try it ! That’s how the Context-Path for EAR can be changed !

Enjoy.

Deploy Java applications on Mac

Hello,

You need to deploy Java applications on Mac? Take a look at : http://thierry.wasylczenko.free.fr/?p=108

Enjoy.

JAI : how to solve vendorName == null exception

Hello everybody,

Have You ever used JAI (Java Advanced Imaging) ? If You used the class ImageIO in order to read and save images, the answer is yes.
Most of the time, You should not have problem when You deploy Your application with an executable JAR even if You’re using JAI. But, most of the time doesn’t mean never, so that’s a problem !
Recently I had to deploy an application as a JAR, and this application was using quantity of external library. And of course it used JAI.
Well, doing a JAR is not really a problem but …

In my case, the application was developped with Eclipse, and was running fine in it. But, when it comes to run it with a JAR, that was problematic…
I got an exception when a tried to load images, especially TIFF images. This exception was thrown by JAI and told me that the vendorName was null. Interessting : but which vendor ??

The answer is quiet simple : JAI needs sometimes to know the name of the vendor who developped the application, and the implementation of it… OK, you’ll tell me “Fine. It seems it’s my application, so I’m the vendor. But how do I specify that?”. And that’s the point.

When You deploy an application as an executable JAR, You probably know that You need a manifest file. And that manifest file is the key. A complete valid manifest file could be this :

Manifest-Version: 1.0
Main-Class: fr.free.thierrywasyl.myproject.launcher.Launch

But where did I specify the author? No where. But JAI will maybe need to know that, or You’ll get an exception about the vendor. So let’s add some lines, which are good to add (take it as a good practice) which are general:

Manifest-Version: 1.0
Implementation-Vendor: Sun Microsystems, Inc
Implementation-Title: Java Runtime Environment
Implementation-Version: 1.6.0
Main-Class: fr.free.thierrywasyl.myproject.launcher.Launch

So far so good. So are You a null vendor? Of course not, You get over the exception 😉
Enjoy.