-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow chePlugin preferences to be string, int or boolean type
- Loading branch information
1 parent
f57a95e
commit ba1c521
Showing
29 changed files
with
289 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...e/src/main/java/org/eclipse/che/api/workspace/server/devfile/PreferencesDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (c) 2012-2018 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.che.api.workspace.server.devfile; | ||
|
||
import static java.lang.String.format; | ||
|
||
import com.fasterxml.jackson.core.JsonParseException; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.eclipse.che.api.core.model.workspace.devfile.Component; | ||
|
||
/** | ||
* Helps to deserialize multi-type preferences into {@link Component} as a {@code Map}. | ||
* | ||
* @author Max Shaposhnyk | ||
*/ | ||
public class PreferencesDeserializer extends JsonDeserializer<Map<String, Serializable>> { | ||
|
||
@Override | ||
public Map<String, Serializable> deserialize(JsonParser jsonParser, DeserializationContext ctxt) | ||
throws IOException { | ||
Map<String, Serializable> result = new HashMap<>(); | ||
jsonParser.nextToken(); | ||
while (!JsonToken.END_OBJECT.equals(jsonParser.getCurrentToken())) { | ||
JsonToken currentToken = jsonParser.nextValue(); | ||
switch (currentToken) { | ||
case VALUE_NUMBER_INT: | ||
case VALUE_NUMBER_FLOAT: | ||
result.put(jsonParser.getCurrentName(), jsonParser.getNumberValue()); | ||
break; | ||
case VALUE_FALSE: | ||
case VALUE_TRUE: | ||
result.put(jsonParser.getCurrentName(), jsonParser.getValueAsBoolean()); | ||
break; | ||
case VALUE_STRING: | ||
result.put(jsonParser.getCurrentName(), jsonParser.getValueAsString()); | ||
break; | ||
default: | ||
throw new JsonParseException( | ||
jsonParser, | ||
format( | ||
"Unexpected value of the preference with key '%s'.", | ||
jsonParser.getCurrentName())); | ||
} | ||
jsonParser.nextToken(); | ||
} | ||
return result; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...ace/src/main/java/org/eclipse/che/api/workspace/server/devfile/SerializableConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (c) 2012-2018 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.che.api.workspace.server.devfile; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import javax.persistence.AttributeConverter; | ||
import javax.persistence.Converter; | ||
import org.eclipse.che.api.core.model.workspace.devfile.Component; | ||
|
||
/** | ||
* Helps to store and read serializable values of the preferences map in {@link Component} to/from | ||
* database. | ||
* | ||
* @author Max Shaposhnyk | ||
*/ | ||
@Converter(autoApply = true) | ||
public class SerializableConverter implements AttributeConverter<Serializable, String> { | ||
|
||
private ObjectMapper objectMapper; | ||
|
||
public SerializableConverter() { | ||
this.objectMapper = new ObjectMapper(); | ||
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.OBJECT_AND_NON_CONCRETE); | ||
} | ||
|
||
@Override | ||
public String convertToDatabaseColumn(Serializable value) { | ||
try { | ||
return objectMapper.writeValueAsString(value); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException("Unable to serialize preference value:" + e.getMessage(), e); | ||
} | ||
} | ||
|
||
@Override | ||
public Serializable convertToEntityAttribute(String dbData) { | ||
try { | ||
return objectMapper.readValue(dbData, Serializable.class); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Unable to deserialize preference value:" + e.getMessage(), e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.