use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.
the class JCacheEntryLoader method process.
@Override
public List process(MutableEntry<String, List> entry, Object... arguments) throws EntryProcessorException {
if (!entry.exists()) {
List result = (List) entryFactory.createObject();
// sanity checking value
if (result == null) {
throw new CayenneRuntimeException("Null object created: %s", entry.getKey());
}
entry.setValue(result);
}
return entry.getValue();
}
use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.
the class EntityIdCoder method toObjectId.
public ObjectId toObjectId(String stringId) {
if (stringId.startsWith(TEMP_ID_PREFIX)) {
String idValues = stringId.substring(entityName.length() + 1 + TEMP_PREFIX_LENGTH);
return new ObjectId(entityName, decodeTemp(idValues));
}
String idValues = stringId.substring(entityName.length() + 1);
if (converters.size() == 1) {
Entry<String, Converter> entry = converters.entrySet().iterator().next();
String decoded;
try {
decoded = URLDecoder.decode(idValues, "UTF-8");
} catch (UnsupportedEncodingException e) {
// unexpected
throw new CayenneRuntimeException("Unsupported encoding", e);
}
return new ObjectId(entityName, entry.getKey(), entry.getValue().fromStringId(decoded));
}
Map<String, Object> idMap = new HashMap<>(idSize);
StringTokenizer toks = new StringTokenizer(idValues, ID_SEPARATOR);
if (toks.countTokens() != converters.size()) {
throw new IllegalArgumentException("Invalid String ID for entity " + entityName + ": " + idValues);
}
for (Entry<String, Converter> entry : converters.entrySet()) {
String value = toks.nextToken();
String decoded;
try {
decoded = URLDecoder.decode(value, "UTF-8");
} catch (UnsupportedEncodingException e) {
// unexpected
throw new CayenneRuntimeException("Unsupported encoding", e);
}
idMap.put(entry.getKey(), entry.getValue().fromStringId(decoded));
}
return new ObjectId(entityName, idMap);
}
use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.
the class CompatibilityDataMapLoader method load.
@Override
public DataMap load(Resource configurationResource) throws CayenneRuntimeException {
Document document = documentProvider.getDocument(configurationResource.getURL());
// no document yet in provider, maybe DataMap is directly loaded
if (document == null) {
if (!(upgradeServiceProvider.get() instanceof CompatibilityUpgradeService)) {
throw new ConfigurationException("CompatibilityUpgradeService expected");
}
// try to upgrade datamap directly
CompatibilityUpgradeService upgradeService = (CompatibilityUpgradeService) upgradeServiceProvider.get();
upgradeService.upgradeDataMap(configurationResource);
document = documentProvider.getDocument(configurationResource.getURL());
// still no document, try to load it without upgrade, though it likely will fail
if (document == null) {
return super.load(configurationResource);
}
}
final DataMap[] maps = new DataMap[1];
try {
DOMSource source = new DOMSource(document);
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(dataMap -> maps[0] = dataMap);
RootDataMapHandler rootHandler = new RootDataMapHandler(loaderContext);
parser.setContentHandler(rootHandler);
parser.setErrorHandler(rootHandler);
parser.parse(isource);
} catch (Exception e) {
throw new CayenneRuntimeException("Error loading configuration from %s", e, configurationResource.getURL());
}
if (maps[0] == null) {
throw new CayenneRuntimeException("Unable to load data map from %s", configurationResource.getURL());
}
DataMap map = maps[0];
if (map.getName() == null) {
// set name based on location if no name provided by map itself
map.setName(mapNameFromLocation(configurationResource.getURL().getFile()));
}
return map;
}
use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.
the class FileProjectSaver method saveCommit.
void saveCommit(Collection<SaveUnit> units) {
for (SaveUnit unit : units) {
File targetFile = unit.targetFile;
// Per CAY-2119, this is an ugly hack to force Windows to unlock the file that was previously locked by
// our process. Without it, the delete operation downstream would fail
System.gc();
if (targetFile.exists()) {
if (!targetFile.delete()) {
throw new CayenneRuntimeException("Unable to remove old master file '%s'", targetFile.getAbsolutePath());
}
}
File tempFile = unit.targetTempFile;
if (!tempFile.renameTo(targetFile)) {
throw new CayenneRuntimeException("Unable to move '%s' to '%s'", tempFile.getAbsolutePath(), targetFile.getAbsolutePath());
}
unit.targetTempFile = null;
try {
if (unit.delegate == null) {
unit.node.acceptVisitor(new ConfigurationSourceSetter(new URLResource(targetFile.toURI().toURL())));
}
} catch (MalformedURLException e) {
throw new CayenneRuntimeException("Malformed URL for file '%s'", e, targetFile.getAbsolutePath());
}
}
}
use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.
the class FileProjectSaver method save.
void save(Project project, Resource baseResource, boolean deleteOldResources) {
Collection<ConfigurationNode> nodes = project.getRootNode().acceptVisitor(saveableNodesGetter);
Collection<SaveUnit> units = new ArrayList<>(nodes.size());
for (ConfigurationNode node : nodes) {
String targetLocation = nameMapper.configurationLocation(node);
Resource targetResource = baseResource.getRelativeResource(targetLocation);
units.add(createSaveUnit(node, targetResource, null));
for (ProjectExtension extension : extensions) {
ConfigurationNodeVisitor<String> namingDelegate = extension.createNamingDelegate();
SaverDelegate unitSaverDelegate = extension.createSaverDelegate();
String fileName = node.acceptVisitor(namingDelegate);
if (fileName != null) {
// not null means that this should go to a separate file
targetResource = baseResource.getRelativeResource(fileName);
units.add(createSaveUnit(node, targetResource, unitSaverDelegate));
}
}
}
checkAccess(units);
try {
saveToTempFiles(units);
saveCommit(units);
} finally {
clearTempFiles(units);
}
try {
if (deleteOldResources) {
clearRenamedFiles(units);
Collection<URL> unusedResources = project.getUnusedResources();
for (SaveUnit unit : units) {
unusedResources.remove(unit.sourceConfiguration.getURL());
}
deleteUnusedFiles(unusedResources);
}
} catch (IOException ex) {
throw new CayenneRuntimeException(ex);
}
// I guess we should reset projects state regardless of the value of
// 'deleteOldResources'
project.getUnusedResources().clear();
}
Aggregations