Log4Net configuration and scope extension

I had the need to setup Log4Net logging in a new project the other day. It is fairly easy to setup, just add the listeners you want in the configuration files. Then use it, of course you get most things for free the log level etc. We also wanted to make it super easy to use Scope, so I added a LogExtension that takes care of that. The idea is if you forget to close the scope it will be automatically done once the scope object gets garbage collected. Below is example code including UnitTests.

logger_sample

Could not load type Microsoft.VisualStudio.Services.Common.VssHttpClientBase

If your getting the following error on your TFS server web interface odds are that you are using incompatible version between TFS and VS or other Microsoft product. The object in question was moved from the common Dll to another. In my case I installed TFS RC and then never version of VS2013, after that I got the error.

Could not load type ‘Microsoft.VisualStudio.Services.Common.VssHttpClientBase’ from assembly ‘Microsoft.VisualStudio.Services.Common, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’.

To remedy the situation I upgraded TFS 2013 with the RTM version. After the upgrade I’m not getting the error anymore ant the TFS web interface works as expected.

Fireflies

I had an idea about writing a small code to animate fireflies. On Christmas break I was bored and I figured I had a few hours to play with it. I also wanted to use some tools I hadn’t used yet and figured that was a good time to do Javascript since I haven’t done much Javascript. I fired up the editor ( Sublime ) and used the browser to run / debug, Firefox and FireBug of course. There is a bit of a difference using this setup compared to writing Java code in Eclipse which I’m used to. First off you don’t get the compilation in Sublime and secondly you have to switch to another program to load and debug. Switching between the two was what annoyed me. Later I though, what if I can find a tool that is an editor and can Ftp to a server as well and runs within the browser. If on each save you can Ftp it to a sever that is serving the Javascript then I could do it all from the browser. I went looking around and fount Net2Ftp which is supposed to at least come close to the tool I’m wishing for. I will have to try it out the next time I’m coding in Javascript. The other thing that got me is that there is no good way of declaring your own classes, you have to work with prototypes and such, why that is beats me.

As you can see from the code, it’s not pretty.

If you want to see the Javascript in action click here.

fireflies

Java Spring MVC 3.2.0 UnitTests

I had a bit of a fight today, I wanted to UnitTest some Java Spring MVC code that I’m using for a demo application. But after a lot of Googling, StackOverflow and hitting different documentation of Spring 3.0, 3.1 and 3.2 I finally came to an acceptable solution. Mind you a minimal sample to test MVC controller without having to go crazy with decelerations, attributes, injections and so on and so forth, you catch my drift. There has been a lot of movement from those three versions I mentioned and finally the spring UnitTesting lib has now been folded into Spring itself, this is good news.
Btw, I found the answer in the Spring MVC showcase on GitHub, where else ?

So lets take a look at what you need, bare minimum just the way I like it.

Using Maven, you will need the ref in your POM file

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.2.0.RELEASE</version>
            <scope>test</scope>
        </dependency>

The controller class I’m using comes pretty much directly from creating a new MVC Spring project from Eclipse.

package us.kristjansson.springTest;
//
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
//
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page.
 */

@Controller
public class CxHomeController
{
       
        private static final Logger logger = LoggerFactory.getLogger(CxHomeController.class);
       
        /**
         * Simply selects the home view to render by returning its name.
        */

        @RequestMapping(value = “/”, method = RequestMethod.GET)
        public String home(Locale locale, Model model)
        {
                logger.info(“Welcome home! The client locale is {}.”, locale);
                //
                Date date = new Date();
                DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
                String formattedDate = dateFormat.format(date);
                model.addAttribute(“serverTime”, formattedDate );
               
                return “home”;
        }
       
}

Then lets look at the Test itself.

//
package us.kristjansson.springTest;
//
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import org.springframework.test.web.servlet.MockMvc;
//
import org.junit.Before;
import org.junit.Test;

public class TxHomeController
{
         //
         private static final Logger logger = LoggerFactory.getLogger(TxSample.class);
         private MockMvc mockMvc;

         @Before
         public void setup() throws Exception
         {
                 logger.info( “Setup CxHomeController” );
                 this.mockMvc = standaloneSetup(new CxHomeController()).build();
         }

         @Test
         public void TestController() throws Exception
         {

                 //
                 logger.info( “testing CxHomeController” );
                
                 // The CxHomeController’s view is "home" without content
                 this.mockMvc.perform(
                           get(“/”))
                                 .andExpect(status().isOk())
                                 .andExpect(content().string(“”))
                                 .andExpect(view().name(“home”) 
                                 );

         }
       
}

curl: (60) SSL certificate problem: unable to get local issuer certificate

If you get the following error when using curl for SSL address, here is an easy fix. Your error will probably look something like the following.

curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a “bundle”
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn’t adequate, you can specify an alternate file
using the –cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
the bundle, the certificate verification probably failed due to a
problem with the certificate (it might be expired, or the name might
not match the domain name in the URL).
If you’d like to turn off curl’s verification of the certificate, use
the -k (or –insecure) option.

Just go to http://curl.haxx.se/ca/cacert.pem it has a list of certification providers. View as source and save it to a file cacert.pem. Then on the curl command just add this in the end pointing to your file –cacert cacert.pem Note you can also try the -k option but that’s cheating :)

Update

A better approach is to save the file as curl-ca-bundle.crt and save it in the Windows System directory (e.g. C:\windows\system32)

Maven Sonar plugin for your Java project

I needed to add Sonar reporting to a small Java project that uses Maven for the build. There is a Maven Sonar plugin available for this task here.

First add sonar properties file ( sonar-project.properties ) to your java project, put it in root.

# info
sonar.projectKey=javaTest
sonar.projectVersion=1.0
sonar.projectName=Sample java spring MVC project

# Comma-separated paths to directories with sources (required)
sonar.sources=src

# Language
sonar.language=java

# Encoding of the source files
sonar.sourceEncoding=UTF-8

Make Maven aware of the Sonar location etc, add that to the Maven settings \maven\conf\settings.xml

Something like this

<profile>
       <id>sonar</id>
       <activation>
          <activeByDefault>true</activeByDefault>
       </activation>
       <properties>
          <!– SERVER ON A REMOTE HOST –>
          <sonar.host.url>http://remotebox.com:9000</sonar.host.url>
          <sonar.jdbc.url>jdbc:h2:tcp://remotebox.com:9092/sonar</sonar.jdbc.url>
       </properties>
    </profile>

For Maven version 3+ add this to the POM file in the build section, else Maven version 2 ( below ).

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>sonar-maven-plugin</artifactId>
        <version>2.1</version>
      </plugin>

For Maven 2.

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>sonar-maven-plugin</artifactId>
        <version>1.0</version>
     </plugin>

Pass the sonar:sonar goal to the Maven script then kick off a build, the dependencies should be downloaded automatically. The report for your project displays on the Sonar server dashboard.