synergy

Last week at work I give synergy a go. It was really amazing. What does this do? Well very simple like most geeks I have different computers (laptops and pcs). I have several screens, what I could do is do a vnc,rdp,… to use the other desktop or I could just synergy. On my work I have to use a windows xp pro, on my laptop I’m running a ubuntu dapper. Between the 2 is a small network, I started a synergy server on my win box and started the client on my linux box. I configured it, telling my windows machine is right from my linux and my linux is on the left of my windows.

Now when I move my mouse on my windows machine the the left and off the screen it appears on my linux box. I can now share the same keyboard I’m using for my windows on my linux box, it even does clipboard sharing. So if you use a lot of computers, check this out!!

Dojo and Quartz

I struck a delicate point using AJAX, the response to my ajax call had things written in javascript but these didn’t get triggered by the browser. Hmmm, well as I was using the dojotoolkit I asked those guys. One simple reply, use our contentpane and set executescripts to true. I did and voila everything works like a charm. I have to admit those guys really know what they are doing. It’s one of the cleanest dhtml toolkits I’ve ever seen. Just take a look at their examples, just amazing!!! So if you have to do dhtml stuff, check it for sure!!!

I’m also using quartz for scheduling stuff in my code,I must say it really looks nice. I’ll be using this much more in all of my applications in the near future, you can be sure of that 2!!

One more converted

Yesterday I had a meeting with my boss. During our last workshop I did part of my demo on ubuntu and most of the other people were really amazed at how far the linux desktop had evolved. My boss mailed me a couple of times asking more information on my linux (ubuntu).
Yesterday he told me he had installed ubuntu and kubuntu. He was really amazed as all his hardware (including wireless) worked out of the box. With windows he had to search ages for his printer drivers, he had to seperately install his wireless,… in short he was dazzled with how far linux was.
I’m proud to say I converted one more it professional to ubuntu, let’s hope the count goes up rapidly!!

Podcast comment

This morning I was listening to my cup of javaposse. They started a discussion about the difference between good and bad codemonkeys. The said that a good monkey would be 300% more productive than a bad one, but what’s more strange is that they would probably still be paid the same.

As I know what a lot of my collegeaus earn, I really have to agree with this. There are guys out there who really get paid way to little for what they know and do, and others who actually don’t do much get paid even more. This is really frustrating, I know that if I’m in charge of the money I’ll be giving my best programmers more than those who do little and know little.

In my former job, I now and then had to interview some sollicitants. I always asked the very simple question, tell me the difference between an abstract class and an interface. If they didn’t knew, well sorry your not a programmer in my eyes. It’s one of the basics of OO programming, what’s almost a defacto standard.

My current employer works with consultants (as I am one of them), I told him as tip. If you get a consultant, just type his name in google. If you don’t get any results, I for one wouldn’t hire them (unless he can explain). Why you might ask, well actually very simple. If you can’t find something, where do you look. Google,Support,… in most cases if you log a question or something you have to provider your credentials, so you leave your credentials behind. These things get picked up by search engines. It’s not always true off course, but in many cases it will give you an indication of the person who is sitting before you.

Now I was wondering if you could take it one step further. Afaik if you are programming you are using your keyboard, hence a person who knows who to type without looking at his keyboard should be faster than someone who needs to look at his keys. So should you say, if you can’t type, you won’t get the job, or is this taking it one step to far?

Gnome fonts follow up

It’s only been 1 day since I setup my gnome to use a smaller font, but it already feels much nicer. Really amazing what a small little change can do. I’m one of those persons who wants as much as possible on his screen. Maybe that’s something typically for coders? Many people I know want clear and big fonts, I want small and more.

For all you coders out there who use gnome and haven’t set there font smallers, really try it out. Maybe you’ll have the same reaction as me.

One thing I noticed was the fact that eclipse has it’s own font settings and I had to change these manually, no biggy but it’s a shame it’s not (yet) integrated.

gnome fonts

In my spare time I always use gnome, but during the day I have to use microsoft. (Client Company policy) One thing that was always bizarre was the fact that in my day job my computer is running at 1200×1024 and my laptop is running 1440×900. But I always had the feeling my windows maching could display more on a smaller screen. So I started looking around in my ubuntu menu’s and I found a configuration under System -> Preferences -> fonts (It was in dutch so I’m not sure if this is the actual translation but should be something like this)

I included a before and after screenshot, just like at the amount of information that is displayed on my screen now. I’m back a happy hacker 🙂

before the update
before

after the update
after

the font menu
font menu

