Sunday, March 08, 2015

RAD 2– Spring for Vaadin without Spring Boot

In my previous blog RAD 1 – Spring for Vaadin, I have introduced the recommended way of integrating Spring and Vaadin. In this blog, I am going to use the alternative way which is without Spring Boot explained in the article“Getting started with Vaadin Spring without Spring Boot”.  This is the hardest way. I’ve configured this in the Intellij Idea 14.

  1. Create a maven archetype as explained in the Creating a Project with Intellij Idea:2.8.3 Create Maven Project.
  2. Create a new folder WEB-INF under the webapp folder.
  3. Create a applicationContext.xml under the WEB-INF folder.
  4. Now you have to add the Spring Facet to the project. You have to add the Spring Context created in the step 3. However, I first add the Spring module.

image

My applicationContext.xml is as follows

<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">

<bean class="au.com.blogspot.ojitha.MyUI.MyConfiguration"/>
<context:component-scan base-package="au.com.blogspot.ojitha"/>

</beans>

Here is the configuration file

package au.com.blogspot.ojitha;

import javax.servlet.annotation.WebListener;
import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.spring.annotation.EnableVaadin;
import com.vaadin.spring.annotation.SpringUI;
import com.vaadin.spring.servlet.SpringAwareVaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.ContextLoaderListener;

/**
*
*/
@Theme("valo")
//@Widgetset("au.com.blogspot.ojitha.MyAppWidgetset")
@SpringUI("")
public class MyUI extends UI {

@Override
protected void init(VaadinRequest vaadinRequest) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);

Button button = new Button("Click");
button.addClickListener(new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
layout.addComponent(new Label("Thank you for clicking"));
}
});
layout.addComponent(button);

}

@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
//@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends SpringAwareVaadinServlet {
}

@WebListener
public static class MyContextLoaderListener extends ContextLoaderListener{}

@Configuration
@EnableVaadin
public static class MyConfiguration {}


}

Here the pom.xml file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>au.com.blogspot.ojitha</groupId>
<artifactId>vaadinex2</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>vaadinex2</name>

<properties>
<vaadin.version>7.4.0</vaadin.version>
<vaadin.plugin.version>${vaadin.version}</vaadin.plugin.version>
<jetty.plugin.version>9.2.3.v20140905</jetty.plugin.version>
<project.source.version>1.7</project.source.version>
<project.target.version>1.7</project.target.version>
<project.encoding>UTF-8</project.encoding>
</properties>

<repositories>
<repository>
<id>vaadin-addons</id>
<url>http://maven.vaadin.com/vaadin-addons</url>
</repository>
<repository>
<id>vaadin-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/vaadin-snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-server</artifactId>
<version>${vaadin.version}</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-push</artifactId>
<version>${vaadin.version}</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-client-compiled</artifactId>
<version>${vaadin.version}</version>
<!--scope>provided</scope-->
</dependency>
<!--
Needed when using the widgetset optimizer (custom ConnectorBundleLoaderFactory).

