Java frameworks popularity trends

I couldn’t compare words such as Struts and Spring, because they are used very extensively in other contexts as well.

Hence, I used, as a common denominator, word JAVA. Which might under-estimate the frequency of such words as GWT, because it is not usually used together with “Java” word.  Nevertheless, we can see the trends, at least the growth rates.

java jsf, java spring, java struts, java gwt, java ejb

Things to notice:

– Spring is steady, and seems to be the most popular so far

– Struts is dying

– JSF started very well, but seems to be steady now (still not as popular as Struts though)

– GWT is not mainstream, but again it is hard to say anything for sure

Argument passing in Java revisited

They say “It doesn’t matter till it affects you directly”… very right so!

Recently when doing a Swing application I was amazed how object passing seems to throw surprises at times. In short, object changed / reassigned in one class/method does not always change in another class/method. Hence, my assumption that objects are passed by reference in Java needed some clarification.

That’s when I started delving into more details of the mysterious “argument passing” in Java, which I always tried to avoid by just grabbing the basics. Here is what I discovered:

Arguments are ALWAYS passed by value in Java.

What I knew before was that in Java:

– primitive variables are passed by value

– objects are passed by reference

However, this was “pseudo-truth”. As a matter of fact, in Java all object variables are just “pointers” (references) to the memory locations. This fact is hidden by Java that’s why many developers don’t even know about it.

When such a “reference” is passed as an argument it’s copy is created (which points to the same memory location), hence we have an illusion that objects are passed by reference. But here is where problem occurs. Consider the code below:

swapper(Object x, Object y){
	Object temp = y;
	y = x;
	x = temp;
}

One might consider this function helps to swap variables in an easy way. But it DOESN’T!

The reason is because variables x and y are now local to “swapper” method. Hence, reassigning them would mean nothing to the “calling” method.

NOTE: As expected, changing state of x and y objects within a “swapper” method affects x and y objects in “calling” method.

Overview of Apache Cassandra, part 1

Overview

Very nice overview video on : http://cassandra.apache.org. A few points were insightful, so I decided to outline them here.

Prehistory

– General purpose DB systems (Oracle, MS SQL, etc… ) solve most of the problems, but point is that they don’t solve ’em all.

– Most obvious problem is scalability, which is solved using vertical scaling in Relational Databases (faster CPUs, faster disks, etc…). To make things worse more serious RDBMS systems cost a lot of money.

– Papers on BigTable and Dynamo present a new approach to solve those problems. Basically the idea is a “system that can handle huge amounts of data horizontally” (over several networked PCs)

– Google’s BigTable entirely depends on distributed file system they already had. In addition, they developed very sophisticated “Sparse” table mechanisms.

– Amazon’s Dynamo is strictly “Distributed Hash Table”. Due to their “high-availability” requirement, it is also one-hop DHT, meaning they should have advanced hashing algorithms. I just guess that they don’t have hierarchical hasing, because if they had, it wouldn’t be called “one-hop” anymore (unless they call O(1) = “one hop”).

– Amazon also uses BASE ( = eventual consistency) because their primary concerns are availability and consistency.

Where does Cassandra fit today?

So called NoSQL projects and those that were specifically designed to scale and deal with huge amounts of data:

Name 				Big Data
- HBase 			[V]	- more like BigTable
- MongoDB			[x]
- Riak				[V]	- more like Dynamo
- Voldemort			[V]	- more like Dynamo
- Neo4J				[x]
- Cassandra			[V]     - combines BOTH
- Hypertable		        [V]	- more like BigTable
- HyperGraphDB		        [x]
- Memcached			[x]
- Tokyo Cabinet		        [x]
- Redis 			[x]
- Couch DB			[x]

CAP Theorem – states that databases can be either CP or AP. Term was coined by Eric Brewer (UC Berkeley).

BigTable approach focuses more on CP(Consistency-PartitionTolerance), while Dynamo provides more AP(Availability-PartitionTolerance).

Cassandra is considered AP. However, I fully agree with the author on his objection of “pick-two” approach that implies that a system can be viewed as  “fully consistent and totally not available” or the other way around. There is, rather, a gradual trade-off between those two concepts. “Eventual Consistency”, in fact, is an example of that tradeoff.

