adrian’s blog

November 27, 2008

Motivation in open source

Filed under: Software — Tags: — Adrian @ 7:07 pm

I responded to Alex’s invitation and I gave today a presentation to a few students about motivation in software development. I hope they liked it and found it interesting.

As my focus was mainly on discussing about open source projects, while I was preparing my presentation, I made a thinking exercise. And I noted down the motivational factors for developing open source applications. Let’s go through the list.

  • Passion. Remember that most of you are working in this field because you like it. Sometimes developers get to work in a company and in a project that they actually don’t like. Open source is their chance to do what they like. So, even tough this takes extra time and effort, the moral satisfaction pays it off. Keep the fire burning with Open Source!
  • Educational. Working in an open source is the best way to keep you up-to-date with the latest technologies. Due to economical reasons, the projects developed in closed profit-based organizations, are not up-to-date with the last technologies. Keep learning with Open Source!
  • Portfolio. What you developed as open source could be a very good showcase to obtain a better job. If you don’t have yet industry practical experience, this can compensate. Show off your Open Source!
  • Status. There is a special pride of being a member in a successful open source community. Be proud with your Open Source!
  • Networking. Working in open source communities is a great way to meet developers, designers etc that share common interest with you. Get together in Open Source!
  • Need. This is actually one of the main reasons for developing open source applications. Either there isn’t a software solution for your problem or it is too expensive, you can always make your own or contribute to other people effort to do it. Use Open Source!
  • Influence. This is somehow related to need. Because a company is needing some features in an open source application, they encourage their employees to take an active role in the development of it. In this way they can easily influence the direction in which the open source project is heading. Influence your Open Source!
  • Altruism/Knowledge sharing. Some people simply do it because is the right and moral thing to do. They used and enjoyed open source and now it’s time to give something back. Share by Open Source!
  • Quality. I know that it may sound like a paradox, but I really think that a successful open source project has a higher quality than a successful closed, private one. Usually an open source project has a more varied and wide pool of users. It means that it also have a wider pool of critics. And they will review not only the final products, but the source code too. Moreover, the commercial products will have to constantly improve because of the competition. Improve Open Source!
  • Economical. Open source is not entirely free. And here we have to talk about cost reduction and profit.
    Cost reduction. A company can develop a project, but due to the lack of resources, they decide to make it open source. If the project is not on their portfolio, but it was merely developed as a library for other projects or to support the internal infrastructure, this is a very good choice. Their business won’t be affected, but their winnings could be huge. In the first place, they win a huge amount of users, which are actually free ad-hoc testers. As the project is growing they can also gain developers or other specialists (like designers or documentation writers), thus tremendously decreasing the maintenance costs.
    Profit. A new business model has lately emerged. The organization is offering the product for free, but they are charging for related services, such as support, development on request or even specialized documentation (like books) etc. More and more companies are moving from a product oriented business model to a service oriented business model.
    Win with Open Source!

I also attached the presentation, but it is in Romanian. If you’re really interested I can translate it in English as well.

Enjoy Open Source!

P.S. Thanks to Alex and Razvan for giving me this opportunity. Thanks to Monica for recommendation.

November 23, 2008

Processes – why we avoid them

Filed under: Web — Adrian @ 8:41 pm

Even tough we design processes to improve our daily routine, people are still want to avoid them. I mostly saw this in Romanians, because I live in Romania, but people from other countries have it too.
Could be the programmer mind. I read once that the programmers are the laziest people. That’s why they try to automate everything and have the computers do their work. If you see a process like this, it can actually help you. But if you think that this only a new task on top of your existing ones or a wall for the common people and you’re special, then, yes, this was actually developed against you by mean old corporatist thinking style.

Let’s take an example. Let’s say that you work in a company with a pretty advanced, web-style interface, ticketing system. This is actually your interface to mostly everything you need, from ordering office supplies to report bugs in applications. Yup, all the internal company processes are in here. Now, you’re browsing the intranet and you read a page telling that your company just bought a corporate license to a totally new cool software that will come in very handy for you. All you have to do is click on a link “Request access to the download page”. But no, this is for everyone else. It just happens that you have a good friend in the IT department. So let’s start talking (through any known software of instant messaging)
You: Hi
IT friend: Hi
You: I just saw that we bought CoolSoft 3.0
IT friend: Yup, just happened. You really have to see what cool features they added
You: Nice! I want to install it too
IT friend: No problem. Just go to our intranet. Let me gave you the link
You: No, no, I have the link, the problem is that I have to request access
IT friend: So? Ask for it.
You: I was thinking that you maybe have the program so that I won’t have to request this. You know how much time takes these kind of things
IT friend: Nope, I deleted the kit after installing it
You: Maybe someone in your team … Pleeeease …
IT friend: Ok, let me check

