Search in sources :

Example 11 with URLResource

use of org.apache.cayenne.resource.URLResource in project cayenne by apache.

the class DBCP2DataSourceFactoryTest method testGetDataSource.

@Test
public void testGetDataSource() throws Exception {
    String baseUrl = getClass().getPackage().getName().replace('.', '/');
    URL url = getClass().getClassLoader().getResource(baseUrl + "/");
    assertNotNull(url);
    DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor();
    nodeDescriptor.setConfigurationSource(new URLResource(url));
    nodeDescriptor.setParameters("testDBCP2.properties");
    DBCPDataSourceFactory factory = new DBCPDataSourceFactory();
    DataSource dataSource = factory.getDataSource(nodeDescriptor);
    assertNotNull(dataSource);
    assertTrue(dataSource instanceof BasicDataSource);
    try (BasicDataSource basicDataSource = (BasicDataSource) dataSource) {
        assertEquals("com.example.jdbc.Driver", basicDataSource.getDriverClassName());
        assertEquals("jdbc:somedb://localhost/cayenne", basicDataSource.getUrl());
        assertEquals("john", basicDataSource.getUsername());
        assertEquals("secret", basicDataSource.getPassword());
        assertEquals(20, basicDataSource.getMaxTotal());
        assertEquals(5, basicDataSource.getMinIdle());
        assertEquals(8, basicDataSource.getMaxIdle());
        assertEquals(10000, basicDataSource.getMaxWaitMillis());
        assertEquals("select 1 from xyz;", basicDataSource.getValidationQuery());
    }
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) URL(java.net.URL) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 12 with URLResource

use of org.apache.cayenne.resource.URLResource in project cayenne by apache.

the class DefaultDbImportAction method saveLoaded.

/**
 * Save imported data.
 * This can create DataMap and/or Project files.
 */
protected void saveLoaded(DataMap dataMap, DbImportConfiguration config) throws MalformedURLException {
    ConfigurationTree<ConfigurationNode> projectRoot;
    if (config.getCayenneProject() == null) {
        // Old version of cdbimport, no Cayenne project, need to save only DataMap
        projectRoot = new ConfigurationTree<>(dataMap);
    } else {
        // Cayenne project is present
        DataChannelDescriptor dataChannelDescriptor;
        if (config.getCayenneProject().exists()) {
            // Cayenne project file exists, need to read it and push DataMap inside
            URLResource configurationResource = new URLResource(config.getCayenneProject().toURI().toURL());
            ConfigurationTree<DataChannelDescriptor> configurationTree = dataChannelDescriptorLoader.load(configurationResource);
            if (!configurationTree.getLoadFailures().isEmpty()) {
                throw new CayenneRuntimeException("Unable to load cayenne project %s, %s", config.getCayenneProject(), configurationTree.getLoadFailures().iterator().next().getDescription());
            }
            dataChannelDescriptor = configurationTree.getRootNode();
            // remove old copy of DataMap if it's there
            DataMap oldDataMap = dataChannelDescriptor.getDataMap(dataMap.getName());
            if (oldDataMap != null) {
                dataChannelDescriptor.getDataMaps().remove(oldDataMap);
            }
        } else {
            // No project file yet, can simply create empty project with resulting DataMap
            dataChannelDescriptor = new DataChannelDescriptor();
            dataChannelDescriptor.setName(getProjectNameFromFileName(config.getCayenneProject().getName()));
            dataChannelDescriptor.setConfigurationSource(new URLResource(config.getCayenneProject().toURI().toURL()));
            logger.info("Project file does not exist. New project will be saved into '" + config.getCayenneProject().getAbsolutePath());
        }
        dataChannelDescriptor.getDataMaps().add(dataMap);
        projectRoot = new ConfigurationTree<>(dataChannelDescriptor);
    }
    Project project = new Project(projectRoot);
    projectSaver.save(project);
    logger.info("");
    logger.info("All changes saved.");
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) Project(org.apache.cayenne.project.Project) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) ConfigurationNode(org.apache.cayenne.configuration.ConfigurationNode) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DataMap(org.apache.cayenne.map.DataMap)

Example 13 with URLResource

use of org.apache.cayenne.resource.URLResource in project cayenne by apache.

the class DefaultDbImportAction method existingTargetMap.

protected DataMap existingTargetMap(DbImportConfiguration configuration) throws IOException {
    File file = configuration.getTargetDataMap();
    if (file != null && file.exists() && file.canRead()) {
        URLResource configurationResource = new URLResource(file.toURI().toURL());
        DataMap dataMap = mapLoader.load(configurationResource);
        dataMap.setNamespace(new EntityResolver(Collections.singleton(dataMap)));
        dataMap.setConfigurationSource(configurationResource);
        return dataMap;
    }
    return null;
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) EntityResolver(org.apache.cayenne.map.EntityResolver) File(java.io.File) DataMap(org.apache.cayenne.map.DataMap)

Example 14 with URLResource

use of org.apache.cayenne.resource.URLResource in project cayenne by apache.

the class OpenProjectAction method openProject.

/**
 * Opens specified project file. File must already exist.
 */
