• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TAKETODAY / today-infrastructure / 18154768944

01 Oct 2025 07:26AM UTC coverage: 81.882% (-0.005%) from 81.887%
18154768944

push

github

web-flow
Merge pull request #290 from TAKETODAY/dev/jspecify

jspecify

59788 of 78013 branches covered (76.64%)

Branch coverage included in aggregate %.

141239 of 167496 relevant lines covered (84.32%)

3.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
today-context/src/main/java/infra/scripting/bsh/BshScriptFactory.java
1
/*
2
 * Copyright 2017 - 2025 the original author or authors.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see [https://www.gnu.org/licenses/]
16
 */
17
package infra.scripting.bsh;
18

19
import org.jspecify.annotations.Nullable;
20

21
import java.io.IOException;
22

23
import bsh.EvalError;
24
import infra.beans.factory.BeanClassLoaderAware;
25
import infra.lang.Assert;
26
import infra.scripting.ScriptCompilationException;
27
import infra.scripting.ScriptFactory;
28
import infra.scripting.ScriptSource;
29
import infra.scripting.support.ScriptFactoryPostProcessor;
30
import infra.util.ClassUtils;
31
import infra.util.ReflectionUtils;
32

33
/**
34
 * {@link ScriptFactory} implementation
35
 * for a BeanShell script.
36
 *
37
 * <p>Typically used in combination with a
38
 * {@link ScriptFactoryPostProcessor};
39
 * see the latter's javadoc for a configuration example.
40
 *
41
 * @author Juergen Hoeller
42
 * @author Rob Harrop
43
 * @author <a href="https://github.com/TAKETODAY">海子 Yang</a>
44
 * @see BshScriptUtils
45
 * @see ScriptFactoryPostProcessor
46
 * @since 4.0
47
 */
