this

Zero2Hero Offering Trainings on Hot Technologies

I’s been a while since I have written my last blog post. Mostly because I have been busy with Zero2Hero which I have co-founded in fall last year. Zero2Hero is offering the coolest trainings on hot technologies. You can checkout the current offerings at http://www.zero2hero.at.

At the moment we are preparing some Git workshops – we have been able to engage Tim Berglund, a well known GitHubber. Registration has already started.

  • Git Beginners
  • Git Advanced

In the past months we started a series of workhops around Groovy technologies. So we had started out with Groovy (with Dierk König, Canoo), did Gradle (with Peter Niederwieser, Gradleware). In Fall this year we should finalize this series with a Grails training.

Hosting a Static Web Site in a Amazon S3 Bucket

Create a Amazon S3 bucket

Using the Amazon Console create a S3 bucket. Use your domain name (e.g. www.domain.tld) to name the bucket. Open the properties panel, switch to the Website tab, enable hosting of static web sites. Next go back to the permissions tab and click Edit bucket policy

Amazon bucket policy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
  "Version": "2008-10-17",
  "Statement": [
      {
          "Sid": "PublicReadGetObject",
          "Effect": "Allow",
          "Principal": {
              "AWS": "*"
          },
          "Action": "s3:GetObject",
          "Resource": "arn:aws:s3:::www.domain.tld/*"
      }
  ]
}

Create a CNAME record with for your domain

The CNAME target is printed on the Amazon bucket properties panel, Website panel (bottom of panel).

CNAME
1
www.domain.tld CNAME www.domain.tld.s3-website-us-east-1.amazonaws.com

Install Octopress

Installing Ruby

1
mkdir -p ~/.rvm/src/ && cd ~/.rvm/src && rm -rf ./rvm/ && git clone --depth 1 git://github.com/wayneeseguin/rvm.git && cd rvm && ./install`

Add the following line to the end of your .profile or .bash_profile or .bashrc file (be sure to edit your user name).

1
2
3
if [[ -s /Users/YOURUSERNAME/.rvm/scripts/rvm ]] ; then
  source /Users/YOURUSERNAME/.rvm/scripts/rvm ;
fi

rvm install 1.9.3

Detailed instructions

Installing Octopress

1
2
3
4
5
6
git clone git://github.com/imathis/octopress.git octopress
cd octopress/

gem install bundler
bundle install
rake install

Moving Files From a Git Repository to Another Keeping History

Yesterday I had to move a few files from a Git repository A to repository B while working on a client’s project. Today I had to do it again on another project. Again I had to think about the sequence of commands I had used. For this reason I’m posting this here.

Prepare repository A (the source)

1
2
3
4
5
6
7
8
9
10
git clone --no-hardlinks /path/to/sourcerepository repositoryA
cd repositoryA
# remove remote origin just so we can't break anything in the source repository
git remote rm origin
# filter for desired files
git filter-branch --subdirectory-filter directory HEAD
# move around files, etc.
# then commit
git add .
git commit -m"isolated files"

In line #1 we clone the source repository so we can’t break the original one, we also remove the remote in line #4 so no links back to the source repository exist. Next using the filter-branch command we filter out everything but the directory we want to keep. Pull files to new repository

1
2
3
4
5
git clone repositoryB_URL repositoryB
cd repositoryB
git remote add repositoryAbranch /path/to/repositoryA
git pull repositoryAbranch master
git remote rm repositoryAbranch

Migrate From Hades to Spring Data JPA

For the past 18 months I have been utilizing hades for implementing JPA based repositories/DAOs. Hades has now been moved into the Spring Data JPA project. Starting with their 1.0.1 release I tried migrating one of my projects to this release. Following I document the steps that were necessary to achieve this.

Their project page starts off with showing the Maven repositories and dependency descriptors (in case you are using Maven). Obviously the first thing is to replace the hades artifact by the spring-data-jpa artifact.

The project’s domain classes implement the org.synyx.hades.domain.Persistable interface which has to be replaced by org.springframework.data.domain.Persistable (takes a single generics parameter for the id type).

DAO interfaces extending org.synyx.hades.dao.GenericDao have to be modified to extend org.springframework.data.repository.CrudRepository. With this modification you will face a couple of compile problems. read*-methods have been renamed to find*-methods. Also you are loosing the paging and sorting capable find* – methods (which I didn’t use). If those are required then extend the org.springframework.data.repository.PagingAndSortingRepository interface.

find* – methods now return iterables rather than list types requiring minor adjustments to existing codebase. To get around this, the interface can also extend from JpaRepository. Using this base interface hands you a pretty powerful DAO that goes far beyond a simple CRUD repository.

When I started off with hades many month ago I used an explicit XML-based configuration of DAO components.

1
<bean id="addressDao" class="org.synyx.hades.dao.orm.GenericDaoFactoryBean" p:daoInterface="at.martinahrer.training.spring.jpa.dao.hades.AddressDao" />

For migration the factory bean class has to be changed:

1
<bean id="addressDao" class="org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean" p:daoInterface="at.martinahrer.training.spring.jpa.dao.hades.AddressDao" />

With these changes in place all tests were showing the green bar again. However, today I would recommend using repository namespace http://www.springframework.org/schema/data/jpa which significantly reduces the amount of configuration needed by utilizing class path scanning to detect repository components.

1
<repository:repositories base-package="at.martinahrer.training.spring.jpa.dao" />

Certainly you might encounter more steps during migration depending on the features you have used from the old hades code base. In my case I only had used named JPA queries which just continued to work like before.

