Tuesday, April 5, 2011

New jQuery version into Liferay Portal

Copy the file "top_head.jsp" located in folder "liferay-portal-src-[version]\portal-web\docroot\html\common\themes\" into the EXT-folder 'liferay-portal-ext\ext-web\docroot\html\common\themes\'

>Open the copied file in your EXT
>Add the following lines:
<script  src="/html/js/jQuery1.3.2/jquery1.3.2.js" type="text/javascript"></script>
<script  src="/html/js/jQuery1.3.2/ui.jquery1.7.js" type="text/javascript"></script>
<script >="" src="/html/js/jQuery1.3.2/ui.stars.js" type="text/javascript"></script>

<script type="text/javascript">
var $132 = jQuery.noConflict(true);
 </script>
> Now you can access jQuery 1.3.2'.

Saturday, December 25, 2010

how to use ZTE Modem in linux


To intall Citycell ZTE Modem in linux you should follow following steps

  1. Plug Modem to PC
  2. open terminal and use lsusb command which will show usb device plugged at the time in my case I get following out put
#lsusb
    Bus 007 Device 002: ID 0a5c:2101 Broadcom Corp. Bluetooth Controller
    Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 006 Device 002: ID 15d9:0a4c Trust International B.V.
    Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 005 Device 003: ID 19d2:fff1 ONDA Communication S.p.A.
    Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    Bus 001 Device 002: ID 064e:a101 Suyin Corp. Acer CrystalEye Webcam
    Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
bold line is my ZTE Modem from that output I get vendor is 19d2 and product id fff1

  1. now load driver using modprobe command
    # sudo modprobe usbserial vendor=0x19d2 product=0xfff
  2. now edit wvdial config
    #sudo gedit /etc/wvdial.conf
    insert followin lines
    [Dialer Defaults]
    Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
    Modem Type = Analog Modem
    ISDN = 0
    Init1 = ATZ
    Modem = /dev/ttyUSB0
    Baud = 9600
    [Modem0]
    Init1 = ATZ
    SetVolume = 0
    Modem = /dev/ttyUSB0
    Baud = 230400
    FlowControl = Hardware (CRTSCTS)
    Dial Command = ATDT
    [Dialer citycell]
    Stupid Mode = 1
    Inherits = Modem0
    Password = waps
    Username = waps
    Phone = #777
  3. now dial you moden using following command
    #sudo wvdial citycell
If you do not have wvdial installed in you pc then install follwing:
  1. wvdial_1.60.1
  2. libxplc0.3.13_0.3.13-1
  3. libwvstreams4.4-base
  4. libwvstreams
  5. libuniconf

Thursday, December 23, 2010

Ubuntu USB file transfer is slow

Some time you might experence Ubuntu USB file transfer is slow then you can try following solution
$ sudo gedit /etc/default/grub

edit as following
#GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash pci=noacpi pci=routeirq"

then reload
$ sudo grub-mkconfig -o /boot/grub/grub.cfg


This fix might not work for every one.

RabbitVCS an alternative to TortoiseSVN

TortoiseSVN is not avail able for linux but we can use rabbitVCS as an alternative to Tortoise. Check following to install rabbitVCS in ubuntu.

Add repository source
$ sudo add-apt-repository ppa:rabbitvcs/ppa

update source list
$ sudo apt-get update

install rabbitvcs core
$ sudo apt-get install rabbitvcs-core

Install nautilus,thunar,gedit and command line plugins
$ sudo apt-get install rabbitvcs-nautilus rabbitvcs-thunar rabbitvcs-gedit rabbitvcs-cli


rabbitVCS installed in your ubutu system. Enjoy it.

If you can't see rebbitVCS in you context menu check this link

Wednesday, September 8, 2010

Hibernate Basics


Hibernate is an open source object relational mapping library. Hibernate provides a solution to map database tables to a class. It copies one row of the database data to a class. In the other direction it supports to save objects to the database. Objects are transformed to one or more tables.


Hibernate solves Object-Relational impedance mismatch problems by replacing direct persistence-related database accesses with high-level object handling functions.


Basic classes used while working with Hibernate are:
§         SessionFactory
§         Session
§         Transaction

SessionFactory
A thread safe, immutable cache of compiled mappings for a database. The SessionFactory is created from a Configuration object.

