001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.scripting.support;
018
019import org.springframework.scripting.ScriptSource;
020import org.springframework.util.Assert;
021
022/**
023 * Static implementation of the
024 * {@link org.springframework.scripting.ScriptSource} interface,
025 * encapsulating a given String that contains the script source text.
026 * Supports programmatic updates of the script String.
027 *
028 * @author Rob Harrop
029 * @author Juergen Hoeller
030 * @since 2.0
031 */
032public class StaticScriptSource implements ScriptSource {
033
034        private String script;
035
036        private boolean modified;
037
038        private String className;
039
040
041        /**
042         * Create a new StaticScriptSource for the given script.
043         * @param script the script String
044         */
045        public StaticScriptSource(String script) {
046                setScript(script);
047        }
048
049        /**
050         * Create a new StaticScriptSource for the given script.
051         * @param script the script String
052         * @param className the suggested class name for the script
053         * (may be {@code null})
054         */
055        public StaticScriptSource(String script, String className) {
056                setScript(script);
057                this.className = className;
058        }
059
060        /**
061         * Set a fresh script String, overriding the previous script.
062         * @param script the script String
063         */
064        public synchronized void setScript(String script) {
065                Assert.hasText(script, "Script must not be empty");
066                this.modified = !script.equals(this.script);
067                this.script = script;
068        }
069
070
071        @Override
072        public synchronized String getScriptAsString() {
073                this.modified = false;
074                return this.script;
075        }
076
077        @Override
078        public synchronized boolean isModified() {
079                return this.modified;
080        }
081
082        @Override
083        public String suggestedClassName() {
084                return this.className;
085        }
086
087
088        @Override
089        public String toString() {
090                return "static script" + (this.className != null ? " [" + this.className + "]" : "");
091        }
092
093}