Wednesday, February 17, 2010

Left padding numbers

Again, a simple task: take a random unsigned integer up to 9999, and return it to the caller, but in a string which is always four characters (even though the random number may be one, two or three characters (like 2, 98, 112; I would be expecting 0002, 0098, 0112).

Took like ten minutes to find the right thing in the docs. But finally.

int randomNumber = randomGenerator.nextInt(9999);
System.out.println(String.format("%04d", randomNumber));


2213
0060
8492
8872
0064
5808
1552
0458
7856
8925

Friday, February 12, 2010

JAVA RIA

I need a Rich Internet Application framework. In Java, of course.
Thinking about either JavaFX. Or, perhaps Apache Pivot.

Wednesday, January 27, 2010

How to add more swap space on Linux

An easy method if you don't want to repartition the whole disk.

dd if=/dev/zero of=/tmp/swap.img bs=1M count=256

Set up a loop device and connect the dummy file with that device:

losetup /dev/loop3 /tmp/swap.img


Then, it's just formatting the loop device:

mkswap /dev/loop3


It also needs activation:

swapon /dev/loop3


Voilá, there is a new swap partition.

To remove it:

swapoff /dev/loop3
losetup -d /dev/loop3
rm /tmp/swap.img

Enable LAN access for Oracle XE admin web on Ubuntu

Edit your .bashrc file to include the lines:

ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server
PATH=$PATH:$ORACLE_HOME/bin
export ORACLE_HOME
export ORACLE_SID=XE
export PATH


Log out. Log in again.

Then start up sqlplus:

sqlplus sys as sysdba


And then just execute:

SQL> exec dbms_xdb.setListenerLocalAccess (l_access => FALSE);

Wednesday, January 20, 2010

Generating unique file names with SimpleDateFormat and Random

Sometimes it is necessary use an unique file name when writing a voice recording with Cisco IP IVR or Cisco Unified Contact Center Express (UCCX). The CCX Editor does support Java, so we can easily do this: let's just create a timestamp with the date and time including milliseconds, then just append a random number between 0 and 9999. Yes, I know there is still some chance this will generate the same string, but this isn't something I cannot live with.

First, create the timeStamp string using this code block:


{
String DATE_FORMAT="yyyyMMdd_HHmmss-SSS";
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
return sdf.format(new Date());
}


Then generate a random number with this code:


{
java.util.Random nahcis = new java.util.Random();
return nahcis.nextInt(9999).toString();
}


And in the end just concatenate:


"recording_" + timeStamp + "_" + randomNumber


This will create a string like:

"recording_20100120_134541-214_6592"


And this is something you can use as the file name.

Sunday, January 3, 2010

Servlets cannot look into their own install directory? Solution.

This applies to a Tomcat 5.5 installation running on a Ubuntu 8.04 server (and, all Ubuntu versions I know).

If you write a servlet that needs to read a file which is in the servlet's WEB-INF directory, for instance, a config.xml, Tomcat, or more precisely, the Java Security Manager will not let you.

(Interestingly, Tomcat that comes with CentOS, will do. Hmm.)

What I did is turning off the security manager in the /etc/init.d/tomcat5.5 file.

Task for the rest of the week: find out how to enable the required thing without turning off the Security Manager.

Saturday, January 2, 2010

Using apt behind a proxy in Ubuntu

It's really easy. All you have to do is:


http_proxy='http://someproxy:someport"
$export http_proxy


and voila, you can apt-get update, apt-get install, whatever.

Monday, November 30, 2009

JCIFS rocks!

Actually, JCIFS works, the way I excpected.

Look at this:

import java.io.*;
import jcifs.smb.*;
public class t_getfile {
    public static void main(String[] args) {
        jcifs.Config.setProperty("jcifs.smb.client.username", "administrator");
        jcifs.Config.setProperty("jcifs.smb.client.password", "password");     
        try {
        SmbFileInputStream in = new SmbFileInputStream("smb://10.112.27.31/c$/Program files/Avaya/IC71/MessageCenter/Messages/9698.msg");
        byte[] b = new byte[8192];
        int n;
        while(( n = in.read( b )) > 0 ) {
            System.out.write( b, 0, n );
        }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}


What I want to achieve: from a servlet, I want to connect to a Samba (Windows) "share" and read the contents of the file.

Update:

This is even cleaner, supports multiple authentication sessions:


import jcifs.smb.*;
import java.io.*;
public class t_getfile2 {
    public static void main(String[] args) {   
        /* NtlmPasswordAuthentication object from the userinfo
         * component of an SMB URL like "domain;user:pass".
         * This constructor is used internally be jCIFS
         * when parsing SMB URLs.
         */
        NtlmPasswordAuthentication auth =  new NtlmPasswordAuthentication("administrator:password");
        try {
        SmbFile file = new SmbFile( "smb://10.112.27.31/c$/Program files/Avaya/IC71/MessageCenter/Messages/9698.msg", auth );
        InputStream in = file.getInputStream();
        byte[] b = new byte[8192];
        int n;
        while(( n = in.read( b )) > 0 ) {
            System.out.write( b, 0, n );
        } //while
        } catch (Exception e) {
            e.printStackTrace();
        } //try..catch
    }
}

Thursday, November 26, 2009

My First Script in BIRT

Yeah, I got it finally. I added a piece of ECMAScript to a BIRT report. Actually, it just does one very simple thing: highlighting one row if the value of a column fulfills a condition.

Created a datasource (MySQL database on my computer) and a dataset. Business as usual, nothing fancy. The dataset is actually a table consisting of two columns, one is the primary key, the second is the value.
On the Layout tab, I clicked the value column's value row, and just added this code to the onRender method (the Help says: "Script executed when the element is prepared for rendering in the Presentation engine"):

var val = this.getValue();
if (val == "boo") {
this.getStyle().backgroundColor = "#AAAAAA";
}

And voilá:




This BIRT thing is not bad, after all.

Tuesday, November 24, 2009