5
0
mirror of https://github.com/apache/sqoop.git synced 2025-05-09 13:41:08 +08:00

SQOOP-2768: Sqoop2: Remove the notion of LinksBeans

(Jarek Jarcec Cecho via Colin Ma)
This commit is contained in:
Colin Ma 2016-01-14 14:24:47 +08:00
parent 11d3973b9a
commit 7b158d51ee
9 changed files with 88 additions and 200 deletions

View File

@ -20,7 +20,6 @@
import org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticatedURL;
import org.apache.sqoop.json.JSONUtils;
import org.apache.sqoop.json.LinkBean;
import org.apache.sqoop.json.LinksBean;
import org.apache.sqoop.json.ValidationResultBean;
import org.apache.sqoop.model.MLink;
import org.apache.sqoop.utils.UrlSafeUtils;
@ -54,7 +53,7 @@ public LinkBean read(String serverUrl, String linkArg) {
}
JSONObject jsonObject = JSONUtils.parse(response);
// defaults to all
LinkBean bean = new LinksBean();
LinkBean bean = new LinkBean();
if (linkArg != null) {
bean = new LinkBean();
}

View File

@ -49,7 +49,7 @@ public class LinkBean implements JsonBean {
static final String CONNECTOR_NAME = "connector-name";
static final String LINK_CONFIG_VALUES = "link-config-values";
static final String LINK = "link";
static final String LINKS = "links";
// Required
@ -62,10 +62,9 @@ public class LinkBean implements JsonBean {
// For "extract"
public LinkBean(MLink link) {
this();
this.links = new ArrayList<MLink>();
this.links = new ArrayList<>();
this.links.add(link);
}
public LinkBean(List<MLink> links) {
this();
this.links = links;
@ -73,7 +72,7 @@ public LinkBean(List<MLink> links) {
// For "restore"
public LinkBean() {
linkConfigBundles = new HashMap<String, ResourceBundle>();
linkConfigBundles = new HashMap<>();
}
public void addConnectorConfigBundle(String connectorName, ResourceBundle connectorConfigBundle) {
@ -95,9 +94,16 @@ public ResourceBundle getConnectorConfigBundle(String connectorName) {
@SuppressWarnings("unchecked")
@Override
public JSONObject extract(boolean skipSensitive) {
JSONObject link = new JSONObject();
link.put(LINK, extractLink(skipSensitive, links.get(0)));
return link;
JSONArray linkArray = extractLinks(skipSensitive);
JSONObject links = new JSONObject();
links.put(LINKS, linkArray);
return links;
}
@Override
public void restore(JSONObject jsonObject) {
JSONArray array = JSONUtils.getJSONArray(jsonObject, LINKS);
restoreLinks(array);
}
@SuppressWarnings("unchecked")
@ -125,15 +131,8 @@ private JSONObject extractLink(boolean skipSensitive, MLink link) {
return linkJsonObject;
}
@Override
public void restore(JSONObject jsonObject) {
links = new ArrayList<MLink>();
JSONObject obj = JSONUtils.getJSONObject(jsonObject, LINK);
links.add(restoreLink(obj));
}
protected void restoreLinks(JSONArray array) {
links = new ArrayList<MLink>();
links = new ArrayList<>();
for (Object obj : array) {
links.add(restoreLink(obj));
}

View File

@ -1,61 +0,0 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.sqoop.json;
import java.util.List;
import org.apache.sqoop.classification.InterfaceAudience;
import org.apache.sqoop.classification.InterfaceStability;
import org.apache.sqoop.model.MLink;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
@InterfaceAudience.Private
@InterfaceStability.Unstable
public class LinksBean extends LinkBean {
static final String LINKS = "links";
public LinksBean(MLink link) {
super(link);
}
public LinksBean(List<MLink> links) {
super(links);
}
// For "restore"
public LinksBean() {
}
@SuppressWarnings("unchecked")
@Override
public JSONObject extract(boolean skipSensitive) {
JSONArray linkArray = extractLinks(skipSensitive);
JSONObject links = new JSONObject();
links.put(LINKS, linkArray);
return links;
}
@Override
public void restore(JSONObject jsonObject) {
JSONArray array = JSONUtils.getJSONArray(jsonObject, LINKS);
super.restoreLinks(array);
}
}

View File

@ -22,6 +22,7 @@
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@ -52,7 +53,7 @@ public void testLinkSerialization() {
JSONObject json = linkBean.extract(false);
// Check for sensitivity
JSONObject linkObj = (JSONObject) json.get(LinkBean.LINK);
JSONObject linkObj = (JSONObject)((JSONArray) json.get(LinkBean.LINKS)).get(0);
JSONObject linkConfigList = (JSONObject) linkObj.get(LinkBean.LINK_CONFIG_VALUES);
JSONArray linkConfigs = (JSONArray) linkConfigList.get(ConfigInputConstants.CONFIGS);
List<MValidator> linkValidators = restoreValidator((JSONArray)
@ -108,7 +109,7 @@ public void testSensitivityFilter() {
JSONObject jsonFiltered = bean.extract(true);
// Sensitive values should exist
JSONObject linkObj = (JSONObject) json.get(LinkBean.LINK);
JSONObject linkObj = (JSONObject)((JSONArray) json.get(LinkBean.LINKS)).get(0);
JSONObject linkConfigList = (JSONObject) linkObj.get(LinkBean.LINK_CONFIG_VALUES);
JSONArray linkConfigs = (JSONArray) linkConfigList.get(ConfigInputConstants.CONFIGS);
JSONObject linkConfigObj = (JSONObject) linkConfigs.get(0);
@ -119,7 +120,7 @@ public void testSensitivityFilter() {
assertTrue(password.containsKey(ConfigInputConstants.CONFIG_INPUT_VALUE));
// Sensitive values should not exist
linkObj = (JSONObject) jsonFiltered.get(LinkBean.LINK);
linkObj = (JSONObject)((JSONArray) jsonFiltered.get(LinkBean.LINKS)).get(0);
linkConfigList = (JSONObject) linkObj.get(LinkBean.LINK_CONFIG_VALUES);
linkConfigs = (JSONArray) linkConfigList.get(ConfigInputConstants.CONFIGS);
linkConfigObj = (JSONObject) linkConfigs.get(0);
@ -129,4 +130,65 @@ public void testSensitivityFilter() {
password = (JSONObject) inputs.get(2);
assertFalse(password.containsKey(ConfigInputConstants.CONFIG_INPUT_VALUE));
}
@Test
public void testLinksSerialization() {
Date created = new Date();
Date updated = new Date();
MLink link1 = BeanTestUtil.createLink("ahoj", "link1", 666L, created, updated);
MLink link2 = BeanTestUtil.createLink("jhoa", "link2", 888L, created, updated);
List<MLink> links = new ArrayList<>();
links.add(link1);
links.add(link2);
// Fill some data at the beginning
MStringInput input = (MStringInput) link1.getConnectorLinkConfig().getConfigs().get(0)
.getInputs().get(0);
input.setValue("Hi there!");
// Serialize it to JSON object
LinkBean linkBean = new LinkBean(links);
JSONObject json = linkBean.extract(false);
// Check for sensitivity
JSONArray linksObj = (JSONArray) json.get(LinkBean.LINKS);
JSONObject linkObj = (JSONObject) linksObj.get(0);
JSONObject linkConfigList = (JSONObject) linkObj.get(LinkBean.LINK_CONFIG_VALUES);
JSONArray linkConfigs = (JSONArray) linkConfigList.get(ConfigInputConstants.CONFIGS);
JSONObject linkConfig = (JSONObject) linkConfigs.get(0);
JSONArray inputs = (JSONArray) linkConfig.get(ConfigInputConstants.CONFIG_INPUTS);
for (Object inp : inputs) {
assertTrue(((JSONObject) inp).containsKey(ConfigInputConstants.CONFIG_INPUT_SENSITIVE));
}
// "Move" it across network in text form
String linkJsonString = json.toJSONString();
// Retrieved transferred object
JSONObject parsedLinkJson = JSONUtils.parse(linkJsonString);
LinkBean retrievedBean = new LinkBean();
retrievedBean.restore(parsedLinkJson);
MLink targetLink1 = retrievedBean.getLinks().get(0);
MLink targetLink2 = retrievedBean.getLinks().get(1);
// Check id and name
assertEquals(666L, targetLink1.getPersistenceId());
assertEquals(888L, targetLink2.getPersistenceId());
assertEquals("link1", targetLink1.getName());
assertEquals("link2", targetLink2.getName());
assertEquals("admin", targetLink1.getCreationUser());
assertEquals(created, targetLink1.getCreationDate());
assertEquals("user", targetLink1.getLastUpdateUser());
assertEquals(updated, targetLink1.getLastUpdateDate());
assertEquals(false, targetLink1.getEnabled());
// Test that value was correctly moved
MStringInput targetInput = (MStringInput) targetLink1.getConnectorLinkConfig().getConfigs()
.get(0).getInputs().get(0);
assertEquals("Hi there!", targetInput.getValue());
}
}

View File

@ -1,98 +0,0 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.sqoop.json;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.sqoop.json.util.BeanTestUtil;
import org.apache.sqoop.json.util.ConfigInputConstants;
import org.apache.sqoop.model.MLink;
import org.apache.sqoop.model.MStringInput;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.testng.annotations.Test;
public class TestLinksBean {
@Test
public void testLinksSerialization() {
Date created = new Date();
Date updated = new Date();
MLink link1 = BeanTestUtil.createLink("ahoj", "link1", 666L, created, updated);
MLink link2 = BeanTestUtil.createLink("jhoa", "link2", 888L, created, updated);
List<MLink> links = new ArrayList<MLink>();
links.add(link1);
links.add(link2);
// Fill some data at the beginning
MStringInput input = (MStringInput) link1.getConnectorLinkConfig().getConfigs().get(0)
.getInputs().get(0);
input.setValue("Hi there!");
// Serialize it to JSON object
LinksBean linkBean = new LinksBean(links);
JSONObject json = linkBean.extract(false);
// Check for sensitivity
JSONArray linksObj = (JSONArray) json.get(LinksBean.LINKS);
JSONObject linkObj = (JSONObject) linksObj.get(0);
JSONObject linkConfigList = (JSONObject) linkObj.get(LinkBean.LINK_CONFIG_VALUES);
JSONArray linkConfigs = (JSONArray) linkConfigList.get(ConfigInputConstants.CONFIGS);
JSONObject linkConfig = (JSONObject) linkConfigs.get(0);
JSONArray inputs = (JSONArray) linkConfig.get(ConfigInputConstants.CONFIG_INPUTS);
for (Object inp : inputs) {
assertTrue(((JSONObject) inp).containsKey(ConfigInputConstants.CONFIG_INPUT_SENSITIVE));
}
// "Move" it across network in text form
String linkJsonString = json.toJSONString();
// Retrieved transferred object
JSONObject parsedLinkJson = JSONUtils.parse(linkJsonString);
LinksBean retrievedBean = new LinksBean();
retrievedBean.restore(parsedLinkJson);
MLink targetLink1 = retrievedBean.getLinks().get(0);
MLink targetLink2 = retrievedBean.getLinks().get(1);
// Check id and name
assertEquals(666L, targetLink1.getPersistenceId());
assertEquals(888L, targetLink2.getPersistenceId());
assertEquals("link1", targetLink1.getName());
assertEquals("link2", targetLink2.getName());
assertEquals("admin", targetLink1.getCreationUser());
assertEquals(created, targetLink1.getCreationDate());
assertEquals("user", targetLink1.getLastUpdateUser());
assertEquals(updated, targetLink1.getLastUpdateDate());
assertEquals(false, targetLink1.getEnabled());
// Test that value was correctly moved
MStringInput targetInput = (MStringInput) targetLink1.getConnectorLinkConfig().getConfigs()
.get(0).getInputs().get(0);
assertEquals("Hi there!", targetInput.getValue());
}
}

View File

@ -31,7 +31,6 @@
import org.apache.sqoop.json.JSONUtils;
import org.apache.sqoop.json.JsonBean;
import org.apache.sqoop.json.LinkBean;
import org.apache.sqoop.json.LinksBean;
import org.apache.sqoop.json.ValidationResultBean;
import org.apache.sqoop.model.*;
import org.apache.sqoop.repository.Repository;
@ -189,7 +188,6 @@ private JsonBean createUpdateLink(RequestContext ctx, boolean create) {
private JsonBean getLinks(RequestContext ctx) {
String linkName = ctx.getLastURLElement();
LinkBean linkBean;
List<MLink> links;
Locale locale = ctx.getAcceptLanguageHeader();
Repository repository = RepositoryManager.getInstance().getRepository();
@ -216,13 +214,8 @@ private JsonBean getLinks(RequestContext ctx) {
// Authorization check
links = AuthorizationEngine.filterResource(ctx.getUserName(), MResource.TYPE.LINK, links);
// Return bean entity (we have to separate what we're returning here)
if(linkName.equals("all")) {
linkBean = createLinksBean(links, locale);
} else {
linkBean = createLinkBean(links, locale);
}
return linkBean;
// And return resulting links
return createLinkBean(links, locale);
}
private LinkBean createLinkBean(List<MLink> links, Locale locale) {
@ -231,12 +224,6 @@ private LinkBean createLinkBean(List<MLink> links, Locale locale) {
return linkBean;
}
private LinksBean createLinksBean(List<MLink> links, Locale locale) {
LinksBean linksBean = new LinksBean(links);
addConnectorConfigBundle(locale, linksBean);
return linksBean;
}
private void addConnectorConfigBundle(Locale locale, LinkBean bean) {
// Add associated resources into the bean
for (MLink link : bean.getLinks()) {

View File

@ -22,7 +22,7 @@
import org.apache.sqoop.common.VersionInfo;
import org.apache.sqoop.json.JSONUtils;
import org.apache.sqoop.json.JobsBean;
import org.apache.sqoop.json.LinksBean;
import org.apache.sqoop.json.LinkBean;
import org.apache.sqoop.json.SubmissionsBean;
import org.apache.sqoop.model.*;
import org.apache.sqoop.submission.SubmissionStatus;
@ -69,7 +69,7 @@ public void testDump() throws Exception {
// verify the links
JSONObject jsonLinks = (JSONObject) json.get(JSONConstants.LINKS);
LinksBean linksBean = new LinksBean();
LinkBean linksBean = new LinkBean();
linksBean.restore(jsonLinks);
verifyLinks(linksBean.getLinks());

View File

@ -34,7 +34,7 @@
import org.apache.sqoop.common.VersionInfo;
import org.apache.sqoop.connector.ConnectorManager;
import org.apache.sqoop.json.JobsBean;
import org.apache.sqoop.json.LinksBean;
import org.apache.sqoop.json.LinkBean;
import org.apache.sqoop.json.SubmissionsBean;
import org.apache.sqoop.model.MLink;
import org.apache.sqoop.repository.Repository;
@ -107,7 +107,7 @@ private JSONObject dump(boolean skipSensitive) {
LOG.info("Dumping Links with skipSensitive=" + String.valueOf(skipSensitive));
List<MLink> links = repository.findLinks();
LinksBean linkBeans = new LinksBean(links);
LinkBean linkBeans = new LinkBean(links);
JSONObject linksJsonObject = linkBeans.extract(skipSensitive);
result.put(JSONConstants.LINKS, linksJsonObject);

View File

@ -42,7 +42,7 @@
import org.apache.sqoop.driver.DriverUpgrader;
import org.apache.sqoop.json.JSONUtils;
import org.apache.sqoop.json.JobsBean;
import org.apache.sqoop.json.LinksBean;
import org.apache.sqoop.json.LinkBean;
import org.apache.sqoop.json.SubmissionsBean;
import org.apache.sqoop.model.ConfigUtils;
import org.apache.sqoop.model.MConfig;
@ -149,7 +149,7 @@ private boolean load(JSONObject repo) {
(JSONArray) jsonLinks.get(JSONConstants.LINKS),
JSONConstants.CONNECTOR_NAME, true);
LinksBean linksBean = new LinksBean();
LinkBean linksBean = new LinkBean();
linksBean.restore(jsonLinks);
for (MLink link : linksBean.getLinks()) {