Analyzing Class Metadata With the Springframework

What a shame I haven’t published anything for a long time. To make up for this long pause, I’m going to discuss how Spring can support with analyzing class metadata. These days everybody is crazy about using annotations in their frameworks. Occasionally you need to know what classes/methods are annotated. A good example might be to build a list of class names for all JPA entities (those classes annotated with javax.persistence.Entity and the like).

A simple use-case showing how to detect classes annotated as components

This use-case is using a few Spring APIs and some home grown code that controls resource matching (ClassResourceResolver and AnnotationMetadataMatcher).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.io.IOException;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.util.ClassUtils;

public class ClassResourceResolverTest {
  @Test
  public void testSelect() throws IOException {
      // given
      ClassResourceResolver scanner = new ClassResourceResolver(
              ClassUtils.convertClassNameToResourcePath(AComponent.class.getPackage().getName()));
      final MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(
              new PathMatchingResourcePatternResolver());

      // when
      String[] classNames = scanner.select(new AnnotationMetadataMatcher(metadataReaderFactory, false,
              Component.class));
      // then
      Assert.assertArrayEquals(new String[] { AComponent.class.getName() }, classNames);
      Assert.assertTrue(metadataReaderFactory.getMetadataReader(classNames[0]).getAnnotationMetadata()
              .isAnnotated(Component.class.getName()));
  }
}

@Component
class AComponent {
}

@Repository
class ARepository {
}

ClassResourceResolver is doing the resource matching all from a set of resource paths

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 public String[] select(ClassResourceMetadataMatcher matcher) throws IOException {
      List<String> result = new ArrayList<String>();

      for (String path : resourcePath) {
          String resourcePattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + path + "/**/*.class";
          Resource[] resources = this.resourcePatternResolver.getResources(resourcePattern);
          for (Resource resource : resources) {
              if (resource.isReadable()) {
                  MetadataReader reader = matcher.match(resource, resourcePatternResolver);
                  if (reader != null) {
                      result.add(reader.getClassMetadata().getClassName());
                  }
              }
          }
      }
      return result.toArray(new String[result.size()]);
  }

A ClassResourceMetadataMatcher is looking at the annotations present on a class resource

1
2
3
4
5
6
7
8
9
10
11
 @Override
  public MetadataReader match(Resource resource, ResourceLoader resolver) throws IOException {
      for (Class<? extends Annotation> annotationType : annotationTypes) {
          MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
          if (new AnnotationTypeFilter(annotationType, considerMetaAnnotations).match(metadataReader,
                  metadataReaderFactory)) {
              return metadataReader;
          }
      }
      return null;
  }

Under the hood Spring is using the ASM bytecode library for looking into the class files. Not that no class examined by the code above is actually loading any of the class objects so it is fairly lightweight and not filling up your perm gen space. Still you need to make up useful search paths so you don’t end up scanning through you full classpath.

The full code just is available as a gist

1
git clone git@gist.github.com:73f21e68d24032ad7672.git

Git Workshop Mit Matthew McCullough (Github Trainer)

Subversion, das wohl immer noch am weitesten verbreitete SCM im Java-Bereich, muss sich in letzter Zeit einige Kritik gefallen lassen. Tatsächlich häufen sich die Anzeichen, dass die Konkurrenten aus dem Lager der verteilten Systeme immer mehr an Popularität gewinnen. So wechseln immer mehr Projekte auf das verteilte Versionsmanagementsystem (DVCS) Git.Ist das nur ein temporärer Hype oder eine technologische ®Evolution?

Am 4.Mai 2011 gibt es erstmals in Österreich einen Git Workshop mit Matthew McCullough (offizieller GitHub Trainer) und somit eine einmalige Gelegenheit zu erfahren was denn hinter dem Erfolg von Git steckt.

  • mit Matthew McCullough
  • am 4. Mai 2011, von 9:00-16:30
  • Spitz Hotel – Raum “Round Table 2”, Fiedlerstrasse 6, 4040 Linz, http://www.spitzhotel.at
  • Trainingsprogramm (Broschüre)
  • €330,00 (zuzüglich Mwst.)

Anmeldung per EMail (mit Name und Adresse des anzumeldenden Teilnehmers) an office@martinahrer.at.
 Zum Workshop in Englisch werden maximal 20 Teilnehmer zugelassen, ein eigener Laptop ist mitzunehmen.

Anmeldung von Gruppen ab 3 Personen zu ermäßigtem Preis auf Anfrage. Im Preis enthalten sind Getränke sowie ein Brötchenbuffet zu Mittag! Ermäßigtes Parken in der Hoteltiefgarage ist möglich.

Matthew McCullough’s Bio Matthew McCullough is an energetic 15 year veteran of enterprise software development, world-traveling open source educator, and co-founder of Ambient Ideas, LLC, a US consultancy. Matthew currently is a trainer for GitHub.com, author of the Git Master Class series for O’Reilly, speaker on the No Fluff Just Stuff tour, author of three of the top 10 DZone RefCards, including the Git RefCard, and President of the Denver Open Source Users Group. His current topics of research center around project automation: build tools (Maven, Leiningen, Gradle), distributed version control (Git), Continuous Integration (Hudson) and Quality Metrics (Sonar).

Testing Static Methods…

We all have learned that it is not wise to overuse static methods as these are kind of hard to test and deal with. So in general I strive to get out of their way. Sometimes I’m facing the situation that the use of a framework or that some technical infrastructure requires me to implement static methods. So for testing I found PowerMock which nicely integrates with EasyMock and Mockito.