[jboss-user] [EJB 3.0] - Re: EJB 3.0 and env-entry injection

itty06 do-not-reply at jboss.com
Wed Nov 22 00:42:44 EST 2006


Here is an example.
All three work but I have shown the first 2 in this example

ejb-jar.xml

ejb-jar.xml

  | <?xml version="1.0" encoding="UTF-8"?>
  | <ejb-jar version="3.0" 
  | 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 ../../../testapp/ejb-jar_3_0.xsd ">
  | <enterprise-beans>
  | <session>
  | 	<ejb-name>com.suresh.ejb.test.HelloBean</ejb-name>
  | 		<remote>com.suresh.ejb.test.Hello</remote>
  | 	<ejb-class>com.suresh.ejb.test.HelloBean</ejb-class>
  | 	<session-type>Stateless</session-type>
  | 	<transaction-type>Container</transaction-type>
  | 		<env-entry>
  | 		  <env-entry-name>com.suresh.ejb.test.HelloBean/urls</env-entry-name>
  | 		  <env-entry-type>java.lang.String</env-entry-type>
  | 		  <env-entry-value>@ldap.url@</env-entry-value>
  | 		</env-entry>
  | 		<env-entry>
  | 		  <env-entry-name>username</env-entry-name>
  | 		  <env-entry-type>java.lang.String</env-entry-type>
  | 		  <env-entry-value>@user.name@</env-entry-value>
  | 		</env-entry>
  | 		<env-entry>
  | 		  <env-entry-name>password</env-entry-name>
  | 		  <env-entry-type>java.lang.String</env-entry-type>
  | 		  <env-entry-value>what</env-entry-value>
  | 		</env-entry>
  | 		<env-entry>
  | 		   <env-entry-name>com.suresh.ejb.test.HelloBean/location</env-entry-name>
  | 		  <env-entry-type>java.lang.String</env-entry-type>
  | 		  <env-entry-value>Australia</env-entry-value>
  | 		</env-entry>				
  | 	<resource-ref>
  | 		<res-ref-name>jdbc/user</res-ref-name>
  | 		<res-type>javax.sql.Datasource</res-type>
  | 		<res-auth>Container</res-auth>
  | 		<mapped-name>java:UserDB</mapped-name>
  | 	</resource-ref>
  | 	
  | </session>
  | </enterprise-beans>
  | 
  | </ejb-jar>
  | 



  | package com.suresh.ejb.test;
  | 
  | import java.sql.Connection;
  | import java.sql.ResultSet;
  | 
  | import javax.annotation.Resource;
  | import javax.naming.Context;
  | import javax.naming.InitialContext;
  | import javax.sql.DataSource;
  | 
  | 
  | 
  | import com.suresh.ejb.test.Hello;
  | 
  | @javax.ejb.Stateless(name="com.suresh.ejb.test.HelloBean")
  | @javax.ejb.TransactionAttribute (value=javax.ejb.TransactionAttributeType.SUPPORTS)		
  | @javax.annotation.Resource(name="jdbc/user",
  | 		type=DataSource.class,
  | 		authenticationType=javax.annotation.Resource.AuthenticationType.CONTAINER, 
  | 		mappedName="java:UserDB")
  | public class HelloBean implements Hello {
  | 	
  | 	@Resource private String urls;
  | 	@Resource(name="username") private String provider;
  | 	@Resource(name="password") private String pwd;
  | 	@Resource private String location;
  | 
  | 	public String greet() {
  | 		return urls + " " + location + " " + pwd + " " +  provider + " at " + greet1();
  | 	}
  | 	//example of datasource
  | 	public String greet1() {
  | 		String data = null;
  | 		Connection c = null;
  | 		try {
  | 			Context ctx = new InitialContext();
  | 			DataSource ds = (DataSource) ctx.lookup("java:comp.ejb3/env/jdbc/user");
  | 			ResultSet rs = ds.getConnection().createStatement().executeQuery("select sysdate from dual");
  | 			while (rs.next()) {
  | 				data = rs.getString(1);
  | 			}
  | 		} catch (Exception e) {
  | 			System.err.println(e);
  | 			e.printStackTrace();
  | 		} finally {
  | 			try {
  | 			c.close();
  | 			} catch (Exception e) {
  | 			}
  | 		}
  | 		return data;
  | 	}
  | }
  | 
  | 


Hello.java

  | package comsuresh.ejb.test;
  | 
  | import javax.ejb.Remote;
  | 
  | @Remote
  | public interface Hello {
  | 	public String greet();
  | }
  | 
  | 


Ran with 4.0.4GA + EJB RC9

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3987797#3987797

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3987797



More information about the jboss-user mailing list