Monday, December 24, 2012

Merry Christmas!

6d605d6a3345ad0edae720f424704a91

May the Christmas Cheer spread around the world and bring peace, health and wealth to everychristmas_pinguinone!

I just found this nice site to create fancy fonts!

Sunday, December 23, 2012

Software State of affairs - Maintaining OO

Last week, a friend at work was analysing code in Powerbuilder and question came up about function Overloading and we branched into Object Orientation in general. That question got me thinking. Though Object oriented programming has been around for a while, the concepts of OO are not completely understood or followed.

I posted about OO concepts here earlier. To do that I bought and read several books, sifted through the web a lot. There were so much confusion and conflicting ideas. Most of those books use obvious objects like animals, people etc in explaining OO concepts that what we learn in those is not enough to translate in to real life OO programming. I mentioned about some of these problems in my earlier post. More and more languages are adding OO concepts, yet people who are used to traditional (procedural) way of programming, struggle to adapt to it or even completely reject it. Often times, Syntax comes in the way of understanding. For e.g., Java borrowed so much from C, C++, that it's often considered to be just enhanced C, thus OO nature of it is completely ignored. And even if these developers switch to OO eventually, the objects they come up with are just repackaged C code, thus perpetuating the problem!

The application I currently support was written in Powerbuilder using a framework called EAF. The software also has business framework that is built around this, definitely a good extensible architecture. Yet, the software has degenerated over the years. Part of the problem is lack of understanding of the framework and/or OO concepts in general. I remember, when I started here, this framework was considered to be the main reason for all evil in the software! There was not enough documentation about proper framework usage, thus it was easy to blame it!

When new developers came in, they wrote and rewrote functions and objects that completely ignored underlying framework thus code is bloated. Further, supporting business has been utmost priority, so such technical issues were completely ignored. I feel, every maintenance project should include the maintenance of the software itself - in addressing technical issues and retuning it to keep it running good, like we do with our cars. After all, business will eventually be affected, if we didn't take care of such technical issues in time! The very first problem I solved in this software was a technical issue - there was a memory leak (due to improper handling of Object pooling) in the software that kept crashing the server during peak usage in the prior years. Business of collecting taxes, was indeed affected during those crashes. And I cannot imagine how much time, effort and resources were wasted during those times.

Another problem I see in software maintenance, is the tendency to solve code smells and code bloats by adding more resources - more servers, more disk space, more memory. When a car runs sluggish, we can't solve it by just adding more engines or more tires, can we? Why is it OK with software? Prior solution to the memory leak problem I mentioned above, was to add more servers and a script to automate the Server Restarts! Once the memory leak was fixed, no more frequent crashes!!!

To curb some of the issues in the application, I recommended and started on a project to re-factor some of the code in the application earlier this year. Main goal was to improve performance in our web application, but I wanted to clean up and reintroduce some of the framework methodologies.  Though there was some push back initially, other developers started buying into the idea. Yet, I see the reluctance to change existing ways; after all the software works during normal (low?) usage and business is OK (if not happy), why change those? When I first introduced log4J (logging libraries for Java) for logging in our JSP pages, it took the team at least couple of months to buy into the idea, in spite of me showing stats to support the change.

Our application architecture uses EA Server (EAS) as the middle tier. EAS supports Java and Powerbuilder objects naturally.While Powerbuilder is great for developing applications quickly, Java was used for any multi-threaded activities such as caching and logging etc. We even have Java Stored procedures running inside Oracle database! I recently introduced tools like Eclipse, Ant etc. to streamline Java development activities. With such exposure and addition of new Java packages prompted a team member to say to the effect "we are becoming a Java shop whether we like it or not". I've tried to explain in vain that it was all there before and why we even need them!!

Part of the problem is also lack of career planning and/or training. I remember the days when companies invested in training employees to keep up with the industry trends. Now, we seem to be spending more in buying those extra servers to support more memory leaks :)

