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