Now, more about Cassandra itself:

– It is symmetric, which means all nodes are exactly the same and there is no centralization of control, hence there is no single point of failure.

– It is linear, meaning increasing size of a cluster will increase storage capacity and querying capacity as well.

– Partitioning is flexible

– Easy to grow

– Highly available (due to ideas borrowed from Dynamo)

As I was guessing, P2P routing is used to communicate between clusters, which means no centralized bottlenecks. It is interesting to note that P2P communication is structured in a Ring model, which means each cluster can pass messages to one cluster only. I guess too many clusters might introduce some delays in message propagations and, hence, some inconsistency. It is interesting how they solved those consistency issues.

For instance, if node receives a request it might serve it while update operation is being propagated over the network, causing some inconsistency. However, due to the fact that most of the related requests will be sent from the same location (and hence to the same node) we might think that this is not a big issue. But it is an issue after all…

Hibernate with DB Visual Architect tool from VisualParadigm

Important things to note:

1. If you want to see “relationship” fields in generated classes -> in ERD (Entity Relationship Diagram) -> right click on the property -> open specifications -> generated: always
2. Note that it will also require property to be set as NULLABLE and have a default value (i use NULL)
1. If working with NetBeans check that error is not actually happening somewhere in the code. Especially if it says something like:
ERROR JDBCExceptionReporter:101 – Unknown column ‘ParentId’ in ‘where clause’
In addition, compiler will not show you the exact position in the code.
I was so frustrated, I actually started doing manual Hibernate code… till I decided to give it a final try!

Ant and Tomcat error: Tried to use command /reload via a GET request but POST is required

When using Ant builder to restart Tomcat 7 server I came across the following error:

Tried to use command /reload via a GET request but POST is required

Here is a piece of code from Ant builder script:

	<target name="reload" description="Reload application in Tomcat">
		<reload url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="/${name}" />
	</target>

With a new Tomcat 7 a very special role (called “manager-script”) is required to run GET commands. Regular web based “manager” application allows POST commands only. Therefore, we need to bind “manager-script” to a user at hand.

<tomcat-users>
<user name="user" password="password" roles="admin-gui,manager-gui,manager-script" />
</tomcat-users>

Note, that we don’t need to specify roles like before. Roles are predefined by default .

In addition, note that a new Tomcat manager URL is as follows:

tomcat.manager.url=http://localhost:8080/manager/text

Raphael JS framework

Really, really, really NEAT thingie! Planning to implement a new project using Raphael, it seems to fit it nice.

However, researching it was a little troublesome, because it’s a new framework and there are some JS alternatives already. Comparison of SVG and Canvas gets a little confusing at first. Presentation podcast from the author, Baranovskiy, was quite convincing though. And I like his attitude… 🙂

We already managed to implement resizing and rotation (think: trigonometry & tangents) using mouse events, which was not very hard. This is gonna be my first BIG and serious JS work, which, I guess, will require a lot of optimization (which I “pervertedly” seem to enjoy somehow 🙂 ).

If setting charset using meta tag doesn’t seem to work

Been busy those days. But when I came across this problem I couldn’t resist not writing about it because I definitely had it before but always left unresolved … now, finally I know how to solve it 🙂

1. If your file encoding is definitely UTF-8

2. If you definitely have the right version of META tag, that is: <meta http-equiv=”Content-Type” content=”text/html;charset=utf-8″> (note that you don’t necessarily need to close this tag… )

3. If browser still shows page in some encoding but not UTF-8 (by checking in View->Character Encoding menu in a browser)

It means there is only 1 place left to look for, it’s in WebServer 🙂

Solution is simple, while page is being served WebServer, apparently, notifies web browser about what is coming in page headers (you can check it using Firebug)… and in my case it was windows-1251 (Russian). Hence, you will need to modify what your WebServer sends in headers.There are 2 solutions for this:

1. Change settings of your WebServer

2. Change the charset of the page dynamically when it is being served (which is more viable, because usually we don’t have access to the WebServer settings…. or we don’t want to change those settings not to break something else)

