Power to Build

Home » Programming » Java

Category Archives: Java

Tools: UMLet – Great tool for drawing UML diagrams


UMLet is an open source UML diagramming tool with a twist! I’ve used many UML tooumlet_logo_smallls in the past, but UMLet is different.

It’s a diagramming tool for the diagram-challenged in us!! You simply type text and it will convert to UML boxes and arrows immediately. It can create simple UML diagrams, Use cases, Activity diagrams and sequence diagrams. I use it to produce some non-UML diagrams as well.

It has a pallet of UML elements already available which you can double click or drag and drop onto your diagram. You will a properties box on the right, where you can start typing – rename, add arrows, lines etc. Yes, you can change the already present elements in the pallet as well!

I particularly like the fact that you can add custom elements to the already rich set of drawing elements. While creating a drawing element, you add Java code snippets to create shapes to your custom elements. Really powerful! OH yeah, it compiles the Java snippets on the fly!!!

Here is a really nice presentation on Prezi to get you started. It has an embedded video as well. Short and sweet.

 

If you want to learn more about the tool, you can always look at the Source code on github.

The diagrams are stored in UXF (UML Exchange format) format. It is really an XML file with specific format used by the tool. I’ve tried and succeeded in generating this file for some simple diagrams. It is available in various flavors – as a standalone (Java) executable or as a plugin in various IDE including Eclipse.

As a tool, it is intriguing – from the simplicity of use to the versatility of adding to new elements. Complex Diagrams can be produced quickly. I love it. Give it a try! You can download the tool here.

Have you heard of CAFEBABE, DEADBEEF, DOCFILE0?


Have you heard of a magic word CAFE BABE? They are the magic numbers of the Java class files. See below link for the interesting story behind it:

https://dzone.com/articles/the-magic-word-in-java-cafebabe

And the words, CAFE and BABE both actually make up the below hexadecimal numbers:

(CAFEBABE)16 = (3405691582)10

If you wonder why a magical word is needed, it’s a long practice in identifying files. See the wiki postwiki post about this.

DEAD BEEF (actually an hexa decimal number) is the magic word used by Texas Instrument in MAC Addresses.

Oh, the other word in my title, DOCFILE0 is actually a pseudo word = D0CF11E0. That is magic word used in the header of Windows EXE files. See here for more on that.

Sybase EAServer: Taking Control – Changing JVM in Jaguar Manager


EA Server 5.5 comes with a manager application called Jaguar Manager (also called Sybase Central). This is really a Java product, but in this version it’s wrapped in a program called scjview.exe. This is available in %SYBASE%\Shared\Sybase Central 4.3\win32.

I found out this is using an old JRE 1.4. (I use process explorer in Sysinternals. It’s a great tool!). I wanted to switch the EA Server to Java 1.5 , which is the latest version of Java supported in EAS 5.5. With the EA Server itself, this is easy. Just pass -jdk15 to the serverstart batch file.

With Jaguar Manager, this is not as straightforward. There is a batch file called, jagmgr.bat, but this only calls scjview.exe mentioned above. The Java runtime is probably picked up using a DLL named jsyblib142.dll, in %SYBASE%\Shared\win32.

After googling for a bit, I found a solution to this problem. Apparently, scjview.exe has a command line argument -batch.

 

scjview -batch

When you run it like that, the program generates a batch file called sybasecentral.bat in the same directory as scjview.

Now, this batch file runs Java with a bunch of Jar files. You can change the path of the Java command to change the Java version it’s run in. Such a simple solution, completely hidden! And with this available, why did they have to create an EXE like that?? Beats me!

Of course, once you convert to batch file, you can tweak the java parameters such as memory etc to run better.


Java: Pass by what?


I was talking to a co-developer last week during one of our lunch time walks(yes, we talk tech during out walks – even if our topic starts with something else, we always end up with tech stuff – once a geek, always a geek! :)). He is a C programmer and we often discuss Java vs C. One thing that came up last week was about Pointers. Having done both C and Java, I started explaining how Java doesn’t really have pointers, but internally it uses pointers. And that though Java has pointers (actually references), we won’t be able to change the address of a pointer like you would in C or C++.

