use of org.apache.cayenne.configuration.DataChannelDescriptor in project cayenne by apache.
the class CompatibilityDataChannelDescriptorLoader method load.
@Override
public ConfigurationTree<DataChannelDescriptor> load(Resource configurationResource) throws ConfigurationException {
if (configurationResource == null) {
throw new NullPointerException("Null configurationResource");
}
if (!(upgradeServiceProvider.get() instanceof CompatibilityUpgradeService)) {
throw new ConfigurationException("CompatibilityUpgradeService expected");
}
CompatibilityUpgradeService upgradeService = (CompatibilityUpgradeService) upgradeServiceProvider.get();
UpgradeMetaData metaData = upgradeService.getUpgradeType(configurationResource);
if (metaData.getUpgradeType() == UpgradeType.UPGRADE_NOT_NEEDED) {
return super.load(configurationResource);
}
if (metaData.getUpgradeType() == UpgradeType.DOWNGRADE_NEEDED) {
throw new ConfigurationException("Unable to load configuration from %s: " + "It was created using a newer version of the Modeler", configurationResource.getURL());
}
if (metaData.getUpgradeType() == UpgradeType.INTERMEDIATE_UPGRADE_NEEDED) {
throw new ConfigurationException("Unable to load configuration from %s: " + "Open the project in the older Modeler to do an intermediate upgrade.", configurationResource.getURL());
}
URL configurationURL = configurationResource.getURL();
upgradeService.upgradeProject(configurationResource);
Document projectDocument = documentProvider.getDocument(configurationURL);
if (projectDocument == null) {
throw new ConfigurationException("Unable to upgrade " + configurationURL);
}
logger.info("Loading XML configuration resource from " + configurationURL);
final DataChannelDescriptor descriptor = new DataChannelDescriptor();
descriptor.setConfigurationSource(configurationResource);
descriptor.setName(nameMapper.configurationNodeName(DataChannelDescriptor.class, configurationResource));
try {
DOMSource source = new DOMSource(projectDocument);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
TransformerFactory transFactory = TransformerFactory.newInstance();
transFactory.newTransformer().transform(source, new StreamResult(baos));
InputSource isource = new InputSource(source.getSystemId());
isource.setByteStream(new ByteArrayInputStream(baos.toByteArray()));
XMLReader parser = Util.createXmlReader();
LoaderContext loaderContext = new LoaderContext(parser, handlerFactory);
loaderContext.addDataMapListener(new DataMapLoaderListener() {
@Override
public void onDataMapLoaded(DataMap dataMap) {
descriptor.getDataMaps().add(dataMap);
}
});
DataChannelHandler rootHandler = new DataChannelHandler(this, descriptor, loaderContext);
parser.setContentHandler(rootHandler);
parser.setErrorHandler(rootHandler);
parser.parse(isource);
} catch (Exception e) {
throw new ConfigurationException("Error loading configuration from %s", e, configurationURL);
}
// Finally upgrade model, if needed
upgradeService.upgradeModel(configurationResource, descriptor);
return new ConfigurationTree<>(descriptor, null);
}
use of org.apache.cayenne.configuration.DataChannelDescriptor in project cayenne by apache.
the class CompatibilityDataChannelDescriptorLoaderIT method testLoad.
@Test
public void testLoad() throws Exception {
Injector injector = getInjector();
DataChannelDescriptorLoader loader = injector.getInstance(DataChannelDescriptorLoader.class);
assertTrue(loader instanceof CompatibilityDataChannelDescriptorLoader);
URL resourceUrl = getClass().getResource("../../project/compatibility/cayenne-project-v6.xml");
Resource resource = new URLResource(resourceUrl);
ConfigurationTree<DataChannelDescriptor> configurationTree = loader.load(resource);
assertNotNull(configurationTree.getRootNode());
assertTrue(configurationTree.getLoadFailures().isEmpty());
assertEquals(1, configurationTree.getRootNode().getDataMaps().size());
DataMap dataMap = configurationTree.getRootNode().getDataMaps().iterator().next();
assertEquals(1, dataMap.getDbEntities().size());
assertEquals(1, dataMap.getObjEntities().size());
assertNotNull(dataMap.getObjEntity("Artist"));
assertNotNull(dataMap.getDbEntity("Artist"));
assertEquals(2, dataMap.getDbEntity("Artist").getAttributes().size());
}
use of org.apache.cayenne.configuration.DataChannelDescriptor in project cayenne by apache.
the class DataMapValidator method validateName.
private void validateName(DataMap map, ValidationResult validationResult) {
String name = map.getName();
if (Util.isEmptyString(name)) {
addFailure(validationResult, map, "Unnamed DataMap");
return;
}
DataChannelDescriptor domain = map.getDataChannelDescriptor();
if (domain == null) {
return;
}
// check for duplicate names in the parent context
for (DataMap otherMap : domain.getDataMaps()) {
if (otherMap == map) {
continue;
}
if (name.equals(otherMap.getName())) {
addFailure(validationResult, map, "Duplicate DataMap name: %s", name);
return;
}
}
}
use of org.apache.cayenne.configuration.DataChannelDescriptor in project cayenne by apache.
the class DataChannelProjectLoaderTest method testLoad.
@Test
public void testLoad() {
DataChannelProjectLoader loader = new DataChannelProjectLoader();
Module testModule = binder -> {
binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
binder.bind(DataMapLoader.class).to(XMLDataMapLoader.class);
binder.bind(DataChannelDescriptorLoader.class).to(XMLDataChannelDescriptorLoader.class);
binder.bind(ConfigurationNameMapper.class).to(DefaultConfigurationNameMapper.class);
binder.bind(HandlerFactory.class).to(DefaultHandlerFactory.class);
binder.bind(DataChannelMetaData.class).to(NoopDataChannelMetaData.class);
binder.bind(XMLReader.class).toProviderInstance(new XMLReaderProvider(false)).withoutScope();
};
Injector injector = DIBootstrap.createInjector(testModule);
injector.injectMembers(loader);
String testConfigName = "PROJECT1";
String baseUrl = getClass().getPackage().getName().replace('.', '/');
URL url = getClass().getClassLoader().getResource(baseUrl + "/cayenne-" + testConfigName + ".xml");
Resource rootSource = new URLResource(url);
Project project = loader.loadProject(rootSource);
assertNotNull(project);
DataChannelDescriptor rootNode = (DataChannelDescriptor) project.getRootNode();
assertNotNull(rootNode);
assertSame(rootSource, rootNode.getConfigurationSource());
assertNotNull(project.getConfigurationResource());
assertEquals(project.getConfigurationResource(), rootSource);
}
use of org.apache.cayenne.configuration.DataChannelDescriptor in project cayenne by apache.
the class NameBuilderTest method testName_DataChannelDescriptorContext.
@Test
public void testName_DataChannelDescriptorContext() throws Exception {
DataChannelDescriptor descriptor = new DataChannelDescriptor();
DataMap m0 = new DataMap();
m0.setName(NameBuilder.builder(m0).in(descriptor).name());
assertEquals("datamap", m0.getName());
descriptor.getDataMaps().add(m0);
DataMap m1 = new DataMap();
m1.setName(NameBuilder.builder(m1).in(descriptor).name());
assertEquals("datamap1", m1.getName());
descriptor.getDataMaps().add(m1);
DataNodeDescriptor nd0 = new DataNodeDescriptor();
nd0.setName(NameBuilder.builder(nd0).in(descriptor).name());
assertEquals("datanode", nd0.getName());
descriptor.getNodeDescriptors().add(nd0);
DataNodeDescriptor nd1 = new DataNodeDescriptor();
nd1.setName(NameBuilder.builder(nd1).in(descriptor).name());
assertEquals("datanode1", nd1.getName());
descriptor.getNodeDescriptors().add(nd1);
}
Aggregations