48
public class BshScriptFactory implements ScriptFactory, BeanClassLoaderAware {
49

50
  private final String scriptSourceLocator;
51

52
  private final Class<?> @Nullable [] scriptInterfaces;
53

54
  @Nullable
×
55
  private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
×
56

57
  @Nullable
58
  private Class<?> scriptClass;
59

60
  private final Object scriptClassMonitor = new Object();
×
61

62
  private boolean wasModifiedForTypeCheck = false;
×
63

64
  /**
65
   * Create a new BshScriptFactory for the given script source.
66
   * <p>With this {@code BshScriptFactory} variant, the script needs to
67
   * declare a full class or return an actual instance of the scripted object.
68
   *
69
   * @param scriptSourceLocator a locator that points to the source of the script.
70
   * Interpreted by the post-processor that actually creates the script.
71
   */
72
  public BshScriptFactory(String scriptSourceLocator) {
×
73
    Assert.hasText(scriptSourceLocator, "'scriptSourceLocator' must not be empty");
×
74
    this.scriptSourceLocator = scriptSourceLocator;
×
75
    this.scriptInterfaces = null;
×
76
  }
×
77

78
  /**
79
   * Create a new BshScriptFactory for the given script source.
80
   * <p>The script may either be a simple script that needs a corresponding proxy
81
   * generated (implementing the specified interfaces), or declare a full class
82
   * or return an actual instance of the scripted object (in which case the
83
   * specified interfaces, if any, need to be implemented by that class/instance).
84
   *
85
   * @param scriptSourceLocator a locator that points to the source of the script.
86
   * Interpreted by the post-processor that actually creates the script.
87
   * @param scriptInterfaces the Java interfaces that the scripted object
88
   * is supposed to implement (may be {@code null})
89
   */
90
  public BshScriptFactory(String scriptSourceLocator, Class<?> @Nullable ... scriptInterfaces) {
×
91
    Assert.hasText(scriptSourceLocator, "'scriptSourceLocator' must not be empty");
×
92
    this.scriptSourceLocator = scriptSourceLocator;
×
93
    this.scriptInterfaces = scriptInterfaces;
×
94
  }
×
95

96
  @Override
97
  public void setBeanClassLoader(ClassLoader classLoader) {
98
    this.beanClassLoader = classLoader;
×
99
  }
×
100

101
  @Override
102
  public String getScriptSourceLocator() {
103
    return this.scriptSourceLocator;
×
104
  }
105

106
  @Override
107
  public Class<?> @Nullable [] getScriptInterfaces() {
108
    return this.scriptInterfaces;
×
109
  }
110

111
  /**
112
   * BeanShell scripts do require a config interface.
113
   */
114
  @Override
115
  public boolean requiresConfigInterface() {
116
    return true;
×
117
  }
118

119
  /**
120
   * Load and parse the BeanShell script via {@link BshScriptUtils}.
121
   *
122
   * @see BshScriptUtils#createBshObject(String, Class[], ClassLoader)
123
   */
124
  @Override
125
  @Nullable
126
  public Object getScriptedObject(ScriptSource scriptSource, Class<?> @Nullable ... actualInterfaces)
127
          throws IOException, ScriptCompilationException {
128

129
    Class<?> clazz;
130

131
    try {
132
      synchronized(this.scriptClassMonitor) {
×
133
        boolean requiresScriptEvaluation = (this.wasModifiedForTypeCheck && this.scriptClass == null);
×
134
        this.wasModifiedForTypeCheck = false;
×
135

136
        if (scriptSource.isModified() || requiresScriptEvaluation) {
×
137
          // New script content: Let's check whether it evaluates to a Class.
138
          Object result = BshScriptUtils.evaluateBshScript(
×
139
                  scriptSource.getScriptAsString(), actualInterfaces, this.beanClassLoader);
×
140
          if (result instanceof Class) {
×
141
            // A Class: We'll cache the Class here and create an instance
142
            // outside of the synchronized block.
143
            this.scriptClass = (Class<?>) result;
×
144
          }
145
          else {
146
            // Not a Class: OK, we'll simply create BeanShell objects
147
            // through evaluating the script for every call later on.
148
            // For this first-time check, let's simply return the
149
            // already evaluated object.
150
            return result;
×
151
          }
152
        }
153
        clazz = this.scriptClass;
×
154
      }
×
155
    }
156
    catch (EvalError ex) {
×
157
      this.scriptClass = null;
×
158
      throw new ScriptCompilationException(scriptSource, ex);
×
159
    }
×
160

161
    if (clazz != null) {
×
162
      // A Class: We need to create an instance for every call.
163
      try {
164
        return ReflectionUtils.accessibleConstructor(clazz).newInstance();
×
165
      }
166
      catch (Throwable ex) {
×
167
        throw new ScriptCompilationException(
×
168
                scriptSource, "Could not instantiate script class: " + clazz.getName(), ex);
×
169
      }
170
    }
171
    else {
172
      // Not a Class: We need to evaluate the script for every call.
173
      try {
174
        return BshScriptUtils.createBshObject(
×
175
                scriptSource.getScriptAsString(), actualInterfaces, this.beanClassLoader);
×
176
      }
177
      catch (EvalError ex) {
×
178
        throw new ScriptCompilationException(scriptSource, ex);
×
179
      }
180
    }
181
  }
182

183
  @Override
184
  @Nullable
185
  public Class<?> getScriptedObjectType(ScriptSource scriptSource)
186
          throws IOException, ScriptCompilationException {
187

188
    synchronized(this.scriptClassMonitor) {
×
189
      try {
190
        if (scriptSource.isModified()) {
×
191
          // New script content: Let's check whether it evaluates to a Class.
192
          this.wasModifiedForTypeCheck = true;
×
193
          this.scriptClass = BshScriptUtils.determineBshObjectType(
×
194
                  scriptSource.getScriptAsString(), this.beanClassLoader);
×
195
        }
196
        return this.scriptClass;
×
197
      }
198
      catch (EvalError ex) {
×
199
        this.scriptClass = null;
×
200
        throw new ScriptCompilationException(scriptSource, ex);
×
201
      }
202
    }
203
  }
204

205
  @Override
206
  public boolean requiresScriptedObjectRefresh(ScriptSource scriptSource) {
207
    synchronized(this.scriptClassMonitor) {
×
208
      return (scriptSource.isModified() || this.wasModifiedForTypeCheck);
×
209
    }
210
  }
211

212
  @Override
213
  public String toString() {
214
    return "BshScriptFactory: script source locator [" + this.scriptSourceLocator + "]";
×
215
  }
216

217
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc