001/* 002 * Copyright 2002-2013 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; 018 019import java.io.IOException; 020 021/** 022 * Script definition interface, encapsulating the configuration 023 * of a specific script as well as a factory method for 024 * creating the actual scripted Java {@code Object}. 025 * 026 * @author Juergen Hoeller 027 * @author Rob Harrop 028 * @since 2.0 029 * @see #getScriptSourceLocator 030 * @see #getScriptedObject 031 */ 032public interface ScriptFactory { 033 034 /** 035 * Return a locator that points to the source of the script. 036 * Interpreted by the post-processor that actually creates the script. 037 * <p>Typical supported locators are Spring resource locations 038 * (such as "file:C:/myScript.bsh" or "classpath:myPackage/myScript.bsh") 039 * and inline scripts ("inline:myScriptText..."). 040 * @return the script source locator 041 * @see org.springframework.scripting.support.ScriptFactoryPostProcessor#convertToScriptSource 042 * @see org.springframework.core.io.ResourceLoader 043 */ 044 String getScriptSourceLocator(); 045 046 /** 047 * Return the business interfaces that the script is supposed to implement. 048 * <p>Can return {@code null} if the script itself determines 049 * its Java interfaces (such as in the case of Groovy). 050 * @return the interfaces for the script 051 */ 052 Class<?>[] getScriptInterfaces(); 053 054 /** 055 * Return whether the script requires a config interface to be 056 * generated for it. This is typically the case for scripts that 057 * do not determine Java signatures themselves, with no appropriate 058 * config interface specified in {@code getScriptInterfaces()}. 059 * @return whether the script requires a generated config interface 060 * @see #getScriptInterfaces() 061 */ 062 boolean requiresConfigInterface(); 063 064 /** 065 * Factory method for creating the scripted Java object. 066 * <p>Implementations are encouraged to cache script metadata such as 067 * a generated script class. Note that this method may be invoked 068 * concurrently and must be implemented in a thread-safe fashion. 069 * @param scriptSource the actual ScriptSource to retrieve 070 * the script source text from (never {@code null}) 071 * @param actualInterfaces the actual interfaces to expose, 072 * including script interfaces as well as a generated config interface 073 * (if applicable; may be {@code null}) 074 * @return the scripted Java object 075 * @throws IOException if script retrieval failed 076 * @throws ScriptCompilationException if script compilation failed 077 */ 078 Object getScriptedObject(ScriptSource scriptSource, Class<?>... actualInterfaces) 079 throws IOException, ScriptCompilationException; 080 081 /** 082 * Determine the type of the scripted Java object. 083 * <p>Implementations are encouraged to cache script metadata such as 084 * a generated script class. Note that this method may be invoked 085 * concurrently and must be implemented in a thread-safe fashion. 086 * @param scriptSource the actual ScriptSource to retrieve 087 * the script source text from (never {@code null}) 088 * @return the type of the scripted Java object, or {@code null} 089 * if none could be determined 090 * @throws IOException if script retrieval failed 091 * @throws ScriptCompilationException if script compilation failed 092 * @since 2.0.3 093 */ 094 Class<?> getScriptedObjectType(ScriptSource scriptSource) 095 throws IOException, ScriptCompilationException; 096 097 /** 098 * Determine whether a refresh is required (e.g. through 099 * ScriptSource's {@code isModified()} method). 100 * @param scriptSource the actual ScriptSource to retrieve 101 * the script source text from (never {@code null}) 102 * @return whether a fresh {@link #getScriptedObject} call is required 103 * @since 2.5.2 104 * @see ScriptSource#isModified() 105 */ 106 boolean requiresScriptedObjectRefresh(ScriptSource scriptSource); 107 108}