Skip to content

Commit

Permalink
tmf: add jsonParameters to ITmfConfiguration and ITmfConfigurationSource
Browse files Browse the repository at this point in the history
This is as alternative to Map<String, Object>.

[Added] jsonParameters to ITmfConfiguration and ITmfConfigurationSource

Signed-off-by: Bernd Hufmann <[email protected]>
  • Loading branch information
bhufmann committed Sep 23, 2024
1 parent 381d0e4 commit 081e731
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> 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$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -43,6 +45,29 @@ public interface ITmfConfigurationSource {
*/
ITmfConfiguration create(Map<String, Object> parameters) throws TmfConfigurationException;

/**
* Creates a new configuration instance.
* <p>
* 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<String, Object> 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.
* <p>
Expand All @@ -59,6 +84,31 @@ public interface ITmfConfigurationSource {
*/
ITmfConfiguration update(String id, Map<String, Object> parameters) throws TmfConfigurationException;

/**
* Updates a configuration instance.
* <p>
* 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<String, Object> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class TmfConfiguration implements ITmfConfiguration {
/**
* Constructor
*
* @param bulider
* @param builder
* the builder object to create the descriptor
*/
private TmfConfiguration(Builder builder) {
Expand Down

0 comments on commit 081e731

Please sign in to comment.