Last week I was not able to complete my regular review blog post. That is why today I will try to make up. I will link here articles not only from the last seven days, but also from the eighth week of the year. Read on!
Issue 180 of the Java specialists newsletter is out. There the Java champion Heinz M. Kabutz describes how during the runtime of your program you get a String (actually a
CharSequence) representing a java class, compile it and run in the same classloader as your main program.
As of Java 6, inside the
javax.tools package (part of JDK’s tools.jar, not in JVM) you can find Java compiler API. That is what the author uses for compiling the string representing the source code into a class. After that he loads the generated class file into the current classloader and starts it (the example source represents a thread).
The author avoids to enter the classloader hell so he does not actually define a child classloader of the current one and run the generated class there. He actually generates a dynamic proxy of the generated class by calling the
defineClass0 static method of the
java.lang.reflect.Proxy class. As this is not a public method, it is invoked via reflection and before that it is explicitly made accessible.
Beware that this article requires much attention in order to understand. I also advise playing around with the sample code by merely doing it by yourself.
Another core java article this week. Actually this one is dealing with concept common for most of the programming languages – sorting algorithms.
Here you can find a way to sort a huge array of positive integers. One peculiarity about this array is that its length should be about 2500 smaller than its greatest element. For example if the greatest element is 1 000 000 (one million), then for optimal results (and for a working algorithm at all), the length of the array should be about 40 000 elements.
The key for this sorting algorithm with linear complexity (O(n)) is that it uses the indexes of a temporary array. The original array is looped through. For each number there the element under the index of the temporary array which corresponds to the number of the original array is increased by one. For example if the first element of the array to sort is 5, then tempArray is increased by one. In the end the temporary array is looped through and all its indexes, which element is greater than 0, are entered in the original array. And it is entered as many times, as great is the element in the temporary array.
Just look at the
sort(int, int, int) method in the sample code to get a better feeling.
Back in the days, when you wrote your web application (and not only web), you had to validate the user input in possibly different layers of the program. With Bean Validation it is no longer needed. Now you can annotate your model classes (java beans) with annotations like
@Past, etc. These annotations are all specified in JSR 303 – bean validation. And it comes from the Hibernate Validator project, which is the reference implementation of the specification.
The author of this short blog entry first shows how easy it is to use bean validation with JPA 2.0. If you combine that with view technologies like JSF, the triggering of the validation process comes out of the box, without the need of you calling any validate() method on the API.
With JPA 1.0 is not so straightforward, but the validation can be activated there as well with the help of implementing you own JPA entity listener, which will execute the validation on certain entity manager events (like pre-persist, pre-update or pre-remove).
Finally the author asks Emmanuel Bernard – the spec lead of the bean validation JSR, what is the reason for validation upon deleting an entity. Which Emmanuel answers in his blog, where he describes a usecase, where an order has to be paid (i.e. its isPaid flag should be set) before it gets removed.
If you are wondering how search engine (like Google) work, then this article is for you. It keeps the balance of not going too much in the details and in the same time not staying too much on the surface.
So, the search engines have four main functions.
1) They are “crawling” the internet pages and downloading new content locally
2) They are indexing each single word in the page they downloaded and are building a big table of all the relevant words and terms from the page
3) Upon user query they are looking for pages that potentially match the query string
4) The found pages are ranked based on their relevance
If you are interested in more details about the above steps and what are the challenges that the search engine developers are facing, then go and read this posting.
With the advance of the hardware industry and the growing number of multi-core processors, writing multi-threaded applications is becoming more and more popular. But how to write an applications that is thread-safe (one thread does not touch the data of another one) and in the same time don’t make it so restrictive that it finally performs like a single-threaded app?
The above article summarizes the design principles in this direction. It starts by looking at the application data model, which is the most common shared resource between different threads. Next the author discusses some thread-safe structures that are part of the JDK. And finally discusses the ways to avoid deadlocks between different threads.
Apache Velocity is the most common template engine in the industry. Its basic idea is to generate resources of a certain kind (XML, DB scripts, HTML) from a template based on a model data. A very good example of what a template engine does are the JSP pages. They contain some static HTML code as well as some templates. These templates are translated at runtime based on a model (usually a map of java beans) – the variables in the template are substituted with the real values coming from the model beans (usually the engine calls the beans getter methods).
If you are already interested in the matter, then go to the first article from the new series on Apache Velocity presented in the InformIT’s reference guides collection – by far the richest java guides collection I’ve seen. In the first installment you can see the above concepts explained in more details with the usual Hello World example at the end. Of course the next series will show the power of Velocity engine and its template language.
Another short article on one of the most valuable new features of Java EE 6 – Contexts and Dependency Injection. This is a specification (JSR 299) that with the help of the annotations from JSR 330 (Dependency Injection for Java) defines a way to inject common resources (data sources, transaction managers, other beans) to Java EE managed beans. The latter may be simple session EJBs, JSF backing beans, web services etc. Not only that, but the specification defines a way to configure the scope and the lifecycle of a bean.
In the sample application created by the blog author, he develops a simple shopping cart web service, where he injects a shopping cart bean (not EJB, but simple Java bean). The latter is configured to be session scoped, which means that the application server will create a new shopping cart for each HTTP session.