[ contact ] [ home ] [ search ] [ submit link ] login | want to join? register in seconds!

home and garden
lawyers reviews
cosmetic surgery
cosmetic surgery cost / price site
channels:
hot tags: [all tags...]
hot tags(2): [all tags...]
[all tags...]
InfoQ Introduction to JBoss Seam
tech related articles:
0
vote!
All AJAXWorld Sponsorships Sold-Out ... (ajax.sys-con.com)
crawler @ 03/30/07 12:36 comments(0) report
0
vote!
OASIS announces an initiative to standardize SCA as OCSA (www.theserverside.com)
crawler @ 04/19/07 21:31 comments(0) report
0
vote!
Jakarta Commons Configuration 1.4 (www.theserverside.com)
crawler @ 04/21/07 19:59 comments(0) report
0
vote!
Faban, another open source web load generator (www.theserverside.com)
crawler @ 04/21/07 19:59 comments(0) report
0
vote!
BarracudaDrive � a web server including Bindows/Lua/AJAX (www.theserverside.com)
crawler @ 04/21/07 19:59 comments(0) report
0
vote!
Remember to register for TSSJS-Europe this week! (www.theserverside.com)
crawler @ 04/19/07 21:31 comments(0) report
InfoQ: Introduction to JBoss Seam
"Seam collapses the artificial layer between EJB3 and JSF. It provides a consistent, annotation-based approach to integrate EJB3 and JSF. With a few simple annotations, the EJB3 business components in Seam can now be used directly to back JSF web forms or handle web UI events. Seam allows developers to use the "same kind of stuff", annotated POJOs, for all application components. Compared with applications developed in other web frameworks, Seam applications are conceptually simple and require significantly less code (both in Java and XML) for the same functionalities. If you are impatient and want a quick preview of how simple a Seam application is, you can have a look at the hello world example described lower in this article. "

"Seam also makes it easy to accomplish tasks that were "difficult" on JSF. For instance, one of the major complaints of JSF is that it relies too much on HTTP POST. It is hard to bookmark a JSF web page and then get it via HTTP GET. Well, with Seam, generating a bookmarkable RESTful web page is very easy. Seam provides a number JSF component tags and annotations that would increase the "web friendliness" and web page efficiency of JSF applications. "

"At the same time, Seam expands the EJB3 component model to POJOs and brings the stateful context from the web tier to the business components. Furthermore, Seam integrates a number of leading other open source frameworks such as jBPM, JBoss Rules (a.k.a Drools), JBoss Portal, JBoss Microcontainer etc. Seam not only "wire them together" but also enhance the frameworks in similar ways it does to the JSF + EJB3 combination."

"While Seam is rooted in Java EE 5.0, its application is not limited to Java EE 5.0 servers. Your Seam applications can be deployed in J2EE 1.4 application servers as well as in plain Tomcat servers. That means you can obtain production support for your Seam applications today!"

" It would be a mistake to think that Seam is just another integration framework that wires various frameworks together. Seam provides its own managed stateful context that allows the frameworks to deeply integrate with others via annotations, EL (Expression Language) expressions etc. That level of integration comes from Seam developer's intimate knowledge of the third party frameworks. "

"Object Relational Mapping (ORM) solutions are widely used in today's enterprise applications. However, most current business and web frameworks are not designed for ORM. They do not manage the persistence context over the entire web interaction lifecycle from the request comes in to the response is fully rendered. That has resulted in all kinds of ORM errors included the dreaded "

"Seam was invented by Gavin King, the inventor of the most popular ORM solution in the world (Hibernate). It is designed from the ground up to promote ORM best practices. With Seam, there is no more DTOs to write; lazy loading just works; and the ORM performance can be greatly improved since the extended persistence context acts as a natural cache to reduce database round trips. "

"Furthermore, since Seam integrates the ORM layer with the business and presentation layer, we can display ORM objects direct, you can even use database validator annotations on input forms , and redirect ORM exceptions to custom error pages."

"Seam is designed for stateful web applications. Web applications are inherently multi-user applications, and e-commerce applications are inherently stateful and transactional. However, most existing web application frameworks are geared toward stateless applications. You have to fiddle with the HTTP session objects to manage user states. That not only clutters your application with code un-related to the core business logic, but also brings on an array of performance issues."

"In Seam, all the basic application components are inherently stateful. They are much easier to use than the HTTP session since their states are declaratively managed by Seam. There is no need to write distracting state management code in a Seam application -- just annotate the component with its scope, lifecycle methods, and other stateful properties -- and Seam takes over the rest. Seam stateful components also provide much finer control over user states than the plain HTTP session does. For instance, you can have multiple "conversations", each consisting of a sequence of web requests and business method calls, in a HTTP session. "

