5
0
mirror of https://github.com/apache/sqoop.git synced 2025-05-04 04:31:18 +08:00

SQOOP-734 Change ConnectorBean to send JSON objects

(Jarek Jarcec Cecho)
This commit is contained in:
Bilung Lee 2012-12-03 17:54:29 -08:00
parent e03b202c11
commit 8a5cd6728a
7 changed files with 76 additions and 62 deletions

View File

@ -19,6 +19,7 @@
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import org.apache.commons.cli.CommandLine;
@ -78,7 +79,7 @@ public Object execute(List<String> args) {
private void showConnector(String cid) {
ConnectorBean connectorBean = readConnector(cid);
List<MConnector> connectors = connectorBean.getConnectors();
List<ResourceBundle> bundles = connectorBean.getResourceBundles();
Map<Long, ResourceBundle> bundles = connectorBean.getResourceBundles();
io.out.println("@|bold " + connectors.size() + " connector(s) to show: |@");
for (int i = 0; i < connectors.size(); i++) {
@ -93,7 +94,7 @@ private void showConnector(String cid) {
io.out.print(" Class: ");
io.out.println(connector.getClassName());
displayFormMetadataDetails(io, connector, bundles.get(i));
displayFormMetadataDetails(io, connector, bundles.get(connector.getPersistenceId()));
}

View File

@ -41,9 +41,6 @@
*/
public class ConnectionBean implements JsonBean {
private static final String ALL = "all";
private static final String ID = "id";
private static final String NAME = "name";
private static final String CONNECTOR_ID = "connector-id";
private static final String CONNECTOR_PART = "connector";
private static final String FRAMEWORK_PART = "framework";

View File

@ -18,7 +18,7 @@
package org.apache.sqoop.json;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
@ -39,11 +39,11 @@ public class ConnectorBean implements JsonBean {
private List<MConnector> connectors;
private List<ResourceBundle> bundles;
private Map<Long, ResourceBundle> bundles;
// for "extract"
public ConnectorBean(List<MConnector> connectors,
List<ResourceBundle> bundles) {
Map<Long, ResourceBundle> bundles) {
this.connectors = connectors;
this.bundles = bundles;
}
@ -56,69 +56,69 @@ public List<MConnector> getConnectors() {
return connectors;
}
public List<ResourceBundle> getResourceBundles() {
public Map<Long, ResourceBundle> getResourceBundles() {
return bundles;
}
@SuppressWarnings("unchecked")
@Override
public JSONObject extract() {
JSONArray idArray = new JSONArray();
JSONArray nameArray = new JSONArray();
JSONArray classArray = new JSONArray();
JSONArray conFormsArray = new JSONArray();
JSONArray jobFormsArray = new JSONArray();
JSONArray bundlesArray;
JSONArray array = new JSONArray();
for (MConnector connector : connectors) {
idArray.add(connector.getPersistenceId());
nameArray.add(connector.getUniqueName());
classArray.add(connector.getClassName());
conFormsArray.add(extractForms(connector.getConnectionForms().getForms()));
JSONObject object = new JSONObject();
object.put(ID, connector.getPersistenceId());
object.put(NAME, connector.getUniqueName());
object.put(CLASS, connector.getClassName());
object.put(CON_FORMS, extractForms(connector.getConnectionForms().getForms()));
JSONObject jobForms = new JSONObject();
for (MJobForms job : connector.getAllJobsForms().values()) {
jobForms.put(job.getType().name(), extractForms(job.getForms()));
}
jobFormsArray.add(jobForms);
object.put(JOB_FORMS, jobForms);
array.add(object);
}
bundlesArray = extractResourceBundles(bundles);
JSONObject all = new JSONObject();
all.put(ALL, array);
JSONObject result = new JSONObject();
result.put(ID, idArray);
result.put(NAME, nameArray);
result.put(CLASS, classArray);
result.put(CON_FORMS, conFormsArray);
result.put(JOB_FORMS, jobFormsArray);
result.put(RESOURCES, bundlesArray);
return result;
if(bundles != null && !bundles.isEmpty()) {
JSONObject jsonBundles = new JSONObject();
for(Map.Entry<Long, ResourceBundle> entry : bundles.entrySet()) {
jsonBundles.put(entry.getKey().toString(),
extractResourceBundle(entry.getValue()));
}
all.put(CONNECTOR_RESOURCES, jsonBundles);
}
return all;
}
@Override
@SuppressWarnings("unchecked")
public void restore(JSONObject jsonObject) {
JSONArray idArray = (JSONArray) jsonObject.get(ID);
JSONArray nameArray = (JSONArray) jsonObject.get(NAME);
JSONArray classArray = (JSONArray) jsonObject.get(CLASS);
JSONArray conFormsArray =
(JSONArray) jsonObject.get(CON_FORMS);
JSONArray jobFormsArray =
(JSONArray) jsonObject.get(JOB_FORMS);
connectors = new ArrayList<MConnector>();
connectors = new LinkedList<MConnector>();
for (int i = 0; i < idArray.size(); i++) {
long persistenceId = (Long) idArray.get(i);
String uniqueName = (String) nameArray.get(i);
String className = (String) classArray.get(i);
JSONArray array = (JSONArray) jsonObject.get(ALL);
List<MForm> connForms = restoreForms((JSONArray) conFormsArray.get(i));
for (Object obj : array) {
JSONObject object = (JSONObject) obj;
JSONObject jobJson = (JSONObject) jobFormsArray.get(i);
long connectorId = (Long) object.get(ID);
String uniqueName = (String) object.get(NAME);
String className = (String) object.get(CLASS);
List<MForm> connForms = restoreForms((JSONArray) object.get(CON_FORMS));
JSONObject jobJson = (JSONObject) object.get(JOB_FORMS);
List<MJobForms> jobs = new ArrayList<MJobForms>();
for( Map.Entry entry : (Set<Map.Entry>) jobJson.entrySet()) {
//TODO(jarcec): Handle situation when server is supporting operation
// that client do not know (server do have newer version than client)
MJob.Type type = MJob.Type.valueOf((String) entry.getKey());
List<MForm> jobForms =
@ -127,12 +127,21 @@ public void restore(JSONObject jsonObject) {
jobs.add(new MJobForms(type, jobForms));
}
MConnector connector = new MConnector(uniqueName, className,
new MConnectionForms(connForms), jobs);
connector.setPersistenceId(persistenceId);
MConnector connector = new MConnector(uniqueName, className, new MConnectionForms(connForms), jobs);
connector.setPersistenceId(connectorId);
connectors.add(connector);
}
bundles = restoreResourceBundles((JSONArray) jsonObject.get(RESOURCES));
if(jsonObject.containsKey(CONNECTOR_RESOURCES)) {
bundles = new HashMap<Long, ResourceBundle>();
JSONObject jsonBundles = (JSONObject) jsonObject.get(CONNECTOR_RESOURCES);
Set<Map.Entry<String, JSONObject>> entrySet = jsonBundles.entrySet();
for (Map.Entry<String, JSONObject> entry : entrySet) {
bundles.put(Long.parseLong(entry.getKey()),
restoreResourceBundle(entry.getValue()));
}
}
}
}

View File

@ -37,11 +37,12 @@
*/
public final class FormSerialization {
public static final String ALL = "all";
public static final String ID = "id";
public static final String NAME = "name";
public static final String CLASS = "class";
public static final String CON_FORMS = "con_forms";
public static final String JOB_FORMS = "job_forms";
public static final String CON_FORMS = "con-forms";
public static final String JOB_FORMS = "job-forms";
public static final String FORM_NAME = "name";
public static final String FORM_TYPE = "type";

View File

@ -24,8 +24,10 @@
import org.json.simple.JSONValue;
import org.junit.Test;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import static org.apache.sqoop.json.TestUtil.*;
@ -47,9 +49,9 @@ public void testSerialization() {
connectors.add(getConnector("mysql"));
// Create testing bundles
List<ResourceBundle> bundles = new LinkedList<ResourceBundle>();
bundles.add(getResourceBundle());
bundles.add(getResourceBundle());
Map<Long, ResourceBundle> bundles = new HashMap<Long, ResourceBundle>();
bundles.put(1L, getResourceBundle());
bundles.put(2L, getResourceBundle());
// Serialize it to JSON object
ConnectorBean bean = new ConnectorBean(connectors, bundles);
@ -66,7 +68,8 @@ public void testSerialization() {
assertEquals(connectors.size(), retrievedBean.getConnectors().size());
assertEquals(connectors.get(0), retrievedBean.getConnectors().get(0));
ResourceBundle retrievedBundle = retrievedBean.getResourceBundles().get(0);
ResourceBundle retrievedBundle = retrievedBean.getResourceBundles().get(1L);
assertNotNull(retrievedBundle);
assertEquals("a", retrievedBundle.getString("a"));
assertEquals("b", retrievedBundle.getString("b"));
}

View File

@ -61,10 +61,12 @@ public static Set<Long> getConnectorIds() {
return nameMap.keySet();
}
public static List<ResourceBundle> getResourceBundles(Locale locale) {
List<ResourceBundle> bundles = new LinkedList<ResourceBundle>();
public static Map<Long, ResourceBundle> getResourceBundles(Locale locale) {
Map<Long, ResourceBundle> bundles = new HashMap<Long, ResourceBundle>();
for(ConnectorHandler handler : handlerMap.values()) {
bundles.add(handler.getConnector().getBundle(locale));
long id = handler.getMetadata().getPersistenceId();
ResourceBundle bundle = handler.getConnector().getBundle(locale);
bundles.put(id, bundle);
}
return bundles;
}

View File

@ -27,9 +27,11 @@
import org.apache.sqoop.server.RequestHandler;
import org.apache.sqoop.server.common.ServerError;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
public class ConnectorRequestHandler implements RequestHandler {
@ -45,7 +47,7 @@ public ConnectorRequestHandler() {
@Override
public JsonBean handleEvent(RequestContext ctx) {
List<MConnector> connectors;
List<ResourceBundle> bundles;
Map<Long, ResourceBundle> bundles;
Locale locale = ctx.getAcceptLanguageHeader();
String cid = ctx.getLastURLElement();
@ -55,7 +57,6 @@ public JsonBean handleEvent(RequestContext ctx) {
// display all connectors
connectors = ConnectorManager.getConnectorsMetadata();
bundles = ConnectorManager.getResourceBundles(locale);
} else {
Long id = Long.parseLong(cid);
@ -65,10 +66,10 @@ public JsonBean handleEvent(RequestContext ctx) {
}
connectors = new LinkedList<MConnector>();
bundles = new LinkedList<ResourceBundle>();
bundles = new HashMap<Long, ResourceBundle>();
connectors.add(ConnectorManager.getConnectorMetadata(id));
bundles.add(ConnectorManager.getResourceBundle(id, locale));
bundles.put(id, ConnectorManager.getResourceBundle(id, locale));
}
return new ConnectorBean(connectors, bundles);