I updated the font size and set it to 8 instead of 10, Appearantly if you change the dpi settings you can do something similar but I don’t know the exact difference between the 2 (I now what they mean but don’t know exactly what impact they have on the system.)

Generics sweetness

As I wrote earlier, last week I did a presentation on Hibernate as a good speaker I investigate my subject. During my investigation I came a cross the project that is being done for the Hibernate in Action second edition. In this book they used some generics for creating the CRUD DAO objects and it really opened my eyes. For me generics was a nice to have gadget for typecasting my collections, I had no idea this could be done with it. So read and be amazed!!

You have an interface like this:

public interface GenericDAO<T , ID extends Serializable>{
    T findById(ID id, boolean lock);
    List<T> findAll();
    List<T> findByExample(T exampleInstance, String... excludeProperty);
    T makePersistent(T entity);
    void makeTransient(T entity);
    void flush();
}


you have an basic implementation of this interface:

public abstract class GenericHibernateDAO<t , ID extends Serializable> implements GenericDAO<t , ID> {
    private Class<T> persistentClass;
    private Session session;
    public GenericHibernateDAO() {
        this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
                                .getGenericSuperclass()).getActualTypeArguments()[0];
     }
    @SuppressWarnings("unchecked")
    public void setSession(Session s) {
        this.session = s;
    }
    protected Session getSession() {
        if (session == null)
            throw new IllegalStateException("Session has not been set on DAO before usage");
        return session;
    }
    public Class<T> getPersistentClass() {
        return persistentClass;
    }
    @SuppressWarnings("unchecked")
    public T findById(ID id, boolean lock) {
        T entity;
        if (lock)
            entity = (T) getSession().load(getPersistentClass(), id, LockMode.UPGRADE);
        else
            entity = (T) getSession().load(getPersistentClass(), id);

        return entity;
    }
    @SuppressWarnings("unchecked")
    public List<T> findAll() {
        return findByCriteria();
    }
    @SuppressWarnings("unchecked")
    public List<T> findByExample(T exampleInstance, String[] excludeProperty) {
        Criteria crit = getSession().createCriteria(getPersistentClass());
        Example example =  Example.create(exampleInstance);
        for (String exclude : excludeProperty) {
            example.excludeProperty(exclude);
        }
        crit.add(example);
        return crit.list();
    }
    @SuppressWarnings("unchecked")
    public T makePersistent(T entity) {
        getSession().saveOrUpdate(entity);
        return entity;
    }
    public void makeTransient(T entity) {
        getSession().delete(entity);
    }
    public void flush() {
        getSession().flush();
    }
    /**
     * Use this inside subclasses as a convenience method.
     */
    @SuppressWarnings("unchecked")
    protected List<T> findByCriteria(Criterion... criterion) {
        Criteria crit = getSession().createCriteria(getPersistentClass());
        for (Criterion c : criterion) {
            crit.add(c);
        }
        return crit.list();
   }

}

And you have a descendent of this base class

public class BillingDetailsDAOHibernate
        extends     GenericHibernateDAO<Billingdetails , Long>
        implements  BillingDetailsDAO {

    @SuppressWarnings("unchecked")
    public List<Billingdetails> findConcrete(Class concreteClass) {
        return getSession().createCriteria(concreteClass).list();
    }
}

In short the descendent tells his parent Substitute all you T with BillingDetails and your Id with Long and abracadabra you have a simple CRUD dao for your billingDetails. Now how’s that for some fancy programming.

At the moment I’m rewritting my DAO objects to return generic collections, maybe in the next refactoring cycle I’ll use this design pattern.

My bet is that with the rise of generics, we’ll see more and more design patterns that are based on generics as this is very powerfull. The example above will surely be used in more projects to come!!

No reaction about my JUG, guess nobody is interested in learning Java. 🙁

ps sorry if wordpress f*cked up the layout, but still check it out

Jboss messaging

I had to give jboss messaging a go to. Hmm doesn’t look that difficult either. Now a simple MessageDrivenBean and I’m in business.

The Sender:

import java.util.Properties;

import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

public class JbossJmsTest
{

