use of org.apache.cayenne.resource.URLResource in project cayenne by apache.
the class CompatibilityUpgradeServiceIT method testUpgradeStandAloneDataMapDom.
@Test
public void testUpgradeStandAloneDataMapDom() throws Exception {
Injector injector = getInjector();
CompatibilityUpgradeService upgradeService = (CompatibilityUpgradeService) injector.getInstance(UpgradeService.class);
DocumentProvider documentProvider = injector.getInstance(DocumentProvider.class);
URL dataMapUrl = getClass().getResource("test-map-v6.map.xml");
Document dataMapDocument = documentProvider.getDocument(dataMapUrl);
assertNull(dataMapDocument);
Resource resource = new URLResource(dataMapUrl);
upgradeService.upgradeDataMap(resource);
dataMapDocument = documentProvider.getDocument(dataMapUrl);
assertNotNull(dataMapDocument);
assertEquals("10", dataMapDocument.getDocumentElement().getAttribute("project-version"));
}
use of org.apache.cayenne.resource.URLResource in project cayenne by apache.
the class DefaultDbImportAction method newTargetDataMap.
protected DataMap newTargetDataMap(DbImportConfiguration config) throws IOException {
DataMap dataMap = new DataMap();
dataMap.setName(config.getDataMapName());
dataMap.setConfigurationSource(new URLResource(config.getTargetDataMap().toURI().toURL()));
dataMap.setNamespace(new EntityResolver(Collections.singleton(dataMap)));
// update map defaults
// do not override default package of existing DataMap unless it is
// explicitly requested by the plugin caller
String defaultPackage = config.getDefaultPackage();
if (defaultPackage != null && defaultPackage.length() > 0) {
dataMap.setDefaultPackage(defaultPackage);
}
CatalogFilter[] catalogs = config.getDbLoaderConfig().getFiltersConfig().getCatalogs();
if (catalogs.length > 0) {
// do not override default catalog of existing DataMap unless it is
// explicitly requested by the plugin caller, and the provided catalog is
// not a pattern
String catalog = catalogs[0].name;
if (catalog != null && catalog.length() > 0 && catalog.indexOf('%') < 0) {
dataMap.setDefaultCatalog(catalog);
}
// do not override default schema of existing DataMap unless it is
// explicitly requested by the plugin caller, and the provided schema is
// not a pattern
String schema = catalogs[0].schemas[0].name;
if (schema != null && schema.length() > 0 && schema.indexOf('%') < 0) {
dataMap.setDefaultSchema(schema);
}
}
return dataMap;
}
use of org.apache.cayenne.resource.URLResource in project cayenne by apache.
the class DBCP2DataSourceFactoryTest method testGetDataSource_InvalidLocation.
@Test
public void testGetDataSource_InvalidLocation() 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.nosuchfile");
DBCPDataSourceFactory factory = new DBCPDataSourceFactory();
try {
factory.getDataSource(nodeDescriptor);
fail("didn't throw on absent config file");
} catch (IOException ex) {
// expected
}
}
use of org.apache.cayenne.resource.URLResource in project cayenne by apache.
the class ImportDataMapAction method importDataMap.
protected void importDataMap() {
File dataMapFile = selectDataMap(Application.getFrame());
if (dataMapFile == null) {
return;
}
DataMap newMap;
try {
URL url = dataMapFile.toURI().toURL();
DataMapLoader loader = application.getInjector().getInstance(DataMapLoader.class);
newMap = loader.load(new URLResource(url));
ConfigurationNode root = getProjectController().getProject().getRootNode();
newMap.setName(NameBuilder.builder(newMap, root).baseName(newMap.getName()).name());
Resource baseResource = ((DataChannelDescriptor) root).getConfigurationSource();
if (baseResource != null) {
Resource dataMapResource = baseResource.getRelativeResource(nameMapper.configurationLocation(newMap));
newMap.setConfigurationSource(dataMapResource);
}
getProjectController().addDataMap(this, newMap);
} catch (Exception ex) {
logObj.info("Error importing DataMap.", ex);
JOptionPane.showMessageDialog(Application.getFrame(), "Error reading DataMap: " + ex.getMessage(), "Can't Open DataMap", JOptionPane.OK_OPTION);
}
}
use of org.apache.cayenne.resource.URLResource in project cayenne by apache.
the class SaveAsAction method saveAll.
/**
* Saves project and related files. Saving is done to temporary files, and
* only on successful save, master files are replaced with new versions.
*/
protected boolean saveAll() throws Exception {
Project p = getCurrentProject();
String oldPath = null;
if (p.getConfigurationResource() != null) {
oldPath = p.getConfigurationResource().getURL().getPath();
}
File projectDir = fileChooser.newProjectDir(Application.getFrame(), p);
if (projectDir == null) {
return false;
}
if (projectDir.exists() && !projectDir.canWrite()) {
JOptionPane.showMessageDialog(Application.getFrame(), "Can't save project - unable to write to file \"" + projectDir.getPath() + "\"", "Can't Save Project", JOptionPane.OK_OPTION);
return false;
}
getProjectController().getFileChangeTracker().pauseWatching();
URLResource res = new URLResource(projectDir.toURI().toURL());
ProjectSaver saver = getApplication().getInjector().getInstance(ProjectSaver.class);
boolean isNewProject = p.getConfigurationResource() == null;
Preferences tempOldPref = null;
if (isNewProject) {
tempOldPref = getApplication().getMainPreferenceForProject();
}
saver.saveAs(p, res);
if (oldPath != null && oldPath.length() != 0 && !oldPath.equals(p.getConfigurationResource().getURL().getPath())) {
String newName = p.getConfigurationResource().getURL().getPath().replace(".xml", "");
String oldName = oldPath.replace(".xml", "");
Preferences oldPref = getProjectController().getPreferenceForProject();
String projPath = oldPref.absolutePath().replace(oldName, "");
Preferences newPref = getProjectController().getPreferenceForProject().node(projPath + newName);
RenamedPreferences.copyPreferences(newPref, getProjectController().getPreferenceForProject(), false);
} else if (isNewProject) {
if (tempOldPref != null) {
String newProjectName = getApplication().getNewProjectTemporaryName();
if (tempOldPref.absolutePath().contains(newProjectName)) {
String projPath = tempOldPref.absolutePath().replace("/" + newProjectName, "");
String newName = p.getConfigurationResource().getURL().getPath().replace(".xml", "");
Preferences newPref = getApplication().getMainPreferenceForProject().node(projPath + newName);
RenamedPreferences.copyPreferences(newPref, tempOldPref, false);
tempOldPref.removeNode();
}
}
}
RenamedPreferences.removeNewPreferences();
File file = new File(p.getConfigurationResource().getURL().toURI());
getApplication().getFrameController().addToLastProjListAction(file);
Application.getFrame().fireRecentFileListChanged();
// Reset the watcher now
getProjectController().getFileChangeTracker().reconfigure();
return true;
}
Aggregations