}
}
private static JndiResourceDescriptor fromDatabase(String alias) throws NamingException
{
JndiObjectResource resource = null;
EntityManager em = null;
try
{
// Using the horrible CriteriaBuilder API instead of a string query. This avoids classloading issues - Hibernate binds
// the entities at run time with the thread current classloader...
em = Helpers.getNewEm();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<JndiObjectResource> q = cb.createQuery(JndiObjectResource.class);
Root<JndiObjectResource> c = q.from(JndiObjectResource.class);
ParameterExpression<String> p = cb.parameter(String.class);
q.select(c).where(cb.equal(c.get("name"), p));
TypedQuery<JndiObjectResource> query = em.createQuery(q);
query.setParameter(p, alias);
resource = query.getSingleResult();
}
catch (Exception e)
{
NamingException ex = new NamingException("Could not find a JNDI object resource of name " + alias);
ex.setRootCause(e);
throw ex;
}
finally
{
if (em != null)
{
em.close();
}
}
// Create the ResourceDescriptor from the JPA object
JndiResourceDescriptor d = new JndiResourceDescriptor(resource.getType(), resource.getDescription(), null, resource.getAuth(),
resource.getFactory(), resource.getSingleton());
for (JndiObjectResourceParameter prm : resource.getParameters())
{
d.add(new StringRefAddr(prm.getKey(), prm.getValue()));
}
return d;