You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by da...@apache.org on 2019/12/16 13:15:34 UTC

[sling-whiteboard] branch master updated: Add ArtifactID and Extension

This is an automated email from the ASF dual-hosted git repository.

davidb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git


The following commit(s) were added to refs/heads/master by this push:
     new d288ab4  Add ArtifactID and Extension
d288ab4 is described below

commit d288ab47337587da245d0199995befc00b174eeb
Author: David Bosschaert <da...@gmail.com>
AuthorDate: Mon Dec 16 13:15:11 2019 +0000

    Add ArtifactID and Extension
---
 .../src/main/java/org/osgi/feature/ArtifactID.java |  68 ++++++++++
 .../src/main/java/org/osgi/feature/Bundle.java     |  29 +----
 .../src/main/java/org/osgi/feature/Extension.java  | 143 +++++++++++++++++++++
 .../src/main/java/org/osgi/feature/Feature.java    |  51 ++++----
 4 files changed, 242 insertions(+), 49 deletions(-)

diff --git a/osgi-featuremodel/src/main/java/org/osgi/feature/ArtifactID.java b/osgi-featuremodel/src/main/java/org/osgi/feature/ArtifactID.java
new file mode 100644
index 0000000..65dcfaa
--- /dev/null
+++ b/osgi-featuremodel/src/main/java/org/osgi/feature/ArtifactID.java
@@ -0,0 +1,68 @@
+/*
+ * 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.osgi.feature;
+
+public class ArtifactID {
+    private final String groupId;
+    private final String artifactId;
+    private final String version; // The Artifact Version may not follow OSGi version rules
+    private final String type;
+    private final String classifier;
+
+    public static ArtifactID fromMavenID(String mavenID) {
+        String[] parts = mavenID.split(":");
+
+        if (parts.length < 3 && parts.length > 5)
+            throw new IllegalArgumentException("Not a valid maven ID" + mavenID);
+
+        String gid = parts[0];
+        String aid = parts[1];
+        String ver = parts[2];
+        String t = parts.length > 3 ? parts[3] : null;
+        String c = parts.length > 4 ? parts[4] : null;
+
+        return new ArtifactID(gid, aid, ver, t, c);
+    }
+
+    public ArtifactID(String groupId, String artifactId, String version, String type, String classifier) {
+        this.groupId = groupId;
+        this.artifactId = artifactId;
+        this.version = version;
+        this.type = type;
+        this.classifier = classifier;
+    }
+
+    public String getGroupId() {
+        return groupId;
+    }
+
+    public String getArtifactId() {
+        return artifactId;
+    }
+
+    public String getVersion() {
+        return version;
+    }
+
+    public String getType() {
+        return type;
+    }
+
+    public String getClassifier() {
+        return classifier;
+    }
+}
diff --git a/osgi-featuremodel/src/main/java/org/osgi/feature/Bundle.java b/osgi-featuremodel/src/main/java/org/osgi/feature/Bundle.java
index 7657590..9d68e99 100644
--- a/osgi-featuremodel/src/main/java/org/osgi/feature/Bundle.java
+++ b/osgi-featuremodel/src/main/java/org/osgi/feature/Bundle.java
@@ -16,38 +16,19 @@
  */
 package org.osgi.feature;
 
-import org.osgi.framework.Version;
-
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
-public class Bundle {
-    private final String groupId;
-    private final String artifactId;
-    private final Version version;
+public class Bundle extends ArtifactID {
     private final Map<String, String> metadata;
 
-    private Bundle(String groupId, String artifactId, Version version, Map<String,String> metadata) {
-        this.groupId = groupId;
-        this.artifactId = artifactId;
-        this.version = version;
+    private Bundle(String groupId, String artifactId, String version, Map<String,String> metadata) {
+        super(groupId, artifactId, version, null, null);
 
         this.metadata = Collections.unmodifiableMap(metadata);
     }
 
-    public String getGroupId() {
-        return groupId;
-    }
-
-    public String getArtifactId() {
-        return artifactId;
-    }
-
-    public Version getVersion() {
-        return version;
-    }
-
     public Map<String, String> getMetadata() {
         return metadata;
     }
@@ -55,11 +36,11 @@ public class Bundle {
     public class Builder {
         private final String groupId;
         private final String artifactId;
-        private final Version version;
+        private final String version;
 
         private final Map<String,String> metadata = new HashMap<>();
 
-        public Builder(String groupId, String artifactId, Version version) {
+        public Builder(String groupId, String artifactId, String version) {
             this.groupId = groupId;
             this.artifactId = artifactId;
             this.version = version;
diff --git a/osgi-featuremodel/src/main/java/org/osgi/feature/Extension.java b/osgi-featuremodel/src/main/java/org/osgi/feature/Extension.java
new file mode 100644
index 0000000..393c97d
--- /dev/null
+++ b/osgi-featuremodel/src/main/java/org/osgi/feature/Extension.java
@@ -0,0 +1,143 @@
+/*
+ * 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.osgi.feature;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.List;
+
+public class Extension {
+    public enum Type { JSON, TEXT, ARTIFACTS };
+
+    private final String name;
+    private final Type type;
+    private final String content;
+
+    private Extension(String name, Type type, String content) {
+        this.name = name;
+        this.type = type;
+        this.content = content;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public Type getType() {
+        return type;
+    }
+
+
+    public String getJSON() {
+        if (type != Type.JSON)
+            throw new IllegalStateException("Extension is not of type JSON " + type);
+
+        return content;
+    }
+
+    public String getText() {
+        if (type != Type.TEXT)
+            throw new IllegalStateException("Extension is not of type Text " + type);
+
+        return content;
+    }
+
+    public List<ArtifactID> getArtifacts() {
+        BufferedReader r = new BufferedReader(new StringReader(content));
+
+        List<ArtifactID> res = new ArrayList<>();
+        String line = null;
+        try {
+            while ((line = r.readLine()) != null) {
+                res.add(ArtifactID.fromMavenID(line));
+            }
+        } catch (IOException e) {
+            // ignore
+        }
+
+        return res;
+    }
+
+    public class Builder {
+        private final String name;
+        private final Type type;
+
+        private final StringBuilder content = new StringBuilder();
+
+        public Builder(String name, Type type) {
+            this.name = name;
+            this.type = type;
+        }
+
+        public Builder addText(String text) {
+            if (type != Type.TEXT)
+                throw new IllegalStateException("Cannot add text to extension of type " + type);
+
+            content.append(text);
+            return this;
+        }
+
+        public Builder addJSON(String json) {
+            if (type != Type.JSON)
+                throw new IllegalStateException("Cannot add text to extension of type " + type);
+
+            content.append(json);
+            return this;
+        }
+
+        public Builder addArtifact(ArtifactID aid) {
+            addArtifact(aid.getGroupId(), aid.getArtifactId(), aid.getVersion(), aid.getType(), aid.getClassifier());
+            return this;
+        }
+
+        public Builder addArtifact(String groupId, String artifactId, String version) {
+            if (type != Type.ARTIFACTS)
+                throw new IllegalStateException("Cannot add artifacts to extension of type " + type);
+
+            content.append(groupId);
+            content.append(':');
+            content.append(artifactId);
+            content.append(':');
+            content.append(version);
+            content.append('\n');
+            return this;
+        }
+
+        public Builder addArtifact(String groupId, String artifactId, String version, String at, String classifier) {
+            if (type != Type.ARTIFACTS)
+                throw new IllegalStateException("Cannot add artifacts to extension of type " + type);
+
+            content.append(groupId);
+            content.append(':');
+            content.append(artifactId);
+            content.append(':');
+            content.append(version);
+            content.append(':');
+            content.append(at);
+            content.append(':');
+            content.append(classifier);
+            content.append('\n');
+            return this;
+        }
+
+        public Extension build() {
+            return new Extension(name, type, content.toString());
+        }
+    }
+}
diff --git a/osgi-featuremodel/src/main/java/org/osgi/feature/Feature.java b/osgi-featuremodel/src/main/java/org/osgi/feature/Feature.java
index f0d4a2f..564ded0 100644
--- a/osgi-featuremodel/src/main/java/org/osgi/feature/Feature.java
+++ b/osgi-featuremodel/src/main/java/org/osgi/feature/Feature.java
@@ -16,8 +16,6 @@
  */
 package org.osgi.feature;
 
-import org.osgi.framework.Version;
-
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -26,10 +24,8 @@ import java.util.List;
 import java.util.Map;
 
 // Thread Safe
-public class Feature {
-    private final String groupId;
-    private final String artifactId;
-    private final Version version;
+// Or do we use an interface?
+public class Feature extends ArtifactID {
     private final String title;
     private final String description;
     private final String vendor;
@@ -42,11 +38,10 @@ public class Feature {
     private final List<Configuration> configurations;
     private final Map<String, String> variables;
 
-    private Feature(String gid, String aid, Version ver, String aTitle, String desc, String vnd, String lic, String loc,
+    private Feature(String gid, String aid, String ver, String type, String classifier, String aTitle, String desc, String vnd, String lic, String loc,
             boolean comp, boolean fin, List<Bundle> bs, List<Configuration> cs, Map<String,String> vars) {
-        groupId = gid;
-        artifactId = aid;
-        version = ver;
+        super(gid, aid, ver, type, classifier);
+
         title = aTitle;
         description = desc;
         vendor = vnd;
@@ -65,18 +60,6 @@ public class Feature {
         // add framework properties
     }
 
-    public String getGroupId() {
-        return groupId;
-    }
-
-    public String getArtifactId() {
-        return artifactId;
-    }
-
-    public Version getVersion() {
-        return version;
-    }
-
     public String getTitle() {
         return title;
     }
@@ -119,10 +102,14 @@ public class Feature {
 
     // Not Thread Safe
     public class Builder {
+        private static final String DEFAULT_FEATURE_TYPE = "osgifeature";
+
         private final String groupId;
         private final String artifactId;
-        private final Version version;
+        private final String version;
 
+        private String type;
+        private String classifier;
         private String title;
         private String description;
         private String vendor;
@@ -135,12 +122,22 @@ public class Feature {
         private final List<Configuration> configurations = new ArrayList<>();
         private final Map<String,String> variables = new HashMap<>();
 
-        public Builder(String groupId, String artifactId, Version version) {
+        public Builder(String groupId, String artifactId, String version) {
             this.groupId = groupId;
             this.artifactId = artifactId;
             this.version = version;
         }
 
+        public Builder setType(String type) {
+            this.type = type;
+            return this;
+        }
+
+        public Builder setClassifier(String cls) {
+            this.classifier = cls;
+            return this;
+        }
+
         public Builder setTitle(String title) {
             this.title = title;
             return this;
@@ -197,7 +194,11 @@ public class Feature {
         }
 
         public Feature build() {
-            return new Feature(groupId, artifactId, version, title,
+            if (classifier != null && type == null) {
+                type = DEFAULT_FEATURE_TYPE;
+            }
+
+            return new Feature(groupId, artifactId, version, type, classifier, title,
                     description, vendor, license, location, complete, isFinal,
                     bundles, configurations, variables);
         }