In this context, we touched on parameter passing. I mentioned casually that Java passes the primitives by value and objects by reference (this seems to be common urban myth! Judging by the hits on this topic in Google, I think I am not the only one thinking this). I thought this would make sense, as it would be ridiculous for Java to pass around bulky objects, but instead the pointers (references) to the objects around. This makes sense, as you are able to change the values of attributes inside the object. (This was only possible in C/C++, if you passed the pointer/reference/address to an object/structure). Since we are passing around reference to the object, I said, it’s pass-by-reference when it came to objects, with the condition that “you cannot change those references”. This is where I slipped.

Apparently, those references (addresses) of those objects are passed around “by value” – meaning you cannot change them. And thus the argument that everything is pass-by-value in Java. Definitely got me. As the O’Reilly book describes it, “Java handles Objects and Arrays “by reference”, but when it comes to parameter passing, it is a Pass-by-value language. When a reference type is involved, the value that is passed is the reference!!”. Here is a nice discussion on Stackoverflow about this. There are several nice responses there that explain this in detail, so I won’t waste your time explaining the same thing here.

But Java is not alone in this – here is a nice discussion on the topic in Python. In the author’s words,

Unfortunately, Python is “pass-by-object-reference”, of which it is often said:

“Object references are passed by value.”

Incidentally, C is also a pass-by-value by default, but a programmer can achieve pass-by-reference there by passing pointers or pointer-to-pointers!! See here. It’s splitting hair at this point, but many languages are pass-by-value by default, but offer some mechanism to pass-by-reference. Even PHP employs a similar technique to pass by reference, as discussed here. Great wikipedia offers this on the topic:

Many languages support call-by-reference in some form or another, but comparatively few use it as a default, e.g. Perl. A few languages, such as C++, PHP, Visual Basic .NET, C# and REALbasic, default to call-by-value, but offer special syntax for call-by-reference parameters. C++ additionally offers call-by-reference-to-const.

There we go, the saving grace. At least, my understanding was not totally off, though I needed new terminology!! Moral of the story is “never assume anything” in IT.

Here are some more interesting links on the topic:

http://javadude.com/articles/passbyvalue.htm

http://programmers.stackexchange.com/questions/141834/how-is-a-java-reference-different-from-a-c-pointer

http://docstore.mik.ua/orelly/java-ent/jnut/ch02_10.htm

http://programmers.stackexchange.com/questions/153474/why-are-so-many-languages-passed-by-value

http://www-cs-students.stanford.edu/~sjac/c-to-cpp-info/references

http://robertheaton.com/2014/02/09/pythons-pass-by-object-reference-as-explained-by-philip-k-dick/

UML Doclet in Eclipse


I use Eclipse (Kepler) for my Java development. As you know, Eclipse is an Open Source IDE available for many languages/platforms. It’s great with many options even my PowerBuilder IDE doesn’t provide.

Where Eclipse is lacking, it allows for Plugins to be added. This is really great. Being Open Source, there are so many options available out there.

I wanted to generate UML for my Java classes and I tried various plugin in Eclipse. I did not want to hand code them, but instead just import Java classes and let the tool generate UML. They did not work as I wanted. Closest one I got was ObjectAid. This actually lets me create a class diagram and drag and drop Java classes. It generates decent class diagram. The only problem being it seems to only list Composition relationships and not inheritance.

Then I stumbled on YWorks UML Doclet. Essentially, you use this as a custom doclet in the Javadoc options and it generates Javadoc with embedded UML diagrams!!

Getting UML Doclet to work is a bit tricky. You have to mention several options, but I didn’t find one place that mentions all these options yet. After some trial and error and enough Googling, I found the following setup works for me:

In Eclipse, Click on Project -> Generate Javadoc. This will open the Javadoc Dialog (You can also use File -> Export -> Javadoc to get here).

If multiple projects open, choose the project you want Javadoc generated for. This is no different from generating the regular Javadoc. Now, if you want UML Doclet to do its job, choose Custom Doclet and enter the following details:

Doclet Name: ydoc.doclets.YStandard
Doclet Class Path: <YDoclet Dir>\lib\ydoc.jar

Where <YDoclet Dir> is the absolute path where installed/copied the YWorks Doclet to. Type the full path there. Since I am on Windows, I use backslashes and double up all backslashes for Java.

For e.g., I’ve installed my Doclet at C:\util\yworks-uml-doclet-3.0_02-jdk1.5\, so I replaced <YDoclet Dir> with this.

Click Next


Where it says VM Options, enter the below string:

-license <YDoclet Dir>\resources\ydoc.license -resourcepath <YDoclet Dir>\resources\ -docletpath <YDoclet Dir>\resources;<YDoclet Dir>\lib\class2svg.jar -umlautogen -d c:\temp\javadoc

