How to programmatically dump the threads list of a running java process in bash

It seems that jdb cannot accept commands from command line, so we must use expect.
I created the following expect script dumpthreads
You can use it like:
dumpthreads java_process_pid

#!/usr/bin/expect
set pid [lindex $argv 0];
spawn jdb -connect sun.jvm.hotspot.jdi.SAPIDAttachingConnector:pid=$pid;
expect ">";
send "threads\n";
expect ">";
send "quit\n";

How to convince the SmartGWT ListGrid to automatically fetch from the server only the data it displays

Since I was looking all over the net how to do this and I only found pieces of information, here is an example:

[java]
public class OperationsGrid extends ListGrid{
OperationsGrid(){
super();
TreeGridField accNoField = new TreeGridField(“accountNo”, 150);
TreeGridField commentField = new TreeGridField(“comment”, 150);

setFields(accNoField,commentField);
setHeight100();
setWidth100();
setAutoFetchData(true);
setAlternateRecordStyles(true);
setDataSource(OperationsDS.getInstance());
setShowFilterEditor(true);
}
}
[/java]

[java]
public class OperationsDS extends DataSource{
static OperationsDS instance = null;
static OperationsDS getInstance(){
if(instance == null){
instance = new OperationsDS();
}
return instance;
}

OperationsDS(){

setDataURL(xpath);
DataSourceTextField noField = new DataSourceTextField(“accountNo”, “Account Number”, 128);
DataSourceTextField commentField = new DataSourceTextField(“comment”, “Comment”, 128);

noField.setRequired(true);
noField.setPrimaryKey(true);

setFields(noField,commentField);
setDataFormat(DSDataFormat.JSON);
setRecordXPath(“response/result”);
setClientOnly(false);
}
@Override
protected Object transformRequest(DSRequest dsRequest){
String url = null;

if(dsRequest.getOperationType().equals(DSOperationType.FETCH)){
dsRequest.setActionURL(getDataURL()+”&start=”+dsRequest.getStartRow()+”&end=”+dsRequest.getEndRow());
}
return super.transformRequest(dsRequest);
}
@Override
protected void transformResponse(DSResponse response, DSRequest request,
Object data) {
DSOperationType operationType = request.getOperationType();

if (operationType == DSOperationType.FETCH) {
JSONArray info = XMLTools.selectObjects(data, “/response/resultInfo/numTotalRows”);
Integer total = (int)Math.round(info.get(0).isNumber().doubleValue());

info = XMLTools.selectObjects(data, “/response/resultInfo/startRow”);
Integer start = (int)Math.round(info.get(0).isNumber().doubleValue());

info = XMLTools.selectObjects(data, “/response/resultInfo/endRow”);
Integer end = (int)Math.round(info.get(0).isNumber().doubleValue());

if(total != null)
response.setTotalRows(total);
if(start != null)
response.setStartRow(start);
if(end != null)
response.setEndRow(end);
} else {
super.transformResponse(response, request, data);
}

}
}
[/java]

In this example the server outputs in JSON format and returns the the current start row, current end row and total number of rows in response/resultInfo.
Basically you need to override transformRequest and transformResponse. In transformRequest you should add the start and end row to the requestURL and in transformResponse you should set the startRow, endRow, and totalRows, provided that you receive this information from your server. Also don’t forget to setAutoFetchData(true) on the grid.

Hope this helps somebody.

MySQL Java SQLException error codes enum

Because I had this problem and I could not find a list of MySQL error codes on the Internet, I’ve decided to implement my own MySQL error codes enum. It is based on MySQL 5.1.35 sources, include/mysqld_ername.h file.
Now I can do something like this:

[java]
catch (SQLException e) {
if(e.getErrorCode() == MySQLExceptionCode.ER_DUP_ENTRY.getErrorCode()){
r = Result.DUPLICATE_ACCOUNT_NO;
} else
r = Result.DATABASE_ERROR;
}
[/java]

You can find the java file here.

CentOS _dl_sysinfo_int80 deadlock

Are you using a CentOS or CentOS derived distribution and you experience deadlocks in dl_sysinfo_int80 function like this?:

#0 0x00ace7a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2
#1 0x00cb248b in __read_nocancel () from /lib/tls/libpthread.so.0
#2 0x080bdf39 in el_push ()
#3 0x080bdfda in el_getc ()
#4 0x080bde50 in el_push ()
#5 0x080be11b in el_gets ()
#6 0x080a6f59 in main ()

Then you need to upgrade the kernel to the lastest version from the CentOS repositories (2.6.9-42.0.10.EL as of today):

yum upgrade kernel

Have Fun!

Using libGD to manipulate jpeg images in C

Yesterday, for a small project of mine, I needed to implement a small utility to deform jpeg images from command line. The idea was to use texture mapping for deformations, so I needed a way to manipulate the jpeg images at the pixel level.

The first thing I tried was the imagemagik utility. I’ve browsed their documentation but I couldn’t find anything close to what I needed. I wanted to be able to draw textured mapped triangles into a picture. I knew that the Allegro library that I’ve used back in the old DOS programming days could do the texturing part. The bad thing that I found out is that it does not know how to handle jpeg files 🙁

Then I googled the Internet for a free and easy to use graphic library and I found libGD. The good thing is that it can handle jpeg files. The bad thing is that it cannot do the texturing part. The next thing I had in mind was to use both libraries but this would mean to add lots of dependencies to my small application for a simple texture mapping function. So I decided to write my own texture mapping function to remember the good old days. I will not describe how texture mapping works as there are plenty of tutorials on the internet for this.
Continue reading