Tuesday, December 11, 2012

Tools: Web Statistics Tools - Analog

We have a web site used by the tax payers to file their annual taxes. This is used by 100s of thousands of users within a short period during peak season (Feb - Mar) and reasonably used other times. During the peak times, it almost explodes.

I have been working on improving performance of this site. Also, there have been no stats whatsoever to be able to confidently say how many users were served successfully or when it failed where the bottlenecks are. We relied on information stored in the Database to "guess" these stats. I am adding a bunch of tools and (Java) programs to get more stats as well.

I am currently doing load testing on a test version of this site using Apache JMeter. While this tool is great and comes with a suite of reports, and I have added a tons of stats on the server side (an old Sybase EA Server) I needed something outside of these two. So, I started looking at the HTTP Access logs. EA Server includes a light weight Apache server at the core. So, the format of the log file is almost identical to a Apache Server Access logs. (On EA Server the file is called JaguarHttpRequest.log).

As usual, I looked around for tools that will help with analyzing these logs (when the computer can do the work, why do it manually?!). While searching for the tools, initially I looked for Apache log parser. I did find a few, but nothing great. (I found one PHP program though, which is kind of interesting from programming point of view). Eventually, I landed in Web Analytics Software and our good old Wiki (here) came to the rescue. I tried a few software listed there (Webalizer, AWStats) look interesting.

Finally, I tried Analog. Bingo!! This is what I have been looking for. Analog is a very simple tool to install and use. (Installation is just unzipping the zip file). Using the tool is as simple as Double clicking or running from command line, the analog.exe file.

This actually produces a (sample) report with the name Report.html. After reading more on Analog web site, I was able to customize the config file named analog.cfg. Once I tweaked this file, I started parsing the log files in minutes. I was even able to append date to the report file name and thus was able to run it in a loop to capture the stats every few minutes.



Apart from the Summary page, it also has several graphs to show stats for Hourly, Daily, Weekly, Monthly etc.

If you are looking to analyse usage patterns on your web site, this is a great tool. Check it out!

Friday, December 7, 2012

Quick Tip: How to exit from SQL*Plus on command line

This is about running SQL*Plus in a batch mode. Suppose you have a script (shell or batch file) that calls SQL*Plus to run a SQL in a batch mode. I am talking about running it from Command line, thus:

$ sqlplus <user_id/password>@SID @<sql_file_name)

Chances, you will want to exit SQL*Plus as soon as the script is done (EOF file is reached in SQL file), so we can continue our processing.

Typically, we add an EXIT statement at the end of the SQL file itself and this will force SQL*Plus to quit. What if you forgot or couldn't add EXIT (you use the same script in different scenarios). If you don't have an EXIT or QUIT statement at the end of your SQL file, you will end up seeing the SQL Prompt:

SQL>


Here is a quick tip to exit Sql*Plus after it's done with the script:

exit | sqlplus <user_id/password>@SID @<sql_file_name)