public void openProject(File file) {
    try {
        if (!file.exists()) {
            JOptionPane.showMessageDialog(Application.getFrame(), "Can't open project - file \"" + file.getPath() + "\" does not exist", "Can't Open Project", JOptionPane.ERROR_MESSAGE);
            return;
        }
        CayenneModelerController controller = Application.getInstance().getFrameController();
        controller.addToLastProjListAction(file);
        URL url = file.toURI().toURL();
        Resource rootSource = new URLResource(url);
        UpgradeService upgradeService = getApplication().getInjector().getInstance(UpgradeService.class);
        UpgradeMetaData metaData = upgradeService.getUpgradeType(rootSource);
        switch(metaData.getUpgradeType()) {
            case INTERMEDIATE_UPGRADE_NEEDED:
                String modelerVersion = PROJECT_TO_MODELER_VERSION.get(metaData.getProjectVersion());
                if (modelerVersion == null) {
                    modelerVersion = "";
                }
                JOptionPane.showMessageDialog(Application.getFrame(), "Open the project in the older Modeler " + modelerVersion + " to do an intermediate upgrade\nbefore you can upgrade to latest version.", "Can't Upgrade Project", JOptionPane.ERROR_MESSAGE);
                closeProject(false);
                return;
            case DOWNGRADE_NEEDED:
                JOptionPane.showMessageDialog(Application.getFrame(), "Can't open project - it was created using a newer version of the Modeler", "Can't Open Project", JOptionPane.ERROR_MESSAGE);
                closeProject(false);
                return;
            case UPGRADE_NEEDED:
                if (processUpgrades()) {
                    rootSource = upgradeService.upgradeProject(rootSource);
                } else {
                    closeProject(false);
                    return;
                }
                break;
        }
        openProjectResourse(rootSource, controller);
    } catch (Exception ex) {
        logObj.warn("Error loading project file.", ex);
        ErrorDebugDialog.guiWarning(ex, "Error loading project");
    }
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) UpgradeService(org.apache.cayenne.project.upgrade.UpgradeService) CayenneModelerController(org.apache.cayenne.modeler.CayenneModelerController) URLResource(org.apache.cayenne.resource.URLResource) Resource(org.apache.cayenne.resource.Resource) UpgradeMetaData(org.apache.cayenne.project.upgrade.UpgradeMetaData) URL(java.net.URL)

Example 15 with URLResource

use of org.apache.cayenne.resource.URLResource in project cayenne by apache.

the class SchemaBuilder method rebuildSchema.

/**
 * Completely rebuilds test schema.
 */
// TODO - this method changes the internal state of the object ... refactor
public void rebuildSchema() {
    // generate schema combining all DataMaps that require schema support.
    // Schema generation is done like that instead of per DataMap on demand
    // to avoid conflicts when dropping and generating PK objects.
    DataMap[] maps = new DataMap[MAPS_REQUIRING_SCHEMA_SETUP.length];
    for (int i = 0; i < maps.length; i++) {
        URL mapURL = getClass().getClassLoader().getResource(MAPS_REQUIRING_SCHEMA_SETUP[i]);
        maps[i] = loader.load(new URLResource(mapURL));
    }
    this.domain = new DataDomain("temp");
    domain.setEventManager(new DefaultEventManager(2));
    domain.setEntitySorter(new AshwoodEntitySorter());
    domain.setQueryCache(new MapQueryCache(50));
    try {
        for (DataMap map : maps) {
            initNode(map);
        }
        if ("true".equalsIgnoreCase(System.getProperty(SKIP_SCHEMA_KEY))) {
            logger.info("skipping schema generation... ");
        } else {
            dropSchema();
            dropPKSupport();
            createSchema();
            createPKSupport();
        }
    } catch (Exception e) {
        throw new RuntimeException("Error rebuilding schema", e);
    }
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) DataDomain(org.apache.cayenne.access.DataDomain) AshwoodEntitySorter(org.apache.cayenne.ashwood.AshwoodEntitySorter) DefaultEventManager(org.apache.cayenne.event.DefaultEventManager) MapQueryCache(org.apache.cayenne.cache.MapQueryCache) URL(java.net.URL) SQLException(java.sql.SQLException) DataMap(org.apache.cayenne.map.DataMap)

Aggregations

URLResource (org.apache.cayenne.resource.URLResource)40 URL (java.net.URL)31 Test (org.junit.Test)29 DataMap (org.apache.cayenne.map.DataMap)19 File (java.io.File)12 Injector (org.apache.cayenne.di.Injector)12 Resource (org.apache.cayenne.resource.Resource)11 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)10 DataMapLoader (org.apache.cayenne.configuration.DataMapLoader)7 XMLDataChannelDescriptorLoader (org.apache.cayenne.configuration.xml.XMLDataChannelDescriptorLoader)5 XMLDataMapLoader (org.apache.cayenne.configuration.xml.XMLDataMapLoader)5 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)4 ConfigurationNameMapper (org.apache.cayenne.configuration.ConfigurationNameMapper)4 DefaultConfigurationNameMapper (org.apache.cayenne.configuration.DefaultConfigurationNameMapper)4 DataChannelMetaData (org.apache.cayenne.configuration.xml.DataChannelMetaData)4 DefaultHandlerFactory (org.apache.cayenne.configuration.xml.DefaultHandlerFactory)4 HandlerFactory (org.apache.cayenne.configuration.xml.HandlerFactory)4 NoopDataChannelMetaData (org.apache.cayenne.configuration.xml.NoopDataChannelMetaData)4 XMLReaderProvider (org.apache.cayenne.configuration.xml.XMLReaderProvider)4 AdhocObjectFactory (org.apache.cayenne.di.AdhocObjectFactory)4