Search in sources :

Example 1 with CayenneRuntimeException

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();
}
Also used : CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) List(java.util.List)

Example 2 with CayenneRuntimeException

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);
}
Also used : StringTokenizer(java.util.StringTokenizer) ObjectId(org.apache.cayenne.ObjectId) HashMap(java.util.HashMap) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 3 with CayenneRuntimeException

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;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) TransformerFactory(javax.xml.transform.TransformerFactory) StreamResult(javax.xml.transform.stream.StreamResult) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) ConfigurationException(org.apache.cayenne.ConfigurationException) DataMap(org.apache.cayenne.map.DataMap) ConfigurationException(org.apache.cayenne.ConfigurationException) ByteArrayInputStream(java.io.ByteArrayInputStream) CompatibilityUpgradeService(org.apache.cayenne.project.compatibility.CompatibilityUpgradeService) XMLReader(org.xml.sax.XMLReader)

Example 4 with CayenneRuntimeException

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());
        }
    }
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) MalformedURLException(java.net.MalformedURLException) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) File(java.io.File)

Example 5 with CayenneRuntimeException

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();
}
Also used : SaverDelegate(org.apache.cayenne.project.extension.SaverDelegate) ArrayList(java.util.ArrayList) URLResource(org.apache.cayenne.resource.URLResource) Resource(org.apache.cayenne.resource.Resource) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) IOException(java.io.IOException) URL(java.net.URL) ConfigurationNode(org.apache.cayenne.configuration.ConfigurationNode) ProjectExtension(org.apache.cayenne.project.extension.ProjectExtension)

Aggregations

CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)168 Test (org.junit.Test)25 DbAttribute (org.apache.cayenne.map.DbAttribute)21 DataMap (org.apache.cayenne.map.DataMap)19 ObjectId (org.apache.cayenne.ObjectId)18 ObjEntity (org.apache.cayenne.map.ObjEntity)18 Persistent (org.apache.cayenne.Persistent)17 Expression (org.apache.cayenne.exp.Expression)17 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)17 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)14 DbEntity (org.apache.cayenne.map.DbEntity)14 IOException (java.io.IOException)13 List (java.util.List)12 ObjRelationship (org.apache.cayenne.map.ObjRelationship)12 DbRelationship (org.apache.cayenne.map.DbRelationship)10 DateTestEntity (org.apache.cayenne.testdo.date_time.DateTestEntity)10 File (java.io.File)9 Connection (java.sql.Connection)9 SQLException (java.sql.SQLException)9