NamingEnumeration<NameClassPair> persUnits = context.list("java:openejb/PersistenceUnit");
if (persUnits == null)
{
throw new CreationException("No PersistenceUnit found in java:openejb/PersistenceUnit!");
}
String shortestMatch = null;
while (persUnits.hasMore())
{
NameClassPair puNc = persUnits.next();
if (puNc.getName().startsWith(unitName))
{
if (shortestMatch == null || shortestMatch.length() > puNc.getName().length())
{
shortestMatch = puNc.getName();
}
}
}
if (shortestMatch == null)
{
throw new CreationException("PersistenceUnit '" + unitName + "' not found");
}
factory = (EntityManagerFactory) context.lookup("java:openejb/PersistenceUnit/" + shortestMatch);
} catch (NamingException e) {
throw new CreationException("PersistenceUnit '" + unitName + "' not found", e );
}
return clazz.cast(factory);
}