-
-
Notifications
You must be signed in to change notification settings - Fork 26.6k
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
Closed
romannimets
wants to merge
9
commits into
iluwatar:master
from
romannimets:Implementation-SOA-Pattern
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1ebf835
Basic view and services implementation
romannimets b35a33f
Starting registry implementation
romannimets ac32fc0
Changed my mind and updated the services
romannimets a532ec7
Basic Service Regisry implementation
romannimets 58d17e4
Simple Security Implemented
romannimets c69cc34
Merge remote-tracking branch 'fork/master' into Implementation-SOA-Pa…
romannimets aeb3bcd
added lombok
romannimets 94ba4ec
Created ServiceConfiguration class,
romannimets 3ac3604
rename randomConditionIndex variable,
romannimets File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?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> |
25 changes: 25 additions & 0 deletions
25
service-oriented-architecture/src/main/java/com/iluwatar/soa/SOAExampleApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.iluwatar.soa; | ||
|
||
import com.iluwatar.soa.services.config.ServiceConfiguration; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
@Slf4j | ||
public class SOAExampleApplication { | ||
|
||
public static void main(String[] args) { | ||
ServiceConfiguration.configureAndRegisterServices(); | ||
SpringApplication.run(SOAExampleApplication.class, args); | ||
LOGGER.info("SOA Example Application started successfully!"); | ||
LOGGER.info("You can access the application via: http://localhost:8080/login"); | ||
LOGGER.info( | ||
"This endpoint will retsurn a personalized greeting based on the weather conditions."); | ||
LOGGER.info("You can explore other endpoints as well for different functionalities."); | ||
} | ||
|
||
|
||
} | ||
|
||
|
21 changes: 21 additions & 0 deletions
21
...e-oriented-architecture/src/main/java/com/iluwatar/soa/controller/GreetingController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
|
15 changes: 15 additions & 0 deletions
15
service-oriented-architecture/src/main/java/com/iluwatar/soa/controller/LoginController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} | ||
*/ |
8 changes: 8 additions & 0 deletions
8
service-oriented-architecture/src/main/java/com/iluwatar/soa/model/WeatherCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
60 changes: 60 additions & 0 deletions
60
service-oriented-architecture/src/main/java/com/iluwatar/soa/security/WebSecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LogoutConfigurer::permitAll -- > ensure that only the logged-in user can initiate their own logout |
||
|
||
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(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...ted-architecture/src/main/java/com/iluwatar/soa/services/classes/GreetingServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?"; | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...ture/src/main/java/com/iluwatar/soa/services/classes/PersonalizedGreetingServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
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 lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class PersonalizedGreetingServiceImpl implements PersonalizedGreetingService { | ||
|
||
private final GreetingService greetingService; | ||
romannimets marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private final WeatherService weatherService; | ||
|
||
@Override | ||
public String generateGreeting() { | ||
String weatherGreeting = getWeatherGreeting(); | ||
return weatherGreeting + "! " + greetingService.getGenericGreeting(); | ||
} | ||
|
||
private String getWeatherGreeting() { | ||
WeatherCondition currentWeather = weatherService.getCurrentWeather(); | ||
return switch (currentWeather) { | ||
case SUNNY -> "What a good sunny day!"; | ||
case RAINY -> "What a rainy day!"; | ||
case CLOUDY -> "What a cloudy day!"; | ||
case FOGGY -> "What a foggy day!"; | ||
default -> { | ||
LOGGER.error("Unexpected weather condition: {}", currentWeather); | ||
yield "unexpected weather condition"; | ||
} | ||
}; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...nted-architecture/src/main/java/com/iluwatar/soa/services/classes/WeatherServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
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 { | ||
private final Random random = new Random(); | ||
|
||
public WeatherCondition getCurrentWeather() { | ||
WeatherCondition[] conditions = WeatherCondition.values(); | ||
int randomConditionIndex = random.nextInt(conditions.length); | ||
return conditions[randomConditionIndex]; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...ted-architecture/src/main/java/com/iluwatar/soa/services/config/ServiceConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.iluwatar.soa.services.config; | ||
|
||
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 lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public final class ServiceConfiguration { | ||
|
||
private ServiceConfiguration() { | ||
throw new IllegalStateException("Utility class"); | ||
} | ||
|
||
public static void configureAndRegisterServices() { | ||
LOGGER.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); | ||
|
||
LOGGER.info("Services configured and registered successfully."); | ||
|
||
logServiceRegistry(); | ||
} | ||
|
||
private static void logServiceRegistry() { | ||
LOGGER.info("Current service registry:"); | ||
ServiceRegistry.registry.forEach( | ||
(serviceName, serviceInstance) -> LOGGER.info("- {} : {}", serviceName, | ||
serviceInstance.getClass().getName())); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...nted-architecture/src/main/java/com/iluwatar/soa/services/interfaces/GreetingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
...cture/src/main/java/com/iluwatar/soa/services/interfaces/PersonalizedGreetingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
...ented-architecture/src/main/java/com/iluwatar/soa/services/interfaces/WeatherService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can consider a @ControllerAdvice to handle errors in controllers? or we can handle it try / catch if something goes wrong?