Search in sources :

Example 16 with Injector

use of org.apache.cayenne.di.Injector in project cayenne by apache.

the class DbGeneratorMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    Logger logger = new MavenLogger(this);
    // check missing data source parameters
    dataSource.validate();
    Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(logger));
    AdhocObjectFactory objectFactory = injector.getInstance(AdhocObjectFactory.class);
    logger.info(String.format("connection settings - [driver: %s, url: %s, username: %s]", dataSource.getDriver(), dataSource.getUrl(), dataSource.getUsername()));
    logger.info(String.format("generator options - [dropTables: %s, dropPK: %s, createTables: %s, createPK: %s, createFK: %s]", dropTables, dropPK, createTables, createPK, createFK));
    try {
        final DbAdapter adapterInst = (adapter == null) ? objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName()) : objectFactory.newInstance(DbAdapter.class, adapter);
        // Load the data map and run the db generator.
        DataMap dataMap = loadDataMap(injector);
        DbGenerator generator = new DbGenerator(adapterInst, dataMap, NoopJdbcEventLogger.getInstance());
        generator.setShouldCreateFKConstraints(createFK);
        generator.setShouldCreatePKSupport(createPK);
        generator.setShouldCreateTables(createTables);
        generator.setShouldDropPKSupport(dropPK);
        generator.setShouldDropTables(dropTables);
        // load driver taking custom CLASSPATH into account...
        DriverDataSource driverDataSource = new DriverDataSource((Driver) Class.forName(dataSource.getDriver()).newInstance(), dataSource.getUrl(), dataSource.getUsername(), dataSource.getPassword());
        generator.runGenerator(driverDataSource);
    } catch (Exception ex) {
        Throwable th = Util.unwindException(ex);
        String message = "Error generating database";
        if (th.getLocalizedMessage() != null) {
            message += ": " + th.getLocalizedMessage();
        }
        logger.error(message);
        throw new MojoExecutionException(message, th);
    }
}
Also used : DriverDataSource(org.apache.cayenne.datasource.DriverDataSource) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Logger(org.slf4j.Logger) NoopJdbcEventLogger(org.apache.cayenne.log.NoopJdbcEventLogger) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) DataMap(org.apache.cayenne.map.DataMap) DbAdapter(org.apache.cayenne.dba.DbAdapter) Injector(org.apache.cayenne.di.Injector) DbSyncModule(org.apache.cayenne.dbsync.DbSyncModule) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) DbGenerator(org.apache.cayenne.access.DbGenerator)

Example 17 with Injector

use of org.apache.cayenne.di.Injector in project cayenne by apache.

the class Main method launch.

protected void launch() {
    // TODO: use module auto-loading...
    final Injector injector = DIBootstrap.createInjector(appendModules(new ArrayList<Module>()));
    // init look and feel before using any Swing classes...
    injector.getInstance(PlatformInitializer.class).initLookAndFeel();
    // logger should go after Look And Feel or Logger Console will be without style
    logger.info("Starting CayenneModeler.");
    logger.info("JRE v." + System.getProperty("java.version") + " at " + System.getProperty("java.home"));
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            Application application = injector.getInstance(Application.class);
            Application.setInstance(application);
            application.startup();
            // start initial project AFTER the app startup, as we need Application
            // preferences to be bootstrapped.
            File project = initialProjectFromArgs();
            if (project == null) {
                project = initialProjectFromPreferences();
            }
            if (project != null) {
                new OpenProjectAction(application).openProject(project);
            }
        }
    });
}
Also used : OpenProjectAction(org.apache.cayenne.modeler.action.OpenProjectAction) Injector(org.apache.cayenne.di.Injector) PlatformInitializer(org.apache.cayenne.modeler.init.platform.PlatformInitializer) ArrayList(java.util.ArrayList) File(java.io.File)

Example 18 with Injector

use of org.apache.cayenne.di.Injector in project cayenne by apache.

the class CAY_743Test method testLoad2MapsWithCrossMapInheritance.

@Test
public void testLoad2MapsWithCrossMapInheritance() throws Exception {
    Injector injector = DIBootstrap.createInjector(new ServerModule() {

        @Override
        public void configure(Binder binder) {
            super.configure(binder);
            ServerModule.contributeProjectLocations(binder).add("cay743/cayenne-domain.xml");
        }
    });
    try {
        DataDomain domain = injector.getInstance(DataDomain.class);
        assertEquals(2, domain.getDataMaps().size());
        DataMap m1 = domain.getDataMap("map1");
        DataMap m2 = domain.getDataMap("map2");
        ObjEntity oe11 = m1.getObjEntity("Entity11");
        ObjEntity oe12 = m1.getObjEntity("Entity12");
        ObjEntity oe21 = m2.getObjEntity("Entity21");
        ObjEntity oe22 = m2.getObjEntity("Entity22");
        // this causes StackOverflow per CAY-743
        ObjEntity oe21Super = oe21.getSuperEntity();
        ObjEntity oe12Super = oe12.getSuperEntity();
        assertSame(oe12Super, oe22);
        assertSame(oe21Super, oe11);
    } finally {
        injector.shutdown();
    }
}
Also used : Binder(org.apache.cayenne.di.Binder) ObjEntity(org.apache.cayenne.map.ObjEntity) DataDomain(org.apache.cayenne.access.DataDomain) Injector(org.apache.cayenne.di.Injector) ServerModule(org.apache.cayenne.configuration.server.ServerModule) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 19 with Injector

use of org.apache.cayenne.di.Injector in project cayenne by apache.

the class DataContextFactoryTest method testCreateDataContextWithDedicatedCache.