Meantime another conversation happens between your IT friend and his friend and in the end you finally end up with a share folder from where you can download the kit. So, in the end you finally made it. You made completely unuseful a piece of software (company’s intranet) worth few thousands (or maybe tens of thousands) by simply wasting half an hour of your time and two other colleagues. Cool, isn’t it?

Let me tell you what would have happened if you have clicked that link. An automatic email would have been sent to someone (possibly from IT or management) with your request and your details. That someone would have seen the request, would have clicked another link and your request would have been approved. You would have received another email with the download link. Two minutes.

I agree that sometimes people are introducing processes just for the sake of it. Sometimes they even do some parts of the process, without even knowing why. I agree that’s stupid. But if you realize this, don’t try to avoid the process, challenge it. Try to change it, improve it.

There is a saying: rules are made to be broken. I would agree. And even these automated workflows agree. Sometimes, these rules have to be bended, and that’s why the human factor is involved to take the critical decisions.

I have to admit that I really like Java. One of the things that I like about is JCP. I really enjoy the idea that everyone can write a piece of software that will interoperate with all the software out there, without having to reverse engineer I don’t know how many programs, use proprietary protocols, that can change over night, without any prior notice etc. The standards are made to avoid all these kind of headaches. And the processes are the equivalent in the operational world.

So work with them to save your time. Challenge them to save everyone’s time.

November 14, 2008

Rotate images according to EXIF info

Filed under: Web — Tags: , — Adrian @ 11:50 pm

Some digital cameras are saving the snapshots as they come from the digital sensor. This is not always the right orientation, as it depends on how you are position the camera. Luckily, the cameras are also saving the orientation information into EXIF attributes.
This article is about how to automatically correct this from your Java program, using jMagick. The entire thing can be break down into pieces: read an EXIF attribute for an image, interpret the value and transform the image.
The EXIF attribute responsible for orientation is called Orientation. According to EXIF specs, the values have the following meaning:

  • 1 = The 0th row is at the visual top of the image, and the 0th column is the visual left-hand side.
  • 2 = The 0th row is at the visual top of the image, and the 0th column is the visual right-hand side.
  • 3 = The 0th row is at the visual bottom of the image, and the 0th column is the visual right-hand side.
  • 4 = The 0th row is at the visual bottom of the image, and the 0th column is the visual left-hand side.
  • 5 = The 0th row is the visual left-hand side of the image, and the 0th column is the visual top.
  • 6 = The 0th row is the visual right-hand side of the image, and the 0th column is the visual top.
  • 7 = The 0th row is the visual right-hand side of the image, and the 0th column is the visual bottom.
  • 8 = The 0th row is the visual left-hand side of the image, and the 0th column is the visual bottom.
  • Other = reserved

Graphically, this looks like this:

EXIF orientation values and meaning

EXIF orientation values and meaning

.
The following transformations should be applied to the images according to their EXIF orientation:
Autorotate EXIF images

Autorotate EXIF images


where the possible operations are horizontal or vertical flip and rotate (90 degrees clockwise or counter-clockwise and 180 degrees). The corresponding jMagick methods from MagickImage class are:

  • horizontal flip – flip()
  • vertical flip – flop()
  • rotate 180 degrees – rotate(180)
  • rotate 90 degrees clockwise – rotate(90)
  • rotate 90 degrees counter-clockwise – rotate(-90)

Now let’s translate everything into Java code:

...
private static final int NONE = 0;
private static final int HORIZONTAL = 1;
private static final int VERTICAL = 2;
private static final int[][] OPERATIONS = new int[][] {
        new int[] {  0, NONE},
        new int[] {  0, HORIZONTAL},
        new int[] {180, NONE},
        new int[] {  0, VERTICAL},
        new int[] { 90, HORIZONTAL},
        new int[] { 90, NONE},
        new int[] {-90, HORIZONTAL},
        new int[] {-90, NONE},
        };

public static MagickImage rotateByExif(MagickImage image) throws MagickException {
    try {
        int index = Integer.parseInt(image.getImageAttribute("EXIF:Orientation")) - 1;
        int degrees = OPERATIONS[index][0];
        if (degrees != 0)
            image = image.rotateImage(degrees);
        switch (OPERATIONS[index][1]) {
            case HORIZONTAL:
                image = image.flopImage();
                break;
            case VERTICAL:
                image = image.flipImage();
        }
    }
    catch (NumberFormatException exc) {}
    catch (NullPointerException exc) {}
    return image;
}
...

Now you can simply call the rotateByExif method. For more information on how you can use jMagick to load and save images please see the official documentation or see Watermark with jMagick article. Actually you can even integrate this, so that you apply it to the watermarked images.

So actually it is pretty simple to rotate images according to the EXIF info using jMagick: just get the EXIF Orientation attribute with getImageAttribute() method and, according to its value, apply the rotateImage, flipImage and flopImage.

Fixing IE: CSS fixed position

