Java源码示例:io.undertow.predicate.PredicateBuilder
示例1
public static List<PredicatedHandler> parse(final String contents, final ClassLoader classLoader) {
Deque<Token> tokens = tokenize(contents);
Node node = parse(contents, tokens);
Map<String, PredicateBuilder> predicateBuilders = loadPredicateBuilders(classLoader);
Map<String, HandlerBuilder> handlerBuilders = loadHandlerBuilders(classLoader);
final ExchangeAttributeParser attributeParser = ExchangeAttributes.parser(classLoader);
return handleNode(contents, node, predicateBuilders, handlerBuilders, attributeParser);
}
示例2
public static Predicate parsePredicate(String string, ClassLoader classLoader) {
Deque<Token> tokens = tokenize(string);
Node node = parse(string, tokens);
Map<String, PredicateBuilder> predicateBuilders = loadPredicateBuilders(classLoader);
final ExchangeAttributeParser attributeParser = ExchangeAttributes.parser(classLoader);
return handlePredicateNode(string, node, predicateBuilders, attributeParser);
}
示例3
private static List<PredicatedHandler> handleNode(String contents, Node node, Map<String, PredicateBuilder> predicateBuilders, Map<String, HandlerBuilder> handlerBuilders, ExchangeAttributeParser attributeParser) {
if(node instanceof BlockNode) {
return handleBlockNode(contents, (BlockNode) node, predicateBuilders, handlerBuilders, attributeParser);
} else if(node instanceof ExpressionNode) {
HandlerWrapper handler = handleHandlerNode(contents, (ExpressionNode) node, handlerBuilders, attributeParser);
return Collections.singletonList(new PredicatedHandler(Predicates.truePredicate(), handler));
} else if(node instanceof PredicateOperatorNode) {
return Collections.singletonList(handlePredicateOperatorNode(contents, (PredicateOperatorNode)node, predicateBuilders, handlerBuilders, attributeParser));
} else {
throw error(contents, node.getToken().getPosition(), "unexpected token " + node.getToken());
}
}
示例4
private static PredicatedHandler handlePredicateOperatorNode(String contents, PredicateOperatorNode node, Map<String, PredicateBuilder> predicateBuilders, Map<String, HandlerBuilder> handlerBuilders, ExchangeAttributeParser parser) {
Predicate predicate = handlePredicateNode(contents, node.getLeft(), predicateBuilders, parser);
HandlerWrapper ret = handlePredicatedAction(contents, node.getRight(), predicateBuilders, handlerBuilders, parser);
HandlerWrapper elseBranch = null;
if(node.getElseBranch() != null) {
elseBranch = handlePredicatedAction(contents, node.getElseBranch(), predicateBuilders, handlerBuilders, parser);
}
return new PredicatedHandler(predicate, ret, elseBranch);
}
示例5
private static HandlerWrapper handlePredicatedAction(String contents, Node node, Map<String, PredicateBuilder> predicateBuilders, Map<String, HandlerBuilder> handlerBuilders, ExchangeAttributeParser parser) {
if(node instanceof ExpressionNode) {
return handleHandlerNode(contents, (ExpressionNode) node, handlerBuilders, parser);
} else if(node instanceof BlockNode) {
List<PredicatedHandler> handlers = handleBlockNode(contents, (BlockNode) node, predicateBuilders, handlerBuilders, parser);
return new PredicatesHandler.Wrapper(handlers, false);
} else {
throw error(contents, node.getToken().getPosition(), "unexpected token " + node.getToken());
}
}
示例6
private static List<PredicatedHandler> handleBlockNode(String contents, BlockNode node, Map<String, PredicateBuilder> predicateBuilders, Map<String, HandlerBuilder> handlerBuilders, ExchangeAttributeParser parser) {
List<PredicatedHandler> ret = new ArrayList<>();
for(Node line : node.getBlock()) {
ret.addAll(handleNode(contents, line, predicateBuilders, handlerBuilders, parser));
}
return ret;
}
示例7
private static Predicate handlePredicateExpressionNode(String contents, ExpressionNode node, Map<String, PredicateBuilder> handlerBuilders, ExchangeAttributeParser parser) {
Token token = node.getToken();
PredicateBuilder builder = handlerBuilders.get(token.getToken());
if (builder == null) {
throw error(contents, token.getPosition(), "no predicate named " + token.getToken() + " known predicates are " + handlerBuilders.keySet());
}
Map<String, Object> parameters = new HashMap<>();
for(Map.Entry<String, Node> val : node.getValues().entrySet()) {
String name = val.getKey();
if(name == null) {
if(builder.defaultParameter() == null) {
throw error(contents, token.getPosition(), "default parameter not supported");
}
name = builder.defaultParameter();
}
Class<?> type = builder.parameters().get(name);
if(type == null) {
throw error(contents, val.getValue().getToken().getPosition(), "unknown parameter " + name);
}
if(val.getValue() instanceof ValueNode) {
parameters.put(name, coerceToType(contents, val.getValue().getToken(), type, parser));
} else if(val.getValue() instanceof ArrayNode) {
parameters.put(name, readArrayType(contents, name, (ArrayNode)val.getValue(), parser, type));
} else {
throw error(contents, val.getValue().getToken().getPosition(), "unexpected node " + val.getValue());
}
}
return builder.build(parameters);
}
示例8
private static Map<String, PredicateBuilder> loadPredicateBuilders(final ClassLoader classLoader) {
ServiceLoader<PredicateBuilder> loader = ServiceLoader.load(PredicateBuilder.class, classLoader);
final Map<String, PredicateBuilder> ret = new HashMap<>();
for (PredicateBuilder builder : loader) {
if (ret.containsKey(builder.name())) {
if (ret.get(builder.name()).getClass() != builder.getClass()) {
throw UndertowMessages.MESSAGES.moreThanOnePredicateWithName(builder.name(), builder.getClass(), ret.get(builder.name()).getClass());
}
} else {
ret.put(builder.name(), builder);
}
}
return ret;
}
示例9
public static List<PredicatedHandler> parse(final String contents, final ClassLoader classLoader) {
Deque<Token> tokens = tokenize(contents);
Node node = parse(contents, tokens);
Map<String, PredicateBuilder> predicateBuilders = loadPredicateBuilders(classLoader);
Map<String, HandlerBuilder> handlerBuilders = loadHandlerBuilders(classLoader);
final ExchangeAttributeParser attributeParser = ExchangeAttributes.parser(classLoader);
return handleNode(contents, node, predicateBuilders, handlerBuilders, attributeParser);
}
示例10
public static Predicate parsePredicate(String string, ClassLoader classLoader) {
Deque<Token> tokens = tokenize(string);
Node node = parse(string, tokens);
Map<String, PredicateBuilder> predicateBuilders = loadPredicateBuilders(classLoader);
final ExchangeAttributeParser attributeParser = ExchangeAttributes.parser(classLoader);
return handlePredicateNode(string, node, predicateBuilders, attributeParser);
}
示例11
private static List<PredicatedHandler> handleNode(String contents, Node node, Map<String, PredicateBuilder> predicateBuilders, Map<String, HandlerBuilder> handlerBuilders, ExchangeAttributeParser attributeParser) {
if(node instanceof BlockNode) {
return handleBlockNode(contents, (BlockNode) node, predicateBuilders, handlerBuilders, attributeParser);
} else if(node instanceof ExpressionNode) {
HandlerWrapper handler = handleHandlerNode(contents, (ExpressionNode) node, handlerBuilders, attributeParser);
return Collections.singletonList(new PredicatedHandler(Predicates.truePredicate(), handler));
} else if(node instanceof PredicateOperatorNode) {
return Collections.singletonList(handlePredicateOperatorNode(contents, (PredicateOperatorNode)node, predicateBuilders, handlerBuilders, attributeParser));
} else {
throw error(contents, node.getToken().getPosition(), "unexpected token " + node.getToken());
}
}
示例12
private static PredicatedHandler handlePredicateOperatorNode(String contents, PredicateOperatorNode node, Map<String, PredicateBuilder> predicateBuilders, Map<String, HandlerBuilder> handlerBuilders, ExchangeAttributeParser parser) {
Predicate predicate = handlePredicateNode(contents, node.getLeft(), predicateBuilders, parser);
HandlerWrapper ret = handlePredicatedAction(contents, node.getRight(), predicateBuilders, handlerBuilders, parser);
HandlerWrapper elseBranch = null;
if(node.getElseBranch() != null) {
elseBranch = handlePredicatedAction(contents, node.getElseBranch(), predicateBuilders, handlerBuilders, parser);
}
return new PredicatedHandler(predicate, ret, elseBranch);
}
示例13
private static HandlerWrapper handlePredicatedAction(String contents, Node node, Map<String, PredicateBuilder> predicateBuilders, Map<String, HandlerBuilder> handlerBuilders, ExchangeAttributeParser parser) {
if(node instanceof ExpressionNode) {
return handleHandlerNode(contents, (ExpressionNode) node, handlerBuilders, parser);
} else if(node instanceof BlockNode) {
List<PredicatedHandler> handlers = handleBlockNode(contents, (BlockNode) node, predicateBuilders, handlerBuilders, parser);
return new PredicatesHandler.Wrapper(handlers, false);
} else {
throw error(contents, node.getToken().getPosition(), "unexpected token " + node.getToken());
}
}
示例14
private static List<PredicatedHandler> handleBlockNode(String contents, BlockNode node, Map<String, PredicateBuilder> predicateBuilders, Map<String, HandlerBuilder> handlerBuilders, ExchangeAttributeParser parser) {
List<PredicatedHandler> ret = new ArrayList<>();
for(Node line : node.getBlock()) {
ret.addAll(handleNode(contents, line, predicateBuilders, handlerBuilders, parser));
}
return ret;
}
示例15
private static Predicate handlePredicateExpressionNode(String contents, ExpressionNode node, Map<String, PredicateBuilder> handlerBuilders, ExchangeAttributeParser parser) {
Token token = node.getToken();
PredicateBuilder builder = handlerBuilders.get(token.getToken());
if (builder == null) {
throw error(contents, token.getPosition(), "no predicate named " + token.getToken() + " known predicates are " + handlerBuilders.keySet());
}
Map<String, Object> parameters = new HashMap<>();
for(Map.Entry<String, Node> val : node.getValues().entrySet()) {
String name = val.getKey();
if(name == null) {
if(builder.defaultParameter() == null) {
throw error(contents, token.getPosition(), "default parameter not supported");
}
name = builder.defaultParameter();
}
Class<?> type = builder.parameters().get(name);
if(type == null) {
throw error(contents, val.getValue().getToken().getPosition(), "unknown parameter " + name);
}
if(val.getValue() instanceof ValueNode) {
parameters.put(name, coerceToType(contents, val.getValue().getToken(), type, parser));
} else if(val.getValue() instanceof ArrayNode) {
parameters.put(name, readArrayType(contents, name, (ArrayNode)val.getValue(), parser, type));
} else {
throw error(contents, val.getValue().getToken().getPosition(), "unexpected node " + val.getValue());
}
}
return builder.build(parameters);
}
示例16
private static Map<String, PredicateBuilder> loadPredicateBuilders(final ClassLoader classLoader) {
ServiceLoader<PredicateBuilder> loader = ServiceLoader.load(PredicateBuilder.class, classLoader);
final Map<String, PredicateBuilder> ret = new HashMap<>();
for (PredicateBuilder builder : loader) {
if (ret.containsKey(builder.name())) {
if (ret.get(builder.name()).getClass() != builder.getClass()) {
throw UndertowMessages.MESSAGES.moreThanOnePredicateWithName(builder.name(), builder.getClass(), ret.get(builder.name()).getClass());
}
} else {
ret.put(builder.name(), builder);
}
}
return ret;
}
示例17
@Test
public void listPredicates() {
out.println();
out.println();
out.println();
out.println("predicates");
ArrayList<PredicateBuilder> builds = new ArrayList<PredicateBuilder>();
for (PredicateBuilder i : ServiceLoader.load(PredicateBuilder.class, getClass().getClassLoader())) {
builds.add(i);
}
Collections.sort(builds, new Comparator<PredicateBuilder>() {
@Override
public int compare(PredicateBuilder o1, PredicateBuilder o2) {
return o1.name().compareTo(o2.name());
}
});
for (PredicateBuilder handler : builds) {
out.print("|" + handler.name());
out.print("\t|");
List<String> parms = new ArrayList<>(handler.parameters().keySet());
Collections.sort(parms);
Iterator<String> it = parms.iterator();
while (it.hasNext()) {
String paramName = it.next();
out.print(paramName + ": ");
Class<?> obj = handler.parameters().get(paramName);
if (obj == ExchangeAttribute.class) {
out.print("attribute");
} else if (obj.equals(ExchangeAttribute[].class)) {
out.print("attribute[]");
} else if (obj.equals(String.class)) {
out.print("String");
} else if (obj.equals(String[].class)) {
out.print("String[]");
} else if (obj.equals(Long.class)) {
out.print("Long");
} else if (obj.equals(Long[].class)) {
out.print("Long[]");
} else if (obj.equals(Boolean.class)) {
out.print("Boolean");
} else {
out.print(obj);
}
if(handler.requiredParameters().contains(paramName)) {
out.print(" (required)");
}
if (it.hasNext()) {
out.print(", ");
}
}
out.print("\t|");
if(handler.defaultParameter() != null) {
out.print(handler.defaultParameter());
}
out.print("\t|\n");
}
}
示例18
@Message(id = 44, value = "More than one predicate with name %s. Builder class %s and %s")
IllegalStateException moreThanOnePredicateWithName(String name, Class<? extends PredicateBuilder> aClass, Class<? extends PredicateBuilder> existing);
示例19
@Message(id = 44, value = "More than one predicate with name %s. Builder class %s and %s")
IllegalStateException moreThanOnePredicateWithName(String name, Class<? extends PredicateBuilder> aClass, Class<? extends PredicateBuilder> existing);