Skip to content

Commit

Permalink
first working spring app
Browse files Browse the repository at this point in the history
  • Loading branch information
zdenek-jonas committed Nov 8, 2023
1 parent 1b7caf2 commit 002977c
Show file tree
Hide file tree
Showing 13 changed files with 570 additions and 26 deletions.
26 changes: 26 additions & 0 deletions examples/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.eclipse.store</groupId>
<artifactId>store-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>examples</artifactId>

<packaging>pom</packaging>

<modules>
<module>spring-boot3-simple</module>
</modules>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
57 changes: 57 additions & 0 deletions examples/spring-boot3-simple/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.eclipse.store</groupId>
<artifactId>examples</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>spring-boot3-simple</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.boot.version>3.1.5</spring.boot.version>
</properties>


<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.eclipse.store</groupId>
<artifactId>spring-boot3</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>

</project>
14 changes: 14 additions & 0 deletions examples/spring-boot3-simple/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@




```shell

curl --location --request POST 'http://localhost:8080/init' \
--header 'Content-Type: application/json'


curl --location --request GET 'http://localhost:8080/jokes' \
--header 'Content-Type: application/json'

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.microstream.spring.boot.example;

import org.eclipse.store.integrations.spring.boot.types.EclipseStoreSpringBoot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;

@SpringBootApplication
@Import(EclipseStoreSpringBoot.class)
public class JokesApplication
{
public static void main(String... args)
{
SpringApplication.run(JokesApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.microstream.spring.boot.example.controller;

import org.microstream.spring.boot.example.service.JokesServices;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class JokesController
{

private final JokesServices jokesServices;

public JokesController(JokesServices jokesServices)
{
this.jokesServices = jokesServices;
}

@GetMapping("/jokes")
public List<String> getAll()
{
return jokesServices.allJokes();
}

@GetMapping("/joke")
public String getOneJoke(@RequestParam(required = false) Integer id)
{
return jokesServices.oneJoke(id);
}

@PostMapping("/add")
public Integer putOne(String joke)
{
return jokesServices.addNewJoke(joke);
}

@PostMapping("/init")
public void init()
{
jokesServices.loadPredefiniedJokes();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.microstream.spring.boot.example.model;

import java.util.ArrayList;
import java.util.List;

public class Root
{
private final List<String> jokes = new ArrayList<>();

public List<String> getJokes()
{
return jokes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.microstream.spring.boot.example.service;

import org.eclipse.store.storage.embedded.types.EmbeddedStorageManager;
import org.microstream.spring.boot.example.model.Root;
import org.microstream.spring.boot.example.storage.JokesStorage;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

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

@Service
public class JokesServices
{
private final JokesStorage jokesStorage;


public String oneJoke(Integer id) {
String joke;
joke = jokesStorage.oneJoke(Objects.requireNonNullElse(id, 0));
return joke;
}

public JokesServices(JokesStorage jokesStorage)
{
this.jokesStorage = jokesStorage;
}

public List<String> allJokes()
{
return jokesStorage.allJokes();
}

public Integer addNewJoke(String joke)
{
return jokesStorage.addNewJoke(joke);
}

public void loadPredefiniedJokes() {
List<String> jokes = null;
try
{
File file = ResourceUtils.getFile("classpath:jokes.txt");
jokes = Files.readAllLines(file.toPath());
} catch (IOException e)
{
throw new RuntimeException(e);
}
List<String> existingJokes = jokesStorage.allJokes();
if (existingJokes.containsAll(jokes)) {
return;
}
jokesStorage.addJokes(jokes);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.microstream.spring.boot.example.storage;

import org.eclipse.store.storage.embedded.types.EmbeddedStorageManager;
import org.microstream.spring.boot.example.model.Root;
import org.springframework.stereotype.Component;

import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.List;


@Component
public class JokesStorage
{
private final EmbeddedStorageManager storageManager;
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

public JokesStorage(EmbeddedStorageManager storageManager)
{
this.storageManager = storageManager;
}

public String oneJoke(Integer id) {
String joke;
lock.readLock().lock();
try
{
Root root = (Root) storageManager.root();
if (id > root.getJokes().size()) {
throw new IllegalArgumentException("No jokes with this id");
}
joke = root.getJokes().get(id);

}
finally
{
lock.readLock().unlock();
}
return joke;
}

public List<String> allJokes()
{
List<String> jokes = null;
lock.readLock().lock();
try
{
Root root = (Root) storageManager.root();
jokes = root.getJokes();
} finally
{
lock.readLock().unlock();
}
return jokes;
}

public Integer addNewJoke(String joke)
{
lock.writeLock().lock();
Root root = (Root) storageManager.root();
try
{
root.getJokes().add(joke);
storageManager.storeRoot();
} finally
{
lock.writeLock().unlock();
}
return root.getJokes().size();
}

public void addJokes(List<String> jokes)
{
lock.writeLock().lock();
Root root = (Root) storageManager.root();
try
{
root.getJokes().addAll(jokes);
} finally
{
lock.writeLock().unlock();
}
storageManager.store(root.getJokes());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
org.eclipse.store.auto-start=true
org.eclipse.store.root=org.microstream.spring.boot.example.model.Root
org.eclipse.store.storage-directory=jokes_storage
Loading

0 comments on commit 002977c

Please sign in to comment.