View Javadoc
1   /*
2    *    Copyright 2018-2022 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.mybatis.scripting.thymeleaf;
17  
18  import java.lang.reflect.Proxy;
19  import java.util.Map;
20  
21  import org.thymeleaf.IEngineConfiguration;
22  import org.thymeleaf.context.IContext;
23  import org.thymeleaf.context.IEngineContext;
24  import org.thymeleaf.context.IEngineContextFactory;
25  import org.thymeleaf.engine.TemplateData;
26  
27  /**
28   * The implementation of {@link IEngineContextFactory} for integrating with MyBatis.
29   *
30   * @author Kazuki Shimizu
31   *
32   * @version 1.0.0
33   */
34  public class MyBatisIntegratingEngineContextFactory implements IEngineContextFactory {
35    private final IEngineContextFactory delegate;
36    private final ClassLoader classLoader = getClass().getClassLoader();
37  
38    /**
39     * Constructor.
40     *
41     * @param delegate
42     *          A target context factory for delegating
43     */
44    public MyBatisIntegratingEngineContextFactory(IEngineContextFactory delegate) {
45      this.delegate = delegate;
46    }
47  
48    /**
49     * {@inheritDoc}
50     */
51    @Override
52    public IEngineContext createEngineContext(IEngineConfiguration configuration, TemplateData templateData,
53        Map<String, Object> templateResolutionAttributes, IContext context) {
54      IEngineContext engineContext = delegate.createEngineContext(configuration, templateData,
55          templateResolutionAttributes, context);
56      return (IEngineContext) Proxy.newProxyInstance(classLoader, new Class[] { IEngineContext.class },
57          (proxy, method, args) -> {
58            if (method.getName().equals("getVariable")) {
59              String name = (String) args[0];
60              Object value;
61              MyBatisBindingContext bindingContext = MyBatisBindingContext.load(engineContext);
62              if (bindingContext.isFallbackParameterObject()) {
63                value = engineContext.containsVariable(name) ? engineContext.getVariable(name)
64                    : engineContext.getVariable(SqlGenerator.ContextKeys.PARAMETER_OBJECT);
65              } else {
66                value = engineContext.getVariable(name);
67              }
68              return value;
69            }
70            return method.invoke(engineContext, args);
71          });
72    }
73  
74  }