Space Shuttle Discovery’s retirement flyover D.C.

Posted on by tom
Reply

Here are some of the photos I captured today of Space Shuttle Discovery flying over D.C. today on board its 747 carrier. The shuttle circled around D.C. a few times before heading for its final resting place at the National Air and Space Museum Steven F. Udvar-Hazy Center next to Dulles International Airport.

Posted in General | Tagged dc, discovery, space shuttle | Leave a reply

Two reasons to prefer Hibernate JPA over EclipseLink on GlassFish

Posted on by tom
spacer vs. spacer
EclipseLink is the default JPA provider on the GlassFish JEE server. As such, I figured EclipseLink would work well as the persistence provider. However, we began a recent JEE 6 project using GlassFish v3 and chose Hibernate as the JPA provider because of the team's familiarity with Hibernate 3. We later switched to EclipseLink to be compatible with another application running on the server -- and immediately encountered two annoying problems that never occurred with Hibernate. This article is meant to document those EclipseLink problems in case it helps others with similar EclipseLink issues on GlassFish.

The first problem we saw was a sporadic ClassCastException after a module redeploy. The second problem we found was that entity methods marked with @PrePersist were not being called when the entity was being saved to the database. Instead, those fields would remain null when EclipseLink executed the INSERT SQL statement, resulting in constraint violations for our non-nullable columns.

The ClassCastException occurs at a line in our code that assigns an entity to a reference variable declared as the type of its parent class, which is a JPA mapped superclass. A widening cast like that should cause no problem, so this was a head-scratcher. Adding to the mystery is we had no problem with this code when using Hibernate JPA. The entity class in question, ActionTypeLookup, as shown in the stack trace below, directly extends the parent class type that it is being assigned to.

Here are the partial class definitions. The parent class type, CodeLookupValues,
@MappedSuperclass
public abstract class CodeLookupValues implements Serializable {
...
}
is a @MappedSuperclass that the entity ActionTypeLookup directly extends:
@Entity
@Table(name="ACTION_TYPE_LOOKUP")
public class ActionTypeLookup extends CodeLookupValues implements Serializable {
...
}
The persistence code is defined in a module (an EAR file) containing the JPA persistence unit definition. The ClassCastException occurs only if that same module is unloaded and reloaded from the server several times. That is, something you do a lot of during development. We had loaded and unloaded this EAR file many times on GlassFish with no problem when using Hibernate. Shortly after the switch to EclipseLink, we would see this stack trace in the log at deploy time:
javax.enterprise.system.core.com.sun.enterprise.v3.server Exception while loading the app
javax.ejb.EJBException: javax.ejb.CreateException: Initialization failed for Singleton LookupSessionFacade
at com.sun.ejb.containers.AbstractSingletonContainer$SingletonContextFactory.create(AbstractSingletonContainer.java:695)
at com.sun.ejb.containers.AbstractSingletonContainer.instantiateSingletonInstance(AbstractSingletonContainer.java:444)
at org.glassfish.ejb.startup.SingletonLifeCycleManager.initializeSingleton(SingletonLifeCycleManager.java:213)
at org.glassfish.ejb.startup.SingletonLifeCycleManager.initializeSingleton(SingletonLifeCycleManager.java:174)
at org.glassfish.ejb.startup.SingletonLifeCycleManager.doStartup(SingletonLifeCycleManager.java:152)
at org.glassfish.ejb.startup.EjbApplication.start(EjbApplication.java:150)
... [removed several classes involved in installing the EAR] ...
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
at java.lang.Thread.run(Thread.java:619)
Caused by: javax.ejb.CreateException: Initialization failed for Singleton LookupSessionFacade
at com.sun.ejb.containers.AbstractSingletonContainer.createSingletonEJB(AbstractSingletonContainer.java:525)
at com.sun.ejb.containers.AbstractSingletonContainer.access$100(AbstractSingletonContainer.java:74)
at com.sun.ejb.containers.AbstractSingletonContainer$SingletonContextFactory.create(AbstractSingletonContainer.java:693)
... 36 more
Caused by: java.lang.ClassCastException: my.customer.package.shared.datamodel.reference.ActionTypeLookup cannot be cast to my.customer.package.shared.datamodel.reference.CodeLookupValues
at my.customer.package.shared.datamodel.persistence.LookupSessionFacadeBean.reloadCommonLookupValues(LookupSessionFacadeBean.java:127)
at my.customer.package.shared.datamodel.persistence.LookupSessionFacadeBean.readLookupTables(LookupSessionFacadeBean.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.ejb.containers.interceptors.BeanCallbackInterceptor.intercept(InterceptorManager.java:1006)
... [removed several more classes involved in installing the EAR] ...
at com.sun.ejb.containers.interceptors.CallbackChainImpl.invokeNext(CallbackChainImpl.java:61)
at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:390)
at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:373)
at com.sun.ejb.containers.AbstractSingletonContainer.createSingletonEJB(AbstractSingletonContainer.java:518)
... 38 more
We see the error at deploy time because the application has a @Singleton EJB that also is annotated with @Startup so the server loads it at deploy time. I don't know if having a non-startup EJB or a non-singleton EJB would make a difference.

