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());
}
}
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.");
}
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;
}
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");
}
}
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);
}
}
Aggregations