(That's it. Essentially piping exit into sqlplus command! When the End of file is reached, SQL*Plus returns to the shell and your shell script can go on!

This tip works on both DOS (Windows command prompt)_ and *nix systems.

Saturday, December 1, 2012

Gotcha: JSP includes

This is related to my earlier post. When we fixed the issue in Servlet Filter in the JSP Web Application we were troubleshooting it went past the first page. We landed on a blank page this time!! We were testing in a mirror site that I had created. According to the developer, the original site work and the new mirror site was having this issue.

To cut the long story short, this was caused by a change developer added to a JSP file to invalidate session. This file was included in the main JSP page that was erroring out. After some digging, I found out that the original JSP wasn't compiled in a while (See below). Bingo! I made it to recompile, and the original site also failed at the same place. This proved the invalidate() added to be culprit.

JSPs are just short hand notation for Java Servlet. It lets the developer/page designer to code the page using HTML like tags. The first time the user accesses the JSP file, the Servlet container automatically generates and recompiles a Java Servlet for the JSP file. After the first time, this step of generate/compile is skipped. In development we could make the Server to recompile each time the JSP file is changed. This works fine. The problem in our case was that an included file was changed and since the main JSP file itself did not change, the Server didn't bother to regenerate the Servlet class.

The lesson learned is not to believe what you see or not see on a JSP page. Always make sure your JSP is compiled up-to-date. To do this, you could use the compiler that comes with the Server (Servlet container) software. Most Servers (for e.g., Apache Tomcat, IBM Websphere, Sybase EA Server) come with a command line script called jspc. Another approach is to clear the your Server's work directory. See here for a discussion of this.

Gotcha: Java Concatenation

The Problem

Recently one of my co-workers had an issue with a website written in JSP. Every time he went to a page, a Servlet Filter was failing with an error that the Java class "StringBuilder" was not found. I've added this Servlet Filter in the application recently and this worked fine on other machines and in our Test Servers, but failed on this one machine. Also, we use Java 1.4 and there is no way we could be using Stringbuilder which was only introduced in Java 1.5.  (Java 1.4 has only StringBuffer. See here for a discussion on the two).

(I know we are using older technology - we use Sybase EA Server 5.5, which forces us to stick with Java 1.4, but go with me on this.)

Java Strings and Java Compiler

I set to find our why it was complaining about something we don't use. I looked at the class file generated. It had "StringBuilder". Then it hit me. We use a lot of String Concatenation in our code and we use String concatenation using plus sign (+). This seemingly innocuous usage caused it and the error has to do with Compiler Optimization.

Java Strings are immutable, meaning they cannot be changed at run time. Each time we concatenate Strings, we are essentially creating newer and newer Strings.

So, str1 + str2 + str3 is essentially equivalent to

res1 = str1 + str2

res2 = res1 + str3

But Compiler may take a different approach to optimize concatenation using StringBuffer or StringBuilder (See here). So the above, concatenation can be rewritten (by compiler) as,

StringBuilder(str1).append(str2).append(str3);

So this is what happened in our case. Some concatenation got turned into StringBuilder by the Compiler and hence the error. Wasn't that supposed to read StringBuffer (because it's supposed to be Java 1.4)? Well that was because of another mistake. When he set up Eclipse our friend used a newer version than the one I recommended which defaulted to Java 1.6 compiler which caused the optimizer to produce > 1.5 code!! Hence the error on StringBuilder. But the error showed up in the first place because of the Compiler Optimization! If the compiler used the String.Concatenate in the first place, we would not have seen this error!!.

And Compiler may sometimes choose to do just that, use String.Concatenate as shown in preciseJava.com. Also, such Compiler optimization may not be always good. See this interesting blog about various possibilities of concatenations.

Oh yeah, We overrode the workspace settings in Eclipse to use 1.4 compiler for the project and all is well now.

Reference Links

  1. http://chaoticjava.com/posts/stringbuilder-vs-string/

  2. http://javamoods.blogspot.com/2010/02/optimization-dont-do-it-compiler-will.html

  3. http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=5&ved=0CF4QFjAE&url=http%3A%2F%2Fciteseerx.ist.psu.edu%2Fviewdoc%2Fdownload%3Fdoi%3D10.1.1.122.5641%26rep%3Drep1%26type%3Dpdf&ei=LIK6UMCwBMWUjAK4_4HIBw&usg=AFQjCNFo6trLwU4wlxgrTNFBa1tGb1p9Cg&sig2=qHQIg8S0qI-CXdJ6EOaa8w

  4. http://www.precisejava.com/javaperf/j2se/StringAndStringBuffer.htm

  5. http://code-thrill.blogspot.com/2012/08/stringbuilder-optimizations-demystified.html

Java: What's up with "Impl" in Class Names?

What to name it?

