View Javadoc
1   /*
2    * Copyright 2016 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    *     http://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.openehealth.ipf.commons.ihe.core.payload;
17  
18  import org.springframework.expression.Expression;
19  import org.springframework.expression.ExpressionParser;
20  import org.springframework.expression.common.TemplateParserContext;
21  import org.springframework.expression.spel.standard.SpelExpressionParser;
22  
23  import static java.util.Objects.requireNonNull;
24  
25  /**
26   * {@link ExpressionResolver} implementation using Spring Expression language. You can also implement your
27   * own resolver that is able to evaluate file path patterns
28   *
29   * @since 3.1
30   */
31  public class SpringExpressionResolver implements ExpressionResolver {
32  
33      private final Expression expression;
34  
35      public SpringExpressionResolver(final String filePathPattern) {
36          requireNonNull(filePathPattern, "log file path/name pattern must not be null");
37          final ExpressionParser parser = new SpelExpressionParser();
38          final TemplateParserContext parserContext = new TemplateParserContext("[", "]");
39          parser.parseExpression(filePathPattern, parserContext);
40          expression = parser.parseExpression(filePathPattern, parserContext);
41      }
42  
43      @Override
44      public String resolveExpression(PayloadLoggingContext context) {
45          return expression.getValue(context, String.class);
46      }
47  
48  }