Mittwoch, 5. August 2015

Debug Tomcat Listener Startup



On startup Tomcat usually does not log detailed information about a failed listener start. Put the following file into the root of class path to enable better logging in catalina.out:


logging.properties

org.apache.catalina.core.ContainerBase.[Catalina].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].handlers = java.util.logging.ConsoleHandler

Montag, 19. Januar 2015

Proguard android obfuscation

To prevent your android app from decompilation and analyzation it is highly recommended to obfuscate the APK file before deployment to play store. Proguard is the proposed tool for this job and already contained in the SDK.

To use Proguard with an Android application a special configuration is required. The JDC already contains an example for a simple Android app. But in combination with Roboguice dependency injection or Gson JSON serialization an extended configuration is required.

Roboguice

If you choose Roboguice for dependency injection in you android app, I recommend to use only the obfuscation functions of Proguard. Also make sure the Roboguice classes are not obfuscated and attributes are not changed. Try the following example configuration:

-ignorewarnings
-dontshrink
-keep class roboguice.** { *; }

-keepattributes *Annotation*
-keep public class roboguice.** { *; }

-keep class **.Finalizer
-keepclassmembers class ** { *** startFinalizer( ... ); }

-keepclassmembers class * {
  void *(**On*Event);
}

-keepattributes **

GSON JSON serialization

The serialization of java objects makes often problems in combination with obfuscation. The produce a consistent object serialization it is required to prevent attribute names of objects and enumerations from obfuscation.


One possibility is to use @SerializedName annotation do set a static name for an attribute, which is not affected from obfuscation.

class TestObject {
  
  @SerializedName("staticName") 
  private String testField;
}

An other solution is the usage of a marker interface e.g. GsonSerializable:

public interface GsonSerializable {}


Use this interface to mark classes and enumerations which should be serialized by Gson. The following pro guard configuration ensures that the deserialize objects are always serialized in the same way:

# keep GsonSerializable interface, it would be thrown away by proguard since it is empty
-keep class de.havre.copymeter.model.GsonSerializable

# member fields of serialized classes, including enums that implement this interface
-keepclassmembers class * implements de.havre.copymeter.model.GsonSerializable {
    <fields>;
}

# also keep names of these classes. not required, but just in case.
-keepnames class * implements de.havre.copymeter.model.GsonSerializable

Links

https://sites.google.com/site/gson/
https://github.com/roboguice/roboguice/
http://proguard.sourceforge.net/

Mittwoch, 3. Dezember 2014

Bean Cloner




Sometimes you need a deep copy of bean structure but no clone constructors are available. Cloner offers a fast reflection based methods to clone object structures. It is also possible to integrate Cloner into Spring.


Code Snippet:

class CloneTest {
  private static final Cloner CLONER = new Cloner(); 
  
  public static void main(String[] args) {
    Object original = new Object();
    Object clone = CLONER.deepClone(tds);
  }
}


Details:
https://code.google.com/p/cloning/wiki/Usage

Maven:
 <dependency>
    <groupId>uk.com.robust-it</groupId>
    <artifactId>cloning</artifactId>
    <version>1.7.4</version>
</dependency>

Dienstag, 17. Juni 2014

Generating JAXB Beans from XSD with Maven

The following example shows a simple maven plugin configuration to generate Java beans from a XSD. I use the “maven-jaxb22-plugin” because it allows a simple integration of plugins to adjust the Bean generation. Leave the following arguments if you don’t need the feature:
  • Xsetters - Generates setters to beans
  • XtoString - Generates common to string method to beans  
  • Xequals - Generates a equals method to beans  
  • XhashCode - Generates a hashcode method to beans 
  • Xfluent-api - Generates a fluent API to beans

1:  <plugin>  
2:   <groupId>org.jvnet.jaxb2.maven2</groupId>  
3:   <artifactId>maven-jaxb22-plugin</artifactId>  
4:   <executions>  
5:    <execution>  
6:     <id>Generating Asset Admin Api Beans</id>  
7:     <goals>  
8:      <goal>generate</goal>  
9:     </goals>  
10:     <configuration>  
11:      <encoding>UTF-8</encoding>  
12:      <forceRegenerate>true</forceRegenerate>  
13:      <schemaDirectory>${basedir}/src/main/resources/xsd/</schemaDirectory>  
14:      <generatePackage>de.lehavre.demo</generatePackage>  
15:      <schemaIncludes>  
16:       <include>model1.xsd</include>  
17:       <include>model2.xsd</include>  
18:      </schemaIncludes>  
19:      <extension>true</extension>  
20:      <args>  
21:       <arg>-Xsetters</arg>  
22:       <arg>-XtoString</arg>  
23:       <arg>-Xequals</arg>  
24:       <arg>-XhashCode</arg>  
25:       <arg>-Xfluent-api</arg>  
26:      </args>  
27:      <plugins>  
28:       <plugin>  
29:        <groupId>org.jvnet.jaxb2_commons</groupId>  
30:        <artifactId>jaxb2-basics</artifactId>  
31:       </plugin>  
32:       <plugin>  
33:        <groupId>net.java.dev.jaxb2-commons</groupId>  
34:        <artifactId>jaxb-fluent-api</artifactId>  
35:        <version>2.1.8</version>  
36:       </plugin>  
37:      </plugins>  
38:     </configuration>  
39:    </execution>  
40:   </executions>  
41:  </plugin>  

Generating Fluent Builder with IntelliJ



The JavaBean getter and setter usage is often a bit laborious. I like to use a fluent API, but I don't like to implement these methods direly in the bean class. There is a nice IntelliJ Plugin named "Builder-Genarer", which generates a Builder from POJO.


Just open class and press ALT-SHIFT-B

Plugin: http://plugins.jetbrains.com/plugin/6585

Mittwoch, 4. Juni 2014

Fast creation of test data



Do you know this problem? You spend too much time into developing unit tests, because you have to fill complex beans with test data? The PodamFactory offers a very easy way to create test data Pojos with reflection:


import de.havre.CpeBo;
import org.junit.Test;
import uk.co.jemos.podam.api.PodamFactory;
import uk.co.jemos.podam.api.PodamFactoryImpl;

public class PojoTest
{

  CommitmentConfigStateHandler sut;

  PodamFactory factory = new PodamFactoryImpl();

  @Test
  public void testProcessConfigState_success() throws Exception
  {
    ComplexBean pojo = factory.manufacturePojo(ComplexBean.class);
    sut.methodToTest(testDataBean);
  }
}

Maven dependency:

<dependency>
  <groupId>uk.co.jemos.podam</groupId>
  <artifactId>podam</artifactId>
  <version>3.6.0.RELEASE</version>
  <scope>test</scope>
</dependency>