So for example, in my case, I had to set the VM options to,

-license C:\\util\\yworks-uml-doclet-3.0_02-jdk1.5\\resources\\ydoc.license -resourcepath C:\\util\\yworks-uml-doclet-3.0_02-jdk1.5\\resources\\ -docletpath C:\\util\\yworks-uml-doclet-3.0_02-jdk1.5\\resources;C:\\util\\yworks-uml-doclet-3.0_02-jdk1.5\\lib\\class2svg.jar -umlautogen -d c:\\temp\\javadoc

Notice -d option to set the destination. I added this, so I could find my generated Javadoc files!! Otherwise, I couldn’t find the files after JavaDoc was generated (no it did not put it in Project folder, like regular Javadoc would do). -umlautogen probably adds the UML diagrams to the Javadoc.

(Also, note that the VM options text box doesn’t scoll in the version of Eclipse I have. If so, make the window itself wider. Or make the options string in notepad and paste it into the above box).

Choose JRE Source compatibility.

These settings will be retained in Javadoc generation, until you change it. You also have the option of saving these saving these settings in an Ant script, so you can run it outside of Eclipse as well.

Click Finish, voila!! You will see your Javadoc with embedded UML like shown below.


Update 11/09/2015

I saw a question on Stackoverflow that linked back to this post. Essentially, the post authors were getting below error message:

javadoc: error - In doclet class ydoc.doclets.YStandard,  method start has thrown  
an exception  
java.lang.reflect.InvocationTargetException java.lang.NoSuchMethodError:

