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/02/19 18:17:13 UTC

[2/2] git commit: CLEREZZA-881:providing a TcProvider for the system garph to make sure it is only available when fully loaded

CLEREZZA-881:providing a TcProvider for the system garph to make sure it is only available when fully loaded

Project: http://git-wip-us.apache.org/repos/asf/clerezza/repo
Commit: http://git-wip-us.apache.org/repos/asf/clerezza/commit/13b6fe35
Tree: http://git-wip-us.apache.org/repos/asf/clerezza/tree/13b6fe35
Diff: http://git-wip-us.apache.org/repos/asf/clerezza/diff/13b6fe35

Branch: refs/heads/master
Commit: 13b6fe35db41cfe5ee55af0644d9dc9585444515
Parents: bf9217a
Author: retobg <re...@apache.org>
Authored: Wed Feb 19 18:16:50 2014 +0100
Committer: retobg <re...@apache.org>
Committed: Wed Feb 19 18:16:50 2014 +0100

----------------------------------------------------------------------
 .../clerezza/platform/config/SystemConfig.java  | 129 ++++++++++++++-----
 1 file changed, 98 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/clerezza/blob/13b6fe35/platform.config/src/main/java/org/apache/clerezza/platform/config/SystemConfig.java
----------------------------------------------------------------------
diff --git a/platform.config/src/main/java/org/apache/clerezza/platform/config/SystemConfig.java b/platform.config/src/main/java/org/apache/clerezza/platform/config/SystemConfig.java
index a241976..ab103ac 100644
--- a/platform.config/src/main/java/org/apache/clerezza/platform/config/SystemConfig.java
+++ b/platform.config/src/main/java/org/apache/clerezza/platform/config/SystemConfig.java
@@ -18,70 +18,75 @@
  */
 package org.apache.clerezza.platform.config;
 
-
 import java.io.IOException;
 import java.net.URL;
+import java.util.Collections;
+import java.util.Set;
 import org.apache.clerezza.platform.Constants;
+import org.apache.clerezza.rdf.core.Graph;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Reference;
 import org.osgi.service.component.ComponentContext;
 import org.apache.clerezza.rdf.core.MGraph;
+import org.apache.clerezza.rdf.core.TripleCollection;
 import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.access.EntityAlreadyExistsException;
+import org.apache.clerezza.rdf.core.access.EntityUndeletableException;
 import org.apache.clerezza.rdf.core.access.NoSuchEntityException;
-import org.apache.clerezza.rdf.core.access.TcManager;
+import org.apache.clerezza.rdf.core.access.WeightedTcProvider;
 import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
 import org.apache.clerezza.rdf.core.serializedform.SupportedFormat;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.apache.clerezza.rdf.core.serializedform.ParsingProvider;
-    
-/**
- * When the <code>SystemConfig</code> component is activated it checks if the
- * system graph exists, in case it does not exist then it creates the
- * system graph and writes the default platform configuration into it.
+import org.apache.felix.scr.annotations.Service;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Deactivate;
 
+/**
+ * When the
+ * <code>SystemConfig</code> component is activated it checks if the system
+ * graph exists, in case it does not exist then it creates the system graph and
+ * writes the default platform configuration into it.
+ *
  * @author mir
  */
 @Component
-public class SystemConfig {
+@Service(WeightedTcProvider.class)
+public class SystemConfig implements WeightedTcProvider {
+
     public static final String CONFIG_FILE = "default-system-graph.rdf";
-        
     private final Logger logger = LoggerFactory.getLogger(getClass());
-
     /**
      *
      * @deprecated use org.apache.clerezza.platform.Contants instead
      */
     @Deprecated
     public static final UriRef SYSTEM_GRAPH_URI = Constants.SYSTEM_GRAPH_URI;
-
     /**
-     * A filter that can be used to get the system graph as OSGi service,
-     * that is provided by <code>org.apache.clerezza.rdf.core.access.TcManager</code>.
+     * A filter that can be used to get the system graph as OSGi service, that
+     * is provided by
+     * <code>org.apache.clerezza.rdf.core.access.TcManager</code>.
      */
     public static final String SYSTEM_GRAPH_FILTER =
-            "(name="+ Constants.SYSTEM_GRAPH_URI_STRING +")";
+            "(name=" + Constants.SYSTEM_GRAPH_URI_STRING + ")";
     public static final String PARSER_FILTER =
-            "(supportedFormat=" + SupportedFormat.RDF_XML +")";
-
-    @Reference
-    private TcManager tcManager;
-    
-    @Reference(target=PARSER_FILTER)
+            "(supportedFormat=" + SupportedFormat.RDF_XML + ")";
+    @Reference(target = PARSER_FILTER)
     private ParsingProvider parser;
+    private MGraph loadedFile;
 
+    @Activate
     protected void activate(ComponentContext componentContext) {
-        try {
-            tcManager.getMGraph(Constants.SYSTEM_GRAPH_URI);
-        } catch (NoSuchEntityException nsee) {
-            MGraph loadedFile = new SimpleMGraph();
-            readConfigGraphFile(loadedFile);
-            MGraph systemGraph = tcManager.createMGraph(Constants.SYSTEM_GRAPH_URI);
-            systemGraph.addAll(loadedFile);
-            logger.info("Add initial configuration to system graph");
-            
-            
-        }
+        //yould be good to use IndexedMGraph to be faster
+        loadedFile = new SimpleMGraph();
+        readConfigGraphFile(loadedFile);
+        logger.info("Add initial configuration to system graph");
+    }
+
+    @Deactivate
+    protected void deactivate(ComponentContext componentContext) {
+        loadedFile = null;
     }
 
     private void readConfigGraphFile(MGraph mGraph) {
@@ -98,4 +103,66 @@ public class SystemConfig {
         }
     }
 
+    /*
+     * Reason to be high: don't allow overwriting of system graph (by accident or as an attack)
+     * Reason to be low: avoid that TcManager always first tries to create TripleCollections using this provider
+     */
+    @Override
+    public int getWeight() {
+        return Integer.MAX_VALUE;
+    }
+
+    @Override
+    public Graph getGraph(UriRef name) throws NoSuchEntityException {
+        throw new NoSuchEntityException(name);
+    }
+
+    @Override
+    public MGraph getMGraph(UriRef name) throws NoSuchEntityException {
+        if (name.equals(Constants.SYSTEM_GRAPH_URI)) {
+            return loadedFile;
+        } else {
+            throw new NoSuchEntityException(name);
+        }
+    }
+
+    @Override
+    public TripleCollection getTriples(UriRef name) throws NoSuchEntityException {
+        return getMGraph(name);
+    }
+
+    @Override
+    public Set<UriRef> listGraphs() {
+        return Collections.emptySet();
+    }
+
+    @Override
+    public Set<UriRef> listMGraphs() {
+        return Collections.singleton(Constants.SYSTEM_GRAPH_URI);
+    }
+
+    @Override
+    public Set<UriRef> listTripleCollections() {
+        return listMGraphs();
+    }
+
+    @Override
+    public MGraph createMGraph(UriRef name) throws UnsupportedOperationException, EntityAlreadyExistsException {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+    @Override
+    public Graph createGraph(UriRef name, TripleCollection triples) throws UnsupportedOperationException, EntityAlreadyExistsException {
+        throw new UnsupportedOperationException("Not supported yet."); 
+    }
+
+    @Override
+    public void deleteTripleCollection(UriRef name) throws UnsupportedOperationException, NoSuchEntityException, EntityUndeletableException {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+    @Override
+    public Set<UriRef> getNames(Graph graph) {
+        return Collections.emptySet();
+    }
 }