Remote debugging jetty

To remote-debug jetty running on the command line, put this before you execute mvn jetty:run

export MAVEN_OPTS="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"

Then debug on port 8000 in your IDE.

Posted in Linux | Comments Off

Reset password for git

If your git password changed, and you need to push / pull something, you will get an authentication error. To reset the password on OSX (Mac), enter this:

git credential-osxkeychain erase
host=github.com
protocol=https

After that, if you try to push / pull once again, you will be prompted to re-enter your (new) password.

Posted in Technology | Comments Off

Installing Java on OpenSuse Linux via RPM

To install linux via console on an OpenSuse machine, do the following:

1. Download the fitting RPM from Oracle:

On the console, enter uname -m. If you get x86_64, you need the 64 bit version of the RPM file. Otherwise take the 32 bit version.
Go to java.oracle.com. Find the download page and copy the link for the correct version. The problem you will now face is that you cannot directly download the file using for example ‘wget’, because you need to send a cookie to the server which says that you accepted the software license for the download. But you can send the cookie via console as well, like this:

wget --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u45-b14/jdk-8u45-linux-x64.rpm

This should start the download

2. You should now be able to install the RPM

First, become the super user by typing:

su

Then type:

rpm -ivh jdk-8u45-linux-x64.rpm

(Of course the name of your file might be different).

If you get this error:

‘/usr/sbin/alternatives is needed by jre…’,

then you have to create a symlink first. So do this:

ln -s /usr/sbin/update-alternatives /usr/sbin/alternatives

Then type this, and it should work:

rpm -ivh --nodeps jdk-8u45-linux-x64.rpm

(Note the –nodeps argument here)

Now you should remove the ‘old’ java that is pre-installed with Suse:

zypper rm icedtea-web

3. Point the system to the new java version

Finally, point to the new installation:

update-alternatives --install "/usr/bin/java" "java" "/usr/java/latest/bin/java" 1

update-alternatives --set java /usr/java/latest/bin/java

4. Finally, to also make the new java work with your web browser, do this (if you have a 64 bit system):

ln -s /usr/java/latest/lib/amd64/libnpjp2.so /usr/lib64/browser-plugins/

If you have a 32 bit system, do this instead:

ln -s /usr/java/latest/lib/i386/libnpjp2.so /usr/lib/browser-plugins/

5. Test if Java is available

by typing

java -version

This should now show the version you downloaded earlier.

6. If you cannot execute Java programs now, try to set the JAVA_HOME variable:

export JAVA_HOME=/usr/java/latest

Posted in Linux | Comments Off

Finding text inside a zip file

If you need to find text that is somewhere inside a zip file, you can’t use the linux ‘grep’ tool. But you can use another tool: zipgrep.
The syntax is as follows (to find ‘myText’ inside all .jar files).

find . -name "*.jar" -exec zipgrep "myText" '{}' \;

Posted in Linux | Comments Off

Json marshaling and field names

If you are using Jackson to marshal and unmarshal Java objects and Json, you might get different behavior depending on how the marshaling is called. For example, I was first using marshaling using a Java-RS webservice, which works something like this:

    @GET
    @Path("myObject")
    @Produces(MediaType.APPLICATION_JSON)
    public MyObject getMyObject() {
      ...
     return new MyObject();
    }

This returns the object as Json. And if the object has getters like ‘getXValues()’, then the Json will look like this:

{"XValues": ...}

Then I switched to Spring MVC, and now my method looks like this:

    @RequestMapping(value = "/myObject",method= RequestMethod.GET, produces=MediaType.APPLICATION_JSON)
    public Quote getQuoteGold() {
     ...
    }

And the Json looks like this:

{"xvalues": ...}

So the upper case is not represented. Then if you want to access the fields explicitly later, you will run into problems. Especially if you can’t change the receiving end any more.

The way to get out of this is to explicitly tell the marshaling library how to name the fields in Json. This will work like this:

@XmlRootElement
public class MyObject { 

    @JsonSerialize
    @JsonProperty("XValues")
    public List<Long> getXvalues() {
        return xValues;
    }

Now the Json will name the field according to what you tell it to.
It is worth noting that I found two annotations with the same name, (but from different packages) for these two properties (JSonSerialize, JsonProperty), and only one works. For me these were

com.fasterxml.jackson.annotation.JsonProperty;
com.fasterxml.jackson.databind.annotation.JsonSerialize;
Posted in Uncategorized | Comments Off

Align a child layout in the middle of other content

I have repeatedly faced the following problem when creating a layout in Android: I have a relative layout, and I have some content at the top (a header or some text for example), then a big thing in the center of the layout (like a chart), which I wrap in it’s own layout, and below that, I want some more content (some buttons or something).

The problem is that when you do that with a relative layout, you would probably first try something like this:

