public OpenJPAEntityManagerSPI createEntityManager(Map props) {
if (props == null)
props = Collections.EMPTY_MAP;
else if (!props.isEmpty())
props = new HashMap(props);
OpenJPAConfiguration conf = getConfiguration();
String user = (String) Configurations.removeProperty
("ConnectionUserName", props);
if (user == null)
user = conf.getConnectionUserName();
String pass = (String) Configurations.removeProperty
("ConnectionPassword", props);
if (pass == null)
pass = conf.getConnectionPassword();
String str = (String) Configurations.removeProperty
("TransactionMode", props);
boolean managed;
if (str == null)
managed = conf.isTransactionModeManaged();
else {
Value val = conf.getValue("TransactionMode");
managed = Boolean.parseBoolean(val.unalias(str));
}
Object obj = Configurations.removeProperty("ConnectionRetainMode",
props);
int retainMode;
if (obj instanceof Number)
retainMode = ((Number) obj).intValue();
else if (obj == null)
retainMode = conf.getConnectionRetainModeConstant();
else {
Value val = conf.getValue("ConnectionRetainMode");
try {
retainMode = Integer.parseInt(val.unalias((String) obj));
} catch (Exception e) {
throw new ArgumentException(_loc.get("bad-em-prop",
"openjpa.ConnectionRetainMode", obj),
new Throwable[]{ e }, obj, true);
}
}
Broker broker = _factory.newBroker(user, pass, managed, retainMode,
false);
// add autodetach for close and rollback conditions to the configuration
broker.setAutoDetach(AutoDetach.DETACH_CLOSE, true);
broker.setAutoDetach(AutoDetach.DETACH_ROLLBACK, true);
broker.setDetachedNew(false);
OpenJPAEntityManagerSPI em = newEntityManagerImpl(broker);
// allow setting of other bean properties of EM
String[] prefixes = ProductDerivations.getConfigurationPrefixes();
List< RuntimeException > errs = null;
Method setter;
String prop, prefix;
Object val;
for (Map.Entry entry : (Set< Map.Entry >) props.entrySet()) {
prop = (String) entry.getKey();
prefix = null;
for (int i = 0; i < prefixes.length; i++) {
prefix = prefixes[i] + ".";
if (prop.startsWith(prefix))
break;
prefix = null;
}
if (prefix == null)
continue;
prop = prop.substring(prefix.length());
try {
setter = Reflection.findSetter(em.getClass(), prop, true);
} catch (OpenJPAException ke) {
if (errs == null)
errs = new LinkedList< RuntimeException >();
errs.add(PersistenceExceptions.toPersistenceException(ke));
continue;
}
val = entry.getValue();
try {
if (val instanceof String) {
if ("null".equals(val))
val = null;
else
val = Strings.parse((String) val,
setter.getParameterTypes()[0]);
}
Reflection.set(em, setter, val);
} catch (Throwable t) {
while (t.getCause() != null)
t = t.getCause();
ArgumentException err = new ArgumentException(_loc.get
("bad-em-prop", prop, entry.getValue()),
new Throwable[]{ t }, null, true);
if (errs == null)
errs = new LinkedList< RuntimeException >();
errs.add(err);
}
}
if (errs != null) {
em.close();
if (errs.size() == 1)
throw errs.get(0);
throw new ArgumentException(_loc.get("bad-em-props"),
errs.toArray(new Throwable[errs.size()]),
null, true);
}
return em;
}
|