It turns out this was the easiest problem to work around -- the ClassCastException goes away if you restart the domain. But I never discovered what causes the exception. At first, I thought the problem must be caused by the two classes being loaded by separate classloaders due to the way we had structured the modules in our application. However, both classes are:
  • Deployed in the same Java package
  • Inside the same JAR
  • Packaged and deployed inside the same EAR.
The ClassCastException does not occur all the time. It just suddenly pops up after a series of deploy/undeploy cycles and does not go away until we restart the GlassFish domain.

My best theory so far as to what causes the ClassCastException is it is a bug in GlassFish's OSGi bundle class loading. The problem appears as if some GlassFish classloader has the parent MappedSuperclass class loaded even after module undeploy but not the child class. Subsequently, when the updated EAR file is deployed again, the new module's newly assigned Archive classloader sees that the parent class is already loaded but needs to load the child classes from the new EAR files's JAR. When the code tries a widening assignment of a child entity to a reference type of the parent class -- kaboom! -- a ClassCastException because the two classes are now unrelated because of the different classloaders.

The GlassFish OSGi implementation gets my suspicion only because EclipseLink is deployed in GlassFish as an OSGi bundle, and the ClassCastException never occurred when using Hibernate, which is deployed as a set of jar files inside the server's shared library classpath.

Better theories are welcome in the comments. I poked around but found no others reporting the same bug or that this problem has been fixed in a GlassFish update (we are not running the latest updates of v3). This ClassCastException is only an annoyance because the workaround is to restart the GlassFish domain/server. A restart always fixes the problem and it goes away, until after another series of deploy/undeploy cycles.

@PrePersist problem

The second and more vexing problem is with the way EclipseLink synchronizes its entity session cache with the database. If I understand the problem correctly from reading various forum postings, if you instantiate a new entity object and call an EntityManager.find() method before the new, detached entity has been persisted with EntityManager.persist(), EclipseLink synchronizes all the detached entity states with the database before performing the query. That synchronization makes sense: the database is going to need to know about the new rows in the tables if the SELECT query is going to find the correct data.

The problem, at least for me, is that when EclipseLink performs the INSERT statement to persist the unsaved entities, it does not call the @PrePersist methods on the entity. I was counting on @PrePersist methods to set values like created-date and last-updated-date, which cannot be null in our schema. OK, I can understand that EclipseLink opted not to call @PrePersist methods when it is merely inserting the data for its convenience and not because EntityManager.persist() was called. But the unexpected behavior required some logic change in our entity code because Hibernate always seemed to call our @PrePersist methods before performing an INSERT on the database. After the switch to EclipseLink, we started seeing database constraint violation errors on these @PrePersist columns.

This second problem with EclipseLink, then, cannot be called a bug, but it was unexpected behavior worth documenting here. I assumed incorrectly that @PrePersist methods, by definition, always would be called before an entity got persisted. Wrong.

Those are the only two unusual problems we saw after switching from Hibernate to EclipseLink. The other problems that occurred were some HQL-specific queries failed, but those could all be converted to standard JPQL with a little alteration.

