Skip to content

Commit

Permalink
INTSAMPLES-148: Make JPA Sample as Boot App
Browse files Browse the repository at this point in the history
JIRA: https://jira.spring.io/browse/INTSAMPLES-148

Upgrade several dependencies, especially OpenJPA-2.4.0 to support Java 8 Class Transformation

Upgrade to DSL-1.1.2 and other polishing

Upgrade to SI-4.3.0.M1
  • Loading branch information
artembilan committed Mar 7, 2016
1 parent b52ee9e commit 6080639
Show file tree
Hide file tree
Showing 22 changed files with 233 additions and 249 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,8 +18,10 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.messaging.Message;
Expand All @@ -33,7 +35,7 @@
*/
public class FtpInboundChannelAdapterSample {

private static final Logger LOGGER = Logger.getLogger(FtpInboundChannelAdapterSample.class);
private static final Logger LOGGER = LoggerFactory.getLogger(FtpInboundChannelAdapterSample.class);

@Test
public void runDemo() throws Exception{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,10 +19,12 @@

import java.io.File;
import java.io.InputStream;
import org.apache.log4j.Logger;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.messaging.Message;
Expand All @@ -37,7 +39,7 @@
*/
public class FtpOutboundChannelAdapterSample {

private static final Logger LOGGER = Logger.getLogger(FtpOutboundChannelAdapterSample.class);
private static final Logger LOGGER = LoggerFactory.getLogger(FtpOutboundChannelAdapterSample.class);

private final File baseFolder = new File("target" + File.separator + "toSend");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,13 +24,15 @@
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.log4j.Logger;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.integration.samples.ftp.support.TestUserManager;
import org.springframework.integration.test.util.SocketUtils;

Expand All @@ -50,7 +52,7 @@
})
public class TestSuite {

private static final Logger LOGGER = Logger.getLogger(TestSuite.class);
private static final Logger LOGGER = LoggerFactory.getLogger(TestSuite.class);

public static final String FTP_ROOT_DIR = "target" + File.separator + "ftproot";
public static final String LOCAL_FTP_TEMP_DIR = "target" + File.separator + "local-ftp-temp";
Expand Down
12 changes: 6 additions & 6 deletions basic/jpa/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This sample illustrates how the JPA Components can be used. The example presente
* List all people from the database
* Create a new Person record in the database

The first example demonstrates the use of an JPA Outbound gateway to retrieve a list of people. The second example uses an JPA Outbound Gateway in order to create a new Person record and then return the newly created Person record.
The first example demonstrates the use of an JPA Outbound gateway to retrieve a list of people. The second example uses an JPA Outbound Gateway in order to create a new Person record and then return the newly created Person record.

You have the option to choose between the following 3 persistence providers:

Expand All @@ -18,18 +18,18 @@ You have the option to choose between the following 3 persistence providers:

# Getting Started

Hibernate works out of the box and there are 2 options on how to execute the sample:
Hibernate works out of the box and there are 2 options on how to execute the sample:

* running the "Main" class from within STS (Right-click on Main class --> Run As --> Java Application)
* or from the command line:
$ gradlew :jpa:run

For **OpenJPA** and **EclipseLink** to work, you must provide a [Java Agent](http://docs.oracle.com/javase/6/docs/api/java/lang/instrument/package-summary.html).
When using the Gradle Application Plugin, this is taken care of for you behind the scenes automatically. However,
For **OpenJPA** and **EclipseLink** to work, you must provide a [Java Agent](http://docs.oracle.com/javase/6/docs/api/java/lang/instrument/package-summary.html).
When using the Gradle Application Plugin, this is taken care of for you behind the scenes automatically. However,
when running the sample from within STS start the Main class with the following JVM flags:

-javaagent:/path/to/.m2/repository/org/springframework/spring-instrument/3.1.1.RELEASE/spring-instrument-3.1.1.RELEASE.jar
-javaagent:/path/to/.m2/repository/org/apache/openjpa/openjpa/2.2.0/openjpa-2.2.0.jar
-javaagent:/path/to/.m2/repository/org/springframework/spring-instrument/4.2.4.RELEASE/spring-instrument-4.2.4.RELEASE.jar
-javaagent:/path/to/.m2/repository/org/apache/openjpa/openjpa/2.4.0/openjpa-2.4.0.jar

With these flags you will be able to use all 3 persistence providers at once.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.springframework.integration.samples.jpa;

import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.ClassUtils;

/**
* @author Artem Bilan
* @since 4.2
*/
@Configuration
@ConditionalOnClass({LocalContainerEntityManagerFactoryBean.class,
EnableTransactionManagement.class, EntityManager.class})
@Conditional(EclipseLinkAutoConfiguration.EclipseLinkEntityManagerCondition.class)
@AutoConfigureAfter({DataSourceAutoConfiguration.class})
@Profile("eclipseLink")
public class EclipseLinkAutoConfiguration extends JpaBaseConfiguration {

@Override
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
return new EclipseLinkJpaVendorAdapter();
}

@Override
protected Map<String, Object> getVendorProperties() {
return new HashMap<String, Object>();
}


@Order(Ordered.HIGHEST_PRECEDENCE + 20)
static class EclipseLinkEntityManagerCondition extends SpringBootCondition {

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
if (ClassUtils.isPresent("org.eclipse.persistence.jpa.JpaEntityManager", context.getClassLoader())) {
return ConditionOutcome.match("found JpaEntityManager class");
}
else {
return ConditionOutcome.noMatch("did not find JpaEntityManager class");
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.integration.samples.jpa;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Scanner;

import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ImportResource;
import org.springframework.integration.samples.jpa.domain.Person;
import org.springframework.integration.samples.jpa.service.PersonService;
import org.springframework.util.StringUtils;
Expand All @@ -30,18 +35,16 @@
* @author Gunnar Hillert
* @author Amol Nayak
* @author Gary Russell
* @author Artem Bilan
* @version 1.0
*
*/
public final class Main {
@SpringBootApplication(exclude = HibernateJpaAutoConfiguration.class)
@ImportResource("spring-integration-context.xml")
public class Main {

private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");

/**
* Prevent instantiation.
*/
private Main() {}

/**
* Load the Spring Integration Application Context
*
Expand All @@ -68,19 +71,19 @@ public static void main(final String... args) {
System.out.println("\tq. Quit the application");
System.out.print("Enter you choice: ");

final GenericXmlApplicationContext context = new GenericXmlApplicationContext();
SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(Main.class).web(false);

while (true) {
final String input = scanner.nextLine();

if("1".equals(input.trim())) {
context.getEnvironment().setActiveProfiles("hibernate");
springApplicationBuilder.sources(HibernateJpaAutoConfiguration.class);
break;
} else if("2".equals(input.trim())) {
context.getEnvironment().setActiveProfiles("openjpa");
springApplicationBuilder.profiles("openJpa");
break;
} else if("3".equals(input.trim())) {
context.getEnvironment().setActiveProfiles("eclipselink");
springApplicationBuilder.profiles("eclipseLink");
break;
} else if("q".equals(input.trim())) {
System.out.println("Exiting application...bye.");
Expand All @@ -91,9 +94,7 @@ public static void main(final String... args) {
}
}

context.load("classpath:META-INF/spring/integration/*-context.xml");
context.registerShutdownHook();
context.refresh();
ConfigurableApplicationContext context = springApplicationBuilder.run(args);

final PersonService personService = context.getBean(PersonService.class);

Expand Down Expand Up @@ -161,10 +162,7 @@ private static void createPersonDetails(final Scanner scanner,PersonService serv
}
}
}
/**
* @param service
* @param input
*/

private static void findPeople(final PersonService service) {

System.out.println("ID NAME CREATED");
Expand All @@ -183,6 +181,6 @@ private static void findPeople(final PersonService service) {
}

System.out.println("==================================\n\n");

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.springframework.integration.samples.jpa;

import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.ClassUtils;

/**
* @author Artem Bilan
* @since 4.2
*/
@Configuration
@ConditionalOnClass({LocalContainerEntityManagerFactoryBean.class,
EnableTransactionManagement.class, EntityManager.class})
@Conditional(OpenJpaAutoConfiguration.OpenJpaEntityManagerCondition.class)
@AutoConfigureAfter({DataSourceAutoConfiguration.class})
@Profile("openJpa")
public class OpenJpaAutoConfiguration extends JpaBaseConfiguration {

@Override
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
return new OpenJpaVendorAdapter();
}

@Override
protected Map<String, Object> getVendorProperties() {
return new HashMap<String, Object>();
}


@Order(Ordered.HIGHEST_PRECEDENCE + 20)
static class OpenJpaEntityManagerCondition extends SpringBootCondition {

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
if (ClassUtils.isPresent("org.apache.openjpa.persistence.OpenJPAEntityManager", context.getClassLoader())) {
return ConditionOutcome.match("found OpenJPAEntityManager class");
}
else {
return ConditionOutcome.noMatch("did not find OpenJPAEntityManager class");
}
}

}

}
Loading

0 comments on commit 6080639

Please sign in to comment.