sqoop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject [2/3] git commit: SQOOP-734 Change ConnectorBean to send JSON objects (Jarek Jarcec Cecho)
Date Tue, 04 Dec 2012 16:31:02 GMT
SQOOP-734 Change ConnectorBean to send JSON objects
(Jarek Jarcec Cecho)


Project: http://git-wip-us.apache.org/repos/asf/sqoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/sqoop/commit/8a5cd672
Tree: http://git-wip-us.apache.org/repos/asf/sqoop/tree/8a5cd672
Diff: http://git-wip-us.apache.org/repos/asf/sqoop/diff/8a5cd672

Branch: refs/heads/sqoop2
Commit: 8a5cd6728a8e7b9f0014456be0580bac45d940c3
Parents: e03b202
Author: Bilung Lee <blee@apache.org>
Authored: Mon Dec 3 17:54:29 2012 -0800
Committer: Bilung Lee <blee@apache.org>
Committed: Mon Dec 3 17:54:29 2012 -0800

----------------------------------------------------------------------
 .../sqoop/client/shell/ShowConnectorFunction.java  |    5 +-
 .../java/org/apache/sqoop/json/ConnectionBean.java |    3 -
 .../java/org/apache/sqoop/json/ConnectorBean.java  |  103 ++++++++-------
 .../apache/sqoop/json/util/FormSerialization.java  |    5 +-
 .../org/apache/sqoop/json/TestConnectorBean.java   |   11 +-
 .../apache/sqoop/connector/ConnectorManager.java   |    8 +-
 .../sqoop/handler/ConnectorRequestHandler.java     |    9 +-
 7 files changed, 79 insertions(+), 65 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/sqoop/blob/8a5cd672/client/src/main/java/org/apache/sqoop/client/shell/ShowConnectorFunction.java
----------------------------------------------------------------------
diff --git a/client/src/main/java/org/apache/sqoop/client/shell/ShowConnectorFunction.java
b/client/src/main/java/org/apache/sqoop/client/shell/ShowConnectorFunction.java
index a34c48c..ac2683c 100644
--- a/client/src/main/java/org/apache/sqoop/client/shell/ShowConnectorFunction.java
+++ b/client/src/main/java/org/apache/sqoop/client/shell/ShowConnectorFunction.java
@@ -19,6 +19,7 @@ package org.apache.sqoop.client.shell;
 
 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 class ShowConnectorFunction extends SqoopFunction
   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 @@ public class ShowConnectorFunction extends SqoopFunction
       io.out.print("  Class: ");
       io.out.println(connector.getClassName());
 
-      displayFormMetadataDetails(io, connector, bundles.get(i));
+      displayFormMetadataDetails(io, connector, bundles.get(connector.getPersistenceId()));
 
     }
 

http://git-wip-us.apache.org/repos/asf/sqoop/blob/8a5cd672/common/src/main/java/org/apache/sqoop/json/ConnectionBean.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/json/ConnectionBean.java b/common/src/main/java/org/apache/sqoop/json/ConnectionBean.java
index c793465..4562047 100644
--- a/common/src/main/java/org/apache/sqoop/json/ConnectionBean.java
+++ b/common/src/main/java/org/apache/sqoop/json/ConnectionBean.java
@@ -41,9 +41,6 @@ import static org.apache.sqoop.json.util.ResourceBundleSerialization.*;
  */
 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";

http://git-wip-us.apache.org/repos/asf/sqoop/blob/8a5cd672/common/src/main/java/org/apache/sqoop/json/ConnectorBean.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/json/ConnectorBean.java b/common/src/main/java/org/apache/sqoop/json/ConnectorBean.java
index f630f75..82d865c 100644
--- a/common/src/main/java/org/apache/sqoop/json/ConnectorBean.java
+++ b/common/src/main/java/org/apache/sqoop/json/ConnectorBean.java
@@ -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 class ConnectorBean implements JsonBean {
     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 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);
-
-      List<MForm> connForms = restoreForms((JSONArray) conFormsArray.get(i));
-
-      JSONObject jobJson = (JSONObject) jobFormsArray.get(i);
+    connectors = new ArrayList<MConnector>();
+
+    JSONArray array = (JSONArray) jsonObject.get(ALL);
+
+    for (Object obj : array) {
+      JSONObject object = (JSONObject) obj;
+
+      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 class ConnectorBean implements JsonBean {
         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()));
+      }
+    }
   }
 }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/8a5cd672/common/src/main/java/org/apache/sqoop/json/util/FormSerialization.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/json/util/FormSerialization.java b/common/src/main/java/org/apache/sqoop/json/util/FormSerialization.java
index 3d69bf8..732293a 100644
--- a/common/src/main/java/org/apache/sqoop/json/util/FormSerialization.java
+++ b/common/src/main/java/org/apache/sqoop/json/util/FormSerialization.java
@@ -37,11 +37,12 @@ import java.util.List;
  */
 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";

http://git-wip-us.apache.org/repos/asf/sqoop/blob/8a5cd672/common/src/test/java/org/apache/sqoop/json/TestConnectorBean.java
----------------------------------------------------------------------
diff --git a/common/src/test/java/org/apache/sqoop/json/TestConnectorBean.java b/common/src/test/java/org/apache/sqoop/json/TestConnectorBean.java
index dc8dd95..38cdb7e 100644
--- a/common/src/test/java/org/apache/sqoop/json/TestConnectorBean.java
+++ b/common/src/test/java/org/apache/sqoop/json/TestConnectorBean.java
@@ -24,8 +24,10 @@ import org.json.simple.JSONObject;
 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 class TestConnectorBean {
     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 class TestConnectorBean {
     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"));
   }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/8a5cd672/core/src/main/java/org/apache/sqoop/connector/ConnectorManager.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/sqoop/connector/ConnectorManager.java b/core/src/main/java/org/apache/sqoop/connector/ConnectorManager.java
index 82f88fd..c2342f7 100644
--- a/core/src/main/java/org/apache/sqoop/connector/ConnectorManager.java
+++ b/core/src/main/java/org/apache/sqoop/connector/ConnectorManager.java
@@ -61,10 +61,12 @@ public final class ConnectorManager {
     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;
   }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/8a5cd672/server/src/main/java/org/apache/sqoop/handler/ConnectorRequestHandler.java
----------------------------------------------------------------------
diff --git a/server/src/main/java/org/apache/sqoop/handler/ConnectorRequestHandler.java b/server/src/main/java/org/apache/sqoop/handler/ConnectorRequestHandler.java
index 8a52243..6dee70c 100644
--- a/server/src/main/java/org/apache/sqoop/handler/ConnectorRequestHandler.java
+++ b/server/src/main/java/org/apache/sqoop/handler/ConnectorRequestHandler.java
@@ -27,9 +27,11 @@ import org.apache.sqoop.server.RequestContext;
 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 class ConnectorRequestHandler implements RequestHandler {
   @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 class ConnectorRequestHandler implements RequestHandler {
       // display all connectors
       connectors = ConnectorManager.getConnectorsMetadata();
       bundles = ConnectorManager.getResourceBundles(locale);
-
     } else {
       Long id = Long.parseLong(cid);
 
@@ -65,10 +66,10 @@ public class ConnectorRequestHandler implements RequestHandler {
       }
 
       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);


Mime
View raw message