GeoNetwork from Scratch II : Attack of the IDEs

We have already seen how to compile and run a basic GeoNetwork instance. Although we know that real developers will probably skip this step too, for new developers in GeoNetwork, it will be relief to have an IDE to work with. I know that many GeoNetwork developers use NetBeans or Intellij but as I am used to work with Eclipse, that’s what we are going to explore on this post.

First of all: Eclipse has better support for Maven projects on each version. So, to avoid headaches, just download the latest eclipse available.Eclipse has many installer tutorials, so I won’t stop here explaining how to run eclipse. I will just assume you know how to do it.

To run GeoNetwork from eclipse is very very easy. Just right click on the Package Explorer view to import -> As Maven Project over the folder you already had cloned on the last post:

Import As Maven Project
Import As Maven Project
Import As Maven Project
Import As Maven Project

There is still something Eclipse does not support right about GeoNetwork: we have a classes folder that Eclipse tends to misconfigure. So, go to that folder, right click and remove as source folder. To do this, go to the “web-app” project and right click on src/main/webapp/WEB-INF/classes. Select Build Path > Remove From BuildPath.

Then, completely remove the folder from the source code. Don’t worry, it’s git, you can recover it later. You can also do this by right-clicking on the folder and selecting Delete. Yes, you are sure you want to delete folder “classes“.

Now, update as maven project right clicking on the project “web-app” and selecting Maven > Update Project …

update project

Once this finishes, you can restore the folder we previously removed. Go to the “web-app” project, right click on src/main/webapp/WEB-INF and select Replace With >HEAD Revision. Yes, you are sure.

Congratulations! You are ready to use Eclipse to modify GeoNetwork.

But wait, how do we run GeoNetwork inside Eclipse to be able to debug?

We have several approaches here. Remember the jetty command to run GeoNetwork from the console? It is available also inside Eclipse (right click on web-app and Run As > Maven Build) and you can add some maven variables to be able to run in parallel a debug watch to debug your code. You can also set up a Tomcat server inside Eclipse and run GeoNetwork from it. This second option is more easy for beginners, so that’s what we are going to do now.

First, you have to create a Tomcat server inside Eclipse. So, search for the “Servers” tab and right click on it. Select New > Server. You will see a windows offering different types of servers. We will select the Tomcat v.7.0 Server one. You will probably won’t have any server runtime environment configured for it, but you can “Add…” a new one. There are many tutorials[1][2] for this, so we won’t stop here.

On the following window, you can select which applications to run. Obviously, you choose the one called “web-app” and Finish.

Now, you will have a new Server on the Servers tab. select it (left-click) and click on the green arrow just on the top of that tab. You will see on the “Console” tab all the output of GeoNetwork starting up. Once it is started, you can enter GeoNetwork the same way as before, using http://localhost:8080/geonetwork

Have fun customizing GeoNetwork!

GeoNetwork From Scratch I : The Phantom Catalog

GeoNetwork never has been an easy software to work with. But specially after the 3.0 version release, many things have changed. On this series of posts we will try to help new developers start with GeoNetwork.

The source code of GeoNetwork is available on a public repository on Github. This means that you can clone, fork and propose pushes of your custom changes. If you are not familiar with repositories of code or git, you should check this quick manual.

GeoNetwork is built using maven version 3+. It is written on Java and requires version 7 or more. It works both with OpenJDK or the Oracle version.You will need git and maven installed on your local machine to work. There are several ways to install this on your local machine; for example if you have a Debian based OS (like Ubuntu), you can install them with just this command:

sudo apt-get install maven git

Make sure you installed maven version 3!!

$ mvn –version
Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T18:37:52+01:00)
Maven home: ….

Remember that this will also install java on your system. You can check that the version is the right one with the following command:

java -version

So, the very first step once you have your environment set up is clone the GeoNetwork repository on your local machine. That can be done on the command line using the following command inside an empty folder where the source code will be populated:

cd yourEmptyFolder
git clone https://github.com/geonetwork/core-geonetwork.git
git submodule init
git submodule update

As you can see, all the source code shown on github is also available on your local machine now.

The source code of GeoNetwork is split on several smaller maven projects. To run GeoNetwork, you have to build all of them and run the project named “web“. If you are familiar to maven, you will probably have guessed that you have to run a package install command on the root folder of GeoNetwork source code. But if you try that, maven will warn you that for building GeoNetwork you need more memory than the default memory provided to maven. This means, you will have to export the maven options to increase the memory like this:

export MAVEN_OPTS=”-Xmx512M -XX:MaxPermSize=256M”

