Skip to main content

Posts

Showing posts from 2010

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 …

log4j.xml - A sample configuration

I could not post anything last month though all the time I was dying to post something. The reason behind this was I change in my profession profile. Actually, I changed my employer. All of the last month I was busy in official work and to settle down in my new office.
The last week of the year is normally a holiday in the new company. So, I could rest a while in my home and able to write the blog post.

Though I had a couple of other topics in mind but writing them will take more time which I don't want to spend during the festivities. So, I'll discuss about a simple but useful thing.
How many of us in Java sphere use log4j for logging? You know the answer. log4j is a very popular logging framework. Configuring log4j is quite simple. Still when you get the task of doing it, you try to find an example (I do the same). That is why I'll show a simple log4j configuration to print to a file.

There are 2 ways to configure log4j, log4j.properties and log4j.xml. I prefer the xml f…

Strings in Switch statements - a new JDK 7 feature

JDK 7 is the next release of Java language. There are lot of expectations from this release. For the first time Java is being modeled outside Sun Microsystems. Now JDK 7 has got a net host, openJdk community.

There are quite a few features planned in JDK 7. Some of them may prove to be a milestone for Java language. But my personal liking is a very small feature rather project called Project Coin. Coin serves to add small language features to Java which will make programmer's life easy. The JavaOne 2010 presentation for Project Coin lists the following features as accepted ones in JDK 7 time frame.


Binary literals and underscores in literalsStrings in switchVarargs warningsDiamondMulti-catch and more precise rethrowtry-with-resources (formerly known as Automatic Resource Management, ARM)
Out of these features, the most attractive to me was "Strings in switch". This is something I've longed for from my college days. I mean, though it was not Java (it was C) still I dre…

An unwise attack on the Singletons

Few days back I was disturbed with some maven build issues in office. I got back home pretty late. Already, as you can understand, mood was off. I thought of relaxing by watching a good movie but next day I had an interview scheduled. So, I had to brush up my skills a bit (I don't use them often in office :-) ). The first topic I started with was Singleton pattern. This one is so famous among interviewers that you better be able to explain it.
I had read the pattern many a times already. Nothing to prove the book wrong, never occurred to me. But that day, may be due the horrible mood, I challenged the pattern. I thought I'll find ways to break the pattern down. I wanted to create 2 Singleton instances in same JVM. Thats how the attack began.

Below is the code for creating a Singleton class

Here is the code to test the pattern

I have used VisualVM (comes as part of JDK 6) tool to check the no. of instances for Singleton class. You just need to pass a VM argument (-Dcom.sun.mana…

Generate POM files for a 3rd party artifact in Maven

Maven is a popular software project management tool. It is used heavily for building you project deliverables. More information about maven can be found here.

Maven works on the dependency concept. It essentially means that every project should identify its required artifacts. These artifacts can be found in repositories. Repository is a central place where you keep all your required jars (or other resources). There is a specific structure required to put your artifacts into the repository. We can not just copy every thing at same place. To identify a resource through dependency mentioned in pom.xml, maven looks for the specific structure. Also ther should be a .POM file at the same location of the artifact.

Normally the artifacts generated by maven build comes with the POM for it. So that, any other project wanting to use this artifact can do that. But if the artifact is created from a build other than maven (e.g. ant, Makefile etc), it will not have the POM generated. So this artifa…

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…

How to add new nonCacheable resource by inheriting Tomcat's DefaultServlet

Do you have a web resource in your application which is dynamically generated/modified? And do you use JavaScript/Ajax (i.e. you do not have the resource mapped to a servlet in your application web.xml) to read that resource? If both answers are yes, then you may have seen that the request for the resource does not return the updated file always. The reason is DefaultServlet class of Tomcat which implements a caching technique for resources. If there is a HTTP request to access the resource before 5 sec of the last modification time, DefaultServlet will send the file from its cache (older content).


DefaultServlet is a servet provided by Tomcat distribution (and JBoss too). As the name suggests, it is the default choice. If your web application does not map a web resource to some servlet, DefaultServlet serves that resource. Only exception is JSP pages. Check out DefaultServlet Javadoc and source code. The servlet mapping is defined in Tomcat web.xml which is normally present at $CATALI…

'Copy attachment from another email' feature in web based email clients

This is purely an experience driven idea. Looks a bit crazy right now. But should be possible to implement this one. Let me brief about the feature I am looking for in web based email clients. To mention a few web based email clients- Gmail, Yahoomail, Hotmail etc which you use from your browser.
Lot of times we send some documents via email. These are called attachments. If you are using a web based email client you have to upload the document to the mail server to attach them in email. The uploading definitely takes time in a slow connection or for a big size attachment. The act gets annoying for me if the same attachment has to be sent in 3 or 4 different emails. Just think of a situation, where I have 3 mails from 3 different contacts requesting for my CV. The easiest way is to reply all of them in a single mail attaching the CV. But what if I don't want to disclose to any contact that I am sending my CV to others as well. I am sure you have been in such a situation many a time…

Starting up

This is the first blog I'm creating and this is the first post. Feeling a bit nervy. Though its a great experience to write a blog. Initially I felt its not my stuff. But I follow other blogs and really like the whole idea of blogging.  Still somehow it took a long time for me to start on this space.  As they say, it is never late to start something new, I have started blogging.


This blog will be about tech topics that I occurs to me. I'll share my new ideas and hacks. Sometimes I may try to explain some common technology concepts in my own way. I do hope that it becomes useful for somebody.

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