Upgrading Docker Compose to latest version

If you use Docker for Mac or something similar, Docker Compose will be installed along with it. Docker Compose has a different release timeline for Docker for Mac so you will not be able to try latest version of Docker compose until you upgrade Docker for Mac. This is limiting. You should be able to install Docker compose independently. To achieve that, you can run following command

$ composeVersion=1.13.0
$ curl -L https://github.com/docker/compose/releases/download/$composeVersion/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose$ chmod +x /usr/local/bin/docker-compose

In the above commands, $ signify bash prompt. You don’t have to type it. Now, you can check Compose version using the command shown below.

$ docker-compose version
Advertisements

MongoDB Query Tip : Find All The Documents Where Array Length is Greater Than N

Suppose we have blog document in blogs collection as shown below.

> db.blogs.insert({author : "Shekhar Gulati","title":"Hello World","text":"Hello World!!","tags":["mongodb","openshift"]})
>
>
> db.blogs.insert({author : "Shekhar Gulati","title":"Hello World","text":"Hello World!!","tags":["mongodb","openshift","nosql"]})

Now you want to find out all those blogs which have more than 2 tags then query is shown below.

> db.blogs.find({$where : "this.tags.length > 2"}).pretty()
{
	"_id" : ObjectId("51011037bf779459a978f96f"),
	"author" : "Shekhar Gulati",
	"title" : "Hello World",
	"text" : "Hello World!!",
	"tags" : [
		"mongodb",
		"openshift",
		"nosql"
	]
}

Spring Test Failing with A ServletContext is required to configure default servlet handling

If you are getting exception shown below in your Spring test

Caused by: java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.(DefaultServletHandlerConfigurer.java:54)
at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping(WebMvcConfigurationSupport.java:330)
at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerByCGLIB$$c733f512.CGLIB$defaultServletHandlerMapping$22()
at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerByCGLIB$$c733f512$$FastClassByCGLIB$$b76778f9.invoke()
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:285)
at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerByCGLIB$$c733f512.defaultServletHandlerMapping()
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:160)

Then to fix this error migrate to Spring 3.2 release. Change the maven dependency as shown below

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

Quick Tip on Mockito — Mocking Iterator

Today I was writing unit test for a piece of code which required me to mock a iterator. My requirement was that I wanted to return true first time when hasNext() method and return false in the second iteration. It took me sometime to figure out how to write such a test case. My class for which I was writing test looks like as shown below.

import java.util.Iterator;

public class ResultFetcher {

	private ResultStore store;
	private ResultProcessor processor;

	public void fetchResults() {
		Iterator<String> iterator = store.resultIterator();
		while (iterator.hasNext()) {
			String result = iterator.next();
			processor.process(result);
		}
	}
}

The unit test code is shown below. The important line in the test is Mockito.when(iterator.hasNext()).thenReturn(true,false); which will return true first time and false second time.

import java.util.Iterator;

import org.junit.Test;
import org.mockito.Mockito;

public class ResultFetcherTest {

@Test
public void testFetchResults() {
ResultFetcher fetcher = new ResultFetcher();

ResultStore store = Mockito.mock(ResultStore.class);
ResultProcessor processor = Mockito.mock(ResultProcessor.class);
fetcher.store = store;
fetcher.processor = processor;

Iteratoriterator = Mockito.mock(Iterator.class);
Mockito.when(store.resultIterator()).thenReturn(iterator);
Mockito.when(iterator.hasNext()).thenReturn(true,false);
Mockito.when(iterator.next()).thenReturn("Hello");
fetcher.fetchResults();
Mockito.verify(processor).process("Hello");
}

}

Logging JAXWS SOAP Request and Response using a Java Property

Today I was faced with the situation that I needed to log the SOAP requests and responses going in and out from a Java client. I just had the client jar no source code so I can’t any any log or change any other configuration. I needed to log the request and response because I was getting some weird exceptions which I was not able to understand. I got the hold of the SOAP request by passing a Java property

java -Dcom.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.dump=true -jar client.jar

Fixing Exception — Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)

If you are working with Spring Roo and suddenly start getting exception “Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)” then make sure you have _Configurable.aj ITD in your project. This ITD makes the sure that your entity classes have all the required dependencies like EntityManager injected in them. Configurable ITD  looks like as shown below

privileged aspect Movie_Roo_Configurable {

    declare @type: Movie: @Configurable;

}

Adding Third Party Jars To An OSGI Bundle

Few days back I was writing a Spring Roo add-on(an OSGI bundle) which required me to add some external 3rd party jars to the OSGI bundle pom.xml file. I am not very well verse with OSGI so it took me some time to figure out how to add 3rd party jars to an OSGI bundle. In this short blog post I am sharing the recipe to add third part jars.

The exception that you get when OSGI bundle is not able to find the dependency is like this.

org.osgi.framework.BundleException: Unresolved constraint com.shekhar.roo.addon [66]: Unable to resolve 66.0: missing requirement [66.0] package; (package=com.shekhar.utils)

To solve this error you need to add Embed-Dependency tag to your maven bundle plugin as shown below. The example shows that I need to add commons-io and utils jar to my OSGI bundle. To get more information about Embed-Dependency tag and maven bundle plugin please refer to the documentation.

<plugin>
	<groupId>org.apache.felix</groupId>
	<artifactId>maven-bundle-plugin</artifactId>
	<version>2.3.4</version>
	<extensions>true</extensions>
	<configuration>
		<instructions>
		<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
		<Bundle-Copyright>Copyright Shekhar Gulati. All Rights Reserved.</Bundle-Copyright>
		<Bundle-DocURL>${project.url}</Bundle-DocURL>
		utils,jsch,commons-io;scope=compile|runtime;inline=false</Embed-Dependency>
		</instructions>
		<remoteOBR>true</remoteOBR>
	</configuration>
</plugin>

SCP command for Copying files from remote linux machine to local machine

I just faced a requirement that I needed to copy a zip file containing logs from a remote server to my local machine for some analysis.
I googled around and found at that you can use scp command to that for you.

scp -r <username of remote machine>:<remote location> <location of local directory>

scp -r shekhar@dev.xebia.com:/home/xebia/application/logs/logs-1-09-2011.zip /home/shekhar/tmp

Java Puzzler : They just find me !!

Couple of days back I wrote a piece of code which was behaving in an unexpected manner. I was confused what was happening. Take a look at the sample code below and predict its behavior

package com.shekhar;

public class JavaPuzzler {

	public static void main(String[] args) {
		JavaPuzzler javaPuzzler = new JavaPuzzler();
		javaPuzzler.doSth();
	}

	public void doSth() {
		float f = 1.2f;
		if (f >= 1.0) {
			f = 0.9999999999999f;
		}
		InnerClass innerClass = new InnerClass(f);
		System.out.println(innerClass.getValue());
	}

	private class InnerClass {

		private float value;

		public InnerClass(float value) {
			if (value >= 1.0f) {
				throw new IllegalArgumentException(
						"Value can't be greater than 1.0f");
			}

			this.value = value;
		}

		public float getValue() {
			return value;
		}
	}
}

My initial expectation was that I would get value 0.9999999999999f as answer.Try it and find the answer. Share your answer and reasoning in comments.

Create a free website or blog at WordPress.com.

Up ↑