Now from Zeit

I have heard of serverless from so many vendors by now that it feels more about marketing than anything else. I have a bad gut reaction when I feel that there is more marketing than substance about any technology.

I decided to look at Now from Zeit just out of curiosity and I think that I should look at it a bit more since it looks quite easy.

On the Zeit site, you can see the free offering that they have and it allows you to get your feet wet and be more curious about what you can do.

The Now CLI also looks like a very simple way to perform everything you need.

I also like the immutability approach that they have that allows you to move quickly from one deployed version to another.

It is definitely worth more time to see what I could do with this platform.

Apache Commons Configuration and Map

Until today, the Apache Commons Configuration was a very helpful tool as a quick configuration utility in many projects. For loading simple configuration item it is fine.

I was trying to find a way to store and read a Map to contain some configuration and there is was no easy way that I could find. I came up with a Krusty solution for now but I am hoping that I can find something more elegant.

GitHub Repo with example code.

Jackson and mapping fields

I was trying to use the useragentstring.com api to query and map browsers to my custom object and I did not want to name my fields with agent_type, agent_name and agent_version.

Simple solution that I found on this page was to annotate my properties in the class with:

@JsonProperty(“agent_type”)
@JsonProperty(“agent_name”)
@JsonProperty(“agent_version”)

The mapping is happening automatically.

Very simple.

Using log4j for email alerts in an application

I want to explore how using log4j for email alerts in an application is feasible but how well it works in a real application.

The idea came from the fact that a team I work with change the rules for alerts on a regular basis based on the work and the situations they are caught in. It is clear that we have to stop changing the code to accommodate the new rules they give us.

Log4j is a possibility and I found this blog post that talks about it:

http://www.srccodes.com/p/article/18/send-logs-by-email-notification-using-apache-log4j-smtpappender

Impact of the scope on the maven assembly plugin

For a Spark project I needed to bundle some dependencies and I found a few Stackoverflow answers that explained to add this section to your pom.xml:

<plugin>
	<artifactId>maven-assembly-plugin</artifactId>
	<executions>
		<execution>
			<phase>package</phase>
			<goals>
				<goal>single</goal>
			</goals>
		</execution>
	</executions>
	<configuration>
		<descriptorRefs>
			<descriptorRef>jar-with-dependencies</descriptorRef>
		</descriptorRefs>
	</configuration>
</plugin>

But once I packaged my application I ended up with a huge jar file. It contained everything the application needed.

My mistake was that the pom.xml did not contain the proper scope for each dependency and because of that they were all getting bundled into the jar.

Specified that a few were provided (provided) and it reduced the jar size considerably.

How a small missing piece can change other behaviours drastically.

Git error on a new repository

I was puzzled by a Git error I saw today.

I simple create a git repo on the “server”:

mkdir /git/newRepo

git init /git/newRepo

 

Then on my workstation I cloned it and added a file:

git clone ssh://server/git/newRepo

cd newRepo

vi test

git add test

git commit -m “testing”

git push

 

and I get this error:

No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as ‘master’.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to ‘ssh://server/git/newRepo’

 

From reading a StackOverFlow answer I understood that the issue on the server side is that the new Git repository is open on the master branch by the user that created it and so it prevents a push to it because it could corrupt it. One way was to make the repo a bare one but another it to get the remote use into another branch than master.

On the server:

 

vi README

git add README

git commit -m “README”

git checkout -b test

 

and after that command I can pull and push normally.

 

git pull

git push

 

 

Reference:

http://stackoverflow.com/questions/2816369/git-push-error-remote-rejected-master-master-branch-is-currently-checked

Random Password Generator in Java

I needed a random password generator done in Java and while reading a few articles here and there I did not find the solution I was looking for. Piecing a few of them I came up with some code.

One of the requirement that made this a bit more complicated is that I needed to have a special character in the password but from a limited list of possible special characters.
I also needed to make sure that it respected some basic complexity rules.

It has a dependency on the commons-lang 3 library from Apache but since I already had it in my project it was easy to accommodate.

Maven dependency:

<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-lang3</artifactId>
	<version>3.3.1</version>
</dependency>

Java code:

package com.halogensoftware.hosting.example;

import org.apache.commons.lang3.RandomStringUtils;

public class Random {

	public static void main(String args[]){
		String pwd = "";
		for ( int i = 0; i < 1000; i++ ) {
			do {
				pwd = RandomStringUtils.random(12, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890%#^*:");
			} while (!valid(pwd));
			System.out.println(i + ".valid pwd: " + pwd);
		}
	}

	private static boolean valid(String pwd){
		return (pwd.matches(".*([0-9]).*") && pwd.matches(".*([a-z]).*") && pwd.matches(".*([A-Z]).*") && pwd.matches(".*([%#^*:]).*"));
	}
}

A Not acceptable Rest with Spring

Trying to come up with a clever title for a blog post is not easy.

This problem caused me headaches for 2 days and as much as I have resolved it I have no idea why and how it happened.

I have done REST application with Spring many times in the past and it easy. At one point in the process for this one I was creating REST application with STS just to confirm that it was a no brainer, which helped and puzzled me.

The simplest class that worked for me was something like this:

@Controller
@RequestMapping(value="/rest")
public class Rest {
    @RequestMapping(value="/{something}", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String[] firstTry(@PathVariable String something){
        return new String[]{something, "testValue"};
    }
}

I could quickly package it with Maven and run it on Wildfly and it would work as expected.

When I tried to do the same in IntelliJ I was getting a non working application that returned a 406, Not Acceptable, return code to all REST calls.

I compared the web.xml, the application context xml files, the pom files, everything I could thing of and the STS app would work with nothing special but the IntelliJ app would not.

Most articles I was finding were talking about bugs in some version of Spring and to make sure you had the element in your application context xml. I tried many version and adding the annotation driven did not fix the issue.

I finally found this article that explained that you needed to make sure you specified the jackson-databind library in your pom.xml otherwise you would get 406 answers. Once I added the jackson libraries to my project from IntelliJ I was able to get things to work as expected.

No Jackson libraries are specified in my STS generated project and they work so I am still puzzled as to why this was needed in the IntelliJ project.

Upgrading Groovy and Grails

I have started to learn what Groovy and Grails can do for web application development and I already faced a fun problem.

After I installed GGTS (from Spring.io), I started a simple application and then wanted to upgrade the Groovy and Grails version to the latest.

Not as straight forward as I would have taught but I got everything working again.

First thing learned is that GVM is a useful tool to use to install Groovy and Grails and manage multiple versions. Doing it manually does not make sense.

I also learned from a stackoverflow article that you need to erase the .metadata directory in the GGTS workspace after upgrading. This will force you to re-import your projects but that is the only way I have found to recover the projects so it is a small inconvenience.

Back to learning!