sqoop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jar...@apache.org
Subject svn commit: r1360620 - in /sqoop/branches/sqoop2: client/src/main/java/org/apache/sqoop/client/shell/ common/src/main/java/org/apache/sqoop/json/ core/src/main/java/org/apache/sqoop/connector/ server/src/main/java/org/apache/sqoop/handler/
Date Thu, 12 Jul 2012 10:34:34 GMT
Author: jarcec
Date: Thu Jul 12 10:34:34 2012
New Revision: 1360620

URL: http://svn.apache.org/viewvc?rev=1360620&view=rev
Log:
SQOOP-502. Implement show connector command end-to-end.

(Bilung Lee via Jarek Jarcec Cecho)

Modified:
    sqoop/branches/sqoop2/client/src/main/java/org/apache/sqoop/client/shell/ShowConnectorFunction.java
    sqoop/branches/sqoop2/client/src/main/java/org/apache/sqoop/client/shell/ShowServerFunction.java
    sqoop/branches/sqoop2/common/src/main/java/org/apache/sqoop/json/ConnectorBean.java
    sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorError.java
    sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorManager.java
    sqoop/branches/sqoop2/server/src/main/java/org/apache/sqoop/handler/ConnectorRequestHandler.java

Modified: sqoop/branches/sqoop2/client/src/main/java/org/apache/sqoop/client/shell/ShowConnectorFunction.java
URL: http://svn.apache.org/viewvc/sqoop/branches/sqoop2/client/src/main/java/org/apache/sqoop/client/shell/ShowConnectorFunction.java?rev=1360620&r1=1360619&r2=1360620&view=diff
==============================================================================
--- sqoop/branches/sqoop2/client/src/main/java/org/apache/sqoop/client/shell/ShowConnectorFunction.java
(original)
+++ sqoop/branches/sqoop2/client/src/main/java/org/apache/sqoop/client/shell/ShowConnectorFunction.java
Thu Jul 12 10:34:34 2012
@@ -18,6 +18,7 @@
 package org.apache.sqoop.client.shell;
 
 import java.io.PrintWriter;
+import java.util.Iterator;
 import java.util.List;
 
 import org.apache.commons.cli.CommandLine;
@@ -25,6 +26,11 @@ import org.apache.commons.cli.OptionBuil
 import org.apache.sqoop.client.core.Environment;
 import org.apache.sqoop.client.request.ConnectorRequest;
 import org.apache.sqoop.json.ConnectorBean;
+import org.apache.sqoop.model.MConnector;
+import org.apache.sqoop.model.MForm;
+import org.apache.sqoop.model.MInput;
+import org.apache.sqoop.model.MInputType;
+import org.apache.sqoop.model.MStringInput;
 import org.codehaus.groovy.tools.shell.IO;
 
 @SuppressWarnings("serial")
@@ -79,36 +85,62 @@ public class ShowConnectorFunction exten
     }
     ConnectorBean connectorBean =
       conntectorRequest.doGet(Environment.getServerUrl(), cid);
-    long[] ids = connectorBean.getIds();
-    String[] names = connectorBean.getNames();
-    String[] classes = connectorBean.getClasses();
-
-    if (cid == null) {
-      io.out.println("@|bold Metadata for all connectors:|@");  
-      int size = ids.length;
-      for (int i = 0; i < size; i++) {
-        io.out.print("Connector ");
-        io.out.print(i+1);
-        io.out.println(":");
-  
-        io.out.print("  Id: ");
-        io.out.println(ids[i]);
-        io.out.print("  Name: ");
-        io.out.println(names[i]);
-        io.out.print("  Class: ");
-        io.out.println(classes[i]);
-      }
+    MConnector[] connectors = connectorBean.getConnectos();
 
-    } else {
-      io.out.println("@|bold Metadata for the connector:|@");
-      io.out.print("  Id: ");
-      io.out.println(ids[0]);
+    io.out.println("@|bold " + connectors.length + " connector(s) to show: |@");
+    for (int i = 0; i < connectors.length; i++) {
+      MConnector connector = connectors[i];
+
+      io.out.print("Connector with id ");
+      io.out.print(connector.getPersistenceId());
+      io.out.println(":");
+  
       io.out.print("  Name: ");
-      io.out.println(names[0]);
+      io.out.println(connector.getUniqueName());
       io.out.print("  Class: ");
-      io.out.println(classes[0]);
+      io.out.println(connector.getClassName());
+
+      displayForms(connector.getConnectionForms(), "Connection");
+      displayForms(connector.getJobForms(), "Job");
     }
 
     io.out.println();
   }
