Skip to content

Commit

Permalink
Doc the translator example, tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuir committed Dec 19, 2008
1 parent 1c6e377 commit 832e5e0
Showing 1 changed file with 242 additions and 15 deletions.
257 changes: 242 additions & 15 deletions reference/en/modules/ri.xml
Expand Up @@ -432,22 +432,18 @@ public class Generator {

<programlisting role="JAVA"><![CDATA[@Named
@SessionScoped
public class Game
{
public class Game {
private int number;
private int guess;
private int smallest;
private int biggest;
private int remainingGuesses;
public Game()
{
}
public Game() {}
@Initializer
Game(@Random int number, @MaxNumber int maxNumber)
{
Game(@Random int number, @MaxNumber int maxNumber) {
this.number = number;
this.smallest = 1;
this.biggest = maxNumber;
Expand All @@ -456,18 +452,14 @@ public class Game
// Getters and setters for fields
public String check()
{
if (guess>number)
{
public String check() {
if (guess>number) {
biggest = guess - 1;
}
if (guess<number)
{
if (guess<number) {
smallest = guess + 1;
}
if (guess == number)
{
if (guess == number) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Correct!"));
}
remainingGuesses--;
Expand All @@ -484,6 +476,241 @@ public class Game
The translator example will take any sentences you enter, and translate
them to Latin.
</para>

<para>
The translator example is built as an ear, and contains EJBs and
enterprise beans. As a result, it's structure is more complex than
the numberguess example.
</para>

<note>
<para>
EJB 3.1 and Jave EE 6 allow you to package EJBs in a war, which will
make this structure much simpler!
</para>
</note>

<para>
First, let's take a look at the ear aggregator, which is located in
<literal>webbeans-translator-ear</literal> module. Maven automatically
generates the <literal>application.xml</literal> and
<literal>jboss-app.xml</literal> for us:
</para>

<programlisting role="XML"><![CDATA[<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<modules>
<webModule>
<groupId>org.jboss.webbeans.examples.translator</groupId>
<artifactId>webbeans-translator-war</artifactId>
<contextRoot>/webbeans-translator</contextRoot>
</webModule>
</modules>
<jboss>
<loader-repository>webbeans.jboss.org:loader=webbeans-translator</loader-repository>
</jboss>
</configuration>
</plugin>]]></programlisting>

<para>
We're doing a couple of things here - firstly we set the context path,
which gives us a nice url
(<ulink url="http://localhost:8080/webbeans-translator">http://localhost:8080/webbeans-translator</ulink>)
and we also enable class loader isolation for JBoss AS.
</para>

<tip>
<para>
If you aren't using Maven to generate these files, you would need
<literal>META-INF/jboss-app.xml</literal>:
</para>

<programlisting role="XML"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss-app
PUBLIC "-//JBoss//DTD J2EE Application 4.2//EN"
"http://www.jboss.org/j2ee/dtd/jboss-app_4_2.dtd">
<jboss-app>
<loader-repository>webbeans.jboss.org:loader=webbeans-translator</loader-repository>
</jboss-app>]]></programlisting>

<para>
and <literal>META-INF/application.xml</literal>:
</para>

<programlisting role="XML"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd"
version="5">
<display-name>webbeans-translator-ear</display-name>
<description>Ear Example for the reference implementation of JSR 299: Web Beans</description>
<module>
<web>
<web-uri>webbeans-translator.war</web-uri>
<context-root>/webbeans-translator</context-root>
</web>
</module>
<module>
<ejb>webbeans-translator.jar</ejb>
</module>
</application>]]></programlisting>
</tip>

<para>
Next, lets look at the war. Just as in the numberguess example, we have
a <literal>faces-config.xml</literal> (to enabled Facelets) and a
<literal>web.xml</literal> (to enable JSF and attach Web Beans services
to the servlet container) in <literal>WebContent/WEB-INF</literal>.
</para>

<para>
More intersting is the facelet used to translate text. Just as in
the numberguess example we have a template, which surrounds the form
(ommitted here for brevity):
</para>

<programlisting role="XML"><![CDATA[<h:form id="NumberGuessMain">
<table>
<tr align="center" style="font-weight: bold" >
<td>
Your text
</td>
<td>
Translation
</td>
</tr>
<tr>
<td>
<h:inputTextarea id="text" value="#{translator.text}" required="true" rows="5" cols="80" />
</td>
<td>
<h:outputText value="#{translator.translatedText}" />
</td>
</tr>
</table>
<div>
<h:commandButton id="button" value="Translate" action="#{translator.translate}"/>
</div>
</h:form>]]></programlisting>

<para>
The user can enter some text in the lefthand textarea, and hit the
translate button to see the result to the right.
</para>

<para>
Finally, let's look at the ejb module,
<literal>webbeans-translator-ejb</literal>.
There are two configuration files in
<literal>src/main/resources/META-INF</literal>, an empty
<literal>web-beans.xml</literal>, used to mark the archive as
containing Web Beans, and <literal>ejb-jar.xml</literal>. Web Beans
provides injection and initializtion services for all EJBs, and uses
<literal>ejb-jar.xml</literal> to enable this, you'll need this in any
EJB project which uses Web Beans:
</para>

<programlisting role="XML"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
version="3.0">
<interceptors>
<interceptor>
<interceptor-class>org.jboss.webbeans.ejb.SessionBeanInterceptor</interceptor-class>
</interceptor>
</interceptors>
<assembly-descriptor>
<interceptor-binding>
<ejb-name>*</ejb-name>
<interceptor-class>org.jboss.webbeans.ejb.SessionBeanInterceptor</interceptor-class>
</interceptor-binding>
</assembly-descriptor>
</ejb-jar>]]></programlisting>

<para>
We've saved the most interesting bit to last, the code! The project has
two simple beans, <literal>SentanceParser</literal> and
<literal>TextTranslator</literal> and two enterprise beans,
<literal>TanslatorControllerBean</literal> and
<literal>SentenceTranslator</literal>. You should be getting quite
familiar with what a Web Bean looks like by now, so we'll just
highlight the most interesting bits here.
</para>

<para>
Both <literal>SentanceParser</literal> and
<literal>TextTranslator</literal> are dependent beans, and
<literal>TextTranslator</literal> uses constructor initialization:
</para>

<programlisting role="JAVA"><![CDATA[public class TextTranslator {
private SentenceParser sentenceParser;
private Translator sentenceTranslator;
@Initializer
TextTranslator(SentenceParser sentenceParser, Translator sentenceTranslator)
{
this.sentenceParser = sentenceParser;
this.sentenceTranslator = sentenceTranslator;]]></programlisting>

<para>
<literal>TextTranslator</literal> is a stateless bean (with a local
business interface), where the magic happens - of course, we couldn't
develop a full translator, but we gave it a good go!
</para>

<para>
Finally, there is UI orientated controller, that collects the text from
the user, and dispatches it to the translator. This is a request
scoped, named, stateful session bean, which injects the translator.
</para>

<programlisting role="JAVA"><![CDATA[@Stateful
@RequestScoped
@Named("translator")
public class TranslatorControllerBean implements TranslatorController
{
@Current TextTranslator translator;]]></programlisting>

<para>
The bean also has getters and setters for all the fields on the page.
</para>

<para>
As this is a stateful session bean, we have to have a remove method:
</para>

<programlisting role="JAVA"><![CDATA[ @Remove
public void remove()
{
}]]></programlisting>

<para>
The Web Beans manager will call the remove method for you when the bean
is destroyed; in this case at the end of the request.
</para>
</section>

<para>
That concludes our short tour of the Web Beans RI examples. For more on
the RI, or to help out, please visit
<ulink url="http://www.seamframework.org/WebBeans/Development">http://www.seamframework.org/WebBeans/Development</ulink>.
</para>

<para>
We need help in all areas - bug fixing, writing new features, writing
examples and translating this reference guide.
</para>

</chapter>

0 comments on commit 832e5e0

Please sign in to comment.