Technical Tips, Tricks and Articles

Archive for the ‘programming’ Category

Quickstart – log4j

leave a comment »

There are like 100 of log4j tutorials on the internet. I am not going to put another one, just a compilation of few resources which are good to go and will add couple of notes, which I wasn’t able to find online or they took forever.

This particular link is pretty decent. It talks about basics, MulitpleAppenders, FileAppenders and then log4j xml configurations.

One of the thing that is missed in above tutorial is configuring your XML based configurations for specific class hierarchy. Lets talk about vaanila.admin and vaanila.report class heirarchy mentioned in FileAppenders  example. In given example, following piece of lines were handling specific logging based on classes in .properties file

log4j.logger.com.vaannila.admin=WARN,AdminFileAppender
log4j.logger.com.vaannila.report=DEBUG,ReportFileAppender

What would be their equvialent in XML ?

01 <logger name="vaanila.admin">
02 	<level value="WARN"/>
03 	<appender-ref ref="AdminFileAppender"/>
04 </logger>

and similarly, for vaanila.report the configuration would be

01 <logger name="vaanila.report">
02 	<level value="WARN"/>
03 	<appender-ref ref="ReportFileAppender"/>
04 </logger>

One of the thing, that these tutorials didn’t help me was how can I log specific logging based on application modules i.e. if I have multiple .ear files, then how can I separate the loggings for each .ear file? Definitely, the class level logging wouldn’t help me because there could be a scenario where there are “shared” classes. The solution to this question is log4j version 3.2.4 and higher. Using that log4j you can use following configuration to log based on ear files.

Assuming I have an application and one of the ear module is named as some.ear. The settings would look like something

01 <appender name="some" class="org.apache.log4j.FileAppender">
02 	<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"></errorHandler>
03 	<param name="Append" value="false"/>
04 	<param name="File" value="./log/some.log"/>
05 	<layout class="org.apache.log4j.PatternLayout">
06 		<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
07 	</layout>
08 	<filter class="org.jboss.logging.filter.TCLFilter">
09 		<param name="AcceptOnMatch" value="true"/>
10 		<param name="DeployURL" value="some.ear"/>
11 	</filter>
12 </appender>
13 
14 ...
15 
16 <root>
17 	<appender-ref ref="CONSOLE"></appender-ref>
18 	<appender-ref ref="FILE"></appender-ref>
19 	<appender-ref ref="some"></appender-ref>
20 </root>

I hope that will be helpful.

Written by ..alee

April 29, 2011 at 4:12 pm

Comparing Wrapper objects values in range –128 to 127

leave a comment »

While debugging through a bug that I encountered in one of my ongoing project. I came to know a strange behavior of Java. Consider the following code segment

1 Integer a = 10;
2 Integer b = 10;
3 
4 if (a == b){
5 	System.out.println("Two values are equal");
6 	}

If you look closely at line no 4, then you would realize that the code is actually comparing “Java objects”. Since “a” and “b” are not wrapper classes not primitive types.  But even then the condition is being true. Ok, some how this makes sense (at least till now unless you read this code segment)

1 Integer a = 150;
2 Integer b = 150;
3 
4 if (a == b){
5 	System.out.println("Two values are equal");
6 	}

Astonishingly, for the above code, the if statement would not execute true. Why?

Well, ever since Java has introduced “auto-boxing” feature, you’ll face this issue. Why is it so? Actually, compiler try to optimize things as much it can. For this, Java has a pool of values from –128 to 127. Whenever, there is some auto-boxing between these values, the reference object for the wrapper class will get the same memory location. In both cases, we are comparing “object references” but in first case, since the value was in the range for both objects hence, Java assigned the same reference location to both objects, making the condition “true” on comparing memory location for two objects.

Now, the question is why between –128 to 127?. Well, the smallest range of values for smallest wrapper class is between –128 to 127 i.e. Byte.

Written by ..alee

July 5, 2009 at 1:06 pm

Posted in code, interview, java, programming

Tagged with , ,

Error starting eclipse

with 4 comments

You might face an error while starting eclipse on a freshly installed system, or on the system which has undergone a fresh upgrade. The error message will be like

—————————
Eclipse
—————————
JVM terminated. Exit code=-1
-Dosgi.requiredJavaVersion=1.5
-Xms40m
-Xmx512m
-XX:MaxPermSize=256M
-Djava.class.path=D:\eclipse-jee-ganymede-win32\eclipse\plugins\org.eclipse.equinox.launcher_1.0.100.v20080509-1800.jar
-os win32
-ws win32
-arch x86
-showsplash D:\eclipse-jee-ganymede-win32\eclipse\\plugins\org.eclipse.platform_3.3.100.v200806172000\splash.bmp
-launcher D:\eclipse-jee-ganymede-win32\eclipse\eclipse.exe
-name Eclipse
–launcher.library D:\eclipse-jee-ganymede-win32\eclipse\plugins\org.eclipse.equinox.launcher.win32.win32.x86_1.0.100.v20080509-1800\eclipse_1114.dll
-startup D:\eclipse-jee-ganymede-win32\eclipse\plugins\org.eclipse.equinox.launcher_1.0.100.v20080509-1800.jar
-framework plugins\org.eclipse.osgi_3.4.0.v20080605-1900.jar
-vm C:\Program Files\Java\jre1.6.0\bin\client\jvm.dll
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Xms40m
-Xmx512m
-XX:MaxPermSize=256M
-Djava.class.path=D:\eclipse-jee-ganymede-win32\eclipse\plugins\org.eclipse.equinox.launcher_1.0.100.v20080509-1800.jar
—————————
OK
—————————

