Wednesday, May 28, 2014

Add new user in Linux/Ubuntu

To add a new user using command line on an Ubuntu machine there are at least two commands that may be used: useradd and adduser

Use adduser instead of useradd if you are not totally certain useradd is the tool you want to use because:
1. useradd is a low level tool.
2. useradd will not add home directory for new user.
3. useradd will not add many other defaults for new user.
4. adduser is more user friendly (but uses useradd in backend).
5. adduser will create home directory.
6. manpages of useradd recommends use of adduser.

Use it like this:
sudo adduser <username>

and to add a system user which has no shell (cannot log in, but just run programs):
sudo adduser --system <username>
 instead of
sudo useradd <username> -s /bin/false
Using adduser, the system user a home directory will be created.

See http://askubuntu.com/questions/374870/home-directory-not-being-created and http://askubuntu.com/questions/345974/what-is-the-difference-between-adduser-and-useradd for good reasons with links to manpages.

Java Listeners and Adapters, almost anonymous functions

Java allows developers to easily add listeners to different events.
e.g. button.addMouseListener(this);
However, often you don't want to implement a listener interface for your class or add an inner class
MyClass implements MouseListener
MyClass {
    private class ListenerClass implements MouseListener {
            public void mouseClicked(MouseEvent e) {}
            public void mouseEntered(MouseEvent e) {}
            .
    }
}

With all the functions required by the interface cluttering up your code. Often you don't even need more than one or two of the functions.

Enter Adapters.
Adapters are classes which are made to match a listeners interface and only that. They do nothing when called, but the developer will override the function he needs:
button.addMouseListener( new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
    }
}


This adds flexibility to Java, almost like the anonymous functions of JavaScript.

Source: https://blogs.oracle.com/CoreJavaTechTips/entry/listeners_vs_adapters
http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html

Wednesday, May 7, 2014

Play2 PersistenceException

You might come across the error:
[PersistenceException: Error loading on models.Period.startYear]
in Play 2 Framework.
This is most likely caused by your database having a NULLable field which doesn't map to a nullable member in the model.
E.g.:

You database has the following fields:
ADD COLUMN `start_year` INT(11) NULL DEFAULT NULL;
ADD COLUMN `end_year` INT(11) NULL DEFAULT NULL;

 which means start_year may be (null) or an int.

Your java model has the following members:
@Entity
public class Period extends Model {
    @Id
    public Long periodId;

    @Required
    public int startYear;
   
    @Required
    public int endYear;
}

Which means that startYear may be an int, but not null.

To fix, make change the database fields to not be nullable.
Or the java model members nullable also, by using the Integer object wrapper instead of int:
@Entity
public class Period extends Model {
    @Id
    public Long periodId;

    @Required
    public Integer startYear;
   
    @Required
    public Integer endYear;
}


See: http://stackoverflow.com/questions/18405141/persistenceexception-when-i-was-using-ebean-in-play-framework