com.sun.tools.doclets.internal.toolkit.taglets.TagletManager.  
getConstructorCustomTags()[Lcom/sun/tools/doclets/internal/toolkit/taglets/Taglet;
....

I did some testing on this and found out that they might be using Java JDK 1.8. My project uses Java 1.5, that’s why I downloaded the ydoc for Java 1.5 and set it to 1.5 compatibility above. But, just for this test, I changed the Javadoc Command on the first window to C:\Java\jre1,8.0_65\bin\Javadoc.exe. This caused the above error.

I tried mixing Javadoc.exe from Java 1.6 and Java 1.7 directories and they did fine. So, it seems to be a Javadoc version issue. Incidentally, yworks mentions only JDK 1.5, 1.6, 1.7 as supported version for the ydoc.

Java Stored Procedures


I am sure you are familiar with Stored Procedures. Almost every major database vendor supports it. They are program units that are precompiled and stored inside the database. Since the program is inside the database, it is tightly coupled with the database SQL language constructs. This and the precompiled nature of these programs, make the database access faster. So, even if you are writing external programs that access the database, you will benefit from coding and combining the database operations in stored procedures. This saves a lot of back and forth and thus network traffic. Since version 7, Oracle has support for Stored Procedures. These programs are coded a in native language called PL/SQL.

Since Oracle 8i, Oracle supports coding and running similar procedures (program units) written in Java. Like PL/SQL procedures, these programs are precompiled and stored inside the database and are known as Java Stored Procedures. The JAVASPs(1) are stored as classes (in blob fields) and when invoked they are run inside a JVM that runs within Oracle database. Prior to Oracle 10g, they needed to be wrapped inside a PL/SQL procedure or package. Since Oracle 10g, you can actually invoke Java classes directly from SQL (just the same way you call a PL/SQL function in a SQL).

JavaSPs are different from regular java programs in that they actually run inside a VM within the database. There are a few restrictions while coding a JAVASP.

Building and Deploying Java SP

To write and build the Java stored procedures, you can use your standard Java development environment. I use eclipse to develop and Ant to build it

build_LATAXSP.xml has steps to compile java classes and build jar file.

Deploying Java SP

At this point the jar file is ready to be deployed to Oracle. Typically we pass this step onto the DBA who will then load the jar into Oracle Database instance. But during development, developer can load these themselves using the loadjava utility. This is typically available on the machine where the database is running. (Remember, it is run by DBAs?). In our case we have the Oracle databases running on Unix boxes, so we have loadjava utility available there. I upload the file to Unix and run loadjava. While uploading make sure it’s in binary mode.

Below screenshot shows a sample run of loadjava command on Unix.

loadjava_results

In this example, I loaded all the classes in a jar file, to the database. As shown there were 24 classes and 2 resources loaded and there were no errors. If the command failed to load the Java classes, you will see an error message here. The first time around, all the classes files are loaded. Next successive load of the same jar file, will load only classes that have been modified since the last load.

Verifying the load

To view the objects in Oracle, following SQLs can be used:

select * from all_objects where object_type = ‘JAVA CLASS’ and owner = <owner>;

or

select * from user_objects where object_type = ‘JAVA CLASS’;

To see a little more detail about the Java stored procs, use

SELECT * FROM user_java_classes; — this lists java procs for the user.

Earlier I posted about displaying contents of the resources files (text files) loaded above.

Notes:
(1) Oracle actually refers to Java Stored Procedure as JSP. To avoid confusion with Java Server Pages, I prefer JavaSP.

Quick Tip: Java Stored Procedures in Oracle – Reading Content


This post is about how to get the contents of a resource file that is loaded into Oracle as part of Java Stored Procedures package. I’ve recently posted about Java Stored Procedures in general here.

While working on enhancing some Java procedures, I added a simple Java logger class, fashioned after Log4J. To be able to dynamically configure the logging properties (log levels, threshold), I added in a properties file, bundled into the Jar file itself1. After loading the properties file into the Database (using LoadJava command; loaded as a Java Resource), I wanted a way to “see” what’s inside the file. That’s when I posted a question on StackOverflow, but eventually googled and found the answer. See my question and my own answer here. And here is the script, I came up with:

SET SERVEROUTPUT ON
EXEC DBMS_JAVA.SET_OUTPUT (1000000);
DECLARE
   bText CLOB; 
   len PLS_INTEGER;
   offset PLS_INTEGER;
   text VARCHAR2(2000);
BEGIN
   DBMS_LOB.CreateTemporary(bText, FALSE );
   DBMS_JAVA.Export_Resource('LAJavaSP.properties', bText);
   len := 1000; -- length of the lob to read.
   offset := 1;
   DBMS_LOB.Read(bText, len, offset, text); 
   DBMS_OUTPUT.Put_Line(text);
END;
/

This printed the following:

logLevel=ERROR
dbLogLevel=ERROR
For now, that’s all I have in the properties file. Hoping to add more later.

1 Adding the properties file into the jar file makes it easy to read it using getSystemResourceAsStream. Otherwise, we will have to worry about Directory objects, Permissions etc.

(Project) Withdrawal Symptoms


Different withdrawal

I must admit. I think, I am going through withdrawal symptoms. No, not that kind. I am not an alcoholic, and I don’t do drugs. I do drink coffee, but definitely no withdrawal from there! This is a different kind of withdrawal. I think, I just OD’d on work and now I am feeling the project withdrawal symptoms. I did a regular day’s work today, it feels like I am not doing any real work. I get out of the building, while there is some light, it feels like I am leaving too early!

I just finished a project of refactoring one of our websites for performance. It took a lot of long days and nights, incessant reading, writing, thinking. Lot of headaches, anxieties, uncertainties. At times, it looked impossible, insurmountable and the team members expressed concerns, doubts. Even the week we were getting ready, there were issues, bugs, failed tests, network issues. After all, we are in the tax (peak) season already, is this a good time to deploy such a big change? Is it going to happen? Is it worth it? It was like writing/reading a mystery novel at the same time. What is the end? I am writing it, but I am also hoping to read it ahead. Couldn’t stand the suspense no more.

The number of tasks grew and grew, as I picked up slack for team members that went on vacation. Finally, it’s over!! poof! gone! We installed the system Thursday night. No issues!!!!! What? No way! I was looking for issues. But none. It was a good thing, lot of planning and work went into it, yet it felt like a very bland ending of the mystery novel. Phew! No issues! It feels good. But, why does it feel like nothing happened? The rest of the team members haven’t said anything yet. Did the implementation really happen? I look at the beautiful log file created by Log4J smiling at me. The changes actually helped us catch an issue, we otherwise wouldn’t. Yes, we did it!!

Now, I am catching up with the rest of life. I was here, wasn’t I? Calls to be made, photos to share, trips to make, people to catch up with. In the past few months, I would come home late from work, eat and jump right back on the computer, pouring over the source code, popping in and out of forums, go round and round the world wide web, eyes glued to pages of the books and the mountain of pages I printed. Now, it feels like I don’t have anything to do. But, it’s a nice withdrawal! No hangovers, no issues! Work, life balance? My wife thinks, I am back to life!!! Good to be back!

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 🙂

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.