How to fix keyboard layout in Ubuntu 14.04

I regularly use Swedish keyboard layout, but I keep the English layout around in case I would like to temporary switch to it.

Ubuntu 14.04 sometimes mess this up and I suddenly get English layout when I log in. I fix this by installing dconf-editor, go to desktop/ibus/general, and make sure that the values engines-order and preload-engines are the same and in the desired order with the default layout first.

Posted in Linux, Ubuntu | Leave a comment

Custom Preference for Android

I need a setting with a numeric value in my Android app. There is no obvious fit among the standard Preferences in Android, so I decided to implement my own Preference subclass.

This was a bit more involved than what I hoped for. I ended up with this. An improvement would be to be able to configure min and max values through XML attributes.

public class NumericPreference extends DialogPreference {
    private static final int DEFAULT_VALUE = 0;

    private NumberPicker mWidget;

    private int value = DEFAULT_VALUE;

    public NumericPreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        setDialogLayoutResource(R.layout.numeric_dialog);
    }

    @Override
    protected void onBindDialogView(View view) {
        mWidget = (NumberPicker)view.findViewById(R.id.picker);
        mWidget.setMinValue(0);
        mWidget.setMaxValue(10);
        mWidget.setValue(value);
        super.onBindDialogView(view);
    }

    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        return a.getInteger(index, DEFAULT_VALUE);
    }

    @Override
    protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
        if (restorePersistedValue) {
            value = getPersistedInt(DEFAULT_VALUE);
        } else {
            value = (Integer)defaultValue;
            persistInt(value);
        }
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        if (positiveResult) {
            value = mWidget.getValue();
            persistInt(value);
        }
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:gravity="center_horizontal">

    <NumberPicker
            android:id="@+id/picker"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

</LinearLayout>

The code is covered by Apache License 2.0.

Posted in Android | 1 Comment

Running Adobe Lightroom 4.4 in Ubuntu 14.04

I use Adobe Lightroom 4.4 for photo editing. There is one very annoying aspect of this program, it is not available for Linux (only for Windows and Max OS X).

In order to run Lightroom on my computer, I had to use VirtualBox and install Windows 7 in it. This works, but is quite clumsy and annoying. And since Lightroom is the only reason for me to run Windows, I would like to get rid of it.

So I decided to try out Wine. The current version of Wine does not support Lightroom out of the box, but I found some patches and tricks which makes it work. This is how I did it, for Ubuntu 14.04 32-bit (might work in older versions as well).

  1. Uninstall any Wine installation you might already have
  2. Install stuff necessary to build Wine:
    sudo apt-get build-dep wine1.6
  3. Download and unpack Wine 1.7.33 sources:
    tar -xjf ~/Downloads/wine-1.7.33.tar.bz2
  4. Apply this patch:
    patch -p0 <Menu-wine-1.7.33.patch
  5. Build and install the patched Wine 1.7.33:
    cd wine-1.7.33
    ./configure
    make
    sudo make install
  6. Install winetricks:
    sudo apt-get install cabextract
    cd /usr/local/bin
    sudo wget http://winetricks.org/winetricks
    sudo chmod +x winetricks
  7. Prepare and install Lightroom 4.4.1 32-bit:
    winetricks win7
    wine ~/Downloads/Lightroom_4_LS11_win_4_4_1.exe
    winetricks winxp
    winetricks gdiplus corefonts ie7
  8. Download unpack and install sRGB color profile:
    tar -xzf ~/Downloads/lr-wine-1.5.17.tar.gz
    cp "sRGB Color Space Profile.icm" ~/.wine/drive_c/windows/system32/spool/drivers/color

Now you can start Lightroom with this command:
wine "C:\\Program Files\\Adobe\\Adobe Photoshop Lightroom 4.4.1\\lightroom.exe"

I found most of the information here, thanks to Roland Baudin who did most of the job.

Posted in Linux, Ubuntu, wine | 1 Comment

Create a self-contained .jar file with Maven

If you want to create a self-contained .jar file, with all library dependencies included, you can do it with Maven. Include this in your pom.xml:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.yourcompany.youapp.Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-jar-with-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
	</plugins>
    </build>

