Custom bean validation constraints

Bean Validation API defines several built-in constraint annotations which are very useful in many situations. However, there are still some cases where these standard constraints are not enough and you have to create your own custom constraint. With Bean Validation this task is pretty easy and straightforward.

In the article Validating HTML forms in Spring using Bean Validation we have built a simple Spring MVC application using built-in constraints only. This time we will introduce a new text form field for entering favourite day of a week (in place of the age field) and add a custom constraint to this field. The new field will be checked if it a part of workweek or weekend depending on the attributes set in the constraint. Additionally, the constraint will allow specifying whether the comparison is case-sensitive or not.

Defining custom annotation

The first step is creating a custom annotation @DayOfWeek which represents a custom constraints:

package com.example.beanvalidationcustomconstraint;

import java.lang.annotation.Documented;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;

@Documented
@Constraint(validatedBy = DayOfWeekValidator.class)
@Target({ METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
public @interface DayOfWeek {
    String message() default "{com.example.beanvalidationcustomconstraint.DayOfWeek.message}";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
    DayOfWeekType[] value() default { };
    boolean ignoreCase() default false;
}

The annotation contains three mandatory attributes: message, groups and payload. The first attribute specifies a message to show or a reference to it if the validation fails. In this case the message attribute references the actual message stored in ValidationMessages.properties file or one of it internationalized versions. Attribute groups allows the definition of validation groups but we won’t use any in this example. The last one payload specifies extra data to be used by the clients of this constraint – we also do not use any in this example.

The are also two other attributes which are more interesting from our point of view and are used to provide additional settings for the custom constraint. The value is a default attribute (used when no other attribute name is specified when using the annotation) and in our case it holds an array of allowed day types:

package com.example.beanvalidationcustomconstraint;

public enum DayOfWeekType {
    WORKWEEK,
    WEEKEND
}

The ignoreCase attribute specifies whether the constraint should use case-sensitive or case-insensitive string comparisons. If these attributes are not specified, they default to an empty array and false, respectively.

We also annotate the newly created annotation with @Documented to enable showing it in JavaDoc for elements annotated with it, @Constraint to indicate that this is a Bean Validation constraint annotation and to specify the custom validator associated with it, @Target to inform that the annotation can be attached to methods, fields and other annotations and @Retention to specify that we want the annotation to be available at runtime via reflection.

Defining the validator

The created annotation does not contain logic which performs actual validation but instead it refers to the class DayOfWeekValidator using @Constraint annotation. The actual validator looks like this:

package com.example.beanvalidationcustomconstraint;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class DayOfWeekValidator implements ConstraintValidator<DayOfWeek, String> {
    private DayOfWeekType[] allowedTypes;
    private boolean ignoreCase;
    
    @Override
    public void initialize(DayOfWeek constraint) {
        allowedTypes = constraint.value();
        ignoreCase = constraint.ignoreCase();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null)
            return true;
        
        for (DayOfWeekType type : allowedTypes) {
            switch (type) {
                case WORKWEEK:
                    if (isWorkWeek(value))
                        return true;
                    break;
                case WEEKEND:
                    if (isWeekEnd(value))
                        return true;
            }
        }
        return false;
    }

    private boolean isWorkWeek(String value) {
        return equalsDay(value, "Monday") || equalsDay(value, "Tuesday")
                || equalsDay(value, "Wednesday") || equalsDay(value, "Thursday")
                || equalsDay(value, "Friday");
    }

    private boolean isWeekEnd(String value) {
        return equalsDay(value, "Saturday") || equalsDay(value, "Sunday");
    }

    private boolean equalsDay(String value1, String value2) {
        return ignoreCase ? value1.equalsIgnoreCase(value2) : value1.equals(value2);
    }

}

The custom validator implements generic ConstraintValidator interface with two type parameters: the type of the custom constraint annotation and the type of the element which can be validated using this validator. Then we implement initialize() method which fetches the attributes/settings of the custom constraint and isValid() method which performs the actual validation and returns true if the validation finished successfully or false otherwise.

Using the constraint

Once we have the annotation and the validator ready, we can use it in the same way as any other built-in constraint:

package com.example.beanvalidationcustomconstraint;

import java.io.Serializable;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class Person implements Serializable {
    private static final long serialVersionUID = 3297423984732894L;
    
    @Size(min = 1, max = 20, message = "{firstNameInvalid}")
    private String firstName;
    @Size(min = 1, max = 40, message = "{lastNameInvalid}")
    private String lastName;

    @NotNull
    @DayOfWeek(value = DayOfWeekType.WEEKEND, ignoreCase = true)
    private String favouriteDayOfWeek;

    // constructor, setters and getters
}

In this case we allow only Saturday and Sunday (ignoring the letter case) as the value of favouriteDayOfWeek field. Because we use Spring MVC, the validation will take place when the user tries to submit the form.

Defining custom constraints using composition

Sometimes we don’t even need to define a validator for the custom constraint. It is possible if we can represent our custom constraint as a conjunction of already existing constraints. In this example we specify a constraint which is met only if @NotNull, @Min and @Max constraints are met:

package com.example.beanvalidationcustomconstraint;

import java.lang.annotation.Documented;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

@NotNull
@Min(0)
@Max(10)
@Documented
@Constraint(validatedBy = {})
@Target({ METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
public @interface Range {
    String message() default "Range is not valid";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};

}

This is especially useful if the same combination of constraints are applied to many different fields or methods.

Conclusion

Bean Validation is very extensible and allows us to define virtually any custom constraint and use it in the same way as the built-in ones.

The sample code for this example was tested with JBoss and is available at GitHub.

Posted in Java, Java EE, Spring | Tagged , , , | 4 Comments

Common exception misuses in Java (and not only)

Exceptions were introduced in many programming languages as a standard method to report and handle errors. If you have ever used functions that return special values (usually -1 or NULL) to indicate an error, you should know how easy it is to forget to check this value and completely ignore the error. One great advantage of exceptions is that it is generally “hard to forget about them”. It means that if you don’t handle the exception somewhere in the code, it will abort the execution of the application and will also appear on a console or in logs.

Although exceptions were introduced into mainstream programming languages many years ago and many books have been written about them, they are still used wrongly. In this article I try to describe several doubtful practices regarding usage of exceptions which it is usually better to avoid.

Using too many try-catch blocks

Sometimes you may see code like this:

Writer writer = null;
try {
     writer = new FileWriter("/tmp/a");
} catch(FileNotFoundException e) {
     // handle error; and return from the method
} catch (IOException ex) {
     // handle error; and return from the method
}       
try {
     writer.write("Line1");
} catch (IOException e) {
     // handle error; close the file and return from the method
}
try {
     writer.close();
} catch (IOException e) {
     // handle error; and return from the method
}

Generally there is nothing really wrong with this code regarding exceptions but it would be much better if all instructions were put into single try-catch block:

Writer writer = null;
try {
    writer = new FileWriter("/tmp/a");
    writer.write("Line1");
} catch(FileNotFoundException e) {
    // handle error
} catch (IOException ex) {
    // handle error
} finally {
    if (writer != null)
        writer.close();
}

It reduces the length of the source code and improves its readability. Merging try-catch blocks changes the behaviour of the application but usually the change is not very significant and is totally acceptable. In some cases we may still prefer to use multiple try-catch blocks (e.g. to provide better error reporting).

Using exceptions for control flow

Exceptions were invented only for error reporting and handling and should not be used for other purposes like control flow. Here is an example of such misuse:

try {
    Iterator<String> it = list.iterator();
    while (true) {
       String value = it.next();
       // do something with value
    }
} catch (NoSuchElementException e) {
    // OK, end of the list
}

Instead of catching NoSuchElementException the code should check whether there is a next element available in the iterator before accessing it. This check would completely prevent appearance of the mentioned exception.

While the code above is ugly, it has also another problem. Exception throwing and catching is generally a very expensive operation in most (all?) programming languages. If the code above is run very often (especially in some loop), it may greatly slow down your application.

Using wrong exception class

Java provides own exception hierarchy with many predefined exception classes. The exceptions in Java can be divided into unchecked and checked ones. The former should be used for reporting programmer errors like dereferencing of null value, accessing elements outside of array bounds or fetching objects from an empty collection. Generally, unchecked exceptions can be easily avoided by adding a simple condition in the code before calling a method. The later are generally unpredictable and usually not much can be done to prevent them. The examples of checked exceptions are I/O or file parsing errors.

The common mistake is throwing checked exception from a method when unchecked one should be used. The programmers using this method will be enforced to catch the exception but there would be nothing they could do to handle it (maybe except rethrowing it as an unchecked exception). This will result in more unnecessary and hard to read code.

The opposite is also possible. If a method throws unchecked exception for an error which must be handled in some way, the programmers simply will not catch this exception which in turn may abort the execution of the application.

Meaningless messages

One of the common mistakes is throwing exception without any message or with message which does not describe the cause of the problem:

if (list.isEmpty())
    throw new IllegalArgumentException();
if (list.size() != array.length)
    throw new IllegalArgumentException("Wrong data");

Such exceptions are generally useless. Even if you have the full source code of the class from which the exception is thrown, it may still not explain what is exactly wrong. It is generally better to put some additional data into exception message so it will be much easier to find the root cause of the exception once it appears:

if (list.isEmpty())
    throw new IllegalArgumentException("List is empty");
if (list.size() != array.length)
    throw new IllegalArgumentException("List and array have different sizes: " + list.size() + " " + array.length);

Catching unchecked exceptions

In most cases unchecked exceptions should not be caught at all but prevented using a simple condition. You can see the example of such issue above in paragraph Using exceptions for control flow. Whenever there is a catch block for unchecked exception in the code, it should be removed and replaced by a proper condition.

Another problem with catching unchecked exceptions is that it hides programming errors and therefore makes it more difficult to find out why certain thing does not work. The same is also true when catching instances of general Exception and Throwable classes.

As always there are some situations in which catching unchecked exceptions is acceptable. The first is that there is no easy way to ensure that the exception won’t happen. For example there is no easy method to check whether the string representation of a number is valid before calling method Integer.parse(). Therefore, it is much easier to call the method and catch the unchecked exception if it happens.

The other situation is that you are running some external code which cannot be validated beforehand and you don’t want the unchecked exceptions thrown by this code to abort the execution of your application. This is what web application servers do.

Reporting exceptions late

Sometimes programmers are afraid to throw an exception whenever an error happens. Instead they often return null value, empty string, empty collection or just ignore the error. For example the method below returns null whenever it is impossible to return the correct value:

public String getSecondElement(List<String> list) {
    if (list.size() >= 2)
        return list.get(1);
    else
        return null;
}

While it may be convenient, there is a risk associated with this solution. The returned null value may be passed to some other part of the code and cause there NullPointerException exception. Additionally, the place where the exception was raised may be very distant from the place where the original problem occurred which makes it much harder to find the root cause.

If we raised an exception (even unchecked one) at the first place we noticed the problem (in our case in getSecondElement method), it would be much easier to find the root cause and fix it.

Handling exceptions early

Some programmers feel obliged to catch and handle every exception in the sample place (a method or a class) where they are raised. Usually, at this place we don’t have enough knowledge about the bigger operation this method or class is a part of and therefore we cannot handle the exceptions properly. If we are unsure how to handle a particular exception, it is usually much better to pass it up to the caller because higher level methods have more knowledge about the context and can better revert or retry the operation or inform the user about the error.

Ignoring exceptions

In my opinion the worst thing we could do with exceptions is ignoring them:

public void loadDrumKit(String name) {
    try {
        // here comes code for loading from file
    } catch (IOException e) {
        // ignore it - we can use the old drum kit
    }
}

When the exception sooner or later happens, we would have no information why the application started to misbehave. As a total minimum we should log the caught exception with full stack trace so that we could find the root cause after checking the logs. And the best way to handle the exception would be to either pass it up to the caller or clearly inform the user about the problem (of course, additional logging is welcome).

Conclusion

Handling of exceptions is never easy but still we should not misuse them and take short-cuts just because it is easier. Otherwise, it may affect the application stability and predictability which will reduce the overall customer satisfaction. Of course, the things written above are just guidelines for exception handling and there are situations where it is completely OK (or even better) to deviate from them.

Posted in Java, Software development practices | Tagged , | 7 Comments

Views in Java Collections Framework

View in Java Collections Framework is a lightweight object which implements Collection or Map interface but is not a real collection in a traditional sense. In fact, view does store objects inside but references another collection, array or a single object and uses it to provide the data to a user.

Empty view

To start with views let’s take a look at the simplest ones which represent empty collections. In Collections class you can find emptyList(), emptySet() and emptyMap() methods which create empty instance of List, Set or Map respectively:

List<String> clearList = Collections.emptyList();
Set<String> clearSet = Collections.emptySet();
Map<String, Integer> clearMap = Collections.emptyMap();

The returned instance is actually immutable so trying to add an element into it will result in an exception. However, this kind of empty collection is very convenient if some API requires a collection but for some reason we don’t want to pass any objects there.

View of a single object

Very often we need a collection with only one element. Achieving this with views is very easy. We can create such list, set or map by calling singletonList(), singleton() or singletonMap() methods respectively:

List<String> oneList = Collections.singletonList("elem");
Set<String> oneSet = Collections.singleton("elem");
Map<String, Integer> oneMap = Collections.singletonMap("one", 1);

It is also possible to create a list which contains specified element given number of times:

List<String> nTimesList = Collections.nCopies(9, "elem");

The created collections are immutable similarly to empty views. Additionally, the views does not have the overhead of a typical collection and are easier to create.

View of an array

If you ever needed to repack elements from an array into a list just to call a single method, you may appreciate asList() method from Arrays class which creates a list view backed by an array:

String[] monthArray = new String[12];
List<String> monthList = Arrays.asList(monthArray);

For obvious reasons the returned collection is immutable which means it is impossible to add or remove elements from it. But it is still possible to modify the elements inside the view using get() or set() methods.

Since Java 5 it is also possible to use varargs in asList() method:

List<String> months = Arrays.asList("July", "August");

View of a portion of a collection

We can also create a view of a portion of a list:

List<String> nextFive = list.subList(5, 10);

The returned view contains 5 elements of the original list between index 5, inclusive, and 10, exclusive. The view is also mutable so any modification on the view (e.g. adding or removing elements), will also affect the original list.

The similar functionality is also possible on SortedSet using methods:

SortedSet<E> 	headSet(E toElement);
SortedSet<E> 	subSet(E fromElement, E toElement);
SortedSet<E> 	tailSet(E fromElement);

and on SortedMap:

SortedMap<K,V> 	headMap(K toKey);
SortedMap<K,V> 	subMap(K fromKey, K toKey);
SortedMap<K,V> 	tailMap(K fromKey);

There are even more such methods in NavigableSet and NavigableMap interfaces.

Views of keys, values and entries

You should be probably aware of keySet(), entrySet() and values() methods of Map interface. They also return views instead of real collections which make them very efficient.

Unmodifiable views

Class Collection provides methods which create immutable view for many collections types:

List<T>         unmodifiableList(List<? extends T> list);
Map<K,V>        unmodifiableMap(Map<? extends K,? extends V> m);
Set<T>          unmodifiableSet(Set<? extends T> s);
SortedMap<K,V>  unmodifiableSortedMap(SortedMap<K,? extends V> m);
SortedSet<T>    unmodifiableSortedSet(SortedSet<T> s);

If somebody tries to add or remove elements from the view, it will throw an exception. This kind of behaviour is very useful if we want to ensure that given method will not modify the collection. However, it is still possible to modify the elements inside the collection.

Creating unmodifiable view does not make the original collection unmodifiable. It is still possible to change it using the original reference.

Synchronized views

Similarly to unmodifiable views we can create synchronized views using methods:

Collection<T>   synchronizedCollection(Collection<T> c);
List<T>         synchronizedList(List<T> list);
Map<K,V>        synchronizedMap(Map<K,V> m);
Set<T>          synchronizedSet(Set<T> s);
SortedMap<K,V>  synchronizedSortedMap(SortedMap<K,V> m);
SortedSet<T>    synchronizedSortedSet(SortedSet<T> s);

Every method of synchronized view is synchronized which make the view thread-safe. Of course, you should no longer hold or use the reference to the original collection because it would allow unsynchronized access to it.

Conclusion

I tried to mention most popular and useful views in Java Collections Framework. They greatly simplify some common tasks, reduce the number of times when repacking is needed and also reduce the amount of code to type. I hope you will find them useful too.

Posted in Java | Tagged , | 1 Comment

Validating HTML forms in Spring using Bean Validation

Validating input is a crucial part to ensure data integrity and proper function of the whole application. JSR-303 also known as Bean Validation is a very convenient way to perform such validation at different levels of the application. In this article I would like to present the most important features of Bean Validation and how to use it to validate HTML forms in Spring.

Built-in constraints

The most important part of Bean Validation are annotations which are used to place constraints on fields, methods or parameters. There are several built-in constraints which are useful in many situations:

@Null Checks whether the value is null.
@NotNull Checks whether the value is not null.
@Min Checks whether the value is not smaller than the specified limit. The value must be of type: long, int, short, byte, their wrappers classes, BigInteger, BigDecimal. Types double and float are not supported.
@Max Similar to @Min but checks whether the value is not higher than the specified limit.
@DecimalMin Similar to @Min but can be also used for strings.
@DecimalMax Similar to @Max but can be also used for strings.
@Digits Checks whether the number of integral and fractional digits of the value are not higher than the specified limit. The supported types are the same as for @DecimalMin
@AssertTrue Checks whether the boolean value is true.
@AssertFalse Checks whether the boolean value is false.
@Past Checks whether the date is in the past.
@Future Checks whether the date is in the future.
@Size Checks whether the size of the string, array, collection or map is within specified limits.
@Pattern Checks whether the string value matches specified regular expression.

In case this is not enough a custom constraint can be defined. The details how to do it are described in Custom bean validation constraints article.

Validation messages

Every validation annotation has message property which can be optionally set to provide a custom message to the user when the validation fails. If it is omitted, default message will be used.

In the following example, we put size constraint on firstName and lastName fields and two different constraint on age field:

@Size(min = 1, max = 20)
private String firstName;
@Size(min = 1, max = 40)
private String lastName;
@Min(value = 1, message = "Age must be positive")
@Max(value = 100, message = "Age must be not higher than 100")
private Integer age;

If the validation of firstName or lastName fails, the default message will be forwarded to a user. Additionally, if validation of age fails, one of the custom messages will be used depending on which constraint failed.

Moving messages to a separate file

Because hard-coding messages is generally not a good idea, we can create ValidationMessages.properties file in the default package of the application and place the messages there:

firstNameInvalid=Must be between {min} and {max} characters
lastNameInvalid=Must be between {min} and {max} characters
ageTooLow=Age cannot be smaller than {value}
ageTooHigh=Age cannot be higher than {value}

The values in the curly braces are place-holders for properties of the annotation. Then we can refer to these messages using curly braces:

@Size(min = 1, max = 20, message = "{firstNameInvalid}")
private String firstName;
@Size(min = 1, max = 40, message = "{lastNameInvalid}")
private String lastName;
@Min(value = 1, message = "{ageTooLow}")
@Max(value = 100, message = "{ageTooHigh}")
private Integer age;

Overriding messages for built-in constraints

If you don’t like the default messages for built-in constraints but also don’t want to specify your own message every time, you can override default messages in ValidationMessages.properties file as follows:

javax.validation.constraint.Size.message=Field size must be between {min} and {max} characters

Internationalizing validation messages

The idea of keeping validation messages in a separate file has one additional benefit. If you want to provide validation messages for many different languages, you can create one such file per language/region.

For example, German messages can be put into ValidationMessages_de.properties file and Canadian French messages into ValidationMessages_fr_CA.properties. The lookup of messages works the same as for resource bundles so the file ValidationMessages.properties will be checked last if nothing else matches.

Validating in a controller

After we have assigned constraints to the properties of our model attribute and set messages, we can configure controller to handle the validation:

@RequestMapping(method = RequestMethod.POST)
public String add(@ModelAttribute(value = "newPerson") @Valid Person newPerson, BindingResult result, Model model) {
    if (result.hasErrors())
        return "personlist";
        
    list.addPerson(newPerson);
    return "redirect:/personlist";
}

The first thing to notice is that we have added @Valid annotation to our model attribute to perform the validation of newPerson argument. The validation errors (if any) will be stored in the immediately following instance of BindingResults. Then at the start of the method we call hasErrors() method to check if there were any errors. If yes, we show the form again to the user to give him a chance to correct the data.

Presenting validation errors to user

The only thing left is presenting validation errors to a user. The easiest way to do so is to sprinkle several <sf:errors> tags in the view:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Person List</title>
        <link href="<s:url value='/resources/css/styles.css'/>" rel="stylesheet" type="text/css"/>
    </head>
    <body>
        <p>
            <c:forEach var="person" items="${list}">
                <span class="box-with-border"><c:out value="${person.firstName} ${person.lastName} (${person.age})" /></span>
            </c:forEach>
        </p>
        
        <sf:form method="POST" modelAttribute="newPerson" cssClass="personForm">
            <table>
                <tr>
                    <td><sf:label path="firstName">First name:</sf:label></td>
                    <td><sf:input path="firstName"/></td>
                </tr>
                <tr><td colspan="2"><sf:errors path="firstName" cssClass="errors"/></td></tr>
                <tr>
                    <td><sf:label path="lastName">Last name:</sf:label></td>
                    <td><sf:input path="lastName"/></td>
                </tr>
                <tr><td colspan="2"><sf:errors path="lastName" cssClass="errors"/></td></tr>
                <tr>
                    <td><sf:label path="age">Age:</sf:label></td>
                    <td><sf:input path="age" /></td>
                </tr>
                <tr><td colspan="2"><sf:errors path="age" cssClass="errors"/></td></tr>
                <tr><td></td><td><input type="submit" value="Add" /></td></tr>
            </table>
        </sf:form>
        
    </body>
</html>

The path attribute of the <sf:errors> tag specifies for which HTML form field validation messages should be shown.

Conclusion

Bean Validation provides standard and very convenient method for validating HTML forms in Spring. Additionally, Bean Validation is not limited to checking form input but can be also used in many other situations.

The complete source code for the example can be found at GitHub.

Posted in Java, Spring | Tagged , , | 4 Comments

Using JPA and JTA with Spring

When building a web application, we will sooner or later need to somehow store data entered by users and retrieve it later. In most cases the best place to keep this data is a database because it additionally provides many useful features including transactions.

Therefore, in this article I would like to show how to extend our previous Spring MVC application to use JPA and JTA to access database and manage transactions. The configuration details strongly depend on the database and application server being used. In our case it will be Oracle 11gR2 database and JBoss 7 Application Server.

Configuring entity manager factory

Before we start we have to configure entity manager factory in Spring. There are three different ways to do this:

  • creating LocalEntityManagerFactoryBean
  • obtaining EntityManagerFactory from JNDI
  • creating LocalContainerEntityManagerFactoryBean

The first and the second option have several limitations so I will concentrate only on the last one which provides full JPA capabilities in a Spring-based application.

First, we have to create instance of LocalContainerEntityManagerFactoryBean in our Spring configuration file:

<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jtaDataSource" ref="dataSource" />
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
</bean>

In property jtaDataSource we refer to JTA data source configured in our application server and exposed via JNDI with name java:/orcl:

    <jee:jndi-lookup id="dataSource" jndi-name="java:/orcl" />

The second property jpaVendorAdapter gives us possibility to configure options specific to JPA provider implementation. There are several adapters to choose:

  • HibernateJpaVendorAdapter
  • OpenJpaVendorAdapter
  • EclipseLinkJpaVendorAdapter
  • TopLinkJpaVendorAdapter

but we choose Hibernate because it is available by default in JBoss:

<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="database" value="ORACLE" />
    <property name="showSql" value="true" />
    <property name="generateDdl" value="true" />
</bean>

In property database we specify that we use Oracle database, then we inform JPA implementation to print issued SQL commands to the server log and to generate necessary objects (like tables) in the database. Please, note that the last option cannot be used in the production code because it may drop already existing tables in the database. We use it only to simplify the example.

Configuring persistence.xml

The last step to configure entity manager factory is to create META-INF/persistence.xml file with our persistence unit:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="mainPU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.example.springjpa.Person</class>
    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
      <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"/>
    </properties>
  </persistence-unit>
</persistence>

We have one persistence unit with name mainPU which supports JTA transactions. We configure Hibernate specific persistence provider class and we configure 2 properties required by it. As you could see the exact values are very database and application server specific. The rest of the properties is set by Spring via jpaVendorAdapter bean created before. At last we specify a single class com.example.springjpa.Person which can be persisted by this persistence unit.

Configuring transaction support

Because we plan to use JTA transactions and declare them using annotations, we have to add two additional lines to our Spring configuration:

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" />
<tx:annotation-driven transaction-manager="transactionManager"/>

The first one informs Spring to instantiate JTA-specific JtaTransationManager transaction manager which uses JTA implementation provided by the application server. The second line tells Spring to scan all classes for @Transactional annotation on a class or method level and associate them with given transaction manager.

Enable injecting with @PersistenceContext

If we want to inject instances of EntityManager using @PersistenceContext annotation, we have to enable annotation bean processor in Spring configuration:

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

Usually this line is optional because a default PersistenceAnnotationBeanPostProcessor will be registered by the <context:annotation-config> and <context:component-scan> XML tags.

Using annotations

After we have finished the configuration of persistence, we can add standard JPA annotations to our Person entity class:

package com.example.springjpa;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Entity
@Table(name = "springjpa_person")
@NamedQuery(name = "Person.selectAll", query = "select o from Person o")
public class Person implements Serializable {
    private static final long serialVersionUID = 3297423984732894L;
    
    @Id
    @GeneratedValue
    private int id;
    private String firstName;
    private String lastName;
    private Integer age;
    // constructor, getters and setters
}

To access the database we create a simple repository class annotated with @Repository:

package com.example.springjpa;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class PersonList implements Serializable {
    private static final long serialVersionUID = 324589274837L;

    @PersistenceContext
    private EntityManager em;
    
    @Transactional
    public void addPerson(Person person) {
        em.persist(person);
    }
    
    @Transactional
    public List<Person> getAll() {
        TypedQuery<Person> query = em.createNamedQuery("Person.selectAll", Person.class);
        return query.getResultList();
    }
}

In this class we obtain reference to correct EntityManager instance using standard @PersistenceContext annotation. Additionally, both methods of this class are annotated with @Transactional to inform Spring that these methods should be executed in a transaction.

Conclusion

The configuration of JPA and JTA in Spring is not very difficult but require setting several parameters specific to the environment (database, persistence provider and application server) in XML configuration files. The rest of the code is totally independent of it so changes necessary to switch to the other database or application server are limited to several lines of configuration.

The complete source code for the example can be found at GitHub.

Posted in Java, Java EE, JPA, JTA, Spring | Tagged , , , , , , | 16 Comments

Git: how to use tags

Tagging is a very popular and common concept in version control systems. Tags are generally used to mark special milestones in a history of your repository like releasing a new version of your product. The common practice is to create a tag for every released version of your product so that you can easily switch back to it, see the source code or rebuild.

What is tag?

In Git tag is a named reference to a specific commit in your repository. It is very convenient because you can use user-friendly names for commits instead of remembering or writing down their SHA hashes.

At this point you may notice a similarity to branches because they are also user-friendly names for commits. The difference is that branches can change the commit they point to while tags are immutable and always point to the same commit. As a consequence it is impossible to add any new change to a tag.

Lightweight tags

Git supports few types of tags. Lightweight tags are the most basic ones because they store only hash of the commit they refer to – nothing else is stored. They can be created by switching to the commit you want to tag and then issuing git tag command with the name of the tag to create:

$ git tag v1.0

Unsigned annotated tags

Lightweight tags have several drawbacks which disqualify them in many situations. They cannot have tag message, creation date, author or optionally cryptographic signature. If you want this kind of information, you can create an unsigned annotated tag by adding -a option:

$ git tag -a v1.1

If you don’t want to be prompted for the tag message, you may provide it on the command line by adding -m option:

$ git tag -a -m 'Release version 1.1' v1.1

Here I would also like to point that if -m or -F file option is given, Git will always create annotated tag so -a may be omitted in this case.

Signed annotated tags

It is also possible to create annotated tag signed using GnuPG program by adding -s option:

$ git tag -s -m 'Release version 1.3' v1.3

You need a passphrase to unlock the secret key for
user: "Robert <robert@example.com>"
4096-bit RSA key, ID A231EC80, created 2014-03-09

The signed tag can be later verified whether is was created by an authorized person.

By default the signing key is chosen from GnuPG keyring using the default e-mail address but it is also possible to explicitly state the key identifier using -u option:

$ git tag -s -u A231EC80 -m 'Release version 1.4' v1.4

You need a passphrase to unlock the secret key for
user: "Robert <robert@example.com>"
4096-bit RSA key, ID A231EC80, created 2014-03-09

Listing tags

Once you create several tags, you may want to see the list of them:

$ git tag -l -n10
v1.1            Release version 1.1
v1.3            Release version 1.3
v1.4            Release version 1.4

The command lists all existing tags with maximum 10 lines of their tag message. If the tag is a lightweight tag, the message of the last commit is shown.

Showing tag details

To see more details about specific tag, you may use git show command:

$ git show v1.4
tag v1.4
Tagger: Robert 
Date:   Sun Mar 9 10:44:55 2014 +0100

Release version 1.4
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAABAgAGBQJTHDgXAAoJEA9AcK2iMeyAO8IP/jaFq+1u6k9Dsd+t6Om2HBDA
pttI01tO1X9YdrM/UbUrqAbnMhkgpiJrmVPKwvyl6yObKDGlho1Px2EosHu4g3G0
lhpXQ46JtYmpwc4edxIzLwS+ZhQrF0TKK9smdxiEsqNrln2o/Ce3skl6FrKKsWr1
K83z218dQnNkB8Xx9JdodttghXIwhsGlJsx9qEBseaHHqFAOZlyLnGADh0uKjsVp
0Lbr/WtiWausFMnIPEek1+cXsLrZlEGVkOm6Ja8QVstcw9ZR4/JhErA0Ahl3L4ZW
Gxgmz0IdRqaBbgSlHt4zVhI6or4yGErYPHeFFwuNo5F2QKUJZLvAj+Ywf9gKEHGL
R6Vj643K5ZTb4Xz/2fxI0erM+Bc/P+RLEo82rw7VIlpvjMuY8smdzLCvwb50ouu2
1k7IItgXtiWVjIkeEUjYq3m9C35ifyzt6gOPf1UaJx8p9OHP0Lm1yGnbK9ApKK2+
XBbprzfJbtDDOctKAwCbS1TA83wN30xVJr9r156ug9DgVoJauMXNA25vJ/Nycn9S
EtVH6FD08zpzWoM6uOlA4dEhELC5vzVl9GbSPupdbMFmS7U0CBCcnfjCrZHaMemr
9CiibnBEK2YVJpYKI6E1AQsGVeNqUb68SoDZ5NQqQSSEXQB6w6i9Jho8bQ5I5UJc
dQbIOcMN0tw5W3AFky2t
=kU0Q
-----END PGP SIGNATURE-----

commit 8f96002526eb5e6b818d719ebe72a7db381eed39
Author: Robert 
Date:   Sun Mar 9 09:06:51 2014 +0100

    Added README
...

It prints tag author, creation date, message, GnuPG signature if present and the information about the referenced commit. If the tag is lightweight, the output will be limited to the information about the referenced commit.

Verifying signed tag

Additionally, if the tag is signed, the signature placed inside the tag can be verified to ensure that the tag was created by an authorized person:

$ git tag -v v1.4
object 8f96002526eb5e6b818d719ebe72a7db381eed39
type commit
tag v1.4
tagger Robert <robert@example.com> 1394358295 +0100

Release version 1.4
gpg: Signature made Sun Mar  9 10:44:55 2014 CET using RSA key ID A231EC80
gpg: Good signature from "Robert <robert@example.com>"

In this case the tag was verified successfully so we can trust it. The verification may fail for two reasons. The typical reason is that we don’t have the signer’s public key. In this case we have to first obtain his/her public key and store it into our GnuPG keyring. Then we can retry the verification. The other unlikely reason is that the tag was created by an unauthorized person in which case we cannot trust this tag at all.

Pushing tags

In most cases you would want to push the newly created tag to the remote repository to make it available to everyone in the development team. It can be done similarly to pushing the branches:

$ git push origin v1.4

Deleting tags

Generally, there is no reason to delete the tags because they are inexpensive and don’t use much resources. The only exception is when you have mistakenly created a tag pointing to the wrong commit. In this case you should delete the local tag:

$ git tag -d v1.4

and remove it from remote repository if it was already pushed:

$ git push origin :v1.4

Moreover, if you have already pushed the wrong tag to the remote repository, the corrected tag should have different name than the wrong one. Otherwise, you may end up in a situation that some team members have a new tag while the rest still have the old one.

Conclusion

While I tried to describe the most interesting and useful functionalities related to tagging, there are still some things which were left out. You can find more about them in git-tag manual page.

Posted in cryptography, Git, Version control | Tagged , | 2 Comments

How to handle HTML forms in Spring

Spring MVC is a part of Spring Framework which main purpose is to simplify development of web applications. In this article I would like to explain how to handle HTTP requests, render the web page and process simple HTML forms in Spring.

To show this we will build a simple application which displays first name, second name and age of already added persons at the top of the page and which additionally displays and handles HTML form for adding more persons. The main window of the application will look like this:
springmvcforms

Servlet configuration

Like most Java web frameworks Spring is based on the servlet technology and therefore it requires special definitions in WEB-INF/web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    
    <servlet>
        <servlet-name>springmvcforms</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>springmvcforms</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

We specify there that we want to use class org.springframework.web.servlet.DispatcherServlet as our main servlet and we map it to handle any requests arriving to our application.

Spring configuration

In Spring we have to additionally provide configuration for our dispatcher servlet named springmvcforms. We can do so by placing an XML file named springmvcforms-servlet.xml in the same directory as web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
 
    <context:component-scan base-package="com.example.springmvcforms" />
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:annotation-driven />
             
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

By default the name of the configuration file is a concatenation of the name of the servlet and -servlet.xml suffix. It possible to change its name but we want to stick to the default values because we want to limit the size of the configuration.

We specify two important tags <context:component-scan> and <mvc:annotation-driven> to inform Spring that we want to use annotations for our beans located under given base package (former) and for our web controllers (later). Without annotations we would have to add every bean and web controller definition to this file which will greatly increase its size.

We also inform Spring that we want to hold our static resources like images, CSS files and scripts under /resources/ directory of our application. By adding mapping attribute we tell Spring that whenever any HTTP request arrives for URL starting with /resources/ (including its children and all other descendants), we want to return one of our resources. Defining static resources improves performance because Spring will serve the resources directly without any checking or processing.

At the end of the configuration file we define our view resolver. Generally, view resolver is responsible for mapping the logical view name (as returned by web controllers) into the actual JSP, FreeMarker, Tiles or any other view file and then for converting it into appropriate HTML file which will be served to the web browser.

We use InternalResourceViewResolver which locates the actual view file in WAR by adding given prefix and suffix to the logical view name. Because we also want to use JSP with JSTL we configure our own view class.

Web controller

In our application we have to tell Spring which URLs we can handle and how. We can do so by creating a web controller class:

package com.example.springmvcforms;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "/personlist")
public class PersonListController {
    
    @Autowired
    private PersonList list;
    
    @RequestMapping(method = RequestMethod.POST)
    public String add(@ModelAttribute(value = "newPerson") Person newPerson, BindingResult result, Model model) {
        if (result.hasErrors())
            return "personlist";
        
        list.addPerson(newPerson);
        return "redirect:/personlist";
    }
    
    @RequestMapping(method = RequestMethod.GET)
    public String list(Model model) {
        model.addAttribute("list", list.getAll());
        model.addAttribute("newPerson", new Person());
        return "personlist";
    }
}

Annotations @Controller and @RequestMapping at class level inform Spring that this class is a web controller and will serve requests for /personlist relative URL.

We don’t have to add this class to any configuration file or register it in any way because Spring will automatically scan all classes within base package specified in XML configuration file and register them.

Gathering data to display

Let’s first take a look at method list which will be called whenever HTTP GET request arrives for /personlist relative URL. This method takes an instance of a model which it fills with two attributes. One is the list of all already added persons and which will be used for display it on the web page. The second is a new person which will be bound to the HTML form and which properties will be attached to appropriate fields of the form.

Person class is a very simple POJO class containing only three properties:

package com.example.springmvcforms;

import java.io.Serializable;

public class Person implements Serializable {
    private static final long serialVersionUID = 3297423984732894L;
    
    private String firstName;
    private String lastName;
    private Integer age;

    public Person() {
        firstName = "";
        lastName = "";
        age = null;
    }
    
    public String getFirstName() {
        return firstName;
    }
    
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

Displaying data

In the list method we return the logical name of the view to display (personlist) which will be mapped to a JSP file at location /WEB-INF/views/personlist.jsp in our WAR:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Person List</title>
        <link href="<s:url value='/resources/css/styles.css'/>" rel="stylesheet" type="text/css"/>
    </head>
    <body>
        <p>
            <c:forEach var="person" items="${list}">
                <span class="box-with-border"><c:out value="${person.firstName} ${person.lastName} (${person.age})" /></span>
            </c:forEach>
        </p>
        
        <sf:form method="POST" modelAttribute="newPerson" cssClass="personForm">
            <table>
                <tr>
                    <td><sf:label path="firstName">First name:</sf:label></td>
                    <td><sf:input path="firstName"/></td>
                </tr>
                <tr>
                    <td><sf:label path="lastName">Last name:</sf:label></td>
                    <td><sf:input path="lastName"/></td>
                </tr>
                <tr>
                    <td><sf:label path="age">Age:</sf:label></td>
                    <td><sf:input path="age" /></td>
                </tr>
                <tr><td></td><td><input type="submit" value="Add" /></td></tr>
            </table>
        </sf:form>
        
    </body>
</html>

In the <c:forEach> element we iterate over all persons using the list attribute which we have previously added to the model in our controller class. Then we display our HTML form and we bind it to the newPerson attribute from model. We don’t define action attribute for the form so the form data will be sent back to the same URL from which it was received and we use POST method so there is no conflict between displaying the persons and creating a new person.

As you could notice we use special Spring-forms tag library which provides path attribute. This attribute attaches the properties of our model attribute newPerson to the appropriate HTML elements.

Processing the HTML forms

If we submit the form by clicking Add button, the form data will be sent to the server to /personlist relative URL using the POST method. Spring will recognize this request and will call add method of our web controller with appropriate arguments:

@RequestMapping(method = RequestMethod.POST)
public String add(@ModelAttribute(value = "newPerson") Person newPerson, BindingResult result, Model model) {
    if (result.hasErrors())
        return "personlist";
        
    list.addPerson(newPerson);
    return "redirect:/personlist";
}

The arguments contain the reference to our model attribute newPerson with properties set based on the data submitted in the form, the reference to BindingResults which contains form validation errors (if any) and the model which may be used to render the response. We annotated the attribute newPerson with @ModelAttribute to explicitly state which model attribute we mean.

In the add method we check if there are validation errors (e.g. age field in the HTML form could not be converted into a number). If the validation went fine, we add the person to the list and we redirect the user to our main page. Otherwise, we return the user back to the form so that he/she has a chance to fix the problems.

Handling the home page

If a user enters the home page of our application (e.g. http://localhost:8080/springmvcforms/), he/she will receive HTTP 400 error which is a bit discouraging. We can change it by creating a new web controller to handle requests for our home page:

package com.example.springmvcforms;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomePageController {

    @RequestMapping("/")
    String showHome() {
        return "redirect:/personlist";
    }
}

This is the easiest controller so far and it redirects the user to our /personlist web page. Also note that we don’t pass any arguments to our showHome method because we just don’t use them.

Conclusion

As you could see displaying web pages and handling forms in Spring is quite simple and straightforward. Although the configuration in XML files may seem overwhelming, it is done only once and usually does not need to changed when adding more controllers or views.

Because I wanted to keep the example quite simple and easy to deploy, I did not use database to store the list of persons but instead I used a bean with a session scope.

The complete code for the example can be found at GitHub.

Posted in Java, Spring | Tagged , , , , , , | 6 Comments

Git: how to resolve merge conflicts

When working in several people on the same part of code, you will sooner or later end up having merge conflicts. At first merge conflict may seem overwhelming but once you resolve few of them, they will cease to be something difficult or scary. In this article I would like to explain why conflicts occur and how to deal with them in your day-to-day work.

What is merge conflict

When you are merging some changes into your current branch, Git tries to merge them automatically as much as possible. It means that for every change in the source branch (the branch you want to merge into your current branch) Git will try to find the right place in one of the files and will try to apply the change. If everything go smoothly and Git has no problem in finding the right place and applying the changes, the whole merge operation will be fully automatic and will not require any user interaction.

However, if Git has any problems or doubts when merging, it will try to merge automatically as much as it can and then will report the list of all merge conflicts it has found and couldn’t resolve by itself:

$ git merge src-branch
Auto-merging filevisitor/src/main/java/com/example/filevisitor/PrintingFileVisitor.java
CONFLICT (content): Merge conflict in filevisitor/src/main/java/com/example/filevisitor/PrintingFileVisitor.java
Automatic merge failed; fix conflicts and then commit the result.

In this case we have one file which was changed in both current and src-branch branches and which could not be merged automatically.

Of course, merge conflicts happen not only when explicitly merging branches using git merge command but may also occur when pulling the changes from remote (e.g. git pull) or when applying the changes from stash into your working copy.

To make it complete I would like to point several common root causes of merge conflicts:

  • modifying the same parts (lines) of the file in both branches
  • replacing the contents of the file (e.g. replacing image or other binary file) in both branches
  • removing the file in one branch but modifying it in another

Presentation of conflicts

When you open a file with merge conflicts in your favourite text editor or IDE and scroll through it, you will quickly notice special markers <<<<<<<, ======= and >>>>>> in it:

    public PrintingFileVisitor() {
        prefix = new StringBuffer();
        fileCount = 0;
<<<<<<< HEAD
        dirCount = 0;
=======
        allCount = 0;
>>>>>>> src-branch
    }

These markers mark the parts of the code which Git was unable to merge automatically and which must be merged manually. The part above ======= comes from HEAD – the top of your current branch. The bottom part is more interesting because it contains the changes from the source branch and which could not be merged into your current branch. A quick look at the example shows that field dirCount was renamed to allCount in source branch and this is the cause of the conflict.

Of course, this is a very simplistic example and in real cases single conflict may span several lines and there may be several conflicts in the same file.

Resolving the conflict

The idea of resolving the conflict is simple: just modify the files so that everybody will be happy (including you, your boss and the team members who made the changes on the source branch) and commit them. Generally, you should select the right changes from your current branch (top part) and from the source branch (bottom part) and mix them together to form working code.

When you are done, you should remove the markers, make sure that the file compiles successfully (in all programming languages I know leaving markers cause compilation errors) and call git add to mark the conflict as resolved:

$ git add file_with_resolved_conflicts

This will also add the file to the index. After you repeat the same process for other conflicting files, you can safely commit your changes using git commit command:

$ git commit -m 'Merged with branch src-branch and resolved the conflicts.'

If you don’t know how to mix both parts of the code together, you should consult your team members for explanation of their changes and help.

Choosing one version

Sometimes after looking at all conflicts in a file, you decide that you don’t want to mix changes from both branches but instead you want to use the file from one specific branch. In this case you can either choose the file from your current branch and reject any changes from the source branch using command:

$ git checkout --ours file_name

or choose the file from the source branch and reject any changes from your current branch:

$ git checkout --theirs file_name

While very useful these commands should be used carefully because they increase the risk of mistakenly loosing some changes (e.g. very important bug fixes) from one of the branches.

When you have a conflict in a binary file, editing the file manually is not an option and these commands are the best way to resolve the conflict.

Aborting the merge

At any time before you commit the changes, you may decide to abort the whole merge operation using command:

$ git merge --abort

It will revert the state of the working copy to the moment it was before issuing git merge command. Then you may try to merge the same branch again if you want.

This command is especially useful when you have made a lot of wrong decisions when resolving the conflicts and you would like to start merging from scratch.

Merge with deleted files

Merge conflict may also occur when the file was modified in one branch and it was deleted in another branch. Here is a sample situation:

[robert@epsilon filevisitor]$ git merge src-branch 
CONFLICT (modify/delete): filevisitor/src/main/java/com/example/filevisitor/PrintingFileVisitor.java deleted in src-branch and modified in HEAD. Version HEAD of filevisitor/src/main/java/com/example/filevisitor/PrintingFileVisitor.java left in tree.
Automatic merge failed; fix conflicts and then commit the result.
[robert@epsilon filevisitor]$ git status
On branch dest-branch
You have unmerged paths.
  (fix conflicts and run "git commit")

Unmerged paths:
  (use "git add/rm ..." as appropriate to mark resolution)

        deleted by them:    src/main/java/com/example/filevisitor/PrintingFileVisitor.java

Resolving this type of conflict is pretty easy. You just have to tell Git whether you want to keep the file in your current branch using command:

$ git add file_name

or if you want to remove it completely:

$ git rm file_name

Conclusion

The only difficult part when resolving a merge conflict is properly mixing code from both branches so that no feature or bug fix is lost. If you are unsure whether it was done correctly, you can run all test cases or even test the functionality manually before you finally commit the changes. And if needed you may also ask your team members for help.

You may always consult git-merge and git-checkout manual pages for more information.

Posted in Git, Version control | Tagged , | 8 Comments

Recursively walking directory using Java NIO

Sometimes you may need to recursively visit all files and subdirectories of given directory and perform some actions on it. The typical use case is searching files of a given type, copying or removing a directory including all its contents. The task is not very difficult but doing it manually is a bit cumbersome. The good thing is that Java 7 introduced a very convenient way of doing it using FileVisitor.

General workflow

When you want to recursively visit specific directory, you call one of the following methods from Files class:

Path walkFileTree(Path start, FileVisitor<? super Path> visitor);
Path walkFileTree(Path start, Set<FileVisitOption> options, int maxDepth, FileVisitor<? super Path> visitor);

The first method begins recursive (depth-first) traversal process starting from the path specified as the first argument and invokes appropriate methods on visitor when some events occur. Actually, there are four possible events and they relate to the following methods of FileVisitor interface:

  • preVisitDirectory – Called when entering a directory (before any entry of a directory is visited)
  • postVisitDirectory – Called when leaving a directory (after all entries of a directory were visited including its subdirectories)
  • visitFile – Called when visiting the file
  • visitFileFailed – Called when some error occurred when visiting given file or directory

The second walkFileTree method gives more control by allowing to specify special options and maximum depth of the recursive traversal. At the moment there is only one available option FOLLOW_LINKS which indicates whether the traversal follows symbolic links.

It’s worth noting that the first method does not follow symbolic links and does not limit the depth of the traversal.

Controlling visiting process

Each method of FileVisitor returns FileVisitResult enumeration value which indicates whether and how to continue the traversal. It can have four possible values:

  • CONTINUE – Specifies that the traversal should continue normally
  • SKIP_SUBTREE – Specifies that given directory and its contents should not be traversed (they should be skipped). This value makes sense only for preVisitDirectory
  • SKIP_SIBLINGS – Specifies that siblings of the current file or directory should be skipped
  • TERMINATE – Specifies that the traversal should be aborted immediately without invoking any other callbacks

In a typical case CONTINUE should be used when traversing.

Handling errors

When an error occurs during the traversal (e.g. permission denied error for a file or directory), no exception is thrown but instead method visitFileFailed from FileVisitor interface is called with the reference to that exception. Within this method you may decide whether to ignore the error, handle it somehow or to explicitly throw the exception which will in turn terminate the traversal.

Adapter SimpleFileVisitor

If you don’t want to override all methods of FileVisitor, you may choose to extend its SimpleFileVisitor adapter class. Methods in this class continue traversal normally unless there is an error, in which case they throw the IOException.

Example

Below is a simple implementation of FileVisitor which prints the visited files and directories on screen and also calculates few simple statistics:

package com.example.filevisitor;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;

class PrintingFileVisitor implements FileVisitor<Path> {

    private static final String TAB = "    ";
    private static final int TAB_SIZE = TAB.length();
    
    private StringBuffer prefix;
    private int fileCount;
    private int directoryCount;
    
    public PrintingFileVisitor() {
        prefix = new StringBuffer();
        fileCount = 0;
        directoryCount = 0;
    }

    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
        System.out.printf("%s%s:%n", prefix, dir.getFileName());
        prefix.append(TAB);
        directoryCount++;
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
        prefix.setLength(prefix.length() - TAB_SIZE);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        System.out.printf("%s%s%n", prefix, file.getFileName());
        fileCount++;
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
        System.out.printf("%s%s (failed)%n", prefix, file.getFileName());
        return FileVisitResult.CONTINUE;
    }

    public void printSummary() {
        System.out.printf("Files: %d    Directories: %d%n", fileCount, directoryCount);
    }
    
}

The most important thing to notice here is that the visitor continue the traversal normally in case of errors instead of terminating it.

The actual traversal is started using the method:

private static void visitRecursively(Path path) throws IOException {
    PrintingFileVisitor visitor = new PrintingFileVisitor();
    Files.walkFileTree(path, visitor);
    visitor.printSummary();
}

The complete code for this example can be found at GitHub.

Posted in Java | Tagged , , | 4 Comments

Calculating cryptographic hash functions in Java

Cryptographic hash function is an algorithm which takes block of data of arbitrary length as an input and outputs a short fixed-length sequence of bits (usually 128-512 bits) and which ideally should have following properties:

  • it is very easy to compute the hash value for any message
  • it is infeasible to generate a message which has given hash
  • it is infeasible to modify a message without changing the hash
  • it is infeasible to generate two different messages with the same hash

Cryptographic hash functions are said to be one-way functions because it’s easy to compute its value but it is technically impossible to get the original message back (or even generate good candidates for it) based on the hash value.

Java implementation from Oracle supports the most popular cryptographic hash function which include:

  • MD5
  • SHA-1
  • SHA-256
  • SHA-384
  • SHA-512

Using standard Java API

In order to calculate the hash you have to first obtain instance of MessageDigest class for specified algorithm:

MessageDigest digest = MessageDigest.getInstance("SHA-256");

The available algorithms are on the list above and if you specify something unsupported, exception NoSuchAlgorithmException will be thrown.

Once you have got the instance, you can push there some binary data using various update methods which accept single byte, ByteBuffer, array of bytes or some portion of it:

digest.update(buffer, 0, readBytes);

When you are done, you can call digest method (optionally with some additional data) to complete the computation and fetch the calculated hash value as an array of bytes.

Alternatively, if your data is short and available in a single ByteBuffer or array, you can omit calls to update method and directly call one of the convenience digest methods passing it the data. It will automatically update the message digest using the provided data and will return the calculated hash value.

The complete code which calculates the hash function for a file using SHA-256 algorithm is provided below:

private static void calculateSha256(Path path) throws IOException, NoSuchAlgorithmException {
    byte[] buffer = new byte[BUFFER_SIZE];
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
        
    try (InputStream is = Files.newInputStream(path)) {
        while (true) {
            int readBytes = is.read(buffer);
            if (readBytes > 0)
               digest.update(buffer, 0, readBytes);
            else
               break;
        }
    }
        
    byte[] hashValue = digest.digest();
    System.out.printf("SHA256(%s) = %s%n", path, bytesToHex(hashValue));
}

The only important thing to notice is that we cannot print the returned hash value directly on the screen but we have to first convert it to a user-readable form using bytesToHex method:

private static String bytesToHex(byte[] hashValue) {
    Formatter form = new Formatter();
    for (int i = 0; i < hashValue.length; i++)
       form.format("%02x", hashValue[i]);
    return form.toString();
}

Using Commons Codec API

As you could notice most of the code above was responsible for reading a file instead of calculating the hash. In most cases it can be greatly simplified using DigestUtils class from Apache Commons Codec library. With it our code can be simplified to:

private static void calculateSha256CommonsIO(Path path) throws IOException {
    try (InputStream is = Files.newInputStream(path)) {
        byte[] hashValue = DigestUtils.sha256(is);
        System.out.printf("SHA256(%s) = %s%n", path, bytesToHex(hashValue));
    }
}

or even:

private static void calculateSha256CommonsIO2(Path path) throws IOException {
    try (InputStream is = Files.newInputStream(path)) {
        String hashString = DigestUtils.sha256Hex(is);
        System.out.printf("SHA256(%s) = %s%n", path, hashString);
    }
}

There are also overloaded versions of sha*/md* methods which accept String and array of bytes.

Using hash functions on strings

You may wonder how DigestUtils calculates hash function for a string while hash functions don’t operate on characters but rather on bytes. The answer is simple: it converts the string to its byte representation using UTF-8 encoding and then calculates hash on it.

Generally, this works pretty fine but you may run into problems if some other code (other part of your application or other application which you are exchanging data with) uses different character encoding or what is worse uses platform’s default character encoding. Therefore, it has to be used carefully and should be double-checked.

Security consideration

There are several cryptographic hash algorithms which were good in the past but now their usage is discouraged. The list includes MD2 and MD5 algorithms which security was compromised some time ago and also SHA-1 which is very similar to MD5 and therefore may be also compromised in near future. They shouldn’t be used in new code and the only exception is when you are communicating with applications or using protocols which does not support any stronger algorithm.

In practice you should always use SHA-2 family of algorithms or something of similar or higher strength.

Conclusion

Cryptographic hash functions are used very often in practice even in areas which don’t fully utilize their power like checking for data transmission errors. Computing them is fairly easy in Java and require only few lines of code.

The sample code for this article can be found at GitHub.

Posted in cryptography, Java | Tagged , , , , | Leave a comment