Skip to content

Commit

Permalink
[Resolve #62] Add support for wildfly sysprops in camel routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Diesler committed Nov 11, 2014
1 parent 992779e commit 6da1690
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 176 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
.settings
target
*.iml
input
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.slf4j.LoggerFactory;
import org.wildfly.camel.examples.HttpRequest;

import java.io.InputStream;
import java.util.concurrent.TimeUnit;


Expand All @@ -55,8 +56,8 @@ public static Archive<?> createDeployment() {
@Test
public void testSayHelloCxfSoapRoute() throws Exception {
// Send HTTP request to greeting service sayHello webservice method
String result = HttpRequest.get(ENDPOINT_ADDRESS, "/org/wildfly/camel/examples/cxf/hello-request.xml",
10, TimeUnit.SECONDS);
InputStream input = HttpRequest.class.getResourceAsStream("/org/wildfly/camel/examples/cxf/hello-request.xml");
String result = HttpRequest.get(ENDPOINT_ADDRESS, input, 10, TimeUnit.SECONDS);

// Log SOAP response
LOG.info("*******************************");
Expand All @@ -70,8 +71,8 @@ public void testSayHelloCxfSoapRoute() throws Exception {
@Test
public void testSayGoodbyeCxfSoapRoute() throws Exception {
// Send HTTP request to greeting service sayGoodbye webservice method
String result = HttpRequest.get(ENDPOINT_ADDRESS, "/org/wildfly/camel/examples/cxf/goodbye-request.xml",
10, TimeUnit.SECONDS);
InputStream input = HttpRequest.class.getResourceAsStream("/org/wildfly/camel/examples/cxf/goodbye-request.xml");
String result = HttpRequest.get(ENDPOINT_ADDRESS, input, 10, TimeUnit.SECONDS);

// Log SOAP response
LOG.info("*******************************");
Expand Down
16 changes: 6 additions & 10 deletions examples/camel-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@
</dependency>

<!-- Test Scope -->
<dependency>
<groupId>org.wildfly.camel</groupId>
<artifactId>example-camel-common</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
Expand All @@ -95,16 +101,6 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<!-- Build -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@
*/
package org.wildfly.camel.examples.jpa;

import org.wildfly.camel.examples.jpa.model.Customer;
import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import java.util.List;

import org.wildfly.camel.examples.jpa.model.Customer;


public class CustomerRepository {

@Inject
private EntityManager em;

Expand All @@ -37,7 +39,6 @@ public class CustomerRepository {
*
* @return A list of customers
*/
@SuppressWarnings("unchecked")
public List<Customer> findAllCustomers() {

CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,30 @@
*/
package org.wildfly.camel.examples.jpa;

import javax.ejb.Startup;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.cdi.ContextName;
import org.apache.camel.component.jpa.JpaEndpoint;
import org.apache.camel.model.dataformat.JaxbDataFormat;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.jta.JtaTransactionManager;
import org.wildfly.camel.examples.jpa.model.Customer;

import javax.ejb.Startup;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;

@Startup
@ApplicationScoped
@ContextName("cdi-context")
@ContextName("jpa-cdi-context")
public class JpaRouteBuilder extends RouteBuilder {

@Inject
private EntityManager em;

@Override
public void configure() throws Exception {

// Configure our JaxbDataFormat to point at our 'model' package
JaxbDataFormat jaxbDataFormat = new JaxbDataFormat();
jaxbDataFormat.setContextPath(Customer.class.getPackage().getName());
Expand All @@ -68,7 +68,7 @@ public void configure() throws Exception {
* unmarshall XML file content to a Customer entity and then use the JPA endpoint
* to persist the it to the 'ExampleDS' datasource (see standalone.camel.xml for datasource config).
*/
from("file://input/customers")
from("file://{{jboss.server.data.dir}}/customers")
.unmarshal(jaxbDataFormat)
.to(jpaEndpoint)
.to("log:input?showAll=true");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import javax.persistence.PersistenceContext;

public class Resources {
@SuppressWarnings("unused")
@Produces
@PersistenceContext
private EntityManager em;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,50 @@
*/
package org.wildfly.camel.examples.jpa;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.cdi.ContextName;
import org.wildfly.camel.examples.jpa.CustomerRepository;
import org.wildfly.camel.examples.jpa.model.Customer;

@SuppressWarnings("serial")
@WebServlet(name = "HttpServiceServlet", urlPatterns = { "/customers/*" }, loadOnStartup = 1)
public class SimpleServlet extends HttpServlet
{
@Inject
private CustomerRepository customerRepository;
public class SimpleServlet extends HttpServlet {

static Path CUSTOMERS_PATH = new File(System.getProperty("jboss.server.data.dir")).toPath().resolve("customers");

@Inject
private CustomerRepository customerRepository;

@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);

// Copy WEB-INF/customer.xml to the data dir
ServletContext servletContext = config.getServletContext();
try {
InputStream input = servletContext.getResourceAsStream("/WEB-INF/customer.xml");
Path xmlPath = CUSTOMERS_PATH.resolve("customer.xml");
Files.copy(input, xmlPath);
} catch (IOException ex) {
throw new ServletException(ex);
}
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
* Simple servlet to retrieve all customers from the in memory database for
* output and display on customers.jsp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@ limitations under the License.
<persistence-unit name="camel">
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
<class>org.wildfly.camel.examples.jpa.model.Customer</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
45 changes: 3 additions & 42 deletions examples/camel-jpa/src/main/webapp/WEB-INF/customers.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,6 @@
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Wildfly Camel Examples :: JPA</title>
<style type="text/css">
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
}
table {
width: 500px;
margin: 5px;
padding: 2px;
border: solid #000000 1px;
}
td {
text-align: center;
}
</style>
<script type="text/javascript">
function refresh() {
window.location.reload(true);
}
setTimeout(refresh, 5000);
</script>
</head>
<body>
<table>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
<c:forEach var="customer" items="${customers}">
<tr>
<td>${customer.id}</td>
<td>${customer.firstName}</td>
<td>${customer.lastName}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
<c:forEach var="customer" items="${customers}">
${customer.firstName} ${customer.lastName}
</c:forEach>
Original file line number Diff line number Diff line change
Expand Up @@ -19,79 +19,28 @@
*/
package org.wildfly.camel.examples.jpa;

import org.apache.camel.Exchange;
import org.apache.camel.converter.jaxb.JaxbDataFormat;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.jboss.arquillian.container.test.api.Deployment;
import java.net.MalformedURLException;
import java.util.concurrent.TimeUnit;

import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.camel.examples.jpa.model.Customer;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.wildfly.camel.examples.HttpRequest;


@RunAsClient
@RunWith(Arquillian.class)
public class JPAIntegrationTest extends CamelTestSupport {

@Deployment
public static Archive<?> createDeployment() {
StringAsset jaxbIndex = new StringAsset("Customer");

return ShrinkWrap.create(WebArchive.class, "test.war")
.addPackage(Customer.class.getPackage())
.addPackage(CamelTestSupport.class.getPackage())
.addPackage(JpaRouteBuilder.class.getPackage())
.addPackage(JaxbDataFormat.class.getPackage())
.addAsResource(jaxbIndex, "org/wildfly/camel/examples/jpa/model/jaxb.index")
.addAsResource("customer.xml", "org/wildfly/camel/examples/jpa/customer.xml")
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
}

@Inject
EntityManager em;
public class JPAIntegrationTest {

@Test
public void testFileToJpaRoute() throws Exception {
// Send a test customer XML file to our file endpoint
template.sendBodyAndHeader("file://input/customers", readResourceFromClasspath("customer.xml"),
Exchange.FILE_NAME, "customer.xml");

// Wait for the file to be consumed
Thread.sleep(2000);

// Query the in memory database customer table to verify that a record was saved
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Long> query = criteriaBuilder.createQuery(Long.class);
query.select(criteriaBuilder.count(query.from(Customer.class)));

long recordCount = em.createQuery(query).getSingleResult();

Assert.assertEquals(1L, recordCount);
String res = HttpRequest.get(getEndpointAddress("/example-camel-jpa/customers"), 10, TimeUnit.SECONDS);
Assert.assertEquals("John Doe", res.trim());
}

private String readResourceFromClasspath(String resourcePath) throws IOException {
StringBuilder builder = new StringBuilder();

BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(resourcePath), "UTF-8"));
for (int c = br.read(); c != -1; c = br.read()) {
builder.append((char) c);
}

return builder.toString();
private String getEndpointAddress(String contextPath) throws MalformedURLException {
return "http://localhost:8080" + contextPath;
}

}
13 changes: 0 additions & 13 deletions examples/camel-jpa/src/test/resources/jboss-ds.xml

This file was deleted.

14 changes: 0 additions & 14 deletions examples/camel-jpa/src/test/resources/test-persistence.xml

This file was deleted.

Loading

0 comments on commit 6da1690

Please sign in to comment.