The Holy Java

Notes of a passionate Java EE developer

Posts Tagged ‘java’

Using Java Compiler Tree API to Extract Generics Types

Posted by Jakub Holý on February 7, 2012

I was looking for some way to extract information about types of elements in Java collections/maps using generics (List<String>, Map<String, MyBean>) so that the users of the Static JSF Expression Validator wouldn’t need to declare the type of the elements manually. One possible way to get this information is to process the source codes with the Sun Compiler Tree API, available since JDK 6.

It might be best to go and check the resulting 263 lines of CollectionGenericsTypeExctractor.java now. The code is little ugly, largely due to the API being ugly.

Read the rest of this entry »

Posted in Java | Tagged: , , , | Leave a Comment »

Simple Logging HTTP Proxy with Grinder

Posted by Jakub Holý on July 28, 2011

Sometimes I need to observe HTTP communication between my and another machine. I usually use Eclipse’ integrated TCP/IP monitor for it’s simple and does its job well but today for a large response it just displayed “The HTTP content is too large to display.” so I started searching for alternatives and found the Grinder TCPProxy, written in Java and distributed under the BSD license.

Grinder is a Java load testing framework and the proxy is just a part of it. Here is how you would start the proxy to forward local port 6080 to the remote address example.webservices.com:80 and log the HTTP communication into a file:

java -cp lib/grinder.jar net.grinder.TCPProxy -console -localhost 127.0.0.1  -localport 6080 -remotehost example.webservices.com -remoteport 80 > http.log

The optional flag -console makes it  to display a window for shutting it down cleanly (likely unnecessary under Linux/Mac). When you want it to log just into the console, add -colour for nicely colored output.

Run it with -? to see all the available options.

Posted in Tools | Tagged: , , | Leave a Comment »

Installing Java 1.4 to Mac OS X 10.6

Posted by Jakub Holý on May 11, 2011

Sometimes you really need java 1.4, mainly because just compiling with -target doesn’t protect you from inadverently using an API that only exists in 1.5+.

Fortunately, Jens v. P. has described how to install Java 1.4 on Mac OS X without destroying your current (latest) java installation – download Java_for_Mac_OS_X_10_5_Update_4 from Apple and use Pacifist to install only System/Library/Frameworks/JavaVM.framework/Versions/1.4.2 and 1.4. Thanks, Jens!

Posted in General, Java | Tagged: , | Leave a Comment »

Joshua Bloch: Performance Anxiety – on Performance Unpredictability, Its Measurement and Benchmarking

Posted by Jakub Holý on December 10, 2010

Joshua Bloch had a great talk called Performance Anxiety (30min, via Parleys; slides also available ) at Devoxx 2010, the main message as I read it was

  1. Nowadays, performance is completely non-predictable. You have to measure it and employ proper statistics to get some meaningful results.
  2. Microbenchmarking is very, very hard to do correctly. No, you misunderstand me, I mean even harder than that! :-)
  3. From the resources: Profiles and result evaluation methods may be very misleading unless used correctly.

Read the rest of this entry »

Posted in Java | Tagged: , , , | 6 Comments »

Most interesting links of November

Posted by Jakub Holý on November 30, 2010

This month has been quite interesting, among others I ‘ve picked up several blogs by Adam Bien. I really like his brief, practical and insightful posts :-)

