Skip to content

Commit

Permalink
Merge pull request #1710 from lat-lon/migrateSpring-1690
Browse files Browse the repository at this point in the history
Upgrading Spring, Spring Boot and Spring Batch
  • Loading branch information
stephanr authored Jun 7, 2024
2 parents 8cb6c31 + 4b42448 commit ad14c62
Show file tree
Hide file tree
Showing 17 changed files with 246 additions and 89 deletions.
4 changes: 4 additions & 0 deletions deegree-tools/deegree-tools-gml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@
<artifactId>jaxb-runtime</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
</dependency>
<!-- test -->
<dependency>
<groupId>org.springframework</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@SpringBootApplication
public class GmlToolsApp {

public static void main(String[] args) {
public static void main(String[] args) throws Exception {
if (args.length == 0 || "--help".equals(args[0]) || "-help".equals(args[0]) || "-h".equals(args[0])) {
printUsage();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*----------------------------------------------------------------------------
This file is part of deegree
Copyright (C) 2001-2024 by:
- Department of Geography, University of Bonn -
and
- lat/lon GmbH -
and others
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact information:
e-mail: info@deegree.org
website: http://www.deegree.org/
----------------------------------------------------------------------------*/
package org.deegree.tools.featurestoresql;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.jdbc.support.JdbcTransactionManager;

import javax.sql.DataSource;

/**
* @author <a href="mailto:goltz@lat-lon.de">Lyn Goltz </a>
*/
@Configuration
public class JobRepositoryConfiguration {

@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
return builder.setType(EmbeddedDatabaseType.HSQL)
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.generateUniqueName(true)
.build();
}

@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*----------------------------------------------------------------------------
This file is part of deegree
Copyright (C) 2001-2024 by:
- Department of Geography, University of Bonn -
and
- lat/lon GmbH -
and others
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact information:
e-mail: info@deegree.org
website: http://www.deegree.org/
----------------------------------------------------------------------------*/
package org.deegree.tools.featurestoresql;

import java.util.HashMap;
import java.util.Map;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;

/**
* @author <a href="mailto:goltz@lat-lon.de">Lyn Goltz </a>
*/
public abstract class SubcommandApp {

protected static void runJob(String[] args, ApplicationContext applicationContext) throws Exception {
JobLauncher jobLauncher = applicationContext.getBean(JobLauncher.class);
Job job = applicationContext.getBean(Job.class);
Map<String, JobParameter<?>> jobParams = createJobParams(args);
jobLauncher.run(job, new JobParameters(jobParams));
}

private static Map<String, JobParameter<?>> createJobParams(String[] args) {
Map<String, JobParameter<?>> jobParams = new HashMap<>();
for (String arg : args) {
if (arg.startsWith("-")) {
int firstIndex = arg.startsWith("-- ") ? 2 : 1;
String key = arg.substring(firstIndex, arg.indexOf("="));
if (arg.contains("=")) {
String value = arg.substring(arg.indexOf("=") + 1);
jobParams.put(key, new JobParameter<>(value, String.class));
}
else {
jobParams.put(key, new JobParameter<>(true, Boolean.class));
}
}
}
return jobParams;
}

protected static boolean isHelpRequested(String[] args) {
return args.length == 1
|| (args.length > 1 && ("--help".equals(args[1]) || "-help".equals(args[1]) || "-h".equals(args[1])));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
*/
package org.deegree.tools.featurestoresql.config;

import java.util.regex.Pattern;
import org.deegree.commons.xml.stax.IndentingXMLStreamWriter;
import org.deegree.cs.persistence.CRSManager;
import org.deegree.cs.refs.coordinatesystem.CRSRef;
Expand All @@ -35,6 +34,7 @@
import org.deegree.sqldialect.oracle.OracleDialect;
import org.deegree.sqldialect.postgis.PostGISDialect;
import org.slf4j.Logger;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;

import javax.xml.stream.XMLOutputFactory;
Expand All @@ -50,6 +50,7 @@
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

import static org.deegree.feature.types.property.GeometryPropertyType.CoordinateDimension.DIM_2;
import static org.slf4j.LoggerFactory.getLogger;
Expand All @@ -73,11 +74,11 @@ public FeatureStoreConfigWriter(LoadParameter loadParameter) {
}

@Override
public void write(List<? extends AppSchema> appSchemas) throws Exception {
public void write(Chunk<? extends AppSchema> appSchemas) throws Exception {
if (appSchemas.isEmpty())
return;

AppSchema appSchema = appSchemas.get(0);
AppSchema appSchema = appSchemas.getItems().get(0);

CRSRef storageCrs = CRSManager.getCRSRef("EPSG:" + loadParameter.getSrid());
GeometryStorageParams geometryParams = new GeometryStorageParams(storageCrs, loadParameter.getSrid(), DIM_2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,32 @@
*/
package org.deegree.tools.featurestoresql.config;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;

import org.deegree.tools.featurestoresql.CommonConfiguration;
import org.deegree.tools.featurestoresql.JobRepositoryConfiguration;
import org.deegree.tools.featurestoresql.SubcommandApp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
* Entry point of the command line interface of SqlFeatureStoreConfigCreator.
*
* @author <a href="mailto:goltz@lat-lon.de">Lyn Goltz </a>
*/
public class SqlFeatureStoreConfigCreatorApp {
public class SqlFeatureStoreConfigCreatorApp extends SubcommandApp {

public static void run(String[] args) {
if (args.length == 1
|| (args.length > 1 && ("--help".equals(args[1]) || "-help".equals(args[1]) || "-h".equals(args[1])))) {
public static void run(String[] args) throws Exception {
if (isHelpRequested(args)) {
SqlFeatureStoreConfigCreatorUsagePrinter.printUsage();
}
else if (args.length == 1) {
printUnexpectedNumberOfParameters();
SqlFeatureStoreConfigCreatorUsagePrinter.printUsage();
}
else {
SpringApplication app = new SpringApplication(CommonConfiguration.class,
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(
JobRepositoryConfiguration.class, CommonConfiguration.class,
SqlFeatureStoreConfigCreatorConfiguration.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
runJob(args, applicationContext);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.SimpleStepBuilder;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.support.JdbcTransactionManager;

/**
* Configuration of the FeatureStoreLoader.
Expand All @@ -43,12 +45,6 @@
@EnableBatchProcessing
public class SqlFeatureStoreConfigCreatorConfiguration {

@Autowired
private JobBuilderFactory jobBuilderFactory;

@Autowired
private StepBuilderFactory stepBuilderFactory;

@StepScope
@Bean
public LoadParameter parseJobParameter(@Value("#{jobParameters[schemaUrl]}") String schemaUrl,
Expand Down Expand Up @@ -86,18 +82,19 @@ public FeatureStoreConfigWriter featureStoreConfigWriter(LoadParameter loadParam
}

@Bean
public Step step(AppSchemaReader appSchemaReader, FeatureStoreConfigWriter featureStoreConfigWriter) {
return stepBuilderFactory.get("featureStoreConfigLoaderStep")
.<AppSchema, AppSchema>chunk(1)
public Step step(AppSchemaReader appSchemaReader, FeatureStoreConfigWriter featureStoreConfigWriter,
JobRepository jobRepository, JdbcTransactionManager transactionManager) {
StepBuilder stepBuilder = new StepBuilder("featureStoreConfigLoaderStep", jobRepository);
return new SimpleStepBuilder<AppSchema, AppSchema>(stepBuilder).<AppSchema, AppSchema>chunk(1)
.transactionManager(transactionManager)
.reader(appSchemaReader)
.writer(featureStoreConfigWriter)
.build();
}

@Bean
public Job job(Step step) {
return jobBuilderFactory.get("featureStoreConfigLoaderJob")
.incrementer(new RunIdIncrementer())
public Job job(Step step, JobRepository jobRepository) {
return new JobBuilder("featureStoreConfigLoaderJob", jobRepository).incrementer(new RunIdIncrementer())
.start(step)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@
import static org.deegree.protocol.wfs.transaction.action.IDGenMode.USE_EXISTING;
import static org.slf4j.LoggerFactory.getLogger;

import java.util.List;

import org.deegree.feature.Feature;
import org.deegree.feature.FeatureCollection;
import org.deegree.feature.GenericFeatureCollection;
import org.deegree.feature.persistence.sql.SQLFeatureStore;
import org.deegree.feature.persistence.sql.SQLFeatureStoreTransaction;
import org.slf4j.Logger;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.util.Assert;

Expand Down Expand Up @@ -62,7 +61,7 @@ public FeatureStoreWriter(SQLFeatureStore sqlFeatureStore, Summary summary) {
}

@Override
public void write(List<? extends Feature> features) throws Exception {
public void write(Chunk<? extends Feature> features) throws Exception {

FeatureCollection featureCollection = new GenericFeatureCollection();
for (Feature featureToAdd : features) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,31 @@
*/
package org.deegree.tools.featurestoresql.loader;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;

import org.deegree.tools.featurestoresql.CommonConfiguration;
import org.deegree.tools.featurestoresql.JobRepositoryConfiguration;
import org.deegree.tools.featurestoresql.SubcommandApp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
* Entry point of the command line interface of GmlLoader.
*
* @author <a href="mailto:goltz@lat-lon.de">Lyn Goltz </a>
*/
public class GmlLoaderApp {
public class GmlLoaderApp extends SubcommandApp {

public static void run(String[] args) {
if (args.length == 1
|| (args.length > 1 && ("--help".equals(args[1]) || "-help".equals(args[1]) || "-h".equals(args[1])))) {
public static void run(String[] args) throws Exception {
if (isHelpRequested(args)) {
GmlLoaderHelpUsage.printUsage();
}
else if (args.length < 4) {
printUnexpectedNumberOfParameters(args);
GmlLoaderHelpUsage.printUsage();
}
else {
SpringApplication app = new SpringApplication(CommonConfiguration.class, GmlLoaderConfiguration.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(
JobRepositoryConfiguration.class, CommonConfiguration.class, GmlLoaderConfiguration.class);
runJob(args, applicationContext);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@
package org.deegree.tools.featurestoresql.loader;

import org.deegree.feature.Feature;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;

import java.util.List;

/**
* Dummy ItemWriter not writing any features.
*
Expand All @@ -34,7 +33,7 @@
public class NullWriter implements ItemWriter<Feature> {

@Override
public void write(List<? extends Feature> list) throws Exception {
public void write(Chunk<? extends Feature> list) throws Exception {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@
import java.io.PrintWriter;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.Date;

import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
Expand Down Expand Up @@ -115,7 +116,9 @@ private StepExecution getStepExecution(JobExecution jobExecution) {

private String getTimeNeeded(StepExecution stepExecution) {
if (stepExecution != null && stepExecution.getStartTime() != null) {
long millis = new Date().getTime() - stepExecution.getStartTime().getTime();
LocalDateTime startTime = stepExecution.getStartTime();
LocalDateTime endTime = LocalDateTime.now();
long millis = ChronoUnit.MILLIS.between(startTime, endTime);
long hours = MILLISECONDS.toHours(millis);
long minutes = MILLISECONDS.toMinutes(millis);
long seconds = MILLISECONDS.toSeconds(millis);
Expand Down
Loading

0 comments on commit ad14c62

Please sign in to comment.