For widgetset compilation, vaadin-client-compiler is automatically added on the
compilation classpath by vaadin-maven-plugin so normally there is no need for an
explicit dependency.
-->
<!--
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-client-compiler</artifactId>
<version>${vaadin.version}</version>
<scope>provided</scope>
</dependency>
-->
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-themes</artifactId>
<version>${vaadin.version}</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring</artifactId>
<version>1.0.0.alpha2</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<encoding>${project.encoding}</encoding>
<source>${project.source.version}</source>
<target>${project.target.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<encoding>${project.encoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<!-- Exclude some unnecessary files generated by the GWT compiler. -->
<packagingExcludes>WEB-INF/classes/VAADIN/gwt-unitCache/**,
WEB-INF/classes/VAADIN/widgetsets/WEB-INF/**</packagingExcludes>
</configuration>
</plugin>
<plugin>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-maven-plugin</artifactId>
<version>${vaadin.plugin.version}</version>
<configuration>
<extraJvmArgs>-Xmx512M -Xss1024k</extraJvmArgs>
<webappDirectory>${basedir}/target/classes/VAADIN/widgetsets</webappDirectory>
<draftCompile>false</draftCompile>
<compileReport>false</compileReport>
<style>OBF</style>
<strict>true</strict>
</configuration>
<executions>
<execution>
<goals>
<goal>update-widgetset</goal>
<goal>compile</goal>
<!-- disabled by default to use on-the-fly theme compilation -->
<!-- <goal>compile-theme</goal> -->
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>2.6.1</version>
<!-- Clean up also any pre-compiled themes -->
<configuration>
<filesets>
<fileset>
<directory>src/main/webapp/VAADIN/themes</directory>
<includes>
<include>**/styles.css</include>
<include>**/styles.scss.cache</include>
</includes>
</fileset>
</filesets>
</configuration>
</plugin>

<!-- The Jetty plugin allows us to easily test the development build by
running jetty:run on the command line. -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.plugin.version}</version>
<configuration>
<scanIntervalSeconds>2</scanIntervalSeconds>
</configuration>
</plugin>
</plugins>

<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<!-- TODO Remove when http://dev.vaadin.com/ticket/14924 is resolved -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>com.vaadin</groupId>
<artifactId>
vaadin-maven-plugin
</artifactId>
<versionRange>[7.1.11,)</versionRange>
<goals>
<goal>resources</goal>
<goal>update-widgetset</goal>
<goal>compile</goal>
<goal>compile-theme</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>

</build>

</project>

Comments are welcome.

RAD–1: Spring for Vaadin 7.4.0

My whole effort to create rapid application development (RAD) stack for Java Web Development. I chose Vaadin for front end development. My development environment is Intellij Idea 14. In this example, I am going to use the recently released Vaadin Spring Add on.

As a first, create a spring boot web application. You have to select the “war” option for the packaging and select the option “web” for the dependencies.

        <dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot</artifactId>
<version>1.0.0.alpha2</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-themes</artifactId>
<version>7.4.0</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-client-compiled</artifactId>
<version>7.4.0</version>
</dependency>

It is important to add the above dependencies to the newly created project.  Let us create just a simple view

package au.com.blogspot.ojitha.vaadin.view;

import com.vaadin.annotations.Theme;
import com.vaadin.server.VaadinRequest;
import com.vaadin.spring.annotation.SpringUI;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;

/**
* Created by Ojitha on 8/03/2015.
*/
@Theme("valo")
@SpringUI("")
public class RootUI extends UI {

@Override
protected void init(VaadinRequest vaadinRequest) {
getPage().setTitle("Root UI");
setContent(new Label("Hello ojtha, World!"));
}
}

Application is ready to deploy to tomcat 8. You can configure the tomcat in the File->Settings->Build, Execution, Deployment->Application Server. After that you can create a new “Run Configuration…” and create tomcat local configuration and add the web application (war or exploded) to that. Good to note that, under the Deployment tab, Application context should be specify. 

Here the complete pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>au.com.blogspot.ojitha</groupId>
<artifactId>vaadinex1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>vaadin-example-1</name>
<description>Vaadin spring example</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<repositories>
<repository>
<id>vaadin-addons</id>
<url>http://maven.vaadin.com/vaadin-addons</url>
</repository>
</repositories>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>au.com.blogspot.ojitha.vaadin.VaadinExample1Application</start-class>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency-->
<!--dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency-->
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot</artifactId>
<version>1.0.0.alpha2</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-themes</artifactId>
<version>7.4.0</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-client-compiled</artifactId>
<version>7.4.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<!--plugin>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot</artifactId>
</plugin-->
</plugins>
</build>

</project>

Comments are welcome.

Sunday, November 09, 2014

Java Generics Covariant and Contravariant

Introduction

Here the getter method

( ) => A

Covariant

A <: B

( ) => A <: ( ) => B

If a A is subtype of B, then getter or A is subtype of getter of B.

Here the setter method

A => ( )

Contravariant

B => ( ) <: A => ( )

Above Covariant and Contravariant can be explained from the following code:

Say A is Animal

public class Animal {
@Override
public String toString(){
return "I am a Animal";
}
}

Say B is Bear as follows

public class Bear extends Animal {
@Override
public String toString(){
return "I am a Bear";
}
}
 

As shown in the above code Bear is subtype of Animal.

Here the Zookeeper class where getter and setter methods are

public class Zookeeper {
private Animal animal;
public void setFeed(Animal animal){
this.animal =animal;
}

public Animal getFed(){
return this.animal;
}
}

As shown in the above Zookeeper class, there is no indication of Bear, but in the following Zoo client code

public class Zoo {
public static void main(String ...args){

Zookeeper zookeeper = new Zookeeper();
Bear bear = new Bear();
zookeeper.setFeed(bear);
Bear retCat = (Bear) zookeeper.getFed();
System.out.println(retCat);

}
}

In the method setFeed you can pass the Bear instance according to the contravariant subtyping, and in the getter method getFed returns the Bear instance as a reference of Animal that is because getters support covariant subtyping.

Invariant

But how to explain this concept. See the Bearkeeper class which is subtype of Zookeeper

/**
* Created by Ojitha on 9/11/2014.
*/
public class Zookeeper {
private Animal animal;
public void setFeed(Animal animal){
this.animal =animal;
}

public Animal getFed(){
return this.animal;
}
}

class BearKeeper extends Zookeeper{
Bear bear;

@Override
public void setFeed(Animal animal) {
this.bear = (Bear)animal;
}

public void setFeed(Bear bear){
this.bear = bear;
}

@Override
public Animal getFed() {
return this.bear;
}
}

As show in the above code, void setFeed(Animal animal)  override the method in the Zookeeper because subclass return the covariant of super return , while overloading with the method void setFeed(Bear bear) method because of invariants of setter methods in Java although Bear is subclass of Animal.

Arrays

Java Arrays support covariant but this is not a good idea. This made Java to throws the bad runtime exception java.lang.ArrayStoreException in the following code:

        Animal[] animals = new Animal[]{new Animal(), new Animal(), new Bear()};
Bear[] bears = new Bear[]{new Bear(), new Bear()};
animals = bears;
animals[1] = new Animal();

As shown in the above code, last line java.lang.ArrayStoreException has thrown to indicate that Animal instance is not a valid instance anymore because animals reference became a Bear type after the animals = bears assignment.

Array doesn’t support contravariant subtyping Sad smile.

Generics

In term of generic, array subtyping covariant means that type S[] is considered to be subtype of T[] wherever S is subtype of T.  Other hand, the subtyping relation for generics is invariant: List<S> is not considered to be subtype of List<T>. Therefore, wildcards present the covariant subtyping for the generics: List<S> is subtype of the List<? extends T> where S <: T. Wildcards introduce contravariant subtyping as well. For example, List<T> is subtype of List<? super S>.

Here the example for congtravariant

        List<Object> objs = new ArrayList<Object>();
objs.add(1);
objs.add(1.23);
List<? super Integer> integers = objs;
Object o = integers.get(1);
//Integer i = integers.get(0);

As shown in the above,  Object is a supper class of the Integer. Even Integer as well as Double has been added. The values are read as Object only. It is impossible to read as Integers. The is where GET/PUT principal comes to the play, that says “use extend if you are reading from the structure, as well as use super if you writing to the structure.