Actually, eclipse is trying to get the version of java that is installed on system. you might have installed that latest one but there are possibilities that any upgradation of later softwares has replaced the java.exe files in system32 folder with older version. All you need to do is to run eclipse with clean parameters. Create a batch file in the directory where eclipse.exe is and write this line in it eclipse -clean -vmargs -Xmx256m

Written by ..alee

January 30, 2009 at 4:11 pm

Posted in java

Let’s get Familiar with Delphi Files

leave a comment »

Before starting writing something in the code window, it’ll be great to get some familiarity with Delphi file types. If you create a delphi project you’ll see files with different extensions. Here are their explanations.

  • .PAS – These are the main files in which you’ll write most of your code. The extension comes from the first three letters of the name of Delphi Ancestor "Pascal".

  • .DFM – These are the files in which your GUI based information is saved i.e; any form that you’ll create will have 2 files, 1 .dfm and other one .pas. the .dfm file will contain the information about controls, where those has been placed, their link and the .pas file will contain the code, you’ll wrote.

  • .DCU – Some sort of compiler specific files for debugging etc. Will be created each time you compile your project.

Written by ..alee

April 30, 2007 at 4:31 pm

Posted in Delphi, programming

Tagged with ,

Why Choose Delphi

leave a comment »

That was the very first thing that came in my mind when i started working on delphi. We have VB6, .NET, Java to make Desktop GUI then why Delphi?

I guess this will be the same question (if you are new to Delphi) thinking; why delphi?

The application that we were creating is for "all" users. So we can expect that a user may be having very good system or may be he is still on P-II or P-III. So, deploying an application which needs some extra SDK (.NET, Java) of some XXMB to run an application of size not more than 5MB is a hassle.

So, why we should not we choose VB6. Well, Microsoft is no longer providing support to it’s VB6 and descendant languages. The only focus of MS is now .NET platform. So choosing VB6 for a professional environment will be quite risky. Though i admit that there are enough things available on internet that can provide us enough support, but still the risk factor is there.

Hence, came the option to choose for Delphi. A language that was design to compete the increasing market of VB 6 for Windows platform and then continue its success towards Desktop application (Windows and *NIX). The language was initially designed/architect by Anders Hejlsberg. One of the architect of C# language.

Delphi, besides having all the necessary things required by a good programming language, is also full of enormous support of Windows Platform by using it’s ShellAPI. In simple words, if there is anything on your windows system accessible or usable by Visual C, it is achievable in Delphi as well. Your system level message, events, dlls, ActiveX, whatever that you can ever use in Visual C to make a good application is available here.

Written by ..alee

April 11, 2007 at 4:22 pm

Posted in Delphi, programming

Tagged with ,

A C Code, getting a string using gets();

leave a comment »

Hi,
last night one of my friend showed me a C code and told me that he was unable to get the string input using gets(); I am posting the code here.

char temp[50];
printf("Please enter your name: ");
gets(temp);
printf("%s", temp);

Now, he was very confused that why this simple code is not working. I asked him to put fflush() before calling to gets(). He did it and the code worked fine. Do you get the reason?

The printf does not flush the memory and when you call the gets() it use the buffer characters already in memory buffer thus returning empty string.

Written by ..alee

January 11, 2007 at 4:01 pm

Posted in C, code, programming

Tagged with , ,

Interview Questions (.NET)

leave a comment »

  1. What’s the difference of using System.Text.StringBuilder over System.String?
    Answer: System.String is immutable. System.StringBuilder was designed with the
    purpose of having a mutable string where a variety of operations can be performed.

  2. What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?
    Answer:
    The Clone() method returns a new array (a shallow copy) object containing all the elements in the original array. The CopyTo() method copies the elements
    into another existing array. Both perform a shallow copy. A shallow copy means the contents (each array element) contains references to the same object as the elements in the original array. A deep copy (which neither of these methods performs) would create a new instance of each element’s object, resulting in a different, yet identical object.

  3. How can you sort the elements of the array in descending order?
    Answer:
    By calling Sort () and then Reverse () methods.
  4. What’s the .NET collection class that allows an element to be accessed using a unique key?
    Answer:
    Hashtable.

Written by ..alee

December 19, 2006 at 3:54 pm