Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tomcat embedded #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
619 changes: 619 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
spring-one-jar
==============
==============

- curl http://localhost:8080/rest/api/people -X PUT -d "[email protected]"
- curl http://localhost:8080/rest/api/people?page=2
175 changes: 121 additions & 54 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

Expand All @@ -7,70 +9,135 @@
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-one-jar</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.version>3.1.1.RELEASE</org.springframework.version>
<org.apache.tomcat>7.0.34</org.apache.tomcat>
<org.apache.cxf.version>2.7.2</org.apache.cxf.version>
<org.springframework.version>3.2.0.RELEASE</org.springframework.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.example.Starter</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.dstovall</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<configuration>
<onejarVersion>0.97</onejarVersion>
<classifier>onejar</classifier>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${org.apache.cxf.version}</version>
</dependency>

<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.11</version>
</dependency>

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.11</version>
</dependency>

<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.2</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${org.apache.tomcat}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>${org.apache.tomcat}</version>
</dependency>
</dependencies>


<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.example.Starter</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.dstovall</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<configuration>
<onejarVersion>0.97</onejarVersion>
<classifier>onejar</classifier>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<pluginRepositories>
<pluginRepository>
<id>onejar-maven-plugin.googlecode.com</id>
<url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
</pluginRepository>
</pluginRepositories>
<pluginRepository>
<id>onejar-maven-plugin.googlecode.com</id>
<url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
</pluginRepository>
</pluginRepositories>

<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
</repositories>
</project>
7 changes: 0 additions & 7 deletions src/main/java/com/example/SimpleBean.java

This file was deleted.

56 changes: 49 additions & 7 deletions src/main/java/com/example/Starter.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,56 @@
package com.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.io.File;
import java.io.IOException;

import org.apache.catalina.Context;
import org.apache.catalina.loader.WebappLoader;
import org.apache.catalina.startup.Tomcat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

import com.example.config.AppConfig;

public class Starter {
public static void main( final String[] args ) {
final ApplicationContext context = new AnnotationConfigApplicationContext( AppConfig.class );
final SimpleBean bean = context.getBean( SimpleBean.class );
bean.print();
public class Starter {
private final static Log log = LogFactory.getLog( Starter.class );

public static void main(final String[] args) throws Exception {
final File base = createBaseDirectory();
log.info( "Using base folder: " + base.getAbsolutePath() );

final Tomcat tomcat = new Tomcat();
tomcat.setPort( 8080 );
tomcat.setBaseDir( base.getAbsolutePath() );

Context context = tomcat.addContext( "/", base.getAbsolutePath() );
Tomcat.addServlet( context, "CXFServlet", new CXFServlet() );

context.addServletMapping( "/rest/*", "CXFServlet" );
context.addApplicationListener( ContextLoaderListener.class.getName() );
context.setLoader( new WebappLoader( Thread.currentThread().getContextClassLoader() ) );

context.addParameter( "contextClass", AnnotationConfigWebApplicationContext.class.getName() );
context.addParameter( "contextConfigLocation", AppConfig.class.getName() );

tomcat.start();
tomcat.getServer().await();
}

private static File createBaseDirectory() throws IOException {
final File base = File.createTempFile( "tmp-", "" );

if( !base.delete() ) {
throw new IOException( "Cannot (re)create base folder: " + base.getAbsolutePath() );
}

if( !base.mkdir() ) {
throw new IOException( "Cannot create base folder: " + base.getAbsolutePath() );
}

return base;
}
}

48 changes: 44 additions & 4 deletions src/main/java/com/example/config/AppConfig.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,54 @@
package com.example.config;

import java.util.Arrays;

import javax.ws.rs.ext.RuntimeDelegate;

import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import com.example.SimpleBean;
import com.example.rs.JaxRsApiApplication;
import com.example.rs.PeopleRestService;
import com.example.services.PeopleService;

@Configuration
public class AppConfig {
public class AppConfig {
@Bean( destroyMethod = "shutdown" )
public SpringBus cxf() {
return new SpringBus();
}

@Bean @DependsOn( "cxf" )
public Server jaxRsServer() {
JAXRSServerFactoryBean factory = RuntimeDelegate.getInstance().createEndpoint( jaxRsApiApplication(), JAXRSServerFactoryBean.class );
factory.setServiceBeans( Arrays.< Object >asList( peopleRestService() ) );
factory.setAddress( "/" + factory.getAddress() );
factory.setProviders( Arrays.< Object >asList( jsonProvider() ) );
return factory.create();
}

@Bean
public JaxRsApiApplication jaxRsApiApplication() {
return new JaxRsApiApplication();
}

@Bean
public PeopleRestService peopleRestService() {
return new PeopleRestService();
}

@Bean
public PeopleService peopleService() {
return new PeopleService();
}

@Bean
public SimpleBean simpleBean() {
return new SimpleBean();
public JacksonJsonProvider jsonProvider() {
return new JacksonJsonProvider();
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/example/config/DispatcherConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
public class DispatcherConfig {

}
43 changes: 43 additions & 0 deletions src/main/java/com/example/model/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.model;

public class Person {
private String email;
private String firstName;
private String lastName;

public Person() {
}

public Person( final String email ) {
this.email = email;
}

public Person( final String firstName, final String lastName ) {
this.firstName = firstName;
this.lastName = lastName;
}

public String getEmail() {
return email;
}

public void setEmail( final String email ) {
this.email = email;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public void setFirstName( final String firstName ) {
this.firstName = firstName;
}

public void setLastName( final String lastName ) {
this.lastName = lastName;
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/example/rs/JaxRsApiApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.rs;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath( "api" )
public class JaxRsApiApplication extends Application {
}
Loading