Archive for the ‘en’ category

How to teach Sonar to find new bug?

19. April 2017

How to teach Sonar to find new bug?


A few days ago my Jenkins instance gave me
the regurlar hint for updates. So I checked the changelog for changes which are
interesting to me. One of them hit my eye: Jenkins 2.53 – „GC Performance:
Avoid using FileInputStream and FileOutputStream in the core codebase.
“ I
read the two tickets (for Jenkins and the JDK itself) and was
surprised. I hadn’t knew that.

Some days later I regognized a longer
about that by CloudBees on DZone.
Also interesting.

During thinking about changing the „new FIS/FOS“
to something better in the open source projects I am working on (Apache Ant, Apache Commons) – Stefan
was faster
. 😉

Using „search and (manually) replace“ on my
office-codebase I thought about how to ensure that nobody intoduced these
instantiations again?

The tool

At work we use a Sonar instance for detecting propable
issues. Sonar uses static code analysis for detecting errors – unclosed
streams, possible NullPointers, etc. But this (new) „bug pattern“ was not
implemented. Sonar itself could scan Java files, but also other languages.

The documentation of Sonar contains a
chapter of writing
custom Java rules

I don’t want to copy that tutorial with my
own word, but here are some highlights:

Finally you end a with a plain JAR you‘ll have
to copy to the Sonar
directory, restart Sonar and activate your new rule.

The Tutorial gives you a pointer to Github
containing the sample sources, so you start with them. I decided
to start from scratch and copied the parts I could need instead of cloning the
whole repo and extend that.

The entry class is an implementation of org.sonar.api.Plugin
and its declaration in the manifest file (key=Plugin-Class).

This PlugIn registers two classes: one defining
your „repository“ with all the help files and metadata. And another which
registers the rules for production code or test code. They have to implement
the interfaces
org.sonar.api.server.rule.RulesDefinition respective

The rule itself could extend several entry
points, here the easiest is extending, so you could benefit from the Java knowledge and the access to the
Java AST.

The rule

Implementing the IssuableSubscriptionVisitor itself is fairly easy:

Register which types of AST nodes you are
interested in – here instantiation of objects:

