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. 

RESTfull webservice

REST stands for Representational State Transfer. REST defines a set of architectural principles by which you can design Web services that focus on a system's resources where as SOAP focus on messaging.

To become REST full following constrains must be satisfied.
  • Client Server system
  • Must be stateless.
  • Support caching system
  • Uniformly accessible


Abstractions that make a Restful system, namely resources, representations, Uri’s, and the HTTP request types that make up the uniform interface used for client/server data transfers.

A Restful resource can be anything that is addressable over the Web.

A representation is a temporal state of the actual data located in some storage device at the time of a request.

A Uniform Resource Identifier, or URI, in a Restful web service is a hyperlink to a
resource


We have four specific actions that we can take upon resources—Create, Retrieve, Update, and Delete (CRUD)


We can now map our CRUD actions to the HTTP methods POST, GET, PUT, and DELETE as follows:

Data action HTTP protocol equivalent
CREATE          POST
RETRIEVE       GET
UPDATE          PUT
DELETE          DELETE


As of today there are a couple of JAX-RS libraries used by the Java community:
Jersey, Restlet, RestEasy etc

check other post for REST implementation in Jersey JAX-RS.

Sunday, February 21, 2010

Speed Searches using search engine:

Do you want to implement a full text search engine into your website? People are seeking information on the web. so why not speed up your search by using Lucene , Sphinx or other open source search engine. There are other search engines available out there but I found so far that this two are much easier to integrate. So use which one for your web site is the question.

May be your web site is now running on the sever and most of us use mysql for web application. Considering this situation I think Sphinx would better choice. Though lucene is much advanced then Sphinx but it will require some code written in java to pull data from mysql and then index them infect there is no api for php too (check nutch may be have something for you ). On the contrary sphinx will require no coding as log as you do not want to deal with the live index. If you want to deal with the live index then you just have to write a small pice of shell code to run indexing and merging operation.

I run sphinx on Windows and Linux. I am going to show you how to run sphinx on window and do indexing and merging. I hope this will help you.


Install Sphinx:
Download sphinx(I use sphinx-0.9.9-win32) from Sphinx site as I told you that I run it on windows so download zip version an unzip it.


Sphinx Configuration:
If you do not want to deal with live index then insert following configurations into your sphinx.conf (you can put it any where) :

source Files
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass =root
sql_db = data_base
sql_port = 3306
sql_query_pre = SET NAMES utf8

sql_query =SELECT id, name, size,address FROM Files;
sql_ranged_throttle = 0

}

index Files
{

source = Files
#docinfo = extern
path = /path/to/index
min_word_len = 3
}


indexer
{
mem_limit = 32M
}


searchd
{
port=9313
log = /path/to/searchd.log
query_log = /path/to/query.log
pid_file = /path/to/searchd.pid
}


Now just open command prompt and run following command and run following command

For indexing:
Path/to/indexer --config path/to/sphinx.conf Files [or put –all in place of Index name]

For Searching :
Path/to/search --config path/to/sphinx.conf Files 765432

so how you gona use it for web pages. Sphinx provide nice api for php, java and ruby yon get it from you downloaded zip. Let se how we can use it in php.

Run following command in the prompt:
Path/to/searchd --config Path/to/sphinx.conf(do not close command prompt)

Run the following code in you webserver(I use apache)

require('sphinxapi.php'); (check path/tp/sphinx/api)
$sp = new SphinxClient();
$sp->SetServer('localhost', 9313);
$sp->SetMatchMode(SPH_MATCH_ALL);
$sp->SetMatchMode(SPH_MATCH_ALL);
$sp->SetArrayResult(true);
$results = $sp->Query('kamal');
print_r($results);

Isn’t it easy to use sphinx?

Now what about live index. What is this live index is. Actually this is most critical part because you might want to keep your user updated as a result you need to update index. May be you have a solution that you are going to reindex. But indexing is slow believe me when you have gigabyte of data to index it will take lots of time. Further if you check sphinx document you could found that Sphinx at the moment is designed for maximum indexing and searching speed as a result indexing is slow.

So thats why merging come into the picture. Merging is relatively easier then the reindexing and inexpensive check the following configuration for live indexing:

source Files
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass =root
sql_db = data_base
sql_port = 3306# optional, default is 3306
sql_query_pre = SET NAMES utf8

sql_query =SELECT id, name, size,address FROM Files;
sql_ranged_throttle = 0
}


source delta : Files
{
sql_query_pre = SET NAMES utf8
sql_query =SELECT id, name, size,address FROM Files;

}


index Files
{

source = Files
path = path/to/index
min_word_len = 3
}


indexer
{
mem_limit = 32M

}

searchd
{
port=9313
pid_file = /path/to/log/searchd.pid

}


Now run the follwing commands to index and merging:

In the first phase:
indexer --config path/to/sphinx.conf files
Form the rest of the phase
indexer --config path/to/sphinx.conf delta
indexer --merge main delta --rotate --merge-dst-range deleted 0 0

--rotate switch will be required if DSTINDEX is already being served by search and last one will delete the existing document from the index

Never ever index on files coz this is our main index where user will search

I hope this will help you to implement sphinx and speed up your search


You can write a shell script or java thread(as i am java developer) to do this process automated.

The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

AWS Services

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