diff --git a/appserver/admingui/full/src/main/java/fish/payara/full/admingui/handlers/BatchHandlers.java b/appserver/admingui/full/src/main/java/fish/payara/full/admingui/handlers/BatchHandlers.java new file mode 100644 index 00000000000..c397974f7e7 --- /dev/null +++ b/appserver/admingui/full/src/main/java/fish/payara/full/admingui/handlers/BatchHandlers.java @@ -0,0 +1,163 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2018 Payara Foundation and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * https://github.com/payara/Payara/blob/master/LICENSE.txt + * See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at glassfish/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * The Payara Foundation designates this particular file as subject to the "Classpath" + * exception as provided by the Payara Foundation in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package fish.payara.full.admingui.handlers; + +import com.sun.jsftemplating.annotation.Handler; +import com.sun.jsftemplating.annotation.HandlerInput; +import com.sun.jsftemplating.annotation.HandlerOutput; +import com.sun.jsftemplating.layout.descriptors.handler.HandlerContext; +import org.glassfish.admingui.common.util.GuiUtil; + +/** + * + * @author Susan Rai + */ +public class BatchHandlers { + + private static final int DEFAULT_OFFSET_INCREMENT = 20; + + @Handler(id = "py.addToOffSetValue", + input = { + @HandlerInput(name = "offset", type = String.class)}, + output = { + @HandlerOutput(name = "result", type = String.class)}) + public static void addToOffSetValue(HandlerContext handlerCtx) { + + String offsetValue = (String) handlerCtx.getInputValue("offset"); + int result = 0; + + try { + result = Integer.parseInt(offsetValue) + DEFAULT_OFFSET_INCREMENT; + } catch (NumberFormatException ex) { + GuiUtil.getLogger().info("Value isn't a valid integer " + ex); + } + + handlerCtx.setOutputValue("result", result); + } + + @Handler(id = "py.subtractFromOffSetValue", + input = { + @HandlerInput(name = "offset", type = String.class)}, + output = { + @HandlerOutput(name = "result", type = String.class)}) + public static void subtractFromOffSetValue(HandlerContext handlerCtx) { + + String offsetValue = (String) handlerCtx.getInputValue("offset"); + int result = 0; + + try { + result = Integer.parseInt(offsetValue); + if (result >= DEFAULT_OFFSET_INCREMENT) { + result = result - DEFAULT_OFFSET_INCREMENT; + } else { + result = 0; + } + } catch (NumberFormatException ex) { + GuiUtil.getLogger().info("Value isn't a valid integer " + ex); + } + + handlerCtx.setOutputValue("result", result); + } + + @Handler(id = "py.getPageNumber", + input = { + @HandlerInput(name = "offset", type = String.class)}, + output = { + @HandlerOutput(name = "result", type = String.class)}) + public static void getPageNumber(HandlerContext handlerCtx) { + + String offsetValue = (String) handlerCtx.getInputValue("offset"); + int result = 0; + + try { + int offSet = Integer.parseInt(offsetValue); + result = (offSet + DEFAULT_OFFSET_INCREMENT) / DEFAULT_OFFSET_INCREMENT; + } catch (NumberFormatException ex) { + GuiUtil.getLogger().info("Value isn't a valid integer " + ex); + } + + handlerCtx.setOutputValue("result", result); + } + + @Handler(id = "py.getSpecifiedPageNumber", + input = { + @HandlerInput(name = "pageNumber", type = String.class)}, + output = { + @HandlerOutput(name = "result", type = String.class)}) + public static void getSpecifiedPageNumber(HandlerContext handlerCtx) { + + String pageNumberValue = (String) handlerCtx.getInputValue("pageNumber"); + int result = 0; + + try { + int pageNumber = Integer.parseInt(pageNumberValue); + if (pageNumber > 0) { + result = (pageNumber * DEFAULT_OFFSET_INCREMENT) - DEFAULT_OFFSET_INCREMENT; + } + } catch (NumberFormatException ex) { + GuiUtil.getLogger().info("Value isn't a valid integer " + ex); + } + + handlerCtx.setOutputValue("result", result); + } + + @Handler(id = "py.getPageCount", + input = { + @HandlerInput(name = "jobCount", type = String.class)}, + output = { + @HandlerOutput(name = "result", type = String.class)}) + public static void getPageCount(HandlerContext handlerCtx) { + + String jobCountValue = (String) handlerCtx.getInputValue("jobCount"); + int result = 1; + + try { + int jobCount = Integer.parseInt(jobCountValue); + if (jobCount > 0) { + result = (jobCount + DEFAULT_OFFSET_INCREMENT - 1) / DEFAULT_OFFSET_INCREMENT; + } + } catch (NumberFormatException ex) { + GuiUtil.getLogger().info("Value isn't a valid integer " + ex); + } + + handlerCtx.setOutputValue("result", result); + } + +} diff --git a/appserver/admingui/full/src/main/resources/batch/assets/js/batch.js b/appserver/admingui/full/src/main/resources/batch/assets/js/batch.js new file mode 100644 index 00000000000..3792dc5819b --- /dev/null +++ b/appserver/admingui/full/src/main/resources/batch/assets/js/batch.js @@ -0,0 +1,53 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2018 Payara Foundation and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * https://github.com/payara/Payara/blob/master/LICENSE.txt + * See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at glassfish/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * The Payara Foundation designates this particular file as subject to the "Classpath" + * exception as provided by the Payara Foundation in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ + + + + diff --git a/appserver/admingui/full/src/main/resources/batch/assets/skins/payara_theme/batch-styles.css b/appserver/admingui/full/src/main/resources/batch/assets/skins/payara_theme/batch-styles.css new file mode 100644 index 00000000000..13773255efd --- /dev/null +++ b/appserver/admingui/full/src/main/resources/batch/assets/skins/payara_theme/batch-styles.css @@ -0,0 +1,104 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2018 Payara Foundation and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * https://github.com/payara/Payara/blob/master/LICENSE.txt + * See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at glassfish/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * The Payara Foundation designates this particular file as subject to the "Classpath" + * exception as provided by the Payara Foundation in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +/* + Created on : Jul 13, 2018, 5:51:48 PM + Author : Susan Rai +*/ + +input[id$="previousJobsButton"], input[id$="nextJobsButton"] { + width: 100px; + height: 30px; + padding: 0px; + color:#000000; + font-weight: bold; + background-color: transparent; +} + +input:hover[id$="previousJobsButton"], +input:hover[id$="nextJobsButton"], +input:hover[id$="goButton"]{ + background-color: #cbced0 !important; +} + +span[id$=":paginationButtons"] { + background-color: #eeeeee; + border-radius: 5px; + padding: 10px; + display:block; + width : 350px; + margin: 0 0 20px 23%; +} + +input[id$="nextJobsButton"] { + float: right; +} + +input[id$="pageNumber"]{ + float: left; + margin: -25px 0 0 135px; + width: 30px; + text-align: center; +} + +label[id$="totalNumberOfPages_label"] { + color: #333333; + float: right; + width: 60px; + font-weight:100; + margin: -22px 110px 0 0; +} +label[for$="pageNumber"] { + color: #333333; + font-weight:100; + float: left; + width: 35px; + margin: -22px 0 0 100px; +} + +input[id$="goButton"]{ + width: 35px; + height: 25px; + padding: 0px; + color: #333333; + font-weight: bold; + background-color: transparent; + float: right; + margin: -27px 95px 0 0; +} \ No newline at end of file diff --git a/appserver/admingui/full/src/main/resources/batch/batchJobs_1.inc b/appserver/admingui/full/src/main/resources/batch/batchJobs_1.inc index 6c2404516b3..b8ddce240f1 100644 --- a/appserver/admingui/full/src/main/resources/batch/batchJobs_1.inc +++ b/appserver/admingui/full/src/main/resources/batch/batchJobs_1.inc @@ -55,12 +55,39 @@ #include "/full/batch/batchRequestParam.inc" setSessionAttribute(key="#{pageSession.tabSetName}" value="batchJobs"); createMap(result="#{requestScope.attrsMap}"); - mapPut(map="#{requestScope.attrsMap}" key="target" value="#{pageSession.encodedTarget}"); + py.getPageNumber(offset="#{pageSession.offsetValue}" result="#{pageSession.pageNumber}"); + mapPut(map="#{requestScope.attrsMap}" key="target" value="#{pageSession.encodedTarget}"); mapPut(map="#{requestScope.attrsMap}" key="output" value="executionid,jobname,batchstatus,exitstatus,instanceid,starttime,endtime"); + mapPut(map="#{requestScope.attrsMap}" key="offset" value="#{pageSession.offsetValue}"); + mapPut(map="#{requestScope.attrsMap}" key="limit" value="$int{20}"); gf.restRequest(endpoint="#{sessionScope.REST_URL}/list-batch-jobs" attrs="#{requestScope.attrsMap}" method="GET" result="#{requestScope.resp}"); setAttribute(key="listOfRows", value="#{requestScope.resp.data.extraProperties.listBatchJobs}"); + setPageSessionAttribute(key="size" value="#{requestScope.listOfRows.size()}"); + setPageSessionAttribute(key="valueMap", value="#{requestScope.resp.data.extraProperties.listJobsCount}"); + py.getPageCount(jobCount="#{pageSession.valueMap['allJobsCount']}" result="#{pageSession.pageCount}"); + setPageSessionAttribute(key="showJobExecutions" value="#{true}"); + setPageSessionAttribute(key="displayNoJobsMessage" value="#{false}"); + + if (#{pageSession.size}<$int{1}){ + setPageSessionAttribute(key="showJobExecutions" value="#{false}"); + setPageSessionAttribute(key="displayNoJobsMessage" value="#{true}"); + } + + if (#{pageSession.pageNumber}<$int{1}){ + setPageSessionAttribute(key="pageNumber" value="$int{1}"); + } + + if(#{pageSession.pageNumber}=#{pageSession.pageCount}){ + setPageSessionAttribute(key="disableNextButton" value="#{true}"); + } + + if(#{pageSession.pageNumber}=$int{1}){ + setPageSessionAttribute(key="disablePreviousButton" value="#{true}"); + } + setPageSessionAttribute(key="tableTitle" value="$resource{i18nf.batch.batchJobsTableTitle}"); setPageSessionAttribute(key="editLink" value="#{request.contextPath}/full/batch/batchJobExecution.jsf?target=#{pageSession.target}&isCluster=#{pageSession.isCluster}&tabSetName=#{pageSession.encodedTabSetName}"); /> +#include"/batch/assets/js/batch.js" diff --git a/appserver/admingui/full/src/main/resources/batch/batchJobs_2.inc b/appserver/admingui/full/src/main/resources/batch/batchJobs_2.inc index 3660f050fe9..63d475e6fea 100644 --- a/appserver/admingui/full/src/main/resources/batch/batchJobs_2.inc +++ b/appserver/admingui/full/src/main/resources/batch/batchJobs_2.inc @@ -37,17 +37,19 @@ and therefore, elected the GPL Version 2 license, then the option applies only if the new code is made subject to such option by the copyright holder. - + + Portions Copyright [2018] [Payara Foundation and/or its affiliates] --> #include "/common/shared/alertMsg_1.inc" #include "/common/shared/nameSection.inc" + paginationControls="$boolean{false}" + paginateButton="$boolean{false}"> $page{tableId}); /> @@ -95,7 +97,85 @@ - - - + + + + + + + + + + + + + #{pageSession.pageCount}){ + setPageSessionAttribute(key="pageNumber" value="#{pageSession.pageCount}"); + } + py.getSpecifiedPageNumber(pageNumber="#{pageSession.pageNumber}" result="offsetNewValue"); + if(#{pageSession.isCluster}=true){ + gf.redirect(page="#{request.contextPath}/full/batch/batchJobsCluster.jsf?target=#{pageSession.encodedTarget}&offsetValue=#{requestScope.offsetNewValue}&isCluster=true&tabSetName=#{pageSession.encodedTabSetName}"); + } + + if(#{pageSession.isCluster}=false){ + if(#{pageSession.target}=server){ + gf.redirect(page="#{request.contextPath}/full/batch/batchJobsServer.jsf?target=server&offsetValue=#{requestScope.offsetNewValue}&isCluster=false&tabSetName=#{pageSession.encodedTabSetName}"); + } + if("!(#{pageSession.target}=server)"){ + gf.redirect(page="#{request.contextPath}/full/batch/batchJobsStandalone.jsf?target=#{pageSession.encodedTarget}&offsetValue=#{requestScope.offsetNewValue}&isCluster=false&tabSetName=#{pageSession.encodedTabSetName}"); + } + } + /> + + + + + + + + + + + + + + + diff --git a/appserver/admingui/full/src/main/resources/batch/batchRequestParam.inc b/appserver/admingui/full/src/main/resources/batch/batchRequestParam.inc index 2c1885e8137..662707df7ea 100644 --- a/appserver/admingui/full/src/main/resources/batch/batchRequestParam.inc +++ b/appserver/admingui/full/src/main/resources/batch/batchRequestParam.inc @@ -38,28 +38,30 @@ only if the new code is made subject to such option by the copyright holder. + Portions Copyright [2018] [Payara Foundation and/or its affiliates] --> getRequestValue(key="target" value="#{pageSession.target}"); urlencode(value="#{pageSession.target}" encoding="UTF-8" result="#{pageSession.encodedTarget}"); getRequestValue(key="isCluster" value="#{pageSession.isCluster}" default="false"); getRequestValue(key="tabSetName" value="#{pageSession.tabSetName}"); +getRequestValue(key="offsetValue" value="#{pageSession.offsetValue}" default="$int{0}"); urlencode(value="#{pageSession.tabSetName}" encoding="UTF-8" result="#{pageSession.encodedTabSetName}"); if(#{pageSession.isCluster}=true){ setPageSessionAttribute(key="clusterName", value="#{pageSession.target}"); setPageSessionAttribute(key="encodedClusterName", value="#{pageSession.encodedTarget}"); - setPageSessionAttribute(key="listJobsLink" value="#{request.contextPath}/full/batch/batchJobsCluster.jsf?target=#{pageSession.encodedTarget}&isCluster=true&tabSetName=#{pageSession.encodedTabSetName}"); + setPageSessionAttribute(key="listJobsLink" value="#{request.contextPath}/full/batch/batchJobsCluster.jsf?target=#{pageSession.encodedTarget}&offsetValue=#{pageSession.offsetValue}&isCluster=true&tabSetName=#{pageSession.encodedTabSetName}"); } if(#{pageSession.isCluster}=false){ setPageSessionAttribute(key="isCluster", value="false"); setPageSessionAttribute(key="instanceName", value="#{pageSession.target}"); setPageSessionAttribute(key="encodedInstanceName", value="#{pageSession.encodedTarget}"); if(#{pageSession.target}=server){ - setPageSessionAttribute(key="listJobsLink" value="#{request.contextPath}/full/batch/batchJobsServer.jsf?target=server&isCluster=false&tabSetName=#{pageSession.encodedTabSetName}"); + setPageSessionAttribute(key="listJobsLink" value="#{request.contextPath}/full/batch/batchJobsServer.jsf?target=server&offsetValue=#{pageSession.offsetValue}&isCluster=false&tabSetName=#{pageSession.encodedTabSetName}"); } if("!(#{pageSession.target}=server)"){ - setPageSessionAttribute(key="listJobsLink" value="#{request.contextPath}/full/batch/batchJobsStandalone.jsf?target=#{pageSession.encodedTarget}&isCluster=false&tabSetName=#{pageSession.encodedTabSetName}"); + setPageSessionAttribute(key="listJobsLink" value="#{request.contextPath}/full/batch/batchJobsStandalone.jsf?target=#{pageSession.encodedTarget}&offsetValue=#{pageSession.offsetValue}&isCluster=false&tabSetName=#{pageSession.encodedTabSetName}"); } } diff --git a/appserver/admingui/full/src/main/resources/org/glassfish/full/admingui/Strings.properties b/appserver/admingui/full/src/main/resources/org/glassfish/full/admingui/Strings.properties index 4feaad7c0b8..fb465560933 100644 --- a/appserver/admingui/full/src/main/resources/org/glassfish/full/admingui/Strings.properties +++ b/appserver/admingui/full/src/main/resources/org/glassfish/full/admingui/Strings.properties @@ -167,6 +167,10 @@ batch.configuration.tablePrefix=Table Prefix batch.configuration.tablePrefixHelp=Prefix to add to the table name (This may be ignored by non-rdbms based stores) batch.configuration.tableSuffix=Table Suffix batch.configuration.tableSuffixHelp=Suffix to add to the table name (This may be ignored by non-rdbms based stores) +batch.pagination.perviousButton=<< Previous +batch.pagination.nextButton=Next >> +batch.pagination.goButton=Go +batch.pagination.noJobsToDisplayMessage=There are no batch jobs to display # End Payara Editions tree.batch=Batch diff --git a/appserver/batch/glassfish-batch-commands/src/main/java/org/glassfish/batch/AbstractListCommandProxy.java b/appserver/batch/glassfish-batch-commands/src/main/java/org/glassfish/batch/AbstractListCommandProxy.java index 91b7223440f..59530e603ae 100644 --- a/appserver/batch/glassfish-batch-commands/src/main/java/org/glassfish/batch/AbstractListCommandProxy.java +++ b/appserver/batch/glassfish-batch-commands/src/main/java/org/glassfish/batch/AbstractListCommandProxy.java @@ -37,23 +37,17 @@ * only if the new code is made subject to such option by the copyright * holder. */ -// Portions Copyright [2016] [Payara Foundation and/or its affiliates] +// Portions Copyright [2016-2018] [Payara Foundation and/or its affiliates] package org.glassfish.batch; -import com.sun.enterprise.config.serverbeans.Domain; import com.sun.enterprise.config.serverbeans.Server; import com.sun.enterprise.util.SystemPropertyConstants; import org.glassfish.api.ActionReport; -import org.glassfish.api.I18n; import org.glassfish.api.Param; import org.glassfish.api.admin.*; -import org.glassfish.config.support.CommandTarget; -import org.glassfish.config.support.TargetType; -import org.glassfish.hk2.api.PerLookup; import org.glassfish.hk2.api.ServiceLocator; import org.glassfish.internal.api.Target; -import org.jvnet.hk2.annotations.Service; import javax.inject.Inject; import java.util.Properties; diff --git a/appserver/batch/glassfish-batch-commands/src/main/java/org/glassfish/batch/ListBatchJobs.java b/appserver/batch/glassfish-batch-commands/src/main/java/org/glassfish/batch/ListBatchJobs.java index baf11102553..83982694efd 100644 --- a/appserver/batch/glassfish-batch-commands/src/main/java/org/glassfish/batch/ListBatchJobs.java +++ b/appserver/batch/glassfish-batch-commands/src/main/java/org/glassfish/batch/ListBatchJobs.java @@ -44,6 +44,17 @@ import com.ibm.jbatch.spi.TaggedJobExecution; import com.sun.enterprise.config.serverbeans.Domain; import com.sun.enterprise.util.ColumnFormatter; +import fish.payara.jbatch.persistence.rdbms.DB2PersistenceManager; +import fish.payara.jbatch.persistence.rdbms.H2PersistenceManager; +import fish.payara.jbatch.persistence.rdbms.JBatchJDBCPersistenceManager; +import fish.payara.jbatch.persistence.rdbms.MySqlPersistenceManager; +import fish.payara.jbatch.persistence.rdbms.OraclePersistenceManager; +import fish.payara.jbatch.persistence.rdbms.PostgresPersistenceManager; +import fish.payara.jbatch.persistence.rdbms.SQLServerPersistenceManager; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; import org.glassfish.api.I18n; import org.glassfish.api.Param; import org.glassfish.api.admin.*; @@ -54,9 +65,16 @@ import javax.batch.operations.*; import javax.batch.runtime.JobExecution; -import javax.batch.runtime.JobInstance; import java.util.*; import java.util.logging.Level; +import java.util.logging.Logger; +import javax.inject.Inject; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.sql.DataSource; +import javax.validation.constraints.Min; +import org.glassfish.batch.spi.impl.BatchRuntimeConfiguration; +import org.glassfish.batch.spi.impl.BatchRuntimeHelper; /** * Command to list batch jobs info @@ -73,13 +91,13 @@ @ExecuteOn(value = {RuntimeType.INSTANCE}) @TargetType({CommandTarget.DAS, CommandTarget.STANDALONE_INSTANCE, CommandTarget.CLUSTER, CommandTarget.CLUSTERED_INSTANCE, CommandTarget.CONFIG}) @RestEndpoints({ - @RestEndpoint(configBean = Domain.class, - opType = RestEndpoint.OpType.GET, - path = "_ListBatchJobs", - description = "_List Batch Jobs") + @RestEndpoint(configBean = Domain.class, + opType = RestEndpoint.OpType.GET, + path = "_ListBatchJobs", + description = "_List Batch Jobs") }) public class ListBatchJobs - extends AbstractLongListCommand { + extends AbstractLongListCommand { private static final String JOB_NAME = "jobName"; @@ -99,46 +117,282 @@ public class ListBatchJobs private static final String START_TIME = "startTime"; private static final String END_TIME = "endTime"; + + private static final String GET_JOB_NAMES_QUERY = "SELECT DISTINCT name FROM JOBINSTANCEDATA"; @Param(primary = true, optional = true) String jobName; + + @Min(value = 0, message = "Offset value needs to be greter than 0") + @Param(name = "offset", optional = true, defaultValue = "0") + String offSetValue; + @Min(value = 0, message = "Limit value needs to be greter than 0") + @Param(name = "limit", optional = true, defaultValue = "2000") + String limitValue; + + @Inject + BatchRuntimeHelper batchRuntimeHelper; + + @Inject + BatchRuntimeConfiguration batchRuntimeConfiguration; + + private DataSource dataSource; + @Override protected void executeCommand(AdminCommandContext context, Properties extraProps) - throws Exception { - - ColumnFormatter columnFormatter = new ColumnFormatter(getDisplayHeaders()); - if (isSimpleMode()) { - extraProps.put("simpleMode", true); - extraProps.put("listBatchJobs", findSimpleJobInfo(columnFormatter)); - } else { - extraProps.put("simpleMode", false); - List> jobExecutions = new ArrayList<>(); - extraProps.put("listBatchJobs", jobExecutions); + throws Exception { + + String dataSourceName = batchRuntimeHelper.getDataSourceLookupName(); + InitialContext ctx = new InitialContext(); + Object object = ctx.lookup(dataSourceName); + //check whether the referenced JNDI entry is a DataSource + if (object instanceof DataSource) { + dataSource = DataSource.class.cast(object); + createTables(); - Map jobInstanceCount = null; - if (Arrays.asList(getOutputHeaders()).contains(INSTANCE_COUNT)) { - jobInstanceCount = getJobInstanceCount(); + ColumnFormatter columnFormatter = new ColumnFormatter(getDisplayHeaders()); + if (isSimpleMode()) { + extraProps.put("simpleMode", true); + Map jobsInstanceCount = new HashMap<>(); + + if (jobName != null) { + jobsInstanceCount.put(jobName, getJobInstanceCount(jobName)); + } else { + // Get all aviable Job Names + List jobNames = executeQuery(GET_JOB_NAMES_QUERY, "name"); + + for (String jobName : jobNames) { + jobsInstanceCount.put(jobName, getJobInstanceCount(jobName)); + } + } + + extraProps.put("listBatchJobs", findSimpleJobInfo(jobsInstanceCount, columnFormatter)); + } else { + extraProps.put("simpleMode", false); + Map map = new HashMap<>(); + map.put("allJobsCount", getAllJobInstanceCount()); + extraProps.put("listJobsCount", map); + List> jobExecutions = new ArrayList<>(); + extraProps.put("listBatchJobs", jobExecutions); + Map jobsInstanceCount = new HashMap<>(); + + if (Arrays.asList(getOutputHeaders()).contains(INSTANCE_COUNT)) { + // Get all aviable Job Names + List jobNames = executeQuery(GET_JOB_NAMES_QUERY, "name"); + + for (String jobName : jobNames) { + jobsInstanceCount.put(jobName, getJobInstanceCount(jobName)); + } + } + + List jobInstanceIDs = getJobInstanceIDs(); + JobOperator jobOperator = AbstractListCommand.getJobOperatorFromBatchRuntime(); + + for (Long jobExecution : jobInstanceIDs) { + try { + if (glassFishBatchSecurityHelper.isVisibleToThisInstance(((TaggedJobExecution) jobOperator.getJobExecution(jobExecution)).getTagName())) { + jobExecutions.add(handleJob(jobOperator.getJobExecution(jobExecution), columnFormatter, jobsInstanceCount)); + } + } catch (Exception ex) { + logger.log(Level.WARNING, "Exception while getting jobExecution details: " + ex); + logger.log(Level.FINE, "Exception while getting jobExecution details ", ex); + } + } } - for (JobExecution jobExecution : findJobExecutions()) { - try { - if (glassFishBatchSecurityHelper.isVisibleToThisInstance(((TaggedJobExecution) jobExecution).getTagName())) - jobExecutions.add(handleJob(jobExecution, columnFormatter, jobInstanceCount)); - } catch (Exception ex) { - logger.log(Level.WARNING, "Exception while getting jobExecution details: " + ex); - logger.log(Level.FINE, "Exception while getting jobExecution details ", ex); + context.getActionReport().setMessage(columnFormatter.toString()); + } + } + + private void createTables(){ + try (Connection connection = dataSource.getConnection()) { + String database = connection.getMetaData().getDatabaseProductName(); + + if (database.contains("Derby")) { + JBatchJDBCPersistenceManager jBatchJDBCPersistenceManager = new JBatchJDBCPersistenceManager(); + jBatchJDBCPersistenceManager.createTables(dataSource, batchRuntimeConfiguration); + } else if (database.contains("H2")) { + H2PersistenceManager h2PersistenceManager = new H2PersistenceManager(); + h2PersistenceManager.createTables(dataSource, batchRuntimeConfiguration); + } else if (database.contains("MySQL")) { + MySqlPersistenceManager mySqlPersistenceManager = new MySqlPersistenceManager(); + mySqlPersistenceManager.createTables(dataSource, batchRuntimeConfiguration); + } else if (database.contains("Oracle")) { + OraclePersistenceManager oraclePersistenceManager = new OraclePersistenceManager(); + oraclePersistenceManager.createTables(dataSource, batchRuntimeConfiguration); + } else if (database.contains("PostgreSQL")) { + PostgresPersistenceManager postgresPersistenceManager = new PostgresPersistenceManager(); + postgresPersistenceManager.createTables(dataSource, batchRuntimeConfiguration); + } else if (database.contains("DB2")) { + DB2PersistenceManager dB2PersistenceManager = new DB2PersistenceManager(); + dB2PersistenceManager.createTables(dataSource, batchRuntimeConfiguration); + } else if (database.contains("Microsoft SQL Server")) { + SQLServerPersistenceManager sQLServerPersistenceManager = new SQLServerPersistenceManager(); + sQLServerPersistenceManager.createTables(dataSource, batchRuntimeConfiguration); + } + } catch (SQLException ex) { + logger.severe(ex.getLocalizedMessage()); + } + } + + private int getJobInstanceCount(String jobName) { + int jobInstanceCount = 0; + + try (Connection connection = dataSource.getConnection()) { + String query = "SELECT COUNT(jobinstanceid) AS jobinstancecount FROM JOBINSTANCEDATA WHERE NAME ='" + jobName + "'"; + try (Statement statement = connection.createStatement()) { + try (ResultSet resultSet = statement.executeQuery(query)) { + resultSet.next(); + jobInstanceCount = resultSet.getInt("jobinstancecount"); + } + } + } catch (SQLException ex) { + logger.severe(ex.getLocalizedMessage()); + } + + return jobInstanceCount; + } + + private int getAllJobInstanceCount() { + int allJobInstanceCount = 0; + + try (Connection connection = dataSource.getConnection()) { + String query = "SELECT COUNT(jobinstanceid) AS jobinstancecount FROM JOBINSTANCEDATA"; + try (Statement statement = connection.createStatement()) { + try (ResultSet resultSet = statement.executeQuery(query)) { + resultSet.next(); + allJobInstanceCount = resultSet.getInt("jobinstancecount"); + } + } + } catch (SQLException ex) { + logger.severe(ex.getLocalizedMessage()); + } + + return allJobInstanceCount; + } + + private List getJobInstanceIDs() { + List result = new ArrayList<>(); + + try (Connection connection = dataSource.getConnection()) { + String database = connection.getMetaData().getDatabaseProductName(); + String query; + String columnID = "jobinstanceid"; + + if (database.contains("Derby") || database.contains("DB2")) { + if (jobName != null) { + query = "SELECT jobinstanceid " + + "FROM JOBINSTANCEDATA " + + "WHERE NAME ='" + jobName + "' " + + "ORDER BY jobinstanceid " + + "DESC FETCH FIRST " + limitValue + " ROWS ONLY"; + } else { + query = "SELECT jobinstanceid " + + "FROM JOBINSTANCEDATA " + + "ORDER BY jobinstanceid DESC " + + "OFFSET " + offSetValue + " " + + "ROWS FETCH FIRST " + limitValue + " ROWS ONLY"; + } + + + } else if (database.contains("Oracle")) { + if (jobName != null) { + query = "SELECT jobinstanceid " + + "FROM (" + + " SELECT jobinstanceid " + + " FROM JOBINSTANCEDATA " + + " WHERE NAME ='" + jobName + "'" + + ") " + + "WHERE ROWNUM <=" + limitValue + " " + + "ORDER BY jobinstanceid DESC"; + } else { + int offset = Integer.parseInt(offSetValue); + int limit = Integer.parseInt(limitValue); + int rowSize = offset + limit; + query = "SELECT jobinstanceid " + + "FROM (" + + " SELECT ROWNUM numofrows, tables.*" + + " FROM(" + + " SELECT jobinstanceid" + + " FROM JOBINSTANCEDATA" + + " ORDER BY jobinstanceid DESC" + + " ) tables" + + " WHERE ROWNUM <= " + rowSize + + ")" + + "WHERE numofrows > " + offset; + } + } else if (database.contains("Microsoft SQL Server")) { + if (jobName != null) { + query = "SELECT TOP " + limitValue + " " + + "jobinstanceid FROM JOBINSTANCEDATA " + + "WHERE NAME ='" + jobName + "' " + + "ORDER BY jobinstanceid DESC"; + } else { + query = "SELECT TOP " + limitValue + " jobinstanceid " + + "FROM (" + + " SELECT jobinstanceid, ROW_NUMBER()" + + " OVER (" + + " ORDER BY jobinstanceid DESC" + + " ) as RowNum " + + " FROM JOBINSTANCEDATA" + + " ) AS MyDerivedTable " + + "WHERE MyDerivedTable.RowNum > " + offSetValue; + } + } else { + if (jobName != null) { + query = "SELECT jobinstanceid " + + "FROM JOBINSTANCEDATA " + + "WHERE NAME ='" + jobName + "' " + + "ORDER BY jobinstanceid " + + "DESC LIMIT " + limitValue; + } else { + query = "SELECT jobinstanceid " + + "FROM JOBINSTANCEDATA " + + "ORDER BY jobinstanceid " + + "DESC LIMIT " + limitValue + + " OFFSET " + offSetValue; } } + + try (Statement statement = connection.createStatement()) { + try (ResultSet resultSet = statement.executeQuery(query)) { + while (resultSet.next()) { + result.add(resultSet.getLong(columnID)); + } + } + } + } catch (SQLException ex) { + Logger.getLogger(ListBatchJobs.class.getName()).log(Level.SEVERE, null, ex); } - context.getActionReport().setMessage(columnFormatter.toString()); + + return result; + } + + private List executeQuery(String query, String columnID) throws NamingException { + List result = new ArrayList<>(); + + try (Connection connection = dataSource.getConnection()) { + + try (Statement statement = connection.createStatement()) { + try (ResultSet resultSet = statement.executeQuery(query)) { + while (resultSet.next()) { + result.add(resultSet.getString(columnID)); + } + } + } + } catch (SQLException ex) { + logger.severe(ex.getLocalizedMessage()); + } + + return result; } @Override protected final String[] getAllHeaders() { return new String[]{ - JOB_NAME, APP_NAME, INSTANCE_COUNT, INSTANCE_ID, EXECUTION_ID, BATCH_STATUS, - START_TIME, END_TIME, EXIT_STATUS + JOB_NAME, APP_NAME, INSTANCE_COUNT, INSTANCE_ID, EXECUTION_ID, BATCH_STATUS, + START_TIME, END_TIME, EXIT_STATUS }; } @@ -157,112 +411,62 @@ private boolean isSimpleMode() { return getOutputHeaders().length == 2; } - private Map findSimpleJobInfo(ColumnFormatter columnFormatter) { - - Map jobToInstanceCountMap = getJobInstanceCount(); - + private Map findSimpleJobInfo(Map jobToInstanceCountMap, ColumnFormatter columnFormatter) { for (Map.Entry e : jobToInstanceCountMap.entrySet()) { columnFormatter.addRow(new Object[]{e.getKey(), e.getValue()}); } return jobToInstanceCountMap; } - private Map getJobInstanceCount() - throws JobSecurityException, NoSuchJobException { - - Map jobInstanceCount = new HashMap<>(); - List jobExecutions = findJobExecutions(); - for (JobExecution jobExecution : jobExecutions) { - if (glassFishBatchSecurityHelper.isVisibleToThisInstance(((TaggedJobExecution) jobExecution).getTagName())) { - String jobName = jobExecution.getJobName(); - int count = 0; - if (jobInstanceCount.containsKey(jobName)) { - count = jobInstanceCount.get(jobName); - } - jobInstanceCount.put(jobName, count + 1); - } - } - return jobInstanceCount; - } - - private List findJobExecutions() - throws JobSecurityException, NoSuchJobException, NoSuchJobInstanceException { - List jobExecutions = new ArrayList<>(); - - JobOperator jobOperator = AbstractListCommand.getJobOperatorFromBatchRuntime(); - if (jobName != null) { - List exe = jobOperator.getJobInstances(jobName, 0, Integer.MAX_VALUE - 1); - if (exe != null) { - for (JobInstance ji : exe) { - jobExecutions.addAll(jobOperator.getJobExecutions(ji)); - } - } - } else { - Set jobNames = jobOperator.getJobNames(); - if (jobNames != null) { - for (String jn : jobOperator.getJobNames()) { - List exe = jobOperator.getJobInstances(jn, 0, Integer.MAX_VALUE - 1); - if (exe != null) { - for (JobInstance ji : exe) { - jobExecutions.addAll(jobOperator.getJobExecutions(ji)); - } - } - } - } - } - - return jobExecutions; - } - - private Map handleJob(JobExecution jobExexution, ColumnFormatter columnFormatter, Map instanceCount) - throws JobSecurityException, NoSuchJobException, NoSuchJobExecutionException { + private Map handleJob(JobExecution jobExecution, ColumnFormatter columnFormatter, Map jobInstanceCount) + throws JobSecurityException, NoSuchJobException, NoSuchJobExecutionException { Map jobInfo = new HashMap<>(); - String[] cfData = new String[getOutputHeaders().length]; + String[] columnFormatterData = new String[getOutputHeaders().length]; JobOperator jobOperator = AbstractListCommand.getJobOperatorFromBatchRuntime(); for (int index = 0; index < getOutputHeaders().length; index++) { Object data = null; switch (getOutputHeaders()[index]) { case JOB_NAME: - data = "" + jobExexution.getJobName(); + data = "" + jobExecution.getJobName(); break; case APP_NAME: try { - String appName = "" + ((TaggedJobExecution) jobExexution).getTagName(); + String appName = "" + ((TaggedJobExecution) jobExecution).getTagName(); int semi = appName.indexOf(':'); - data = appName.substring(semi+1); + data = appName.substring(semi + 1); } catch (Exception ex) { logger.log(Level.FINE, "Error while calling ((TaggedJobExecution) je).getTagName() ", ex); data = ex.toString(); } break; case INSTANCE_COUNT: - data = instanceCount != null ? instanceCount.get(jobExexution.getJobName()) : ""; + data = jobInstanceCount.size() > 0 ? jobInstanceCount.get(jobExecution.getJobName()) : ""; break; case INSTANCE_ID: - data = jobOperator.getJobInstance(jobExexution.getExecutionId()).getInstanceId(); + data = jobOperator.getJobInstance(jobExecution.getExecutionId()).getInstanceId(); break; case EXECUTION_ID: - data = jobExexution.getExecutionId(); + data = jobExecution.getExecutionId(); break; case BATCH_STATUS: - data = jobExexution.getBatchStatus() != null ? jobExexution.getBatchStatus() : ""; + data = jobExecution.getBatchStatus() != null ? jobExecution.getBatchStatus() : ""; break; case EXIT_STATUS: - data = jobExexution.getExitStatus() != null ? jobExexution.getExitStatus() : ""; + data = jobExecution.getExitStatus() != null ? jobExecution.getExitStatus() : ""; break; case START_TIME: - if (jobExexution.getStartTime() != null) { - data = jobExexution.getStartTime().getTime(); - cfData[index] = jobExexution.getStartTime().toString(); + if (jobExecution.getStartTime() != null) { + data = jobExecution.getStartTime().getTime(); + columnFormatterData[index] = jobExecution.getStartTime().toString(); } else { data = ""; } break; case END_TIME: - if (jobExexution.getEndTime() != null) { - data = jobExexution.getEndTime().getTime(); - cfData[index] = jobExexution.getEndTime().toString(); + if (jobExecution.getEndTime() != null) { + data = jobExecution.getEndTime().getTime(); + columnFormatterData[index] = jobExecution.getEndTime().toString(); } else { data = ""; } @@ -271,10 +475,11 @@ private Map handleJob(JobExecution jobExexution, ColumnFormatter throw new IllegalArgumentException("Unknown header: " + getOutputHeaders()[index]); } jobInfo.put(getOutputHeaders()[index], data); - if (cfData[index] == null) - cfData[index] = data.toString(); + if (columnFormatterData[index] == null) { + columnFormatterData[index] = data.toString(); + } } - columnFormatter.addRow(cfData); + columnFormatter.addRow(columnFormatterData); return jobInfo; } diff --git a/appserver/batch/glassfish-batch-commands/src/main/java/org/glassfish/batch/ListBatchJobsProxy.java b/appserver/batch/glassfish-batch-commands/src/main/java/org/glassfish/batch/ListBatchJobsProxy.java index e76f4e400c6..b81d758ee72 100644 --- a/appserver/batch/glassfish-batch-commands/src/main/java/org/glassfish/batch/ListBatchJobsProxy.java +++ b/appserver/batch/glassfish-batch-commands/src/main/java/org/glassfish/batch/ListBatchJobsProxy.java @@ -52,6 +52,7 @@ import org.jvnet.hk2.annotations.Service; import java.util.Properties; +import javax.validation.constraints.Min; /** * Command to list batch jobs info @@ -79,24 +80,41 @@ public class ListBatchJobsProxy @Param(primary = true, optional = true) String jobName; + @Min(value = 0, message = "Offset value needs to be greter than 0") + @Param(name = "offset", optional = true, defaultValue = "0") + String offSetValue; + + @Min(value = 0, message = "Limit value needs to be greter than 0") + @Param(name = "limit", optional = true, defaultValue = "2000") + String limitValue; + + @Override protected String getCommandName() { return "_ListBatchJobs"; } protected void fillParameterMap(ParameterMap parameterMap) { - super.fillParameterMap(parameterMap); - if (jobName != null) + super.fillParameterMap(parameterMap); + if (jobName != null) { parameterMap.add("DEFAULT", jobName); + } + parameterMap.add("offset", offSetValue); + parameterMap.add("limit", limitValue); } protected void postInvoke(AdminCommandContext context, ActionReport subReport) { Properties subProperties = subReport.getExtraProperties(); Properties extraProps = context.getActionReport().getExtraProperties(); - if (subProperties.get("simpleMode") != null) + if (subProperties.get("simpleMode") != null) { extraProps.put("simpleMode", subProperties.get("simpleMode")); - if (subProperties.get("listBatchJobs") != null) + } + if (subProperties.get("listBatchJobs") != null) { extraProps.put("listBatchJobs", subProperties.get("listBatchJobs")); + } + if (subProperties.get("listJobsCount") != null) { + extraProps.put("listJobsCount", subProperties.get("listJobsCount")); + } } } diff --git a/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/DB2PersistenceManager.java b/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/DB2PersistenceManager.java index 407dae77907..82e6bfa86ed 100644 --- a/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/DB2PersistenceManager.java +++ b/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/DB2PersistenceManager.java @@ -1,16 +1,41 @@ /* - * Copyright (c) 2016 Payara Foundation. All rights reserved. - - * The contents of this file are subject to the terms of the Common Development + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2016-2018 Payara Foundation and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can * obtain a copy of the License at - * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html - * or packager/legal/LICENSE.txt. See the License for the specific + * https://github.com/payara/Payara/blob/master/LICENSE.txt + * See the License for the specific * language governing permissions and limitations under the License. - + * * When distributing the software, include this License Header Notice in each - * file and include the License file at packager/legal/LICENSE.txt. + * file and include the License file at glassfish/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * The Payara Foundation designates this particular file as subject to the "Classpath" + * exception as provided by the Payara Foundation in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. */ package fish.payara.jbatch.persistence.rdbms; @@ -33,6 +58,8 @@ import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; +import static org.glassfish.batch.spi.impl.BatchRuntimeHelper.PAYARA_TABLE_PREFIX_PROPERTY; +import static org.glassfish.batch.spi.impl.BatchRuntimeHelper.PAYARA_TABLE_SUFFIX_PROPERTY; /** * @@ -59,8 +86,9 @@ public void init(IBatchConfig batchConfig) this.batchConfig = batchConfig; schema = batchConfig.getDatabaseConfigurationBean().getSchema(); - jndiName = batchConfig.getDatabaseConfigurationBean().getJndiName(); + prefix = batchConfig.getConfigProperties().getProperty(PAYARA_TABLE_PREFIX_PROPERTY, ""); + suffix = batchConfig.getConfigProperties().getProperty(PAYARA_TABLE_SUFFIX_PROPERTY, ""); try { Context ctx = new InitialContext(); @@ -77,7 +105,7 @@ public void init(IBatchConfig batchConfig) // Load the table names and queries shared between different database // types - tableNames = getSharedTableMap(batchConfig); + tableNames = getSharedTableMap(); try { queryStrings = getSharedQueryMap(batchConfig); @@ -86,11 +114,6 @@ public void init(IBatchConfig batchConfig) throw new BatchContainerServiceException(e1); } - // put the create table strings into a hashmap - // createTableStrings = setCreateTableMap(batchConfig); - - createDB2Strings = setCreateDB2StringsMap(batchConfig); - logger.config("JNDI name = " + jndiName); if (jndiName == null || jndiName.equals("")) { @@ -100,10 +123,10 @@ public void init(IBatchConfig batchConfig) try { - if (!isDB2SchemaValid()) { + if (!isSchemaValid()) { setDefaultSchema(); } - checkDB2Tables(); + checkTables(); } catch (SQLException e) { logger.severe(e.getLocalizedMessage()); @@ -118,7 +141,8 @@ public void init(IBatchConfig batchConfig) * @return * @throws SQLException */ - private boolean isDB2SchemaValid() throws SQLException { + @Override + protected boolean isSchemaValid() throws SQLException { boolean result = false; Connection conn = null; @@ -155,10 +179,11 @@ private boolean isDB2SchemaValid() throws SQLException { * Check the relevant db2 tables exist in the relevant schema * @throws SQLException */ - private void checkDB2Tables() throws SQLException { - + @Override + protected void checkTables() throws SQLException { + logger.entering(CLASSNAME, "checkDB2Tables"); - + setCreateDB2StringsMap(tableNames); createDB2TableNotExists(tableNames.get(CHECKPOINT_TABLE_KEY), createDB2Strings.get(DB2_CREATE_TABLE_CHECKPOINTDATA)); @@ -202,7 +227,6 @@ protected void createDB2TableNotExists(String tableName, ResultSet.CONCUR_READ_ONLY); String query = "select name from sysibm.systables where name =" + "\'" + tableName.toUpperCase() + "\'" + "and type = 'T'"; - ; rs = stmt.executeQuery(query); int rowcount = getTableRowCount(rs); @@ -230,8 +254,7 @@ protected void createDB2TableNotExists(String tableName, * Method invoked to insert the DB2 create table strings into a hashmap **/ - protected Map setCreateDB2StringsMap( - IBatchConfig batchConfig) { + private Map setCreateDB2StringsMap(Map tableNames) { createDB2Strings = new HashMap<>(); createDB2Strings.put(DB2_CREATE_TABLE_CHECKPOINTDATA, "CREATE TABLE " + tableNames.get(CHECKPOINT_TABLE_KEY) @@ -240,7 +263,8 @@ protected Map setCreateDB2StringsMap( .put(DB2_CREATE_TABLE_JOBINSTANCEDATA, "CREATE TABLE " + tableNames.get(JOB_INSTANCE_TABLE_KEY) - + " (jobinstanceid BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1) CONSTRAINT JOBINSTANCE_PK PRIMARY KEY,name VARCHAR(512), apptag VARCHAR(512))"); + + " (jobinstanceid BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1) CONSTRAINT JOBINSTANCE_PK PRIMARY KEY," + + "name VARCHAR(512), apptag VARCHAR(512))"); createDB2Strings .put(DB2_CREATE_TABLE_EXECUTIONINSTANCEDATA, diff --git a/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/H2PersistenceManager.java b/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/H2PersistenceManager.java index 69345d7039a..fd06bdd24d7 100644 --- a/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/H2PersistenceManager.java +++ b/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/H2PersistenceManager.java @@ -1,7 +1,7 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright (c) 2017 Payara Foundation and/or its affiliates. All rights reserved. + * Copyright (c) 2017-2018 Payara Foundation and/or its affiliates. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development @@ -60,6 +60,8 @@ import com.ibm.jbatch.spi.services.IBatchConfig; import fish.payara.nucleus.requesttracing.RequestTracingService; +import static org.glassfish.batch.spi.impl.BatchRuntimeHelper.PAYARA_TABLE_PREFIX_PROPERTY; +import static org.glassfish.batch.spi.impl.BatchRuntimeHelper.PAYARA_TABLE_SUFFIX_PROPERTY; /** * H2 Persistence Manager @@ -84,6 +86,8 @@ public void init(IBatchConfig batchConfig) throws BatchContainerServiceException schema = batchConfig.getDatabaseConfigurationBean().getSchema(); jndiName = batchConfig.getDatabaseConfigurationBean().getJndiName(); + prefix = batchConfig.getConfigProperties().getProperty(PAYARA_TABLE_PREFIX_PROPERTY, ""); + suffix = batchConfig.getConfigProperties().getProperty(PAYARA_TABLE_SUFFIX_PROPERTY, ""); logger.log(Level.CONFIG, "JNDI name = {0}", jndiName); @@ -99,7 +103,7 @@ public void init(IBatchConfig batchConfig) throws BatchContainerServiceException } // Load the table names and queries shared between different database types - tableNames = getSharedTableMap(batchConfig); + tableNames = getSharedTableMap(); try { queryStrings = getSharedQueryMap(batchConfig); @@ -108,10 +112,10 @@ public void init(IBatchConfig batchConfig) throws BatchContainerServiceException } try { - if (!isH2SchemaValid()) { + if (!isSchemaValid()) { setDefaultSchema(); } - checkH2Tables(); + checkTables(); } catch (SQLException e) { logger.severe(e.getLocalizedMessage()); @@ -151,7 +155,8 @@ public String setDefaultSchema() throws SQLException { * @return true if the schema exists, false otherwise. * @throws SQLException */ - protected boolean isH2SchemaValid() throws SQLException { + @Override + protected boolean isSchemaValid() throws SQLException { logger.entering(CLASSNAME, "isH2SchemaValid"); try (Connection connection = getConnectionToDefaultSchema()) { @@ -176,8 +181,9 @@ protected boolean isH2SchemaValid() throws SQLException { * Check if the h2 jbatch tables exist, if not create them * */ - private void checkH2Tables() throws SQLException { - setCreateH2StringsMap(batchConfig); + @Override + protected void checkTables() throws SQLException { + setCreateH2StringsMap(tableNames); createH2TableNotExists(tableNames.get(CHECKPOINT_TABLE_KEY), createH2Strings.get(H2_CREATE_TABLE_CHECKPOINTDATA)); @@ -234,7 +240,7 @@ protected void createH2TableNotExists(String tableName, String createTableStatem @Override protected void setSchemaOnConnection(Connection connection) throws SQLException { logger.log(Level.FINEST, "Entering {0}.setSchemaOnConnection()", CLASSNAME); - try (PreparedStatement preparedStatement = connection.prepareStatement(queryStrings.get(Q_SET_SCHEMA) + schema)) { + try (PreparedStatement preparedStatement = connection.prepareStatement("SET SCHEMA " + schema)) { preparedStatement.executeUpdate(); } logger.log(Level.FINEST, "Exiting {0}.setSchemaOnConnection()", CLASSNAME); @@ -254,7 +260,7 @@ protected Map getSharedQueryMap(IBatchConfig batchConfig) throws * @param batchConfig * @return */ - protected Map setCreateH2StringsMap(IBatchConfig batchConfig) { + private Map setCreateH2StringsMap(Map tableNames) { createH2Strings = new HashMap<>(); createH2Strings.put(H2_CREATE_TABLE_CHECKPOINTDATA, "CREATE TABLE " + tableNames.get(CHECKPOINT_TABLE_KEY) diff --git a/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/JBatchJDBCPersistenceManager.java b/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/JBatchJDBCPersistenceManager.java index 14066fa1906..2a0e2bc2d3d 100644 --- a/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/JBatchJDBCPersistenceManager.java +++ b/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/JBatchJDBCPersistenceManager.java @@ -96,9 +96,10 @@ import com.ibm.jbatch.container.status.StepStatus; import com.ibm.jbatch.container.util.TCCLObjectInputStream; import com.ibm.jbatch.spi.services.IBatchConfig; -import fish.payara.notification.requesttracing.RequestTraceSpanLog; import fish.payara.nucleus.requesttracing.RequestTracingService; +import fish.payara.notification.requesttracing.RequestTraceSpanLog; +import org.glassfish.batch.spi.impl.BatchRuntimeConfiguration; /** * @@ -118,6 +119,8 @@ public class JBatchJDBCPersistenceManager implements protected DataSource dataSource = null; protected String jndiName = null; + protected String prefix = null; + protected String suffix = null; protected String schema = ""; @@ -147,6 +150,8 @@ public void init(IBatchConfig batchConfig) throws BatchContainerServiceException schema = batchConfig.getDatabaseConfigurationBean().getSchema(); jndiName = batchConfig.getDatabaseConfigurationBean().getJndiName(); + prefix = batchConfig.getConfigProperties().getProperty(PAYARA_TABLE_PREFIX_PROPERTY, ""); + suffix = batchConfig.getConfigProperties().getProperty(PAYARA_TABLE_SUFFIX_PROPERTY, ""); logger.config("JNDI name = " + jndiName); @@ -164,7 +169,7 @@ public void init(IBatchConfig batchConfig) throws BatchContainerServiceException } // Load the table names and queries shared between different database types - tableNames = getSharedTableMap(batchConfig); + tableNames = getSharedTableMap(); try { queryStrings = getSharedQueryMap(batchConfig); @@ -173,10 +178,10 @@ public void init(IBatchConfig batchConfig) throws BatchContainerServiceException } try { - if (!isDerbySchemaValid()) { + if (!isSchemaValid()) { setDefaultSchema(); } - checkDerbyTables(); + checkTables(); } catch (SQLException e) { logger.severe(e.getLocalizedMessage()); @@ -294,7 +299,7 @@ private boolean isDerby(Connection connection) throws SQLException { * @return true if the schema exists, false otherwise. * @throws SQLException */ - protected boolean isDerbySchemaValid() throws SQLException { + protected boolean isSchemaValid() throws SQLException { logger.entering(CLASSNAME, "isDerbySchemaValid"); try (Connection connection = getConnectionToDefaultSchema()) { @@ -317,9 +322,12 @@ protected boolean isDerbySchemaValid() throws SQLException { /** * Check if the derby jbatch tables exist, if not create them + * + * @param tableNames + * @throws java.sql.SQLException **/ - private void checkDerbyTables() throws SQLException { - setCreateDerbyStringsMap(batchConfig); + protected void checkTables() throws SQLException { + setCreateDerbyStringsMap(tableNames); createDerbyTableNotExists(tableNames.get(CHECKPOINT_TABLE_KEY), createDerbyStrings.get(DERBY_CREATE_TABLE_CHECKPOINTDATA)); @@ -339,6 +347,23 @@ private void checkDerbyTables() throws SQLException { } + public void createTables(DataSource dataSource, BatchRuntimeConfiguration batchRuntimeConfiguration){ + this.dataSource = dataSource; + prefix = batchRuntimeConfiguration.getTablePrefix(); + suffix = batchRuntimeConfiguration.getTableSuffix(); + schema = batchRuntimeConfiguration.getSchemaName(); + tableNames = getSharedTableMap(); + + try { + if (!isSchemaValid()) { + setDefaultSchema(); + } + checkTables(); + } catch (SQLException ex) { + logger.severe(ex.getLocalizedMessage()); + } + } + /** * Create the Derby tables **/ @@ -352,8 +377,8 @@ protected void createDerbyTableNotExists(String tableName, String createTableSta try (PreparedStatement statement = connection.prepareStatement(createTableStatement)) { statement.executeUpdate(); } - } - } + } + } } catch (SQLException e) { logger.severe(e.getLocalizedMessage()); throw e; @@ -465,7 +490,7 @@ protected void setSchemaOnConnection(Connection connection) throws SQLException logger.finest("Entering " + CLASSNAME + ".setSchemaOnConnection()"); if (!(connection.getMetaData().getDatabaseProductName().contains("Oracle"))) { - try (PreparedStatement preparedStatement = connection.prepareStatement(queryStrings.get(Q_SET_SCHEMA))) { + try (PreparedStatement preparedStatement = connection.prepareStatement("SET SCHEMA ?")) { preparedStatement.setString(1, schema); preparedStatement.executeUpdate(); } @@ -2478,11 +2503,8 @@ public void shutdown() throws BatchContainerServiceException { * the prefix and suffix to the table names **/ - protected Map getSharedTableMap(IBatchConfig batchConfig) { - String prefix = batchConfig.getConfigProperties().getProperty(PAYARA_TABLE_PREFIX_PROPERTY, ""); - String suffix = batchConfig.getConfigProperties().getProperty(PAYARA_TABLE_SUFFIX_PROPERTY, ""); - - Map result = new HashMap(6); + protected Map getSharedTableMap() { + Map result = new HashMap<>(6); result.put(JOB_INSTANCE_TABLE_KEY, prefix + "JOBINSTANCEDATA" + suffix); result.put(EXECUTION_INSTANCE_TABLE_KEY, prefix + "EXECUTIONINSTANCEDATA" + suffix); result.put(STEP_EXECUTION_INSTANCE_TABLE_KEY, prefix + "STEPEXECUTIONINSTANCEDATA" + suffix); @@ -2744,8 +2766,7 @@ protected Map getSharedQueryMap(IBatchConfig batchConfig) throws * Method invoked to insert the Derby create table strings into a hashmap **/ - protected Map setCreateDerbyStringsMap( - IBatchConfig batchConfig) { + private Map setCreateDerbyStringsMap(Map tableNames) { createDerbyStrings = new HashMap<>(); createDerbyStrings.put(DERBY_CREATE_TABLE_CHECKPOINTDATA, "CREATE TABLE " + tableNames.get(CHECKPOINT_TABLE_KEY) @@ -2849,4 +2870,4 @@ private RequestTraceSpanLog constructJBatchExecutionSpanLog(RuntimeJobExecution return spanLog; } -} +} \ No newline at end of file diff --git a/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/MySqlPersistenceManager.java b/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/MySqlPersistenceManager.java index 9a0d9b0d1ed..07b876ee18a 100644 --- a/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/MySqlPersistenceManager.java +++ b/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/MySqlPersistenceManager.java @@ -1,16 +1,41 @@ /* - * Copyright (c) 2016 Payara Foundation. All rights reserved. - - * The contents of this file are subject to the terms of the Common Development + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2016-2018 Payara Foundation and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can * obtain a copy of the License at - * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html - * or packager/legal/LICENSE.txt. See the License for the specific + * https://github.com/payara/Payara/blob/master/LICENSE.txt + * See the License for the specific * language governing permissions and limitations under the License. - + * * When distributing the software, include this License Header Notice in each - * file and include the License file at packager/legal/LICENSE.txt. + * file and include the License file at glassfish/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * The Payara Foundation designates this particular file as subject to the "Classpath" + * exception as provided by the Payara Foundation in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. */ package fish.payara.jbatch.persistence.rdbms; @@ -33,6 +58,8 @@ import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; +import static org.glassfish.batch.spi.impl.BatchRuntimeHelper.PAYARA_TABLE_PREFIX_PROPERTY; +import static org.glassfish.batch.spi.impl.BatchRuntimeHelper.PAYARA_TABLE_SUFFIX_PROPERTY; /** * @@ -59,8 +86,9 @@ public void init(IBatchConfig batchConfig) this.batchConfig = batchConfig; schema = batchConfig.getDatabaseConfigurationBean().getSchema(); - jndiName = batchConfig.getDatabaseConfigurationBean().getJndiName(); + prefix = batchConfig.getConfigProperties().getProperty(PAYARA_TABLE_PREFIX_PROPERTY, ""); + suffix = batchConfig.getConfigProperties().getProperty(PAYARA_TABLE_SUFFIX_PROPERTY, ""); if (jndiName == null || jndiName.equals("")) { throw new BatchContainerServiceException( @@ -81,7 +109,7 @@ public void init(IBatchConfig batchConfig) // Load the table names and queries shared between different database // types - tableNames = getSharedTableMap(batchConfig); + tableNames = getSharedTableMap(); try { queryStrings = getSharedQueryMap(batchConfig); @@ -89,20 +117,16 @@ public void init(IBatchConfig batchConfig) // TODO Auto-generated catch block throw new BatchContainerServiceException(e1); } - // put the create table strings into a hashmap - //createTableStrings = setCreateTableMap(batchConfig); - - createMySQLStrings = setCreateMySQLStringsMap(batchConfig); logger.config("JNDI name = " + jndiName); try { - if (!isMySQLSchemaValid()) { + if (!isSchemaValid()) { setDefaultSchema(); } - checkMySQLTables(); + checkTables(); } catch (SQLException e) { logger.severe(e.getLocalizedMessage()); @@ -117,7 +141,8 @@ public void init(IBatchConfig batchConfig) * @return * @throws SQLException */ - private boolean isMySQLSchemaValid() throws SQLException { + @Override + protected boolean isSchemaValid() throws SQLException { logger.entering(CLASSNAME, "isMySQLSchemaValid"); boolean result = false; @@ -148,11 +173,15 @@ private boolean isMySQLSchemaValid() throws SQLException { /** * Verify the relevant JBatch tables exist. + * + * @param tableNames * @throws SQLException */ - private void checkMySQLTables() throws SQLException { + @Override + protected void checkTables() throws SQLException { logger.entering(CLASSNAME, "checkMySQLTables"); + setCreateMySQLStringsMap(tableNames); createMySQLTableNotExists(tableNames.get(CHECKPOINT_TABLE_KEY), createMySQLStrings.get(MYSQL_CREATE_TABLE_CHECKPOINTDATA)); @@ -245,7 +274,7 @@ protected Map getSharedQueryMap(IBatchConfig batchConfig) throws @Override protected void setSchemaOnConnection(Connection connection) throws SQLException { PreparedStatement ps = null; - ps = connection.prepareStatement(queryStrings.get(Q_SET_SCHEMA)); + ps = connection.prepareStatement("USE " + schema); ps.executeUpdate(); ps.close(); } @@ -254,7 +283,7 @@ protected void setSchemaOnConnection(Connection connection) throws SQLException * Method invoked to insert the MySql create table strings into a hashmap **/ - protected Map setCreateMySQLStringsMap (IBatchConfig batchConfig) { + private Map setCreateMySQLStringsMap (Map tableNames) { createMySQLStrings = new HashMap<>(); createMySQLStrings.put(MYSQL_CREATE_TABLE_CHECKPOINTDATA, "CREATE TABLE " diff --git a/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/OraclePersistenceManager.java b/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/OraclePersistenceManager.java index fde74d41684..6e6cafe9961 100644 --- a/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/OraclePersistenceManager.java +++ b/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/OraclePersistenceManager.java @@ -1,24 +1,53 @@ /* - * Copyright (c) 2014, 2015, 2016 Payara Foundation. All rights reserved. - - * The contents of this file are subject to the terms of the Common Development + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2014-2018 Payara Foundation and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can * obtain a copy of the License at - * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html - * or packager/legal/LICENSE.txt. See the License for the specific + * https://github.com/payara/Payara/blob/master/LICENSE.txt + * See the License for the specific * language governing permissions and limitations under the License. - + * * When distributing the software, include this License Header Notice in each - * file and include the License file at packager/legal/LICENSE.txt. + * file and include the License file at glassfish/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * The Payara Foundation designates this particular file as subject to the "Classpath" + * exception as provided by the Payara Foundation in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. */ package fish.payara.jbatch.persistence.rdbms; import com.ibm.jbatch.container.exception.BatchContainerServiceException; -import com.ibm.jbatch.container.exception.PersistenceException; -import com.ibm.jbatch.container.jobinstance.JobInstanceImpl; import com.ibm.jbatch.spi.services.IBatchConfig; +import static fish.payara.jbatch.persistence.rdbms.JDBCQueryConstants.CHECKPOINT_TABLE_KEY; +import static fish.payara.jbatch.persistence.rdbms.JDBCQueryConstants.EXECUTION_INSTANCE_TABLE_KEY; +import static fish.payara.jbatch.persistence.rdbms.JDBCQueryConstants.JOB_INSTANCE_TABLE_KEY; +import static fish.payara.jbatch.persistence.rdbms.JDBCQueryConstants.JOB_STATUS_TABLE_KEY; +import static fish.payara.jbatch.persistence.rdbms.JDBCQueryConstants.STEP_EXECUTION_INSTANCE_TABLE_KEY; +import static fish.payara.jbatch.persistence.rdbms.JDBCQueryConstants.STEP_STATUS_TABLE_KEY; import java.sql.Connection; import java.sql.DatabaseMetaData; @@ -31,12 +60,13 @@ import java.util.logging.Level; import java.util.logging.Logger; -import javax.batch.runtime.JobInstance; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; -import org.glassfish.batch.spi.impl.BatchRuntimeHelper; +import org.glassfish.batch.spi.impl.BatchRuntimeConfiguration; +import static org.glassfish.batch.spi.impl.BatchRuntimeHelper.PAYARA_TABLE_PREFIX_PROPERTY; +import static org.glassfish.batch.spi.impl.BatchRuntimeHelper.PAYARA_TABLE_SUFFIX_PROPERTY; /** * @@ -98,11 +128,12 @@ public void init(IBatchConfig batchConfig) this.batchConfig = batchConfig; - schema = batchConfig.getDatabaseConfigurationBean().getSchema(); - - jndiName = batchConfig.getDatabaseConfigurationBean().getJndiName(); - - + schema = batchConfig.getDatabaseConfigurationBean().getSchema(); + jndiName = batchConfig.getDatabaseConfigurationBean().getJndiName(); + prefix = batchConfig.getConfigProperties().getProperty(PAYARA_TABLE_PREFIX_PROPERTY, ""); + suffix = batchConfig.getConfigProperties().getProperty(PAYARA_TABLE_SUFFIX_PROPERTY, ""); + + if (jndiName == null || jndiName.equals("")) { throw new BatchContainerServiceException( "JNDI name is not defined."); @@ -122,8 +153,8 @@ public void init(IBatchConfig batchConfig) // Load the table names and queries shared between different database // types - tableNames = getSharedTableMap(batchConfig); - oracleObjectNames = getOracleObjectsMap(batchConfig); + tableNames = getSharedTableMap(); + oracleObjectNames = getOracleObjectsMap(); try { queryStrings = getSharedQueryMap(batchConfig); @@ -136,11 +167,11 @@ public void init(IBatchConfig batchConfig) try { - if (!isOracleSchemaValid()) { + if (!isSchemaValid()) { setDefaultSchema(); } - checkOracleTables(); + checkOracleTables(); } catch (SQLException e) { logger.severe(e.getLocalizedMessage()); @@ -155,7 +186,8 @@ public void init(IBatchConfig batchConfig) * @return * @throws SQLException */ - private boolean isOracleSchemaValid() throws SQLException { + @Override + protected boolean isSchemaValid() throws SQLException { logger.entering(CLASSNAME, "isOracleSchemaValid"); boolean result = false; @@ -193,11 +225,8 @@ private boolean isOracleSchemaValid() throws SQLException { * @throws SQLException */ private void checkOracleTables() throws SQLException { - // put the create table strings into a hashmap - createOracleTableStrings = setOracleTableMap(batchConfig); - - // put the create index strings into a hashmap - createOracleIndexStrings = setOracleIndexMap(batchConfig); + setOracleTableMap(); + setOracleIndexMap(); logger.entering(CLASSNAME, "checkOracleTables"); createOracleTableNotExists(tableNames.get(CHECKPOINT_TABLE_KEY), @@ -250,6 +279,25 @@ private void checkOracleTables() throws SQLException { logger.exiting(CLASSNAME, "checkOracleTables"); } + + @Override + public void createTables(DataSource dataSource, BatchRuntimeConfiguration batchRuntimeConfiguration){ + this.dataSource = dataSource; + prefix = batchRuntimeConfiguration.getTablePrefix(); + suffix = batchRuntimeConfiguration.getTableSuffix(); + schema = batchRuntimeConfiguration.getSchemaName(); + tableNames = getSharedTableMap(); + oracleObjectNames = getOracleObjectsMap(); + + try { + if (!isSchemaValid()) { + setDefaultSchema(); + } + checkOracleTables(); + } catch (SQLException ex) { + logger.severe(ex.getLocalizedMessage()); + } + } /** * Create the jbatch tables if they do not exist. @@ -444,11 +492,7 @@ public boolean checkOracleIndexExists(String indexname, String defaultIndexname, * strings into a hashmap **/ - protected Map setOracleTableMap(IBatchConfig batchConfig) { - String prefix = batchConfig.getConfigProperties().getProperty( - BatchRuntimeHelper.PAYARA_TABLE_PREFIX_PROPERTY, ""); - String suffix = batchConfig.getConfigProperties().getProperty( - BatchRuntimeHelper.PAYARA_TABLE_SUFFIX_PROPERTY, ""); + private Map setOracleTableMap() { createOracleTableStrings = new HashMap<>(); createOracleTableStrings.put(CREATE_TABLE_CHECKPOINTDATA, "CREATE TABLE " + tableNames.get(CHECKPOINT_TABLE_KEY) @@ -540,7 +584,7 @@ protected Map setOracleTableMap(IBatchConfig batchConfig) { * Method invoked to insert the Oracle create index strings into a hashmap **/ - protected Map setOracleIndexMap(IBatchConfig batchConfig) { + private Map setOracleIndexMap() { createOracleIndexStrings = new HashMap<>(); createOracleIndexStrings.put( CREATE_CHECKPOINTDATA_INDEX, @@ -549,11 +593,7 @@ protected Map setOracleIndexMap(IBatchConfig batchConfig) { return createOracleIndexStrings; } - protected Map getOracleObjectsMap(IBatchConfig batchConfig) { - String prefix = batchConfig.getConfigProperties().getProperty( - BatchRuntimeHelper.PAYARA_TABLE_PREFIX_PROPERTY, ""); - String suffix = batchConfig.getConfigProperties().getProperty( - BatchRuntimeHelper.PAYARA_TABLE_SUFFIX_PROPERTY, ""); + protected Map getOracleObjectsMap() { Map result = new HashMap(7); result.put(JOBINSTANCEDATA_SEQ_KEY, prefix + JOBINSTANCEDATA_SEQ_KEY + suffix); diff --git a/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/PostgresPersistenceManager.java b/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/PostgresPersistenceManager.java index 1c47767633f..c32dc7f283d 100644 --- a/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/PostgresPersistenceManager.java +++ b/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/PostgresPersistenceManager.java @@ -1,16 +1,41 @@ /* - * Copyright (c) 2014, 2016 Payara Foundation. All rights reserved. - - * The contents of this file are subject to the terms of the Common Development + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2014-2018 Payara Foundation and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can * obtain a copy of the License at - * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html - * or packager/legal/LICENSE.txt. See the License for the specific + * https://github.com/payara/Payara/blob/master/LICENSE.txt + * See the License for the specific * language governing permissions and limitations under the License. - + * * When distributing the software, include this License Header Notice in each - * file and include the License file at packager/legal/LICENSE.txt. + * file and include the License file at glassfish/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * The Payara Foundation designates this particular file as subject to the "Classpath" + * exception as provided by the Payara Foundation in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. */ package fish.payara.jbatch.persistence.rdbms; @@ -42,6 +67,8 @@ import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; +import static org.glassfish.batch.spi.impl.BatchRuntimeHelper.PAYARA_TABLE_PREFIX_PROPERTY; +import static org.glassfish.batch.spi.impl.BatchRuntimeHelper.PAYARA_TABLE_SUFFIX_PROPERTY; /** * PostgreSQL Persistence Manager @@ -75,7 +102,7 @@ protected Map getSharedQueryMap(IBatchConfig batchConfig) throws @Override protected void setSchemaOnConnection(Connection connection) throws SQLException { PreparedStatement ps = null; - ps = connection.prepareStatement(queryStrings.get(Q_SET_SCHEMA)); + ps = connection.prepareStatement("set search_path to " + schema); ps.executeUpdate(); ps.close(); } @@ -89,8 +116,9 @@ public void init(IBatchConfig batchConfig) this.batchConfig = batchConfig; schema = batchConfig.getDatabaseConfigurationBean().getSchema(); - jndiName = batchConfig.getDatabaseConfigurationBean().getJndiName(); + prefix = batchConfig.getConfigProperties().getProperty(PAYARA_TABLE_PREFIX_PROPERTY, ""); + suffix = batchConfig.getConfigProperties().getProperty(PAYARA_TABLE_SUFFIX_PROPERTY, ""); try { Context ctx = new InitialContext(); @@ -106,7 +134,7 @@ public void init(IBatchConfig batchConfig) // Load the table names and queries shared between different database // types - tableNames = getSharedTableMap(batchConfig); + tableNames = getSharedTableMap(); try { queryStrings = getSharedQueryMap(batchConfig); @@ -114,10 +142,7 @@ public void init(IBatchConfig batchConfig) // TODO Auto-generated catch block throw new BatchContainerServiceException(e1); } - // put the create table strings into a hashmap - // createTableStrings = setCreateTableMap(batchConfig); - createPostgresStrings = setCreatePostgresStringsMap(batchConfig); logger.config("JNDI name = " + jndiName); @@ -126,13 +151,11 @@ public void init(IBatchConfig batchConfig) "JNDI name is not defined."); } - - try { - if (!isPostgresSchemaValid()) { + if (!isSchemaValid()) { setDefaultSchema(); } - checkPostgresTables(); + checkTables(); } catch (SQLException e) { logger.severe(e.getLocalizedMessage()); @@ -148,7 +171,8 @@ public void init(IBatchConfig batchConfig) * @return * @throws SQLException */ - private boolean isPostgresSchemaValid() throws SQLException { + @Override + protected boolean isSchemaValid() throws SQLException { boolean result = false; Connection conn = null; @@ -187,9 +211,10 @@ private boolean isPostgresSchemaValid() throws SQLException { * * @throws SQLException */ - private void checkPostgresTables() throws SQLException { + @Override + protected void checkTables () throws SQLException { logger.entering(CLASSNAME, "checkPostgresTables Postgres"); - + setCreatePostgresStringsMap(tableNames); createPostgresTableNotExists(tableNames.get(CHECKPOINT_TABLE_KEY), createPostgresStrings.get(POSTGRES_CREATE_TABLE_CHECKPOINTDATA)); @@ -270,8 +295,7 @@ protected void createPostgresTableNotExists(String tableName, * Method invoked to insert the Postgres create table strings into a hashmap **/ - protected Map setCreatePostgresStringsMap( - IBatchConfig batchConfig) { + private Map setCreatePostgresStringsMap(Map tableNames) { createPostgresStrings = new HashMap<>(); createPostgresStrings.put(POSTGRES_CREATE_TABLE_CHECKPOINTDATA, "CREATE TABLE " + tableNames.get(CHECKPOINT_TABLE_KEY) diff --git a/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/SQLServerPersistenceManager.java b/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/SQLServerPersistenceManager.java index d971d96b46b..21068b7998e 100644 --- a/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/SQLServerPersistenceManager.java +++ b/appserver/batch/glassfish-batch-connector/src/main/java/fish/payara/jbatch/persistence/rdbms/SQLServerPersistenceManager.java @@ -1,19 +1,41 @@ /* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright (c) 2016 Payara Foundation. All rights reserved. - * - * The contents of this file are subject to the terms of the Common Development - * and Distribution License("CDDL") (collectively, the "License"). You - * may not use this file except in compliance with the License. You can - * obtain a copy of the License at - * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html - * or packager/legal/LICENSE.txt. See the License for the specific - * language governing permissions and limitations under the License. - * - * When distributing the software, include this License Header Notice in each - * file and include the License file at packager/legal/LICENSE.txt. - * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2016-2018 Payara Foundation and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * https://github.com/payara/Payara/blob/master/LICENSE.txt + * See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at glassfish/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * The Payara Foundation designates this particular file as subject to the "Classpath" + * exception as provided by the Payara Foundation in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. */ package fish.payara.jbatch.persistence.rdbms; @@ -33,7 +55,6 @@ import static fish.payara.jbatch.persistence.rdbms.SQLServerJDBCConstants.SQLSERVER_CREATE_TABLE_STEPINSTANCEDATA; import static fish.payara.jbatch.persistence.rdbms.SQLServerJDBCConstants.SQLSERVER_CREATE_TABLE_STEPSTATUS; import java.sql.Connection; -import java.sql.DatabaseMetaData; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -45,7 +66,9 @@ import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; -import org.glassfish.batch.spi.impl.BatchRuntimeHelper; +import org.glassfish.batch.spi.impl.BatchRuntimeConfiguration; +import static org.glassfish.batch.spi.impl.BatchRuntimeHelper.PAYARA_TABLE_PREFIX_PROPERTY; +import static org.glassfish.batch.spi.impl.BatchRuntimeHelper.PAYARA_TABLE_SUFFIX_PROPERTY; public class SQLServerPersistenceManager extends JBatchJDBCPersistenceManager implements SQLServerJDBCConstants { @@ -63,6 +86,8 @@ public void init(IBatchConfig batchConfig) throws BatchContainerServiceException schema = batchConfig.getDatabaseConfigurationBean().getSchema(); jndiName = batchConfig.getDatabaseConfigurationBean().getJndiName(); + prefix = batchConfig.getConfigProperties().getProperty(PAYARA_TABLE_PREFIX_PROPERTY, ""); + suffix = batchConfig.getConfigProperties().getProperty(PAYARA_TABLE_SUFFIX_PROPERTY, ""); if (null == jndiName || jndiName.isEmpty()) { throw new BatchContainerServiceException("JNDI name is not defined."); @@ -83,8 +108,8 @@ public void init(IBatchConfig batchConfig) throws BatchContainerServiceException } // Load the table names and queries shared between different database types - tableNames = getSharedTableMap(batchConfig); - schemaTableNames = getSharedSchemaTableMap(batchConfig); + tableNames = getSharedTableMap(); + schemaTableNames = getSharedSchemaTableMap(); try { queryStrings = getSQLServerSharedQueryMap(batchConfig); @@ -92,12 +117,12 @@ public void init(IBatchConfig batchConfig) throws BatchContainerServiceException throw new BatchContainerServiceException(e1); } - SQLServerCreateStrings = setCreateSQLServerStringsMap(batchConfig); + //SQLServerCreateStrings = setCreateSQLServerStringsMap(batchConfig); LOGGER.log(Level.CONFIG, "JNDI name = {0}", jndiName); try { - if (!isSQLServerSchemaValid()) { + if (!isSchemaValid()) { setDefaultSchema(); } checkSQLServerTables(); @@ -115,7 +140,8 @@ public void init(IBatchConfig batchConfig) throws BatchContainerServiceException * @return * @throws SQLException */ - private boolean isSQLServerSchemaValid() throws SQLException { + @Override + protected boolean isSchemaValid() throws SQLException { LOGGER.entering(CLASSNAME, "isSQLServerSchemaValid"); boolean result = false; @@ -151,7 +177,7 @@ private boolean isSQLServerSchemaValid() throws SQLException { private void checkSQLServerTables() throws SQLException { LOGGER.entering(CLASSNAME, "checkSQLServerTables"); - + setCreateSQLServerStringsMap(); createSQLServerTableIfNotExists(tableNames.get(CHECKPOINT_TABLE_KEY), SQLServerCreateStrings.get(SQLSERVER_CREATE_TABLE_CHECKPOINTDATA)); @@ -173,6 +199,24 @@ private void checkSQLServerTables() throws SQLException { LOGGER.exiting(CLASSNAME, "checkSQLServerTables"); } + @Override + public void createTables(DataSource dataSource, BatchRuntimeConfiguration batchRuntimeConfiguration){ + this.dataSource = dataSource; + prefix = batchRuntimeConfiguration.getTablePrefix(); + suffix = batchRuntimeConfiguration.getTableSuffix(); + schema = batchRuntimeConfiguration.getSchemaName(); + tableNames = getSharedTableMap(); + schemaTableNames = getSharedSchemaTableMap(); + + try { + if (!isSchemaValid()) { + setDefaultSchema(); + } + checkSQLServerTables(); + } catch (SQLException ex) { + LOGGER.severe(ex.getLocalizedMessage()); + } + } /** * Create the jbatch tables if they do not exist. * @param tableName @@ -222,13 +266,7 @@ protected void createSQLServerTableIfNotExists(String tableName, } - protected Map getSharedSchemaTableMap(IBatchConfig batchConfig) { - String prefix = batchConfig.getConfigProperties().getProperty( - BatchRuntimeHelper.PAYARA_TABLE_PREFIX_PROPERTY, ""); - String suffix = batchConfig.getConfigProperties().getProperty( - BatchRuntimeHelper.PAYARA_TABLE_SUFFIX_PROPERTY, ""); - String schema = batchConfig.getDatabaseConfigurationBean().getSchema(); - + protected Map getSharedSchemaTableMap() { String schemaPrefix; if(schema == null || schema.isEmpty()) { schemaPrefix = ""; @@ -259,10 +297,9 @@ protected void setSchemaOnConnection(Connection connection){ /** * Method invoked to insert the MySql create table strings into a hashmap - * @param batchConfig * @return SQLServerCreateStrings **/ - protected Map setCreateSQLServerStringsMap (IBatchConfig batchConfig) { + private Map setCreateSQLServerStringsMap () { SQLServerCreateStrings = new HashMap<>();