Filed under: Web — Tags: , , , — Adrian @ 3:35 pm

I will start a series of articles about how you can fix some IE bugs. We all agree that IE is very buggy, but instead of whining, let’s see what we can do about it. So I will present you the problem, the fix, explain the fix and how you can EASILY INTEGRATE it.

Before, I will explain you a few things that I will use in these fixes. There are few different things in IE when it comes to CSS.

If you prefix a CSS property with _, IE will still recognize it. If you think this can be very useful. You can specify values for CSS properties only for IE. Let’s take an example.

color: green;
_color: red;

The font color will be green in all the browsers, except IE, where it will be red.
Instead of _, you can also use *, + and some other special signs.

The CSS value supports in IE a special value: expression([expr]). The [expr] is a JavaScript expression, which is evaluated and the CSS value is assigned to it. Again an example:

_height: expression((1 + 1) + 'px');

The CSS height in the above example is actually 2 pixels in IE. In the expression we can even use this to refer to the element for which the CSS value is applied.

Now let’s get back to the current issue. If you specify the fixed value for the position property, the element should be absolutely positioned, but relatively to the viewport, instead of the document. For more detailed info, please see the CSS specification. Not surprisingly, this works in most of the browsers, except IE.
And now, the fix. Which is actually very simple. We will use absolute positioning but we will calculate the coordinates dynamically relative to the viewport.
So the CSS code

position: fixed;
top: 10px;
left: 20px;

will have the following IE equivalent

_position: absolute;
_top: expression(((ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) + 10) + 'px' );
_left: expression(((ignoreMe = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) + 20) + 'px' );

.
Don’t remove the ignoreMe variable as this will force the expression to be evaluated not only the first time the page is loaded, but always.

In the end, a live example. And if you complain that in Mozilla the things are smoother, then simply switch to Mozilla :) .

November 3, 2008

Detecting process exit in Java

Filed under: Software — Tags: — Adrian @ 4:04 pm

If you develop a more complex system, the chances of being a heterogeneous are pretty big. So if you develop a system in Java, but you have to integrate all ready build parts with other technology, the most desirable option is some kind of service oriented architecture. But when this is not available you will simply rely on an external process.
To start an external process is quite simple. Just look at the exec methods from Runtime.
What could be more difficult is to know when that process as finished. The Java process and that subprocess will run in parallel. If you want to perform an action when the subprocess has finish its execution, this article may help you in this direction.
It would be very nice if the Process class would implement the Observable design pattern in some way. But it doesn’t and on this we will base our workaround creation.
Process contains a useful method for us, waitFor, that pauses the current thread execution until the given subprocess is finished. So, we will create a thread that will be suspended until the given subprocess is finished. Then we will fire the listeners to notify that the process has finished its execution.
Enough talking, let’s see some code.

<

/**
 * Detects when a process is finished and invokes the associated listeners.
 */
public class ProcessExitDetector extends Thread {

    /** The process for which we have to detect the end. */
    private Process process;
    /** The associated listeners to be invoked at the end of the process. */
    private List listeners = new ArrayList();

    /**
     * Starts the detection for the given process
     * @param process the process for which we have to detect when it is finished
     */
    public ProcessExitDetector(Process process) {
        try {
            // test if the process is finished
            process.exitValue();
            throw new IllegalArgumentException("The process is already ended");
        } catch (IllegalThreadStateException exc) {
            this.process = process;
        }
    }

    /** @return the process that it is watched by this detector. */
    public Process getProcess() {
        return process;
    }

    public void run() {
        try {
            // wait for the process to finish
            process.waitFor();
            // invokes the listeners
            for (ProcessListener listener : listeners) {
                listener.processFinished(process);
            }
        } catch (InterruptedException e) {
        }
    }

    /** Adds a process listener.
     * @param listener the listener to be added
     */
    public void addProcessListener(ProcessListener listener) {
        listeners.add(listener);
    }

    /** Removes a process listener.
     * @param listener the listener to be removed
     */
    public void removeProcessListener(ProcessListener listener) {
        listeners.remove(listener);
    }
}

The code is very easy to understand. It creates a class as the detector for the process exit. It has a process to watch for passed in as a constructor argument. The entire thread does nothing else than wait for the given process to finish and then invokes the listeners.
The code for the ProcessListener is as easy:

public interface ProcessListener extends EventListener {
    void processFinished(Process process);
}

Now you have a very easy way to detect when a subprocess is finished, and as proof, here is a code sample:

    ...
    processExitDetector = new ProcessExitDetector(subprocess);
    processExitDetector .addProcessListener(new ProcessListener() {
        public void processFinished(Process process) {
            System.out.println("The subprocess has finished.");
        }
    });
    processExitDetector.start();
    ...

.
All the code samples were tested with JDK 6 under Windows XP.

Blog at WordPress.com.