The SessionFactory is an expensive object to create. It like is usually created during application start up. However, unlike the Configuration object, it should be created once and kept for later use. The SessionFactory object is used by all the threads of an application.

SessionFactory object is created per database. Multiple SessionFactory objects (each requiring a separate Configuration) are created when connecting to multiple databases.

Session
A single-threaded, short-lived object representing a conversation between the application and the persistent store.  
Session objects are not thread safe. Therefore, session objects should not be kept open for a long time. Applications create and destroy these as needed.  Typically, they should be created to complete a single unit of work, but may span many units.


Transaction

Transaction object created from Session. Single-threaded, short-lived object used by the application. A Transaction represents a unit of work with the database (and potentially other systems). Transactions in Hibernate are handled by an underlying transaction manager and transaction.

Instance states

An instance of persistent classes may be in one of three different states

Transient:
The instance is not, and has never been associated with any persistence context (A persistence context is a set of entity instances in which any persistent entity identity is unique). It has no persistent identity.

Persistent:
The instance is currently associated with a persistence context. It has a persistent identity (primary key value) and, perhaps, a corresponding row in the database.

Detached
The instance was once associated with persistence context, a detached instance is an has been persistent, but its Session has been closed. The reference to the object is still valid, and the detached instance might even be modified in this state.  detached instance can be reattached to a new Session at a later point in time, making it (and all the modifications) persistent again. Can be reattachment of detached instances using the session.update() or Session.merge()  methods

Session and transaction scopes

Session-per-operation: open and close a Session for every simple database call in a single thread

Session-per-request: The most common pattern in a multi-user client/server application. In this model, a request from the client is send to the server (where the Hibernate persistence layer runs), a new Hibernate Session is opened, and all database operations are executed in this unit of work. Once the work has been completed (and the response for the client has been prepared), the session is flushed and closed. 

Open Session in View: scope of a Session and database transaction until the "view has been rendered"

Session-per-conversation: The Hibernate Session may be disconnected from the underlying JDBC connection after the database transaction has been committed, and reconnected when a new client request occurs.

 


Locking:
There are two common strategies when dealing with updates to database records, pessimistic locking and optimistic locking.

Optimistic locking is a strategy where you read a record, take note of a version number and check that the version hasn't changed before you write the record back. Optimistic assumes that nothing's going to change while you're reading it.

Pessimistic Lock is where you lock the record/data for your exclusive use until you have finished with it so that no one can access it in between.

 


Hibernate Automatic Dirty Check:


Hibernate automatically detects object state changes in order to synchronize the updated state with the database, this is called dirty checking. An important note here is, Hibernate will compare objects by value, except for Collections, which are compared by identity. For this reason you should return exactly the same collection instance as Hibernate passed to the setter method to prevent unnecessary database updates.

Hibernate can perform dirty checks only when the objects are loaded and changed in the scope of a single Session

Friday, August 27, 2010

Unit Testing Jersey REST service

In my last post i show how to create Rest service with Jersey api. I also try to give some idea about the basic concept of REST in this link. Now we should start testing before start to do anything else. I write one test case using Jersey Test Framework also create one client using Jersey client tool.

The Jersey Test Framework currently allows you to run your tests on any of the following three light weight containers:
• Embedded GlassFish
• Grizzly Web Server
• Lightweight HTTP Server
The framework is built over JUnit 4.x using Maven.
For details information on Jersey Test Framework check this post


Test case for ProductResource
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.test.framework.JerseyTest;
import com.sun.jersey.test.framework.spi.container.TestContainerFactory;
import java.io.UnsupportedEncodingException;
import org.junit.Test;
import static org.junit.Assert.*;
import com.sun.jersey.test.framework.WebAppDescriptor;
import com.sun.jersey.test.framework.spi.container.http.HTTPContainerFactory;
import java.math.BigDecimal;
import org.model.Product;
import javax.ws.rs.core.MediaType;

