001/*
002 * Copyright 2002-2017 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.lang.Nullable;
020import org.springframework.scripting.ScriptSource;
021import org.springframework.util.Assert;
022
023/**
024 * Static implementation of the
025 * {@link org.springframework.scripting.ScriptSource} interface,
026 * encapsulating a given String that contains the script source text.
027 * Supports programmatic updates of the script String.
028 *
029 * @author Rob Harrop
030 * @author Juergen Hoeller
031 * @since 2.0
032 */
033public class StaticScriptSource implements ScriptSource {
034
035        private String script = "";
036
037        private boolean modified;
038
039        @Nullable
040        private String className;
041
042
043        /**
044         * Create a new StaticScriptSource for the given script.
045         * @param script the script String
046         */
047        public StaticScriptSource(String script) {
048                setScript(script);
049        }
050
051        /**
052         * Create a new StaticScriptSource for the given script.
053         * @param script the script String
054         * @param className the suggested class name for the script
055         * (may be {@code null})
056         */
057        public StaticScriptSource(String script, @Nullable String className) {
058                setScript(script);
059                this.className = className;
060        }
061
062        /**
063         * Set a fresh script String, overriding the previous script.
064         * @param script the script String
065         */
066        public synchronized void setScript(String script) {
067                Assert.hasText(script, "Script must not be empty");
068                this.modified = !script.equals(this.script);
069                this.script = script;
070        }
071
072
073        @Override
074        public synchronized String getScriptAsString() {
075                this.modified = false;
076                return this.script;
077        }
078
079        @Override
080        public synchronized boolean isModified() {
081                return this.modified;
082        }
083
084        @Override
085        @Nullable
086        public String suggestedClassName() {
087                return this.className;
088        }
089
090
091        @Override
092        public String toString() {
093                return "static script" + (this.className != null ? " [" + this.className + "]" : "");
094        }
095
096}