"Furthermore, database caches and transactions can be automatically tied with the application state in Seam. Seam automatically holds database updates in memory and only commits to the database at the end of a conversation. The in-memory cache greatly reduces database load in complex stateful applications. "

"In addition to all the above, Seam takes state management in web applications a big step further by supporting integration with the Open Source JBoss jBPM business process engine. You can now specify the work flows of different people in the organization (i.e., customers, managers, technical support etc.) and use the work flow to drive the application, instead of relying on the UI event handlers and databases. "

"Seam is fully optimized for Web 2.0 style applications. It provides multiple ways for AJAX (Asynchronous JavaScript And XML, a technology to add interactivity to web pages) support -- from drop-in JavaScript-less AJAX components, AJAX-enabling existing JSF components, to a custom JavaScript library that provide direct access to Seam server components from the browser as Javascript objects. Internally, Seam provides an advanced concurrency model to efficiently manage multiple AJAX requests from the same user."

"A big challenge for AJAX applications is the increased database load. An AJAX application makes much more frequent requests to the server than its non-AJAX counterpart does. If all those AJAX requests have to be served by the database, the database would not be able to handle the load. The stateful persistence context in Seam acts as an in-memory cache. It can hold information throughout a long running conversation, and hence helps to reduce the database round trips."

"Web 2.0 applications also tend to employ complex relational models for its data (e.g., a social network site is all about managing and presenting the relationships between "users"). For those sites, lazy loading in the ORM layer is crucial. Otherwise, a single query could cascade to loading the entire database. As we discussed earlier, Seam is the only web framework today that supports lazy loading correctly for web applications."

"Seam is a "lightweight framework" because it promotes the use of POJO (plain old Java objects) as service components. There are no framework interfaces or abstract classes to "hook" components into the application. The question, of course, is how do those POJOs interact with each other to form an application? How do they interact with container services (e.g., the database persistence service)?"

"Seam wires POJO components together using a popular design pattern known as "dependency injection" (DI). Under this pattern, the Seam framework manages the lifecyle of all the components. When a component needs to use another, it declares this dependency to Seam using annotations. Seam determines where to get this dependent component based on the application's current state and "injects" it into the asking component."

"Expanding on the dependency injection concept, a Seam component A can also create another component B and "outjects" the created component B back to Seam for other components, such as C, to use later."

"This type of bi-directional dependency management is widely used in even the simplest Seam web applications (e.g., the hello world example in Chapter 2). In Seam terms, we call this "dependency bijection"."

"The key design principal that makes Seam so easy to use is "Configuration by exception". The idea is to have a set of common-sense default behavior for the components. The developer only needs to configure the component explicitly when the desired behavior is not the default. For instance, when Seam injects component A as a property of component B, the Seam name of component A defaults to the recipient property name in component B. There are many little things like that in Seam. The overall result is that configuration metadata in Seam is much simpler than competing Java frameworks. As a result, most Seam applications can be adequately configured with a small number of simple Java annotations. Developers benefit from reduced complexity and, in the end, much less lines of code for the same functionalities developed in competing frameworks."

"As you probably noticed, Java annotations play a crucial role in expressing and managing Seam configuration metadata. That is done by design to make the framework easier to work with."

"In the early days of J2EE, XML was viewed as the "holy grail" for configuration management. Framework designers throw all kinds of configuration information, including Java class and method names, in XML files without much thought about the consequence to developers. In retrospect, that was a big mistake. XML configuration files are highly repetitive. They have to repeat information already in the code in order to connect the configuration to the code. Those repetitions make the application prone to minor errors (e.g., a mis-spelled class name would show up as an hard-to-debug error at runtime). The lack of reasonable default configuration settings further compounds this problem. In fact, in some frameworks, the amount of boilerplate code disguised as XML may rival or even exceed the amount of actual Java code in the application. For J2EE developers, this abuse of XML is commonly known as the "XML hell"."

"The enterprise Java community recognizes this problem with XML abuse and has very successful attempts to replace XML files with annotations in Java source code. EJB3 is the effort by the official Java standardization body to promote the use of annotations in enterprise Java components. EJB3 makes XML files completely optional, and it is definitely a step toward the right direction. Seam adds to EJB3 annotations and expands the annotation-based programming model to the entire web application."

