Friday, June 30, 2006

Creating a branch in CVS and using it from Eclipse

Wanna create a branch in your CVS tree and use it from Eclipse for develpoment? Read on...
We had never created a branch in our CVS tree thus far, partly because there aren't very nice visual merge tools for CVS (like clearcase, for example). That makes it a bit tricky to merge from one branch to another - basically you gotta know what you are doing! :).
Anyhow, we reached a point in our development cycle that we had to create a branch for a release. After much googling, I found this concise article which was very helpful. Still, in short, what we did boils down to this:

Set up the branch in CVS:

  • cd your-project-directory

  • cvs tag -R -b tag-name


Checking out code from this branch:

For those using cvs from command line -

    >cvs checkout -r tag-name project-name


For those using Eclipse:

It's slightly more tricky than you'd think :). At first we were all worried since our branch was just not showing up in the CVS Tree. Here's how to get your branch to display there correctly.


  • Go to "CVS Repository Exploring" perspective and select your project from "HEAD"

  • Now right-click on this node and click on "Tag with Existing..."

  • In the dialog that pops up, click on "Refresh Tags". This forces eclipse to go fetch the latest branch and tag information.

  • Now click "Cancel" on the "Tag with Existing..." dialog.

  • Now in the "CVS Repositories" tab in eclipse, you should see your new branch under the "Branches" node (at the same level as "HEAD").

  • Find your module under this node, tagged with the branch you wanted. Then check out as usual by right-clicking on it and selecting "Check out as"





Disclaimer: I'm no expert in doing this stuff, but I've found a way that worked for me. If you know a better way, please post it here!

Thursday, June 22, 2006

xsl:fo dynamically insert external image.

We were trying to insert a dynamically generated image in our PDF document generated using apache FOP. Reading the documentation on their site it was clear that you can insert an external image by specifying the URL to that image. However, it wasn't quite clear how I could dynamically generate this image URL.
We needed this badly as we wanted to insert scanned signature images (which of course were not public URLs :).
The end goal in our case was to generate something like this:

<fo:external-graphic src="url('file:d:///images/toms_signature.jpg')"/>

when tom is logged in, and

<fo:external-graphic src="url('file:d:///images/teenas_signature.jpg')"/>

when teena is logged in.
After much googling, we found the solution. Simply put, you can extract your image URL from the XML. I'm assuming you know how to dynamically generate it in the XML (duh!). Here's how:

We changed the XML to generate a <signatureImage> element as follows:

<document>
.....
<signatureImage>url('file:d:///images/teenas_signature.jpg')</signatureImage>
.....
</document>

Then we added the following element in our XSL:FO in the correct place (the place in the document where the signature was to be displayed).

<xsl:template match="document">

.....

<fo:external-graphic src="{signatureImage}"/>

.....

</xsl:template match="document">



That's it! It works like a charm!!

Wednesday, June 21, 2006

Spring Validator "does not support command class" problem - and solution

If you are using Spring Validator (with XDoclet maybe) and are frustrated with an exception that looks like the following, read on. You are in luck!


org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'myController' defined in ServletContext resource [/WEB-INF/action-servlet.xml]:
Initialization of bean failed; nested exception is java.lang.IllegalArgumentException:
Validator [org.springmodules.commons.validator.DefaultBeanValidator@1be2de2] does not support command class [com.foo.model.MyClass]


This happens because you have not specified any validation requirements for com.foo.model.MyClass
If using XDoclet, the most common reason is specifying your validation tags on the getFooBar() method rather than the setFooBar() method of MyClass.


If you think you've done everything right, check the generated (or hand-edited) validation.xml file (typically deployed in the WEB-INF directory of your war file). It must contain a <form> stanza for MyClass with some fields to validate.

Cron job - quick & dirty

