Search in sources :

Example 1 with ShineRuntimeException

use of com.thoughtworks.studios.shine.ShineRuntimeException in project gocd by gocd.

the class StageStorage method save.

// File writes are not atomic in nature. There can be cases where the file is not completely written to disk.
// This is why we write to a tmp file and move it over to the actual file. File moves are atomic operations.
// If rename failed because target file already exists, we just ignore it because some other thread may have generated it.
public void save(Graph graph) {
    StageIdentifier identifier = extractStageIdentifier(graph);
    if (isStageStored(identifier)) {
        return;
    }
    synchronized (stageKey(identifier)) {
        if (isStageStored(identifier)) {
            return;
        }
        File file = new File(tmpStagePath(identifier));
        file.getParentFile().mkdirs();
        try {
            OutputStream os = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(file)));
            graph.persistToTurtle(os);
            os.flush();
            os.close();
            file.renameTo(new File(stagePath(identifier)));
        } catch (IOException e) {
            throw new ShineRuntimeException(e);
        } finally {
            file.delete();
        }
    }
}
Also used : StageIdentifier(com.thoughtworks.go.domain.StageIdentifier) ShineRuntimeException(com.thoughtworks.studios.shine.ShineRuntimeException) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 2 with ShineRuntimeException

use of com.thoughtworks.studios.shine.ShineRuntimeException in project gocd by gocd.

the class StageStorage method extractStageIdentifier.

private StageIdentifier extractStageIdentifier(Graph graph) {
    String select = "" + "PREFIX cruise: <" + GoOntology.URI + "> " + "SELECT ?pipelineName ?pipelineCounter ?stageName ?stageCounter WHERE {" + "  ?pipeline a cruise:Pipeline ." + "  ?pipeline cruise:pipelineName ?pipelineName ." + "  ?pipeline cruise:pipelineCounter ?pipelineCounter ." + "  ?pipeline cruise:hasStage ?stage ." + "  ?stage cruise:stageName ?stageName ." + "  ?stage cruise:stageCounter ?stageCounter ." + "}";
    BoundVariables bv = graph.selectFirst(select);
    if (bv == null) {
        throw new ShineRuntimeException("Cannot save a stage graph without stage identification information!");
    }
    StageIdentifier stageIdentifier = new StageIdentifier(bv.getString("pipelineName"), bv.getInt("pipelineCounter"), bv.getString("stageName"), bv.getString("stageCounter"));
    return stageIdentifier;
}
Also used : StageIdentifier(com.thoughtworks.go.domain.StageIdentifier) BoundVariables(com.thoughtworks.studios.shine.semweb.BoundVariables) ShineRuntimeException(com.thoughtworks.studios.shine.ShineRuntimeException)

Example 3 with ShineRuntimeException

use of com.thoughtworks.studios.shine.ShineRuntimeException in project gocd by gocd.

the class StageStorage method load.

public Graph load(StageIdentifier stageIdentifier) {
    String fileName = stagePath(stageIdentifier);
    try {
        InputStream inputStream = new BufferedInputStream(new FileInputStream(fileName));
        Graph tempGraph = new InMemoryTempGraphFactory().createTempGraph();
        tempGraph.addTriplesFromTurtle(inputStream);
        inputStream.close();
        return tempGraph;
    } catch (IOException e) {
        throw new ShineRuntimeException(String.format("Unable to read stage n3 file for " + stageIdentifier + "(file: %s). This should never happen.", fileName), e);
    }
}
Also used : Graph(com.thoughtworks.studios.shine.semweb.Graph) ShineRuntimeException(com.thoughtworks.studios.shine.ShineRuntimeException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) InMemoryTempGraphFactory(com.thoughtworks.studios.shine.semweb.sesame.InMemoryTempGraphFactory) FileInputStream(java.io.FileInputStream)

Example 4 with ShineRuntimeException

use of com.thoughtworks.studios.shine.ShineRuntimeException in project gocd by gocd.

the class GRDDLTransformer method transform.

public Graph transform(final Document inputDoc, TempGraphFactory graphFactory) throws GrddlTransformException {
    final DocumentResult result = new DocumentResult();
    try {
        xsltTransformerRegistry.transformWithCorrectClassLoader(key, new XSLTTransformerExecutor<Void>() {

            @Override
            public Void execute(Transformer transformer) throws TransformerException {
                DocumentSource source = new DocumentSource(inputDoc);
                transformer.transform(source, result);
                return null;
            }
        });
        // TODO: likely need to optimize with some sort of streaming document reader here
        Graph graph = graphFactory.createTempGraph();
        graph.addTriplesFromRDFXMLAbbrev(new StringReader(result.getDocument().asXML()));
        return graph;
    } catch (ShineRuntimeException e) {
        LOGGER.error("Could not convert to a graph. The document was: \n{}", result.getDocument().asXML(), e);
        throw e;
    } catch (TransformerException e) {
        LOGGER.warn("Could not perform grddl transform. The document was: \n{}", result.getDocument().asXML(), e);
        throw new GrddlTransformException(e);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) Graph(com.thoughtworks.studios.shine.semweb.Graph) ShineRuntimeException(com.thoughtworks.studios.shine.ShineRuntimeException) DocumentResult(org.dom4j.io.DocumentResult) DocumentSource(org.dom4j.io.DocumentSource) StringReader(java.io.StringReader) TransformerException(javax.xml.transform.TransformerException)

Example 5 with ShineRuntimeException

use of com.thoughtworks.studios.shine.ShineRuntimeException in project gocd by gocd.

the class SesameGraph method close.

public void close() {
    try {
        for (Graph tempGraph : tempGraphs) {
            tempGraph.close();
        }
        tempGraphs.clear();
        conn.commit();
        conn.close();
    } catch (RepositoryException e) {
        throw new ShineRuntimeException("Could not close graph!", e);
    }
}
Also used : Graph(com.thoughtworks.studios.shine.semweb.Graph) ShineRuntimeException(com.thoughtworks.studios.shine.ShineRuntimeException) RepositoryException(org.openrdf.repository.RepositoryException)

Aggregations

ShineRuntimeException (com.thoughtworks.studios.shine.ShineRuntimeException)23 RepositoryException (org.openrdf.repository.RepositoryException)16 URI (org.openrdf.model.URI)8 IOException (java.io.IOException)7 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)7 MoreThanOneResultFoundException (com.thoughtworks.studios.shine.semweb.MoreThanOneResultFoundException)6 Graph (com.thoughtworks.studios.shine.semweb.Graph)5 MalformedSPARQLException (com.thoughtworks.studios.shine.semweb.MalformedSPARQLException)5 UnsupportedSPARQLStatementException (com.thoughtworks.studios.shine.semweb.UnsupportedSPARQLStatementException)5 NoSuchElementException (java.util.NoSuchElementException)5 OpenRDFException (org.openrdf.OpenRDFException)5 MalformedQueryException (org.openrdf.query.MalformedQueryException)5 TupleQueryResultHandlerException (org.openrdf.query.TupleQueryResultHandlerException)5 RDFHandlerException (org.openrdf.rio.RDFHandlerException)5 BoundVariables (com.thoughtworks.studios.shine.semweb.BoundVariables)3 BooleanQuery (org.openrdf.query.BooleanQuery)3 TupleQuery (org.openrdf.query.TupleQuery)3 StageIdentifier (com.thoughtworks.go.domain.StageIdentifier)2 Transformer (javax.xml.transform.Transformer)2 TransformerException (javax.xml.transform.TransformerException)2