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/17 12:58:52 UTC
[sling-whiteboard] branch master updated: Convert Extension and
Configuration to interfaces with external builder
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 fa2b116 Convert Extension and Configuration to interfaces with external builder
fa2b116 is described below
commit fa2b1165a1c4e8cf81c514b3027a3386a5b2e016
Author: David Bosschaert <da...@gmail.com>
AuthorDate: Tue Dec 17 12:57:39 2019 +0000
Convert Extension and Configuration to interfaces with external builder
---
.../main/java/org/osgi/feature/Configuration.java | 64 +--------
.../src/main/java/org/osgi/feature/Extension.java | 124 +-----------------
.../osgi/feature/builder/ConfigurationBuilder.java | 85 ++++++++++++
.../org/osgi/feature/builder/ExtensionBuilder.java | 144 +++++++++++++++++++++
4 files changed, 240 insertions(+), 177 deletions(-)
diff --git a/osgi-featuremodel/src/main/java/org/osgi/feature/Configuration.java b/osgi-featuremodel/src/main/java/org/osgi/feature/Configuration.java
index e0761dc..4bd7782 100644
--- a/osgi-featuremodel/src/main/java/org/osgi/feature/Configuration.java
+++ b/osgi-featuremodel/src/main/java/org/osgi/feature/Configuration.java
@@ -16,68 +16,12 @@
*/
package org.osgi.feature;
-import java.util.Collections;
-import java.util.HashMap;
import java.util.Map;
-public class Configuration {
- private final String pid;
- private final String factoryPid;
- private final Map<String, Object> values;
+public interface Configuration {
+ String getPid();
- private Configuration(String pid, String factoryPid,
- Map<String, Object> values) {
- this.pid = pid;
- this.factoryPid = factoryPid;
- this.values = Collections.unmodifiableMap(values);
- }
+ String getFactoryPid();
- public String getPid() {
- return pid;
- }
-
- public String getFactoryPid() {
- return factoryPid;
- }
-
- public Map<String, Object> getValues() {
- return values;
- }
-
- public static class Builder {
- private final String p;
- private final String name;
-
- private final Map<String,Object> values = new HashMap<>();
-
- public Builder(String pid) {
- this.p = pid;
- this.name = null;
- }
-
- public Builder(String factoryPid, String name) {
- this.p = factoryPid;
- this.name = null;
- }
-
- public Builder addConfiguration(String key, Object value) {
- // TODO can do some validation on the configuration
- this.values.put(key, value);
- return this;
- }
-
- public Builder addConfiguration(Map<String, Object> cfg) {
- // TODO can do some validation on the configuration
- this.values.putAll(cfg);
- return this;
- }
-
- public Configuration build() {
- if (name == null) {
- return new Configuration(p, null, values);
- } else {
- return new Configuration(p, p + "~" + name, values);
- }
- }
- }
+ Map<String, Object> getValues();
}
diff --git a/osgi-featuremodel/src/main/java/org/osgi/feature/Extension.java b/osgi-featuremodel/src/main/java/org/osgi/feature/Extension.java
index 999263f..9309c46 100644
--- a/osgi-featuremodel/src/main/java/org/osgi/feature/Extension.java
+++ b/osgi-featuremodel/src/main/java/org/osgi/feature/Extension.java
@@ -16,128 +16,18 @@
*/
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 };
+public interface Extension {
+ enum Type { JSON, TEXT, ARTIFACTS };
- private final String name;
- private final Type type;
- private final String content;
+ String getName();
- private Extension(String name, Type type, String content) {
- this.name = name;
- this.type = type;
- this.content = content;
- }
+ Type getType();
- public String getName() {
- return name;
- }
+ String getJSON();
- public Type getType() {
- return type;
- }
+ String getText();
-
- 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 static 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());
- }
- }
+ List<ArtifactID> getArtifacts();
}
diff --git a/osgi-featuremodel/src/main/java/org/osgi/feature/builder/ConfigurationBuilder.java b/osgi-featuremodel/src/main/java/org/osgi/feature/builder/ConfigurationBuilder.java
new file mode 100644
index 0000000..40bac8c
--- /dev/null
+++ b/osgi-featuremodel/src/main/java/org/osgi/feature/builder/ConfigurationBuilder.java
@@ -0,0 +1,85 @@
+/*
+ * 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.builder;
+
+import org.osgi.feature.Configuration;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+public class ConfigurationBuilder {
+ private final String p;
+ private final String name;
+
+ private final Map<String,Object> values = new HashMap<>();
+
+ public ConfigurationBuilder(String pid) {
+ this.p = pid;
+ this.name = null;
+ }
+
+ public ConfigurationBuilder(String factoryPid, String name) {
+ this.p = factoryPid;
+ this.name = null;
+ }
+
+ public ConfigurationBuilder addConfiguration(String key, Object value) {
+ // TODO can do some validation on the configuration
+ this.values.put(key, value);
+ return this;
+ }
+
+ public ConfigurationBuilder addConfiguration(Map<String, Object> cfg) {
+ // TODO can do some validation on the configuration
+ this.values.putAll(cfg);
+ return this;
+ }
+
+ public Configuration build() {
+ if (name == null) {
+ return new ConfigurationImpl(p, null, values);
+ } else {
+ return new ConfigurationImpl(p, p + "~" + name, values);
+ }
+ }
+
+ private static class ConfigurationImpl implements Configuration {
+ private final String pid;
+ private final String factoryPid;
+ private final Map<String, Object> values;
+
+ private ConfigurationImpl(String pid, String factoryPid,
+ Map<String, Object> values) {
+ this.pid = pid;
+ this.factoryPid = factoryPid;
+ this.values = Collections.unmodifiableMap(values);
+ }
+
+ public String getPid() {
+ return pid;
+ }
+
+ public String getFactoryPid() {
+ return factoryPid;
+ }
+
+ public Map<String, Object> getValues() {
+ return values;
+ }
+ }
+}
diff --git a/osgi-featuremodel/src/main/java/org/osgi/feature/builder/ExtensionBuilder.java b/osgi-featuremodel/src/main/java/org/osgi/feature/builder/ExtensionBuilder.java
new file mode 100644
index 0000000..e4be602
--- /dev/null
+++ b/osgi-featuremodel/src/main/java/org/osgi/feature/builder/ExtensionBuilder.java
@@ -0,0 +1,144 @@
+/*
+ * 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.builder;
+
+import org.osgi.feature.ArtifactID;
+import org.osgi.feature.Extension;
+import org.osgi.feature.Extension.Type;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.List;
+
+public class ExtensionBuilder {
+ private final String name;
+ private final Type type;
+
+ private final StringBuilder content = new StringBuilder();
+
+ public ExtensionBuilder(String name, Type type) {
+ this.name = name;
+ this.type = type;
+ }
+
+ public ExtensionBuilder addText(String text) {
+ if (type != Type.TEXT)
+ throw new IllegalStateException("Cannot add text to extension of type " + type);
+
+ content.append(text);
+ return this;
+ }
+
+ public ExtensionBuilder addJSON(String json) {
+ if (type != Type.JSON)
+ throw new IllegalStateException("Cannot add text to extension of type " + type);
+
+ content.append(json);
+ return this;
+ }
+
+ public ExtensionBuilder addArtifact(ArtifactID aid) {
+ addArtifact(aid.getGroupId(), aid.getArtifactId(), aid.getVersion(), aid.getType(), aid.getClassifier());
+ return this;
+ }
+
+ public ExtensionBuilder 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 ExtensionBuilder 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 ExtensionImpl(name, type, content.toString());
+ }
+
+ private static class ExtensionImpl implements Extension {
+ private final String name;
+ private final Type type;
+ private final String content;
+
+ private ExtensionImpl(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;
+ }
+ }
+}