Here's how you set up a cron job on a linux machine:


  • Write the shell script you want to execute wherever you want. Say, /tmp/myCron.sh

  • Try executing this script manually from the console. Make sure it does what you need.

  • >sudo vi /etc/crontab
    Here I'm assuming that you have root access using sudo and you know that you ought to be careful with sudo etc.

  • Now in this file, you will see something like this:

    47 1 * * * root run-parts /etc/cron.daily

    This means : Run all scripts under the directory /etc/cron.daily every day at 01:47 (AM). See what your crontab file is set up to do. Here is the generic description of the format:





    00,30 12,13,14 1 4 3 <user> run-parts <dir>
    00,30 This is where you specify the minutes (0-59) We have
    chosen both :00 and :30 (right on the hour and
    half hour)

    12,13,14 These are the hours, it is in military time so 0-23
    (this example equals to 12pm, 1pm, and 2pm)

    1 The day of the month (1-31) This is of course the first
    day of the month.

    4 This is the month (1-12) April in this case

    3 This is the day of the week (0-6 with 0 being Sunday)
    Wednesday is being used in example.
    <user> Run the cron job as this user
    <dir> Run the cron job parts (scripts) from this dir


  • Based on the contents of your crontab file, copy your script in the right place. In my case it would be: >sudo cp /tmp/myCron.sh /etc/cron.daily/

  • Make sure the script is executable.
    >sudo chmod +x /etc/cron.daily/myCron.sh

  • That's it! Now to test your cron job, look at the system date as:

    >date
    Wed Jun 21 03:34:00 PDT 2006

    Assuming you got the above output for the system date, you can schedule the cron job (cron.daily) to run at the next minute as:
    34 3 * * * root run-parts /etc/cron.daily





That's of course, the quick & dirty way to get you going. If you need more information, just do man crontab or google it!

AJAX - unplugged

These days, everyone developing webapps seems to be doing AJAX. If you got bogged down by the hype around it (as I did at first), here's a posting from the web that I found very useful in cutting through the hype. This is really the core of AJAX, and like most successful technologies, it's very very easy to understand. I'm pasting below the contents of the original posting

I find a lot of this AJAX stuff a bit of a hype. Lots of people have
been using similar things long before it became "AJAX". And it really
isn't as complicated as a lot of people make it out to be. Here is a
simple example from one of my apps. First the Javascript:

function createRequestObject() {
var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP");
}else{
ro = new XMLHttpRequest();
}
return ro;
}

var http = createRequestObject();

function sndReq(action) {
http.open('get', 'rpc.php?action='+action);
http.onreadystatechange = handleResponse;
http.send(null);
}

function handleResponse() {
if(http.readyState == 4){
var response = http.responseText;
var update = new Array();

if(response.indexOf('|' != -1)) {
update = response.split('|');
document.getElementById(update[0]).innerHTML = update[1];
}
}
}

This creates a request object along with a send request and handle
response function. So to actually use it, you could include this js in
your page. Then to make one of these backend requests you would tie it
to something. Like an onclick event or a straight href like this:

[foo]

That means that when someone clicks on that link what actually happens
is that a backend request to rpc.php?action=foo will be sent.

In rpc.php you might have something like this:

switch($_REQUEST['action']) {
case 'foo':
/* do something */
echo "foo|foo done";
break;
...
}

Now, look at handleResponse. It parses the "foo|foo done" string and
splits it on the '|' and uses whatever is before the '|' as the dom
element id in your page and the part after as the new innerHTML of that
element. That means if you have a div tag like this in your page:




Once you click on that link, that will dynamically be changed to:


foo done


That's all there is to it. Everything else is just building on top of
this. Replacing my simple response "id|text" syntax with a richer XML
format and makine the request much more complicated as well. Before you
blindly install large "AJAX" libraries, have a go at rolling your own
functionality so you know exactly how it works and you only make it as
complicated as you need. Often you don't need much more than what I
have shown here.

Expanding this approach a bit to send multiple parameters in the
request, for example, would be really simple. Something like:

function sndReqArg(action,arg) {
http.open('get', 'rpc.php?action='+action+'&arg='+arg);
http.onreadystatechange = handleResponse;
http.send(null);
}

And your handleResponse can easily be expanded to do much more
interesting things than just replacing the contents of a div.

-Rasmus

Wednesday, June 14, 2006

Installing Mysql Server on Red Hat Linux (for the impatient)

I have a red-hat linux 9 server on which I needed to install MySQL 4.1.20. I had already tried using the rpm, and for whatever reason, that just didn't work. So I decided to go with the binary distribution, and boy, it was so much easier!

Here's what I did:


That's it. It was a heck of a lot simpler than I had thought after reading many articles :).
If you read this and are as lucky as I was - it shouldn't take more than 15 minutes to get it up and running!

This page is powered by Blogger. Isn't yours?