At this point we are not interested in running the tests, so you can skip them using the parameter “-DskipTests”:

mvn package install -DskipTests

At the end of this build (which can take long, depending on your network connection, as it has many third party libraries), you will see something like this:

[INFO] ————————————————————————
[INFO] Reactor Summary:
[INFO]
[INFO] GeoNetwork opensource ……………………….. SUCCESS [ 3.111 s]
[INFO] common utils ……………………………….. SUCCESS [ 13.678 s]
[INFO] Caching xslt module …………………………. SUCCESS [ 7.607 s]
[INFO] ArcSDE module (dummy-api) ……………………. SUCCESS [ 7.860 s]
[INFO] GeoNetwork domain …………………………… SUCCESS [ 33.785 s]
[INFO] Oaipmh modules ……………………………… SUCCESS [ 0.833 s]
[INFO] GeoNetwork Events …………………………… SUCCESS [ 0.654 s]
[INFO] GeoNetwork schema plugins ……………………. SUCCESS [ 4.646 s]
[INFO] GeoNetwork schema plugins core ……………….. SUCCESS [ 5.338 s]
[INFO] GeoNetwork schema plugin for ISO19139/119 standards SUCCESS [ 8.432 s]
[INFO] GeoNetwork core …………………………….. SUCCESS [ 16.304 s]
[INFO] GeoNetwork schema plugin for Dublin Core records retrieved by CSW SUCCESS [ 5.031 s]
[INFO] GeoNetwork schema plugin for Dublin Core standard . SUCCESS [ 8.419 s]
[INFO] GeoNetwork schema plugin for ISO19110 standard …. SUCCESS [ 3.627 s]
[INFO] GeoNetwork CSW server ……………………….. SUCCESS [ 5.546 s]
[INFO] GeoNetwork harvesters ……………………….. SUCCESS [ 3.888 s]
[INFO] GeoNetwork health monitor ……………………. SUCCESS [ 2.489 s]
[INFO] GeoNetwork services …………………………. SUCCESS [ 8.597 s]
[INFO] Geonetwork Web Resources 4 Java ………………. SUCCESS [ 5.261 s]
[INFO] Cobweb Customizations ……………………….. SUCCESS [ 4.226 s]
[INFO] GeoNetwork INSPIRE Atom ……………………… SUCCESS [ 3.990 s]
[INFO] Tests for schema plugins …………………….. SUCCESS [ 2.334 s]
[INFO] GeoNetwork user interface module ……………… SUCCESS [ 35.356 s]
[INFO] JS API and Service documentation ……………… SUCCESS [ 21.203 s]
[INFO] GeoNetwork web client module …………………. SUCCESS [ 47.484 s]
[INFO] GeoNetwork Web module ……………………….. SUCCESS [ 48.490 s]
[INFO] GeoNetwork E2E Javascript Tests ………………. SUCCESS [ 1.645 s]
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 02:19 min (Wall Clock)
[INFO] Finished at: 2015-07-17T10:36:43+01:00
[INFO] Final Memory: 232M/441M
[INFO] ————————————————————————

This will generate a war file you can use in any Java Application Container (server) like Tomcat on web/target/geonetwork.war

Congratulations! You are ready to run GeoNetwork. To do this, just go to the web folder and run jetty in there:

cd web; mvn jetty:run

After jetty starts, you can see your running GeoNetwork by opening a browser and enter to http://localhost:8080/geonetwork

Continue in Attack of the IDEs

¿Qué es GeoNetwork?

GeoNetwork es una aplicación web que permite mantener un catálogo de datos referenciados geográficamente. Esto es, un portal con buscador que permite visualizar metadatos combinándolos con mapas. Sigue estrictamente los diferentes estándares para datos, desde Inspire hasta OGC. Esto ha propiciado su gran expansión en muchas organizaciones, como el geoportal suizo o el brasileño, pasando por el neozelandés.

Se despliega sobre un contenedor de aplicaciones java (como tomcat o jetty), funcionando sobre el framework Jeeves. Jeeves se basa en transformaciones XSLT que permiten un desarrollo rápido y sencillo (a la vez que potente) de interfaces tanto para un usuario como para máquinas (XML).  Esto hace que los datos en GeoNetwork sean fácilmente accesibles por diferentes plataformas.GeoNetwork is a web application that allows you to maintain a geographic referenced metadata catalogue. This means, a search portal that allows you to view metadata combined with maps. It strictly follows different standards for metadata, from Inspire to OGC. This has allowed GeoNetwork to expand to a lot of organizations, like the swiss geoportal or the brasilian one, not forgetting the New zealander.

