Thursday, June 2, 2016

Display the value of a property with Maven

Sometime you need to know the value of a property in you maven scripts.
This happend to me when there were to many profiles.

So can just write the value of a property.
Add this plugin definintion in you build section
et voilĂ 


<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.1</version>
  <executions>
    <execution>
      <phase>validate</phase>
      <goals>
        <goal>run</goal>
      </goals>
      <configuration>
       <tasks>
         <echo>Displaying value of 'myProperty' property</echo>
         <echo>[myProperty] ${myProperty}</echo>
       </tasks>
      </configuration>
    </execution>
  </executions>
</plugin>

Thank you http://www.avajava.com/tutorials/lessons/how-do-i-display-the-value-of-a-property.html

Tuesday, April 5, 2016

Accessing an in-memory H2 Database

Some times while trying to understand what is going on in your unit tests, you might want to see what is written in an in-memory H2 database.
You can do this :)

You have to start a server in your test.

Server.createWebServer("-web", "-webAllowOthers", "-webPort", "8082").start();

and you can then access the db via a web browser at following address: http://localhost:8082/ In order to access the db you will have to stop your unit test thread. Do something like

boolean sleep = true;
while(sleep)
    Thread.sleep(10000);

This will allow you to connect to the db.

Friday, January 29, 2016

Decorator Pattern with Spring

The decorator pattern is well known and described.
See for example https://en.wikipedia.org/wiki/Decorator_pattern

 The question is how can we implement easily the pattern with Spring.

 Lets just do an example.
 We have a SearchService which can be configuratively be switch on or off.

We have a simple interface
public interface SearchService{
    List find(Param p);
}
and a simple implementation
@Component
public class DefaultSearchService implements SearchService{
    public List find(Param p){
        repository.find(p);
    }
}
Our decorator logically implements the SearchService interface as well
@Component
public class SearchServiceDecorator implements SearchService{

    private final SearchService searchService;
    private final Config config;

    @Autowired
    public SearchServiceDecorator(SearchService searchService, Config config){
        this.searchService = searchService;
    }

    public List find(Param p){
        if(config.allowed())
            searchSerice.find(p);
    }
}
This will obviously not work. Spring cannot decide which version of SearchService to use. The solution is quite elegant:
@Component
@Primary
public class SearchServiceDecorator implements SearchService{

    private final SearchService searchService;
    private final Config config;

    @Autowired
    public SearchServiceDecorator(@Named("defaultSearchService")SearchService searchService, Config config){
        this.searchService = searchService;
    }

    public List find(Param p){
        if(config.allowed())
            searchSerice.find(p);
    }
}
The @Primary annotation, according to its Javadoc "Indicates that a bean should be given preference when multiple candidates * are qualified to autowire a single-valued dependency." The @Named allows to inject the decorator chain. Thats it :)