Tuesday, December 29, 2009

@DataSourceDefinition -- A Hidden Gem from Java EE 6

In the old days, DataSources were configured -- well, they were configured in lots of different ways... That's because there was no 'one way' to do it -- in JBoss, you created an XML file that ended in '-ds.xml' and dumped it in the deploy folder... in Glassfish, you either use the admin console or muck with the domain.xml file... in WebLogic you used the web console... and this was all well and good -- until I worked with an IT guy who told me just how much of a pain in the ass it was...

Up until then, it wasn't such a big deal to me -- I set it up once, and that was that... then I ran into this guy a few jobs ago who liked to bitch and complain about how much harder it was to deploy our application than the .NET or Ruby apps he was used to... he had to deploy our data source, then he had to deploy our JMS configurations -- only then would our application work... in the other platforms, that was all built into the app (I'll have to take his word for it, since I haven't actually deployed anything in either platform)... I was a but surprised at first, and then I realized that maybe he had a point... nah, it couldn't be, he must just be having a bad day (lots of us were having bad days back then :) )...

Then I ran into Grails, which is dead simple -- you have a Groovy configuration file that has your db info in it... you even have the ability to specify different 'environments', which can change depending on how you create your archives or run your app... pretty slick...

The Gem

Well, lo and behold, we now have something that's nearly equivalent in Java EE 6 -- the @DataSourceDefinition attribute... it's a new attribute that you can put on a class that provides a standard mechanism to configure a JDBC DataSource into JNDI, and as expected, it can work with local JNDI scopes or the new global scope, meaning you can have an Environment Configuration that uses this attribute making it shareable across your server... it works like this:

import javax.annotation.sql.DataSourceDefinition;
import org.jboss.seam.envconfig.Bind;
import org.jboss.seam.envconfig.EnvironmentBinding;