"Of course, XML is not entirely bad for configuration data. Seam designers recognize that XML is well-suited to specify web application pages flows or define business process work flows. The XML file allows us to centrally manage the work flow for the entire application, as opposed to scatter the information around in Java source files. The work flow information has little coupling with the source code -- and hence the XML files do not need to duplicate typed information already available in the code."

"Seam is designed from ground up for easy testing. Since all Seam components are just annotated POJOs, they are very easy to unit test. You can just create instances of the POJOs using the regular Java "

" keyword and then run any methods in your testing framework (e.g., JUnit or TestNG). If you need to test the interaction between multiple Seam components, you can instantiate those components individually and then setup their relationships manually (i.e., use the setter methods explicitly instead of relying on Seam's dependency injection features)."

"For integrated testing of the entire Seam application, it is a little more complex since you have to run the application inside a Seam container. Seam comes with an embeddable lightweight container that helps this type of testing. In your test framework, you can load the Seam container programatically and then run the tests."

"Tools support is crucial for an application framework that focuses on developer productivity. Seam is distributed with a command line application generator called Seam Gen. Seam Gen closely resembles the tools available in Ruby-On-Rails. It supports features like generating complete CRUD applications from a database, quick developer turn around for web applications via the "edit / save / reload browser" actions, testing support etc."

"But more importantly, Seam Gen generated projects work out-of-the-box with leading Java IDEs such as Eclipse and NetBeans. With Seam Gen, you can get started with Seam in no time!"

"In a nutshell, Seam simplifies the developer overhead for Java EE applications, and at the same time, adds powerful new features beyond Java EE 5.0. In this next section (excerpted from chapter 2 in the book), we will show you some real code examples to illustrate how Seam works.  You can find the source code download for all example applications in the book from the book web site "

"The most basic and widely used functionality of JBoss Seam is to be the glue between EJB3 and JSF. Seam allows seamless (no pun intended!) integration between the two frameworks through managed components. It extends the EJB3 annotated POJO (plain old Java objects) programming model to the entire web application. There is no more artificially required JNDI lookup, verbose JSF backing bean declaration, excessive facade business methods, and painstakingly passing objects between tiers etc."

" In traditional Java EE applications, some design patterns, such as JNDI lookup, XML declaration of components, value objects, business facade, are mandatory. Seam eliminates those artificial requirements with annotated POJOs. However, you are still free to use those patterns when they are truely needed in your Seam applications."

"Writing a Seam web application is conceptually very simple. You just need to code the following components:"

"All the above components are managed by Seam and they are automatically injected into the right pages / objects at runtime. For instance, when the user clicks a button to submit a JSF form, Seam automatically parses the form fields and constructs an entity bean. Then, Seam passes the entity bean into the event handler session bean, which is also created by Seam, for processing. You do not need to manage component lifecycles and relationships between components in your own code. There is no boilerplate code and no XML file for dependency management."

"In this chapter, we use a hello world example to show exactly how Seam glues together a web application. The example application works like this: The user can enter her name on a web form to "say hello" to Seam. Once she submits, the application saves her name to a relational database and displays all the users that have said hello to Seam. The example project is in the "

" folder in the source code download for this book. To build it, you must have Apache ANT 1.6+ (http://ant.apache.org/) installed. Enter the "

" To run examples in the book, we recommend you to use the JEMS (JBoss Enterprise Middleware Suite) GUI installer to install a Seam-compatible version of JBoss AS. The JEMS installer can be downloaded from "

"You are welcome to use the sample application as a template to jump start your own Seam projects (see Appendix B, "

") to automatically generate project templates, including all configuration files, for you. In this chapter, we will not spend too much time explaining the details of the directory structure in the source code project. Instead, we focus on the code and configuration artifacts a developer must write or manage to build a Seam application. This way, you can apply the knowledge to any project structure without being confined to our template."

" A Seam application consists of Java classes and XML/text configuration files. In the book's example projects, the Java source code files are in the "

" annotation tells the container to map this class to a relational database table, with each property a column in the table. Each "

" instance corresponds to a row of data in the table. Since Seam is "configuration by exception", the container simply uses the class name property name for the table name and column name. The "

" column is for the primary key and its value is automatically generated by the application server for each "

" bean should be registered under Seam. In other Seam components (e.g., pages and session beans), you can reference the managed "

"Below the entry form, the JSF page displays all people who has said "hello" to Seam in the database. The list of people is stored in a Seam component named "

"When the user clicks on the "Say Hello" button to submit the form, Seam creates the "

" annotations are at the heart of the Seam programming model. So, let's look at exactly what they do here."