List nodesToVisit() {




2.      Implement the logic for handling this. Here we are interested in
instantiation of

forbiddenClasses = ImmutableList.of(






public void
tree) {

    NewClassTree nct =

    for(String forbiddenClass : forbiddenClasses) {

        if (nct.symbolType().is(forbiddenClass)) {

tree, „Avoid using „ + forbiddenClass);




3.      The typecast in the visitNode
method could be done safely because we registered only (that) one type in the
nodesToVisit method. First I intended to use a list.contains(nct.symbolType()) but the is() method also checks
for subclasses.

When reporting
the issue the method registers the exact location via the give node.

4.      Provide metadata via a @org.sonar.check.Rule annotation:



„Avoid usage of FileInputStream or FileOutputStream“,

„FileInputStream and FileOutputStream can cause memory

and should be replaced by NIO-functionality“




public class AvoidClassRule
extends IssuableSubscriptionVisitor {


Sadly we have
to duplicate the key. Also the other information we have duplicated in metadata
files. I haven’t dived that deep to check if we could get rid of some
duplicatations here.

In addition to these Java files we supply
two resources: a JSON file with metadata and a HTML file with documentation.

The most interesting part of the JSON file
is (in my opinion) the remediation key: here you specify the amount of work to
fix the problem.


  „title“: „Avoid
usage of special Classes“,


  „remediation„: {

    „func„: „Constant\/Issue“,



  „tags“: [





The HTML file provides that part of the
which is shown as explanation of the rule.



Of course we have to test our code. The
example shows how to do that: provide classes which contains the error and use to verify that an issue was reported or not reported.

public class InValidFISInstance {


public void x() {

try (InputStream is = new
„test“)) {


catch (IOException e) {





public class CompliantClass {


public void x() {

try (InputStream in = Files.newInputStream(Paths.get(„file.txt“))) {


catch (IOException e) {


try (OutputStream out = Files.newOutputStream(Paths.get(„file.txt“))) {


catch (IOException e) {






public class AvoidClassRuleTest {



public TestName testname = new





    public void compliantClass() {

        verify( (file,rule) ->
file, rule) );




public void invalidFISInstance()

verify( (
file,rule) ->
file, rule) );



    private void
consumer) {

filename = String.format(„src/test/files/“,

consumer.accept(filename, new





Some notes

The JSON and HTML resource have to be in /org/sonar/l10n/java/rules/squid/. Of course your RulesDefinition class could load them from wherever
you want, but the used JavaCheckVerifier expect them to be there.

The file names of the resources have to be ${ruleKey}.html and ${ruleKey}_${language}.json.

In constrast to the example I tend to also
have a positive test. I am not only interested in that the rule reports the
issue when required, it should do nothing if the code is compliant.

Rules could be parameterized via the web
ui. For that you annotate the field with
@RuleProperty. You could access the values via:


public void init() {

rulesDefinition = new

context = new


    repository = context.repository((„RepositoryKey“);




public void
assertParameterProperties() {

param =

    // Not


    // If

    // assertThat(param).isNotNull();

    // assertThat(param.defaultValue()).isEqualTo(„Inject“);

    // assertThat(param.description()).isEqualTo(„…“);

    // assertThat(param.type()).isEqualTo(RuleParamType.STRING);



Managing „My favorite plugins“ in Finale

24. August 2015

Finale is a great notation program. After doing lot of work only by hand I start using plugins. Finale ships a lot of them (and you could purchase more or develop your own …). Now you have many of them, but use only a selection of them on a regular basis.

For easier overview Finale provides a favorite folder. How to get your favorites into that?

All plugins are stored on disk in your FINALE_HOME\Plug-Ins directory. There is also a favorite folder (in German „Bevorzugte Plug-Ins“). All you have to do is copy your required plugin into that folder.

Stop! – copy?

Maybe a copy would break your license (multiple installations), but basically a copy has to drawbacks: increased amount of disk space and hard to update (always update ALL copies).

So use LINKS for that 🙂

On windows navigate to your favorite-plugin-directory and create a link using „mklink TARGETNAME SOURCENAME“. For example on my german computer I’ll do

mklink CHKRNG32.FXT "..\Partitur und Arrangement\CHKRNG32.FXT"

for placing the „check range“-plugin.

My first Camel path was accepted

8. Januar 2014

Hooray! My patch to CAMEL-6993 was accepted and will be in the next Camel version. 🙂

Digital Music Sheet – An Evaluation

26. Dezember 2013


A colluege of mine has a nice use case for his old iPad2 – he uses that for displaying the music. So he doesn’t have to carry all the paper when rehearsing or performing.

I am not only a trumpet player, I am also interested in technical gimmicks. So I also want to have such a thing. 😉

So I started an evaluation. Because I don’t own a tablet I am not fit to a special device. There are mainly three different platforms you could use: t dAndroid, Windows 8 RT and iPad. For the first two you can choose between several devices and sizes while the iPad is (still) available at 10.1″.

Because I am more familiar with Android because of my mobile, I decided to start here. I ordered a Samsung Galaxy Tab 10.1 2014. I like the hand writing there and its performance. But for playing music this device was too small for me. I don’t want to say, that 10.1″ is not enough in general, this format is too small for ME with my 10dpt bad eyes …

So I send that device back and did an evaluation of 13″ devices. If you want to have a device in that size, you won’t find as many as for 10″ … But on the other hand this limits the possibilities and the work for searching. 😉
With 13″ you could use only Android or WinRT at the moment. Apple had announced bigger devices and Samsung did that also, but I am curious NOW :O.

To be honest I am not sure about using Windows on a tablet … maybe I am prestressed from my old mobile running Windows Mobile.


So now I had to search for candidates available in Germany (where I live). A little bit googling gave me: Odys Aeon, Archos Family Pad2, Archos Arnova Family Pad, Point of View Graphics Mobii 1325 Tab-P 1325  and the Hannspree SN14T71B.
Next step is collecting the requirements: should not be too big and too heavy. Nice display, actual OS, enough storage. Bluetooth is also a benefit for connecting to page-turners. And a low price would also be fine 🙂

From that table I choosed the Hannspree due its good hardware data. After that I did some searches again for a better price than the 245,30 € at Amazon and found  226,85 € at Atelco. After only a few days I got the tablet.

First impression of that tablet is: big. Reminds me on my 15″ notebook. But directly comparing with that the Hannspree is not all big as the notebook. And it’s much thinner.
What I dont like is the power adapter. It is not an USB one (the tablet has two micro USB ports) – it’s a propriety one.


A real evaluation would have to evaluate multiple music readers. But I found Mobile Sheets it impressed me: you could to much of what you are doing with paper, behind that tool there is programmer who is very responsible in the tools forum and there is a free ‚test‘ version (full features but limited songs).

Mobile Sheets Free

Installation is done via Google Play Store very quickly. And there are some points I had written in the forum – u.a. as error feedback to the programmer. Very nice: only two days later I had a good feedback from the programmer.

There is also an open bugtracker where you could place your wishes. And here are my first few:

  • #60: MS supports image rotation. This is done via double-tipping. Maybe an alternative would be more intuitive: place two fingers on the tablet a let them rotate. Answer:v5 is adding rotation support where you can rotate any amount you want. This will be done with a slider on the song editor screen (with the ability to enter a value manually if desired).
  • #61: You could configure how to activate the overlap. Default is a single tap. Because chances increase that a simple „next page“ results in activating the overlap, I suggest having a „double-tap“ or „long-tap“ as default. (There was a forum entry here which shows how to configure that behaviour.) Answer: There are already three overlay modes available in MobileSheets. If you look under the options for these overlay settings, you have the option for single tap, long tap, or swipe.
  • #62: Just in idea: improve the AutoScroll and the Metronome by supplying more information. The user could mark the area which build a „line“ in the music and save how many measures this line would include. Additional information is the tempo (which defaults to the tempo of the previous line and finally in the tempo of the song).
    • A Metronome could change its tempo while playing according to the music.
    • AutoScroll could use this information for calculating the time which is needed for playing one line. Also AutoScroll could ignore not marked lines (because they dont contain music).Anwer: These are good ideas. I am introducing more advanced auto-scrolling options, but I didn’t really think about adjusting the metronome mid-song. In a future update, I will look into adding these capabilities.
  • Preload of the Setlist. Having all songs loaded into memory would speed up page-turning. If it is not possible to load the entire setlist (maybe to less memory) preloading the next page and holding the last page would also tune the speed. Answer: I already load up to 10 pages in each direction from the current page. Even this can cause problems on some devices with limited resources.
  • Multi display support. As the price for tablets decrease you could choose the trend using more „commodity hardware“ instead of „high end hardware“. You could combine multible tablets for building one „big“ display.
    You have to store where each of the tablets is (like dual monitor support in desktop operating systems). Then input and output could be done via all displays. One of the tablets will be the „master“ which holds the configuration and the songs. The „slaves“ only require a thin layer for transport the input and output data. Communication could be done via bluetooth or wifi.
    This feauture could be interesting for conductors who have to see much more music at once.
    Answer: I am adding two pages up at a time, but the other features you’ve mentioned are pretty extensive. I’m not sure when I will try to tackle them yet.
  • Support for „native keyboards“. When entering text you only have the virtual keyboard. Supplying other input formats would be nice:speech input and pen writing. Answer: Speech input is naturally built into the Android OS, is it not? There is usually a microphone button on the keyboard for this (I don’t have my tablet to verify this). I also support voice-based searching for songs already as well. As for native keyboards and pen writing, these are also OS things. If I plug the keyboard into my ASUS Transformer, it works automatically. I don’t know if you can plug in any usb keyboard, but in theory, my app shouldn’t have to change at all to support this if the OS supports it. After that explanation I found the way of „entering text by speaking“. Nice.
  • #63:   Export the metadata (title, artist, notes, …) as plain text (CSV, XML) for further processing. I think there was such a feature request already … Answer: This will be tackled along with the other sharing features.
  • #64: Table overview of all notes. So you could create a „todo list“ from all saved notes. Answer: Are you saying you want a list on the library screen of notes taken for every song? I can consider adding this if it would be useful to people.
  • #65: Custom stamps. E.g. a danger sign, glasses – so the musician knows to have a deeper look. Answer: User provided custom bitmaps are still on my list of things to finish adding, along with converting the existing implementation to use fonts instead of bitmaps for the core symbols.

Mobile Sheets Companion

Another good tool is Mobile Sheets Companion. This is a program for your PC for managing the data on your tablet. It is much easier to import files or enter text with a ‚real‘ computer than with a tablet.

MS-C is available without additional charge and requires a connection to a running MS instance. Connection could be established via USB or Wifi (I choose Wifi because that’s much easier).

External Tools

When playing from tablet you have to get your music into that tablet. One way is using PDF (Mobile Sheets supports graphics too). When handling with PDF is use these external tools:

  • scan2pdf: Scans one or multiple pages into a PDF file.
  • pdfsam: split, joins, rotate PDFs

It is also possible to attach audio files to songs. So these tools may be worthful:

  • Audiograbber: grabbing audio files from CD into your computer
  • mp3tag: add MP3 ID3-tags to your file

Mobile Sheet

After the first iteration I decided to stay at this combination (Mobile Sheets on Hannspree), so I purchased the full version.

Here I did more experiments – adding audio files. Are you familiar with ‚Play Alongs‘? You get the orchestra music on CD and the solo part as music. So you could play ‚with your own orchestra‘ (or band). If you now attach the audio file to the song, you could directly play the background music from your tablet. Switching to another song would directly start the next audio file. No more searching for the right CD. 😉 And if you have Bluetooth you maybe connect BT speakers to get the music louder …


For 10.1″ tablets there are lot of stands like the K&M 19742.

tablet-holderFor a 13″ tablet I haven’t found a good stand yet. So I place that on my usual music stand.

Java-Cron-Jobs with Spring and Quartz

3. Februar 2012

We have several Jobs running the business. At the moment they are implemented as Spring-driven TimerTasks. But the next job should be run only nightly and a cron-like configuration syntax would be fine.

So I found Quartz and had to integrate that. For getting familiar with Quartz+Spring I wrote a small proof of concept.

The job implementation is just a POJO with a public void method. It should not throw any exception. The bean can be configured via usual Spring DI:

public class MyBean {

private String message;

public void doIt() {
System.out.println("Hello World: " + message);

public String getMessage() {
return message;

public void setMessage(String message) {
this.message = message;


So fine. But how to test? For this PoC a very simple test is ok – just wait for some Quartz run and check the output manually:

import static org.junit.Assert.assertNotNull;

import org.junit.AfterClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;

public class QuartzTest {

public void context() {
System.out.println("Starting Spring and within that: Quartz");
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");

@AfterClass public static void sleep() throws InterruptedException {
System.out.println("Wait for job output");

When using Spring, we have to provide the configuration.
First I define my Job-bean

<bean id="myBean">
<property name="message" value="TEST" />

After that I configure Quartz:

<bean id="scheduler"
<property name="triggers">
<!-- trigger list -->
<ref bean="job1" />

The binding between job and Quartz is done via a configured CronTriggerBean.

<bean id="job1">
<property name="cronExpression" value="0/2 * * * * ?" />
<property name="jobDetail">
<property name="name" value="job1" />
<property name="group" value="nightlyJobs" />
<!-- Delegieren auf unsere Bean und unsere Methode -->
<property name="targetObject" ref="myBean" />
<property name="targetMethod" value="doIt" />
<property name="concurrent" value="false" />

This CronTriggerBean defines two values:
1. cronExpression specifies when to run the job. An explanation about that syntax can be found at
2. jobDetail specifies a JavaBean with information about the job. But we dont have to write this Bean, we just can configure the generic one from Spring:
– name and group for controlling the execution via control interfaces
– targetObject and targetMethod for the delegation to our JobBean
– specify if this job should run in concurrent mode (where concurrent means in one VM!)

Building this sample with Maven is easy. You’ll need just following dependencies:

  • org.springframework::spring-core::3.0.5.RELEASE
  • org.springframework::spring-context::3.0.5.RELEASE
  • org.springframework::spring-context-support::3.0.5.RELEASE
  • org.springframework::spring-tx::3.0.5.RELEASE
  • org.quartz-scheduler::quartz::1.8.5
  • junit::junit::4.9::{scope=test}

As far as I read you can’t use Quartz 2.x because Spring 3 doesnt support that. But I havent tried it.

When running you’ll get this output:

Starting Spring and within that: Quartz
... logs from Spring
... infos from SLF4J
INFO: Starting Quartz Scheduler now
Wait for job output
Hello World: TEST
Hello World: TEST
Hello World: TEST
Hello World: TEST
Hello World: TEST

Update to the Hudson book

22. November 2010

News from the Hudson by Simon Wiest book I reviewed: it is now in the press and if you order it (e.g. at Amazon), you should be able to read it by Xmas 😉 (if you can read German :-O )

Links for 2010-11-08

8. November 2010

TecChannel had a (German) article about one of my favorite tools: Launchy – a program for starting applications or opening web pages …


If you are a PHP programmer and interested in CakePHP, CakeFest was a conference for you. Not attended? There are slides online:

And according to Entwickler.COM the conference should be held in 2011 too…


If you are a Java programmer unit tests are a standard. Therefore mocking is also a discipline you know. If you are interested in the mock library Mockito, the DZone article „5 minute Mockito“ is worth reading.



Hudson: Review and ideas

8. November 2010

In June I had the pleasure to do a review of the first German book about CI with Hudson by Simon Wiest. The book is announced for December 2010 and it will cover topics from basic (setup, first jobs) up to deep knowledge like writing plugins for Hudson.

As with earlier reviews I had printed the book (draft), read it and made comments on the side (and Simon had to listen to them for hours 😉

But some comments weren’t for the author – they „just“ ideas which came into my mind. But because I won’t have the time for implementing them, I’ll write about them …

Capability Plugin

The main idea is automatic dispatching of jobs to slaves according to their needs. If I remember right, TeamCity has this feature too …

The Slaves

The slaves have some capabilities. There are two kinds of them:

  1. automatic capabilities: result from tool configuration, operating system, jdk versions, build tool name+version, …
  2. manual capabilities: the admin of the slave can define key-value pairs

The Jobs

For jobs you define requirements according to the provided capabilities. And like them there are two requirements:

  1. automatic requirements: selected jdk, build tool, operating system (if you use e.g. a Windows Batch …)
  2. manual requirement: define additional requirements (or overwrite the complete formula). Use key=value (aspectj.available=true) or comparisons (ant.version >= 1.8) and combine them (and, or, xor, …)

Dispatcher strategy

The dispatcher must select all slaves which fit the defined requirements. From this list it selects the one by looking at the build queue of the slave (does it have time for building?) and net-response-time (don’t forget that much data has to be transfered …)


Not sure if there is something like this now …. 😉

Another Admin job Hudson: ensure that there are not too many old builds …

14. August 2010

Especially if you have a large number of jobs and they are running more often, you’ll come to a point, where your disk is full of old builds.

Hudson provides a configuration parameter for that: „discard old builds“. This will delete old builds according to the specified number of days or number of builds.

For Apache I wrote a script which ensures, that all jobs have „discard“ setting and that existing values are not higher than a defined maximum value.

/** Default-Setting for the "number of old builds" */
numberOfOldBuilds  = 10

/** Maximum of "number of days" */
maxDaysOfOldBuilds = 14

/** Should we override existing values? */
overrideExistingValues = true

/** Closures for setting default 'max number' */
setMaxNum = { job ->
   job.logRotator = new hudson.tasks.LogRotator(-1, numberOfOldBuilds)

/** Closures for setting default 'max number' */
setMaxDays = { job ->
 job.logRotator = new hudson.tasks.LogRotator(maxDaysOfOldBuilds, -1)

// ----- Do the work. -----

// Access to the Hudson Singleton
hudsonInstance = hudson.model.Hudson.instance

// Retrieve all active Jobs
allItems = hudsonInstance.items
activeJobs = allItems.findAll{job -> job.isBuildable()}

// Table header
col1 = "Old".center(10)
col2 = "New".center(10)
col3 = "Job".center(50)
col4 = "Action".center(14)
header = "$col1 | $col2 | $col3 | $col4"
line = header.replaceAll("[^|]", "-").replaceAll("\\|", "+")
title = "Set 'Discard old builds'".center(line.size())

println title
println line
println header
println line

// Do work and create the result table
activeJobs.each { job ->

 // Does the job have a discard setting?
 discardActive = job.logRotator != null

 // Enforce the settings
 action   = ""
 newValue = ""
 oldValue = ""
 if (!discardActive) {
 // No discard settings, so set the default
 action   = "established"
 newValue = "$numberOfOldBuilds jobs"
 } else {
 // What are the current settings?
 oldDays = job.logRotator.daysToKeep
 oldNums    = job.logRotator.numToKeep

 if (oldNums > 0) {
 // We have a set value for 'numbers'
 if (oldNums > numberOfOldBuilds && overrideExistingValues) {
 // value is too large so set a new one
 action   = "updated"
 newValue = "$numberOfOldBuilds jobs"
 oldValue = "$oldNums jobs"
 } else {
 // Correct value or we arent allowed to override.
 oldValue = "$oldNums jobs"
 } else {
 // we have a value for 'days'
 if (oldDays > maxDaysOfOldBuilds && overrideExistingValues) {
 // value is too large so set a new one
 action   = "updated"
 newValue = "$maxDaysOfOldBuilds days"
 oldValue = "$oldDays days"
 } else {
 // Correct value or we aren't allowed to override.
 oldValue = "$oldDays days"

 // String preparation for table output
 oldValue = oldValue.padLeft(10)
 newValue = newValue.padLeft(10)
 jobname  =

 // Table output
 println "$oldValue | $newValue | $jobname | $action"
println line

// Meaningful output on the Groovy console
// (the console will output the result of the last statement)
printout = "Number of Jobs: $activeJobs.size"

In the first section I define the „constants“ (line 001-008). After that I define two closures which update a given Hudson job (line 010-018).
The basic structure is the one I used in earlier scripts
The work here is in lines 053-088. But that’s pretty easy: check the given values and eventually set new values using the pre defined closures.
New is the last line: I dont use a >x = „“< instruction for suppressing the output. I use a more meaningful message: the number of jobs.

Extend Windows-Search in Windows 7

13. August 2010

I wanted to search for a string in Java properties files. I knew that there was one entry – somewhere deep in the codebase … But the search from Win7 didnt find it.

Sounds familiar? So read further …

I remembered that the search works only on particular files (doc, ppt …) and my properties files are not in that list. 😦
So I wanted to tune the search:

Open the system configuration via the start menu and type „search“ into the search field. You should find the „index options“ somewhere.

Press the „Advanced“ button (translated from the German „Erweitert“) and swith to the second „file types“ tab.

Here you can add additional file types by their suffix and specify their index option (only file information, file content).

Reindexing needs time …. but I hope after this I will be able to find my file 😉