Tuesday, April 21, 2009

Running an Inno Setup script using Ant

To use ant to run an .iss script, do this:
< target name="createExecutable" >
< exec dir="." executable="C:/Program Files/Inno Setup 5/Compil32.exe" >
< arg line="/cc 'Setup.iss'" / >
< / exec >
< / target >

Monday, April 20, 2009

Running Maven goals in Ant

To run maven goals using Ant, use this:

< target name="mavenCleanBuild" >

< exec dir="." executable="C:/Program Files/Apache Software Foundation/ApacheMaven2.0.8/bin/mvn.bat" >
< arg line="clean" / >
< arg line="install" / >
< / exec >

< / target >

Thursday, April 16, 2009

Bits for bites...


(Src: http://www.html4.com/mime/jpg/Will_code_HTML_for_food.jpg)

Creating and Calling a PL/SQL Function that sets the currval of a sequence

I came across this problem recently. I needed to update the current value of a sequence in existing databases. This is due to an erroneous current value set in the initial install.

My requirements were:
1. The patch should be able to detect whether or the current value of the sequence is the erroneous value. If it is, it should be updated to the new value.
2. Additions to the DB that incremented the current value should not be overriden with the new value.

Using the following function, this can be accomplished:

CREATE OR REPLACE FUNCTION schema_name.checkSeq() RETURNS integer AS
$BODY$
declare
v_CurrVal int4;
begin
select nextval('schema_name.sequence_name') into v_CurrVal;

IF v_CurrVal < 10 then
PERFORM setval('schema_name.sequence_name', 32, true);
v_CurrVal = 32;
RETURN v_CurrVal;
END IF;

PERFORM setval('redflex_express.seq_incidentproperties_id', v_CurrVal - 1, true);
v_CurrVal = v_CurrVal - 1;
RETURN v_CurrVal;

RETURN currval('redflex_express.seq_incidentproperties_id');
end;
$BODY$
LANGUAGE 'plpgsql' VOLATILE
COST 100;


This checks to gets the nextVal of the schema_name.sequence_name sequence and sets it to 32 if it is less than 10. Using currVal did not yield the required results. Hence, the nextVal is used and decremented by 1.

To call this function, use this:

select * from schema_name.checkSeq();

Wednesday, April 15, 2009

The Classic Problem of Java Ping

In previous versions of Java (before version 1.5), Java only supported the TCP and UDP protocols. It did not support ICMP protocol. Hence, there was no way of running the ping application from Java other than using native methods (JNI) or the Runtime.exec() to execute shell commands.

In Version 1.5, the InetAddress class had a method called isReachable() which tests whether the address is reachable (also see http://www.rgagnon.com/javadetails/java-0093.html for a good explanation):

try
{
InetAddress addr = InetAddress.getByName(hostName);
pingable = addr.isReachable(timeOut);
}
catch (Exception e)
{
System.out.println("Host " + hostName + " cannot be ping'ed. " +
"Please check to see if this computer is up and running.");
e.printStackTrace();
}

This method sends ICMP “echo request” packets and listens for ICMP “echo response” replies (essentially what the ping network tool does: http://en.wikipedia.org/wiki/Ping). However, as the API says, "firewalls and server configuration may block requests resulting in a unreachable status while some specific ports may be accessible". If this is the case, the method will then establish a TCP connection to Port 7 (which is an echo port on the server that echos whatever to write to that port). THis could also be blocked depending on your firewall settings. Worst comes to worst, you can still use the Runtime.exec() method to execute the ping shell command:

Boolean pingable;
try
{
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("ping.exe " + _hostNameOrIpAddress);

int exitVal = p.waitFor();
System.out.println("Process exitVal " + exitVal);
}
catch (Throwable t)
{
t.getMessage();
pingable = false;
}

This should do the trick. Make sure that if you use a _hostName, there is a mapping of that hostname and IP in your C:\Windows\system32\drivers\hosts file.

Wednesday, April 1, 2009

Setting Tomcat's JVM path in the registry

If you have, like me, installed Tomcat using its .exe installer (which displays a set of dialog boxes) rather than running the bin\service.bat install in a pre-configured Tomcat, there is a way to change Tomcat's JVM path.

As you all probably already know, Tomcat's GUI has a Java tab
Tomcat's JVM settings are stored in the registry. You can change the JVM path to the jvm.dll file in your newly installed JRE directory.

In my case, I needed to configure it using regedit. To do this, go to the following subkey in HKLM:
SOFTWARE\Apache Software Foundation\Procrun 2.0\Tomcat6\Parameters\Java\

Here, you can change the value data for the 'Jvm' value name.

Hope this helps!