diff --git a/tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/model/config/TmfConfigurationTest.java b/tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/model/config/TmfConfigurationTest.java index 07a7bc931d..6e6b54e0d1 100644 --- a/tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/model/config/TmfConfigurationTest.java +++ b/tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/model/config/TmfConfigurationTest.java @@ -14,6 +14,7 @@ import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.fail; +import java.util.Collections; import java.util.Map; import org.eclipse.jdt.annotation.NonNull; @@ -36,7 +37,8 @@ public class TmfConfigurationTest { private static final String DESC = "descriptor"; private static final String SOURCE_ID = "my-source-id"; private static final @NonNull Map<@NonNull String, @NonNull Object> PARAM = ImmutableMap.of("path", "/tmp/home/my-test.xml"); - private static final String EXPECTED_TO_STRING = "TmfConfiguration[fName=/tmp/my-test.xml, fDescription=descriptor, fType=my-source-id, fId=my-test.xml, fParameters={path=/tmp/home/my-test.xml}]"; + private static final String EXPECTED_TO_STRING = "TmfConfiguration[fName=/tmp/my-test.xml, fDescription=descriptor, fType=my-source-id, fId=my-test.xml, fParameters={path=/tmp/home/my-test.xml}, fJsonParameters=]"; + private static final String EXPECTED2_TO_STRING = "TmfConfiguration[fName=/tmp/my-test.xml, fDescription=descriptor, fType=my-source-id, fId=my-test.xml, fParameters={}, fJsonParameters={path:/tmp/my-test.xml}]"; // ------------------------------------------------------------------------ // Tests @@ -137,7 +139,8 @@ public void testEquality() { .setId(ID) .setDescription(DESC) .setSourceTypeId(SOURCE_ID) - .setParameters(PARAM); + .setParameters(PARAM) + .setJsonParameters("{path:\"" + PATH +"\"}"); ITmfConfiguration baseConfiguration = builder.build(); // Make sure it is equal to itself @@ -188,6 +191,15 @@ public void testToString() { .setSourceTypeId(SOURCE_ID) .setParameters(PARAM); assertEquals(EXPECTED_TO_STRING, builder.build().toString()); + + builder = new TmfConfiguration.Builder() + .setName(PATH) + .setId(ID) + .setDescription(DESC) + .setSourceTypeId(SOURCE_ID) + .setParameters(Collections.emptyMap()) + .setJsonParameters("{path:\"" + PATH +"\"}"); + assertEquals(EXPECTED2_TO_STRING, builder.build().toString()); } /** @@ -212,8 +224,19 @@ public void testHashCode() { ITmfConfiguration config2 = builder.build(); + builder = new TmfConfiguration.Builder() + .setName(PATH + "1") + .setId(ID + "1") + .setDescription(DESC + "1") + .setSourceTypeId(SOURCE_ID + "1") + .setJsonParameters("{path:\"" + PATH +"\"}"); + + ITmfConfiguration config3 = builder.build(); + assertEquals(config1.hashCode(), config1.hashCode()); assertEquals(config2.hashCode(), config2.hashCode()); + assertEquals(config3.hashCode(), config3.hashCode()); assertNotEquals(config1.hashCode(), config2.hashCode()); + assertNotEquals(config2.hashCode(), config3.hashCode()); } } diff --git a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/config/ITmfConfiguration.java b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/config/ITmfConfiguration.java index 045a3a97ce..4555c5d77e 100644 --- a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/config/ITmfConfiguration.java +++ b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/config/ITmfConfiguration.java @@ -40,8 +40,18 @@ public interface ITmfConfiguration { String getSourceTypeId(); /** - * @return optional informational parameters to return. Can be used to show - * more details to users of the configuration instance. + * @return optional parameters representing the configuration parameters used to create + * this configuration. This is intended to be used instead of {@link #getJsonParameters()}. */ Map getParameters(); + + /** + * @return optional JSON string representing the configuration parameters used to create + * this configuration. This is intended to be used instead of {@link #getParameters()}. + * + * @since 9.5 + */ + default String getJsonParameters() { + return ""; //$NON-NLS-1$ + } } diff --git a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/config/ITmfConfigurationSource.java b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/config/ITmfConfigurationSource.java index 3061bd09c6..7829f1adc3 100644 --- a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/config/ITmfConfigurationSource.java +++ b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/config/ITmfConfigurationSource.java @@ -17,6 +17,8 @@ import org.eclipse.jdt.annotation.Nullable; import org.eclipse.tracecompass.tmf.core.exceptions.TmfConfigurationException; +import com.google.gson.Gson; + /** * Interface to implement for providing a configuration source. * @@ -43,6 +45,29 @@ public interface ITmfConfigurationSource { */ ITmfConfiguration create(Map parameters) throws TmfConfigurationException; + /** + * Creates a new configuration instance. + *

+ * The parameters to be provided are described by + * {@link ITmfConfigurationSourceType#getConfigParamDescriptors()}. + * + * @param parameters + * The query parameters as JSON string used to create a configuration instance. + * @return a new {@link ITmfConfiguration} if successful + * @throws TmfConfigurationException + * If the creation of the configuration fails + * @since 9.5 + */ + default ITmfConfiguration create(String parameters) throws TmfConfigurationException { + try { + @SuppressWarnings("null") + Map map = new Gson().fromJson(parameters, Map.class); + return create(map); + } catch (Exception e) { + throw new TmfConfigurationException("Can't convert json string to Map to update configuration", e); //$NON-NLS-1$ + } + } + /** * Updates a configuration instance. *

@@ -59,6 +84,31 @@ public interface ITmfConfigurationSource { */ ITmfConfiguration update(String id, Map parameters) throws TmfConfigurationException; + /** + * Updates a configuration instance. + *

+ * The parameters to be provided are described by + * {@link ITmfConfigurationSourceType#getConfigParamDescriptors()}. + * + * @param id + * The configuration ID of the configuration to update + * @param parameters + * The query parameters as JSON string used to update a configuration instance + * @return a new {@link ITmfConfiguration} if successful + * @throws TmfConfigurationException + * If the update of the configuration fails + * @since 9.5 + */ + default ITmfConfiguration update(String id, String parameters) throws TmfConfigurationException { + try { + @SuppressWarnings("null") + Map map = new Gson().fromJson(parameters, Map.class); + return update(id, map); + } catch (Exception e) { + throw new TmfConfigurationException("Can't convert json string to Map to update configuration", e); //$NON-NLS-1$ + } + } + /** * Gets a configuration instance. * diff --git a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/config/TmfConfiguration.java b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/config/TmfConfiguration.java index 42dd4c1b3d..855e62f962 100644 --- a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/config/TmfConfiguration.java +++ b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/config/TmfConfiguration.java @@ -30,11 +30,12 @@ public class TmfConfiguration implements ITmfConfiguration { private final String fDescription; private final String fSourceTypeId; private final Map fParameters; + private final String fJsonParameters; /** * Constructor * - * @param bulider + * @param builder * the builder object to create the descriptor */ private TmfConfiguration(Builder builder) { @@ -43,6 +44,7 @@ private TmfConfiguration(Builder builder) { fDescription = builder.fDescription; fSourceTypeId = Objects.requireNonNull(builder.fSourceTypeId); fParameters = builder.fParameters; + fJsonParameters = builder.fJsonParameters; } @Override @@ -70,6 +72,11 @@ public Map getParameters() { return fParameters; } + @Override + public String getJsonParameters() { + return fJsonParameters; + } + @Override @SuppressWarnings("nls") public String toString() { @@ -80,6 +87,7 @@ public String toString() { .append(", fType=").append(getSourceTypeId()) .append(", fId=").append(getId()) .append(", fParameters=").append(getParameters()) + .append(", fJsonParameters=").append(getJsonParameters()) .append("]").toString(); } @@ -90,12 +98,13 @@ public boolean equals(@Nullable Object arg0) { } TmfConfiguration other = (TmfConfiguration) arg0; return Objects.equals(fName, other.fName) && Objects.equals(fId, other.fId) - && Objects.equals(fSourceTypeId, other.fSourceTypeId) && Objects.equals(fDescription, other.fDescription) && Objects.equals(fParameters, other.fParameters); + && Objects.equals(fSourceTypeId, other.fSourceTypeId) && Objects.equals(fDescription, other.fDescription) + && Objects.equals(fParameters, other.fParameters) && Objects.equals(fJsonParameters, other.fJsonParameters); } @Override public int hashCode() { - return Objects.hash(fName, fId, fSourceTypeId, fDescription, fParameters); + return Objects.hash(fName, fId, fSourceTypeId, fDescription, fParameters, fJsonParameters); } /** @@ -108,6 +117,7 @@ public static class Builder { private String fDescription = ""; //$NON-NLS-1$ private String fSourceTypeId = ""; //$NON-NLS-1$ private Map fParameters = new HashMap<>(); + private String fJsonParameters = ""; //$NON-NLS-1$ /** * Constructor @@ -178,6 +188,21 @@ public Builder setParameters(Map parameters) { return this; } + /** + * Sets the optional JSON parameters of the {@link ITmfConfiguration} + * instance + * + * @param jsonParameters + * the optional JSON parameters of the {@link ITmfConfiguration} + * instance + * @return the builder instance + * @since 9.5 + */ + public Builder setJsonParameters(String jsonParameters) { + fJsonParameters = jsonParameters; + return this; + } + /** * The method to construct an instance of {@link ITmfConfiguration} *