" In Seam documentation, you sometimes see the term "bijection". That refers to the two-way injection and outjection interaction between Seam components and the Seam managed context."

" to indicate that the current JSF page will be re-displayed with the most up-to-date model data after the call."

" interface. In order to conform to the EJB3 session bean specification, we need an interface that lists all the business methods in the bean. Below is the code for the "

"That is all the code you need for the Hello World example. In the next two sections, we cover alternative ways to do things and the configuration of Seam applications. You can skip the rest of the chapter for now if you want to jump right into the code and customize the "

"Now we have rushed through the Hello World example application. But we have left off some important topics, such as alternative ways to do things and important features not covered by the above code. In this section, let's go through those topics. They help you gain a deeper understanding of Seam. But for the impatient, you can skip this section and come back later."

"In the above example, we used an EJB3 session bean to implement the application logic. But we are not limited to use EJB3 components in Seam. In fact, in Seam, any POJO with a "

"Using POJOs to replace EJB3 beans has pros and cons. POJOs are slightly simpler to program since they do not require EJB3-specific annotations and interfaces (see above). If all your business components are Seam POJOs, you can run your Seam application outside of the EJB3 application server (see Chapter 23, "

"However, POJOs also have less features than EJB3 components since POJOs cannot get EJB3 container services. Examples of EJB3 services you lose in non-EJB3 Seam POJOs include the following."

"So, why would anyone want to use POJO components when deploying in an EJB3 container? Well, POJO components are good for pure "business logic" components, which delegate data access, messaging, and other infrastructure functionalities to other components. For instance, we can use POJO components to manage Seam data access objects.  The "business logic" POJO is useful since they can be re-used in other frameworks if you need to. But all in all, their application is much smaller than EJB3 components, especially in small to middle sized applications. So, in most examples throughout this book, we use EJB3 components."

", Seam is built from ground up to enable easy and out-of-the-container testing. In the "

" folder. The Seam testing infrastructure mocks the database, JSF, Seam context, and other application server services in plain Java SE environment. Just run "

"In the Hello World example, we demonstrated how to biject Seam components against field variables. You can also biject components against getter and setter methods. For instance, the following code would work just fine."

"While the above getter / setter methods are trivial, the real value of bijection via getter / setter methods is that you can add custom logic to manipulate the bijection process. For instance, you can validate the injected object or retrieve the outjected object on the fly from the database."

"Dependency bijection is a very useful design pattern. However, like any other design pattern, there is always a danger of overusing it. Too much dependency bijection can make the code harder to read since the developer must mentally figure out where each component is injected from. Too much bijection could also adds performance overhead since the bijection happens at runtime."

"In the Hello World example, there is a simple way to reduce and even eliminate the bijection: just make the data components properties of the business component. This way, in the JSF pages, we only need to reference the business component and there is no bijection needed to tie the business and data components. For instance, we can change the ManagerAction class to the following."

"The bottom line is that Seam is versatile when it comes to dependency management. It is generally a good practice to encapsulate the data component with its data access business component. This is especially the case for stateful business components."

"In this example, we have a single page application. After each button click, JSF re-renders the page with updated data model values. Obviously, most web applications would have more than one page. In JSF, an UI event handler method can determine which page to display next by returning the string name of a navigation rule. For instance, you can define the following navigation rule in the "

" page next. This gives us programatic control over which page to display next from inside the UI event handler method. "

" method runs an SQL-like query to retrieve data from the database in the form of a collection of entity bean objects. Please refer to the JPA documentation for more on how to use the "

" saves data to the embedded HSQL database. If you are running the application in JBoss AS on the local machine, you can open a GUI console for the HSQL database via the following steps: go to page "

"Next, let's move on to configuration files and application packaging next. You can actually generate almost all the configuration files and build script via the Seam Gen command line utility, or you can simply reuse the ones in the sample application source project. So, if you want to learn Seam programming techniques first and worry about configuration / deployment later, that is fine. You can safely skip this section and come back later when you need it."

"In this section, we focus on the Seam EJB3 component configuration here. Seam POJO configuration and deployment outside of JBoss AS is of course also possible. "

"Most Seam configuration files are XML files. But wait! Hadn't we just promised that Seam would get us out of the "XML hell" in J2EE and Spring? How come it has XML files too? Well, as it turns out, there are some good uses for XML files! XML files are very good for deployment time configuration (e.g., the root URL of the web application and the location of the backend database) because it allows us to make deploy-time changes without changing and re-compiling
... read the whole article


comments:(log in to vote on this article or comment on it)