@DataSourceDefinition (
public class Config {

As you would expect, that annotation will create a DataSource that will point to a local Derby db, and stick it into JNDI at the global address 'java:global/jdbc/AppDB', which your application, or other applications can refer to as needed... no separate deployment and no custom server-based implementation -- this code should be portable across any Java EE 6 server (including the Web Profile!)...

It's almost perfect!

In typical Java EE style, there's one thing that just doesn't appear to be working the way I'd like it -- it doesn't appear to honor JCDI Alternatives (at least not in Glassfish)... Here's what I'm thinking -- we should be able to have a different Config class for each of our different environments... in other words, we'd have a QAConfig that pointed to a different Derby db, a StagingConfig that pointed to a MySQL db somewhere on another server, and a ProductionConfig that pointed to kick ass, clustered MySQL db... we could then use Alternatives to turn on the ones that we want in certain environments with a simple XML change, and not have to muck with code... unfortunately, it doesn't appear to work -- it appears in Glassfish that it is processing them in an undeterministic order, with (presumably) the class that is processed last overwriting the others that came before it...

There is a solution, though, and it is on the lookup side of the equation -- using JCDI Alternatives, we can selectively lookup the DataSource that we're interested in, and then enable that Managed Bean in the beans.xml file... it's definitely not ideal, since we need to actually inject all of our DataSources into JNDI in all scenarios, but it works, it's something I can live with, and is probably easily fixed in a later Java EE release... Update: Looks like it's in the plan, according to this link -- thanks, Gavin :)

Here's how it works -- first the 'common' case, probably for a Development environment:

public class DSProvider {
@Resource (lookup="java:global/jdbc/AppDB")
private DataSource normal;

public DataSource getDataSource() {
return normal;

Simple enough -- has a field that looks up 'jdbc/AppDB' from JNDI, and provides a getter... now for QA:

@RequestScoped @Alternative
public class QADSProvider extends DSProvider{
@Resource (lookup="java:global/jdbc/AppQADB")
private DataSource normal;

public DataSource getDataSource() {
return normal;

Pretty much the same, except this does the lookup from 'jdbc/AppQADB', and it is annotated with @Alternative... so how do these things work together? Take a look:

public class Test {
private DSProvider dsProvider;


Again, simple -- we're injecting a DSProvider instance here, and presumably running a few fancy queries... Nothing Dev-ish or QA-ish here at all, which is the beauty of Alternatives... finally, when building the .war file for QA, we turn on our Alternative in the beans.xml, like so:


You'll notice that this solution requires us to rebuild our .war file for QA, which I obviously don't like -- not to worry, there will be support for this in the Seam 3 Environment Configuration Module, which will effectively create a binding by mapping from one JNDI key to another... I have no idea what the syntax will look like at this point, but it should be pretty straight forward, and will allow us to -- you guessed it -- build our .war one, and copy it from place to place without modification...


Saturday, December 26, 2009

Say hello to the Seam 3 Environment Configuration module

A funny thing happened after my last post -- I got an email from Dan Allen, from RedHat, with some interest in making my last JCDI Portable Extension -- EnvironmentBindingExtension -- into a Seam 3 Module... pretty cool for a fairly modest effort at finding a new way to solve a problem I've faced in the past... it will be my first official foray into open source (not counting that one line NetBeans patch I submitted in, like, 2000), so it will be interesting to see how this will actually work from the authoring side, as opposed to the user side, especially in a relatively well organized project like Seam...

What it's about

The idea behind the Environment Configuration module is to inject fairly static configuration information into any JEE 6 environment... it's typically done outside of your application, in a deployment that isn't regularly deployed or updated, so you can configure each of your environments separately, including Development, Testing, QA, Staging and Production -- once this is done, you can build your application once (or better yet -- have a Continuous Integration server build it!), and copy the same binary from server to server without having to reconfigure it, ensuring that the archive that you deploy to production is the same exact archive that you tested in QA... this allows you to streamline your deployment processes, removing any possible human error involved in building your code over, and over, and over again (and in some cases, it'll save a lot of time if you have a particularly slow build!)

How's it work? It takes advantage of JNDI -- one of the resources that all JEE servers provide... say, for example, that you have a system that needs to access a database, a filesystem, and has a batch process that runs at a specific frequency -- in development, you'll want to point to a personal Derby database, use a local folder on your Windows box for your filesystem, and run the batch process very frequently for testing... QA is similar, although it has different database, but say Staging and Production run on a cluster of Linux boxes that access a MySQL database, use a mounted shared drive for its' filesystem, and have its' batch processes run once an hour...

With the Seam 3 Environment Configuration module, you can create a simple .ear file for each of these environments that contains all of this data -- create them once, deploy them once, and you're good to go... take a look at the following example of a configuration that you could use in development:

* An Environment Configuration for Development
* @author Matt
@DataSourceDefinition (
public class Config {
@Bind ("myApp/fs-root")
String rootFolder = "C:\fs-root";

@Bind ("myApp/batch-frequency")
long batchFrequencyInMs = 60 * 1000;

Pretty simple -- toss this class into its' own .war file, and it will define three global JNDI entries, one for each of the items mentioned above... your other applications are now free to read these resources in whatever way they need to, even using the standard @Resource(lookup="java:global/myApp/fs-root") notation... a similar configuration file would be created for QA, but perhaps the @DataSourceDefinition annotation will use a MySQL datasource, and likewise for Staging and Production...

What next?

Well, there are a few things on my list of features here, including, but not limited to:
  • Test, Test, Test!
  • Using the @Bind attribute on methods, including @Produces methods
  • Support 'unbinding', if needed
  • Create a Maven Archetype that could be used to quickly and easily setup an Environment Configuration deployment
  • Create an interface of some kind to be able to review the available findings -- either web app or simply JAX-RS based
I am, of course, interested in any ideas or feedback anyone would have, but one goal I would have here is to keep it simple and portable -- what this module is intended to do isn't exactly brain surgery, so I don't think it's necessary to throw in too many 'extras'...


Tuesday, December 15, 2009

External CDI Configuration with Portable Extensions

A common requirement for web and enterprise applications is that they have the capability to configure themselves for each environment without modifying the archive itself -- most commonly this is used only for environment specific attributes such as a test vs. production data store, or for Strings describing a file or directory on the file system which will be different on a developers box vs. a clustered production server, or perhaps it is a URL that points to your test payment gateway vs. your production gateway... This is the sort of thing that might easily be done with 'Alternatives' in CDI, but many shops put a premium on the ability to package the application once (on a Continuous Integration server, for example) and copy that file from development to integration to QA to staging to production, all of which are on very (very!) different platforms, using different databases, different file systems, and must integrate with different third party environments -- configuring this stuff externally means you don't have to deal with the error prone and possibly time consuming process of building for each environment... unfortunately, this doesn't appear to be a scenario that Alternatives can help us with...

One resource that works really well for this sort of configuration is JNDI... configure these items on your servers' JNDI registry independently from your application, and then have your application read the environment configuration settings from here -- and CDI makes it very easy to manage both sides of this scenario!

Reading from JNDI

The easier side of this is reading the data from JNDI, so let's start there... actually, you don't need CDI at all to start doing this -- the easiest way is to use the '@Resources' annotation provided in Java EE 5, like so:

public class FolderConfig {
private String folderToPoll;

public String folderToPoll() {
return folderToPoll;

Not much to this -- we have an ApplicationScoped Managed Bean which does a lookup from JNDI, and provides a getter for the result... in this case we're pulling from the new "java:global" context that is provided with Java EE 6 -- there's no reason we couldn't map this to local context, but frankly, I wanted to fiddle with the global context :)

Ok, now on to something more interesting...

Writing to JNDI

Writing to JNDI is pretty easy -- get an InitialContext and call 'bind'... it's basically an overblown HashMap... for some reason, though, configuring JNDI outside of an application always seems to be more difficult than it should be -- several years ago, I actually had to write a JBoss plugin to do it, even though they had quite an advanced configuration mechanism for the time... all I wanted to do was put String 'A' at Key 'B', but no -- not supported out of the box!

That solution was configured by an XML file, which left me dealing with Strings... this solution is better on two accounts: 1) It can bind any Object into JNDI, and 2) it's a Portable Extension, and should therefore work on any platform... whew!

So here's how it works -- this extension would likely be packaged into a .jar library, and deployed with a simple webapp or ear archive that is packaged separately from the main application... the piece that provides the configuration is actually a class or a set of classes that are annotated to bind certain fields and/or methods into JNDI, like this:

public class Env {
@Inject @Bind(jndiAddress="adminUser")
private User admin;

@Bind(jndiAddress="test") private String test = "This is a test";

Pretty straight forward -- what's going on here? Well, first you'll notice that the class is annotated with @EnvironmentBinding -- this is a Stereotype annotation that extends @ApplicationScoped, and acts as a marker for the class to be processed later on... further down, we have two fields that are annotated with @Bind and provided with a jndiAddress... this pretty much works as you would expect -- the value of that object is injected into JNDI, with the 'java:global/' prefix added to the front...

You'll also notice that one of the elements has its' value injected into the field -- this means that the Objects that are bound into JNDI can be derived from a more complex application if need be, so the support that we have here goes well above and beyond the simple XML file configuration that I dealt with way back when...

So how does this thing work? Well, one implementation that I put together has a two part infrastructure to do the job... remember, the end user should never be exposed to the following two items -- the extent of their exposure into this library will be the two annotations shown above...

First, our Portable Extension class:

public class EnvironmentBindingExtension implements Extension {
private Set envBeans = new HashSet();
private BeanManager beanManager;

public void discoverEnvironmentBindingClasses(@Observes ProcessBean pb, BeanManager bm) throws Exception {
this.beanManager = bm;

Bean bean = pb.getBean();
Class beanClass = bean.getBeanClass();

Set sts = bean.getStereotypes();

for (Class st : sts) {
if (st.equals(EnvironmentBinding.class)) {
log.info("Found class annotated with EnvironmentBinding: " + beanClass.getName());


public Set getEnvBeans() {
return Collections.unmodifiableSet(envBeans);

public BeanManager getBeanManager() {
return beanManager;

This Extension class is pretty straight forward -- as with all Portable Extensions, it starts by implementing the 'Extension' interface... in this case, we're also creating an Observer method for the 'ProcessBean' event... this event is fired during the application startup lifecycle for every 'Bean' that is discovered in a Bean archive... this will fire for Managed Beans, EJB's, Interceptors, etc, but here, we're specifically looking for beans that have the EnvironmentBinding Stereotype on them -- that is the trigger to further process this class... in this case, our process simply consists of adding the Bean to our 'envBeans' Set for later use... in addition, we provide accessor methods for the BeanManager (which is injected into our Observer method), and the envBeans Set... Now let's have a look at what we do with these Beans...

The next class is the one that does most of the heavy lifting -- it is a Singleton EJB which is marked as a Startup bean, meaning it will be instantiated upon application startup, after the CDI discovery phases are complete... in this case, we have created a PostConstruct method to do our work for us:

public class BindingsProcessor {
private EnvironmentBindingExtension bindingExtension;

public void processBindings() throws Exception {
Set envBeans = bindingExtension.getEnvBeans();

log.info("Processing EnvironmentBinding Classes: "+envBeans);

Context appContext = bindingExtension.getBeanManager().getContext(ApplicationScoped.class);
for(Bean bean:envBeans) {
Class beanClass = bean.getBeanClass();

Object beanInstance = appContext.get(bean, bindingExtension.getBeanManager().createCreationalContext(bean));

Field[] fields = beanClass.getDeclaredFields();
for(Field field:fields) {
if(field.isAnnotationPresent(Bind.class)) {

String jndi = field.getAnnotation(Bind.class).jndiAddress();
Object val = field.get(beanInstance);

bindValue(jndi, val);

Hey, wait a minute -- this is pretty simple, too! Iterate over the set of Beans that we've collected, use reflection to find all of the fields that are annotated with @Bind, and bind the value into the appropriate JNDI location... I've even removed the JNDI api work here, because it's not interesting at all...

This could be expanded in a couple of way, most obviously to allow methods to act as Binders as well... I do want to discuss my choice here of using the Singleton EJB as well, since I've had a few posts recently which talk about doing away with EJB's altogether -- well, initially I was attempting to use the 'AfterBeanDiscovery' or 'AfterDeploymentValidation' events to trigger this loading, but I was having trouble getting an instance of 'Env' that was capable of having its' injection points... er... injected...

The Singleton EJB is somewhat of a last-ditch sanity effort, but after considering it for a few days, I'm actually alright with it... the Startup Singleton EJB's are something that has interested me for a while, and it proves its' usefulness here, but what's more, I'm still able to take the EJB interface out of the end-user's experience here... they simply need to make use of the EnvironmentBinding annotation, and be on their merry way, as long as they are deployed in a container which supports Singletons (which all Java EE 6 containers do)... that being said, I'm hoping that Gavin will show me what the heck I was doing wrong :)

One other thing -- using an @Inject method on an ApplicationScoped bean doesn't appear to do the trick... reading the spec, it appears to be caused by the fact that ApplicationScoped beans are 'active' during Servlet calls, EJB calls, etc -- meaning it doesn't have it's own 'startup' lifecycle, but depends on the lifecycle of other Java EE component models... interesting, to be sure -- adding a more generic Startup capability would be a cinch if done similar to how I've done this...

Wow, that was a lot of words

So what does this all mean? Basically, it just shows another way of skinning that old, damn cat that is environment configuration -- but it also shows that it's pretty darn easy to put together some CDI extensions, and when working with the surrounding Java EE specs and resources, that it can be done in a minimal amount of code... in this case, I was looking at a requirement that I often have to support external configuration -- one that CDI doesn't accommodate out of the box... with a few lines of code, it turned out to be possible to break that box open and stuff some more toys inside :)

Finally, the more complete code samples can be found here -- the EnvironmentBinding project has the core code, the TestEnvironmentConfig project shows a test web application that could be used to create the binding configuration, and the EnvTest project is an application which makes use of the JNDI entries... have fun!


Saturday, December 12, 2009

JSR-299 Tx Interceptor code + JAX RS Sample

Quick update -- I've thrown the source code to the Transactional Interceptor from my previous post into a source repository, along with a sample webapp that shows how it can be used... they're both maven projects, and should be easy enough to download and fiddle around with -- I've tested it in Glassfish v3, but it should work in JBoss just as well...

I'm going to use this repository for a number of random tests, experiments, and whatever it is I'm fiddling with at the time -- hopefully there'll be a few more CDI projects up there, or whatever else it is that strikes my fancy at the time :)

Update: I've just thrown together a sample that shows how CDI can be combined with a JAX RS interface as well... cool, and simple!


Wednesday, December 9, 2009

Thoughts on JSR 330

I'm not sure what to think about JSR-330 -- what do you think?

A quick caveat before I begin -- I'm playing devils advocate here, so I have no idea if I believe any of the gibberish that I'm about to write :)

On one hand, I think it's Mostly Harmless... CDI/JSR-299 is obviously the default implementation of the spec, and perhaps having a very small portion of the DI capability abstracted so that we have the choice to use another framework if we want isn't necessarily a bad thing (even if standardizing injection targets isn't exactly ground breaking value) -- after all, if it weren't for those 'outside' framework writers, we might not have gotten EJB 3, and all the improvements that came along with Java EE 5... perhaps JSR 330 is a way to bring those folks inside of the Java EE fold, so it's not another three years before the standards catch up with the private innovation

On the other hand, is it at all likely that an application would be able to just replace the DI capability of CDI with, say, Spring or Guice, but still use the contexts, interceptors, extensions, etc? After all, Dependency Injection is actually a pretty small part of what the CDI spec provides... Perhaps, if one were to write a CDI Portable Extension that basically vetos all bean discovery, and instead uses one of the other libraries to do the injection, but that seems like a stretch to me...

What seems more likely is that another implementation would simply not use CDI, which can easily be done by not providing a beans.xml file, and using some other mechanism, like the Spring WebApplicationContext -- in that case, is there value in what the JSR-330 spec provides? Has the addition of this spec, which provides little value in-and-of itself incorporated too much confusion?

My original instinct was that it's a good addition to the Java EE fold, but I've heard a few voices of opposition (including from those who don't have a big stake in the outcome)... now I think, perhaps, that I could take it or leave it -- I haven't been convinced that it's destructive, but I'm also not convinced that it's worth it :)

What do y'all think?


Monday, December 7, 2009

CDI and Declarative Transactions...

Ok, so since I'm already on the books for declaring that CDI/JSR-299 will become the dominant framework of the Java EE standards, while pushing EJB 3 off to the side, let's start to figure out how this could work... I don't think too many people would argue that the number one reason for using EJB 3.x Session beans are the Declarative Transaction support -- that is, being able to create and commit a new Transaction around a method call by annotating it with @TransactionAttribute(TransactionAttributeType.REQUIRED) -- and compared to the boilerplate code that needs to be put in place to do it programatically, it is indeed a pretty nice feature...

So how can this be done in CDI? Simple -- with Interceptors... CDI actually reuses the interceptor support from EJB 3, but it takes it a step further -- it allows you to use a custom annotation to bind your code to the interceptor definition in a pretty slick way... but first, consider the following interceptor, which might be the most basic implementation of Declarative Transactions:

//import removed...

@RequiredTx @Interceptor
public class RequiredTransactionInterceptor implements Serializable {
private UserTransaction utx;

public Object openIfNoTransaction(InvocationContext ic) throws Throwable {
boolean startedTransaction = false;
if(utx.getStatus() != Status.STATUS_ACTIVE) {
startedTransaction = true;

Object ret = null;
try {
ret = ic.proceed();

} catch(Throwable t) {

throw t;

return ret;

You'll notice that most of this looks familiar -- we're not implementing any particular Interface, we can use any old name for our wrap-around method, we use the @AroundInvoke annotation to indicate which method is to be used for intercepting, and how it's used, etc. The only thing new here comes on the first line -- the dual annotations of @RequiredTx and @Interceptor... so far this is wicked easy!

So what the heck are those annotations? Well, the @Interceptor annotation is part of the javax.interceptor package, and was added as part of the CDI spec -- clever how they snuck a new annotation in but didn't put it under any CDI-specific package, eh?

The other annotation is one of my own, and when used in concert with @Interceptor, it acts as the 'binding definition' of the interceptor -- you can then put the @RequiredTx annotation on any method or class of any CDI bean, and it will be as if you've annotated an EJB with '@Interceptors(RequiredTransactionInterceptor.class)', only not as wordy (and why would you write a transactional interceptor that mimics the EJB usage, and then use it on an EJB?)

Without further ado, the definition of our annotation:

//more imports removed...

@Target({ElementType.METHOD, ElementType.TYPE})
public @interface RequiredTx {

Not much here -- the only thing you may not have seen before is that InterceptorBinding annotation, which simply declares that this annotation is used to bind an interceptor to a method or class... simple -- but how is it used? This is the cool part -- just annotate any old CDI bean with this, and you're good to go -- like this:

//yep -- no imports here either...

public class UserGetterer {
public User loadUser() {
\\Do some fancy JPA loadin' here

Load that up in your webapp, point your fancy JSF 2.0 preRenderView event to "#{userGetterer.loadUser()}", and voila -- you are loading your data in a transaction... sweet!

Doh -- almost forgot
What's that? It didn't work? Crap... oh wait, one last thing -- we need to enable our interceptor... This part is easy to forget, but easy to do -- you need to add an <interceptors> element to your beans.xml... (you did add the beans.xml file, right?)... try this:


Yeah, I know -- it feels kind of like we're exposing your implementation detail by asking the user to enable the interceptor instead of the annotation... kind of annoying, but I can deal with it... another thing to note is that if you pack your interceptors into a shareable library file, all other library files in your application will still need to do this, even if you enable it in your .jar file -- that's because there's no concept of a global interceptor in CDI, but to be safe, each library is forced to enable all interceptors in use, so that they can enforce the ordering of interceptors in a very intuitive manner... I can accept this reasoning simply because they have defined it, rather than letting it be an undefined mess, so again, it's more of annoyance for me -- hopefully there will be other options in the future... (Note - due to a bug in the current version of Weld, Glassfish and JBoss will both have a problem with this -- the work around at the moment is to include all of your <interceptors> definitions in one library file, or your .war file directly)

Sum It Up
So there you have it -- a bare minimum implementation of Declarative Transaction support for CDI that can be applied to both classes or methods in less than 40 lines of code... easy, and very reusable! It would be similarly easy to implement for the other EJB TransactionAttributeTypes (SUPPORTS, REQUIRES_NEW, etc), although there is some coordination to be done to support putting one annotation on a class and another on a method without having them tripping over each other...

All in all, it provides a very straight forward programming model that is at least as clean as the EJB 3.0 model -- cleaner in many ways, actually, as you'll have @RequiredTx instead of @Interceptors(RequiredTransactionInterceptor.class), better context management with @RequestScoped, @SessionScoped and @ConversationScoped instead of just @Stateful, and much better integration with JSF... Plus, you'll have the benefit of being able to customize it if you need to, adding support for things like timeouts, isolation levels, or custom logging... very nice, indeed!


Wednesday, December 2, 2009

CDI/JSR-299 preparing to marginalize EJB... again...

Whew, it's been a while since I've started looking at what's new in Java EE 6, partially because I've been fiddling with the newest addition to the Java EE portfolio -- JSR-299, aka CDI, f/k/a Web Beans, and this one could be big... Let's start with a bang -- CDI will eventually push EJB to obscurity... yeah, big statement, I know, but here's why I believe this will happen:
  • CDI has the potential, I believe, to provide all of the most commonly used services that the EJB spec provides, and let's face it, this mean transactions, timers, asynchronous process with MDB's, and now Singleton Startup beans. It does not mean remoting, instance pooling, and most certainly not any of the old CORBA crap :) -- also note that I'm not referring to JPA here at all, now that it has been removed from the EJB spec (as it should be!)
  • The CDI spec comes in at 98 pages.
  • The EJB 3.1 spec (PFD version), comes in at 618 pages -- yikes!
  • The CDI spec is far more flexible than the EJB spec, allows for more interesting and transparent customizations, and is built specifically to support extensions.
  • CDI's mechanisms for injection (which it inherits from JSR-330, thanks to Rod and Bob!), is more consistent, inclusive and unified than EJB's/JEE 5's.
  • CDI's inclusion of Stereotypes provides an awesome mechanism to create a lightweight framework, without directly exposing any CDI api to the end user.
  • CDI's context support is very extensive, and is far more complete and useful than EJB's Stateless and Stateful beans. Plus, it finally gives us a standard between Request and Session scoped.
  • CDI's integration with JSF is far better than EJB's (ever had a manually remove a Managed Bean from scope because its' Stateful bean has completed its' lifecycle? Come on!).
  • CDI's event mechanism provides for extensibility and easy, lightweight asynchronous processing.
  • There's no such thing as CDI Lite :)
That's not a short list, and admittedly, it's very early to make claims like this... after all, the spec has just finally been approved, we have yet to fully see the quality of the implementations (three that I know about, Weld, CanDI and OpenWebBeans -- hey, how come they got to keep the name!?), we have yet to see how the closed-source vendors will handle it, etc., but when it comes down to it, the CDI spec is simply better suited for POJO-style development, using Object Oriented core design principals, and it was designed specifically with this in mind... Contrast this with EJB, which has over a decade of cruft to carry along with it, and actually prevents the use of OO techniques altogether (historically more-so than today, but it's still an issue), and I think the future becomes fairly clear... All-in-all, I see it as great news for the Java EE platform...

One final caveat, though -- EJB has risen from the ashes before, specifically with the 3.0 version... if the vendors still feel the need to continue to push EJB, then who knows what we'll see :)


Monday, August 17, 2009

Nice NetBeans/Maven integration

Just found this pretty cool feature in the latest 6.8 M1 release of NetBeans... when working with Maven project, you can, of course setup dependencies on other Maven libraries, but 'native' NetBeans projects don't have this ability -- you can only setup dependencies to other Projects, to 'Libraries', and to random .jar files... however, NetBeans now has the ability to create Libraries from Maven libraries, and then use those in any projects... check this out...

Open the Maven Repository Browser (Window -> Other -> Maven Repository Browser if you haven't seen this before), and find a project you're interested in by either browsing or searching... once you're here, right click on the version you want, and select 'View Details', like so:

This brings up the details of the .pom file, which has all sorts of good stuff, but the cool part is the icon of the stack of books in the upper right -- click this, and it will automatically download the project and set it up as a NetBeans Library, for use in any project... pretty sweet!

Of course, this may have been here all along -- if anyone knows of a prior version of NetBeans that this works with, chime in!


Thursday, August 6, 2009

What's cool in Java EE 6 -- Singleton EJB's and Pruning

Well, with the all the excitement about the addition of JSR 330 Dependency Injection into the EE 6 platform, I figured I'd switch gears a bit and talk about the changes in a different spec -- this time, the EJB spec... Now, this spec is enormous -- 618 pages to be exact -- but luckily, we can ignore most of it! I mean seriously, who wants to read about EJB QL, 2.1 Client Views and the like... I'd rather dig into the meatier stuff, like...

Singleton Session Beans
We all know the Singleton pattern -- it may be one of the most well known designs patterns, and it's easy to understand... basically, if something's a singleton, then there's only one of 'em... ever... that means if you lose it or break it, it's gone for good... ok, that last part isn't true, and really the parts before that aren't quite true, either -- in most implementations of the Singleton pattern, folks conveniently forget to account for the fact that Enterprise applications often live in clusters, and instead they make singletons so that there is only one per virtual machine... most of the time this is fine -- they're often used for things like Service Locators, which were used back in the olden days of EJB 2 as a convenient way to do all the nasty JNDI lookups for EJB's...

Enter Singleton Session Beans... these are pretty much what you'd expect them to be -- an EJB that you can only find one of per JVM (the spec is specific about that last part, so really, they're only Singletons part of the time)... frankly, I was somewhat underwhelmed when I first heard of them, but they do add one interesting capability that I've needed on several EJB applications in the past, and have never had in an easy, portable way to pull off -- the ability to execute on application startup...

This can be achieved with a few simple annotations, like so:

public class InitBean {
public void initializeSomeStuff() {

I know, lot's of annotations on that sucker, but it's pretty straight forward:
  • @Singleton -- defines this as a Singleton Session Bean... big surprise here, eh?
  • @Startup -- this is an indication to the app server that this class must be created and initialized at application startup, and yes, the spec uses the 'must', so there's no soft language here
  • @PostConstruct and @TransactionAttribute -- these are the old tags that we all know and love...
Anyone who's read this blog before may notice a hidden gem in here that makes all of this worthwhile... I'll give you a hint -- go read this entry on @PostConstruct... yes, that's right, this is a Transactional @PostConstruct method... and it will work! They got this one right when they wrote the EJB 3.1 spec, in that it guarantees that @PostConstruct methods will obey your @TransactionAttribute attributes! Now if only they would extend this capability to all EJB's, we'd have all of our problems solved! Ok, perhaps not all of them...

So anyway, you can do pretty much whatever you want in here, including initializing 3rd party systems, creating timers, sending email, truncating all of your database tables, etc... what most intrigues me, though, is data bootstrapping, similar to what you get with the Grails Bootstrap class:


public void setupUserData() {

List expectedUsers = getExpectedUsers();

for (User u : expectedUsers) {
User found = em.find(User.class, u.getId());
if (found == null) {
log.info("Persisting user "+u.getUsername());
} else if (!found.equals(u)) {
log.info("Updating user "+u.getUsername());
} else {
log.info("No need to update user "+u.getUsername());


In other words, this Singleton will ensure that your standard set of users will always be available upon application startup -- if this is something you want only in a development environment, you can configure each machine with environment information via JNDI, System properties, or a configuration that is set in your build so that it only takes these actions on a development environment -- it is a great way to ensure a consistent development environment across your team...

A Word about Pruning
Ok, this is, by far, my favorite feature of Java EE 6 -- and it's not even a feature! That's right, we're finally going to be able to actually remove some of the stuff that's been added to Java EE over the years! This could include a wide variety of things, but just think -- some day in the near future, you won't have to sift through details about Entity Beans, outdated communication protocols like JAX-RPC, EJB Home interfaces... instead, it will all be gone! Unfortunately, this day hasn't come just yet -- they're just telling us that they're planning to do it some day, but at least it's a step in the right direction... now I have to wonder -- will Java EE 7 actually be a smaller spec than EE 6? Now that would be impressive :)

In the meantime, you can get a little hint of the pruning goodness by taking advantage of the new Web Profile -- this is a nice customization of the spec that removes a lot of the stuff that most folks don't use anyway, but that is still pretty useful in some cases... unfortunately, you still do have to thumb past all of the extra stuff if you decide to go diving into the spec documents...

It seems odd to me that it took so long for Java EE to standardize such a simple, but useful piece of functionality as startup logic -- to me, this is more important than the fact that this EJB is a (kind-of-)Singleton! Sure, I've always been able to do this through an eagerly loaded Servlet, or with WebLogic or JBoss specific code, but the Servlet solution is just plain weird if I'm not acting upon my web container, and this is obviously more portable than an app-server specific piece of functionality... the above example just seems a little less... dirty to me :)

But who am I to complain about a nice new feature :)


Tuesday, August 4, 2009

What's cool in Java EE 6 -- JSR 330?

Hey, interesting timing on this one, given my previous two entries this week -- a blog entry just caught my attention indicating that Java EE 6 is getting a late entry in the form of JSR-330 -- Dependency Injection for Java... I noticed this spec when it was first announced back in May or so, and immediately thought "Oh boy, a direct competitor to Web Beans! This is going to confuse things!"... don't forget, Web Beans was renamed at some point to 'Contexts and Dependency Injection for the Java EE Platform', so these specs appear to be very similar at first glance...

So what's going on here? I haven't the foggiest, but it is interesting that JSR-330 is headed up by Bob Lee and Rod Johnson, the masterminds behind Guice and the Spring Framework -- certainly a pair of heavy hitters in open source Java land... I seemed to recall that Bob Lee had some involvement with Web Beans as well, but he is not listed on the JSR-299 expert group, so it may well have been my imagination...

Another interesting blog by Roberto Chinnici has a little more detail, giving some background behind the decision and indicating that Web Beans will be modified to use the JSR-330 annotations -- I'm quite interested in his last sentence (well, second to last -- the "Stay tuned!" doesn't count), which is a vague reference to reconciling Web Beans with the rest of the spec, especially in regards to Managed Beans... I was actually wondering if anything was going to happen here -- early indications are that a Managed Bean can become a Web Bean by replacing the @ManagedBean annotation with @Named, so why have both, especially if both are included in the Web Profile? At first glance, they seem to both be extremely similar, with Web Beans simply adding a few pieces of functionality -- more digging needs to happen on my part here, for sure...

So what the heck does this mean? Well here's my initial impression:

  • This appears to be a good thing -- one of my pet peeves with the EE spec process is the tendancy to simply not make some hard decisions. This appears to be a pretty bold move, and may have prevented a situation where we have three distinct DI mechanisms in the standard (the original @EJB/@Resource/@PersistenceContext, the Web Beans @Current and @BindingType, and the JSR-330 @Inject and @Qualifier annotations)
  • It is an interesting turn of events for SpringSource, which has appeared poised to become a possible implementation of the Web Profile with their new tc Server, or even a full fledged app server with the dm Server -- this certainly seems to indicate the direction they plan to go
  • Gavin King must be pissed :)... ok, so I have no idea about this one, but it will be interesting to see how it turns out anyway...
  • We may end up with a slightly more cohesive web framework if, for example, Managed Beans and Web Beans are combined, but we have yet to see what direction this is going to go -- certainly interesting! If I'm correct in my understanding of the similarities between the two types of objects (and I may very well be incorrect at this point), one could make do by always using the Web Beans version, which makes @ManagedBean dispensable...
So my ears will definitely be open to movement in this space... nothing like an 11th hour addition to the spec which may cause changes in two or more existing and 'finalized' specs to make things interesting!


Saturday, August 1, 2009

More cool in Java EE 6 -- JSF 2.0, Bean Validation

There's definitely more cool stuff in JSF 2.0 that I didn't get to last time -- so let's get right to it...

Annotations for Converters and Validators
The @FacesConverter and @FacesValidator annotations are nice little additions to the framework... they're simple to use -- you write your converter and validator classes in the same way as you always did, but instead of needing to crack open your already-way-too-large-faces-config.xml and look up the syntax for the converter tags, you can now rely on your IDE to help you out -- just throw @FacesConverter on the class with the 'forClass' attribute, and it will automatically register that converter for all form fields of the appropriate class... it doesn't get much easier than that, and of course you can use the 'value' attribute in the same way you used to use the 'converter-id' tag in the faces-config.xml, and then reference it in any of your pages...

Ok, you're saying -- so I get to add a line to my class file and take four lines out of an xml file -- so what? But wait, there's more -- and this is my favorite part... throw an empty faces-config.xml file in your META-INF folder, and these converters and validators will automatically be registered for any JSF web app that includes this jar! This is a great mechanism to promote reuse -- create a utility component that has a bunch of these files, and every one of your web apps can get them with a couple of lines in a Maven .pom file (or whatever it is you use for your builds)... I suspect it won't take long for standard sets of converters and validators to become available in component libraries like RichFaces or IceFaces...

Bean Validation
Speaking of validation, here's where it gets interesting -- with the new Bean Validation integration, it's no longer necessary to think about validation just in terms of JSF... I personally believe that validation could be better approached as an integral part of a well designed domain model (think Domain-Driven Design here), so putting that logic in a JSF specific validator has always seemed awkward -- likewise, adding a 'validate()' method that needs to be called while processing a form submit or just before persisting your object (in the case of a JPA Entity, for example) is just as awkward, if not worse... not only that, but many applications are more than just web apps -- some have batch-processing or scheduled aspects to them which don't have any exposure to JSF... using JSF validators in these cases would be a bit smelly, and duplicating the code is even worse -- this is where Bean Validation comes in...

With the annotations in the javax.validation.constraints package, we can now annotate our domain fields with things like @Min, @Max, @NotNull, @Pattern, etc, so that the validation rules that our domain objects abide by are defined right along-side the fields themselves... there is, of course, an API available for evaluating the rules against a given object, but here's where it gets cool -- JSF and JPA both provide support for this automagically! That means whenever you have a domain object that is exposed through a ManagedBean in a form, the validation rules will be executed for you, and any failures are made available through the standard h:message tags! Not only that, but the validation can also be executed when you persist or merge your domain object via JPA, so batch-processing applications are covered as well -- very cool...

A very similar incarnation has been available through the Hibernate Validation and Seam projects, but it is great to see this get worked into the standards... The basic constraints here are lean, but take care of most use cases -- I have experimented with some simple custom validators, but am looking forward to getting into some more complex rules with multiple fields and see how it handles them...

Still more?
There's still more stuff in EE 6 that I'd like to talk about, including the Facelets support and the new Composite Component stuff, but I need to do a little more fiddling with that -- I came across the new 'Behavior' API as well, which looks like a slick way to make your components do what you want, as well as make them extensible... At some point, I'm going to get my hands on Web Beans, the new JPA 2.0 capabilities, Singleton EJB's, Asynchronous EJB's, etc -- there is a lot of good stuff in this new spec, so stay tuned!


Wednesday, July 29, 2009

What's cool in Java EE 6 -- JSF 2.0

Howdy -- been a while since I've been around here, but I've been fiddling with some of the new technologies in Java EE 6, and figured that this is as good a place as any to start to talk about them :)...

First off, I've been working with NetBeans 6.7 and Glassfish v3 Preview (look here for info on how to get the two to work together)... I have plenty of experience with NetBeans, but less with Glassfish -- ever since it took forever for JBoss 5 to come out, it has been my App Server of choice for 'fiddling' with technologies... it's really quite impressive, and seems to be one of the places for early access into new technologies...

Anyway, there is plenty of new stuff in EE 6, and one of the areas that shows a ton of improvement is JSF 2.0... Where to start?

So long, faces-config.xml!
Well the most obvious starting point has to be the Annotations... in old-school JSF, any sizable application needed to have every Managed Bean registered in an XML file, which is fine for the smaller examples, but quickly gets to the point of needing to use an appropriate tool to decipher and organize it... Thankfully, this has been replaced by a single (sometimes two) annotations directly on the class, like so:

public class LoginBean {

And no discussion of XML in JSF would be complete without mention of the navigation... not only was this too verbose, but it never struck me as an appropriate place to store navigation information -- after all, I already have my Managed Beans and XHTML pages (we've all been using Facelets for years now, right?) -- why do I need to look in a third place to configure what page an action in a page takes us? Again, manageable, but a bit smelly...

Once again, JSF 2.0 makes an improvement here by allowing us to return the names of our views directly from our Managed Beans -- so instead of returning "Success", and then having to indicate what that really means in another file that's too big as it is, I can just return "success.xhtml", and the page will be found for me (in fact, I believe a return of "success" would have the .xhtml added for me, but for some reason the explicit version seems more palatable to me)... and if I want this to be a redirect, it becomes "success.xhtml?faces-redirect=true" -- admittedly a bit awkward, but simple and easy to remember...

Ok, this is the really big one -- frankly, I am stunned that it has taken, what -- 8 years for JSF to add this feature natively (I apologize if that number is wrong, I'm not up to looking up the JSF history right now)... in the past, I have created my links with standard html '<a href' tags and used a @PostConstruct tag in my Managed Bean to load whatever data I needed, but frankly it's a hack, and the fact that @PostConstruct operates in an undefined transactional state forces you to jump through hoops, not to mention that circumventing the JSF View lifecycle can be difficult at times... In fact, I was actually quite concerned in the later days of the spec development, because it appeared that this feature was not being taken seriously by the spec group -- that Ajax and skinning were more important features than perhaps one of the most basic features of any browser... blog entries by those with 'inside information' were coming out as late as early 2009 that seemed to gloss over the demand for bookmarkable features, but thankfully it made it in at the last moment... whew!

There are a couple of items that are important here, but the basic idea is that any page that can be bookmarked (and that requires info from the query string) uses three new tags -- f:metadata, to define metadata for a view, f:viewParam, which can take a parameter from the query string and store it in a variable (including conversion, validation, etc), and f:event, which can be used to register an event listener on the new PreRenderViewEvent -- it might look something like this:

<f:viewParam name="id" value="#{editBean.id}" />
<f:event type="preRenderView" listener="#{editBean.load()}"/>

Alternately, you could simply use the old c:set tag to set the variable in the page, however this appears to store a copy of the variable, which doesn't work out well if you are later editing or modifying it...

Finally, to create a link to your new-fangled page, there is also the new h:link tag, which follows the same direct navigation rules mentioned above, and that can support the f:param tag to easy include parameters -- easy peasy!

To Be Continued...
Ok, this turned out to be longer than expected -- I still want to talk about PDL, Events, Annotations for Converters and Validators, Bean Validation integration, Web Beans integration, and plenty more, but that will have to wait for a later date...

The bottom line is that yes, JSF is a viable web framework -- it has had its' problems historically, but the latest version seems to have addressed them quite effectively, and there are plenty of component libraries out there like RichFaces, IceFaces, etc., that can make your apps look and operate great... Hopefully it can shed some of its' poor reputation (much of it deserved, some of it not) -- it's definitely worth a look!