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

Thursday, November 22, 2012

Now you know: Serifs and Fonts

Have you ever felt like you always knew something and turns out you didn't? Paul Harvey used to host a show called "Rest of the story" on the radio. He would take a familiar subject and give you a little known fact or story about the subject. If Paul was alive and decided to run such a show on technology, he would never run out of ideas for the show. There are several terminologies we take for granted on a daily basis. Some of them are real simple and others more complex. I will share my list of such stories here. If you know of any, please feel free to add them as comments below.

I've always taken HTML and CSS for granted. So, I decided to learn it in a proper way. Who better to learn than w3schools (W3 stands for WWW)? While I was going through their chapters, ended up on a topic about Fonts,  that caught my attention and this is the basis of my post today.

I use various fonts, like Arial, Times New Roman, MS-Sans-Serif etc. Didn't pay much attention to the names or how they looked. These Fonts vary in size and shape. Some of them are basic fonts and there are fancy fonts. And of course, there are various other characteristics of a font that make them look different from each other. One such Characteristic is a "Serif". According to Merriam-Webster Dictionary, Serif is actually a word (noun) that means the little edgers added to letters when we write them. Where as, San-Serif would mean the opposite, those fonts without Serif's!! (I don't know if they teach this in English classes in schools, but this is the first time I came across this).

font-typesThough the word is adopted for the computer fonts, the idea of a serif is not new. Apparently, like any western concept, it has origins in Latin and Roman scripts. But it is not unique to the west either. Chinese and Japanse characters have something similar too. See the wikipedia post for more interesting facts.


So next time, you pick a font, enlarge it and see if it has "serifs". As shown Times New Roman font does and Arial doesn't.


Apart from these two types, we also have Monospace fonts that have constant width for all characters. Courier New is of this type and is typically used by text editor and program (code) editors. "Courier New" uses Serifs to adjust the widths to be uniform. Check it out.


Now you know (the rest of the story)!


References:

http://www.w3schools.com/css/css_font.asp
http://en.wikipedia.org/wiki/Serif
http://www.merriam-webster.com/dictionary/serif
http://www.merriam-webster.com/dictionary/sans

Sunday, November 18, 2012

Powerbuilder and SAP

As I mentioned in my earlier post Sybase's merger with SAP seems to be complete. Since SAP took over, there was some uncertainty in the developer community about their interest in keeping the tool alive. From various posts online so far, SAP seems to committed to developing PB further.

If you haven't already noticed, Powerbuilder (PB) is definitely making a come back (among those who left it in the late 90's and the new developers trying to explore PB). I see lot more interest in the tool and many more blogs like this are coming up on the subject. This is almost like before Sybase took over in late 90's. While Sybase kept the product alive, there was a dormant period where the product lost community following. With SAP in the picture, there seems to be renewed interest in PB. They are constantly adding resources related to PB on their site.

http://www.youtube.com/user/saptechnology/videos?query=PowerBuilder
(Courtesy: Bruce Armstrong)

If you are new to PB, these videos may be helpful. I will be posting more links here later.

Thursday, November 15, 2012

Gotcha - JMeter: Response Data Display

This Gotcha is about the Load Testing tool called JMeter. JMeter is a real nice open source tool from Apache to load test Web Apps. You can set it up as a proxy and capture web pages and then customize the scripts in the GUI to build powerful Load testing scripts. I will be posting more about JMeter in separate post(s) later.

For those who are familiar with JMeter, this tip may be helpful. When you look at the results in a "View Results Tree" (See image below), the Response Data typically comes out as Text (HTML source). I was looking at these text HTML for a bit, baffled what to make out of it. I recently (accidentally) found out that you can change that. There is a drop-down below the tree (list) of pages visited. This is normally defaulted to "TEXT". If you change it to "HTML" or "HTML (download resources)", then bingo! the HTML page is displayed. "HTML(download resources)" downloads images etc, so the page looks much more like the original web page. There are other options like XML etc.

This choice (drop-down) is kind of away from the response view, so I didn't pay attention to it. Moral of the story is to click on every buttons and drop-downs on a GUI!! (or read the documentation fully!)

[caption id="attachment_616" align="alignright" width="1024"] JMeter Response View Gotcha[/caption]