/** *
* @author Kamal Hossain
*/
public class ProductServiceTest extends JerseyTest {

public static final String PACKAGE_NAME = "org.resources";
private WebResource ws;

public ProductServiceTest() {
super(new WebAppDescriptor.Builder(PACKAGE_NAME).build());
}

@Override
protected TestContainerFactory getTestContainerFactory() {
return new HTTPContainerFactory();

}

@Test
public void testProductResponse() throws UnsupportedEncodingException {
ws = resource().path("product/kamal");
ClientResponse response = ws.accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
assertEquals(200, response.getStatus());
System.out.println(response.getStatus());


}

@Test
public void testProductPrice() throws UnsupportedEncodingException {
ws = resource().path("product/kamal");
Product prod = ws.accept(MediaType.APPLICATION_XML).get(Product.class);
assertEquals(BigDecimal.valueOf(120), prod.getPrice());


}
}

Test case for ProductsResource
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.test.framework.JerseyTest;
import com.sun.jersey.test.framework.spi.container.TestContainerFactory;
import java.io.UnsupportedEncodingException;
import org.junit.Test;
import static org.junit.Assert.*;
import com.sun.jersey.test.framework.WebAppDescriptor;
import com.sun.jersey.test.framework.spi.container.http.HTTPContainerFactory;
import org.model.Product;
import java.util.Collection;
import java.util.List;
import javax.ws.rs.core.MediaType;

/** *
* @author kamal hossain
*/
public class ProductsServiceTest extends JerseyTest {
public static final String PACKAGE_NAME = 'org.resources';
private WebResource ws;

public ProductsServiceTest() {
super(new WebAppDescriptor.Builder(PACKAGE_NAME).build());
}

@Override
protected TestContainerFactory getTestContainerFactory() {
return new HTTPContainerFactory();

}

@Test
public void testProductResponse() throws UnsupportedEncodingException {
ws = resource().path("product/kamal");
ClientResponse response = ws.accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
assertEquals(200, response.getStatus());
System.out.println(response.getStatus());


}

@Test
public void testProductList() throws UnsupportedEncodingException {
ws = resource().path("products");
GenericType<Collection<Product>> genericType = new GenericType<Collection<Product>>(){};
List<Product> wsResult = (List<Product>) ws.accept(MediaType.APPLICATION_XML).get(genericType);
assertEquals(2, wsResult.size());

}    

}




You can also install firefox addon Poster to test services.

Monday, August 23, 2010

Jersey JAX-RS implementation


JAX-RS annotations

@Path Every Jersey resource has a URI pointing to it. A resource is identified with the @Path(“relative URI path”)annotation. This annotation is also used to define variables in URIs @Path(“relative URI path/{variable}”). Regular expression also allowed @Path("relative URI path/{variable:[a-zA-Z][a-zA-Z_0-9]}")

@GET, @PUT, @POST, @DELETE, and @HEAD are resource method designator annotations defined by JAX-RS and which correspond to the similarly named HTTP methods.

@Produces annotation is used to specify the MIME media types of representations a resource can produce and send back to the client. can be applied at both the class and method levels. This annotation works with @GET, @POST, and @PUT. if the value of the
HTTP header Accept is application/xml, the method handling the request returns
a stream of MIME type application/xml

@Consumes annotation is used to specify the MIME media types of representations a resource can consume. This annotation works together with @POST and @PUT. the client sets
the Content-Type HTTP header and the framework delegates the request to the
corresponding handling method.

@PathParam to extract a path parameter from the path component of the request URL. This annotation is used together with @Path and in conjunction with @GET, @POST,
@PUT, and @DELETE. we don't use the @PathParam annotation with a POST request.

@QueryParam is used to extract query parameters from the Query component of the request.

@FormParam is slightly special because it extracts information from a request representation that is of the MIME media type "application/x-www-form-urlencoded" and conforms to the encoding specified by HTML forms

@DefaultValue annotation is used to set default value to the query parameter if not exist.

@MatrixParam extracts information from URL path segments.

@HeaderParam extracts information from the HTTP headers.

@CookieParam extracts information from the cookies declared in cookie related HTTP headers

@Provider annotation is used for anything that is of interest to the JAX-RS runtime

@Context  ServletConfig, ServletContext, HttpServletRequest HttpServletResponse and SecurityContextare available using this annotation









