├── .gitignore ├── README.md ├── pom.xml ├── spring-boot-osgi-demo-application ├── pom.xml └── src │ └── main │ ├── java │ └── de │ │ └── isb │ │ └── demo │ │ ├── DemoApplication.java │ │ └── DemoConfiguration.java │ └── resources │ └── application.properties ├── spring-boot-osgi-demo-common ├── pom.xml └── src │ └── main │ └── java │ └── de │ └── isb │ └── demo │ └── model │ ├── Applicant.java │ ├── Visa.java │ ├── VisaType.java │ ├── error │ └── SerializationException.java │ ├── rest │ ├── AboutResponse.java │ ├── CreateVisaResponse.java │ ├── ErrorResponse.java │ └── RestResponse.java │ └── validation │ ├── ValidationResult.java │ └── ValidationResultType.java ├── spring-boot-osgi-demo-core ├── pom.xml └── src │ ├── main │ └── java │ │ └── de │ │ └── isb │ │ └── demo │ │ ├── osgi │ │ └── OSGiInitializerBean.java │ │ └── service │ │ ├── api │ │ ├── SerializationService.java │ │ ├── ValidationService.java │ │ └── VisaService.java │ │ └── impl │ │ ├── OSGiDummyService.java │ │ ├── SerializationServiceImpl.java │ │ ├── ValidationServiceImpl.java │ │ └── VisaServiceImpl.java │ └── test │ └── java │ └── de │ └── isb │ └── demo │ ├── Fixtures.java │ └── service │ └── impl │ └── SerialitzationServiceImplTest.java └── spring-boot-osgi-demo-web ├── pom.xml └── src └── main └── java └── de └── isb └── demo └── rest ├── AboutController.java └── VisaController.java /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | *.iml 3 | .idea/ 4 | felix-cache/ 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Nothing yet 2 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | de.isb 8 | spring-boot-osgi-demo 9 | pom 10 | 1.0.0-SNAPSHOT 11 | 12 | 13 | 1.8 14 | 15 | 16 | 17 | 18 | spring-boot-osgi-demo-core 19 | spring-boot-osgi-demo-common 20 | spring-boot-osgi-demo-web 21 | spring-boot-osgi-demo-application 22 | 23 | 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-dependencies 30 | 1.5.3.BUILD-SNAPSHOT 31 | pom 32 | import 33 | 34 | 35 | de.isb 36 | spring-boot-osgi-demo-applicant 37 | ${project.version} 38 | 39 | 40 | de.isb 41 | spring-boot-osgi-demo-core 42 | ${project.version} 43 | 44 | 45 | de.isb 46 | spring-boot-osgi-demo-web 47 | ${project.version} 48 | 49 | 50 | de.isb 51 | spring-boot-osgi-demo-common 52 | ${project.version} 53 | 54 | 55 | com.fasterxml.jackson.databind 56 | jackson-core 57 | 2.8.7 58 | 59 | 60 | org.osgi 61 | org.osgi.core 62 | 6.0.0 63 | 64 | 65 | org.osgi 66 | org.osgi.service.component.annotations 67 | 1.3.0 68 | 69 | 70 | org.apache.felix 71 | org.apache.felix.framework 72 | 5.6.2 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | org.apache.maven.plugins 81 | maven-compiler-plugin 82 | 3.6.1 83 | 84 | 1.8 85 | 1.8 86 | 87 | 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-application/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-osgi-demo 7 | de.isb 8 | 1.0.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-osgi-demo-applicant 13 | 14 | 15 | 16 | de.isb 17 | spring-boot-osgi-demo-core 18 | 19 | 20 | de.isb 21 | spring-boot-osgi-demo-web 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-web 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-actuator 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-hateoas 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-devtools 42 | 43 | 44 | org.osgi 45 | org.osgi.core 46 | 47 | 48 | 49 | 50 | 51 | 52 | src/main/resources 53 | 54 | application.properties 55 | 56 | true 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-application/src/main/java/de/isb/demo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class DemoApplication { 8 | public static void main(String[] args) { 9 | SpringApplication.run(DemoConfiguration.class, args); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-application/src/main/java/de/isb/demo/DemoConfiguration.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo; 2 | 3 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 4 | import org.springframework.context.annotation.ComponentScan; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.web.servlet.config.annotation.EnableWebMvc; 7 | 8 | @Configuration 9 | @EnableAutoConfiguration 10 | @EnableWebMvc 11 | @ComponentScan(basePackages = { "de.isb.demo" }) 12 | public class DemoConfiguration { 13 | 14 | } 15 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-application/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | management.security.enabled=false 2 | demo.application.version=${project.version} 3 | demo.application.name=${project.artifactId} 4 | demo.application.data.directory=${project.build.directory}/json-store 5 | demo.application.json.pretty=true -------------------------------------------------------------------------------- /spring-boot-osgi-demo-common/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-osgi-demo 7 | de.isb 8 | 1.0.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-osgi-demo-common 13 | 14 | 15 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-common/src/main/java/de/isb/demo/model/Applicant.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo.model; 2 | 3 | public class Applicant { 4 | private String firstNames; 5 | private String lastName; 6 | 7 | public String getFirstNames() { 8 | return firstNames; 9 | } 10 | 11 | public void setFirstNames(String firstNames) { 12 | this.firstNames = firstNames; 13 | } 14 | 15 | public String getLastName() { 16 | return lastName; 17 | } 18 | 19 | public void setLastName(String lastName) { 20 | this.lastName = lastName; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-common/src/main/java/de/isb/demo/model/Visa.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo.model; 2 | 3 | public class Visa { 4 | private String id; 5 | private VisaType type; 6 | private Applicant applicant; 7 | 8 | public String getId() { 9 | 10 | return id; 11 | } 12 | 13 | public void setId(String id) { 14 | this.id = id; 15 | } 16 | 17 | public VisaType getType() { 18 | return type; 19 | } 20 | 21 | public void setType(VisaType type) { 22 | this.type = type; 23 | } 24 | 25 | public Applicant getApplicant() { 26 | return applicant; 27 | } 28 | 29 | public void setApplicant(Applicant applicant) { 30 | this.applicant = applicant; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-common/src/main/java/de/isb/demo/model/VisaType.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo.model; 2 | 3 | public enum VisaType { 4 | CVISUM, DVISUM; 5 | } 6 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-common/src/main/java/de/isb/demo/model/error/SerializationException.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo.model.error; 2 | 3 | public class SerializationException extends RuntimeException { 4 | 5 | public SerializationException(Throwable cause) { 6 | super(cause); 7 | } 8 | 9 | public SerializationException(String message) { 10 | super(message); 11 | } 12 | 13 | public SerializationException(String message, Throwable cause) { 14 | super(message, cause); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-common/src/main/java/de/isb/demo/model/rest/AboutResponse.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo.model.rest; 2 | 3 | public class AboutResponse extends RestResponse { 4 | private final String applicationName; 5 | private final String applicationVersion; 6 | 7 | public AboutResponse(String applicationName, String applicationVersion) { 8 | this.applicationName = applicationName; 9 | this.applicationVersion = applicationVersion; 10 | } 11 | 12 | public String getApplicationName() { 13 | return applicationName; 14 | } 15 | 16 | public String getApplicationVersion() { 17 | return applicationVersion; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-common/src/main/java/de/isb/demo/model/rest/CreateVisaResponse.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo.model.rest; 2 | 3 | public class CreateVisaResponse extends RestResponse { 4 | private final String id; 5 | 6 | public CreateVisaResponse(String id) { 7 | this.id = id; 8 | } 9 | 10 | public static CreateVisaResponse ok(String id) { 11 | return new CreateVisaResponse(id); 12 | } 13 | 14 | public String getId() { 15 | return id; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-common/src/main/java/de/isb/demo/model/rest/ErrorResponse.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo.model.rest; 2 | 3 | public class ErrorResponse extends RestResponse { 4 | private final String message; 5 | 6 | public ErrorResponse(String message) { 7 | this.message = message; 8 | } 9 | 10 | public static ErrorResponse error(String message) { 11 | return new ErrorResponse(message); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-common/src/main/java/de/isb/demo/model/rest/RestResponse.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo.model.rest; 2 | 3 | import java.time.ZonedDateTime; 4 | 5 | public class RestResponse { 6 | private ZonedDateTime timestamp; 7 | 8 | public RestResponse() { 9 | timestamp = ZonedDateTime.now(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-common/src/main/java/de/isb/demo/model/validation/ValidationResult.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo.model.validation; 2 | 3 | import java.util.Map; 4 | 5 | public class ValidationResult { 6 | private ValidationResultType result; 7 | private Map constraints; 8 | 9 | private ValidationResult() { 10 | 11 | } 12 | 13 | public ValidationResult(ValidationResultType result, Map contraints) { 14 | this.result = result; 15 | this.constraints = contraints; 16 | } 17 | 18 | public ValidationResultType getResult() { 19 | return result; 20 | } 21 | 22 | public void setResult(ValidationResultType result) { 23 | this.result = result; 24 | } 25 | 26 | public Map getConstraints() { 27 | return constraints; 28 | } 29 | 30 | public void setConstraints(Map constraints) { 31 | this.constraints = constraints; 32 | } 33 | 34 | public static ValidationResult ok() { 35 | return new ValidationResult(ValidationResultType.VALIDATION_SUCCESSFUL, null); 36 | } 37 | 38 | public static ValidationResult error(Map constraints) { 39 | return new ValidationResult(ValidationResultType.VALIDATION_ERROR, constraints); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-common/src/main/java/de/isb/demo/model/validation/ValidationResultType.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo.model.validation; 2 | 3 | public enum ValidationResultType { 4 | VALIDATION_SUCCESSFUL, VALIDATION_ERROR 5 | } 6 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-osgi-demo 7 | de.isb 8 | 1.0.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-osgi-demo-core 13 | 14 | 15 | 16 | de.isb 17 | spring-boot-osgi-demo-common 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter 22 | 23 | 24 | com.fasterxml.jackson.core 25 | jackson-databind 26 | 27 | 28 | org.osgi 29 | org.osgi.core 30 | 31 | 32 | org.apache.felix 33 | org.apache.felix.framework 34 | 35 | 36 | org.osgi 37 | org.osgi.core 38 | 39 | 40 | org.osgi 41 | org.osgi.service.component.annotations 42 | 43 | 44 | junit 45 | junit 46 | 4.12 47 | test 48 | 49 | 50 | org.apache.commons 51 | commons-lang3 52 | 3.5 53 | test 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-core/src/main/java/de/isb/demo/osgi/OSGiInitializerBean.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo.osgi; 2 | 3 | import org.osgi.framework.Bundle; 4 | import org.osgi.framework.BundleException; 5 | import org.osgi.framework.ServiceReference; 6 | import org.osgi.framework.launch.Framework; 7 | import org.osgi.framework.launch.FrameworkFactory; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | import org.springframework.stereotype.Component; 11 | 12 | import javax.annotation.PostConstruct; 13 | import java.util.Arrays; 14 | import java.util.HashMap; 15 | import java.util.Iterator; 16 | import java.util.Map; 17 | import java.util.ServiceLoader; 18 | 19 | @Component 20 | public class OSGiInitializerBean { 21 | 22 | private static final Logger log = LoggerFactory.getLogger(OSGiInitializerBean.class); 23 | 24 | private Framework framework; 25 | 26 | public Framework getFramework() { 27 | return framework; 28 | } 29 | 30 | @PostConstruct 31 | public void init() { 32 | final Iterator iterator = ServiceLoader.load(FrameworkFactory.class).iterator(); 33 | if (!iterator.hasNext()) { 34 | throw new IllegalStateException("Unable to locate OSGi framework factory"); 35 | } 36 | final FrameworkFactory frameworkFactory = iterator.next(); 37 | final Map config = new HashMap<>(); 38 | final Framework framework = frameworkFactory.newFramework(config); 39 | try { 40 | framework.start(); 41 | Arrays.asList(framework.getBundleContext().getBundles()).forEach(b -> log.info("Bundle: {}", b.getSymbolicName())); 42 | Arrays.asList(framework.getRegisteredServices()).forEach(s -> log.info("Service reference: {}", s.toString())); 43 | } catch (BundleException e) { 44 | throw new IllegalStateException("Unable to start Framework"); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-core/src/main/java/de/isb/demo/service/api/SerializationService.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo.service.api; 2 | 3 | import java.io.InputStream; 4 | import java.io.OutputStream; 5 | 6 | public interface SerializationService { 7 | String toJson(Object obj); 8 | void toJson(Object obj, OutputStream sink); 9 | T fromJson(InputStream src, Class clazz); 10 | T fromJson(String json, Class clazz); 11 | } 12 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-core/src/main/java/de/isb/demo/service/api/ValidationService.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo.service.api; 2 | 3 | import de.isb.demo.model.validation.ValidationResult; 4 | 5 | public interface ValidationService { 6 | ValidationResult validate(Object o); 7 | } 8 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-core/src/main/java/de/isb/demo/service/api/VisaService.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo.service.api; 2 | 3 | import de.isb.demo.model.Visa; 4 | 5 | import java.io.IOException; 6 | import java.util.List; 7 | 8 | public interface VisaService { 9 | String create(Visa visa) throws IOException; 10 | Visa retrieve(String id) throws IOException; 11 | List list() throws IOException; 12 | } 13 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-core/src/main/java/de/isb/demo/service/impl/OSGiDummyService.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo.service.impl; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | public class OSGiDummyService { 7 | private static final Logger log = LoggerFactory.getLogger(OSGiDummyService.class); 8 | void doStuff() { 9 | log.info("Doing stuff"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-core/src/main/java/de/isb/demo/service/impl/SerializationServiceImpl.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo.service.impl; 2 | 3 | import com.fasterxml.jackson.core.JsonProcessingException; 4 | import com.fasterxml.jackson.databind.ObjectMapper; 5 | import com.fasterxml.jackson.databind.SerializationFeature; 6 | import de.isb.demo.model.error.SerializationException; 7 | import de.isb.demo.service.api.SerializationService; 8 | import org.springframework.beans.factory.annotation.Value; 9 | import org.springframework.stereotype.Service; 10 | 11 | import javax.annotation.PostConstruct; 12 | import java.io.IOException; 13 | import java.io.InputStream; 14 | import java.io.OutputStream; 15 | 16 | @Service 17 | public class SerializationServiceImpl implements SerializationService { 18 | 19 | final ObjectMapper mapper = new ObjectMapper(); 20 | 21 | @Value("${demo.application.json.pretty}") 22 | private boolean jsonPrettyPrint; 23 | 24 | @PostConstruct 25 | public void init() { 26 | if (jsonPrettyPrint) { 27 | mapper.enable(SerializationFeature.INDENT_OUTPUT); 28 | } 29 | } 30 | 31 | @Override 32 | public String toJson(Object obj) throws SerializationException { 33 | try { 34 | return mapper.writeValueAsString(obj); 35 | } catch (JsonProcessingException e) { 36 | throw new SerializationException(e); 37 | } 38 | } 39 | 40 | @Override 41 | public void toJson(Object obj, OutputStream sink) throws SerializationException { 42 | try { 43 | mapper.writeValue(sink, obj); 44 | } catch (IOException e) { 45 | throw new SerializationException(e); 46 | } 47 | } 48 | 49 | @Override 50 | public T fromJson(InputStream src, Class clazz) throws SerializationException { 51 | try { 52 | return mapper.readValue(src, clazz); 53 | } catch (IOException e) { 54 | throw new SerializationException(e); 55 | } 56 | } 57 | 58 | @Override 59 | public T fromJson(String json, Class clazz) throws SerializationException { 60 | try { 61 | return mapper.readValue(json, clazz); 62 | } catch (IOException e) { 63 | throw new SerializationException(e); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-core/src/main/java/de/isb/demo/service/impl/ValidationServiceImpl.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo.service.impl; 2 | 3 | import de.isb.demo.model.validation.ValidationResult; 4 | import de.isb.demo.service.api.ValidationService; 5 | import org.springframework.stereotype.Service; 6 | 7 | @Service 8 | public class ValidationServiceImpl implements ValidationService { 9 | public ValidationResult validate(Object o) { 10 | return ValidationResult.ok(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-core/src/main/java/de/isb/demo/service/impl/VisaServiceImpl.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo.service.impl; 2 | 3 | import de.isb.demo.model.Visa; 4 | import de.isb.demo.service.api.SerializationService; 5 | import de.isb.demo.service.api.VisaService; 6 | import org.osgi.service.component.annotations.Reference; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.beans.factory.annotation.Value; 11 | import org.springframework.stereotype.Service; 12 | 13 | import javax.annotation.PostConstruct; 14 | import java.io.File; 15 | import java.io.FileInputStream; 16 | import java.io.FileOutputStream; 17 | import java.io.IOException; 18 | import java.io.InputStream; 19 | import java.io.OutputStream; 20 | import java.util.ArrayList; 21 | import java.util.List; 22 | import java.util.UUID; 23 | 24 | @Service 25 | public class VisaServiceImpl implements VisaService { 26 | 27 | private static final Logger log = LoggerFactory.getLogger(VisaServiceImpl.class); 28 | 29 | private File dataDirectory; 30 | 31 | @Autowired 32 | private SerializationService serializationService; 33 | 34 | @Reference 35 | private OSGiDummyService osgiDummyService; 36 | 37 | @Value("${demo.application.data.directory}") 38 | private String dataDirectoryPath; 39 | 40 | @PostConstruct 41 | public void init() { 42 | this.dataDirectory = new File(dataDirectoryPath); 43 | if (!this.dataDirectory.exists()) { 44 | log.warn("Data directory will be created since it does not yet exist"); 45 | this.dataDirectory.mkdir(); 46 | } 47 | this.checkDataDirectory(); 48 | } 49 | 50 | public String create(final Visa visa) throws IOException { 51 | 52 | osgiDummyService.doStuff(); 53 | 54 | /* make sure that the data directory is still there */ 55 | this.checkDataDirectory(); 56 | 57 | /* Create and set a visa id for further referencing of the visa */ 58 | final String id = UUID.randomUUID().toString(); 59 | visa.setId(id); 60 | 61 | /* Create the target file for writring the json to */ 62 | final File jsonFile = new File(dataDirectory, "visa-" + id + ".json"); 63 | try (OutputStream sink = new FileOutputStream(jsonFile)) { 64 | serializationService.toJson(visa, sink); 65 | } 66 | log.info("Succesfully saved application {}", id); 67 | return visa.getId(); 68 | } 69 | 70 | private void checkDataDirectory() { 71 | if (!this.dataDirectory.isDirectory()) { 72 | throw new IllegalStateException("Data directory " + dataDirectoryPath + " is not a valid directory"); 73 | } 74 | if (!this.dataDirectory.canWrite()) { 75 | throw new IllegalStateException("Unable to create data directory " + dataDirectoryPath); 76 | } 77 | } 78 | 79 | public Visa retrieve(final String id) throws IOException { 80 | final File jsonFile = new File(dataDirectoryPath, "visa-" + id + ".json"); 81 | return serializationService.fromJson(new FileInputStream(jsonFile), Visa.class); 82 | } 83 | 84 | @Override 85 | public List list() throws IOException { 86 | this.checkDataDirectory(); 87 | final String[] visaFilePaths = dataDirectory.list((File dir, String name) -> name.startsWith("visa-") && name.endsWith(".json")); 88 | final List visas = new ArrayList<>(); 89 | for (final String path : visaFilePaths) { 90 | try (final InputStream src = new FileInputStream(new File(dataDirectory, path))) { 91 | visas.add(serializationService.fromJson(src, Visa.class)); 92 | } 93 | } 94 | return visas; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-core/src/test/java/de/isb/demo/Fixtures.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo; 2 | 3 | import de.isb.demo.model.Applicant; 4 | import de.isb.demo.model.Visa; 5 | import de.isb.demo.model.VisaType; 6 | import org.apache.commons.lang3.RandomStringUtils; 7 | 8 | public class Fixtures { 9 | private Fixtures() { 10 | } 11 | 12 | public static Visa createRandomVisa() { 13 | final Applicant applicant = new Applicant(); 14 | applicant.setFirstNames(RandomStringUtils.randomAlphabetic(16)); 15 | applicant.setLastName(RandomStringUtils.randomAlphabetic(16)); 16 | 17 | final Visa visa = new Visa(); 18 | visa.setType(VisaType.CVISUM); 19 | visa.setApplicant(applicant); 20 | 21 | return visa; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-core/src/test/java/de/isb/demo/service/impl/SerialitzationServiceImplTest.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo.service.impl; 2 | 3 | import de.isb.demo.Fixtures; 4 | import de.isb.demo.model.Visa; 5 | import de.isb.demo.service.api.SerializationService; 6 | import org.junit.Test; 7 | 8 | import static org.junit.Assert.assertEquals; 9 | import static org.junit.Assert.assertNotNull; 10 | import static org.junit.Assert.assertTrue; 11 | 12 | public class SerialitzationServiceImplTest { 13 | 14 | private SerializationService serializationService = new SerializationServiceImpl(); 15 | 16 | @Test 17 | public void testSerialization() { 18 | final Visa visa = Fixtures.createRandomVisa(); 19 | final String json = serializationService.toJson(visa); 20 | final Visa deserialized = serializationService.fromJson(json, Visa.class); 21 | 22 | assertNotNull(deserialized.getType()); 23 | assertNotNull(deserialized.getApplicant()); 24 | assertTrue(deserialized.getApplicant().getFirstNames().length() > 0); 25 | assertTrue(deserialized.getApplicant().getLastName().length() > 0); 26 | assertEquals(visa.getApplicant().getFirstNames(), deserialized.getApplicant().getFirstNames()); 27 | assertEquals(visa.getApplicant().getLastName(), deserialized.getApplicant().getLastName()); 28 | 29 | System.out.println(serializationService.toJson(visa)); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-web/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-osgi-demo 7 | de.isb 8 | 1.0.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-osgi-demo-web 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-web 18 | 19 | 20 | de.isb 21 | spring-boot-osgi-demo-common 22 | 23 | 24 | de.isb 25 | spring-boot-osgi-demo-core 26 | 27 | 28 | org.osgi 29 | org.osgi.core 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-web/src/main/java/de/isb/demo/rest/AboutController.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo.rest; 2 | 3 | import de.isb.demo.model.rest.AboutResponse; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.http.MediaType; 6 | import org.springframework.stereotype.Component; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RequestMethod; 9 | import org.springframework.web.bind.annotation.ResponseBody; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | @RestController 13 | public class AboutController { 14 | 15 | @Value("${demo.application.name}") 16 | private String applicationName; 17 | 18 | @Value("${demo.application.version}") 19 | private String applicationVersion; 20 | 21 | @RequestMapping(value="/about", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 22 | @ResponseBody 23 | public AboutResponse retrieveAbout() { 24 | return new AboutResponse(applicationName, applicationVersion); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /spring-boot-osgi-demo-web/src/main/java/de/isb/demo/rest/VisaController.java: -------------------------------------------------------------------------------- 1 | package de.isb.demo.rest; 2 | 3 | import de.isb.demo.model.Visa; 4 | import de.isb.demo.model.rest.CreateVisaResponse; 5 | import de.isb.demo.model.rest.ErrorResponse; 6 | import de.isb.demo.model.rest.RestResponse; 7 | import de.isb.demo.service.api.VisaService; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.http.HttpStatus; 12 | import org.springframework.http.MediaType; 13 | import org.springframework.web.bind.annotation.ExceptionHandler; 14 | import org.springframework.web.bind.annotation.RequestBody; 15 | import org.springframework.web.bind.annotation.RequestMapping; 16 | import org.springframework.web.bind.annotation.RequestMethod; 17 | import org.springframework.web.bind.annotation.ResponseBody; 18 | import org.springframework.web.bind.annotation.ResponseStatus; 19 | import org.springframework.web.bind.annotation.RestController; 20 | 21 | import javax.websocket.server.PathParam; 22 | import java.io.FileNotFoundException; 23 | import java.io.IOException; 24 | import java.util.List; 25 | 26 | @RestController 27 | public class VisaController { 28 | 29 | private static final Logger log = LoggerFactory.getLogger(VisaController.class); 30 | 31 | @Autowired 32 | private VisaService visaService; 33 | 34 | @RequestMapping(value = "/visa", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) 35 | @ResponseBody 36 | public CreateVisaResponse create(@RequestBody final Visa visa) throws IOException { 37 | final String id = visaService.create(visa); 38 | return CreateVisaResponse.ok(id); 39 | } 40 | 41 | @RequestMapping(value = "/visa/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 42 | @ResponseBody 43 | public Visa retrieve(@PathParam("id") final String id) throws IOException { 44 | return visaService.retrieve(id); 45 | } 46 | 47 | @RequestMapping(value = "/visa-list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 48 | @ResponseBody 49 | public List list() throws IOException { 50 | return visaService.list(); 51 | } 52 | 53 | @ResponseStatus(HttpStatus.NOT_FOUND) 54 | @ExceptionHandler(value = FileNotFoundException.class) 55 | public RestResponse handleFileNotFoundException(FileNotFoundException e) { 56 | log.error("Unable to find target file", e); 57 | return ErrorResponse.error("Unable to find resource: " + e.getMessage()); 58 | } 59 | 60 | @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) 61 | @ExceptionHandler(value = IOException.class) 62 | public RestResponse handleFileIOException(FileNotFoundException e) { 63 | log.error("I/O Error occurred: ", e); 64 | return ErrorResponse.error("Unable to find resource: " + e.getMessage()); 65 | } 66 | } 67 | --------------------------------------------------------------------------------