It is deployed inside a java application container (like tomcat or jetty), working over the Jeeves framework. Jeeves is based on XSTL transformations that allows a simple quick development (and powerfull) of interfaces, for humans or machines (XML). This makes metadata from GeoNetwork to be easily accesible by different platforms.

Anotaciones en Java

Las anotaciones o decoradores sobre el código se han vuelto muy comunes en los últimos tiempos. Permiten al programador añadir información útil extra ya sea para comentar mejor el código o para modificar la forma de compilar/ejecutar una clase concreta. Son una extensión a Java para permitir la programación orientada a aspectos.

Las anotaciones pueden ser de tres tipos, según el momento en el que son visibles:

Información para el Compilador

Estas anotaciones permiten al compilador indicar si debe o no omitir errores y warnings o qué hacer con ellos. A nada que se haya trabajado con un IDE Java (como eclipse) se habrán utilizado este tipo de anotaciones de forma natural, por ejemplo usando @Override sobre una función para indicar que está sobreescribiendo una declaración definida en una clase padre. Esta anotación es completamente opcional,  pero permite tanto al compilador como al desarrollador comprobar que efectivamente se está sobreescribiendo una funcionalidad existente por herencia.

Por ejemplo:

public class Padre {     
    public void do(){
        System.out.println("Padre");
     }
}

public class Hijo extends Padre{     
    @Override
    public void do(){
        System.out.println("Hijo");
     }
}
Anotaciones en Tiempo de Compilación y Despliegue

Estas anotaciones permiten añadir información extra al compilador para que modifique la forma en la que se genera el código de los ficheros .class. Puede servir para modificar clases (añadiendo o modificando funcionalidad respecto a lo descrito en el código fuente), generar clases nuevas (por ejemplo en base a un fichero descriptor), etc…

Estas anotaciones sólo serán visibles en este punto, es decir, no se escribirán sobre los .class y por tanto no se podrán consultar en tiempo de ejecución.

Anotaciones en Tiempo de Ejecución

Estas anotaciones pueden ser consultadas en tiempo de ejecución y funcionan de forma muy parecida a como se utilizaría una interfaz.

Veamos directamente un ejemplo de cómo crear una anotación Runtime y cómo se puede utilizar. La anotación MyAnnotation se podrá aplicar a elementos de tipo field, es decir, a atributos de una clase:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
     public @interface MyAnnotation {
}

Ahora podemos crear una clase que esté anotada con esta anotación:

public class myObject
 {
 @MyAnnotation
 public String field;
 }

De esta forma, en cualquier otra parte del código, podemos comprobar mediante reflexión si un objeto tiene un campo marcado con la anotación:

Class<?> res = objeto.getClass();
for (Field f : res.getFields()) {
     if (f.isAnnotationPresent(MyAnnotation.class)) {
          System.out.println("OK");
      }
}

Más información:

The annotations on the code or decorators have become very common. They allow the programmer to add additional useful information about how to improve the code or change how to compile / run a particular class. They are a Java extension to allow aspect-oriented programming.

There are three types of annotations, according to the moment when they are used:

Information for the Compiler

These annotations allow the compiler to indicate whether or not to ignore errors and warnings or what to do with them. If you’ve worked with a Java IDE (like eclipse) probably you would have used this type of annotations, for example using @Override on a function to indicate that you are overwriting a method defined on a parent class. This annotation is completely optional, but allows both the compiler and the developer to check that they are indeed overwriting existing hierarchical functionality.

For example:

public class Parent {     
    public void do(){
        System.out.println("Parent");
     }
}

public class Son extends Parent{     
    @Override
    public void do(){
        System.out.println("Son");
     }
}
Compiler-time and deployment-time processing

These annotations allow the compiler to add extra information about how to generate the code. It can serve to modify classes (adding or modifying functionality from those described in the source code), create new classes (based on a file descriptor), etc …

These annotations will only be visible at this point, they are not written on the .class files and therefore they cannot be available at runtime.

Runtime Annotations

This annotations can be used on runtime and they work on a very similar way as an interface.

Let’s see an example on how to create a Runtime Annotation and how can we use it. The annotation named MyAnnotation can be applied to elements oftype field:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
     public @interface MyAnnotation {
}

Now we can create an annotated class by this annotation:

public class myObject
 {
 @MyAnnotation
 public String field;
 }

This way, we can check by reflection if an object has an annotated field on any part of the code:

Class<?> res = objeto.getClass();
for (Field f : res.getFields()) {
     if (f.isAnnotationPresent(MyAnnotation.class)) {
          System.out.println("OK");
      }
}

More Information: