required.put("JtaManaged", ANY);
String possibleJta = findResourceId(unit.getJtaDataSource(), "DataSource", required, null);
String possibleNonJta = findResourceId(unit.getNonJtaDataSource(), "DataSource", required, null);
if (possibleJta != null && possibleJta == possibleNonJta){
ResourceInfo dataSource = configFactory.getResourceInfo(possibleJta);
String jtaManaged = (String) dataSource.properties.get("JtaManaged");
logger.warning("PeristenceUnit(name=" + unit.getName() + ") invalidly refers to Resource(id=" + dataSource.id + ") as both its <jta-data-source> and <non-jta-data-source>.");
if ("true".equalsIgnoreCase(jtaManaged)){
nonJtaDataSourceId = null;
unit.setNonJtaDataSource(null);
} else if ("false".equalsIgnoreCase(jtaManaged)){
jtaDataSourceId = null;
unit.setJtaDataSource(null);
}
}
//
// Do the jta-data-source and non-jta-data-source references
// point to innapropriately configured Resources?
//
checkUnitDataSourceRefs(unit);
//
// Do either the jta-data-source and non-jta-data-source
// references point to the explicit name of a ServiceProvider?
//
if (jtaDataSourceId == null && nonJtaDataSourceId == null){
jtaDataSourceId = findResourceProviderId(unit.getJtaDataSource());
nonJtaDataSourceId = findResourceProviderId(unit.getNonJtaDataSource());
// if one of them is not null we have a match on at least one
// we can just create the second resource using the first as a template
if (jtaDataSourceId != null || nonJtaDataSourceId != null){
Resource jtaResource = new Resource(jtaDataSourceId, "DataSource", jtaDataSourceId);
jtaResource.getProperties().setProperty("JtaManaged", "true");
Resource nonJtaResource = new Resource(nonJtaDataSourceId, "DataSource", nonJtaDataSourceId);
nonJtaResource.getProperties().setProperty("JtaManaged", "false");
if (jtaDataSourceId == null){
jtaResource.setId(nonJtaDataSourceId+"Jta");
jtaResource.setProvider(nonJtaDataSourceId);
} else if (nonJtaDataSourceId == null){
nonJtaResource.setId(jtaDataSourceId+"NonJta");
nonJtaResource.setProvider(jtaDataSourceId);
}
ResourceInfo jtaResourceInfo = configFactory.configureService(jtaResource, ResourceInfo.class);
ResourceInfo nonJtaResourceInfo = configFactory.configureService(nonJtaResource, ResourceInfo.class);
logAutoCreateResource(jtaResourceInfo, "DataSource", unit.getName());
jtaDataSourceId = installResource(unit.getName(), jtaResourceInfo);
logAutoCreateResource(nonJtaResourceInfo, "DataSource", unit.getName());
nonJtaDataSourceId = installResource(unit.getName(), nonJtaResourceInfo);
setJtaDataSource(unit, jtaDataSourceId);
setNonJtaDataSource(unit, nonJtaDataSourceId);
continue;
}
}
// No data sources were specified:
// Look for defaults, see https://issues.apache.org/jira/browse/OPENEJB-1027
if (jtaDataSourceId == null && nonJtaDataSourceId == null) {
// We check for data sources matching the following names:
// 1. The persistence unit id
// 2. The web module id
// 3. The web module context root
// 4. The application module id
List<String> ids = new ArrayList<String>();
ids.add(unit.getName());
for (WebModule webModule : app.getWebModules()) {
ids.add(webModule.getModuleId());
ids.add(webModule.getContextRoot());
}
ids.add(app.getModuleId());
// Search for a matching data source
for (String id : ids) {
//Try finding a jta managed data source
required.put("JtaManaged", "true");
jtaDataSourceId = findResourceId(id, "DataSource", required, null);
if (jtaDataSourceId == null) {
//No jta managed data source found. Try finding a non-jta managed
required.clear();
required.put("JtaManaged", "false");
nonJtaDataSourceId = findResourceId(id, "DataSource", required, null);
}
if (jtaDataSourceId == null && nonJtaDataSourceId == null) {
// Neither jta nor non-jta managed data sources were found. try to find one with it unset
required.clear();
required.put("JtaManaged", NONE);
jtaDataSourceId = findResourceId(id, "DataSource", required, null);
}
if (jtaDataSourceId != null || nonJtaDataSourceId != null) {
//We have found a default. Exit the loop
break;
}
}
}
//
// If neither of the references are valid yet, then let's take
// the first valid datasource.
//
// We won't fill in both jta-data-source and non-jta-data-source
// this way as the following code does a great job at determining
// if any of the existing data sources are a good match or if
// one needs to be generated.
//
if (jtaDataSourceId == null && nonJtaDataSourceId == null){
required.clear();
required.put("JtaManaged", "true");
jtaDataSourceId = firstMatching("DataSource", required, null);
if (jtaDataSourceId == null){
required.clear();
required.put("JtaManaged", "false");
nonJtaDataSourceId = firstMatching("DataSource", required, null);
}
}
//
// Does the jta-data-source reference point an existing
// Resource in the system with JtaManaged=true?
//
// If so, we can search for an existing datasource
// configured with identical properties and use it.
//
// If that doesn't work, we can copy the jta-data-source
// and auto-create the missing non-jta-data-source
// using it as a template, applying the overrides,
// and finally setting JtaManaged=false
//
if (jtaDataSourceId != null && nonJtaDataSourceId == null){
ResourceInfo jtaResourceInfo = configFactory.getResourceInfo(jtaDataSourceId);
Properties jtaProperties = jtaResourceInfo.properties;
if (jtaProperties.containsKey("JtaManaged")){
// Strategy 1: Best match search
required.clear();
required.put("JtaManaged", "false");
for (String key : asList("JdbcDriver", "JdbcUrl")) {
if (jtaProperties.containsKey(key)) required.put(key, jtaProperties.get(key));
}
nonJtaDataSourceId = firstMatching("DataSource", required, null);
// Strategy 2: Copy
if (nonJtaDataSourceId == null) {
ResourceInfo nonJtaResourceInfo = copy(jtaResourceInfo);
nonJtaResourceInfo.id = jtaResourceInfo.id + "NonJta";
Properties overrides = ConfigurationFactory.getSystemProperties(nonJtaResourceInfo.id, nonJtaResourceInfo.service);
nonJtaResourceInfo.properties.putAll(overrides);
nonJtaResourceInfo.properties.setProperty("JtaManaged", "false");
logAutoCreateResource(nonJtaResourceInfo, "DataSource", unit.getName());
logger.info("configureService.configuring", nonJtaResourceInfo.id, nonJtaResourceInfo.service, jtaResourceInfo.id);
nonJtaDataSourceId = installResource(unit.getName(), nonJtaResourceInfo);
}
}
}
//
// Does the jta-data-source reference point an existing
// Resource in the system with JtaManaged=false?
//
// If so, we can search for an existing datasource
// configured with identical properties and use it.
//
// If that doesn't work, we can copy the jta-data-source
// and auto-create the missing non-jta-data-source
// using it as a template, applying the overrides,
// and finally setting JtaManaged=false
//
final String deduceJtaFromNonJta = SystemInstance.get().getOptions().get(AUTOCREATE_JTA_DATASOURCE_FROM_NON_JTA_ONE_KEY, (String) null);
if (nonJtaDataSourceId != null && jtaDataSourceId == null
// hibernate uses the fact that this ds is missing to get a non jta em instead of a JTA one
&& (deduceJtaFromNonJta == null || (deduceJtaFromNonJta != null && Boolean.parseBoolean(deduceJtaFromNonJta)))) {
ResourceInfo nonJtaResourceInfo = configFactory.getResourceInfo(nonJtaDataSourceId);
Properties nonJtaProperties = nonJtaResourceInfo.properties;
if (nonJtaProperties.containsKey("JtaManaged")){
// Strategy 1: Best match search
required.clear();
required.put("JtaManaged", "true");
for (String key : asList("JdbcDriver", "JdbcUrl")) {
if (nonJtaProperties.containsKey(key)) required.put(key, nonJtaProperties.get(key));
}
jtaDataSourceId = firstMatching("DataSource", required, null);
// Strategy 2: Copy
if (jtaDataSourceId == null) {
ResourceInfo jtaResourceInfo = copy(nonJtaResourceInfo);
jtaResourceInfo.id = nonJtaResourceInfo.id + "Jta";
Properties overrides = ConfigurationFactory.getSystemProperties(jtaResourceInfo.id, jtaResourceInfo.service);
jtaResourceInfo.properties.putAll(overrides);
jtaResourceInfo.properties.setProperty("JtaManaged", "true");