LENS-1205: Fix ToXMLString class to serialise and deserialise xsd generated classes
Project: http://git-wip-us.apache.org/repos/asf/lens/repo Commit: http://git-wip-us.apache.org/repos/asf/lens/commit/a33cc53b Tree: http://git-wip-us.apache.org/repos/asf/lens/tree/a33cc53b Diff: http://git-wip-us.apache.org/repos/asf/lens/diff/a33cc53b Branch: refs/heads/current-release-line Commit: a33cc53b38d559ba743b3d9664735698e22623c3 Parents: 5f79cc6 Author: Lavkesh Lahngir <[email protected]> Authored: Wed Jun 29 13:26:00 2016 +0530 Committer: Rajat Khandelwal <[email protected]> Committed: Wed Jun 29 13:26:00 2016 +0530 ---------------------------------------------------------------------- .../org/apache/lens/api/LensSessionHandle.java | 6 +-- .../java/org/apache/lens/api/ToXMLString.java | 39 +++++++++++--------- ...apache.lens.api.query.SchedulerJobHandle.xml | 24 ------------ ...pache.lens.api.query.SchedulerJobHandle.yaml | 16 -------- .../lens/server/scheduler/SchedulerDAO.java | 32 +++++++++------- 5 files changed, 42 insertions(+), 75 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lens/blob/a33cc53b/lens-api/src/main/java/org/apache/lens/api/LensSessionHandle.java ---------------------------------------------------------------------- diff --git a/lens-api/src/main/java/org/apache/lens/api/LensSessionHandle.java b/lens-api/src/main/java/org/apache/lens/api/LensSessionHandle.java index 74725e3..ed7eb19 100644 --- a/lens-api/src/main/java/org/apache/lens/api/LensSessionHandle.java +++ b/lens-api/src/main/java/org/apache/lens/api/LensSessionHandle.java @@ -27,10 +27,7 @@ import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; +import lombok.*; /** * The Class LensSessionHandle. @@ -49,6 +46,7 @@ import lombok.NoArgsConstructor; * Instantiates a new lens session handle. */ @NoArgsConstructor(access = AccessLevel.PROTECTED) +@EqualsAndHashCode public class LensSessionHandle extends ToXMLString { /** http://git-wip-us.apache.org/repos/asf/lens/blob/a33cc53b/lens-api/src/main/java/org/apache/lens/api/ToXMLString.java ---------------------------------------------------------------------- diff --git a/lens-api/src/main/java/org/apache/lens/api/ToXMLString.java b/lens-api/src/main/java/org/apache/lens/api/ToXMLString.java index fca56a8..e74adc9 100644 --- a/lens-api/src/main/java/org/apache/lens/api/ToXMLString.java +++ b/lens-api/src/main/java/org/apache/lens/api/ToXMLString.java @@ -6,9 +6,9 @@ * 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 - * + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> * 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 @@ -18,32 +18,28 @@ */ package org.apache.lens.api; - import java.io.StringReader; import java.io.StringWriter; import java.util.HashMap; import java.util.Map; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; -import javax.xml.bind.Unmarshaller; +import javax.xml.bind.*; import org.apache.lens.api.jaxb.LensJAXBContext; public abstract class ToXMLString { protected static final Map<Class<?>, JAXBContext> JAXB_CONTEXTS = new HashMap<>(); - - @Override - public String toString() { - return toString(this); - } - public static String toString(Object o) { try { StringWriter stringWriter = new StringWriter(); - Marshaller marshaller = getLensJAXBContext(o.getClass()).createMarshaller(); + Class cl = null; + if (o instanceof JAXBElement) { + cl = ((JAXBElement) o).getDeclaredType(); + } else { + cl = o.getClass(); + } + Marshaller marshaller = getLensJAXBContext(cl).createMarshaller(); marshaller.marshal(o, stringWriter); return stringWriter.toString(); } catch (JAXBException e) { @@ -62,12 +58,21 @@ public abstract class ToXMLString { return JAXB_CONTEXTS.get(clazz); } - public static <T> T valueOf(String sessionStr, Class<T> tClass) { + public static <T> T valueOf(String sessionStr, Class tClass) { try { Unmarshaller unmarshaller = getLensJAXBContext(tClass).createUnmarshaller(); - return (T) unmarshaller.unmarshal(new StringReader(sessionStr)); + Object ret = unmarshaller.unmarshal(new StringReader(sessionStr)); + if (ret instanceof JAXBElement) { + return ((JAXBElement<T>) ret).getValue(); + } + return (T) ret; } catch (JAXBException e) { return null; } } + + @Override + public String toString() { + return toString(this); + } } http://git-wip-us.apache.org/repos/asf/lens/blob/a33cc53b/lens-api/src/test/resources/toString/org.apache.lens.api.query.SchedulerJobHandle.xml ---------------------------------------------------------------------- diff --git a/lens-api/src/test/resources/toString/org.apache.lens.api.query.SchedulerJobHandle.xml b/lens-api/src/test/resources/toString/org.apache.lens.api.query.SchedulerJobHandle.xml deleted file mode 100644 index ef7d0c7..0000000 --- a/lens-api/src/test/resources/toString/org.apache.lens.api.query.SchedulerJobHandle.xml +++ /dev/null @@ -1,24 +0,0 @@ -<?xml version="1.0" encoding="UTF-8" standalone="yes"?> -<!-- - - 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. - ---> -<schedulerJobHandle> - <handleId>e554a2a4-b19a-4df9-9015-323d0f145d7f</handleId> -</schedulerJobHandle> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lens/blob/a33cc53b/lens-api/src/test/resources/toString/org.apache.lens.api.query.SchedulerJobHandle.yaml ---------------------------------------------------------------------- diff --git a/lens-api/src/test/resources/toString/org.apache.lens.api.query.SchedulerJobHandle.yaml b/lens-api/src/test/resources/toString/org.apache.lens.api.query.SchedulerJobHandle.yaml deleted file mode 100644 index 567d8cf..0000000 --- a/lens-api/src/test/resources/toString/org.apache.lens.api.query.SchedulerJobHandle.yaml +++ /dev/null @@ -1,16 +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. -e554a2a4-b19a-4df9-9015-323d0f145d7f \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lens/blob/a33cc53b/lens-server/src/main/java/org/apache/lens/server/scheduler/SchedulerDAO.java ---------------------------------------------------------------------- diff --git a/lens-server/src/main/java/org/apache/lens/server/scheduler/SchedulerDAO.java b/lens-server/src/main/java/org/apache/lens/server/scheduler/SchedulerDAO.java index e866eb3..8179fef 100644 --- a/lens-server/src/main/java/org/apache/lens/server/scheduler/SchedulerDAO.java +++ b/lens-server/src/main/java/org/apache/lens/server/scheduler/SchedulerDAO.java @@ -24,6 +24,8 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import javax.xml.bind.JAXBElement; + import org.apache.lens.api.LensSessionHandle; import org.apache.lens.api.ToXMLString; import org.apache.lens.api.scheduler.*; @@ -63,7 +65,7 @@ public class SchedulerDAO { throw new LensException("No class found ", e); } catch (InstantiationException | IllegalAccessException e) { log.error("Illegal access exception", e); - throw new LensException("Illegal access exceptio ", e); + throw new LensException("Illegal access exception ", e); } } @@ -221,10 +223,10 @@ public class SchedulerDAO { * @param endTime : Created on should be less than the end time. * @return List of Job handles */ - public List<SchedulerJobHandle> getJobs(String username, SchedulerJobInstanceState state, Long startTime, + public List<SchedulerJobHandle> getJobs(String username, SchedulerJobState state, Long startTime, Long endTime) { try { - return store.getJobs(username, state.name(), startTime, endTime); + return store.getJobs(username, state == null ? null : state.name(), startTime, endTime); } catch (SQLException e) { log.error("Error while getting jobs ", e); return null; @@ -247,6 +249,7 @@ public class SchedulerDAO { protected static final String COLUMN_RESULT_PATH = "resultpath"; protected static final String COLUMN_QUERY = "query"; protected QueryRunner runner; + protected ObjectFactory jobFactory = new ObjectFactory(); // Generic multiple row handler for the fetch query. private ResultSetHandler<List<Object[]>> multipleRowsHandler = new ResultSetHandler<List<Object[]>>() { @Override @@ -297,9 +300,9 @@ public class SchedulerDAO { */ public int insertIntoJobTable(SchedulerJobInfo jobInfo) throws SQLException { String insertSQL = "INSERT INTO " + JOB_TABLE + " VALUES(?,?,?,?,?,?)"; - return runner - .update(insertSQL, jobInfo.getId().toString(), ToXMLString.toString(jobInfo.getJob()), jobInfo.getUserName(), - jobInfo.getState().name(), jobInfo.getCreatedOn(), jobInfo.getModifiedOn()); + JAXBElement<XJob> xmlJob = jobFactory.createJob(jobInfo.getJob()); + return runner.update(insertSQL, jobInfo.getId().toString(), ToXMLString.toString(xmlJob), jobInfo.getUserName(), + jobInfo.getState().name(), jobInfo.getCreatedOn(), jobInfo.getModifiedOn()); } /** @@ -333,7 +336,7 @@ public class SchedulerDAO { } else { Object[] jobInfo = result.get(0); SchedulerJobHandle id = SchedulerJobHandle.fromString((String) jobInfo[0]); - XJob xJob = ToXMLString.valueOf((String) jobInfo[1], XJob.class); + XJob xJob = ToXMLString.valueOf((String) jobInfo[1], ObjectFactory.class); String userName = (String) jobInfo[2]; String state = (String) jobInfo[3]; long createdOn = (Long) jobInfo[4]; @@ -355,7 +358,7 @@ public class SchedulerDAO { if (result.size() == 0) { return null; } else { - return ToXMLString.valueOf((String) result.get(0)[0], XJob.class); + return ToXMLString.valueOf((String) result.get(0)[0], ObjectFactory.class); } } @@ -390,19 +393,19 @@ public class SchedulerDAO { throws SQLException { String whereClause = ""; if (username != null && !username.isEmpty()) { - whereClause += (whereClause.isEmpty()) ? " WHERE " : " AND " + COLUMN_USER + "=?"; + whereClause += ((whereClause.isEmpty()) ? " WHERE " : " AND ") + COLUMN_USER + " = '" + username+"'"; } if (state != null && !state.isEmpty()) { - whereClause += (whereClause.isEmpty()) ? " WHERE " : " AND " + COLUMN_STATE + "=?"; + whereClause += ((whereClause.isEmpty()) ? " WHERE " : " AND ") + COLUMN_STATE + " = '" + state + "'"; } if (starttime != null && starttime > 0) { - whereClause += (whereClause.isEmpty()) ? " WHERE " : " AND " + COLUMN_CREATED_ON + ">=?"; + whereClause += ((whereClause.isEmpty()) ? " WHERE " : " AND ") + COLUMN_CREATED_ON + " >= " + starttime; } if (endtime != null && endtime > 0) { - whereClause += (whereClause.isEmpty()) ? " WHERE " : " AND " + COLUMN_CREATED_ON + "< ?"; + whereClause += ((whereClause.isEmpty()) ? " WHERE " : " AND ") + COLUMN_CREATED_ON + " < " + endtime; } String fetchSQL = "SELECT " + COLUMN_ID + " FROM " + JOB_TABLE + whereClause; - List<Object[]> result = runner.query(fetchSQL, multipleRowsHandler, username, state, starttime, endtime); + List<Object[]> result = runner.query(fetchSQL, multipleRowsHandler); List<SchedulerJobHandle> resOut = new ArrayList<>(); for (int i = 0; i < result.size(); i++) { Object[] row = result.get(i); @@ -424,7 +427,8 @@ public class SchedulerDAO { String updateSQL = "UPDATE " + JOB_TABLE + " SET " + COLUMN_JOB + "=?, " + COLUMN_MODIFIED_ON + "=? " + " WHERE " + COLUMN_ID + "=?"; - return runner.update(updateSQL, ToXMLString.toString(job), modifiedOn, id); + JAXBElement<XJob> xmlJob = jobFactory.createJob(job); + return runner.update(updateSQL, ToXMLString.toString(xmlJob), modifiedOn, id); } /**