@Test
public void testCreateDataContextWithDedicatedCache() throws Exception {
    final EventManager eventManager = new MockEventManager();
    final DataDomain domain = new DataDomain("d1");
    domain.setSharedCacheEnabled(false);
    Module testModule = binder -> {
        binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
        binder.bind(DataDomain.class).toInstance(domain);
        binder.bind(EventManager.class).toInstance(eventManager);
        binder.bind(QueryCache.class).toInstance(new MapQueryCache(5));
        binder.bind(RuntimeProperties.class).toInstance(new DefaultRuntimeProperties(Collections.<String, String>emptyMap()));
        binder.bind(ObjectMapRetainStrategy.class).to(DefaultObjectMapRetainStrategy.class);
        binder.bind(ObjectStoreFactory.class).to(DefaultObjectStoreFactory.class);
        binder.bind(TransactionFactory.class).to(DefaultTransactionFactory.class);
        binder.bind(TransactionManager.class).to(DefaultTransactionManager.class);
        binder.bind(DataRowStoreFactory.class).to(DefaultDataRowStoreFactory.class);
        binder.bind(EventBridge.class).toProvider(NoopEventBridgeProvider.class);
        binder.bind(DataRowStoreFactory.class).to(DefaultDataRowStoreFactory.class);
    };
    Injector injector = DIBootstrap.createInjector(testModule);
    DataContextFactory factory = new DataContextFactory();
    injector.injectMembers(factory);
    DataContext c3 = (DataContext) factory.createContext();
    assertNotNull(c3.getObjectStore().getDataRowCache());
    assertNull(domain.getSharedSnapshotCache());
    assertNotSame(c3.getObjectStore().getDataRowCache(), domain.getSharedSnapshotCache());
}
Also used : RuntimeProperties(org.apache.cayenne.configuration.RuntimeProperties) Injector(org.apache.cayenne.di.Injector) DataContext(org.apache.cayenne.access.DataContext) DataRowStoreFactory(org.apache.cayenne.access.DataRowStoreFactory) MapQueryCache(org.apache.cayenne.cache.MapQueryCache) TransactionManager(org.apache.cayenne.tx.TransactionManager) QueryCache(org.apache.cayenne.cache.QueryCache) Module(org.apache.cayenne.di.Module) ObjectMapRetainStrategy(org.apache.cayenne.access.ObjectMapRetainStrategy) ObjectStoreFactory(org.apache.cayenne.configuration.ObjectStoreFactory) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) DataDomain(org.apache.cayenne.access.DataDomain) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) EventBridge(org.apache.cayenne.event.EventBridge) MockEventManager(org.apache.cayenne.event.MockEventManager) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) TransactionFactory(org.apache.cayenne.tx.TransactionFactory) DefaultDataRowStoreFactory(org.apache.cayenne.access.DefaultDataRowStoreFactory) DIBootstrap(org.apache.cayenne.di.DIBootstrap) DefaultTransactionManager(org.apache.cayenne.tx.DefaultTransactionManager) DefaultTransactionFactory(org.apache.cayenne.tx.DefaultTransactionFactory) Test(org.junit.Test) DefaultObjectMapRetainStrategy(org.apache.cayenne.access.DefaultObjectMapRetainStrategy) EventManager(org.apache.cayenne.event.EventManager) NoopEventBridgeProvider(org.apache.cayenne.event.NoopEventBridgeProvider) Assert(org.junit.Assert) Collections(java.util.Collections) DefaultObjectStoreFactory(org.apache.cayenne.configuration.DefaultObjectStoreFactory) DataDomain(org.apache.cayenne.access.DataDomain) MockEventManager(org.apache.cayenne.event.MockEventManager) EventManager(org.apache.cayenne.event.EventManager) DefaultObjectStoreFactory(org.apache.cayenne.configuration.DefaultObjectStoreFactory) MapQueryCache(org.apache.cayenne.cache.MapQueryCache) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) DefaultDataRowStoreFactory(org.apache.cayenne.access.DefaultDataRowStoreFactory) DefaultTransactionManager(org.apache.cayenne.tx.DefaultTransactionManager) DataContext(org.apache.cayenne.access.DataContext) DefaultObjectMapRetainStrategy(org.apache.cayenne.access.DefaultObjectMapRetainStrategy) DefaultTransactionFactory(org.apache.cayenne.tx.DefaultTransactionFactory) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) Injector(org.apache.cayenne.di.Injector) NoopEventBridgeProvider(org.apache.cayenne.event.NoopEventBridgeProvider) Module(org.apache.cayenne.di.Module) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 20 with Injector

use of org.apache.cayenne.di.Injector in project cayenne by apache.

the class DefaultDataSourceFactoryLoaderTest method testGetDataSourceFactory_UnusedProperties.

