5
0
mirror of https://github.com/apache/sqoop.git synced 2025-05-08 03:51:38 +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.io.PrintWriter;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLine;
@ -78,7 +79,7 @@ public Object execute(List<String> args) {
private void showConnector(String cid) { private void showConnector(String cid) {
ConnectorBean connectorBean = readConnector(cid); ConnectorBean connectorBean = readConnector(cid);
List<MConnector> connectors = connectorBean.getConnectors(); 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: |@"); io.out.println("@|bold " + connectors.size() + " connector(s) to show: |@");
for (int i = 0; i < connectors.size(); i++) { for (int i = 0; i < connectors.size(); i++) {
@ -93,7 +94,7 @@ private void showConnector(String cid) {
io.out.print(" Class: "); io.out.print(" Class: ");
io.out.println(connector.getClassName()); 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 { 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_ID = "connector-id";
private static final String CONNECTOR_PART = "connector"; private static final String CONNECTOR_PART = "connector";
private static final String FRAMEWORK_PART = "framework"; private static final String FRAMEWORK_PART = "framework";

View File

@ -18,7 +18,7 @@
package org.apache.sqoop.json; package org.apache.sqoop.json;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.ResourceBundle; import java.util.ResourceBundle;
@ -39,11 +39,11 @@ public class ConnectorBean implements JsonBean {
private List<MConnector> connectors; private List<MConnector> connectors;
private List<ResourceBundle> bundles; private Map<Long, ResourceBundle> bundles;
// for "extract" // for "extract"
public ConnectorBean(List<MConnector> connectors, public ConnectorBean(List<MConnector> connectors,
List<ResourceBundle> bundles) { Map<Long, ResourceBundle> bundles) {
this.connectors = connectors; this.connectors = connectors;
this.bundles = bundles; this.bundles = bundles;
} }
@ -56,69 +56,69 @@ public List<MConnector> getConnectors() {
return connectors; return connectors;
} }
public List<ResourceBundle> getResourceBundles() { public Map<Long, ResourceBundle> getResourceBundles() {
return bundles; return bundles;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public JSONObject extract() { public JSONObject extract() {
JSONArray idArray = new JSONArray();
JSONArray nameArray = new JSONArray(); JSONArray array = new JSONArray();
JSONArray classArray = new JSONArray();
JSONArray conFormsArray = new JSONArray();
JSONArray jobFormsArray = new JSONArray();
JSONArray bundlesArray;
for (MConnector connector : connectors) { for (MConnector connector : connectors) {
idArray.add(connector.getPersistenceId()); JSONObject object = new JSONObject();
nameArray.add(connector.getUniqueName());
classArray.add(connector.getClassName()); object.put(ID, connector.getPersistenceId());
conFormsArray.add(extractForms(connector.getConnectionForms().getForms())); object.put(NAME, connector.getUniqueName());
object.put(CLASS, connector.getClassName());
object.put(CON_FORMS, extractForms(connector.getConnectionForms().getForms()));
JSONObject jobForms = new JSONObject(); JSONObject jobForms = new JSONObject();
for (MJobForms job : connector.getAllJobsForms().values()) { for (MJobForms job : connector.getAllJobsForms().values()) {
jobForms.put(job.getType().name(), extractForms(job.getForms())); 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(); if(bundles != null && !bundles.isEmpty()) {
result.put(ID, idArray); JSONObject jsonBundles = new JSONObject();
result.put(NAME, nameArray);
result.put(CLASS, classArray); for(Map.Entry<Long, ResourceBundle> entry : bundles.entrySet()) {
result.put(CON_FORMS, conFormsArray); jsonBundles.put(entry.getKey().toString(),
result.put(JOB_FORMS, jobFormsArray); extractResourceBundle(entry.getValue()));
result.put(RESOURCES, bundlesArray); }
return result;
all.put(CONNECTOR_RESOURCES, jsonBundles);
}
return all;
} }
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void restore(JSONObject jsonObject) { public void restore(JSONObject jsonObject) {
JSONArray idArray = (JSONArray) jsonObject.get(ID); connectors = new ArrayList<MConnector>();
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 LinkedList<MConnector>(); JSONArray array = (JSONArray) jsonObject.get(ALL);
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);
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>(); List<MJobForms> jobs = new ArrayList<MJobForms>();
for( Map.Entry entry : (Set<Map.Entry>) jobJson.entrySet()) { 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()); MJob.Type type = MJob.Type.valueOf((String) entry.getKey());
List<MForm> jobForms = List<MForm> jobForms =
@ -127,12 +127,21 @@ public void restore(JSONObject jsonObject) {
jobs.add(new MJobForms(type, jobForms)); jobs.add(new MJobForms(type, jobForms));
} }
MConnector connector = new MConnector(uniqueName, className, MConnector connector = new MConnector(uniqueName, className, new MConnectionForms(connForms), jobs);
new MConnectionForms(connForms), jobs); connector.setPersistenceId(connectorId);
connector.setPersistenceId(persistenceId);
connectors.add(connector); 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 final class FormSerialization {
public static final String ALL = "all";
public static final String ID = "id"; public static final String ID = "id";
public static final String NAME = "name"; public static final String NAME = "name";
public static final String CLASS = "class"; public static final String CLASS = "class";
public static final String CON_FORMS = "con_forms"; public static final String CON_FORMS = "con-forms";
public static final String JOB_FORMS = "job_forms"; public static final String JOB_FORMS = "job-forms";
public static final String FORM_NAME = "name"; public static final String FORM_NAME = "name";
public static final String FORM_TYPE = "type"; public static final String FORM_TYPE = "type";

View File

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

View File

@ -61,10 +61,12 @@ public static Set<Long> getConnectorIds() {
return nameMap.keySet(); return nameMap.keySet();
} }
public static List<ResourceBundle> getResourceBundles(Locale locale) { public static Map<Long, ResourceBundle> getResourceBundles(Locale locale) {
List<ResourceBundle> bundles = new LinkedList<ResourceBundle>(); Map<Long, ResourceBundle> bundles = new HashMap<Long, ResourceBundle>();
for(ConnectorHandler handler : handlerMap.values()) { 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; return bundles;
} }

View File

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