Showing posts with label Seam. Show all posts
Showing posts with label Seam. Show all posts

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
*/
@EnvironmentBinding
@DataSourceDefinition (
className="org.apache.derby.jdbc.ClientDriver",
name="java:global/jdbc/AppDB",
serverName="localhost",
portNumber=1527,
user="user",
password="password",
properties={"create=true"},
databaseName="dev-db"
)
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'...

M


Monday, September 24, 2007

Web Beans Sneak Preview

In case you didn't notice, Gavin King is offering a sneak preview of the Web Beans spec (JSR 299) so far in part 1, part 2, part 3 and part 4... It's been a whole day since his last post, so it appears like now is a good time to put out some thoughts... These are in no particular, but will hopefully serve to continue the conversation to make the eventual spec as useful as possible...
  • Web Beans is not just about the web, as many people have already commented (including Gavin)... while I don't necessarily need it to be brought to JSE, I believe a name change is in order...
  • If Web Beans becomes a generic Dependency Injection mechanism, then we would be left with at least three distinct DI systems in JEE -- the Web Beans DI, the @EJB/@Resource DI from JEE 5, and the Managed Bean mechanism from JSF... something will need to be done to address this (please don't say 'Unified DI', a la 'Unified EL' :) )...
  • The @In annotation clearly comes from Seam, but there might be something more appropriate here -- in Seam, there is also an @Out annotation, representing 'outjection'... So far, Gavin hasn't given us a hint of whether this will be around in Web Beans, but if not, then I think @In should become something like @Inject, @Bean, @Component, etc
  • The Guice influence appears to have replaced the String-based naming in Seam (i.e. -- @Name("myBean") ) with annotations (@MyBean)... while this requires a little more code, it seems to be a good call -- it should help prevent things like typo's in the Strings causing NullPointerExceptions at Runtime...
  • Very flexible -- follows the JEE5 pattern of defining a default configuration with annotations, but allowing it to be overridden or changed with an XML configuration... alternately, the XML configuration could be used as a central configuration file, as in Spring...
  • The various Component Types could be hugely useful for having multiple configurations for a single application, but I would like to see more ways of defining your components -- for example, I like to be able to keep my system configurations in the app server itself, so when I deploy an EAR from test to staging to production, I can simply copy an EAR that has passed QA -- if I have to change the web-beans.xml file, I can't do this (unless there is perhaps a default that you could configure in your server)
  • I love the scopes, but Gavin mentions that the @ConversionScoped will be available from JSF managed beans, while request, session and application will be available from Servlets -- what about JSF makes it the only thing that is appropriate for the conversation scope? Any reason it couldn't be available to Servlets-based frameworks like Struts, etc.?
  • Could we reduce some of the annotation chatter with something like @In (scope=ConversationScoped, new=true)? Hmm... actually that ends up with more chatter -- perhaps not :)
That's enough for now... So far all I can say is "Great work guys!"... oh, and keep the previews coming!

M