Let’s start with creating project.
  1. create web project
  2. To add Jersey JARs (download link https://jersey.dev.java.net/)
asm-3.1.jar ,jsr311-api-3.1.jar ,jersey-bundle-1.3.jar,jersey-json-1.3.jar

3. Open the file "web.xml" and modify the file to the following.


<br />
<?xml version="1.0" encoding="UTF-8"?><br />
<web-app version="2.5" 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_2_5.xsd"><br />
<servlet><br />
<servlet-name>WebService</servlet-name><br />
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class><br />
<init-param><br />
<param-name>com.sun.jersey.config.property.packages</param-name> <param-value>org.resources</param-value> </init-param><br />
<load-on-startup>1</load-on-startup><br />
</servlet><br />
<servlet><br />
<servlet-name>ServletAdaptor</servlet-name><br />
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class><br />
<load-on-startup>1</load-on-startup><br />
</servlet><br />
<servlet-mapping><br />
<servlet-name>ServletAdaptor</servlet-name><br />
<url-pattern>/resources/*</url-pattern><br />
</servlet-mapping><br />
<servlet-mapping><br />
<servlet-name>WebService</servlet-name><br />
<url-pattern>/*</url-pattern><br />
</servlet-mapping><br />
<welcome-file-list><br />
<welcome-file>index.jsp</welcome-file><br />
</welcome-file-list><br />
</web-app><br />
<br />
<br />




The parameter "com.sun.jersey.config.property.package" defines in which package jersey will look for the web service classes. This property must point to your resources classes.

4. Create the following class.



package org.model;

import java.math.BigDecimal;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement
public class Product {

String name;
String description;
BigDecimal price;
int limit;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public BigDecimal getPrice() {
return price;
}

public void setProce(BigDecimal price) {
this.price = price;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public int getLimit() {
return limit;
}

public void setLimit(int limit) {
this.limit = limit;
}
}




package org.resources;

import com.sun.jersey.spi.resource.Singleton;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.model.Product;



@Path("/product/{name}")
@Singleton
public class ProductResource {

@GET
@Produces(MediaType.APPLICATION_XML)
public Product getXml(@PathParam("name") String name) {
Product p = new Product();
p.setName(name);
p.setDescription("descript");
p.setPrice(new BigDecimal(120));
return p;
}

@GET
@Produces(MediaType.APPLICATION_JSON)
public Product getJson(@PathParam("name") String name) {
Product p = new Product();
p.setName(name);
p.setDescription("descript");
p.setPrice(new BigDecimal(120));
return p;
}

@GET
@Produces(MediaType.TEXT_HTML)
public String getHtml(@PathParam("name") String name) {
Product p = new Product();
p.setName(name);
p.setDescription("descript");
p.setPrice(new BigDecimal(120));
return " " + "" + "Name: " + p.getName() + "
Description: " + p.getDescription() + "
Price: " + p.getProce() + "" + " ";
}

@Path("/allproducts")
@GET
@Produces(MediaType.APPLICATION_XML)
public List getProducts() {
List pl = new ArrayList();
Product p = new Product();
p.setName("Product x");
p.setDescription("descript");
p.setPrice(new BigDecimal(120));
pl.add(p);

p = new Product();
p.setName("Product x-2");
p.setDescription("descript-2");
p.setPrice(new BigDecimal(140));
pl.add(p);
return pl;
}
}


package org.resources;

import org.model.*;
import com.sun.jersey.spi.resource.Singleton;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;


@Path("/products")
@Singleton
public class ProductsResource {

@GET
@Produces(MediaType.APPLICATION_XML)
public List getXml(@DefaultValue("10") @QueryParam("limit") int limit) {
List pl = new ArrayList();
Product p = new Product();
p.setName("Product x");
p.setDescription("descript");
p.setPrice(new BigDecimal(120));
p.setLimit(limit);
pl.add(p);

p = new Product();
p.setName("Product x-2");
p.setDescription("descript-2");
p.setLimit(limit);
p.setPrice(new BigDecimal(140));
pl.add(p);

return pl;
}

@GET
@Produces(MediaType.TEXT_HTML)
public String getHtml() {
Product p = new Product();
p.setName("Product x");
p.setDescription("descript");
p.setProce(new BigDecimal(120));
return " " + "" + "Name: " + p.getName() + "
Description: " + p.getDescription() + "
Price: " + p.getProce() + "" + " ";
}
}








5. Now deploy in web server. I use Apache Tomcat v6.0


In the next post i will show you different way to test this service. 

AWS Services

      1.         Identity Access Management (IAM): Used to control Identity (who) Access (what AWS resources).                   1....