]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.project/src/org/simantics/project/features/DependencyValidationFeature.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / features / DependencyValidationFeature.java
index 45ca05be7e359c7d8dd856b37e233874a62b3a18..ddaa2c88b905168fde21731757f3273053a9fe47 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.project.features;\r
-\r
-import java.net.URI;\r
-import java.net.URISyntaxException;\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-\r
-import org.eclipse.core.runtime.CoreException;\r
-import org.eclipse.core.runtime.IConfigurationElement;\r
-import org.eclipse.core.runtime.IExecutableExtension;\r
-import org.eclipse.core.runtime.IStatus;\r
-import org.eclipse.core.runtime.Status;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.VirtualGraph;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.CommentMetadata;\r
-import org.simantics.db.common.request.WriteRequest;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.exception.AssumptionException;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.exception.ResourceNotFoundException;\r
-import org.simantics.db.service.VirtualGraphSupport;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.project.exception.ProjectException;\r
-import org.simantics.project.ontology.ProjectResource;\r
-import org.simantics.scl.reflection.OntologyVersions;\r
-\r
-/**\r
- * A project feature for validating that a set of namespaces (URIs) are\r
- * reachable in the database for which this feature is configured.\r
- * \r
- * The URIs are described as class arguments in the extension class spec (after\r
- * ':' character).\r
- * \r
- * @author Tuukka Lehtonen\r
- */\r
-public class DependencyValidationFeature extends AbstractProjectFeature implements IExecutableExtension {\r
-\r
-    private String   virtualGraphId;\r
-    private String[] uris = {};\r
-\r
-    public DependencyValidationFeature() {\r
-    }\r
-\r
-    public DependencyValidationFeature(String virtualGraphId, String[] uris) {\r
-        this.virtualGraphId = virtualGraphId;\r
-        this.uris = uris;\r
-    }\r
-\r
-    @Override\r
-    public void setInitializationData(IConfigurationElement config, String propertyName, Object data)\r
-    throws CoreException {\r
-        if (data instanceof String) {\r
-            // Expecting comma-separated list of URIs in the argument.\r
-            String[] uris = ((String) data).split(",");\r
-            if (uris.length > 0) {\r
-                for(int i=0;i<uris.length;i++) uris[i] = OntologyVersions.getInstance().currentVersion(uris[i]);\r
-                // Validate the input data to contain valid URIs\r
-                for (String uri : uris) {\r
-                    try {\r
-                        new URI(uri);\r
-                    } catch (URISyntaxException e) {\r
-                        throw new CoreException(new Status(IStatus.ERROR, config.getContributor().getName(), "extension configuration element executable extension attribute '" + propertyName + "' argument contains invalid URIs. See cause for details.", e));\r
-                    }\r
-                }\r
-\r
-                this.uris = uris;\r
-            }\r
-        }\r
-    }\r
-\r
-    @Override\r
-    public void configure() throws ProjectException {\r
-        try {\r
-            Session s = getSession();\r
-            VirtualGraph vg = null;\r
-            if (virtualGraphId != null) {\r
-                VirtualGraphSupport support = s.getService(VirtualGraphSupport.class);\r
-                vg = support.getWorkspacePersistent(virtualGraphId);\r
-            }\r
-            getSession().syncRequest(new WriteRequest(vg) {\r
-                @Override\r
-                public void perform(WriteGraph graph) throws DatabaseException {\r
-                    configure(graph);\r
-                    graph.addMetadata(graph.getMetadata(CommentMetadata.class).add("Configured by Dependency Validation Feature."));\r
-                }\r
-            });\r
-        } catch (DatabaseException e) {\r
-            throw new ProjectException(e.getMessage(), e);\r
-        }\r
-    }\r
-\r
-    protected void configure(WriteGraph graph) throws DatabaseException {\r
-        Collection<String> nss = new ArrayList<String>();\r
-        Collection<String> notFound = new ArrayList<String>();\r
-\r
-        Resource project = getProject().get();\r
-        String projectName = NameUtils.getSafeName(graph, project);\r
-\r
-        ProjectResource PROJ = ProjectResource.getInstance(graph);\r
-\r
-        ArrayList<Resource> resourcesToLinkToProject = new ArrayList<Resource>();\r
-\r
-        for (String uri : uris) {\r
-            // This will fail if the extension-specified URI does not exist\r
-            Resource namespaceRequirement = null;\r
-            try {\r
-                namespaceRequirement = graph.getResource(uri);\r
-                resourcesToLinkToProject.add(namespaceRequirement);\r
-            } catch (ResourceNotFoundException e) {\r
-                notFound.add(uri);\r
-                continue;\r
-            }\r
-\r
-            for (Resource nsp : graph.getObjects(namespaceRequirement, PROJ.RequiresNamespace)) {\r
-                String ns = graph.getValue(nsp);\r
-                nss.add(ns);\r
-            }\r
-\r
-            for (String ns : nss) {\r
-                try {\r
-                    // This will fail if the namespace is not found.\r
-                    graph.getResource(ns);\r
-                } catch (ResourceNotFoundException e) {\r
-                    notFound.add(ns);\r
-                }\r
-            }\r
-        }\r
-\r
-        if (!notFound.isEmpty()) {\r
-            StringBuilder sb = new StringBuilder();\r
-            sb.append("Failed to locate the following namespaces required by project '");\r
-            sb.append(projectName);\r
-            sb.append("':\n");\r
-            for (String nf : notFound) {\r
-                sb.append("\t");\r
-                sb.append(nf);\r
-                sb.append("\n");\r
-            }\r
-            throw new AssumptionException(sb.toString());\r
-        }\r
-\r
-        // Ensure that the namespace requirements are linked to the project to\r
-        // make them discoverable by database queries.\r
-        linkTo(graph, project, resourcesToLinkToProject);\r
-    }\r
-\r
-    protected void linkTo(WriteGraph graph, Resource target, ArrayList<Resource> resourcesToLink) throws DatabaseException {\r
-        Layer0 L0 = Layer0.getInstance(graph);\r
-        for (Resource resource : resourcesToLink) {\r
-            if (!graph.hasStatement(target, L0.IsLinkedTo, resource))\r
-                graph.claim(target, L0.IsLinkedTo, resource);\r
-        }\r
-    }\r
-\r
-    @Override\r
-    public void deconfigure() throws ProjectException {\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.project.features;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IConfigurationElement;
+import org.eclipse.core.runtime.IExecutableExtension;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.VirtualGraph;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.CommentMetadata;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.AssumptionException;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.ResourceNotFoundException;
+import org.simantics.db.service.VirtualGraphSupport;
+import org.simantics.layer0.Layer0;
+import org.simantics.project.exception.ProjectException;
+import org.simantics.project.ontology.ProjectResource;
+import org.simantics.scl.reflection.OntologyVersions;
+
+/**
+ * A project feature for validating that a set of namespaces (URIs) are
+ * reachable in the database for which this feature is configured.
+ * 
+ * The URIs are described as class arguments in the extension class spec (after
+ * ':' character).
+ * 
+ * @author Tuukka Lehtonen
+ */
+public class DependencyValidationFeature extends AbstractProjectFeature implements IExecutableExtension {
+
+    private String   virtualGraphId;
+    private String[] uris = {};
+
+    public DependencyValidationFeature() {
+    }
+
+    public DependencyValidationFeature(String virtualGraphId, String[] uris) {
+        this.virtualGraphId = virtualGraphId;
+        this.uris = uris;
+    }
+
+    @Override
+    public void setInitializationData(IConfigurationElement config, String propertyName, Object data)
+    throws CoreException {
+        if (data instanceof String) {
+            // Expecting comma-separated list of URIs in the argument.
+            String[] uris = ((String) data).split(",");
+            if (uris.length > 0) {
+                for(int i=0;i<uris.length;i++) uris[i] = OntologyVersions.getInstance().currentVersion(uris[i]);
+                // Validate the input data to contain valid URIs
+                for (String uri : uris) {
+                    try {
+                        new URI(uri);
+                    } catch (URISyntaxException e) {
+                        throw new CoreException(new Status(IStatus.ERROR, config.getContributor().getName(), "extension configuration element executable extension attribute '" + propertyName + "' argument contains invalid URIs. See cause for details.", e));
+                    }
+                }
+
+                this.uris = uris;
+            }
+        }
+    }
+
+    @Override
+    public void configure() throws ProjectException {
+        try {
+            Session s = getSession();
+            VirtualGraph vg = null;
+            if (virtualGraphId != null) {
+                VirtualGraphSupport support = s.getService(VirtualGraphSupport.class);
+                vg = support.getWorkspacePersistent(virtualGraphId);
+            }
+            getSession().syncRequest(new WriteRequest(vg) {
+                @Override
+                public void perform(WriteGraph graph) throws DatabaseException {
+                    configure(graph);
+                    graph.addMetadata(graph.getMetadata(CommentMetadata.class).add("Configured by Dependency Validation Feature."));
+                }
+            });
+        } catch (DatabaseException e) {
+            throw new ProjectException(e.getMessage(), e);
+        }
+    }
+
+    protected void configure(WriteGraph graph) throws DatabaseException {
+        Collection<String> nss = new ArrayList<String>();
+        Collection<String> notFound = new ArrayList<String>();
+
+        Resource project = getProject().get();
+        String projectName = NameUtils.getSafeName(graph, project);
+
+        ProjectResource PROJ = ProjectResource.getInstance(graph);
+
+        ArrayList<Resource> resourcesToLinkToProject = new ArrayList<Resource>();
+
+        for (String uri : uris) {
+            // This will fail if the extension-specified URI does not exist
+            Resource namespaceRequirement = null;
+            try {
+                namespaceRequirement = graph.getResource(uri);
+                resourcesToLinkToProject.add(namespaceRequirement);
+            } catch (ResourceNotFoundException e) {
+                notFound.add(uri);
+                continue;
+            }
+
+            for (Resource nsp : graph.getObjects(namespaceRequirement, PROJ.RequiresNamespace)) {
+                String ns = graph.getValue(nsp);
+                nss.add(ns);
+            }
+
+            for (String ns : nss) {
+                try {
+                    // This will fail if the namespace is not found.
+                    graph.getResource(ns);
+                } catch (ResourceNotFoundException e) {
+                    notFound.add(ns);
+                }
+            }
+        }
+
+        if (!notFound.isEmpty()) {
+            StringBuilder sb = new StringBuilder();
+            sb.append("Failed to locate the following namespaces required by project '");
+            sb.append(projectName);
+            sb.append("':\n");
+            for (String nf : notFound) {
+                sb.append("\t");
+                sb.append(nf);
+                sb.append("\n");
+            }
+            throw new AssumptionException(sb.toString());
+        }
+
+        // Ensure that the namespace requirements are linked to the project to
+        // make them discoverable by database queries.
+        linkTo(graph, project, resourcesToLinkToProject);
+    }
+
+    protected void linkTo(WriteGraph graph, Resource target, ArrayList<Resource> resourcesToLink) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        for (Resource resource : resourcesToLink) {
+            if (!graph.hasStatement(target, L0.IsLinkedTo, resource))
+                graph.claim(target, L0.IsLinkedTo, resource);
+        }
+    }
+
+    @Override
+    public void deconfigure() throws ProjectException {
+    }
+
+}