After complaining about EclipseLink issues, I will profess one preference for EclipseLink: The debugging log messages and error log messages seemed clearer and more straightforward with EclipseLink. When one our our HQL queries failed or I needed to trace what queries were being called and when, EclipseLink's debugging messages just seemed clearer and easier to understand than many of Hibernate's logging messages. I could see queries more clearly being translated from JPQL to Oracle SQL, and see what values were being bound to query substitution parameters. I do hand it to EclipseLink for making its database interactions more transparent than I am used to with Hibernate. So when we had the @PrePersist problem, at least I was able to debug and diagnose them fairly easily.
Posted in Java

Astrogeeks and photographers: Look eastward this weekend

Posted on by tom
spacer Moonrise on Feb. 28, 2010

The full moon will rise this weekend at nearly 90 degrees azimuth for those in Washington, D.C. That means the moon will be almost directly to the east. Since the National Mall and many of its famous monuments and buildings align along an east-west axis, the astronomical phenomenon promises stunning moonrises from places like the Washington Monument, the Lincoln Memorial, and the Netherlands Carillon as the moon slowly rises behind or next to the U.S. Capitol. If the expected clouds abate on Friday and the weather holds out, that is.

As you can see from these photos of near-90 azimuth moonrises last year, the moon looks great near the horizon when looking east across the Mall. Here are the stats for the weekend:

On Friday, the nearly-full moon will rise in D.C. at 6:23 p.m. EDT at 89 degrees azimuth (source). On Saturday, the full moon rises at 7:39 p.m. at 97 degrees azimuth.

And if the full moon due east isn't enough to pique your geeky astronomical interest, this weekend's moon will be a big and bright moon. Saturday, the full moon is at perigee -- its closest approach to earth. This perigee is the closest the moon will get to the Earth for all of 2011: 221,575 miles (356,575 kilometers). That's 31,064 miles closer than the moon was on March 6, according to EarthSky. EarthSky says Saturday's full moon will be its closest encounter with the Earth since Dec. 12, 2008, and the closest it will be until Nov. 14, 2016.