Java, Jave EE, architecture etc.

  • EJB 3.1 + Hessian = (Almost) Perfect Binary Remoting – “With hessian it is very easy to expose existing Java-interfaces with almost no overhead. Hessian is also extremely (better than IIOP and far better than SOAP) fast and scalable.” See also the discussion regarding a “DynamicHessianServlet” (which would be more comfortable then creating a new servlet for each service).
  • Binary XML – Fast Infoset performance results – FI is the name of a standard for binary XML encoding and also of its OSS implementation; according to these measurements (with default settings, compared to Xerces 2.7.1), on average: The FI SAX parser  is about 5 times faster than the Xerces SAX parser, the FI DOM serializer (using default settings) is 25% to 30% faster than the Xerces DOM XMLSerializer, and FI documents are 40% to 60% smaller than XML documents (i.e. it can provide modest to good compression). And: “The use of external vocabularies can be a very effective way to increase the efficiency of parsing, serializing and size at the expense of the fast infoset documents no longer being self-describing … .”
    For the interested ones: FI is built on ASN.1, a standard for describing data structures in a way that is independent of machine architecture and implementation language, it also includes a selection on different binary encoding rules, e.g. DER (triplets tag id – length – value).
  • EJB 3.1 And REST – The Lightweight Hybrid – Why to use @Stateless – there have been recently discussion about Spring vs. EJB and whether @Stateless is of any use. According to this article, the added value of EJB (though some are provided also by Spring) include injection capabilities, transactions, single threading model (for why that is good see #2 on Why I like EJB 3.0/3.1), visibility in JMX, concurrency restriction via thread/bean pools.
  • Why Service Isn’t A ServiceFacade, But ServiceFacade Is Sometimes A Service…
  • Experiences from migrating Hyperic 4.5 from EJB/JBoss to Spring/Tomcat, what good has it brought – simplified unit and integration tests, simplified code thanks to Jdbc/JmsTemplate, … (My comment: EJB 3.1 would certainly bring at least some of the advantages too.)

Performance – large JVM heaps are very much feasible

  • Tuning the IBM JVM for large heaps – tuning 64b IBM JVM 6 with 100GB heap to have acceptable GC times (~ 1/2s as opposed to 25s with the default settings)
  • BigMemory: Heap Envy – there have been recently a lot of fuss about Terracotta’s new BigMemory, a  GC-resistent many GB cache space (using NIO byte buffer). This post discusses it’s disadvantages and compares it with a solution based on ConcurrentHashMap, comming to the conclusion that the old good ConcurrentHashMap outperforms BigMemory considerably.

Other

Posted in j2ee, Java, Top links of month | Tagged: , , , , , , , | Leave a Comment »

Book review: Refactoring by Martin Fowler

Posted by Jakub Holý on November 25, 2010

I had high expectations for Martin Fowler’s Refactoring (1999/2002) but it turned out that both me and the book are too old. It had some interesting parts, but the main one – the refactoring catalog itself – had little new for me because I already know most of the refactorings and the description of steps how to perform them safely is nowadays essentially useless as they’re already automatically and safely performed by our IDEs.

I’ve enjoyed chapter 1 with a nice example of how bad code is turned into a nice one via a series of refactorings and I’d recommend it to any beginning developer. For others than me also chapter 2 may be useful, it explains why and when to refactor, how it impacts your development speed and how to justify it to the manager. I’d skip chapter 3 – bad smells in the code – and read instead of it the Uncle Bob’s lovely Clean Code , which is a great justification and basis for refactoring anyway.

In the catalog I’ve appreciated the description of some refactorings for various reasons, such as 6.4 Replace Temp with Query, 8.14 Replace Type Code with Subclasses, 9.7 Introduce Null Object, 9.8 Introduce Assertion. Of course also the other ones are good but they are just too familiar to me and my IDE to draw my attention anymore.

Chapter 12, Big Refactorings, is quite interesting, especially Tease Apart Inheritance.

Finally there are some good advices in Putting It All Together by Kent Beck, such as that it’s good to know when to stop and to be able to resist the temptation to refactor too much at once.

I’ve also appreciated the “war stories”, especially regarding performance tuning, where once again it’s demonstrated that a guess (however founded) is incomparable to hard evidence.

Conclusion

If you do refactorings daily, perhaps skip the book. If not, read chapter 1, perhaps browse through 2 and 12, check whether anything catches your eye in the refactoring catalog table of content, read Kent’s closing chapter and return the book to the local museum.

Posted in Java | Tagged: , , , | Leave a Comment »

Most interesting links of October

Posted by Jakub Holý on October 31, 2010

Few of my favourite themes this month – TDD, performance, build tools/Maven. Plus a usefel JS library, news from the Java community x Oracle world etc.

  • About dying JCP and too silent Oracle (or mostly silent) – a summary of the latest issues and hot topics in the Java community that cause lot of rumor but still no reaction from Oracle including the criticism of JCP and its proclaimed decline. Update 11/1: The article about IBM, Oracle and OpenJDK has links to many related resources and a section on JCP future.
  • A fair evaluation of TDD – Test driven development at Transloadit (“honest assessment of the beauty and pain of tdd” – Kent Beck) – according to the author, TDD requires a lot of discipline and is a pain to do but it really pays off if your risks are high, basically it’s something like an insurance – there are people living without it but to some it can save life. I miss there a thing I find essential about good test coverage – namely that it forces you to write a better code (more modular, following the single responsibility principle etc.).
  • Dear Javascript Guru: Please Stop Using The Hash Symbol For No-Op HREFs – don’t use href=”#” for it modifies the browsing history and makes the browser scroll to the top. Prefer <a href=”javascript:void(0);” … > or just use the javascript: protocol for a function call that returns false (you can force it like this: <a href=”javascript:doSomething(); void(0);”>).
  • Why Hibernate 4 switches to Gradle instead of Maven 3 – “a means to describe the issues and frustrations I have seen in my 2.5+ years of using Maven for Hibernate builds; in many cases the cause is simply an assumption or concept in Maven itself which did not line up cleanly with how I wanted to do build stuff in Hibernate.” The main issues were that Hibernate is a very specific project, which doesn’t line up very well with the Maven philosophy and, at the same time, Maven is very strict at forcing it and not really flexible to accommodate to unusual needs (and if Maven is, its plugin often aren’t). For example Hibernate is composed of modules that depend on each other while Maven really supports only an aggregation of independent projects. Also, “the release plugin is completely worthless”. On the other hand, Gradle is very flexible and – among others – offers powerful scripting, doesn’t enforce its way of doing things at all cost (i.e. directory structure), let you also define dependencies on tasks, modules, directories, etc.
  • Flot – JavaScript plotting library for jQuery, which has replaced Flash at WordPress.com (so it must be really good!) for blog statistics visualization. Main points: simple usage (all settings are optional), attractive looks and interactive features like zooming and mouse tracking. Really nice one! Check Flot examples.
  • String Concatenation Performance vs. String Builder/Buffer and how Liferay 6 achieved a speedup by not using S.B. [that much] – StringBuilder/Buffer has lot of overhead and thus String.concat or custom code can be faster sometimes. Also see the linked ticket, esp. the comment ‘most javac will try to translate “+” to StringBuilder whenever possible. So if you do need to use String.concate(), you’d better use it explicitly.’
  • Paul Graham – Beating the Averages – why it’s good to learn Lisp. (Because it makes you able to see the limitations of you current language as it’s most likely superior to it – among others thanks to Lisp macros.) A really good essay on the power of programming languages, which has persuaded me about some year ago, when I’ve originally read it, to learn Clojure (a modern Lisp dialect running on the JVM).

Posted in General, Java, Testing, Tools, Top links of month | Tagged: , , , , , , , , | Leave a Comment »

Jetty-maven-plugin: Running a webapp with a DataSource and security

Posted by Jakub Holý on September 10, 2010

This post describes how to configure the jetty-maven-plugin and the Jetty servlet container to run a web application that uses a data source and requires users to log in, which are the basic requirements of most web applications. I use Jetty in development because it’s fast and easy to work with. Read the rest of this entry »

Posted in j2ee, Java, Tools | Tagged: , , , , , , | Leave a Comment »

Most interesting links of August

Posted by Jakub Holý on August 31, 2010

I hope everybody is enjoying the holiday and not spending hours on tech blogs and sites. At least I do :-) and thus this month’s list is a short one:

  • Working With Static Imports in Eclipse – how to make working with static imports (nearly) as easy as with the normal ones (especially useful for fluent interfaces and “DSLs”), mainly by adding types like JUnit’s Assert and Mockito to your favorite imports and setting Eclipse to always generate static imports in the form <type>.*
  • 5 things you didn’t know about … Java Database Connectivity – it was interesting to learn that JDBC specifies some scalar functions that drivers may support and translate into the DB’s language such as “{CURRENT_DATE()}”; for common functions supported by most drivers this should make your implementation more portable
  • Four Things to Remember about java.lang.String – a really good one thanks to information on how to compare correctly the same Unicode character/string that can be encoded in different ways with java.text.Normalizer.normalize and Locale-sensitive comparison ignoring optionally unimportant differences such as letter size and accents (using a Collator)

Posted in Java, Tools, Top links of month | Tagged: , , , | Leave a Comment »

Most interesting links of July

Posted by Jakub Holý on August 2, 2010

This month about performance, the Java language and patterns, the development process, and a few interesting news.

Read the rest of this entry »

Posted in Java, Top links of month | Tagged: , , , , | Leave a Comment »