-->

lundi 16 décembre 2013

NoSQL : Using JSF2 Spring 3 with Couchbase in clever cloud

Introduction :

I only wanted to test couchbase without installing it. After search, i discovered that there are a Paas service offered by Clever-cloud. This PaaS is the first cloud platform that offer a couchbase as service. This article show how configure Couchbase Bucket and how to connect to this service using Spring.

First Step : Creation of the bucket in clever-cloud :

In this step, i will create an account in clever-cloud and sign in to can create an instance.
 

After creation, check your mail and verify that you received an email from clever cloud that contains all informations to connect to your instance, all parameters should be like this : 


db_host = ****************.couchbase.clvrcld.net
db_name = ****************
db_username = ****************
db_password = *******************************

The problem with clever-cloud is that you don't have access to manage your instance, but you can do this with your client.

Second Step : JSF 2 and Spring 3 application :

Here, just you create a jsf web app maven prototype. maven dependencies are as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  <!-- JSF -->
  <dependency>
   <groupId>com.sun.faces</groupId>
   <artifactId>jsf-api</artifactId>
   <version>2.1.13</version>
  </dependency>
  <dependency>
   <groupId>com.sun.faces</groupId>
   <artifactId>jsf-impl</artifactId>
   <version>2.1.13</version>
  </dependency>

  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
  </dependency>

  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>servlet-api</artifactId>
   <version>2.5</version>
  </dependency>

  <dependency>
   <groupId>javax.servlet.jsp</groupId>
   <artifactId>jsp-api</artifactId>
   <version>2.1</version>
  </dependency>

  <!-- EL -->
  <dependency>
   <groupId>org.glassfish.web</groupId>
   <artifactId>el-impl</artifactId>
   <version>2.2</version>
  </dependency>

  <!-- Tomcat 6 need this -->
  <dependency>
   <groupId>com.sun.el</groupId>
   <artifactId>el-ri</artifactId>
   <version>1.0</version>
  </dependency>

To inject Spring beans in jsf managed bean, you can use the dependency injection : JSR 330, to do this just add in your pom the dependency : 

1
2
3
4
5
6
  <!-- JSR-330 -->
  <dependency>
   <groupId>javax.inject</groupId>
   <artifactId>javax.inject</artifactId>
   <version>1</version>
  </dependency>

Now, to add Spring to your project, you can add dependencies in your pom file :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
  <!-- Spring framework -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>3.1.2.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-web</artifactId>
   <version>3.1.2.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>3.1.2.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-test</artifactId>
   <version>3.1.2.RELEASE</version>
   <scope>test</scope>
  </dependency>

At this stage, to add record in your data base, you should specify it in JSON format, and if you want to read it you should convert it from JSON to your code source langage, in our case, we should add firt the java couchbase-client and i will use google-gson :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
  <!-- couchbase -client -->
  <dependency>
   <groupId>couchbase</groupId>
   <artifactId>couchbase-client</artifactId>
   <version>1.1.0</version>
  </dependency>

  <!-- Gson: Java to Json conversion -->
  <dependency>
   <groupId>com.google.code.gson</groupId>
   <artifactId>gson</artifactId>
   <version>2.2.4</version>
   <scope>compile</scope>
  </dependency>

Don't forget to add the couch base repository to your pom : 


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
 <repositories>
  <repository>
   <id>couchbase</id>
   <name>Couchbase Repository</name>
   <layout>default</layout>
   <url>http://files.couchbase.com/maven2/</url>
   <snapshots>
    <enabled>false</enabled>
   </snapshots>
  </repository>
 </repositories>