1 /* 2 * Copyright 2002-2012 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 17 package org.springframework.scripting.support; 18 19 import org.springframework.scripting.ScriptSource; 20 import org.springframework.util.Assert; 21 22 /** 23 * Static implementation of the 24 * {@link org.springframework.scripting.ScriptSource} interface, 25 * encapsulating a given String that contains the script source text. 26 * Supports programmatic updates of the script String. 27 * 28 * @author Rob Harrop 29 * @author Juergen Hoeller 30 * @since 2.0 31 */ 32 public class StaticScriptSource implements ScriptSource { 33 34 private String script; 35 36 private boolean modified; 37 38 private String className; 39 40 41 /** 42 * Create a new StaticScriptSource for the given script. 43 * @param script the script String 44 */ 45 public StaticScriptSource(String script) { 46 setScript(script); 47 } 48 49 /** 50 * Create a new StaticScriptSource for the given script. 51 * @param script the script String 52 * @param className the suggested class name for the script 53 * (may be {@code null}) 54 */ 55 public StaticScriptSource(String script, String className) { 56 setScript(script); 57 this.className = className; 58 } 59 60 /** 61 * Set a fresh script String, overriding the previous script. 62 * @param script the script String 63 */ 64 public synchronized void setScript(String script) { 65 Assert.hasText(script, "Script must not be empty"); 66 this.modified = !script.equals(this.script); 67 this.script = script; 68 } 69 70 71 @Override 72 public synchronized String getScriptAsString() { 73 this.modified = false; 74 return this.script; 75 } 76 77 @Override 78 public synchronized boolean isModified() { 79 return this.modified; 80 } 81 82 @Override 83 public String suggestedClassName() { 84 return this.className; 85 } 86 87 88 @Override 89 public String toString() { 90 return "static script" + (this.className != null ? " [" + this.className + "]" : ""); 91 } 92 93 }