@Test
public void testGetDataSourceFactory_UnusedProperties() throws Exception {
    final RuntimeProperties properties = mock(RuntimeProperties.class);
    when(properties.get(Constants.JDBC_DRIVER_PROPERTY)).thenReturn("x");
    when(properties.get(Constants.JDBC_URL_PROPERTY)).thenReturn(null);
    when(properties.get(Constants.JDBC_USERNAME_PROPERTY)).thenReturn("username");
    when(properties.get(Constants.JDBC_PASSWORD_PROPERTY)).thenReturn("12345");
    DataChannelDescriptor channelDescriptor = new DataChannelDescriptor();
    channelDescriptor.setName("X");
    DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor();
    nodeDescriptor.setName("node1");
    nodeDescriptor.setDataSourceFactoryType(MockDataSourceFactory1.class.getName());
    nodeDescriptor.setDataChannelDescriptor(channelDescriptor);
    Module testModule = binder -> {
        binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
        binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
        binder.bind(ResourceLocator.class).to(MockResourceLocator.class);
        binder.bind(Key.get(ResourceLocator.class, Constants.SERVER_RESOURCE_LOCATOR)).to(MockResourceLocator.class);
        binder.bind(RuntimeProperties.class).toInstance(properties);
        binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
    };
    Injector injector = DIBootstrap.createInjector(testModule);
    DelegatingDataSourceFactory factoryLoader = new DelegatingDataSourceFactory();
    injector.injectMembers(factoryLoader);
    DataSourceFactory factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
    assertNotNull(factory);
    assertFalse(factory instanceof PropertyDataSourceFactory);
    nodeDescriptor.setName("node2");
    when(properties.get(Constants.JDBC_MIN_CONNECTIONS_PROPERTY + ".X.node2")).thenReturn("3");
    when(properties.get(Constants.JDBC_PASSWORD_PROPERTY + ".X.node2")).thenReturn("123456");
    factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
    assertNotNull(factory);
    assertFalse(factory instanceof PropertyDataSourceFactory);
    nodeDescriptor.setName("node3");
    when(properties.get(Constants.JDBC_URL_PROPERTY + ".X.node3")).thenReturn("url");
    factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
    assertNotNull(factory);
    assertTrue(factory instanceof PropertyDataSourceFactory);
}
Also used : RuntimeProperties(org.apache.cayenne.configuration.RuntimeProperties) Injector(org.apache.cayenne.di.Injector) MockDataSourceFactory1(org.apache.cayenne.configuration.mock.MockDataSourceFactory1) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) Module(org.apache.cayenne.di.Module) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) Assert.assertSame(org.junit.Assert.assertSame) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) DIBootstrap(org.apache.cayenne.di.DIBootstrap) Key(org.apache.cayenne.di.Key) Before(org.junit.Before) ResourceLocator(org.apache.cayenne.resource.ResourceLocator) Assert.assertNotNull(org.junit.Assert.assertNotNull) MockResourceLocator(org.apache.cayenne.resource.mock.MockResourceLocator) Constants(org.apache.cayenne.configuration.Constants) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ClassLoaderManager(org.apache.cayenne.di.ClassLoaderManager) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) DataSourceInfo(org.apache.cayenne.conn.DataSourceInfo) Assert.assertFalse(org.junit.Assert.assertFalse) Mockito.mock(org.mockito.Mockito.mock) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) Injector(org.apache.cayenne.di.Injector) MockDataSourceFactory1(org.apache.cayenne.configuration.mock.MockDataSourceFactory1) Module(org.apache.cayenne.di.Module) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) RuntimeProperties(org.apache.cayenne.configuration.RuntimeProperties) MockResourceLocator(org.apache.cayenne.resource.mock.MockResourceLocator) ResourceLocator(org.apache.cayenne.resource.ResourceLocator) MockResourceLocator(org.apache.cayenne.resource.mock.MockResourceLocator) Test(org.junit.Test)

Aggregations

Injector (org.apache.cayenne.di.Injector)64 Test (org.junit.Test)52 Module (org.apache.cayenne.di.Module)27 DIBootstrap (org.apache.cayenne.di.DIBootstrap)23 URL (java.net.URL)15 ToolsModule (org.apache.cayenne.dbsync.reverse.configuration.ToolsModule)15 DataMap (org.apache.cayenne.map.DataMap)15 RuntimeProperties (org.apache.cayenne.configuration.RuntimeProperties)14 AdhocObjectFactory (org.apache.cayenne.di.AdhocObjectFactory)14 JdbcEventLogger (org.apache.cayenne.log.JdbcEventLogger)14 Slf4jJdbcEventLogger (org.apache.cayenne.log.Slf4jJdbcEventLogger)14 URLResource (org.apache.cayenne.resource.URLResource)14 Assert (org.junit.Assert)14 DefaultAdhocObjectFactory (org.apache.cayenne.di.spi.DefaultAdhocObjectFactory)13 DefaultRuntimeProperties (org.apache.cayenne.configuration.DefaultRuntimeProperties)12 DbSyncModule (org.apache.cayenne.dbsync.DbSyncModule)12 ClassLoaderManager (org.apache.cayenne.di.ClassLoaderManager)12 DefaultClassLoaderManager (org.apache.cayenne.di.spi.DefaultClassLoaderManager)12 Logger (org.slf4j.Logger)11 Constants (org.apache.cayenne.configuration.Constants)10