Skip to main content

Posts

Showing posts from September, 2011

Non capturing group in regular expression and it's use

Recently I have been stuck in a regular expression where I had to use grouping in places, values of which were not required. It won't make sense to explain the problem without the text. So here it is


Controller Id Connection Status Connection State Secure Role
------------- ----------------- ---------------- ------ ------
1             Connected         Active           No     Slave
25            Disconnected      Idle             No     Master


My goal was to get the numeric values at the beginning of the line while still matching the whole line for safety (so that any other numeric values in the output don't match). As you see in the output, each column can contain a value from a list of fixed values. e.g. Connection Status can only contain Connected or Disconnected. That demands the following regular expression -




But with the above expression, we are fetching all the columns when all we want is the first column. This is where the non capturing group of regular expression comes …

Setup VNC server in Ubuntu 10.04

I needed a system outside my company firewall to access access some internet based applications. I wanted to use a web browse from that. But all I had was, a cloud node running Ubuntu 10.04 with command line access to it. I installed VNC server in that to run firefox web browser. Steps are below -

sudo apt-get install xinitsudo apt-get install x11vncsudo xinit &x11vnc -safer -localhost -nopw -xkb -once -display :0 &Create a ssh connection from Putty. I have user ssh port forwarding feature to make the connection secure. After configuring normal host name, user id or ssh key etc. go to Connection > SSH > Tunnels section. Input 5902 for Source and localhost:5900 for Destination. Click Add.Now open this session from Putty.Download Tightvnc java client and unzip it to some directory.Run the jar file that was extracted in last step.In the new window, input 127.0.0.1 for Server and 5902 for port. It should launch another window showing Ubuntu system. Now you can install any GU…

Popular posts from this blog

Call an external program from MySQL trigger

MySQL is the choice of many when it comes to database. Its free and quite robust.
During one of our prototype implementations we had a requirement of calling some external processes when there is a change in a MySQL table. MySQL triggers are provided for the same purpose. They get executed when the table is changed in certain ways that is specified by the programmer. Now it is very easy (rather trivial) to do some thing in other MySQL database tables when trigger gets fired. But, our requirement was to call a C program.

Fortunately MySQL provides a way to implement your own functions, its called User Defined Functions (UDF). The "how to" is here. Now that we know, how to define your own functions and call them from MySQL events, we need to write our logic in a C program by following the interface provided by MySQL and we are done.

Wait a minute. That is already done by somebody. They have made a library of UDFs. One of them, LIB_MYSQLUDF_SYS does exactly what we want. It al…

Using Multi threading with Drools

Drools is a very popular rule engine. It is based on Rete algorithm. To understand what is a rule engine and how to use it check here. But here I will assume that you have some working knowledge on Drools.

What I am going to discuss in this post is 'how to use multi-threading with Drools'. Below is a snippet which creates a KnowledgeBase from a DRL file.


If this method is called from multiple threads I get below exception -



Exception in thread "Thread-60" [Error: incomplete statement: (possible use of reserved keyword as identifier: )]
[Near : {... globals != empt ....}]
^
[Line: 0, Column: 0]
at org.mvel2.MVELInterpretedRuntime.parseAndExecuteInterpreted(* MVELInterpretedRuntime.java:153*)
at org.mvel2.MVELInterpretedRuntime.parse(* MVELInterpretedRuntime.java:44*)
at org.mvel2.MVEL.eval(*MVEL.java:514*)
at org.mvel2.templates.res.IfNode.eval(*IfNode.java:61*)
at org.mvel2.templates.res.TextNode.eval(*TextNode.java:46*)
at org.mvel2.templates.res.TerminalN…

Get JSON data response with JQuery Form plugin for a file upload form

My latest project involves JQuery for UI development. Frankly speaking, I did not have much idea of JQuery before. But I found it very easy to understand. All APIs are well documented with lots of examples available on Internet. The best feature of JQuery is its plugins. There are thousands of plugins available to play with. For every need of my web app I could find a plugin.

In my web app I am using JQuery Form plugin to submit the forms through AJAX. The return from my servlet is JSON object. The form plugin works great for all my forms. Well almost all, there is a HTML form which has a file upload box along with other input fields. This form was not able to get the response in JSON format. Though it worked in Google Chrome, but Firefox was presenting me with a file save dialog box.
I did a lot of googling to find out whats going on. Finally I observed that the problem is with HTTP headers that the form submit request has. Here are the headers captured by Chrome



As you can see the