Then when you run mvn package, you will get a target/*-jar-with-dependencies.jar file which you can run with java -jar.

Posted in Java | 1 Comment

Running Jetty as a service in Ubuntu Linix

Jetty is a popular open source Java application server. In order to run it as a service on a Linux server, they recommend using a horribly overcomplicated and quite fragile script.

In Ubuntu server, there is a better way. Leverage Upstart and its declarative job definitions.

First install Jetty into /opt/jetty and create a jetty user:

useradd –user-group –shell /bin/false –home-dir /opt/jetty/temp jetty

Then create a file /etc/init/jetty.conf with content like this:

start on runlevel [2345]
stop on starting rc RUNLEVEL=[016]

chdir /opt/jetty

setuid jetty

reload signal SIGQUIT

exec /usr/local/lib/jvm/bin/java \
-Xmx512m \
-Djava.awt.headless=true \
-Djetty.state=/opt/jetty/jetty.state \
-Djetty.port=8080 \
-Djetty.home=/opt/jetty \
-Djava.io.tmpdir=/tmp \
-jar /opt/jetty/start.jar \
jetty.port=8080 \
etc/jetty-logging.xml \
etc/jetty-started.xml \
etc/jetty-requestlog.xml \
OPTIONS=plus etc/jetty-plus.xml \
–daemon

This will replace both /etc/init.d/jetty and /etc/default/jetty.

If you run service jetty reload, it will create a thread dump (like jstack) in /var/log/upstart/jetty.log.

You can even add respwan to have the JVM automatically restarted if it unexpectedly quits for some reason.

A similar approach can be used for other JVM based servers, like Apache Tomcat.

Posted in Java, Linux, Ubuntu | 1 Comment

Don’t use large BLOBs in MySQL from Java

The MySQL database can store large chunks of binary data (up to 1 GB) using the BLOB data types.

However, this does not work well if you access the MySQL database from Java (or any other JVM based language) using the MySQL JDBC driver.

The JDBC API supports an efficient stream based way to handle BLOBs, but the MySQL JDBC driver does not implement this properly. It works, but it will buffer all data in memory in a way which is very inefficient and can make your JVM run out of memory if the BLOBs are large.

A good rule of thumb is to not store any objects larger than 64 KB in MySQL. Store such objects as files outside of the database instead. So never use the MEDIUMBLOB, LONGBLOB, MEDIUMTEXT or LONGTEXT data types.

(I am not sure whether this advise is only valid for JVM, or for usage of MySQL from other environments as well.)

Posted in database, Java | 3 Comments

Don’t use PipedOutputStream on Android

I was using java.io.PipedOutputStream in an Android app. The app performed horribly bad. After doing some profiling, it turned out that the call to PipedOutputStream.write(byte[]) that was really slow. After digging into the Android source code, I discovered that PipedOutputStream.write(byte[]) was not implemented, it just delegated to the default implementation in OutputStream which iterate through the array and call PipedOutputStream.write(byte) for each byte.

Since PipedOutputStream.write(byte) does some synchronization and Object.notifyAll() each time, it is really slow to do this 1000 times when you write a block of 1 KB data.

Just out of curiosity, I had a look on how PipedOutputStream is implemented in Oracle’s standard Java implementation. I haven’t actually done any benchmarks on it, but I can see from the source code that it is a completely different implementation which does implement writing a block properly and probably efficiently.

The bottom line is: Don’t use PipedOutputStream on Android. If you need similar functionality, implement it yourself or find a 3rd party library which does it.

Posted in Android, Java | 3 Comments

Running Adobe Lightroom 4.4 in Linux

I use Adobe Lightroom 4.4 for photo editing. There is one very annoying aspect of this program, it is not available for Linux (only for Windows and Max OS X).

In order to run Lightroom on my computer, I had to use VirtualBox and install Windows 7 in it. This works, but is quite clumsy and annoying. And since Lightroom is the only reason for me to run Windows, I would like to get rid of it.

So I decided to try out Wine. The current version of Wine does not support Lightroom out of the box, but I found some patches which seems make it work. This is how I did it, for Ubuntu 12.04 32-bit.

  1. Uninstall any Wine installation you might already have
  2. Prepare to build Wine from source (for some odd reason, it should be wine1.4 and not wine1.6):
    sudo apt-get build-dep wine1.4
  3. Download Wine 1.6 sources
  4. Unpack Wine 1.6 sources:
    tar -xjf ~/Downloads/wine-1.6.tar.bz2
  5. Download the necessary patches
  6. Unpack and apply the paches:
    tar -xzf ~/Downloads/wine-1.6-lr5-patches.tar.gz
    patch -p0 <0-Menu-wine-1.6.patch
    patch -p0 <1-ConditionVariables-wine-1.6.patch
    patch -p0 <2-InitOnceExecuteOnce-wine-1.6.patch
  7. Build and install the patched Wine 1.6:
    cd wine-1.6
    ./configure
    make
    sudo make install
    
  8. Install winetricks:
    cd /usr/local/bin
    sudo wget http://winetricks.org/winetricks
    sudo chmod +x winetricks
    
  9. Install cabextract:
    sudo apt-get install cabextract
    
  10. Prepare and install Lightroom 4.4.1 32-bit:
    winetricks win7
    wine ~/Downloads/Lightroom_4_LS11_win_4_4_1.exe
    winetricks winxp
    winetricks corefonts gdiplus ie7
    
  11. Download sRGB color profile
  12. Unpack and install sRGB color profile:
    tar -xzf ~/Downloads/lr-wine-1.5.17.tar.gz
    cp "sRGB Color Space Profile.icm" ~/.wine/drive_c/windows/system32/spool/drivers/color
    
  13. Now you can start Lightroom with this command:

    wine "C:\\Program Files\\Adobe\\Adobe Photoshop Lightroom 4.4.1\\lightroom.exe"
    

    I found most of the information here.

Posted in Linux, Ubuntu | 17 Comments

Using an embedded SQL database in Java web application

You have a Java web application needing a relational SQL database. According to JavaEE conventions, you declare the DataSource in web.xml:

<resource-ref>
    <res-ref-name>jdbc/DS</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

and then fetch it in code with (DataSource)new InitialContext().lookup("java:comp/env/jdbc/DS").

Using Maven, Jetty and HSQLDB, you can very easily run the web application for local testing without having to setup neither a web container, nor a database. Add this to pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.0.5.v20130815</version>
            <configuration>
                <webApp>
                    <contextPath>/${project.name}</contextPath>
                    <jettyEnvXml>src/etc/jetty-ds.xml</jettyEnvXml>
                </webApp>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.hsqldb</groupId>
                    <artifactId>hsqldb</artifactId>
                    <version>2.3.1</version>
                 </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

And add a file src/etc/jetty-ds.xml to your project:

<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure id="wac" class="org.eclipse.jetty.webapp.WebAppContext">
  <New id="DS" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg><Ref refid="wac"/></Arg>
    <Arg>jdbc/DS</Arg>
    <Arg>
      <New class="org.hsqldb.jdbc.JDBCDataSource">
        <Set name="DatabaseName">mem:db</Set>
        <Set name="User">SA</Set>
        <Set name="Password">""</Set>
      </New>
    </Arg>
  </New>
</Configure>

Then run it locally with mvn jetty:run. The database will be in-memory and does not presist between runs. It is also possible to configure HSQLDB to persist data on disk, see documentation.

If you want to have access to in-memory database in your tests, add this dependency to pom.xml:

<dependencies>
    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>2.3.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

And create the DataSource in your test code like this:

import org.hsqldb.jdbc.JDBCDataSource;

        JDBCDataSource hsqldbDs = new JDBCDataSource();
        hsqldbDs.setDatabaseName("mem:.");
        hsqldbDs.setUser("SA");
        hsqldbDs.setPassword("");

Note: This will not add the DataSource to JNDI.

Posted in Java, JavaEE, web | 3 Comments

GIMP 2.8 in Ubuntu 12.04

This is no longer relevant, since the PPA have updated to GIMP 2.8.10, which has fixed the bug.

If you use Ubuntu 12.04 LTS, you only have access to the old GIMP 2.6 in the standard repositories. This is unfortunate since GIMP 2.8 have a lot of new useful features.

The standard way to get GIMP 2.8 is to use the a PPA, as described here. This used to work fine. However, then the PPA upgraded to GIMP 2.8.6, which contains a horrible UI bug (seems like neither the GIMP team, nor the PPA maintainer has any particularly strong QA process).

I find this bug unacceptable, and the PPA has not yet released any new update. So I chose to use GIMP 2.8.4 instead. This is not available from the PPA, so I decided to build it from source.

I found some instructions for building here. However, I was unable to build GEGL since the latest version uses a newer version of a standard library than what is available in Ubuntu 12.04.

Fortunately, you can still use BABL and GEGL from the PPA, and only build GIMP itself from source.

This is what you need to do:

  1. Install the PPA (if you haven’t done so already), but do not install GIMP.
  2. Uninstall any version of GIMP already installed (from standard repository or PPA):

    sudo apt-get purge gimp gimp-data gimp-gmic gimp-plugin-registry libgimp2.0 gimp-help-common gimp-help-en gimp-lensfun
    
  3. Install various dependencies:

    sudo apt-get -y build-dep gimp
    sudo apt-get purge libgegl-0.0-dev libbabl-0.0-0-dev libgegl-0.0 libbabl-0.0-0
    sudo apt-get -y install gegl libgegl-0.2-0 libgegl-dev libbabl-0.1-0 libbabl-dev
    sudo apt-get -y install libavformat-dev libavcodec-dev valac-0.16 graphviz ruby1.9.1-dev liblua5.1-0-dev libopenexr-dev libopenraw-dev libexiv2-dev libsdl1.2-dev libgs-dev libjasper-dev checkinstall
  4. Build GIMP 2.8.4 from source:

    cd /tmp
    mkdir gimp_build && cd gimp_build
    export PATH=/opt/gimp-2.8/bin:$PATH
    wget ftp://ftp.gimp.org/pub/gimp/v2.8/gimp-2.8.4.tar.bz2
    tar -xjf gimp-2.8.4.tar.bz2
    cd gimp-2.8.4
    ./configure --prefix=/opt/gimp-2.8
    make
    sudo make install
    
  5. Make it possible to launch it from Unity:

    cp /opt/gimp-2.8/share/applications/gimp.desktop ~/.local/share/applications/
    

    and edit~/.local/share/applications/gimp.desktop to include this:

    Exec=/opt/gimp-2.8/bin/gimp-2.8 %U
    Icon=/opt/gimp-2.8/share/icons/hicolor/48x48/apps/gimp.png
    

Unfortunately, I have not managed to get the G’MIC GIMP plugin to work.

Posted in Linux, Ubuntu | Tagged | 3 Comments