|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 5 of 6
import javax.ejb.EJBContext;
import java.util.Properties;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class PortableContext1_0 extends PortableContext {
public Object lookup(String name, Class type)
throws PortableContextException {
try {
Properties props = ejbContext.getEnvironment();
String value = props.getProperty(name);
if (value == null)
return null;
if (name.startsWith("java:comp/env/jdbc"))
return DriverManager.getConnection(value);
else {
if (type == String.class)
return value;
else
return primitiveWrapper(value, type);
}
} catch(Exception e) {
throw new PortableContextException(e);
}
}
private Object primitiveWrapper(String value, Class type)
throws PortableContextException {
if (type == Double.class)
return new Double(value);
if (type == Integer.class)
return new Integer(value);
if (type == Boolean.class)
return new Boolean(value);
if (type == Long.class)
return new Long(value);
if (type == Byte.class)
return new Byte(value);
if (type == Short.class)
return new Short(value);
if (type == Float.class)
return new Float(value);
else
throw new PortableContextException();
}
}
|
import javax.ejb.EJBContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.SQLException;
public class PortableContext1_1 extends PortableContext {
InitialContext jndiContext;
public Object lookup(String name, Class type)
throws PortableContextException {
try {
jndiContext = new InitialContext();
Object value = jndiContext.lookup(name);
if (name.startsWith("java:comp/env/jdbc")) {
DataSource ds = (DataSource)value;
return ds.getConnection();
} else
return value;
} catch(NamingException ne) {
throw new PortableContextException(ne);
} catch(SQLException se) {
throw new PortableContextException(se);
}
}
}
|
In the PortableContext1_0 implementation, the name variable is used to obtain the JDBC URL from the environment properties. The JDBC URL is used in the DriverManager.getConnection() to obtain the JDBC connection. In the PortableContext1_1 context, the name is used to look up the JDBC factory (javax.sql.DataSource), which is in turn used to get the connection.
Below is an example of how the PortableContext would be used to get a database connection in a forward-compatible enterprise bean.
public class AccountBean implements javax.ejb.EntityContext {
int id;
double balance;
EntityContext ejbContext; PortableContext portableContext;
...
public void ejbLoad() {
try { Connection con = (Connection)
portableContext.lookup("java:comp/env/jdbc/AccountDB", Connection.class);
PreparedStatement ps = con.prepareStatement("select .... ");
...
}
...
}
|
EJB 1.1 recommends, but does not require, that the JDBC DataSource be mapped to the java:comp/env/jdbc directory name. This naming scheme is adopted on the PortableContext for consistency and forward compatibility.