    <TextView
        android:id="@+id/textAtTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <LinearLayout
        android:id="@+id/chartInMiddle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/textAtTop"
        android:orientation="vertical"/>

    <Button
		android:id="@+id/buttonBottom"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_below="@+id/chartInMiddle" />

Seems logical, right? I’m using a relative layout, so I expect to be able to say that the chart goes below the text, and the button goes below the chart. At that should work.
Well, at least for me, this does not work. The chart will take up all the space up to the bottom, pushing the button out of the visible area.

What you need to do here is to define everything going from the chart. So you don’t say that the button is below the chart. You say that the text is above the chart, and that the button is below the chart. But you define this all from within the chart definition

So you would do it like this:

    <TextView
        android:id="@+id/textAtTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <LinearLayout
        android:id="@+id/chart"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/buttonBottom"
        android:layout_below="@+id/textAtTop"
        android:orientation="vertical"/>

    <Button
		android:id="@+id/buttonBottom"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_alignParentBottom="true"
		android:layout_alignLeft="@+id/chart"
		android:layout_marginTop="10dp" />

So the button is now anchored at the end of the outermost layout (the relative layout). And the linear layout in the middle defines what’s around it. This makes the layout behave the way you expect it to.

Posted in Technology | Comments Off

How to install Git on Suse Linux

To install git on Suse, you have to first add the repository where it can be found. Suse provides this. For the correct version, you can open the link in the browser and look around at http://download.opensuse.org/repositories:

sudo zypper addrepo http://download.opensuse.org/repositories/devel:/tools:/scm/SLE_11_SP2/devel:tools:scm.repo

Then:

sudo zypper install git-core

If Suse complains about some perl dependency, this will also need to be registererd and downloaded:

Problem: nothing provides perl-Error needed by git-core-2.1.3-219.2.x86_64

So do this:

sudo zypper addrepo http://download.opensuse.org/repositories/devel:/languages:/perl/SLE_11_SP3/devel:languages:perl.rep

sudo zypper install perl-Error

Posted in Linux | Comments Off

Google App Engine, JPA (datanucleus) and maven

I just managed to get persistence working in the Google App Engine, using JPA and datanucleus. I had a lot of trouble with this, because I’m building the project using maven, and along the way I came across many versioning issues etc. This is how it works for me now:

I’m using the dependencies as outlined here:

DataNucleus App Engine Plugin Compatibility

Before I found this, I was happily throwing in random versions of datanucleus dependencies etc. Obviously, that is not going to work in most cases. Specifically, there are a lot of configurations out there that use deprecated packages. For example ‘datanucleus-jpa’, which is now called ‘datanucleus-api-jpa’.

Also, I got some errors with duplicate references for dependencies. These need to be resolved by excluding some dependencies from some declarations, as shown in the code below.

You will also note the plugin for enhancing the model classes (maven-datanucleus-plugin). If you don’t include this, JPA will not be able to use your classes as database models.

The pom that worked for me in the end is this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>

    <groupId>de.martinfromme</groupId>
    <artifactId>android-webservice</artifactId>