For photographers interested in capturing the event, outside the Lincoln Memorial and the Netherlands Carillon should be good places to set up your tripod. (Tip: You might want to get to the Carillon early to stake out an unobstructed spot for your tripod. It's a popular place for full moon photos. Note that the path in front of the bronze lions is a popular one with joggers, bikers and pedestrians, so get your siteline set up with that in mind.) As far as weather goes, the current forecast calls for partly cloudy Friday around moonrise with a slight chance of rain. But sometimes a low cloud layer can make for great photos if the clouds aren't dense. Saturday also promises to be partly cloudy with some rain possible in the morning. But the clouds are supposed to clear by moonrise. Saturday promises to be the better day for weather but with the 97 degrees azimuth, perhaps less-stunning photos (more like the one below).
spacer Moonrise on Aug. 25, 2010
I'm looking forward to some beautiful moonrises this weekend. Share the moment with someone you love -- but hey, take the camera. If you grab some good photos, please send me a link in the comments.
Posted in General

Working around ddclient’s “bad hostname” and “network is unreachable” problems

Posted on by tom
I have had continuing problems with ddclient being able to connect to the network and make an http call to check my current IP address. If you use ddclient and also see this problem, this workaround might work for you, too.

The ddclient bug exhibits itself with two errors I would see in the system log and also kindly emailed to me by the ddclient daemon itself:
WARNING:  cannot connect to checkip.dyndns.org:80 socket: IO::Socket::INET: Bad hostname 'checkip.dyndns.org'
or the more generic error:
WARNING:  cannot connect to 192.168.0.1:80 socket: IO::Socket::INET: connect: Network is unreachable
The issue seems to be that ddclient, a Perl client that talks to dynamic DNS services like dyndns.org, has problems either making network connections or perhaps caches a bad address at system start when networking services might not yet be up. This problem with ddclient seems longstanding, with a bug filed in 2003 on the Debian list and a bug filed in 2009 on the Red Hat list.

The Red Hat bug was closed May 29 with a fix (ddclient-3.8.0.2) posted to update sites for Fedora 11 and later. But if you have not or cannot update, or still see the bug, here's my workaround: Instead of using ddclient's built-in web client to connect to your dynamic-DNS service, call a shell script that uses curl to make the network call. Specifically, I replaced this line in my /etc/ddclient.conf configuration file:
use=web, web=checkip.dyndns.org/, web-skip='IP Address' # found after IP Address
with this line:
use=cmd, cmd=/home/tom/bin/checkip.sh, cmd-skip='IP Address' # found after IP Address
Here is my checkip.sh shell script, stored in my home "bin" directory:
#!/bin/sh
#
# A script to fill in for what ddclient
# can't seem to do: reliably connect to checkip.dyndns.org.
curl checkip.dyndns.org/
That's it. The only extra steps you need to take are to ensure the user that runs your ddclient daemon (typically user "ddclient") has access to the script. That means in my case making sure the script itself is executable, e.g. chmod 755 ~/bin/checkip.sh, and that my home directory and bin directory are world executable, e.g. chmod --recursive o+x ~/bin/checkip.sh

When I eventually upgrade my system and use version 3.8.0.2 of ddclient, I look forward to seeing if this longstanding networking bug really got fixed.
Posted in General

Clojure’s inventor and author make a case for the new language

Posted on by tom
Rich Hickey, inventor of the Clojure language, and Stuart Halloway, author of "Programming Clojure," presented introductory and advanced concepts of the young JVM language at Wednesday's Northern Virginia Java Users Group. These are some of my notes from the meeting. The session served to whet interest in learning Clojure, thus these notes do not include a lot of code or explain Clojure's unusual syntax. There are many other sources for that.

Clojure, a Lisp-like language that compiles to Java byte code and runs on the Java virtual machine, was created as a general-purpose programming language that embraces a functional style of software design, rather than the imperative style typical in languages like Java -- and most other general purpose languages in use today. Functional programming languages like Clojure, Scheme and Erlang have been getting a lot of attention at technology conferences over the last few years, which first brought my attention to Clojure. Its functional style and its ability to run alongside and integrate with existing Java code interested me in learning more about Clojure. The fact that its inventor and a technology instructor I highly respect were presenting a free session on Clojure compelled me to attend the JUG meeting.

Rich Hickey released the first version of Clojure in October, 2007, with version 1.0 released May 1, 2009. We are talking about a young language. Still, from what I learned last night, it looks like a powerful language with potential. Clojure is released as open source under the Eclipse Public License 1.0, which makes it easy to use in a non-open source commercial environment.


Stuart Halloway
Stu Halloway, co-founder of the top-notch professional training and agile consulting company Relevance Inc., began with an introduction to Clojure's features and why a Java developer might want to learn it. Rich then took over and introduced three new features of Clojure (Protocols, Reify and Datatypes) that can be downloaded from the latest source tree but are not part of the current 1.1 release of the language.

According to Stu, some of the compelling features of Clojure are its:
  • Easy interoperability with Java
  • Lisp syntax
  • Functional style
  • Ability to run in a multi-threaded environment with no coding overhead
To demonstrate the syntax benefit, Stu "refactored" the StringUtils.isBlank method from the Apache Commons lang library. He started by showing the full Java source code and then removing all the ceremonial scaffolding code to expose the core logic, then simplified the Java code into the definition of an equivalent Clojure function:
(defn blank? [s]
(every? #(Character/isWhitespace %) s))
I'm not a Clojure programmer (yet) but I think I captured the above syntax correctly. Like Ruby, Clojure uses the question mark to replace the traditional "is" prefix in boolean functions. The # symbol introduces an anonymous function. From what Stu described, the functional programming paradigm in Clojure handles most (all?) corner cases for you. There is no need to write special-case "if" statements to deal with a null parameter, for instance.

For Clojure's interoperability with Java, Clojure code can call Java, and Java code can call Clojure functions. (According to Rich, the integration is implemented with little or no need to use Java reflection at runtime, adding less runtime overhead.)

For Clojure's advantage by using Lisp syntax, Stu referred everyone to Paul Graham's 2001 article, "What Made Lisp Different" as the best explanation. Most languages have "special forms" like imports, scopes, protection definitions, metadata, keywords. These special forms are language features you can use, but you cannot create them yourself and add them to the language. These language features are thus unavailable for reuse. Lisp abandoned this restriction. In a Lisp-like language, special forms of the language look like anything else in the language. All forms of the language are created equal. In Lisp (and Clojure), defining scope, the control flow, method calls, operators, functions, import mechanisms -- they are all lists. Stu said a language's "special forms" restrictions cause a programming language to "crap out," and joked that the restrictions bring about magical cut-and-paste reuse workarounds we call "design patterns."

For Clojure's advantage by being a functional language, Clojure encourages you to write small pieces of code that work well together. Good code has the same shape as pseudo code, he said, and Clojure's functional style lets you create more pseudo-code looking real code. According to Stu, functional languages are simpler to understand. They let you write code that eliminates or reduces what he called "incidental complexity" required by non-functional languages:
  • Corner cases
  • Class definitions
  • Internal exit points
  • Variables
  • Branches
The resulting code is less complex, he said, and simpler to understand by orders of magnitude.

The final benefit he talked about is Clojure's inherent ability to run in a multi-threaded environment with no special concurrency-handling code from the developer. Clojure and other functional programming languages perform this feat by treating data as immutable and producing a new copy of a data structure when data needs to be changed. Two threads never look at the same data at the same time, so there is never any need to synchronize access to code that reads and writes data. Clojure's solution, Stu said, is to separate identify from value. He went on to explain what this means, but maybe the late hour caused me to miss the details.

Rich Hickey

After Stuart set the stage for why learn and use Clojure, Rich Hickey took over to talk about new features he is adding to the language. He said, quite truthfully, that for those in the audience who don't already know Clojure, what he was about to say would not make a lot of sense. These features are Protocols, Reify and Datatypes. As a result of my newness to Clojure, I will pass along what I thought Rich said and hope he and the Clojure crowd forgive my ignorance.

Rich Hickey, inventor of Clojure, speaking March 17, 2010 at the
Northern Virginia Java Users Group meeting.
[taken from my phone]


Rich, for an open source programming language inventor, was a refreshingly clear advocate for his new language. Maybe I'm jaded from years of slogging through open-source code, but from my experience, most open source projects release their code with little explanation of how or even why to use it, and then treat users like they are the ones who failed if they misunderstand how to use the code correctly. Rich actually understood where most of us in the audience were coming from. "I know it's a big deal to try to learn a new programming language," he said, but he believes Clojure is worth taking the time to learn and will make our jobs as programmers easier.

Before delving into the new features he is adding, Rich provided a summary of how Clojure is implemented. Part of it is written in Java for performance, and the rest is written in Clojure itself. He said his goal is to eventually write most of Clojure in Clojure once he can get performance boosted to an equivalent level.

Clojure is built using abstractions, with those abstractions written as Java interfaces. The fundamental implementation objectives of Clojure (or at least the ones I picked up on), he said, are to leverage high-performance polymorphism mechanisms of the host environment, to write to abstractions not concrete types, and to enable extension and interoperability with Java.

From what I understood of the new language features, Protocols are named sets of generic functions. Reify allows developers to use the "cool code generation" in the built-in fn function. "I put a lot of work into 'fn' and I wanted to make it reusable," he said. Even though it went over my head, Rich said Reify allows developers to create an instance of an unnamed type that implements protocols, like proxy for protocols. For the new Datatypes feature, if I understood correctly, he said he added a new construct, deftype, to define a name for a type and list of fields in that type.

Additional details that might make sense if you know Clojure:
  • Datatypes fields can be primitives
  • Datatypes support metadata and value-based equality by default
  • In-line method definitions are true methods, no indirection or lookup and calls can be inlined by just-in-time compilers, like Hotspot
  • Keyword-style field lookups can be inlined just like (.field x) calls
Rich concluded by offering more reasons to explore and begin using Clojure. "Closure has dramatically less implicit complexity than other languages," he said. You don't need to write a lot of code simply to support the needs of the language. You spend your time with Clojure focusing on domain complexity, not language complexity, he said. "It has a lot of newness, so the unfamiliarity level is high," he said. "But it is very, very simple."
Posted in Java

The Lost Symbol: Nix It From Your Christmas List

Posted on by tom
Let me start this review of Dan Brown's latest novel by saying I read Angels & Demons and The Da Vinci Code and thoroughly enjoyed the stories and the storytelling. Second, although The Lost Symbol was at times painful to read, I do not join other critics who point out the preachy, moralistic ending. Sometimes we need a reminder to return to the basics of our morality. Finally, I plan to reveal minor details of the book here but I won't disclose any plot twists or surprises.

The Lost Symbol reads as if Dan Brown had been kidnapped and tortured by the Masons, just like one of the characters in the book is kidnapped and tortured by an evildoer, and forced to write this book under duress. Each chapter, while revealing frat-boy antics committed by the Masons during its rituals, also includes what seem to be apologies to the reader for those antics. Brown constantly reminds the reader that Masons have included the geniuses of history, the rich, the politically powerful -- including, he says, most of the high-ranking members of all three branches of the U.S. government. Whenever a character in the book criticizes a Masonic activity, the hero of the book reminds us how warm and cuddly the Masons really are to the point that the subtitle of the book could have been, "Hug a Mason Today."

The constant apologies for the Masons is not why I thought this book was a Brown dud. I actually learned what I hope are facts about Masonic history from this book, which I thought were enlightening and interesting. No, the worst part of this book is the amateurish writing and the forced, silly narrative. Brown wanted to ladle so much history and symbology onto the pages that the hero of the story, Robert Langdon, has to constantly stop and lecture one or more of the other characters in this book on the history of Freemasonry and all the wonderful contributions the world has received unto it by a Mason. We're 30 seconds from the clutches of the bad guys, from whom we are running so we can save someone's life, but wait, let's stop a moment so I can explain in historic detail a particular symbol, or show you this nifty, magical number sequence and spell out in detail why it pertains to our rescue mission. Those stop-and-explain moments clue the reader in early that the tension the author is trying so hard to build must not be really all that tense if the main characters have so much time to marvel over history while being hotly pursued.

To add to the amateurish narrative, the characters, all portrayed as very smart and world-wise, are shocked, shocked! at every predictable turn of events. The characters actually exclaim, quite regularly, "Oh my God!" when something occurs that the readers will have predicted 5 pages ago, pandering to our egos so we can constantly pat ourselves on the back on how smart we are. Langdon, who is surprised the most, has evolved from a savvy, likeable university professor in The Da Vinci Code to a naive, gullible idiot savant. What? You mean this secret package as heavy as a bowling ball, the one my good friend and mentor (and, gasp, a 33rd degree Mason) told me years ago to keep safe and guard with my life because evil people across the entire globe would kill for it, and for which I got a mysterious phone call this morning telling me to bring this vital package to Washington, D.C., this heavy package I have been carrying over my shoulder, which I completely forgot I was carrying even though my shoulder is aching from the weight, might have something to do with why my friend and mentor has been kidnapped? Oh my God! How could this be? I'm shocked! Shocked! And sadly, I'm not exaggerating.

Another example of the irritating writing packed inside The Lost Symbol is that nearly every chapter begins with a retelling of what has occurred up to this point -- just in case the previous section had lulled you into a deep case of neurasthenia and you lost all memory of the previous dozen pages. Why Dan Brown felt he had to constantly summarize previous events is a mystery. If you ignore my suggestion to pass on this book, you will remark to yourself each chapter how you haven't seen such great recapping of events since watching the first three minutes of Batman reruns from the 1960s where they summarize the previous week's cliffhanger.

As the final reader irritation (especially to us in Washington, D.C.), Brown gets some of his D.C. geography, details and landmarks wrong. Here are some of the more obvious factual indiscretions:
  • His limo driver takes him from Dulles Airport to the Capitol via an unlikely route: the Dulles toll road to the beltway to the George Washington Parkway, then finally over the Memorial Bridge. Unless I-66 was closed, the limo driver would not have taken the beltway.
  • The book says the trip from the airport took a half