   public static void main(String[] args) throws Exception
   {
      log.info("Creating jndi context - alternatively use a jndi.properties");
      Properties properties = new Properties();
      properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      properties.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
      properties.put(Context.PROVIDER_URL, "localhost");
      InitialContext ctx = new InitialContext(properties);

      log.info("Looking up queue");
      Queue queue = (Queue) ctx.lookup("queue/testQueue");

      log.info("Looking up connection factory");
      QueueConnectionFactory qcf = (QueueConnectionFactory) ctx.lookup("UIL2ConnectionFactory");

      log.info("Creating connection");
      QueueConnection qc = qcf.createQueueConnection();
      try
      {
         log.info("Creating session");
         QueueSession qs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
         
         log.info("Creating sender");
         QueueSender sender = qs.createSender(queue);

         log.info("Creating message");
         TextMessage message = qs.createTextMessage("hello");

         log.info("Sending message");
         sender.send(message);
      }
      finally
      {
         qc.close();
      }
   }

   public static class log
   {
      public static void info(String message)
      {
         System.out.println(message);
      }
      public static void error(String message, Throwable t)
      {
         System.err.println(message);
         t.printStackTrace();
      }
   }
}

The receiver

import java.util.Properties;

import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

public class JbossJmsReceiver {
	public static void main(String[] args) throws Exception
	   {
	      log.info("Creating jndi context - alternatively use a jndi.properties");
	      Properties properties = new Properties();
	      properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
	      properties.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
	      properties.put(Context.PROVIDER_URL, "localhost");
	      InitialContext ctx = new InitialContext(properties);

	      log.info("Looking up queue");
	      Queue queue = (Queue) ctx.lookup("queue/testQueue");

	      log.info("Looking up connection factory");
	      QueueConnectionFactory qcf = (QueueConnectionFactory) ctx.lookup("UIL2ConnectionFactory");

	      log.info("Creating connection");
	      QueueConnection qc = qcf.createQueueConnection();
	      try
	      {
	         log.info("Creating session");
	         QueueSession qs = qc.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);

	         log.info("Creating receiver");
	         QueueReceiver receiver = qs.createReceiver(queue);

	         log.info("Try to receive message, it will not work");
	         while(true){
		    	 Message received = receiver.receiveNoWait();
		    	 /*if (received != null)
		    		 throw new RuntimeException("Should not get a message if the connection is not started!");*/
		
		    	 //log.info("You have to start the connection before receiving messages");
		    	 qc.start();
		
		    	 //log.info("This receive will work");
		    	 received = receiver.receiveNoWait();
		    	 if(received!=null){
		    		 log.info("Got message: " + received);
		    	 }
	         }
	      }
	      finally
	      {
	         qc.close();
	      }
	   }

	   public static class log
	   {
	      public static void info(String message)
	      {
	         System.out.println(message);
	      }
	      public static void error(String message, Throwable t)
	      {
	         System.err.println(message);
	         t.printStackTrace();
	      }
	   }
}

this is almost the identic code of the jboss site only problem is finding it. It seems jboss has changed the jms server. I’m using jboss-4.0.3SP1 and somewhere they have changed JbossMQ to JBossMessaging or something. I still have to get the details but first things first getting some code working 🙂

First Jabber POC

On my current project I see some real future into messaging. I had some intensive discussions with my manager as he is more pro SOAP webservices then into messaging. But when you have push and pull mechanisms SOAP really isn’t the answer, what would you do? setup 2 SOAP services and let them communicate?? Well what if the specs change and a third party joins in? recode everything and if bla = true connect to X, else connect to Y? Really not!!!
What you can do is place a Queue where you publish everything on. You can use Jboss 4, it has one build in or if you want to go further you could use the Jabber protocol (same as google talk if I’m not mistaken). I have a jabber running on my server and did a very small test in java. I used the smack library, a small native java library. And I wrote this stupid little expample

public static void main(String[] args){
		try {
			XMPPConnection.DEBUG_ENABLED = true;
			SSLXMPPConnection.DEBUG_ENABLED = true;
			XMPPConnection con = new XMPPConnection("pczone.be",5222);
			con.login("xxx", "xxx");
			con.createChat("me").sendMessage("hello");
			PacketFilter filter = new PacketTypeFilter(Message.class);
			PacketCollector myCollector = con.createPacketCollector(filter);
			
			PacketListener myListener = new PacketListener(){
				public void processPacket(Packet pac){
					System.out.println(pac.toString());
				}
			};
			con.addPacketListener(myListener,filter);
		} catch (Exception e) {
			e.printStackTrace();
		}
}

What this stupid little example did was simply logon to my server, send me a message and listen for incoming messages. I ran this in debugmode with a breakpoint on the System.out.println line, when I replied to my message my breakpoint worked. I could inspect the message and see somewhere in the body my content wrapped in a jabber xml.

Off course it’s no rocket science, but it’s always nice when you see something spectaculair. 🙂

I’ll certainly use this in future projects. How exactly, hmm not yet sure 🙂