How to delete recursively nested directories in Linux by name

February 5, 2010 germanogiudici Leave a comment
find . -type d -name NOME-DIRECTORY -exec rm -rf {} \;

thanks to Mirko…

Categories: My Work Tags:

How to exclude some nested dependencies with Maven

February 2, 2010 germanogiudici Leave a comment

With Maven it’s possible to have some configuration problems due to nested dependencies import (e.g. a project dependency importing another dependency).

We can resolve this problem using the following configuration:

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
</exclusion>
</exclusions>
</dependency>
In this example I’ve excluded the import of the nested slf4j-jdk14 library.
Categories: My Work Tags:

Java Day – Rome 30 01 2010

January 14, 2010 germanogiudici Leave a comment

Next 30 january at Rome, there will be one of the most important event in the java comunity.

Here more infos (italian)

Categories: My Work

How to download dependencies sources via Maven

December 10, 2009 germanogiudici Leave a comment

You can use the dependency plugin adding this to your pom.xml  (i’ve reported the pom’s build section):

....
<build>

<finalName>booking-mvc</finalName>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<configuration>

<source>1.5</source>

<target>1.5</target>

</configuration>

</plugin>
<!-- you have to insert this plugin definition-->
<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-dependency-plugin</artifactId>

</plugin>

</plugins>

</build>

...
then you can use the command mvn dependency:sources.
The sources will be downloaded into the mvn local repository, together with the dependency itself.
This is very useful with maven modules in intellij idea because it links automatically sources and jars, configuring debugging too.
Categories: My Work

How to write exception stack trace on a String

November 27, 2009 germanogiudici Leave a comment

Sometimes, we have the necessity to transform the exception stack trace in a string (e.g. for using a log method whose parameter is a string).

This is the code I use:

StringWriter sw = new StringWriter();
PrintWriter printWriter = new PrintWriter(sw);
ex.printStackTrace(printWriter);
log.write(sw.toString());
Categories: My Work

When == return true in String comparison?

November 5, 2009 germanogiudici 1 comment

In java String objects are pooled for performance issue. There are two different moments during which new strings are inserted into the pool. During compile time, literals are inserted into the pool so you could(but you must not) use == to compare two literals. At runtime, strings aren’t pull back from the pool… So e comparison between a literal value and a runtime value MUST use equals(). To decrease the memory footprint we can use the intern() method, which returns the string from the pool if it exists otherwise it will be inserted.

public class Test {
//to run with a a
public static void main(String[] args) throws Exception{

String literal1 = "a";
System.out.println("arg[0] = " + args[0]);
System.out.println("arg[1] = " + args[1]);
String runtime1 = args[0];
String intern1 = args[1].intern(); //retrieved from pool
String literal2 = "a";
System.out.println("literal1==runtime1: "+(literal1==runtime1)); //false
System.out.println("literal1==literal2: "+(literal1==literal2)); //true
System.out.println("runtime1==intern1: "+(runtime1==intern1)); //false
System.out.println("literal1==intern1: "+(literal1==intern1)); //true
System.out.println("literal1.equals(runtime1): "+(literal1.equals(runtime1))); //true

}
}

http://forums.sun.com/thread.jspa?threadID=669414


Categories: My Work

Thread-safe singleton

October 11, 2009 germanogiudici Leave a comment

It seems that the better way to implement a thread safe singleton in java is using an holder that solves the lazy initialization too:

public class ConfigurationRegistry
{
private ConfigurationRegistry()
{
}

public static ConfigurationRegistry getInstance()
{
//Holder.instance is lazy initialized the first time access...
return Holder.instance;
}

private static class Holder
{
//single line thread safe
static final ConfigurationRegistry instance = new ConfigurationRegistry();
}

}
Categories: My Work

Java and DST (daylight saving time)

Java uses time zone to mark a time that is relative to a DST period.
This is important in system integration. E.g. I have a system1 sending an xml file containing a date to another system2, these systems are located in the same country (having the same time zone).
Say that system2 parses the xml date ignoring its time zone, this could be a problem if the xml will be sent before DST and will be received during DST.
So: 14:00 +1 (before DST) is 14:00 +2 (during DST) and not 15:00 +1! If system2 parses the date without using the time zone it will remain 14:00 and will not became 15:00.

Categories: My Work

Instrumenting an ear with cobertura and ant

Using cobertura with ant is very simple, this is the build xml I’ve used:

################START build.xml################

<?xml version="1.0" encoding="UTF-8"?>

<project name="labs.cobertura.generic" basedir=".">

<description>
Generic file to instrument a an already compiled jar
precondition: having a jar normally compiled
usege: after the instrumentation, we have to insert the instrumentated jar,
containing the same classes as the original jar, into a location where it will be loaded.
(e.g. in the classpath startup of the application server)
author: Germano Giudici
</description>

<property file="genericBuild.properties" />

<path id="cobertura.classpath">
<fileset dir="${cobertura.dir}">
<include name="*.jar" />
</fileset>
</path>

<taskdef classpathref="cobertura.classpath" resource="tasks.properties" />

<target name="instrument-jar">
<mkdir dir="${instrumented.dir}" />
<cobertura-instrument datafile="${basedir}/cobertura.ser" todir="${instrumented.dir}">
<fileset dir=".">
<include name="${original-jar-name}" />
</fileset>
</cobertura-instrument>
</target>

<target name="coverage-reports">
<mkdir dir="${coverage.xml.dir}" />
<cobertura-report datafile="${basedir}/cobertura.ser" srcdir="${src.dir}" destdir="${coverage.xml.dir}" format="xml" />

<mkdir dir="${coverage.html.dir}" />
<cobertura-report datafile="${basedir}/cobertura.ser" destdir="${coverage.html.dir}">
<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
</cobertura-report>
</target>



<target name="clean">
<delete dir="${instrumented.dir}" />
<delete dir="${reports.dir}" />
<delete file="cobertura.log" />
<delete file="cobertura.ser" />
<delete file="cobertura.ser.lock" />
</target>
</project>

################END build.xml################

################START genericBuild.properties################

# The path to cobertura.jar
cobertura.dir=../lib

 

# the path to the instrumented classes' sources
src.dir=../src
instrumented.dir=instrumented
reports.dir=reports
coverage.xml.dir=${reports.dir}/cobertura-xml
coverage.html.dir=${reports.dir}/cobertura-html
# The name of the jar to be instrumented. It must reside in the same sirectory of this file
original-jar-name=mnp.ear

################END genericBuild.properties################

I’ve used this to instrument a jar inside an ear, deployed to weblogic 8.1.4.

For default cobertura writes all stats whenever the jvm ends… to semplify this task I’ve deployed a simple servlet containing this code:

String className = "net.sourceforge.cobertura.coveragedata.ProjectData";
String methodName = "saveGlobalProjectData";
Class saveClass = Class.forName(className);
java.lang.reflect.Method saveMethod = saveClass.getDeclaredMethod(methodName, new Class[0]);
saveMethod.invoke(null, new Object[0]);

So when I want to flush the statistics gained by cobertura, all I’ve to do is invoke that servlet.

It’s important to start weblogic (or even wathever other AS) with the cobertura.jar in the classpath.

The statistics flush whill generate a file called cobertura.sar in the domain’s directory, then you can use it to generate the report (with coverage-reports task ).

Categories: My Work

No more “Unknown source” on exception stacktraces with code compiled with ant

Add this to javac tag: <javac srcdir=”${my-source}” destdir=”${my-dest}/classes” debug=”true” debuglevel=”lines,vars,source”>

Categories: My Work