Saturday, August 13, 2005

another EJB3 container

Resin-3.0.14, another open source EJB 3 container. Though it is in beta version, it is really good for academic purpose. Since EJB 3 has become extraordinarily simple, it would take some mins to deploy your EJBs. I deployed a simple stateless session bean, similar to the one coming with the Resin, have uploaded the same, might be useful for you to get started.

In EJB3, there are no mandatory interfaces like, EJBHome and EJBObject, you may just have some business interface:
public interface Calculator {

public int add(int a, int b);

public int sub(int a, int b);

}


Bean implementation:
import static

javax.ejb.TransactionAttributeType.SUPPORTS;

@javax.ejb.Stateless
// Marks the bean as Stateless

public class CalculatorBean

implements Calculator {

@javax.ejb.TransactionAttribute(SUPPORTS)
//Marks the method with

//Supports transaction attribute

public int add(int a, int b)

{

return a+b;

}



@javax.ejb.TransactionAttribute(SUPPORTS)
//Marks the method with

//Supports transaction attribute

public int sub(int a, int b)

{

return a-b;

}

}


I could manage only to write the client as a Servlet, there is no proper documentation which describes how to write a standalone client. If anyone have used Resin to deploy prior versions of EJB, they can help us out with this. Anyhow found out that, we need to use either Burlap or Hessian implementations to expose the EJB to outside the container. Nevertheless, the ejb client servlet is:

public class ClientServlet extends HttpServlet {

private Calculator calc;

/**

* Dependency injector

* for the Calculator interface.

*/

@EJB(name="CalculatorBean")

public void setCalculator(Calculator newCalc)

{

calc = newCalc;

}

public void service(HttpServletRequest req,

HttpServletResponse res)

throws IOException, ServletException

{

PrintWriter out = res.getWriter();

if (calc == null) {

out.println("Calculator not found!");

return;

}

out.println("Add: "+calc.add(4,5));

out.println("Sub: "+calc.sub(4,5));

}

}

The above setCalculator method basically looks up the JNDI with "CalculatorBean" name , finds and assigns the instance. All it does since it is marked with @EJB annotation.

The configuration is pretty simple, just need to add <ejb-server> tag to web.xml:
<web-app xmlns="http://caucho.com/ns/resin">



<ejb-server jndi-name="java:comp/env/ejb">

<bean type="stateless.CalculatorBean"/>

</ejb-server>



<servlet servlet-name="calc"

servlet-class="stateless.ClientServlet">

<load-on-startup>0</load-on-startup>

</servlet>



<servlet-mapping url-pattern="/calc"

servlet-name="calc"/>

</web-app>

Thursday, August 11, 2005

dabbling with EJB3...

If you cannot wait for the public release of EJB3 spec and major vendors' implementations, there is an option for you to test your EJB3 skills. JBoss has already released a EJB 3 container in ALPHA version. JBoss-EJB-3.0_Embeddable_ALPHA, they claim is a microcontainer and which will be the core container for JBoss 5.0.

Unlike other EJB containers, this can be used even for standalone applications, junit tests, etc. Ideally, you will bootup the container in your first statement of your program, and shut it down in the last statement. I believe, it will be very handy for writing test cases. And moreover, you can plug this microcontainer with Tomcat, or any web/application server for that matter. Anyhow, for the time being, it is absolutely handy to dabble with EJB3.

All you need is Java 5, and the microcontainer. CLASSPATH setting is pretty simple, conf directory and all jars in lib directory of microcontainer need to be added. JBoss Embeddable EJB 3 can be downloaded here [http://www.jboss.com/products/list/downloads#ejb3]

Monday, August 1, 2005

reading a file that resides inside a jar

Most of the time, we end up reading a text or properties file that is bundled with the application. If the application jar is extracted and run, we wouldnt have problem in reading the file since it is in the file system now. But, sometimes we would be running the application as it is, may be using "java -jar" command or an Applet for example. In this case, the application need to read the file which resides in the same application jar.
import java.io.*;

public class TxtRead

{

public void read() throws IOException

{
InputStream ios=getClass().

getResourceAsStream("test.txt");

BufferedReader reader=new BufferedReader(

new InputStreamReader(ios));

String line=reader.readLine();



while (line != null)

{

System.out.println(line);

line=reader.readLine();

}

}



public static void main(String[] arg)

throws IOException

{

new TxtRead().read();

}

}

/home/poorna> cat test.txt
This is a text file

/home/poorna> javac TxtRead.java

# Create a manifest file in META-INF directory with "Main-Class: TxtRead"

/home/poorna> jar -cvfm txt.jar META-INF\MANIFEST.MF TxtRead.class test.txt


/home/poorna> java -jar txt.jar
This is a text file