This is a constant question we are faced with in daily life. We want to come up with perfect names for our babies. We want to name our animals (pets) and they seem to be happy to have a name! And it seems to be a big thing! Otherwise we wouldn't have naming ceremonies like these. We try to name (rename) our buildings (otherwise what would  you call Empire State Building?), streets, cities and countries. (Myanmar or Mumbai is yesterday's story. Mexico wants to rename themselves as Mexico! See here.) And naming is not an easy thing to do. A world recognized music composer in India, Illayaraja, called his symphonic music album "How to Name it" for lack of a better name.

What's that got to do with this?

Java developers (or any developer for that matter) are merely imitating the real world and they too have to face such challenges while naming their programs, classes and objects. We don't want to name (can't) two entities same name. That would cause confusion (Name Collision). To avoid this, we use packages, name spaces, domain etc.

Each "object" tends to have 2 forms in Java world (or .Net, C++ etc) - the class and it's interface! An interface is a class's "public profile" - much like public profile we have in linked-in, what we want other classes to know about this class.

We could say the interface is not unlike a business card or a cover letter we send with our resume when we apply for a job. Cover letter contains exact amount of information that may be of interest to the other party while the resume may have lot more inner details. Now they both are about you, but not the same.

So, what's up with "Impl"?

So, if you are a developer, what would you call the interface and implementation for an object that models a real car?

[caption id="" align="alignright" width="356"]OO interface names OO interface names[/caption]

This is not as simple as it seems. There are at least 2 camps. One camp, the Microsoft based camp calls the (implementation) classes by just the name and prefixes their interfaces with "I". To them the Implementation is the real object and interface is it's business card. The other camp (seemingly started by IBM) uses the plain name for interfaces and the (implementation) classes are named with a suffix "Impl" for Implementation. Here the interface represents the abstracted real word object and the Implementation class merely has extra (inner) details that doesn't concern outsiders (other classes). The Java world mostly follows the "Impl" convention.

So there you have it, when you see Java classes named with "Impl" at the end, you know why. End of story, right? No, not really. Object Oriented pundits and purists don't agree. Here is a nice post where the author diisagrees with "Impl" usage. And here is another perspective to it. And it's an ongoing debate. Don't believe me? See here, here, here.

So which way to go?

Naming your class can be a personal choice, but most of us write programs that will be shared with others in the community. That's why conventions and best practices exist. If you are working on a project, set up and follow standards appropriate to your shop.

In some cases, you may not be able to change the "I" or "Impl" in names. Many programs may have some type of interface (protocol) like CORBA, COM, Web Services. In these cases, they generate code (Stubs, Skeletons etc) that you have to based your own code on. Document these and stick to those conventions to avoid any surprises.

Personally, I am OK with using Impl suffix. I work with EAServer which runs both Powerbuilder and Java objects. These objects "talk" to each other using CORBA IIOP. Both Powerbuilder and EAServer tools generate CORBA IDL files which in turn are used to generate stubs and skeletons in Java (or C++). Developers then write Implementation classes for the interfaces generated. The class files generated and the impl classes may end up in the same Classpath and to avoid Name Collision, we have to name implementation classes differently and the convention is to use Impl suffix. This is a convention I am sticking with in my current project.

Reference Links

A note about my "cheesy" cartoon there. While searching for something relevant I found Toondoo where you can make your own cartoon. You can start with a free account. I also use openclipart for some of the pictures in my other posts.

http://www.toondoo.com

http://openclipart.org

http://isagoksu.com/2009/development/java/naming-the-java-implementation-classes/

http://docs.oracle.com/cd/E13183_01/en/alui/devdoc/docs6x/aluidevguide/ref_idk_deploymentimplkeys.html

http://leshazlewood.com/2009/03/03/java-class-naming-conventions/

http://www.gnu.org/software/libc/manual/html_node/Interface-Naming.html

http://osherove.com/blog/2006/6/14/interface-naming-anything-but-javas-standard-please.html