+
+  private void displayForms(List<MForm> forms, String type) {
+    Iterator<MForm> fiter = forms.iterator();
+    int findx = 1;
+    while (fiter.hasNext()) {
+      io.out.print("  ");
+      io.out.print(type);
+      io.out.print(" form ");
+      io.out.print(findx++);
+      io.out.println(":");
+
+      MForm form = fiter.next();
+      io.out.print("    Name: ");
+      io.out.println(form.getName());
+
+      List<MInput<?>> inputs = form.getInputs();
+      Iterator<MInput<?>> iiter = inputs.iterator();
+      int iindx = 1;
+      while (iiter.hasNext()) {
+        io.out.print("    Input ");
+        io.out.print(iindx++);
+        io.out.println(":");
+
+        MInput<?> input = iiter.next();
+        io.out.print("      Name: ");
+        io.out.println(input.getName());
+        io.out.print("      Type: ");
+        io.out.println(input.getType());
+        if (input.getType() == MInputType.STRING) {
+          io.out.print("      Mask: ");
+          io.out.println(((MStringInput)input).isMasked());
+          io.out.print("      Size: ");
+          io.out.println(((MStringInput)input).getMaxLength());
+        }
+      }
+    }
+  }
 }
\ No newline at end of file

Modified: sqoop/branches/sqoop2/client/src/main/java/org/apache/sqoop/client/shell/ShowServerFunction.java
URL: http://svn.apache.org/viewvc/sqoop/branches/sqoop2/client/src/main/java/org/apache/sqoop/client/shell/ShowServerFunction.java?rev=1360620&r1=1360619&r2=1360620&view=diff
==============================================================================
--- sqoop/branches/sqoop2/client/src/main/java/org/apache/sqoop/client/shell/ShowServerFunction.java
(original)
+++ sqoop/branches/sqoop2/client/src/main/java/org/apache/sqoop/client/shell/ShowServerFunction.java
Thu Jul 12 10:34:34 2012
@@ -94,17 +94,17 @@ public class ShowServerFunction extends 
   private void showServer(boolean host, boolean port, boolean webapp,
       boolean version) {
     if (host) {
-      io.out.print("@|bold Server host:|@ ");
+      io.out.print("@|bold Server host:|@");
       io.out.println(Environment.getServerHost());
     }
 
     if (port) {
-      io.out.print("@|bold Server port:|@ ");
+      io.out.print("@|bold Server port:|@");
       io.out.println(Environment.getServerPort());
     }
 
     if (webapp) {
-      io.out.print("@|bold Server webapp:|@ ");
+      io.out.print("@|bold Server webapp:|@");
       io.out.println(Environment.getServerWebapp());
     }
 

Modified: sqoop/branches/sqoop2/common/src/main/java/org/apache/sqoop/json/ConnectorBean.java
URL: http://svn.apache.org/viewvc/sqoop/branches/sqoop2/common/src/main/java/org/apache/sqoop/json/ConnectorBean.java?rev=1360620&r1=1360619&r2=1360620&view=diff
==============================================================================
--- sqoop/branches/sqoop2/common/src/main/java/org/apache/sqoop/json/ConnectorBean.java (original)
+++ sqoop/branches/sqoop2/common/src/main/java/org/apache/sqoop/json/ConnectorBean.java Thu
Jul 12 10:34:34 2012
@@ -17,87 +17,175 @@
  */
 package org.apache.sqoop.json;
 
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.sqoop.model.MConnector;
+import org.apache.sqoop.model.MForm;
+import org.apache.sqoop.model.MFormType;
+import org.apache.sqoop.model.MInput;
+import org.apache.sqoop.model.MInputType;
+import org.apache.sqoop.model.MMapInput;
+import org.apache.sqoop.model.MStringInput;
 import org.json.simple.JSONArray;
 import org.json.simple.JSONObject;
 
 public class ConnectorBean implements JsonBean {
 
-  public static final String IDS = "ids";
-  public static final String NAMES = "names";
-  public static final String CLASSES = "classes";
-
-  private long[] ids;
-  private String[] names;
-  private String[] classes;
+  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 FORM_NAME = "name";
+  public static final String FORM_TYPE = "type";
+  public static final String FORM_INPUTS = "inputs";
+  public static final String FORM_INPUT_NAME = "name";
+  public static final String FORM_INPUT_TYPE = "type";
+  public static final String FORM_INPUT_MASK = "mask";
+  public static final String FORM_INPUT_SIZE = "size";
+
+  private MConnector[] connectors;
 
   // for "extract"
-  public ConnectorBean(long[] ids, String[] names, String[] classes) {
-    this.ids = new long[ids.length];
-    System.arraycopy(ids, 0, this.ids, 0, ids.length);
-    this.names = new String[names.length];
-    System.arraycopy(names, 0, this.names, 0, names.length);
-    this.classes = new String[classes.length];
-    System.arraycopy(classes, 0, this.classes, 0, classes.length);
+  public ConnectorBean(MConnector[] connectors) {
+    this.connectors = new MConnector[connectors.length];
+    System.arraycopy(connectors, 0, this.connectors, 0, connectors.length);
   }
 
   // for "restore"
   public ConnectorBean() {
   }
 
+  public MConnector[] getConnectos() {
+    return connectors;
+  }
+
   @SuppressWarnings("unchecked")
   @Override
   public JSONObject extract() {
-    JSONObject result = new JSONObject();
-    JSONArray idsArray = new JSONArray();
-    for (long id : ids) {
-      idsArray.add(id);
-    }
-    result.put(IDS, idsArray);
-    JSONArray namesArray = new JSONArray();
-    for (String name : names) {
-      namesArray.add(name);
-    }
-    result.put(NAMES, namesArray);
-    JSONArray classesArray = new JSONArray();
-    for (String clz : classes) {
-      classesArray.add(clz);
+    JSONArray idArray = new JSONArray();
+    JSONArray nameArray = new JSONArray();
+    JSONArray classArray = new JSONArray();
+    JSONArray conFormsArray = new JSONArray();
+    JSONArray jobFormsArray = new JSONArray();
+
+    for (MConnector connector : connectors) {
+      idArray.add(connector.getPersistenceId());
+      nameArray.add(connector.getUniqueName());
+      classArray.add(connector.getClassName());
+      conFormsArray.add(extractForms(connector.getConnectionForms()));
+      jobFormsArray.add(extractForms(connector.getJobForms()));
     }
-    result.put(CLASSES, classesArray);
+
+    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);
     return result;
   }
 
-  @Override
-  public void restore(JSONObject jsonObject) {
-    JSONArray idsArray = (JSONArray) jsonObject.get(IDS);
-    int idsSize = idsArray.size();
-    ids = new long[idsSize];
-    for (int i = 0; i<idsSize; i++) {
-      ids[i] = (Long) idsArray.get(i);
-    }
-    JSONArray namesArray = (JSONArray) jsonObject.get(NAMES);
-    int namesSize = namesArray.size();
-    names = new String[namesSize];
-    for (int i = 0; i<namesSize; i++) {
-      names[i] = (String) namesArray.get(i);
-    }
-    JSONArray classesArray = (JSONArray) jsonObject.get(CLASSES);
-    int classeSize = classesArray.size();
-    classes = new String[classeSize];
-    for (int i = 0; i<classeSize; i++) {
-      classes[i] = (String) classesArray.get(i);
+  @SuppressWarnings("unchecked")
+  private JSONArray extractForms(List<MForm> mForms) {
+    JSONArray forms = new JSONArray();
+
+    for (MForm mForm : mForms) {
+      forms.add(extractForm(mForm));
     }
-  }
 
-  public long[] getIds() {
-    return this.ids;
+    return forms;
   }
 
-  public String[] getNames() {
-    return this.names;
+  @SuppressWarnings("unchecked")
+  private JSONObject extractForm(MForm mForm) {
+    JSONObject form = new JSONObject();
+    form.put(FORM_NAME, mForm.getName());
+    form.put(FORM_TYPE, MFormType.CONNECTION.toString());
+    JSONArray mInputs = new JSONArray();
+    form.put(FORM_INPUTS, mInputs);
+
+    for (MInput<?> mInput : mForm.getInputs()) {
+      JSONObject input = new JSONObject();
+      mInputs.add(input);
+
+      input.put(FORM_INPUT_NAME, mInput.getName());
+      input.put(FORM_INPUT_TYPE, mInput.getType().toString());
+      if (mInput.getType() == MInputType.STRING) {
+        input.put(FORM_INPUT_MASK,
+            ((MStringInput)mInput).isMasked());
+        input.put(FORM_INPUT_SIZE,
+            ((MStringInput)mInput).getMaxLength());
+      }
+    }
+
+    return form;
   }
 
-  public String[] getClasses() {
-    return this.classes;
+  @Override
+  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 MConnector[idArray.size()];
+    for (int i = 0; i < connectors.length; i++) {
+      long persistenceId = (Long) idArray.get(i);
+      String uniqueName = (String) nameArray.get(i);
+      String className = (String) classArray.get(i);
+
+      List<MForm> conMForms = restoreForms((JSONArray) conFormsArray.get(i));
+      List<MForm> jobMForms = restoreForms((JSONArray) jobFormsArray.get(i));
+      MConnector connector = new MConnector(uniqueName, className,
+          conMForms, jobMForms);
+      connector.setPersistenceId(persistenceId);
+      connectors[i] = connector;
+    }
   }
 
+  private List<MForm> restoreForms(JSONArray forms) {
+    List<MForm> mForms = new ArrayList<MForm>();
+
+    for (int i = 0; i < forms.size(); i++) {
+      mForms.add(restoreForm((JSONObject) forms.get(i)));
+    }
+
+    return mForms;
+  }
+
+  private MForm restoreForm(JSONObject form) {
+    JSONArray inputs = (JSONArray) form.get(FORM_INPUTS);
+
+    List<MInput<?>> mInputs = new ArrayList<MInput<?>>();
+    for (int i = 0; i < inputs.size(); i++) {
+      JSONObject input = (JSONObject) inputs.get(i);
+      MInputType type =
+          MInputType.valueOf((String) input.get(FORM_INPUT_TYPE));
+      switch (type) {
+        case STRING: {
+          String name = (String) input.get(FORM_INPUT_NAME);
+          boolean mask = (Boolean) input.get(FORM_INPUT_MASK);
+          long size = (Long) input.get(FORM_INPUT_SIZE);
+          MInput<String> mInput = new MStringInput(name, mask, (short)size);
+          mInputs.add(mInput);
+          break;
+        }
+        case MAP: {
+          String name = (String) input.get(FORM_INPUT_NAME);
+          MInput<Map<String, String>> mInput = new MMapInput(name);
+          mInputs.add(mInput);
+          break;
+        }
+      }
+    }
+
+    return new MForm((String) form.get(FORM_NAME), mInputs);
+  }
 }

Modified: sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorError.java
URL: http://svn.apache.org/viewvc/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorError.java?rev=1360620&r1=1360619&r2=1360620&view=diff
==============================================================================
--- sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorError.java
(original)
+++ sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorError.java
Thu Jul 12 10:34:34 2012
@@ -58,8 +58,8 @@ public enum ConnectorError implements Er
       + "previously registered connector; or the connector metadata has "
       + "changed since it was registered previously."),
 
-  /** A connector is found with an invalid id. */
-  CONN_0010("A connector is found with an invalid id");
+  /** A connector is not assigned with a valid id yet. */
+  CONN_0010("A connector is not assigned with a valid id yet");
 
   private final String message;
 

Modified: sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorManager.java
URL: http://svn.apache.org/viewvc/sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorManager.java?rev=1360620&r1=1360619&r2=1360620&view=diff
==============================================================================
--- sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorManager.java
(original)
+++ sqoop/branches/sqoop2/core/src/main/java/org/apache/sqoop/connector/ConnectorManager.java
Thu Jul 12 10:34:34 2012
@@ -44,12 +44,18 @@ public class ConnectorManager {
   private static Map<String, ConnectorHandler> handlerMap =
       new HashMap<String, ConnectorHandler>();
 
-  public static ConnectorHandler[] getHandlers() {
-    return handlerMap.values().toArray(new ConnectorHandler[]{});
+  public static MConnector[] getConnectors() {
+    MConnector[] connectors = new MConnector[handlerMap.size()];
+    int indx = 0;
+    for (ConnectorHandler handler : handlerMap.values()) {
+      connectors[indx++] = handler.getMetadata();
+    }
+    return connectors;
   }
 
-  public static ConnectorHandler getHandler(long connectorId) {
-    return handlerMap.get(nameMap.get(connectorId));
+  public static MConnector getConnector(long connectorId) {
+    ConnectorHandler handler = handlerMap.get(nameMap.get(connectorId));
+    return handler.getMetadata();
   }
 
   public static synchronized void initialize() {

Modified: sqoop/branches/sqoop2/server/src/main/java/org/apache/sqoop/handler/ConnectorRequestHandler.java
URL: http://svn.apache.org/viewvc/sqoop/branches/sqoop2/server/src/main/java/org/apache/sqoop/handler/ConnectorRequestHandler.java?rev=1360620&r1=1360619&r2=1360620&view=diff
==============================================================================
--- sqoop/branches/sqoop2/server/src/main/java/org/apache/sqoop/handler/ConnectorRequestHandler.java
(original)
+++ sqoop/branches/sqoop2/server/src/main/java/org/apache/sqoop/handler/ConnectorRequestHandler.java
Thu Jul 12 10:34:34 2012
@@ -23,6 +23,7 @@ import org.apache.sqoop.connector.Connec
 import org.apache.sqoop.connector.ConnectorManager;
 import org.apache.sqoop.json.JsonBean;
 import org.apache.sqoop.json.ConnectorBean;
+import org.apache.sqoop.model.MConnector;
 import org.apache.sqoop.server.RequestContext;
 import org.apache.sqoop.server.RequestHandler;
 
@@ -34,9 +35,6 @@ public class ConnectorRequestHandler imp
   /** The API version supported by this server */
   public static final String PROTOCOL_V1 = "1";
 
-
-  private ConnectorBean connectors;
-
   public ConnectorRequestHandler() {
     LOG.info("ConnectorRequestHandler initialized");
   }
@@ -44,7 +42,7 @@ public class ConnectorRequestHandler imp
 
   @Override
   public JsonBean handleEvent(RequestContext ctx) throws SqoopException {
-    ConnectorBean connectorBean;
+    MConnector[] connectors;
 
     String uri = ctx.getRequest().getRequestURI();
     int slash = uri.lastIndexOf("/");
@@ -52,30 +50,14 @@ public class ConnectorRequestHandler imp
     LOG.info("ConnectorRequestHandler handles cid: " + cid);
     if (cid.equals("all")) {
       // display all connectors
-      if (connectors == null) {
-        ConnectorHandler[] handlers = ConnectorManager.getHandlers();
-        long[] ids = new long[handlers.length];
-        String[] names = new String[handlers.length];
-        String[] classes = new String[handlers.length];
-        for (int i = 0; i < handlers.length; i++) {
-          ids[i] = handlers[i].getMetadata().getPersistenceId();
-          names[i] = handlers[i].getUniqueName();
-          classes[i] = handlers[i].getConnectorClassName();
-        }
-        connectors = new ConnectorBean(ids, names, classes);
-      }
-      connectorBean = connectors;
+      connectors = ConnectorManager.getConnectors();
 
     } else {
       // display one connector
-      ConnectorHandler handler =
-          ConnectorManager.getHandler(Long.parseLong(cid));
-      long[] ids = new long[] { handler.getMetadata().getPersistenceId() };
-      String[] names = new String[] { handler.getUniqueName() };
-      String[] classes = new String[] { handler.getConnectorClassName() };
-      connectorBean = new ConnectorBean(ids, names, classes);
+      connectors = new MConnector[] {
+          ConnectorManager.getConnector(Long.parseLong(cid)) };
     }
 
-    return connectorBean;
+    return new ConnectorBean(connectors);
   }
 }



Mime
View raw message