use of org.talend.utils.json.JSONException in project tbd-studio-se by Talend.
the class NoSqlContextUpdateService method updateContextParameter.
@Override
public boolean updateContextParameter(Connection conn, String oldValue, String newValue) {
boolean isModified = false;
if (conn.isContextMode()) {
if (conn instanceof NoSQLConnection) {
NoSQLConnection connection = (NoSQLConnection) conn;
EMap<String, String> connAttributes = (EMap<String, String>) connection.getAttributes();
if (connAttributes == null) {
return isModified;
}
for (Map.Entry<String, String> attr : connection.getAttributes()) {
if (attr.equals(IMongoDBAttributes.REPLICA_SET)) {
String replicaSets = connAttributes.get(IMongoDBAttributes.REPLICA_SET);
try {
JSONArray jsa = new JSONArray(replicaSets);
for (int i = 0; i < jsa.length(); i++) {
JSONObject jso = jsa.getJSONObject(i);
String hostValue = jso.getString(IMongoConstants.REPLICA_HOST_KEY);
if (hostValue != null && hostValue.equals(oldValue)) {
jso.put(IMongoConstants.REPLICA_HOST_KEY, newValue);
connAttributes.put(IMongoDBAttributes.REPLICA_SET, jsa.toString());
isModified = true;
}
String portValue = jso.getString(IMongoConstants.REPLICA_PORT_KEY);
if (portValue != null && portValue.equals(oldValue)) {
jso.put(IMongoConstants.REPLICA_PORT_KEY, newValue);
connAttributes.put(IMongoDBAttributes.REPLICA_SET, jsa.toString());
isModified = true;
}
}
} catch (JSONException e) {
ExceptionHandler.process(e);
}
} else if (attr.getValue().equals(oldValue)) {
attr.setValue(newValue);
isModified = true;
}
}
}
}
return isModified;
}
use of org.talend.utils.json.JSONException in project tbd-studio-se by Talend.
the class DefaultConfigurationManagerTest method testGetValue_Array.
@Test
public void testGetValue_Array() throws JSONException {
JSONObject json = new JSONObject();
json.put("key1", "123");
JSONArray arr = new JSONArray();
arr.put(1);
arr.put(2);
arr.put(13);
arr.put("abc");
json.put("arr1", arr);
String value = DefaultConfigurationManager.getValue(json, "arr1");
assertNotNull(value);
JSONArray getArr = null;
try {
getArr = new JSONArray(value);
} catch (JSONException e) {
//
}
assertNotNull("Can't support to get array", getArr);
assertEquals(4, getArr.length());
assertEquals(1, getArr.get(0));
assertEquals(2, getArr.get(1));
assertEquals(13, getArr.get(2));
assertEquals("abc", getArr.get(3));
}
use of org.talend.utils.json.JSONException in project tbd-studio-se by Talend.
the class DefaultConfigurationManagerTest method testGetValue_DeepLevel.
@Test
public void testGetValue_DeepLevel() throws JSONException {
JSONObject json = new JSONObject();
json.put("key1", "123");
JSONObject level1 = new JSONObject();
level1.put("level1", "789");
json.put("L1", level1);
JSONObject level2 = new JSONObject();
level2.put("level2", 10);
level1.put("L2", level2);
String value = DefaultConfigurationManager.getValue(json, "key1");
assertNotNull(value);
assertEquals("123", value);
value = DefaultConfigurationManager.getValue(json, "L1", "level1");
assertNotNull(value);
assertEquals("789", value);
value = DefaultConfigurationManager.getValue(json, "L1", "L2", "level2");
assertNotNull(value);
assertEquals("10", value);
value = DefaultConfigurationManager.getValue(json, "L1", "L2");
assertNotNull(value);
JSONObject getJson = null;
try {
getJson = new JSONObject(value);
} catch (JSONException e) {
//
}
assertNotNull(getJson);
assertEquals(10, getJson.getInt("level2"));
}
use of org.talend.utils.json.JSONException in project tbd-studio-se by Talend.
the class RetrieveLocalConfsService method getSupportConfsList.
private List<String> getSupportConfsList() {
try {
List<String> supportConfs = new ArrayList<String>();
String confStr = distributionVersion.getDefaultConfig(distributionVersion.getDistribution().getName());
// $NON-NLS-1$
JSONArray jsonArray = new JSONObject(confStr).getJSONArray("HADOOP_CONFIG_LIST");
for (int i = 0; i < jsonArray.length(); i++) {
Object obj = jsonArray.get(i);
if (obj instanceof String) {
supportConfs.add((String) obj);
}
}
if (!supportConfs.isEmpty()) {
return supportConfs;
}
} catch (JSONException e) {
ExceptionHandler.process(e);
}
return null;
}
use of org.talend.utils.json.JSONException in project tbd-studio-se by Talend.
the class DefaultConfigurationManager method loadPathFile.
public static JSONObject loadPathFile(Class bundleClass, String path) {
try {
Bundle b = FrameworkUtil.getBundle(bundleClass);
URL url = FileLocator.find(b, new Path(path), null);
if (url != null) {
url = FileLocator.toFileURL(url);
if (url != null) {
File configFile = new File(url.getPath());
String contents = getContents(configFile);
if (contents != null) {
return new JSONObject(contents);
}
}
}
} catch (IOException e) {
// ExceptionHandler.process(e);
} catch (JSONException e) {
// ExceptionHandler.process(e);
}
return null;
}
Aggregations