    <properties>
        <appengine.app.version>1</appengine.app.version>
        <appengine.target.version>1.9.10</appengine.target.version>
        <datanucleusVersion>3.1.1</datanucleusVersion>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- Compile/runtime dependencies -->
        <dependency>
            <groupId>com.google.appengine</groupId>
            <artifactId>appengine-api-1.0-sdk</artifactId>
            <version>${appengine.target.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-bundle</artifactId>
            <version>1.18.1</version>
        </dependency>
        <dependency>
            <!-- Google App Engine supports both JPA1 and JPA2.
      JPA1: datanucleus appengine plugin v1 (1.0.10) + datanucleus v1 (1.1.5)
      JPA2: datanucleus appengine plugin v2 (2.1.1) + datanucleus v3 (3.1.1) -->
            <groupId>com.google.appengine.orm</groupId>
            <artifactId>datanucleus-appengine</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <!-- datanucleus-core is not needed for compilation. in fact, it cannot have compile
      scope because the datanucleus plugin automatically adds it during enhancement and complains
      if there are two copies. app should not depend on any classes in this lib anyways. -->
            <groupId>org.datanucleus</groupId>
            <artifactId>datanucleus-core</artifactId>
            <version>${datanucleusVersion}</version>
            <scope>runtime</scope>
            <exclusions>
                <exclusion>
                    <groupId>javax.transaction</groupId>
                    <artifactId>transaction-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <!-- datanucleus-jpa is needed during compilation for its
      @Extension annotation which is used throughout entity classes -->
            <groupId>org.datanucleus</groupId>
            <artifactId>datanucleus-api-jpa</artifactId>
            <version>${datanucleusVersion}</version>
        </dependency>
        <dependency>
            <!-- normally this library is only needed when using JDO however datanucleus throws
      some JDO exceptions even when using JPA. there are several places in our code that
      explicitly catch these JDO exceptions therefore it is needed during compilation -->
            <groupId>org.datanucleus</groupId>
            <artifactId>datanucleus-api-jdo</artifactId>
            <version>${datanucleusVersion}</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.transaction</groupId>
                    <artifactId>transaction-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.0.0</version>
        </dependency>

        <!-- Test Dependencies -->
        <dependency>
            <groupId>com.google.appengine</groupId>
            <artifactId>appengine-testing</artifactId>
            <version>${appengine.target.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.appengine</groupId>
            <artifactId>appengine-api-stubs</artifactId>
            <version>${appengine.target.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <version>2.5.1</version>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <archiveClasses>true</archiveClasses>
                    <webResources>
                        <!-- in order to interpolate version from pom into appengine-web.xml -->
                        <resource>
                            <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                            <filtering>true</filtering>
                            <targetPath>WEB-INF</targetPath>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>

            <plugin>
                <groupId>com.google.appengine</groupId>
                <artifactId>appengine-maven-plugin</artifactId>
                <version>${appengine.target.version}</version>
            </plugin>

            <!-- This plug-in "enhances" your domain model objects (i.e. makes them
                persistent for datanucleus) -->
            <!--  Might not need this, appengine is supposed to do this appengine:enhance -->
            <plugin>
                <groupId>org.datanucleus</groupId>
                <artifactId>maven-datanucleus-plugin</artifactId>
                <version>${datanucleusVersion}</version>
                <configuration>
                    <mappingIncludes>**/model/*.class</mappingIncludes>
                    <metadataIncludes>**/model/*.class</metadataIncludes>
                    <verbose>false</verbose>
                    <enhancerName>ASM</enhancerName>
                    <api>JPA</api>
                </configuration>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>enhance</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <!-- force maven-datanucleus-plugin to use the same version of datanucleus-core -->
                        <groupId>org.datanucleus</groupId>
                        <artifactId>datanucleus-core</artifactId>
                        <version>${datanucleusVersion}</version>
                    </dependency>
                </dependencies>
            </plugin>

        </plugins>
    </build>
</project>

Finally, JPA complained about entities that are not configured correctly (“Type XXX is not that of an entity but needs to be for this operation”).
Turns out that you need to specify the classes that are entities inside your persistence.xml, like this:

    <persistence-unit name="transactions-optional">
        <provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
        <class>de.martinfromme.webservices.model.QuoteModel</class>
        <properties>
            <property name="datanucleus.NontransactionalRead" value="true"/>
            <property name="datanucleus.NontransactionalWrite" value="true"/>
            <property name="datanucleus.ConnectionURL" value="appengine"/>
            <property name="datanucleus.singletonEMFForName" value="true"/>
        </properties>
    </persistence-unit>

The key thing is to include the used JPA enabled classes in the ‘class’ tag.

Posted in Technology | Comments Off

Finding out which Java processes a user started

I just tried to remote attach to a JVM, but the remote agent told me that there are no java processes running. So I assumed it was someone else who started the process, and I have to attach as that user. So an easy way to find out all processes started by some user, do this:

Go to the directory

$TMP/hsperfdata_$USER,

which is created by the hot spot JVM. It will contain PID files for all running JVMs started by the user.

Posted in Linux | Comments Off

Using character classes with sed in Mac OS

I recently had the problem that I could not extract a sequence of digits from a string using sed. It stumped me, because I thought that this should not be so hard. What I tried was

echo "abc_123_def" | sed 's/.*_\([0-9]\+\)_.*/\1/g'

So I wanted to extract ‘123’ from ‘abc_123_def’. But it didn’t work.
Then I realized I’m on a Mac, and the Mac OS sed is different from the standard GNU linux one. For example, the ‘+’ operator does not work the way I’m used to. So what I did is use homebrew to install gnu-sed:

homebrew install gnu-sed

Then I can use gnu-sed like this

echo "abc_123_def" | gsed 's/.*_\([0-9]\+\)_.*/\1/g'

and the output is ‘123’ like I want it.

Posted in Linux | Comments Off