Skip to content
This repository has been archived by the owner on Jul 21, 2020. It is now read-only.

Commit

Permalink
Minor API cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
AFaust committed May 1, 2014
1 parent 887d636 commit 96662a0
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* @author Axel Faust, <a href="http://www.prodyna.com">PRODYNA AG</a>
*/
public interface EnhancedScriptProcessor<Script extends ReferenceScript>
public interface EnhancedScriptProcessor<SL>
{

/**
Expand All @@ -32,7 +32,7 @@ public interface EnhancedScriptProcessor<Script extends ReferenceScript>
* the scope the script is to be executed in
*
*/
void executeInScope(Script location, Object scope);
Object executeInScope(SL location, Object scope);

/**
* Executes a script in a provided scope. When the scope is mutable, execution of the script may result in modifications of its state
Expand All @@ -44,7 +44,7 @@ public interface EnhancedScriptProcessor<Script extends ReferenceScript>
* the scope the script is to be executed in
*
*/
void executeInScope(String source, Object scope);
Object executeInScope(String source, Object scope);

/**
* Retrieves the script location for the current script execution context. This result of this method heavily depends on the state of
Expand Down Expand Up @@ -83,7 +83,7 @@ public interface EnhancedScriptProcessor<Script extends ReferenceScript>
* the script to initialize a execution context for
* @return the new execution context
*/
Object initializeScope(Script location);
Object initializeScope(SL location);

/**
* Registers a scope contributor to be invoked whenever a new scripting scope is initialized for contribution of additional values /
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public interface ReferenceScript
*
* @author Axel Faust, <a href="http://www.prodyna.com">PRODYNA AG</a>
*/
interface ReferencePathType
public interface ReferencePathType
{
// marker interface
}
Expand All @@ -37,7 +37,7 @@ interface ReferencePathType
*
* @author Axel Faust, <a href="http://www.prodyna.com">PRODYNA AG</a>
*/
enum CommonReferencePath implements ReferencePathType
public enum CommonReferencePath implements ReferencePathType
{
CLASSPATH, FILE;
}
Expand All @@ -46,7 +46,7 @@ enum CommonReferencePath implements ReferencePathType
*
* @author Axel Faust, <a href="http://www.prodyna.com">PRODYNA AG</a>
*/
class DynamicScript implements ReferenceScript
public class DynamicScript implements ReferenceScript
{
private final String name;

Expand All @@ -65,6 +65,16 @@ public String getName()
return this.name;
}

/**
*
* {@inheritDoc}
*/
@Override
public String getFullName()
{
return this.name;
}

/**
*
* {@inheritDoc}
Expand Down Expand Up @@ -98,9 +108,16 @@ public Collection<ReferencePathType> getSupportedReferencePathTypes()
}

/**
* Obtains the name of the script
* Obtains the full name of the script
*
* @return the full name of the script
*/
String getFullName();

/**
* Obtains the simple name of the script
*
* @return the name of the script
* @return the simple name of the script
*/
String getName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
*
* @author Axel Faust, <a href="http://www.prodyna.com">PRODYNA AG</a>
*/
public class EnhancedJSScriptProcessor extends BaseRegisterableScriptProcessor implements ScriptProcessor,
EnhancedScriptProcessor<ScriptContentAdapter>, InitializingBean
public class EnhancedJSScriptProcessor extends BaseRegisterableScriptProcessor implements InitializingBean, ScriptProcessor,
EnhancedScriptProcessor<ScriptContent>
{
private static final String CLASSPATH_RESOURCE_IMPORT_PATTERN = "<import(\\s*\\n*\\s+)+resource(\\s*\\n*\\s+)*=(\\s*\\n*\\s+)*\"classpath:(/)?([^\"]+)\"(\\s*\\n*\\s+)*(/)?>";
private static final String CLASSPATH_RESOURCE_IMPORT_REPLACEMENT = "importScript(\"classpath\", \"/$5\", true);";
Expand Down Expand Up @@ -160,7 +160,7 @@ public String getExtension()
* {@inheritDoc}
*/
@Override
public void executeInScope(final String source, final Object scope)
public Object executeInScope(final String source, final Object scope)
{
ParameterCheck.mandatoryString("source", source);

Expand Down Expand Up @@ -262,7 +262,8 @@ else if (!(scope instanceof Scriptable))
realScope = (Scriptable) scope;
}

this.executeScriptInScopeImpl(script, realScope);
final Object result = this.executeScriptInScopeImpl(script, realScope);
return result;
}
finally
{
Expand Down Expand Up @@ -293,12 +294,13 @@ else if (!(scope instanceof Scriptable))
* {@inheritDoc}
*/
@Override
public void executeInScope(final ScriptContentAdapter content, final Object scope)
public Object executeInScope(final ScriptContent content, final Object scope)
{
ParameterCheck.mandatory("content", content);

final Script script = this.getCompiledScript(content);
final String debugScriptName = content.getName();
final ScriptContentAdapter contentAdapter = new ScriptContentAdapter(content, this.standardScriptLoader);
final String debugScriptName = contentAdapter.getName();

LOGGER.info("{} Start", debugScriptName);

Expand All @@ -315,7 +317,7 @@ public void executeInScope(final ScriptContentAdapter content, final Object scop
newChain = true;
}
// else: assume the original script chain is continued
currentChain.add(content);
currentChain.add(contentAdapter);

try
{
Expand Down Expand Up @@ -354,7 +356,8 @@ else if (!(scope instanceof Scriptable))
realScope = (Scriptable) scope;
}

this.executeScriptInScopeImpl(script, realScope);
final Object result = this.executeScriptInScopeImpl(script, realScope);
return result;
}
finally
{
Expand Down Expand Up @@ -384,7 +387,7 @@ else if (!(scope instanceof Scriptable))
* {@inheritDoc}
*/
@Override
public Object initializeScope(final ScriptContentAdapter location)
public Object initializeScope(final ScriptContent location)
{
ParameterCheck.mandatory("location", location);

Expand Down Expand Up @@ -663,23 +666,24 @@ protected Script getCompiledScript(final ScriptContent content)

if (content instanceof ReferenceScript)
{
realPath = ((ReferenceScript)content).getReferencePath(CommonReferencePath.FILE);
realPath = ((ReferenceScript) content).getReferencePath(CommonReferencePath.FILE);
}

if (realPath == null)
{
// check if the path is in classpath form
// TODO: can we generalize external form file:// to a classpath-relative location? (best-effort)
if (!path.matches("^(classpath(\\*)?:)+.*$"))
{
// take path as is - can be anything depending on how content is loaded
realPath = path;
}
else
{
// we always want to have a fully-qualified file-protocol path (unless we can generalize all to classpath-relative locations)
realPath = this.getClass().getClassLoader().getResource(path.substring(path.indexOf(':') + 1)).toExternalForm();
}
// check if the path is in classpath form
// TODO: can we generalize external form file:// to a classpath-relative location? (best-effort)
if (!path.matches("^(classpath(\\*)?:)+.*$"))
{
// take path as is - can be anything depending on how content is loaded
realPath = path;
}
else
{
// we always want to have a fully-qualified file-protocol path (unless we can generalize all to classpath-relative
// locations)
realPath = this.getClass().getClassLoader().getResource(path.substring(path.indexOf(':') + 1)).toExternalForm();
}
}

// store since it may be reset between cache-check and cache-put, and we don't want debug-enabled scripts cached
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ public String toString()
return this.scriptContent.toString();
}

/**
*
* {@inheritDoc}
*/
@Override
public String getFullName()
{
return this.getPath();
}

/**
*
* {@inheritDoc}
Expand Down
Loading

0 comments on commit 96662a0

Please sign in to comment.