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

Service-Oriented Architecture pattern (#2937) #2946

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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@
<module>gateway</module>
<module>serialized-lob</module>
<module>server-session</module>
<module>service-oriented-architecture</module>
<module>virtual-proxy</module>
<module>function-composition</module>
</modules>
Expand Down
101 changes: 101 additions & 0 deletions service-oriented-architecture/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).

The MIT License
Copyright © 2014-2022 Ilkka Seppälä

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

-->
<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>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.26.0-SNAPSHOT</version>
</parent>
<artifactId>service-oriented-architecture</artifactId>
<name>service-oriented-architecture</name>
<description>service-oriented-architecture</description>

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

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>2023.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>

</dependencies>

</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.iluwatar.soa;

import com.iluwatar.soa.services.classes.GreetingServiceImpl;
import com.iluwatar.soa.services.classes.PersonalizedGreetingServiceImpl;
import com.iluwatar.soa.services.classes.WeatherServiceImpl;
import com.iluwatar.soa.services.interfaces.GreetingService;
import com.iluwatar.soa.services.interfaces.PersonalizedGreetingService;
import com.iluwatar.soa.services.interfaces.WeatherService;
import com.iluwatar.soa.services.registry.ServiceRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SOAExampleApplication {

private static final Logger log = LoggerFactory.getLogger(SOAExampleApplication.class);

public static void main(String[] args) {
configureAndRegisterServices();
SpringApplication.run(SOAExampleApplication.class, args);
log.info("SOA Example Application started successfully!");
log.info("You can access the application via: http://localhost:8080/login");
log.info("This endpoint will return a personalized greeting based on the weather conditions.");
log.info("You can explore other endpoints as well for different functionalities.");
}

private static void configureAndRegisterServices() {
log.info("Configuring and registering services...");


GreetingService greetingService = new GreetingServiceImpl();
WeatherService weatherService = new WeatherServiceImpl();
PersonalizedGreetingService personalizedGreetingService = new PersonalizedGreetingServiceImpl(greetingService, weatherService);


ServiceRegistry.registerService("greetingService", greetingService);
ServiceRegistry.registerService("weatherService", weatherService);
ServiceRegistry.registerService("personalizedGreetingService", personalizedGreetingService);

log.info("Services configured and registered successfully.");


log.info("Current service registry:");
ServiceRegistry.registry.forEach((serviceName, serviceInstance) -> log.info("- {} : {}", serviceName, serviceInstance.getClass().getName()));
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.iluwatar.soa.controller;

import com.iluwatar.soa.services.interfaces.PersonalizedGreetingService;
import com.iluwatar.soa.services.registry.ServiceRegistry;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("home")
public class GreetingController {

@GetMapping("/greeting")
public String getGreeting() {
PersonalizedGreetingService personalizedGreetingService =
(PersonalizedGreetingService) ServiceRegistry.getService("personalizedGreetingService");
return personalizedGreetingService.generateGreeting();
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
package com.iluwatar.soa.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class LoginController {

@GetMapping("/login")
public String loginPage() {
return "loginPage";
}
}
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.iluwatar.soa.model;

public enum WeatherCondition {
SUNNY,
RAINY,
CLOUDY,
FOGGY
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.iluwatar.soa.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.LogoutConfigurer;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;


@Configuration
@EnableWebSecurity
public class WebSecurityConfig {

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(requests -> requests
.requestMatchers("/login").permitAll()
.anyRequest().authenticated()
)
.formLogin(form -> form
.defaultSuccessUrl("/home/greeting", true)
.permitAll()
)
.logout(LogoutConfigurer::permitAll);

return http.build();
}


@Bean
public UserDetailsService userDetailsService() {
UserDetails user =
User.builder()
.username("user")
.password(passwordEncoder().encode("password"))
.roles("USER")
.build();
UserDetails admin =
User.builder()
.username("admin")
.password(passwordEncoder().encode("password"))
.roles("USER", "ADMIN")
.build();
return new InMemoryUserDetailsManager(user, admin);
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.iluwatar.soa.services.classes;

import com.iluwatar.soa.services.interfaces.GreetingService;
import org.springframework.stereotype.Service;

@Service
public class GreetingServiceImpl implements GreetingService {

public String getGenericGreeting() {
return "Hello, how are you today?";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.iluwatar.soa.services.classes;

import com.iluwatar.soa.model.WeatherCondition;
import com.iluwatar.soa.services.interfaces.GreetingService;
import com.iluwatar.soa.services.interfaces.PersonalizedGreetingService;
import com.iluwatar.soa.services.interfaces.WeatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PersonalizedGreetingServiceImpl implements PersonalizedGreetingService {

private final GreetingService greetingService;
private final WeatherService weatherService;

@Autowired
public PersonalizedGreetingServiceImpl(GreetingService greetingService, WeatherService weatherService) {
this.greetingService = greetingService;
this.weatherService = weatherService;
}

public String generateGreeting() {
String weatherGreeting = getWeatherGreeting();
return weatherGreeting + "! " + greetingService.getGenericGreeting();
}

private String getWeatherGreeting() {
WeatherCondition currentWeather = weatherService.getCurrentWeather();
switch (currentWeather) {
case SUNNY:
return "What a good sunny day!";
case RAINY:
return "What a rainy day!";
case CLOUDY:
return "What a cloudy day!";
case FOGGY:
return "What a foggy day!";
default:
// error case
return "unexpected weather condition";
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.iluwatar.soa.services.classes;


import com.iluwatar.soa.model.WeatherCondition;
import java.util.Random;
import com.iluwatar.soa.services.interfaces.WeatherService;
import org.springframework.stereotype.Service;

@Service
public class WeatherServiceImpl implements WeatherService {
final Random random = new Random();

public WeatherCondition getCurrentWeather() {
// Simulazione di una scelta casuale delle condizioni meteo
WeatherCondition[] conditions = WeatherCondition.values();
int index = random.nextInt(conditions.length);
return conditions[index];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.iluwatar.soa.services.interfaces;

public interface GreetingService {

String getGenericGreeting();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.iluwatar.soa.services.interfaces;

public interface PersonalizedGreetingService {

String generateGreeting();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.iluwatar.soa.services.interfaces;

import com.iluwatar.soa.model.WeatherCondition;

public interface WeatherService {
WeatherCondition getCurrentWeather();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.iluwatar.soa.services.registry;

import java.util.HashMap;
import java.util.Map;

public class ServiceRegistry {
public static final Map<String, Object> registry = new HashMap<>();

public static void registerService(String serviceName, Object serviceInstance) {
registry.put(serviceName, serviceInstance);
}

public static Object getService(String serviceName) {
return registry.get(serviceName);
}
}
Empty file.