Showing posts with label My Explorations. Show all posts
Showing posts with label My Explorations. Show all posts

Wednesday, February 11, 2009

Maintaining and Managing variables stored in application's session

In one of our project, we were storing frequently used application related data into the session, but now as the project is growing incrementally, storing new data / modifying existing data into session has became very confusing and difficult to manage.

I was just thinking about the workaround for coming out of this havoc, and voila, I got one :). This workaround I've implemented in our project. I would like to share the idea which brought us out of this mess.

Idea (Workaround): We can use one singleton java class, which has one HashMap variable. There will be custom methods for getting, setting and removing (operations same as that of session operations) values from HashMap. Now we can use this implementation for storing data (which was supposed to be stored into the session) into the HashMap present in this class, and then we can put the object of this java class (we'll be having one object for each session) into the session. So, at the session level, there will be only one object present in the session, and now it's lot easy to maintain and manage session variables (now HashMap data). Also, there will be only two session operations in each class, first: getting this java class object from session, second: storing back this java class object back to the session.

Implementing this thing was really fun and gave us lot of flexibility to maintain and manage session variables... :)

Saturday, January 3, 2009

Custom Map Implementation in Java

Blogging after a long-long-long-... time :)

When we fetch records from HashMap, it gives records according to hash code, i.e. the records returned are not in the order when they're placed in the Map.

When we fetch records from TreeMap, it gives records in the sorted order of key, i.e. the records returned are not in the order when they're placed in the Map.

What I want was a Map that should return sequential values/objects that are put into when they're fetched. So, why not to make our own custom implementation of Map that does what is needed.

I constructed following Custom Map Implementation. Please find the code below:

CustomMapImpl.java



===
Supporting Class: CustomEntrySetImpl.java



===

Output of above program:

key1 : value1
key2 : value2
key4 : value4
key5 : value5
key6 : value6
key7 : value7

---

It was a very great experience. I also digged into the actual Java Map source code which was an amazing experience.


Thursday, July 31, 2008

Updating the state of a Singleton Object after specific interval of time

I've created a Singleton Object which holds some values which are fetched from the database table. Following are the class specs:

1) All setter methods are private
2) All getter methods are public
3) One configuration method that sets all the values in the related attributes of class using their private setters

The need was to update the state of this singleton object when there are any changes made in the configuration table in the database. But to achieve this, I need to restart the server each time when any changes has been made in the configuration table.

So I created one WatchDog class in a thread that continuously changes the state of a singleton configuration object after a specific interval of time, & this time interval in-turn is fetched dynamically from the same configuration table from the database :)

Tuesday, July 22, 2008

Some experience with Log4j

I was doing some basic practice programs which implements Log4j for logging. I was getting following warning message on the console. Here I was using PropertyConfigurator to configure Log4j using the log4j.properties file.



I found the description about the above warning on many of the technical forums which gave a reason that this warning occurs when the log4j.properties file has not been found by the program in the specified path.

But if that was the case, I purposely tried giving wrong file path to the same program, and whoa I got the following exception:



Later I found that there was a small spelling mistake in my log4j.properties file, due to which it was failing to initialize the log4j system. So, when I corrected that mistake & tried to run the program, I got the expected output :)

Thursday, June 26, 2008

Deployed static web application on "NGINX" HTTP server

NGINX which stands for "ENGINE X" is a light weight HTTP server which serves static content (normal HTML pages) efficiently. I was doing some R'n'D from yesterday and was successful in deploying my first Static Web Application on nginx server. Really it was a great experience.

There is some sort of small configuration which is need to be done in "nginx.conf" to navigate between the static pages. You just need to understand the basics of these entries and there you're with the running application on nginx server :)

Some useful pointers:
http://nginx.net
http://www.kevinworthington.com

Stay tuned... :)

Thursday, October 25, 2007

Implementing Fluent Interface in Java

Yesterday my friend Neeraj came across the concept (?) called "Fluent Interface". He told that he successfully implemented it in Java. Today I read lot of articles on it, and finally I was too successful to implement it in Java using a very-very simple program.

