Using The Jboss Ide For Eclipse: Important
Using The Jboss Ide For Eclipse: Important
Important:
Linux
Configuration #1
JDK 1.4.2
Eclipse 3.0.2 (GTK)
JBoss 3.2.7
JBossIDE 1.4.1.e31-jre14
Configuration #2
JDK 1.5.0
Eclipse 3.1.2
JBoss 4.0.4
JBossIDE 1.6.0
Windows
Configuration #1
JDK 1.5.0_03
JBoss 3.2.7
JBossIDE-1.5M1-jre15 (http://jboss.sourceforge.net)
Configuration #2
JDK 1.5.0_06
JBoss 4.0.4
JBossIDE 1.6.0 GA
Page: 1
Building a Simple EJB
Now, click the Browse button (near the bottom) and create a new
output folder name bin
Next click the Add Folder button (near the upper-right) and creating a
folder named src
NOTE: Creating these folders makes Eclipse store your .java source
code files in the src folder while the compiled .class files will be
stored in the bin folder. Separate folders makes packaging and
managing your projects easier.
Select File -> New -> Other from the Eclipse menu
Browse to JBoss-IDE -> EJB Components, select Session Bean and click
Next
NOTE: It is highly recommended that you place all your beans inside
a package that ends with '.ejb' and to name the beans so they end
with Bean.
Page: 2
file found in the src/edu/uah/coned/ejb folder. Right-click on the
CalculatorBean class (the green C icon).
Enter add for the Method Name, double for the Return Type and then
use the Add button next to the Parameters window to create 2
parameters named a and b both of type double. Click the Finish
button once the method is defined.
Open the CalculatorBean.java file (if not already open) and modify the
newly created methods as shown below:
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public double add(double a, double b) {
return a + b;
}
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public double subtract(double a, double b) {
return a - b;
}
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public double multiply(double a, double b) {
return a * b;
}
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public double divide(double a, double b) {
if (b == 0.0) {
throw new EJBException("Divide by zero error");
}
return a / b;
Page: 3
}
Now that our bean has been created, we need to build interfaces that
clients will use to access the code. Enterprise JavaBean components
are only used by the application server, never directly by clients. We
are going to use the XDoclet tool which is bundled with the JBossIDE to
create the various interface files for us.
First, select Project > Properties from the menu (or right-click on the
SimpleCalc project and select Properties)
Select XDoclet Configurations from the list and click the Enable
XDoclet check box if not already enabled.
Click the Add Standard button. Enter EJB in the Name file, select
Standard EJB and then click the OK button.
Now, click on the new EJB configuration. Expand the bottom left tree
near and find the fileset entry. Double the includes property (in the
right list) and change it to **/*Bean.java. This restricts XDoclet so it
only processes files that are named *Bean.java.
Details
Once the XDoclet tool runs, you will find that 2 new folders have been
added to your project. They are the src/edu.uah.coned.interfaces and
src/META-INF folders.
The interfaces folder should have 3 files that define the interfaces for
your Bean class. Clients will use these interfaces to access your Bean.
If you add more methods to your Bean class, remember to Run
XDoclet again to update the interface files.
In the META-INF folder you will find 2 files named ejb-jar.xml and
jboss.xml. The first is Sun's standard EJB descriptor file that is
required for all Bean classes. The second contains Java Naming and
Directory Interface (JNDI) information JBoss needs about your Bean
Page: 4
class, so it can be found by clients.
At this point we need a Java program that exercises the new Bean we
just created. Let's create a Java servlet and HTML page. Alternately,
we could also build a Java command-line or GUI application instead.
Now select JBoss-IDE -> Web Components -> HTTP Servlet and click
Next
Also check the init() method and doPost() method check boxes and
click the Finish button.
First add a private variable to the class that will hold a reference to the
Bean object.
public CalculatorServlet() {
super();
// TODO Auto-generated constructor stub
}
...
Page: 5
Next, modify the init() method to read as shown below:
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>");
out.println("Calculator Results");
out.println("</title></head>");
out.println("<body>");
out.println("<h1>Calculator Results</h1>");
double a = 0, b = 0, result = 0;
String operation = "";
try {
Calculator bean = home.create();
String aStr = request.getParameter("a");
String bStr = request.getParameter("b");
if (request.getParameter("Add") != null) {
operation = " + ";
Page: 6
result = bean.add(a,b);
} else if (request.getParameter("Subtract") != null) {
operation = " - ";
result = bean.subtract(a,b);
} else if (request.getParameter("Multiply") != null) {
operation = " * ";
result = bean.multiply(a,b);
} else if (request.getParameter("Divide") != null) {
operation = " / ";
result = bean.divide(a,b);
} else {
throw new ServletException("Unrecognized operation");
}
} else {
throw new ServletException("Missing one or more input values");
}
bean.remove();
} catch (Exception e) {
out.println(e.getMessage());
e.printStackTrace(out);
} finally {
out.println("</body></html>");
out.close();
}
}
The final step for the servlet is to add some metadata to the top of the
class. XDoclet uses this information to create the required JBoss
configuration files for the servlet. This metadata must be created
inside the comments at the top of the file. Modify it to read like this:
/**
* Servlet Class
*
* @web.servlet name="Calculator"
* display-name="CalculatorServlet"
* description="Exercises the Calculator EJB"
*
* @web.servlet-mapping url-pattern="/Calculator"
*
* @web.ejb-ref name = "ejb/Calculator"
* type = "Session"
* home = "edu.uah.coned.interfaces.CalculatorHome"
* remote = "edu.uah.coned.interfaces.Calculator"
Page: 7
* description = "CalculatorBean references"
*
* @jboss.ejb-ref-jndi ref-name = "ejb/Calculator"
* jndi-name = "ejb/Calculator"
*/
Click the Add Standard button. Enter Web in the Name file, select
Standard Web and then click the OK button.
Next, select the Web configuration and click on webdoclet in the lower-
left panel. Change the destDir property property on the right to
src/WEB-INF. This will force XDoclet to store the servlet related files
in that folder within the web project.
Next, select the fileset entry on the left list and change the includes
property to read **/*Servlet.java. Again, this makes Xdoclet only
process source files that end with that extension.
Finally, clear the check box next to the jsptaglib entry on the left list.
This is not required for our project.
NOTE: This should create a new src/WEB-INF folder with XML files
that describe the servlet to Jboss. XDoclet has extracted the
metadata we added to the top of our file and used that to generate
a jboss-web.xml and web.xml file.
Now we need an HTML page that will allow the user to enter the
parameters desired and then invoke the Servlet we just created.
Page: 8
First, add a new folder to the project named 'docroot'.
Page: 9
server. Create the file by doing this:
First right-click on the src/META-INF folder and select New -> Other
EJB Jar – This jar file will contain our Bean interfaces, classes and the
EJB deployment descriptor files (under src/META-INF) we created
earlier using the XDoclet utility.
EJB Client Jar – This jar file will contain only the EJB interfaces (withou
the Bean) and is needed by the servlet class.
Web Application War – This file (with a .WAR extension) will contain the
web site related files including the HTML documents, servlet class, the
EJB Client and various deployment descriptors.
Page: 10
J2EE Application EAR – The Enterprise Application Archive (with a .EAR
extension) will contain the EJB Jar, WAR and the web deployment
descriptors (under src/WEB-INF). This has everything JBoss needs to
run our application.
Highlight the 'Packing Configurations' option on the list and click the
check box near the top named Enable Packaging.
NOTE: We are not using JDBC for this example, so the jbosscmp-
jdbc.xml file is not needed. The MANIFEST.MF file is normally used
to set the correct CLASSPATH entries and other related properties
needed by the application, but since the jboss.xml file already
contains that information, we can safely remove it from this
configuration. Application servers other than JBoss may have
slightly different requirements.
Expand the tree and again remove the MANIFEST.MF file from the list.
Page: 11
Also edit the '/SimpleCalc/bin' entry and change the Includes value
to edu/coned/uah/web/*.class. That way the WAR file will only
contain classes related to our servlet.
Since the servlet uses our bean class, we must add the bean interface
classes to the WAR package. If you remember, we earlier setup our
packages so the interface classes will be packaged into the
CalculatorEJB-client.jar file. So that means we need to add that file
to the WAR file also.
The WAR file must also have the HTML page we created earlier. This
will be the default page users see when they visit our application.
Right-click on the CalculatorWeb.war file and select the Add Folder
option. Browse to find the docroot folder and add it. That should
complete our WAR package.
Finally we need to create one more package. The last package must
be an Enterprise Application Archive that contains all of the other JAR
and WAR files we just defined. This will be the complete application
and all support files needed by JBoss.
Once again, expand the tree and remove the MANIFEST.MF file.
Now you can generate the archive files by right-clicking on the project
Page: 12
and selecting Run Packaging from the menu. If the packaging is
successful, you should now see the new files in the Project Explorer
window.
Select the desired JBoss installation where you want to deploy the
application and click OK.
Page: 13
Accessing the Bean from a normal Java Applications
NOTE: You may wish to create separate source and bin folders for
the code and class files like we did for the bean project. You can
also make this the default behavior by setting the appropriate
options under Window -> Preferences -> Build Path.
Visit the Libraries tab where we must add 2 JARs to our project.
Click on Add JARs and browse down into the SimpleCalc project and
select the CalculatorEJB-client.jar file.
Next, click on Add External JARs and browse to find the client folder
under your JBoss installation. Select the jbossall-client.jar file.
Use File -> New Class to create a new class named CalcClient. Make
sure the option to create a main method is enabled.
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import edu.uah.coned.interfaces.Calculator;
import edu.uah.coned.interfaces.CalculatorHome;
/*
* Created on May 23, 2005
Page: 14
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author randy
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class CalcClient {
////////////////////////////////////////////////////////////
// NOTE: You can put these values in jndi.properties instead
// of hard-coding them inside the application.
ht.put
(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContext
Factory");
ht.put(InitialContext.PROVIDER_URL,"jnp://localhost:1099");
ht.put
(InitialContext.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
// System.out.println("Expecting an error.");
// System.out.println("4 / 0 = " + bean.divide(4,0));
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
} finally {
if (bean != null) {
try {
bean.remove();
Page: 15
} catch (Exception e) {
System.out.println("Error removing bean:" + e.getMessage
());
e.printStackTrace();
}
}
}
}
}
At this point you should be able to run and/or debug the code, after
making sure JBoss is running and that the CalculatorBean is deployed.
Page: 16