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;
}
}
//
// 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
//
if (nonJtaDataSourceId != null && jtaDataSourceId == null){
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");