use of org.apache.cayenne.configuration.ConfigurationTree 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.ConfigurationTree in project cayenne by apache.
the class XMLDataChannelDescriptorLoader method load.
@Override
public ConfigurationTree<DataChannelDescriptor> load(Resource configurationResource) throws ConfigurationException {
if (configurationResource == null) {
throw new NullPointerException("Null configurationResource");
}
URL configurationURL = configurationResource.getURL();
logger.info("Loading XML configuration resource from " + configurationURL);
final DataChannelDescriptor descriptor = new DataChannelDescriptor();
descriptor.setConfigurationSource(configurationResource);
descriptor.setName(nameMapper.configurationNodeName(DataChannelDescriptor.class, configurationResource));
try (InputStream in = configurationURL.openStream()) {
XMLReader parser = xmlReaderProvider.get();
LoaderContext loaderContext = new LoaderContext(parser, handlerFactory);
loaderContext.addDataMapListener(dataMap -> descriptor.getDataMaps().add(dataMap));
DataChannelHandler rootHandler = new DataChannelHandler(this, descriptor, loaderContext);
parser.setContentHandler(rootHandler);
parser.setErrorHandler(rootHandler);
InputSource input = new InputSource(in);
input.setSystemId(configurationURL.toString());
parser.parse(input);
loaderContext.dataChannelLoaded(descriptor);
} catch (Exception e) {
throw new ConfigurationException("Error loading configuration from %s", e, configurationURL);
}
// TODO: andrus 03/10/2010 - actually provide load failures here...
return new ConfigurationTree<>(descriptor, null);
}
use of org.apache.cayenne.configuration.ConfigurationTree in project cayenne by apache.
the class ManyToManyCandidateEntityTest method setUp.
@Before
public void setUp() throws Exception {
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(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);
// create and initialize loader instance to test
XMLDataChannelDescriptorLoader loader = new XMLDataChannelDescriptorLoader();
injector.injectMembers(loader);
String testConfigName = "relationship-optimisation";
URL url = getClass().getResource("cayenne-" + testConfigName + ".xml");
ConfigurationTree<DataChannelDescriptor> tree = loader.load(new URLResource(url));
map = tree.getRootNode().getDataMap(testConfigName);
}
use of org.apache.cayenne.configuration.ConfigurationTree in project cayenne by apache.
the class DataDomainProviderTest method testGet.
@Test
public void testGet() {
// create dependencies
final String testConfigName = "testConfig";
final DataChannelDescriptor testDescriptor = new DataChannelDescriptor();
DataMap map1 = new DataMap("map1");
testDescriptor.getDataMaps().add(map1);
DataMap map2 = new DataMap("map2");
testDescriptor.getDataMaps().add(map2);
DataNodeDescriptor nodeDescriptor1 = new DataNodeDescriptor();
nodeDescriptor1.setName("node1");
nodeDescriptor1.getDataMapNames().add("map1");
nodeDescriptor1.setAdapterType(OracleAdapter.class.getName());
nodeDescriptor1.setDataSourceFactoryType(MockDataSourceFactory.class.getName());
nodeDescriptor1.setParameters("jdbc/testDataNode1");
nodeDescriptor1.setSchemaUpdateStrategyType(ThrowOnPartialOrCreateSchemaStrategy.class.getName());
testDescriptor.getNodeDescriptors().add(nodeDescriptor1);
DataNodeDescriptor nodeDescriptor2 = new DataNodeDescriptor();
nodeDescriptor2.setName("node2");
nodeDescriptor2.getDataMapNames().add("map2");
nodeDescriptor2.setParameters("testDataNode2.driver.xml");
testDescriptor.getNodeDescriptors().add(nodeDescriptor2);
final DataChannelDescriptorLoader testLoader = new DataChannelDescriptorLoader() {
@Override
public ConfigurationTree<DataChannelDescriptor> load(Resource configurationResource) throws ConfigurationException {
return new ConfigurationTree<>(testDescriptor, null);
}
};
final EventManager eventManager = new MockEventManager();
final TestListener mockListener = new TestListener();
Module testModule = binder -> {
final ClassLoaderManager classLoaderManager = new DefaultClassLoaderManager();
binder.bind(ClassLoaderManager.class).toInstance(classLoaderManager);
binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
ServerModule.contributeProperties(binder);
ServerModule.contributeAdapterDetectors(binder).add(FirebirdSniffer.class).add(OpenBaseSniffer.class).add(FrontBaseSniffer.class).add(IngresSniffer.class).add(SQLiteSniffer.class).add(DB2Sniffer.class).add(H2Sniffer.class).add(HSQLDBSniffer.class).add(SybaseSniffer.class).add(DerbySniffer.class).add(SQLServerSniffer.class).add(OracleSniffer.class).add(PostgresSniffer.class).add(MySQLSniffer.class).add(MariaDBSniffer.class);
ServerModule.contributeDomainFilters(binder);
ServerModule.contributeDomainQueryFilters(binder);
ServerModule.contributeDomainSyncFilters(binder);
ServerModule.contributeDomainListeners(binder).add(mockListener);
ServerModule.contributeProjectLocations(binder).add(testConfigName);
binder.bind(PkGenerator.class).to(JdbcPkGenerator.class);
binder.bind(PkGeneratorFactoryProvider.class).to(PkGeneratorFactoryProvider.class);
ServerModule.contributePkGenerators(binder).put(DB2Adapter.class.getName(), DB2PkGenerator.class).put(DerbyAdapter.class.getName(), DerbyPkGenerator.class).put(FrontBaseAdapter.class.getName(), FrontBasePkGenerator.class).put(H2Adapter.class.getName(), H2PkGenerator.class).put(IngresAdapter.class.getName(), IngresPkGenerator.class).put(MySQLAdapter.class.getName(), MySQLPkGenerator.class).put(OpenBaseAdapter.class.getName(), OpenBasePkGenerator.class).put(OracleAdapter.class.getName(), OraclePkGenerator.class).put(Oracle8Adapter.class.getName(), OraclePkGenerator.class).put(PostgresAdapter.class.getName(), PostgresPkGenerator.class).put(SQLServerAdapter.class.getName(), SybasePkGenerator.class).put(SybaseAdapter.class.getName(), SybasePkGenerator.class);
// configure extended types
ServerModule.contributeDefaultTypes(binder);
ServerModule.contributeUserTypes(binder);
ServerModule.contributeTypeFactories(binder);
binder.bind(EventManager.class).toInstance(eventManager);
binder.bind(EntitySorter.class).toInstance(new AshwoodEntitySorter());
binder.bind(SchemaUpdateStrategyFactory.class).to(DefaultSchemaUpdateStrategyFactory.class);
final ResourceLocator locator = new ClassLoaderResourceLocator(classLoaderManager) {
public Collection<Resource> findResources(String name) {
// if this is the request we are getting, just let it go through..
if (name.endsWith("types.xml")) {
return super.findResources(name);
}
assertEquals(testConfigName, name);
return Collections.<Resource>singleton(new MockResource());
}
};
binder.bind(ResourceLocator.class).toInstance(locator);
binder.bind(Key.get(ResourceLocator.class, Constants.SERVER_RESOURCE_LOCATOR)).toInstance(locator);
binder.bind(ConfigurationNameMapper.class).to(DefaultConfigurationNameMapper.class);
binder.bind(DataChannelDescriptorMerger.class).to(DefaultDataChannelDescriptorMerger.class);
binder.bind(DataChannelDescriptorLoader.class).toInstance(testLoader);
binder.bind(DbAdapterFactory.class).to(DefaultDbAdapterFactory.class);
binder.bind(RuntimeProperties.class).to(DefaultRuntimeProperties.class);
binder.bind(BatchTranslatorFactory.class).to(DefaultBatchTranslatorFactory.class);
binder.bind(SelectTranslatorFactory.class).to(DefaultSelectTranslatorFactory.class);
binder.bind(DataSourceFactory.class).toInstance(new MockDataSourceFactory());
binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
binder.bind(QueryCache.class).toInstance(mock(QueryCache.class));
binder.bind(RowReaderFactory.class).toInstance(mock(RowReaderFactory.class));
binder.bind(DataNodeFactory.class).to(DefaultDataNodeFactory.class);
binder.bind(SQLTemplateProcessor.class).toInstance(mock(SQLTemplateProcessor.class));
binder.bind(EventBridge.class).toProvider(NoopEventBridgeProvider.class);
binder.bind(DataRowStoreFactory.class).to(DefaultDataRowStoreFactory.class);
ServerModule.contributeValueObjectTypes(binder);
binder.bind(ValueObjectTypeRegistry.class).to(DefaultValueObjectTypeRegistry.class);
binder.bind(ValueComparisonStrategyFactory.class).to(DefaultValueComparisonStrategyFactory.class);
};
Injector injector = DIBootstrap.createInjector(testModule);
// create and initialize provide instance to test
DataDomainProvider provider = new DataDomainProvider();
injector.injectMembers(provider);
DataChannel channel = provider.get();
assertNotNull(channel);
assertTrue(channel instanceof DataDomain);
DataDomain domain = (DataDomain) channel;
assertSame(eventManager, domain.getEventManager());
assertEquals(2, domain.getDataMaps().size());
assertTrue(domain.getDataMaps().contains(map1));
assertTrue(domain.getDataMaps().contains(map2));
assertEquals(2, domain.getDataNodes().size());
DataNode node1 = domain.getDataNode("node1");
assertNotNull(node1);
assertEquals(1, node1.getDataMaps().size());
assertSame(map1, node1.getDataMaps().iterator().next());
assertSame(node1, domain.lookupDataNode(map1));
assertEquals(nodeDescriptor1.getDataSourceFactoryType(), node1.getDataSourceFactory());
assertNotNull(node1.getDataSource());
assertNotNull(node1.getSchemaUpdateStrategy());
assertEquals(nodeDescriptor1.getSchemaUpdateStrategyType(), node1.getSchemaUpdateStrategy().getClass().getName());
assertNotNull(node1.getAdapter());
assertEquals(OracleAdapter.class, node1.getAdapter().getClass());
DataNode node2 = domain.getDataNode("node2");
assertNotNull(node2);
assertEquals(1, node2.getDataMaps().size());
assertSame(map2, node2.getDataMaps().iterator().next());
assertSame(node2, domain.lookupDataNode(map2));
assertNull(node2.getDataSourceFactory());
assertNotNull(node2.getDataSource());
assertNotNull(node2.getSchemaUpdateStrategy());
assertEquals(SkipSchemaUpdateStrategy.class.getName(), node2.getSchemaUpdateStrategy().getClass().getName());
assertNotNull(node2.getAdapter());
// check that we have mock listener passed correctly
Persistent mockPersistent = mock(Persistent.class);
ObjectId mockObjectId = mock(ObjectId.class);
when(mockObjectId.getEntityName()).thenReturn("mock-entity-name");
when(mockPersistent.getObjectId()).thenReturn(mockObjectId);
domain.getEntityResolver().getCallbackRegistry().performCallbacks(LifecycleEvent.POST_LOAD, mockPersistent);
assertEquals("Should call postLoadCallback() method", 1, TestListener.counter.get());
}
Aggregations