You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by re...@apache.org on 2014/01/16 16:01:09 UTC
git commit: CLEREZZA-867: locking graph during serialization
Updated Branches:
refs/heads/master 0a3c1ddf7 -> aa2eb1d16
CLEREZZA-867: locking graph during serialization
Project: http://git-wip-us.apache.org/repos/asf/clerezza/repo
Commit: http://git-wip-us.apache.org/repos/asf/clerezza/commit/aa2eb1d1
Tree: http://git-wip-us.apache.org/repos/asf/clerezza/tree/aa2eb1d1
Diff: http://git-wip-us.apache.org/repos/asf/clerezza/diff/aa2eb1d1
Branch: refs/heads/master
Commit: aa2eb1d16ce8ab2a1bbbd759b18d8bcacbcb7ead
Parents: 0a3c1dd
Author: Reto Bachmann-Gmür <re...@apache.org>
Authored: Thu Jan 16 16:00:12 2014 +0100
Committer: Reto Bachmann-Gmür <re...@apache.org>
Committed: Thu Jan 16 16:00:12 2014 +0100
----------------------------------------------------------------------
.../jena/serializer/JenaSerializerProvider.java | 185 ++++++++++---------
1 file changed, 97 insertions(+), 88 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/clerezza/blob/aa2eb1d1/rdf.jena.serializer/src/main/java/org/apache/clerezza/rdf/jena/serializer/JenaSerializerProvider.java
----------------------------------------------------------------------
diff --git a/rdf.jena.serializer/src/main/java/org/apache/clerezza/rdf/jena/serializer/JenaSerializerProvider.java b/rdf.jena.serializer/src/main/java/org/apache/clerezza/rdf/jena/serializer/JenaSerializerProvider.java
index 3f35fec..e425cb5 100644
--- a/rdf.jena.serializer/src/main/java/org/apache/clerezza/rdf/jena/serializer/JenaSerializerProvider.java
+++ b/rdf.jena.serializer/src/main/java/org/apache/clerezza/rdf/jena/serializer/JenaSerializerProvider.java
@@ -1,88 +1,97 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.clerezza.rdf.jena.serializer;
-
-import java.io.OutputStream;
-
-import org.apache.clerezza.rdf.core.serializedform.SerializingProvider;
-import org.apache.clerezza.rdf.core.serializedform.SupportedFormat;
-import org.apache.clerezza.rdf.jena.facade.JenaGraph;
-
-import com.hp.hpl.jena.rdf.model.Model;
-import com.hp.hpl.jena.rdf.model.ModelFactory;
-import com.hp.hpl.jena.rdf.model.RDFWriter;
-import org.apache.clerezza.rdf.core.TripleCollection;
-import org.apache.clerezza.rdf.core.serializedform.UnsupportedSerializationFormatException;
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Property;
-import org.apache.felix.scr.annotations.Service;
-/**
- * A {@link org.apache.clerezza.rdf.core.serializedform.SerializingProvider} based on Jena
- *
- * @author mir
- */
-/*
- * see http://jena.sourceforge.net/IO/iohowto.html
- */
-@Component
-@Service(SerializingProvider.class)
-@Property(name="supportedFormat", value={SupportedFormat.RDF_XML,
- SupportedFormat.TURTLE, SupportedFormat.X_TURTLE,
- SupportedFormat.N_TRIPLE, SupportedFormat.N3})
-@SupportedFormat({SupportedFormat.RDF_XML,
- SupportedFormat.TURTLE, SupportedFormat.X_TURTLE,
- SupportedFormat.N_TRIPLE, SupportedFormat.N3})
-public class JenaSerializerProvider implements SerializingProvider {
-
- @Override
- public void serialize(OutputStream serializedGraph, TripleCollection tc,
- String formatIdentifier) {
- String jenaFormat = getJenaFormat(formatIdentifier);
- com.hp.hpl.jena.graph.Graph graph = new JenaGraph(tc);
- Model model = ModelFactory.createModelForGraph(graph);
- RDFWriter writer = model.getWriter(jenaFormat);
- if ("RDF/XML".equals(jenaFormat)) {
- //jena complains about some URIs that aren't truely bad
- //see: http://tech.groups.yahoo.com/group/jena-dev/message/38313
- writer.setProperty("allowBadURIs", Boolean.TRUE);
- }
- writer.write(model, serializedGraph, "");
- }
-
- private String getJenaFormat(String formatIdentifier) {
- int semicolonPos = formatIdentifier.indexOf(';');
- if (semicolonPos > -1) {
- formatIdentifier = formatIdentifier.substring(0, semicolonPos);
- }
- if (formatIdentifier.equals(SupportedFormat.RDF_XML)) {
- return "RDF/XML";
- }
- if (formatIdentifier.equals(SupportedFormat.TURTLE) ||
- formatIdentifier.equals(SupportedFormat.X_TURTLE)) {
- return "TURTLE";
- }
- if (formatIdentifier.equals(SupportedFormat.N3)) {
- return "N3";
- }
- if (formatIdentifier.equals(SupportedFormat.N_TRIPLE)) {
- return "N-TRIPLE";
- }
- throw new UnsupportedSerializationFormatException(formatIdentifier);
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.clerezza.rdf.jena.serializer;
+
+import java.io.OutputStream;
+
+import org.apache.clerezza.rdf.core.serializedform.SerializingProvider;
+import org.apache.clerezza.rdf.core.serializedform.SupportedFormat;
+import org.apache.clerezza.rdf.jena.facade.JenaGraph;
+
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.ModelFactory;
+import com.hp.hpl.jena.rdf.model.RDFWriter;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.clerezza.rdf.core.TripleCollection;
+import org.apache.clerezza.rdf.core.access.LockableMGraph;
+import org.apache.clerezza.rdf.core.serializedform.UnsupportedSerializationFormatException;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+/**
+ * A {@link org.apache.clerezza.rdf.core.serializedform.SerializingProvider} based on Jena
+ *
+ * @author mir
+ */
+/*
+ * see http://jena.sourceforge.net/IO/iohowto.html
+ */
+@Component
+@Service(SerializingProvider.class)
+@Property(name="supportedFormat", value={SupportedFormat.RDF_XML,
+ SupportedFormat.TURTLE, SupportedFormat.X_TURTLE,
+ SupportedFormat.N_TRIPLE, SupportedFormat.N3})
+@SupportedFormat({SupportedFormat.RDF_XML,
+ SupportedFormat.TURTLE, SupportedFormat.X_TURTLE,
+ SupportedFormat.N_TRIPLE, SupportedFormat.N3})
+public class JenaSerializerProvider implements SerializingProvider {
+
+ @Override
+ public void serialize(OutputStream serializedGraph, TripleCollection tc,
+ String formatIdentifier) {
+ String jenaFormat = getJenaFormat(formatIdentifier);
+ Lock l = (tc instanceof LockableMGraph) ? ((LockableMGraph)tc).getLock().readLock() : (new ReentrantReadWriteLock()).readLock();
+ l.lock();
+ try {
+ com.hp.hpl.jena.graph.Graph graph = new JenaGraph(tc);
+ Model model = ModelFactory.createModelForGraph(graph);
+ RDFWriter writer = model.getWriter(jenaFormat);
+ if ("RDF/XML".equals(jenaFormat)) {
+ //jena complains about some URIs that aren't truely bad
+ //see: http://tech.groups.yahoo.com/group/jena-dev/message/38313
+ writer.setProperty("allowBadURIs", Boolean.TRUE);
+ }
+ writer.write(model, serializedGraph, "");
+ } finally {
+ l.unlock();
+ }
+ }
+
+ private String getJenaFormat(String formatIdentifier) {
+ int semicolonPos = formatIdentifier.indexOf(';');
+ if (semicolonPos > -1) {
+ formatIdentifier = formatIdentifier.substring(0, semicolonPos);
+ }
+ if (formatIdentifier.equals(SupportedFormat.RDF_XML)) {
+ return "RDF/XML";
+ }
+ if (formatIdentifier.equals(SupportedFormat.TURTLE) ||
+ formatIdentifier.equals(SupportedFormat.X_TURTLE)) {
+ return "TURTLE";
+ }
+ if (formatIdentifier.equals(SupportedFormat.N3)) {
+ return "N3";
+ }
+ if (formatIdentifier.equals(SupportedFormat.N_TRIPLE)) {
+ return "N-TRIPLE";
+ }
+ throw new UnsupportedSerializationFormatException(formatIdentifier);
+ }
+}