About Fluent Interface:
Fluent Interface is used to create a readable model i.e. to convert APIs to English-like sentences. It is slightly harder to write but much easier to read.

"Martin Fowler coined the term "FluentInterface" to describe objects that expose an interface that flows, and is designed to be readable and concise. The cost of this fluency is additional effort required to design the interface for your object and the slight increase in complexity. These types of interfaces are often utilized to create configurations for your objects but can progress into an internal Domain Specific Language or DSL." [Excerpt from Randy Patterson's blog post ]

Some useful pointers:
http://www.martinfowler.com/bliki/FluentInterface.html
http://www.bofh.org.uk/articles/2005/12/21/fluent-interfaces

Thursday, July 26, 2007

Using regular expressions in JavaScript

Today I learnt to use Regular Expressions in JavaScript. Actually I haven't touched regular expressions from last two years. I used them two years before in Lex & Yacc programming which was solely based on REs.
It's very interesting & useful in JavaScript to make any type of validations like for float value, email address, etc. on the client side.
I tried to develop the RE for validating the float value which contains e/E in it. It was pretty interesting to see the RE coming up part by part. Finally the full fledged RE to validate float value was formed. I would like to share it:

var floatRegExp = /^([+\-])?\d*([\.])?\d*([eE]([+\-])?)?\d*$/;

Your view on the above RE is welcomed :)

Wednesday, July 11, 2007

Dynamic TABLE operations in HTML using JavaScript

Today I created one full fledged app using simple HTML, JavaScript & AJAX, which dynamically create, edit & delete the TABLE row (in the UI), which in turn is reflected in the database itself. While doing all these operations, no page REFRESH / RELOAD takes place, which gives feel that all these operations are done very faster than that of traditional approach.

As I've used JavaScript majorly, I've tested the app in IE(6.0), Mozilla Firefox(2.0.0.4) & Opera(9.20). It works fine without any browser compatibility issues. For AJAX, i've used JSON, which is supported by these browsers.

I think it's really great achievement for me as I've tried to create most user-friendly & interactive app.

Following is the snap of my app:
[All the operations takes place in the single page itself without refresh / reload]

Thursday, April 19, 2007

MyEclipse Enterprise Workbench starting error

From last two days, I was facing the problem while starting the MyEclipse Enterprise Workbench, by directly executing the eclipse.exe. It was giving the following error while starting:

Required Java version: 1.4.1. Available 1.3.1_01

So, I was starting the MyEclipse by using the startup.jar. But today I was able to resolve this problem by visiting some MyEclipse forums.

I done the following steps:
1. Right click the shortcut and go to Properties->Shortcut tab.
2. In the Target box, add the following in front of eclipse.exe as:
"eclipse.exe -vm [Java 1.4 or above installation directory]\bin\javaw.exe"

and the problem was solved :-)

Wednesday, March 21, 2007

Running more than one instance of Tomcat Server on the same machine

I came across one of the question in one of the Java forum on ITTOOLBOX which was like this:

Can we run more than one instance of the Tomcat server on the same machine?

I was trying this from two days, and finally I was successful in starting three Tomcat instances on the same machine with different port numbers.

I followed the following steps to do the same:
a. Made three different folders of Tomcat directory.

b. Made changes in the environment variables as CATALINA_HOME1, CATALINA_HOME2, and CATALINA_HOME3.

c. Then I reflected the above changes in the "startup.bat", "shutdown.bat" and "catalina.bat" batch files in each Tomcat "bin" folder accordingly.

d. Then I changed the default port numbers 8005, 8080, and 8009 in server.xml & server-minimal.xml files of second and third Tomcat directories.

e. Now first server was running at port 8080, second one at 8090, and the third one at 8095.

Conclusion:
We can either have the instances running on the same machine (different ports), different machines (same ports) or different machines (different ports).


_________________________________________________________________________________________________________________

"Look at the sky. We are not alone. The whole Universe is friendly to us and conspires only to give the best to those who dream and work."

- Dr. A P J Abdul Kalam
_________________________________________________________________________________________________________________