Return-Path: Delivered-To: apmail-incubator-clerezza-commits-archive@minotaur.apache.org Received: (qmail 50575 invoked from network); 14 Jan 2011 03:12:14 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 14 Jan 2011 03:12:14 -0000 Received: (qmail 5346 invoked by uid 500); 14 Jan 2011 03:12:14 -0000 Delivered-To: apmail-incubator-clerezza-commits-archive@incubator.apache.org Received: (qmail 5327 invoked by uid 500); 14 Jan 2011 03:12:13 -0000 Mailing-List: contact clerezza-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: clerezza-dev@incubator.apache.org Delivered-To: mailing list clerezza-commits@incubator.apache.org Received: (qmail 5320 invoked by uid 99); 14 Jan 2011 03:12:13 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 14 Jan 2011 03:12:13 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 14 Jan 2011 03:12:12 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 44CCB23889BF; Fri, 14 Jan 2011 03:11:46 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1058845 - /incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.web/org.apache.clerezza.rdf.web.core/src/main/java/org/apache/clerezza/rdf/web/core/WebAccess.java Date: Fri, 14 Jan 2011 03:11:46 -0000 To: clerezza-commits@incubator.apache.org From: hasan@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110114031146.44CCB23889BF@eris.apache.org> Author: hasan Date: Fri Jan 14 03:11:45 2011 New Revision: 1058845 URL: http://svn.apache.org/viewvc?rev=1058845&view=rev Log: CLEREZZA-396: added missing lock and improved code Modified: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.web/org.apache.clerezza.rdf.web.core/src/main/java/org/apache/clerezza/rdf/web/core/WebAccess.java Modified: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.web/org.apache.clerezza.rdf.web.core/src/main/java/org/apache/clerezza/rdf/web/core/WebAccess.java URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.web/org.apache.clerezza.rdf.web.core/src/main/java/org/apache/clerezza/rdf/web/core/WebAccess.java?rev=1058845&r1=1058844&r2=1058845&view=diff ============================================================================== --- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.web/org.apache.clerezza.rdf.web.core/src/main/java/org/apache/clerezza/rdf/web/core/WebAccess.java (original) +++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.web/org.apache.clerezza.rdf.web.core/src/main/java/org/apache/clerezza/rdf/web/core/WebAccess.java Fri Jan 14 03:11:45 2011 @@ -1,26 +1,28 @@ /* * 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 + * 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. + * "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.web.core; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.security.AccessController; +import java.util.concurrent.locks.Lock; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; @@ -48,6 +50,7 @@ import org.slf4j.LoggerFactory; import org.apache.clerezza.rdf.core.Triple; import org.apache.clerezza.rdf.core.TripleCollection; import org.apache.clerezza.rdf.core.UriRef; +import org.apache.clerezza.rdf.core.access.LockableMGraph; import org.apache.clerezza.rdf.core.access.NoSuchEntityException; import org.apache.clerezza.rdf.core.access.TcManager; import org.apache.clerezza.rdf.core.serializedform.Parser; @@ -87,7 +90,7 @@ public class WebAccess { .type(MediaType.TEXT_PLAIN_TYPE).build(); throw new WebApplicationException(r); } - TripleCollection result = tcManager.getTriples(name); + TripleCollection result = tcManager.getTriples(name); logger.debug("Got graph of size {} ", result.size()); int i = 1; if (logger.isDebugEnabled()) { @@ -111,14 +114,20 @@ public class WebAccess { @PUT public void putTriples(@QueryParam("name") UriRef name, TripleCollection triples) { AccessController.checkPermission(new WebAccessPermission()); - TripleCollection tc; + LockableMGraph mGraph; try { - tc = tcManager.getTriples(name); - tc.clear(); + mGraph = tcManager.getMGraph(name); } catch (NoSuchEntityException e) { - tc = tcManager.createMGraph(name); + mGraph = tcManager.createMGraph(name); + } + Lock writeLock = mGraph.getLock().writeLock(); + writeLock.lock(); + try { + mGraph.clear(); + mGraph.addAll(triples); + } finally { + writeLock.unlock(); } - tc.addAll(triples); } /** @@ -160,38 +169,44 @@ public class WebAccess { if (graph == null || (graph.length == 0)) { responseWithBadRequest("no triples uploaded"); } - InputStream is = new ByteArrayInputStream(graph); - MediaType mediaType = formFile.getMediaType(); if (mediaType == null) { responseWithBadRequest("mime-type not specified"); } - Graph parsedGraph = parser.parse(is, mediaType.toString()); - String graphName = getFirstTextParameterValue(form, "name", true); if (graphName == null) { responseWithBadRequest("graph name not specified"); } + String mode = getFirstTextParameterValue(form, "mode", false); + if (mode != null) { + if (!(mode.equals("replace") || mode.equals("append"))) { + responseWithBadRequest("unknown mode"); + } + } else { + mode = "append"; + } + InputStream is = new ByteArrayInputStream(graph); + Graph parsedGraph = parser.parse(is, mediaType.toString()); UriRef graphUri = new UriRef(graphName); - TripleCollection tc; + LockableMGraph mGraph; boolean newGraph = false; try { - tc = tcManager.getTriples(graphUri); - String mode = getFirstTextParameterValue(form, "mode", false); - if (mode != null) { - if (mode.equals("replace")) { - tc.clear(); - } else if (!mode.equals("append")) { - responseWithBadRequest("unknown mode"); - } - } + mGraph = tcManager.getMGraph(graphUri); } catch (NoSuchEntityException e) { - tc = tcManager.createMGraph(graphUri); + mGraph = tcManager.createMGraph(graphUri); newGraph = true; } - tc.addAll(parsedGraph); - String redirection = getFirstTextParameterValue(form, "redirection", - false); + Lock writeLock = mGraph.getLock().writeLock(); + writeLock.lock(); + try { + if (!newGraph && mode.equals("replace")) { + mGraph.clear(); + } + mGraph.addAll(parsedGraph); + } finally { + writeLock.unlock(); + } + String redirection = getFirstTextParameterValue(form, "redirection", false); if (redirection == null) { if (newGraph) { return Response.status(Status.CREATED).build();