Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.result;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.CopyOnWriteArraySet;
import org.springframework.core.ResolvableType;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.lang.Nullable;
import org.springframework.shell.ResultHandler;
import org.springframework.shell.ResultHandlerService;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
/**
* Base {@ResultHandlerService} implementation suitable for use in most
* environments.
*
* @author Janne Valkealahti
*/
public class GenericResultHandlerService implements ResultHandlerService {
private final ResultHandlers resultHandlers = new ResultHandlers();
@Override
public void handle(Object source) {
handle(source, TypeDescriptor.forObject(source));
}
@Override
public void handle(Object result, TypeDescriptor resultType) {
if (result == null) {
return;
}
GenericResultHandler handler = getResultHandler(resultType);
if (handler != null) {
invokeHandler(handler, result, resultType);
return;
}
handleResultHandlerNotFound(result, resultType);
}
/**
* Add a plain result handler to this registry.
*
* @param resultHandler the result handler
*/
public void addResultHandler(ResultHandler> resultHandler) {
ResolvableType[] typeInfo = getRequiredTypeInfo(resultHandler.getClass(), ResultHandler.class);
if (typeInfo == null) {
throw new IllegalArgumentException("Unable to determine result type for your " +
"ResultHandler [" + resultHandler.getClass().getName() + "]; does the class parameterize those types?");
}
addResultHandler(new ResultHandlerAdapter(resultHandler, typeInfo[0]));
}
/**
* Add a plain result handler to this registry.
*
* @param the type of result handler
* @param resultType the class of a result type
* @param resultHandler the result handler
*/
public void addResultHandler(Class resultType, ResultHandler super T> resultHandler) {
addResultHandler(new ResultHandlerAdapter(resultHandler, ResolvableType.forClass(resultType)));
}
/**
* Add a generic result handler this this registry.
*
* @param handler the generic result handler
*/
public void addResultHandler(GenericResultHandler handler) {
this.resultHandlers.add(handler);
}
private GenericResultHandler getResultHandler(TypeDescriptor resultType) {
return this.resultHandlers.find(resultType);
}
@Nullable
private Object handleResultHandlerNotFound(
@Nullable Object source, @Nullable TypeDescriptor sourceType) {
if (source == null) {
return null;
}
if (sourceType == null) {
return source;
}
throw new ResultHandlerNotFoundException(sourceType);
}
@Nullable
private ResolvableType[] getRequiredTypeInfo(Class> handlerClass, Class> genericIfc) {
ResolvableType resolvableType = ResolvableType.forClass(handlerClass).as(genericIfc);
ResolvableType[] generics = resolvableType.getGenerics();
if (generics.length < 1) {
return null;
}
Class> resultType = generics[0].resolve();
if (resultType == null) {
return null;
}
return generics;
}
@SuppressWarnings("unchecked")
private final static class ResultHandlerAdapter implements GenericResultHandler {
ResultHandler