Ex: In PHP this is done using “header” function (this should be called before anything else) as follows:

<?php header(‘Content-type: text/html; charset=utf-8’);?>

About layering DIVs and IMGs

Recently I was making a JS slideshow and because requirements were unusual using a third party slideshow was not an option.

Images were supposed to be rendered in a particular fixed width and length (even if they do not actually fit proportionally, well that’s what client wants… ). Being lazy (and because client did not ask for performance), I just resized images “in-browser”.

I had to create a function that renders it right (without loosing quality) both in IE and other browsers. Old IE (dough! as usually… ) don’t support automatic ALPHA image rendering (basically that’s smoothening when image is resized), so I had to manually forcefully do it in the function.

But that was an easy part!

Apparently, in IE, when resized images and DIVs are used together as layers (z-index), positioning (TOP, TOP-MARGIN etc…) starts behaving VERY weird. I guess that’s because even if image is resized, it’s pixels are still treated as actual pixels (those before resizing). So style TOP:-50px; might actually be a proportion of resized image, hence less than 50.

Well good thing is that there is a solution! Tadaah! 🙂

Use all your DIV elements first, and IMG at the end. Thus, even if your DIV elements are at the bottom, place them first in the code and then just move them DOWN (which will require to lift an IMG element up). The strange thing is that moving IMG element up (as well as moving DIV element down ) does not cause any problems.

Starting research of a distributed DB

Our current billing system is becoming insufficient for handling real-time billing and real-time business operations  simultaneously. Hence, I am starting to look for better solutions. Right now system is running on Oracle (on a fast machine), but because tables have grown too big search times are taking longer and longer.

There are several paths to take:
– backup monthly or quarterly data, thus making tables smaller (but because system uses past data from time to time, especially when client asks for an old data, this does not seem a viable approach…)
– buy a stronger machine (not a long-term solution, and I don’t think top-management will approve anyways…)
– make a clustered distributed database (this seems like a good idea, but the system will need to be slightly redesigned, not sure to what extent though, in addition making Oracle clusters is a little of a hassle)
– make a real-time one-way replication of a database and assign purely SELECT queries to it (also sounds good,  but need to analyze proportion of select statements during overloads, less hassle but is it really gonna be useful)
– install another Open Source distributed database  and move non-integral high-load tables there (no experience with Open Source databases, will it be possible to extract and separate necessary tables, this is a very good in the long run tough)
 

In addition to those things, there is a need to virtualize servers (basically move them to virtual machines), because occasionally failing hardware (especially that unknown-brand made-in-China server) is driving me mad.

Passing property by reference (ref) in C#

In C# it is impossible to pass a property by reference.
This is due to the fact that properties are actually methods in disguise.
They behave like members (which are real objects), but are methods in nature.

Probably, this is done to enhance memory allocation issues for objects in C#.
Whatever the reason is, I faced the problem with passing those properties into the function so that the function alters them.

Initially I tried to do some workarounds using delegates (pointers to the methods).
However, they were not flexible enough to meet my needs.

Here is the final solution, which is based on the Microsoft’s “reflection” library.

public static void assignValueByRef(object obj, string propertyName, object src)
{
PropertyInfo propInfo = obj.GetType().GetProperty(propertyName);

//if “property to set” is INT
if (propInfo.GetValue(obj, null).GetType() == typeof(int))
{
if (src.GetType() == typeof(TextBox))
{
if (((TextBox)src).Text.Trim() != “”)
{
propInfo.SetValue(obj, Convert.ToInt32(((TextBox)src).Text.Trim()), null);
}
}
else if (src.GetType() == typeof(DropDownList))
{
if (((DropDownList)src).SelectedValue.ToString() != “”)
{
propInfo.SetValue(obj, Convert.ToInt32(((DropDownList)src).SelectedValue.ToString()), null);
}
}
}
}

We call the method as follows:
Utilities.assignValueX(objJF,
“PloshadObshaia”,
bridgeControl.FindControl(“FormView1”).FindControl(“dataPloshadObshaia”));

where “PloshadObshaia” is the name of the property of objJF object.