Tuesday, June 7, 2011

Java Heap Space

The issue which most developers come across is the OutOfMemory: perm space error, which usually leads to increasing the JVM's perm space, here is great explanation on the details of java heap space:

http://www.freshblurbs.com/explaining-java-lang-outofmemoryerror-permgen-space

Monday, May 9, 2011

Integrating soap ui maven plugin

What a pleasure, a way to integrate your soap ui project into a maven build :)

Just pop this into your pom.xml from where your web module is located; this will start/stop jetty for the soap ui plugin to invoke its tests:


<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.21</version>
<configuration>
<stopKey>foo</stopKey>
<stopPort>9999</stopPort>
<webAppConfig>
<contextPath>/test</contextPath>
</webAppConfig>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>




And insert the soap ui plugin to invoke on inegration-test phase pointing to a valid soap ui project XML:

<plugin>
<groupId>eviware</groupId>
<artifactId>maven-soapui-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<projectFile>${project.basedir}/soapui-project.xml
</projectFile>
<outputFolder>${project.basedir}/target/test/soapui-results</outputFolder>
<junitReport>true</junitReport>
</configuration>
<executions>
<execution>
<id>soapUI</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>

Thursday, April 28, 2011

Spring LDAP

Here is a simple example on how you would retrieve a user's ldap credentials using spring ldap:
http://blog.javachap.com/index.php/ldap-user-management-with-spring-ldap/

Configure a spring ldap context source:

<bean id="contextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
<constructor-arg value="${ldap.url}"/>
<property name="base" value="${ldap.base}"/>
<property name="userDn" value="${ldap.username}"/>
<property name="password" value="${ldap.password}"/>
</bean>

<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="contextSource"/>
</bean>


You can then use the ldap template to query the ldap directory and map the relevant fields:

public User getUser(String userId) {
AndFilter filter = new AndFilter();
filter.and(new EqualsFilter("objectclass", "person")).and(
new EqualsFilter("uid", userId));
List<User> users = ldapTemplate.search(DistinguishedName.EMPTY_PATH,
filter.encode(), new AttributesMapper() {
public Object mapFromAttributes(Attributes attributes) throws NamingException {

User user = new User();
user.setEmail(attributes.get("mail").toString());

return user;
}
});

if (users.isEmpty()) {
return null;
}
return users.get(0);
}

Tuesday, April 19, 2011

Maven assembly plugin - create jar dependancies directory

Here is how to create a directory within a maven module containing all the dependant jars referenced by the module itself:

Put this in the pom.xml:

<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>distro-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>


And then put this in assembly.xml:

<assembly>
<id>uberjar</id>
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<unpack>false</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>

Wednesday, April 6, 2011

Grails Maven Integration

If you want to migrate an existing grails project created with the typical 'create-app' grails tool (that by default uses Ant as build tool) to maven, u can use this command:

mvn org.grails:grails-maven-plugin:1.3.2:create-pom -DgroupId=com.mycompany

http://grails.org/doc/latest/guide/4.%20The%20Command%20Line.html#Mavenizing an existing project

Wednesday, March 9, 2011

Loading files from classpath

Here is a great tip for loading resources in classpath from within Java:

// From ClassLoader, all paths are "absolute" already - there's no context
// from which they could be relative. Therefore you don't need a leading slash.
InputStream in = this.getClass().getClassLoader().getResourceAsStream("SomeTextFile.txt");

// From Class, the path is relative to the package of the class unless
// you include a leading slash, so if you don't want to use the current
// package, include a slash like this:
InputStream in = this.getClass().getResourceAsStream("/SomeTextFile.txt");

//If you dont include the '/path' in the above example and the class your calling this from is in package
// com.me.happy then it will look for that resource in that package
InputStream in = this.getClass().getResourceAsStream("SomeTextFile.txt");