Skip to content

Commit d883e0e

Browse files
committed
Introduce ResultHandlerService
- Replace main use of ResultHandler with ResultHandlerService which is a framework type of impl for handlers found from conversion service. This handles types better and easier to handle with bean cycles, etc. - Removed IterableResultHandler to think about these use cases later when further refactoring is done. - TypeHierarchyResultHandler is removed and better functionality now via ResultHandlerService. - Relates #336
1 parent c5081ac commit d883e0e

File tree

12 files changed

+531
-160
lines changed

12 files changed

+531
-160
lines changed

spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/SpringShellAutoConfiguration.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.shell.boot;
1818

1919
import java.util.Collection;
20+
import java.util.Set;
2021

2122
import javax.validation.Validation;
2223
import javax.validation.Validator;
@@ -33,8 +34,9 @@
3334
import org.springframework.core.convert.converter.GenericConverter;
3435
import org.springframework.core.convert.support.DefaultConversionService;
3536
import org.springframework.shell.ResultHandler;
37+
import org.springframework.shell.ResultHandlerService;
3638
import org.springframework.shell.Shell;
37-
import org.springframework.shell.result.IterableResultHandler;
39+
import org.springframework.shell.result.GenericResultHandlerService;
3840
import org.springframework.shell.result.ResultHandlerConfig;
3941

4042
/**
@@ -71,8 +73,16 @@ public Validator validator() {
7173
}
7274

7375
@Bean
74-
public Shell shell(@Qualifier("main") ResultHandler resultHandler, @Qualifier("iterableResultHandler") IterableResultHandler iterableResultHandler) {
75-
iterableResultHandler.setDelegate(resultHandler);
76-
return new Shell(resultHandler);
76+
public ResultHandlerService resultHandlerService(Set<ResultHandler<?>> resultHandlers) {
77+
GenericResultHandlerService service = new GenericResultHandlerService();
78+
for (ResultHandler<?> resultHandler : resultHandlers) {
79+
service.addResultHandler(resultHandler);
80+
}
81+
return service;
82+
}
83+
84+
@Bean
85+
public Shell shell(ResultHandlerService resultHandlerService) {
86+
return new Shell(resultHandlerService);
7787
}
7888
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell;
17+
18+
import org.springframework.core.convert.TypeDescriptor;
19+
import org.springframework.lang.Nullable;
20+
21+
/**
22+
* A service interface for result handling.
23+
*
24+
* @author Janne Valkealahti
25+
*/
26+
public interface ResultHandlerService {
27+
28+
/**
29+
* Handle result.
30+
*
31+
* @param result the result
32+
*/
33+
void handle(@Nullable Object result);
34+
35+
/**
36+
* Handle result to the specified {@ resultType}.
37+
*
38+
* @param result the result
39+
* @param resultType the result type
40+
*/
41+
void handle(@Nullable Object result, @Nullable TypeDescriptor resultType);
42+
}

spring-shell-core/src/main/java/org/springframework/shell/Shell.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@
5555
* </p>
5656
*
5757
* @author Eric Bottard
58+
* @author Janne Valkealahti
5859
*/
5960
public class Shell {
6061

61-
private final ResultHandler resultHandler;
62+
private final ResultHandlerService resultHandlerService;
6263

6364
/**
6465
* Marker object returned to signify that there was no input to turn into a command
@@ -84,8 +85,8 @@ public class Shell {
8485
*/
8586
protected static final Object UNRESOLVED = new Object();
8687

87-
public Shell(ResultHandler resultHandler) {
88-
this.resultHandler = resultHandler;
88+
public Shell(ResultHandlerService resultHandlerService) {
89+
this.resultHandlerService = resultHandlerService;
8990
}
9091

9192
@Autowired(required = false)
@@ -125,7 +126,7 @@ public void run(InputProvider inputProvider) throws IOException {
125126
if (e instanceof ExitRequest) { // Handles ExitRequest thrown from hitting CTRL-C
126127
break;
127128
}
128-
resultHandler.handleResult(e);
129+
resultHandlerService.handle(e);
129130
continue;
130131
}
131132
if (input == null) {
@@ -134,7 +135,7 @@ public void run(InputProvider inputProvider) throws IOException {
134135

135136
result = evaluate(input);
136137
if (result != NO_INPUT && !(result instanceof ExitRequest)) {
137-
resultHandler.handleResult(result);
138+
resultHandlerService.handle(result);
138139
}
139140
}
140141
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.springframework.shell.result;
2+
3+
import java.util.Set;
4+
5+
import org.springframework.core.convert.TypeDescriptor;
6+
7+
public interface GenericResultHandler {
8+
9+
Set<Class<?>> getHandlerTypes();
10+
11+
void handle(Object result, TypeDescriptor resultType);
12+
13+
boolean matches(TypeDescriptor resultType);
14+
}

0 commit comments

Comments
 (0)