Search in sources :

Example 11 with JSONException

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;
}
Also used : JSONObject(org.talend.utils.json.JSONObject) EMap(org.eclipse.emf.common.util.EMap) JSONArray(org.talend.utils.json.JSONArray) JSONException(org.talend.utils.json.JSONException) NoSQLConnection(org.talend.repository.model.nosql.NoSQLConnection) Map(java.util.Map) EMap(org.eclipse.emf.common.util.EMap)

Example 12 with JSONException

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));
}
Also used : JSONObject(org.talend.utils.json.JSONObject) JSONArray(org.talend.utils.json.JSONArray) JSONException(org.talend.utils.json.JSONException) Test(org.junit.Test)

Example 13 with JSONException

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"));
}
Also used : JSONObject(org.talend.utils.json.JSONObject) JSONException(org.talend.utils.json.JSONException) Test(org.junit.Test)

Example 14 with JSONException

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;
}
Also used : JSONObject(org.talend.utils.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(org.talend.utils.json.JSONArray) JSONException(org.talend.utils.json.JSONException) JSONObject(org.talend.utils.json.JSONObject)

Example 15 with JSONException

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;
}
Also used : Path(org.eclipse.core.runtime.Path) JSONObject(org.talend.utils.json.JSONObject) Bundle(org.osgi.framework.Bundle) JSONException(org.talend.utils.json.JSONException) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL)

Aggregations

JSONException (org.talend.utils.json.JSONException)26 JSONObject (org.talend.utils.json.JSONObject)21 JSONArray (org.talend.utils.json.JSONArray)14 Project (org.talend.core.model.general.Project)6 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)5 PersistenceException (org.talend.commons.exception.PersistenceException)4 NoSQLConnection (org.talend.repository.model.nosql.NoSQLConnection)4 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 ConnectionBean (org.talend.core.model.general.ConnectionBean)3 EHadoopParamName (org.talend.metadata.managment.ui.utils.ExtendedNodeConnectionContextUtils.EHadoopParamName)3 Iterator (java.util.Iterator)2 SystemException (org.talend.commons.exception.SystemException)2 Context (org.talend.core.context.Context)2 RepositoryContext (org.talend.core.context.RepositoryContext)2 User (org.talend.core.model.properties.User)2 IConnParamName (org.talend.metadata.managment.ui.model.IConnParamName)2 IProxyRepositoryFactory (org.talend.repository.model.IProxyRepositoryFactory)2