You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by tv...@apache.org on 2007/05/05 08:58:51 UTC
svn commit: r535465 [16/49] - in /jakarta/turbine/fulcrum/trunk: ./ bsf/
bsf/src/java/org/apache/fulcrum/bsf/ bsf/src/test/ bsf/xdocs/ cache/
cache/src/java/org/apache/fulcrum/cache/
cache/src/java/org/apache/fulcrum/cache/impl/ cache/src/test/ cache/s...
Modified: jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptCache.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptCache.java?view=diff&rev=535465&r1=535464&r2=535465
==============================================================================
--- jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptCache.java (original)
+++ jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptCache.java Fri May 4 23:58:06 2007
@@ -1,85 +1,87 @@
-package org.apache.fulcrum.script.impl;
-
-/*
- * Copyright 2005 Apache Software Foundation
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- * implied.
- *
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import java.util.Hashtable;
-import java.util.Map;
-
-/**
- * Type-safe wrapper for storing ScriptCacheEntry.
- *
- * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
- */
-public class ScriptCache
-{
- /** mapping from script name to cached scripts */
- private Hashtable scriptCache;
-
- /**
- * Constructor
- */
- public ScriptCache()
- {
- this.scriptCache = new Hashtable();
- }
-
- /**
- * Does the cache contains the script?.
- * @param scriptName the name of the script
- * @return true if the script is already in the cache
- */
- public boolean contains( String scriptName )
- {
- return this.getScriptCache().containsKey( scriptName );
- }
-
- /**
- * Gets a cached script
- * @param scriptName the name of the script
- * @return the ScriptCacheEntry
- */
- public ScriptCacheEntry get( String scriptName )
- {
- return (ScriptCacheEntry) this.getScriptCache().get( scriptName );
- }
-
- /**
- * Puts a new script into the cache.
- * @param scriptCacheEntry the ScriptCacheEntry to add
- */
- public void put( ScriptCacheEntry scriptCacheEntry )
- {
- this.getScriptCache().put( scriptCacheEntry.getScriptName(), scriptCacheEntry );
- }
-
- /**
- * Clears the cache.
- */
- public void clear()
- {
- this.getScriptCache().clear();
- }
-
- /**
- * @return Returns the scriptCache.
- */
- private Map getScriptCache()
- {
- return scriptCache;
- }
-}
+package org.apache.fulcrum.script.impl;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.Hashtable;
+import java.util.Map;
+
+/**
+ * Type-safe wrapper for storing ScriptCacheEntry.
+ *
+ * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
+ */
+public class ScriptCache
+{
+ /** mapping from script name to cached scripts */
+ private Hashtable scriptCache;
+
+ /**
+ * Constructor
+ */
+ public ScriptCache()
+ {
+ this.scriptCache = new Hashtable();
+ }
+
+ /**
+ * Does the cache contains the script?.
+ * @param scriptName the name of the script
+ * @return true if the script is already in the cache
+ */
+ public boolean contains( String scriptName )
+ {
+ return this.getScriptCache().containsKey( scriptName );
+ }
+
+ /**
+ * Gets a cached script
+ * @param scriptName the name of the script
+ * @return the ScriptCacheEntry
+ */
+ public ScriptCacheEntry get( String scriptName )
+ {
+ return (ScriptCacheEntry) this.getScriptCache().get( scriptName );
+ }
+
+ /**
+ * Puts a new script into the cache.
+ * @param scriptCacheEntry the ScriptCacheEntry to add
+ */
+ public void put( ScriptCacheEntry scriptCacheEntry )
+ {
+ this.getScriptCache().put( scriptCacheEntry.getScriptName(), scriptCacheEntry );
+ }
+
+ /**
+ * Clears the cache.
+ */
+ public void clear()
+ {
+ this.getScriptCache().clear();
+ }
+
+ /**
+ * @return Returns the scriptCache.
+ */
+ private Map getScriptCache()
+ {
+ return scriptCache;
+ }
+}
Modified: jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptCacheEntry.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptCacheEntry.java?view=diff&rev=535465&r1=535464&r2=535465
==============================================================================
--- jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptCacheEntry.java (original)
+++ jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptCacheEntry.java Fri May 4 23:58:06 2007
@@ -1,108 +1,110 @@
-package org.apache.fulcrum.script.impl;
-
-/*
- * Copyright 2005 Apache Software Foundation
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- * implied.
- *
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import javax.script.CompiledScript;
-
-/**
- * Keeping track of the metadata of a cached script.
- *
- * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
- */
-public class ScriptCacheEntry
-{
- /** the name of the scripting engine */
- private String engineName;
-
- /** the plain script */
- private String plainScript;
-
- /** the precompiled script */
- private CompiledScript compiledScript;
-
- /** the name of the script */
- private String scriptName;
-
- /**
- * Constructor.
- *
- * @param engineName the name of the script engine
- * @param scriptName the name of the script
- * @param plainScript the plain text script
- * @param compiledScript an optional compiled script
- */
- public ScriptCacheEntry( String engineName, String scriptName, String plainScript, CompiledScript compiledScript )
- {
- Validate.notEmpty(engineName, "engineName");
- Validate.notEmpty(scriptName, "scriptName");
- Validate.notEmpty(plainScript, "plainScript");
-
- this.engineName = engineName;
- this.scriptName = scriptName;
- this.plainScript = plainScript;
- this.compiledScript = compiledScript;
- }
-
- /**
- * @return Returns the compiledScript.
- */
- public CompiledScript getCompiledScript()
- {
- return compiledScript;
- }
-
- /**
- * @param compiledScript The compiledScript to set.
- */
- public void setCompiledScript(CompiledScript compiledScript)
- {
- this.compiledScript = compiledScript;
- }
-
- /**
- * @return Returns the plainScript.
- */
- public String getPlainScript()
- {
- return plainScript;
- }
-
- /**
- * @return Returns the scriptName.
- */
- public String getScriptName()
- {
- return scriptName;
- }
-
- /**
- * @return Returns the engineName.
- */
- public String getEngineName()
- {
- return engineName;
- }
-
- /**
- * Is the script already compiled
- */
- public boolean isCompiled()
- {
- return ( this.getCompiledScript() != null ? true : false );
- }
-}
+package org.apache.fulcrum.script.impl;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import javax.script.CompiledScript;
+
+/**
+ * Keeping track of the metadata of a cached script.
+ *
+ * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
+ */
+public class ScriptCacheEntry
+{
+ /** the name of the scripting engine */
+ private String engineName;
+
+ /** the plain script */
+ private String plainScript;
+
+ /** the precompiled script */
+ private CompiledScript compiledScript;
+
+ /** the name of the script */
+ private String scriptName;
+
+ /**
+ * Constructor.
+ *
+ * @param engineName the name of the script engine
+ * @param scriptName the name of the script
+ * @param plainScript the plain text script
+ * @param compiledScript an optional compiled script
+ */
+ public ScriptCacheEntry( String engineName, String scriptName, String plainScript, CompiledScript compiledScript )
+ {
+ Validate.notEmpty(engineName, "engineName");
+ Validate.notEmpty(scriptName, "scriptName");
+ Validate.notEmpty(plainScript, "plainScript");
+
+ this.engineName = engineName;
+ this.scriptName = scriptName;
+ this.plainScript = plainScript;
+ this.compiledScript = compiledScript;
+ }
+
+ /**
+ * @return Returns the compiledScript.
+ */
+ public CompiledScript getCompiledScript()
+ {
+ return compiledScript;
+ }
+
+ /**
+ * @param compiledScript The compiledScript to set.
+ */
+ public void setCompiledScript(CompiledScript compiledScript)
+ {
+ this.compiledScript = compiledScript;
+ }
+
+ /**
+ * @return Returns the plainScript.
+ */
+ public String getPlainScript()
+ {
+ return plainScript;
+ }
+
+ /**
+ * @return Returns the scriptName.
+ */
+ public String getScriptName()
+ {
+ return scriptName;
+ }
+
+ /**
+ * @return Returns the engineName.
+ */
+ public String getEngineName()
+ {
+ return engineName;
+ }
+
+ /**
+ * Is the script already compiled
+ */
+ public boolean isCompiled()
+ {
+ return ( this.getCompiledScript() != null ? true : false );
+ }
+}
Modified: jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptEngineEntry.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptEngineEntry.java?view=diff&rev=535465&r1=535464&r2=535465
==============================================================================
--- jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptEngineEntry.java (original)
+++ jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptEngineEntry.java Fri May 4 23:58:06 2007
@@ -1,132 +1,134 @@
-package org.apache.fulcrum.script.impl;
-
-/*
- * Copyright 2005 Apache Software Foundation
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- * implied.
- *
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import java.util.List;
-
-import javax.script.ScriptEngine;
-
-/**
- * An entry of the script engine.
- *
- * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
- */
-public class ScriptEngineEntry
-{
- /** the name of the script engine */
- private String name;
-
- /** is the script cached or loaded for each execution */
- private boolean isCached;
-
- /** is the script compiled to improve performance */
- private boolean isCompiled;
-
- /** the location of the scripts */
- private String location;
-
- /** the associated scripting engine */
- private ScriptEngine scriptEngine;
-
- /** a list of scripts to execute during initialization */
- private List scriptList;
-
- /**
- * Constructor
- *
- * @param name the name of the script engine
- * @param isCached is the script cached or loaded for each execution
- * @param isCompiled is the script compiled to improve performance
- * @param location the location of the scripts
- * @param scriptEngine the associated scripting engine
- */
- public ScriptEngineEntry(
- String name,
- boolean isCached,
- boolean isCompiled,
- String location,
- ScriptEngine scriptEngine)
- {
- Validate.notEmpty(name, "name");
- Validate.notEmpty(location, "location");
- Validate.notNull(scriptEngine, "scriptEngine");
-
- this.name = name;
- this.isCached = isCached;
- this.isCompiled = isCompiled;
- this.location = location;
- this.scriptEngine = scriptEngine;
- }
-
- /**
- * @return Returns the isCached.
- */
- public boolean isCached()
- {
- return isCached;
- }
-
- /**
- * @return Returns the isCompiled.
- */
- public boolean isCompiled()
- {
- return isCompiled;
- }
-
- /**
- * @return Returns the location.
- */
- public String getLocation()
- {
- return location;
- }
-
- /**
- * @return Returns the name.
- */
- public String getName()
- {
- return name;
- }
-
- /**
- * @return Returns the scriptEngine.
- */
- public ScriptEngine getScriptEngine()
- {
- return scriptEngine;
- }
-
- /**
- * @return Returns the scriptList.
- */
- public List getScriptList()
- {
- return scriptList;
- }
-
- /**
- * @param scriptList The scriptList to set.
- */
- public void setScriptList(List scriptList)
- {
- Validate.notNull(scriptList, "scriptList");
- this.scriptList = scriptList;
- }
-}
+package org.apache.fulcrum.script.impl;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.List;
+
+import javax.script.ScriptEngine;
+
+/**
+ * An entry of the script engine.
+ *
+ * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
+ */
+public class ScriptEngineEntry
+{
+ /** the name of the script engine */
+ private String name;
+
+ /** is the script cached or loaded for each execution */
+ private boolean isCached;
+
+ /** is the script compiled to improve performance */
+ private boolean isCompiled;
+
+ /** the location of the scripts */
+ private String location;
+
+ /** the associated scripting engine */
+ private ScriptEngine scriptEngine;
+
+ /** a list of scripts to execute during initialization */
+ private List scriptList;
+
+ /**
+ * Constructor
+ *
+ * @param name the name of the script engine
+ * @param isCached is the script cached or loaded for each execution
+ * @param isCompiled is the script compiled to improve performance
+ * @param location the location of the scripts
+ * @param scriptEngine the associated scripting engine
+ */
+ public ScriptEngineEntry(
+ String name,
+ boolean isCached,
+ boolean isCompiled,
+ String location,
+ ScriptEngine scriptEngine)
+ {
+ Validate.notEmpty(name, "name");
+ Validate.notEmpty(location, "location");
+ Validate.notNull(scriptEngine, "scriptEngine");
+
+ this.name = name;
+ this.isCached = isCached;
+ this.isCompiled = isCompiled;
+ this.location = location;
+ this.scriptEngine = scriptEngine;
+ }
+
+ /**
+ * @return Returns the isCached.
+ */
+ public boolean isCached()
+ {
+ return isCached;
+ }
+
+ /**
+ * @return Returns the isCompiled.
+ */
+ public boolean isCompiled()
+ {
+ return isCompiled;
+ }
+
+ /**
+ * @return Returns the location.
+ */
+ public String getLocation()
+ {
+ return location;
+ }
+
+ /**
+ * @return Returns the name.
+ */
+ public String getName()
+ {
+ return name;
+ }
+
+ /**
+ * @return Returns the scriptEngine.
+ */
+ public ScriptEngine getScriptEngine()
+ {
+ return scriptEngine;
+ }
+
+ /**
+ * @return Returns the scriptList.
+ */
+ public List getScriptList()
+ {
+ return scriptList;
+ }
+
+ /**
+ * @param scriptList The scriptList to set.
+ */
+ public void setScriptList(List scriptList)
+ {
+ Validate.notNull(scriptList, "scriptList");
+ this.scriptList = scriptList;
+ }
+}
Modified: jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptRunnableImpl.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptRunnableImpl.java?view=diff&rev=535465&r1=535464&r2=535465
==============================================================================
--- jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptRunnableImpl.java (original)
+++ jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptRunnableImpl.java Fri May 4 23:58:06 2007
@@ -1,125 +1,127 @@
-package org.apache.fulcrum.script.impl;
-
-/*
- * Copyright 2005 Apache Software Foundation
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- * implied.
- *
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import java.util.Map;
-
-import javax.script.SimpleNamespace;
-
-import org.apache.fulcrum.script.ScriptRunnable;
-import org.apache.fulcrum.script.ScriptService;
-
-/**
- * A Runnable executing scripts
- *
- * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
- */
-public class ScriptRunnableImpl implements ScriptRunnable
-{
- /** the name of the script to execute */
- private String scriptName;
-
- /** the service to execute the scripts */
- private ScriptService scriptService;
-
- /** the arguments for executing the script */
- private SimpleNamespace args;
-
- /** the result of the execution */
- private Object result;
-
- /** the execption thrown by the scipt if any */
- private Exception exception;
-
- /**
- * Constructor
- *
- * @param scriptService the service to execute the script
- * @param scriptName the name of the script to execute
- * @param args the arguments passed to the script
- */
- public ScriptRunnableImpl(
- ScriptService scriptService,
- String scriptName,
- Map args)
- {
- Validate.notEmpty( scriptName, "scriptName" );
- Validate.notNull( scriptService, "scriptService" );
-
- this.scriptName = scriptName;
- this.scriptService = scriptService;
- this.args = new SimpleNamespace(args);
- }
-
- /**
- * @see java.lang.Runnable#run()
- */
- public void run()
- {
- try
- {
- this.result = null;
- this.exception = null;
-
- this.result = this.scriptService.eval(
- this.getScriptName(),
- this.args
- );
- }
- catch (Exception e)
- {
- this.exception = e;
- }
- catch (Throwable t)
- {
- this.exception = new Exception(t.getMessage());
- }
- }
-
- /**
- * @return Returns the result.
- */
- public Object getResult() throws Exception
- {
- if( this.exception != null )
- {
- throw this.exception;
- }
- else
- {
- return result;
- }
- }
-
- /**
- * @param args The args to set.
- */
- public void setArgs(Map args)
- {
- Validate.notNull( args, "args" );
- this.args = new SimpleNamespace(args);
- }
-
- /**
- * @return Returns the scriptName.
- */
- private String getScriptName()
- {
- return scriptName;
- }
-}
+package org.apache.fulcrum.script.impl;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.Map;
+
+import javax.script.SimpleNamespace;
+
+import org.apache.fulcrum.script.ScriptRunnable;
+import org.apache.fulcrum.script.ScriptService;
+
+/**
+ * A Runnable executing scripts
+ *
+ * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
+ */
+public class ScriptRunnableImpl implements ScriptRunnable
+{
+ /** the name of the script to execute */
+ private String scriptName;
+
+ /** the service to execute the scripts */
+ private ScriptService scriptService;
+
+ /** the arguments for executing the script */
+ private SimpleNamespace args;
+
+ /** the result of the execution */
+ private Object result;
+
+ /** the execption thrown by the scipt if any */
+ private Exception exception;
+
+ /**
+ * Constructor
+ *
+ * @param scriptService the service to execute the script
+ * @param scriptName the name of the script to execute
+ * @param args the arguments passed to the script
+ */
+ public ScriptRunnableImpl(
+ ScriptService scriptService,
+ String scriptName,
+ Map args)
+ {
+ Validate.notEmpty( scriptName, "scriptName" );
+ Validate.notNull( scriptService, "scriptService" );
+
+ this.scriptName = scriptName;
+ this.scriptService = scriptService;
+ this.args = new SimpleNamespace(args);
+ }
+
+ /**
+ * @see java.lang.Runnable#run()
+ */
+ public void run()
+ {
+ try
+ {
+ this.result = null;
+ this.exception = null;
+
+ this.result = this.scriptService.eval(
+ this.getScriptName(),
+ this.args
+ );
+ }
+ catch (Exception e)
+ {
+ this.exception = e;
+ }
+ catch (Throwable t)
+ {
+ this.exception = new Exception(t.getMessage());
+ }
+ }
+
+ /**
+ * @return Returns the result.
+ */
+ public Object getResult() throws Exception
+ {
+ if( this.exception != null )
+ {
+ throw this.exception;
+ }
+ else
+ {
+ return result;
+ }
+ }
+
+ /**
+ * @param args The args to set.
+ */
+ public void setArgs(Map args)
+ {
+ Validate.notNull( args, "args" );
+ this.args = new SimpleNamespace(args);
+ }
+
+ /**
+ * @return Returns the scriptName.
+ */
+ private String getScriptName()
+ {
+ return scriptName;
+ }
+}
Modified: jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptServiceImpl.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptServiceImpl.java?view=diff&rev=535465&r1=535464&r2=535465
==============================================================================
--- jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptServiceImpl.java (original)
+++ jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptServiceImpl.java Fri May 4 23:58:06 2007
@@ -1,788 +1,790 @@
-package org.apache.fulcrum.script.impl;
-
-/*
- * Copyright 2005 Apache Software Foundation
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- * implied.
- *
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Set;
-
-import javax.script.Compilable;
-import javax.script.CompiledScript;
-import javax.script.Invocable;
-import javax.script.Namespace;
-import javax.script.ScriptContext;
-import javax.script.ScriptEngine;
-import javax.script.ScriptEngineManager;
-import javax.script.ScriptException;
-
-import org.apache.avalon.framework.activity.Disposable;
-import org.apache.avalon.framework.activity.Initializable;
-import org.apache.avalon.framework.configuration.Configuration;
-import org.apache.avalon.framework.configuration.ConfigurationException;
-import org.apache.avalon.framework.configuration.Reconfigurable;
-import org.apache.avalon.framework.context.Contextualizable;
-import org.apache.avalon.framework.service.ServiceException;
-import org.apache.avalon.framework.service.ServiceManager;
-import org.apache.avalon.framework.service.Serviceable;
-import org.apache.avalon.framework.thread.ThreadSafe;
-import org.apache.fulcrum.resourcemanager.ResourceManagerService;
-import org.apache.fulcrum.script.ScriptAvalonContext;
-import org.apache.fulcrum.script.ScriptService;
-
-/**
- * A Fulcrum service to use the JSR-233.
- *
- * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
- */
-public class ScriptServiceImpl
- extends ScriptBaseService
- implements ScriptService, Contextualizable, Initializable, Disposable, Serviceable, Reconfigurable, ThreadSafe
-{
- /** the name of the default scripting engine*/
- private String defaultEngineName;
-
- /** the script manager */
- private ScriptEngineManager scriptEngineManager;
-
- /** the mapof scripting engines to use */
- private HashMap scriptEngineMap;
-
- /** maps from a script name to a script */
- private ScriptCache scriptCache;
-
- /** a list of excluded services */
- private Set excludedServices;
-
- /** common script configuration */
- private Configuration scriptConfiguration;
-
- /////////////////////////////////////////////////////////////////////////
- // Avalon Service Lifecycle Implementation
- /////////////////////////////////////////////////////////////////////////
-
- /**
- * Constructor
- */
- public ScriptServiceImpl()
- {
- super();
- this.scriptCache = new ScriptCache();
- this.scriptEngineMap = new HashMap();
- this.scriptEngineManager = new ScriptEngineManager();
- this.excludedServices = new HashSet();
- }
-
- /**
- * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
- */
- public void service(ServiceManager serviceManager) throws ServiceException
- {
- super.service( serviceManager );
- this.getServiceManager().lookup( ResourceManagerService.class.getName() );
- }
-
- /**
- * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
- */
- public void configure(Configuration configuration) throws ConfigurationException
- {
- super.configure( configuration );
-
- // extract the common script configuration
-
- this.scriptConfiguration = configuration.getChild("scriptConfiguration");
-
- // create all scripting engines
-
- Configuration[] scriptEngineConfigurationList = configuration.getChild("scriptEngines").getChildren("scriptEngine");
-
- for (int i=0; i<scriptEngineConfigurationList.length; i++)
- {
- ScriptEngineEntry scriptEngineEntry = this.createScriptEngineEntry(scriptEngineConfigurationList[i]);
- this.getScriptEngineMap().put(scriptEngineEntry.getName(), scriptEngineEntry);
- this.getLogger().debug("Added ScriptEngine for " + scriptEngineEntry.getName());
-
- if( this.defaultEngineName == null)
- {
- this.defaultEngineName = scriptEngineEntry.getName();
- }
- }
-
- // verify that at least one scripting engine is available
-
- if( this.getScriptEngineMap().size() == 0 )
- {
- String msg = "At least one scripting engine must be defined";
- this.getLogger().error(msg);
- throw new ConfigurationException(msg);
- }
-
- // evaluate the scripts defined in the onLoad section
-
- this.preloadScripts();
- }
-
- /**
- * @see org.apache.avalon.framework.activity.Initializable#initialize()
- */
- public void initialize() throws Exception
- {
- ScriptAvalonContext avalonContext = this.createScriptAvalonContext();
- this.scriptEngineManager.put("avalonContext", avalonContext);
- }
-
- /**
- * @see org.apache.avalon.framework.configuration.Reconfigurable#reconfigure(org.apache.avalon.framework.configuration.Configuration)
- */
- public void reconfigure(Configuration cfg) throws ConfigurationException
- {
- super.reconfigure(cfg);
- this.defaultEngineName = null;
- this.scriptConfiguration = null;
- this.getScriptEngineMap().clear();
- this.getScriptCache().clear();
- this.configure(cfg);
- }
-
- /**
- * @see org.apache.avalon.framework.activity.Disposable#dispose()
- */
- public void dispose()
- {
- super.dispose();
- this.scriptCache.clear();
- this.scriptCache = null;
- this.excludedServices = null;
- }
-
- /////////////////////////////////////////////////////////////////////////
- // Service interface implementation
- /////////////////////////////////////////////////////////////////////////
-
- /**
- * @see org.apache.fulcrum.script.ScriptService#getScriptEngineManager()
- */
- public ScriptEngineManager getScriptEngineManager()
- {
- return this.scriptEngineManager;
- }
-
- /**
- * @see org.apache.fulcrum.script.ScriptService#getScriptEngine()
- */
- public ScriptEngine getScriptEngine()
- {
- return this.getScriptEngine(this.getDefaultEngineName());
- }
-
- /**
- * @see org.apache.fulcrum.script.ScriptService#eval(java.lang.String, javax.script.Namespace)
- */
- public Object eval(String scriptName, Namespace namespace)
- throws IOException, ScriptException
- {
- Validate.notEmpty(scriptName, "scriptName");
- Validate.notNull(namespace, "namespace");
- String currScriptName = this.makeScriptName(scriptName);
- ScriptCacheEntry scriptCacheEntry = this.createScriptCacheEntry(currScriptName);
- Object result = this.doEval( scriptCacheEntry, namespace, null );
- return result;
-
- }
-
- /* (non-Javadoc)
- * @see org.apache.fulcrum.script.ScriptService#eval(java.lang.String, javax.script.ScriptContext)
- */
- public Object eval(String scriptName, ScriptContext context)
- throws IOException, ScriptException
- {
- Validate.notEmpty(scriptName, "scriptName");
- Validate.notNull(context, "context");
- String currScriptName = this.makeScriptName(scriptName);
- ScriptCacheEntry scriptCacheEntry = this.createScriptCacheEntry(currScriptName);
- ScriptAvalonContext avalonContext = this.createScriptAvalonContext();
- context.setAttribute("avalonContext", avalonContext, ScriptContext.ENGINE_SCOPE);
- Object result = this.doEval( scriptCacheEntry, null, context );
- return result;
- }
-
- /**
- * @see org.apache.fulcrum.script.ScriptService#eval(java.lang.String)
- */
- public Object eval(String scriptName) throws IOException, ScriptException
- {
- Validate.notEmpty(scriptName, "scriptName");
- String currScriptName = this.makeScriptName(scriptName);
- ScriptCacheEntry scriptCacheEntry = this.createScriptCacheEntry(currScriptName);
- Object result = this.doEval( scriptCacheEntry, null, null );
- return result;
- }
-
- /**
- * @see org.apache.fulcrum.script.ScriptService#call(java.lang.String, java.lang.Object[])
- */
- public Object call(String name, Object [] args) throws ScriptException,
- NoSuchMethodException
- {
- Validate.notEmpty(name, "name");
- return this.call(this.getDefaultEngineName(), name, args);
-
- }
-
- /**
- * @see org.apache.fulcrum.script.ScriptService#getInterface(java.lang.Class)
- */
- public Object getInterface(Class clazz)
- {
- return this.getInterface(this.getDefaultEngineName(), clazz);
- }
-
- /**
- * @see org.apache.fulcrum.script.ScriptService#exists(java.lang.String)
- */
- public boolean exists(String name)
- {
- Validate.notEmpty(name, "name");
-
- String scriptName = this.makeScriptName(name);
- String engineName = this.getEngineName(scriptName);
- ScriptEngineEntry scriptEngineEntry = this.getScriptEngineEntry(engineName);
- String location = scriptEngineEntry.getLocation();
-
- if( this.getResourceManagerService().exists(location) &&
- this.getResourceManagerService().exists(location, scriptName) )
- {
- return true;
- }
- else
- {
- return false;
- }
- }
-
- /////////////////////////////////////////////////////////////////////////
- // Service Implementation
- /////////////////////////////////////////////////////////////////////////
-
- private ScriptEngine getScriptEngine(String engineName)
- {
- ScriptEngineEntry scriptEngineEntry = this.getScriptEngineEntry(engineName);
- return scriptEngineEntry.getScriptEngine();
- }
-
- /**
- * @param engineName the engine name
- * @param clazz the inteface to implement
- * @return a scripting object implementing the given interface
- */
- private Object call(String engineName, String name, Object[] args)
- throws ScriptException, NoSuchMethodException
- {
- Validate.notEmpty(engineName, "engineName");
-
- Object result = this.doCall(engineName, name, args);
- return result;
- }
-
- /**
- * @param engineName the engine name
- * @param clazz the inteface to implement
- * @return a scripting object implementing the given interface
- */
- private Object getInterface(String engineName, Class clazz)
- {
- Validate.notEmpty(engineName, "engineName");
- Validate.notNull(clazz, "clazz");
-
- Object result = this.doGetInterface(engineName, clazz);
- return result;
- }
-
- /**
- * @return an instance of the ResourceService
- */
- private ResourceManagerService getResourceManagerService()
- {
- try
- {
- return (ResourceManagerService) this.getServiceManager().lookup(
- ResourceManagerService.class.getName()
- );
- }
- catch (ServiceException e)
- {
- throw new RuntimeException( e.getMessage() );
- }
- }
-
- /**
- * Load a script from the persistent storage.
- *
- * @param scriptName the name of the Groovy script
- * @return the plain content of the script
- */
- private String loadScript( String engineName, String scriptName )
- throws IOException
- {
- ScriptEngineEntry scriptEngineEntry = this.getScriptEngineEntry(engineName);
- String location = scriptEngineEntry.getLocation();
-
- String result = null;
- byte[] scriptContent = null;
- String[] sriptContext = {};
-
- scriptContent = this.getResourceManagerService().read(
- location,
- sriptContext,
- scriptName
- );
-
- result = new String( scriptContent );
- return result;
- }
-
- /**
- * Creates a compiled script.
- *
- * @param scriptName the name of the scipt to compile
- * @param scriptContent the name scipt to compile
- * @return the compiled script
- * @throws ScriptException failed to compile the script
- */
- private CompiledScript compileScript( String engineName, String scriptName, String scriptContent )
- throws ScriptException
- {
- CompiledScript result = null;
-
- try
- {
- ScriptEngineEntry scriptEngineEntry = this.getScriptEngineEntry(engineName);
- Compilable compilable = (Compilable) scriptEngineEntry.getScriptEngine();
- long startTime = System.currentTimeMillis();
- result = compilable.compile(scriptContent);
- long endTime = System.currentTimeMillis();
-
- if( this.getLogger().isDebugEnabled() )
- {
- this.getLogger().debug(
- "Compiling of " + scriptName + " took "
- + (endTime - startTime) + " ms"
- );
- }
-
- return result;
- }
- catch (ScriptException e)
- {
- String msg = "Compiling the script failed : " + scriptName;
- this.getLogger().error( msg, e );
- throw new RuntimeException( msg ,e );
- }
- }
-
- /**
- * Evaluates a script.
- *
- * @param scriptCacheEntry the script to run
- * @param namespace the parameters to pass to the script
- * @return the result of the executed script
- */
- private Object doEval( ScriptCacheEntry scriptCacheEntry, Namespace namespace, ScriptContext scriptContext ) throws ScriptException
- {
- Object result = null;
- String scriptName = scriptCacheEntry.getScriptName();
- ScriptEngineEntry scriptEngineEntry = this.getScriptEngineEntry(scriptCacheEntry.getEngineName());
- ScriptEngine scriptEngine = scriptEngineEntry.getScriptEngine();
-
- try
- {
- long startTime = System.currentTimeMillis();
-
- if( scriptCacheEntry.isCompiled() )
- {
- if( namespace != null )
- {
- result = scriptCacheEntry.getCompiledScript().eval(namespace);
- }
- else if( scriptContext != null )
- {
- result = scriptCacheEntry.getCompiledScript().eval(scriptContext);
- }
- else
- {
- result = scriptCacheEntry.getCompiledScript().eval();
- }
- }
- else
- {
- if( namespace != null )
- {
- result = scriptEngine.eval(scriptCacheEntry.getPlainScript(), namespace);
- }
- else if( scriptContext != null )
- {
- result = scriptEngine.eval(scriptCacheEntry.getPlainScript(), scriptContext);
- }
- else
- {
- result = scriptEngine.eval(scriptCacheEntry.getPlainScript());
- }
- }
-
- long endTime = System.currentTimeMillis();
-
- if( this.getLogger().isDebugEnabled() )
- {
- this.getLogger().debug(
- "Execution of " + scriptName + " took "
- + (endTime - startTime) + " ms"
- );
- }
-
- return result;
- }
- catch( ScriptException e )
- {
- String msg= "Execution of the script failed : " + scriptName;
- this.getLogger().error( msg, e );
- throw e;
- }
- }
-
- /**
- * Invokes a global script method
- */
- private Object doCall( String engineName, String name, Object[] args )
- throws ScriptException, NoSuchMethodException
- {
- Object result = null;
- ScriptEngineEntry scriptEngineEntry = this.getScriptEngineEntry(engineName);
- ScriptEngine scriptEngine = scriptEngineEntry.getScriptEngine();
- Invocable invocable = (Invocable) scriptEngine;
-
- try
- {
- long startTime = System.currentTimeMillis();
- result = invocable.call(name, args);
- long endTime = System.currentTimeMillis();
-
- if( this.getLogger().isDebugEnabled() )
- {
- this.getLogger().debug(
- "Calling the method " + name + " took "
- + (endTime - startTime) + " ms"
- );
- }
-
- return result;
- }
- catch( ScriptException e )
- {
- String msg= "Caling the following method failed : " + name;
- this.getLogger().error( msg, e );
- throw e;
- }
- catch( NoSuchMethodException e )
- {
- String msg= "Caling the following method failed : " + name;
- this.getLogger().error( msg, e );
- throw e;
- }
- }
-
- /**
- * Creates a script object implementing the requested interface.
- *
- * @param engineName the name of the scripting engine
- * @param clazz the requested interface
- * @return the result of the executed script
- */
- private Object doGetInterface( String engineName, Class clazz )
- {
- Object result = null;
- ScriptEngineEntry scriptEngineEntry = this.getScriptEngineEntry(engineName);
- ScriptEngine scriptEngine = scriptEngineEntry.getScriptEngine();
- Invocable invocable = (Invocable) scriptEngine;
-
- try
- {
- long startTime = System.currentTimeMillis();
- result = invocable.getInterface(clazz);
- long endTime = System.currentTimeMillis();
-
- if( this.getLogger().isDebugEnabled() )
- {
- this.getLogger().debug(
- "Creating the interface for " + clazz.getName() + " took "
- + (endTime - startTime) + " ms"
- );
- }
-
- return result;
- }
- catch( RuntimeException e )
- {
- String msg= "Creation of the interface failed : " + clazz.getName();
- this.getLogger().error( msg, e );
- throw e;
- }
- }
-
- /**
- * Create the Avalon specific context
- */
- private ScriptAvalonContext createScriptAvalonContext()
- {
- ScriptAvalonContextImpl result = null;
-
- ScriptServiceManagerImpl scriptServiceManager = new ScriptServiceManagerImpl(
- this.getServiceManager(),
- this.excludedServices
- );
-
- result = new ScriptAvalonContextImpl(
- this.getLogger().getChildLogger("script"),
- scriptServiceManager,
- this.getContext(),
- this.scriptConfiguration,
- this.getParameters()
- );
-
- return result;
- }
-
- /**
- * @return the name of the default engine
- */
- private String getDefaultEngineName()
- {
- return this.defaultEngineName;
- }
-
- /**
- * @return Returns the scriptCache.
- */
- private ScriptCache getScriptCache()
- {
- return scriptCache;
- }
-
- /**
- * Creates a script to be evaluates.
- *
- * @param scriptName the name of the script to execute
- * @return ScriptCacheEntry
- */
- private synchronized ScriptCacheEntry createScriptCacheEntry(String scriptName)
- throws IOException, ScriptException
- {
- String engineName = this.getEngineName(scriptName);
- ScriptCacheEntry scriptCacheEntry = null;
- ScriptEngineEntry scriptEngineEntry = this.getScriptEngineEntry(engineName);
- String plainScript = null;
- CompiledScript compiledScript = null;
- boolean isInCache = false;
-
- // try to load the script from the cache or from the resource manager
-
- if( scriptEngineEntry.isCached() == true )
- {
- scriptCacheEntry = this.getScriptCache().get(scriptName);
-
- if( scriptCacheEntry == null )
- {
- plainScript = this.loadScript(engineName, scriptName);
- }
- else
- {
- plainScript = scriptCacheEntry.getPlainScript();
- isInCache = true;
- }
- }
- else
- {
- plainScript = this.loadScript(engineName, scriptName);
- }
-
- if( isInCache == false )
- {
- // compile the script if required
-
- if( scriptEngineEntry.isCompiled())
- {
- compiledScript = this.compileScript(engineName, scriptName, plainScript);
- }
-
- scriptCacheEntry = new ScriptCacheEntry(
- engineName,
- scriptName,
- plainScript,
- compiledScript
- );
-
- // update the cache
-
- if( scriptEngineEntry.isCached() )
- {
- this.getScriptCache().put(scriptCacheEntry);
- }
- }
-
- return scriptCacheEntry;
- }
-
- /**
- * Create a ScriptEngineEntry by parsing the configuration.
- *
- * @param configuration the configuration
- * @return ScriptEngineEntry
- * @throws ConfigurationException parsing the configuration failed
- */
- private ScriptEngineEntry createScriptEngineEntry(Configuration configuration)
- throws ConfigurationException
- {
- String name = configuration.getChild("name").getValue();
- boolean isCached = configuration.getChild("isCached").getValueAsBoolean(true);
- boolean isCompiled = configuration.getChild("isCompiled").getValueAsBoolean(true);
- String location = configuration.getChild("location").getValue(name);
- ScriptEngine scriptEngine = this.getScriptEngineManager().getEngineByName(name);
-
- if( (scriptEngine instanceof Compilable) == false )
- {
- isCompiled = false;
- }
-
- ScriptEngineEntry result = new ScriptEngineEntry(name, isCached, isCompiled, location, scriptEngine);
-
- // evaluate the given scripts
-
- ArrayList scriptList = new ArrayList();
- Configuration[] scriptListConfiguration = configuration.getChild("preLoad").getChildren("script");
- for( int i=0; i<scriptListConfiguration.length; i++ )
- {
- scriptList.add(scriptListConfiguration[i].getValue());
- }
-
- result.setScriptList(scriptList);
-
- return result;
- }
-
- /**
- * @param engineName the name of the scriptng engine
- * @return the ScriptEngineEntry
- */
- private ScriptEngineEntry getScriptEngineEntry( String engineName)
- {
- ScriptEngineEntry result = (ScriptEngineEntry) this.getScriptEngineMap().get(engineName);
-
- if( result == null )
- {
- String msg = "Unable to handle scripts for the following script engine : " + engineName;
- this.getLogger().error(msg);
- throw new IllegalArgumentException(msg);
- }
-
- return result;
- }
-
- /**
- * Determines the name of the scripting engine by looking at the extension
- *
- * @param scriptName the script name
- * @return name of the scripting engine
- */
- private String getEngineName( String scriptName )
- {
- int pos = scriptName.lastIndexOf('.');
-
- if( pos > 0 )
- {
- return scriptName.substring(pos+1, scriptName.length());
- }
- else
- {
- return this.getDefaultEngineName();
- }
- }
-
- /**
- * @return Returns the scriptEngineMap.
- */
- private HashMap getScriptEngineMap()
- {
- return scriptEngineMap;
- }
-
- /**
- * Ensure that the name has a proper extension.
- *
- * @param name the name of the script
- * @return script name
- */
- private String makeScriptName( String name )
- {
- String engineName = this.getEngineName(name);
- String extension = '.' + engineName;
-
- if( name.endsWith(extension) )
- {
- return name;
- }
- else
- {
- return name + extension;
- }
- }
-
- /**
- * Preload the scripts defined in the onLoad section
- */
- private void preloadScripts()
- {
- Iterator iter = this.getScriptEngineMap().keySet().iterator();
-
- while( iter.hasNext() )
- {
- String engineName = (String) iter.next();
- ScriptEngineEntry scriptEngineEntry = this.getScriptEngineEntry(engineName);
- ScriptEngine scriptEngine = scriptEngineEntry.getScriptEngine();
- Object[] scriptList = scriptEngineEntry.getScriptList().toArray();
-
- for( int i=0; i<scriptList.length; i++ )
- {
- String scriptName = scriptList[i].toString();
-
- try
- {
- String plainScript = this.loadScript(engineName, scriptName);
- scriptEngine.eval(plainScript);
- }
- catch (Exception e)
- {
- String msg = "Unable to load the following script : " + scriptName;
- this.getLogger().error(msg, e);
- throw new RuntimeException(msg);
- }
- }
- }
- }
-}
+package org.apache.fulcrum.script.impl;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import javax.script.Compilable;
+import javax.script.CompiledScript;
+import javax.script.Invocable;
+import javax.script.Namespace;
+import javax.script.ScriptContext;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
+
+import org.apache.avalon.framework.activity.Disposable;
+import org.apache.avalon.framework.activity.Initializable;
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.configuration.Reconfigurable;
+import org.apache.avalon.framework.context.Contextualizable;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.Serviceable;
+import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.fulcrum.resourcemanager.ResourceManagerService;
+import org.apache.fulcrum.script.ScriptAvalonContext;
+import org.apache.fulcrum.script.ScriptService;
+
+/**
+ * A Fulcrum service to use the JSR-233.
+ *
+ * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
+ */
+public class ScriptServiceImpl
+ extends ScriptBaseService
+ implements ScriptService, Contextualizable, Initializable, Disposable, Serviceable, Reconfigurable, ThreadSafe
+{
+ /** the name of the default scripting engine*/
+ private String defaultEngineName;
+
+ /** the script manager */
+ private ScriptEngineManager scriptEngineManager;
+
+ /** the mapof scripting engines to use */
+ private HashMap scriptEngineMap;
+
+ /** maps from a script name to a script */
+ private ScriptCache scriptCache;
+
+ /** a list of excluded services */
+ private Set excludedServices;
+
+ /** common script configuration */
+ private Configuration scriptConfiguration;
+
+ /////////////////////////////////////////////////////////////////////////
+ // Avalon Service Lifecycle Implementation
+ /////////////////////////////////////////////////////////////////////////
+
+ /**
+ * Constructor
+ */
+ public ScriptServiceImpl()
+ {
+ super();
+ this.scriptCache = new ScriptCache();
+ this.scriptEngineMap = new HashMap();
+ this.scriptEngineManager = new ScriptEngineManager();
+ this.excludedServices = new HashSet();
+ }
+
+ /**
+ * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
+ */
+ public void service(ServiceManager serviceManager) throws ServiceException
+ {
+ super.service( serviceManager );
+ this.getServiceManager().lookup( ResourceManagerService.class.getName() );
+ }
+
+ /**
+ * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
+ */
+ public void configure(Configuration configuration) throws ConfigurationException
+ {
+ super.configure( configuration );
+
+ // extract the common script configuration
+
+ this.scriptConfiguration = configuration.getChild("scriptConfiguration");
+
+ // create all scripting engines
+
+ Configuration[] scriptEngineConfigurationList = configuration.getChild("scriptEngines").getChildren("scriptEngine");
+
+ for (int i=0; i<scriptEngineConfigurationList.length; i++)
+ {
+ ScriptEngineEntry scriptEngineEntry = this.createScriptEngineEntry(scriptEngineConfigurationList[i]);
+ this.getScriptEngineMap().put(scriptEngineEntry.getName(), scriptEngineEntry);
+ this.getLogger().debug("Added ScriptEngine for " + scriptEngineEntry.getName());
+
+ if( this.defaultEngineName == null)
+ {
+ this.defaultEngineName = scriptEngineEntry.getName();
+ }
+ }
+
+ // verify that at least one scripting engine is available
+
+ if( this.getScriptEngineMap().size() == 0 )
+ {
+ String msg = "At least one scripting engine must be defined";
+ this.getLogger().error(msg);
+ throw new ConfigurationException(msg);
+ }
+
+ // evaluate the scripts defined in the onLoad section
+
+ this.preloadScripts();
+ }
+
+ /**
+ * @see org.apache.avalon.framework.activity.Initializable#initialize()
+ */
+ public void initialize() throws Exception
+ {
+ ScriptAvalonContext avalonContext = this.createScriptAvalonContext();
+ this.scriptEngineManager.put("avalonContext", avalonContext);
+ }
+
+ /**
+ * @see org.apache.avalon.framework.configuration.Reconfigurable#reconfigure(org.apache.avalon.framework.configuration.Configuration)
+ */
+ public void reconfigure(Configuration cfg) throws ConfigurationException
+ {
+ super.reconfigure(cfg);
+ this.defaultEngineName = null;
+ this.scriptConfiguration = null;
+ this.getScriptEngineMap().clear();
+ this.getScriptCache().clear();
+ this.configure(cfg);
+ }
+
+ /**
+ * @see org.apache.avalon.framework.activity.Disposable#dispose()
+ */
+ public void dispose()
+ {
+ super.dispose();
+ this.scriptCache.clear();
+ this.scriptCache = null;
+ this.excludedServices = null;
+ }
+
+ /////////////////////////////////////////////////////////////////////////
+ // Service interface implementation
+ /////////////////////////////////////////////////////////////////////////
+
+ /**
+ * @see org.apache.fulcrum.script.ScriptService#getScriptEngineManager()
+ */
+ public ScriptEngineManager getScriptEngineManager()
+ {
+ return this.scriptEngineManager;
+ }
+
+ /**
+ * @see org.apache.fulcrum.script.ScriptService#getScriptEngine()
+ */
+ public ScriptEngine getScriptEngine()
+ {
+ return this.getScriptEngine(this.getDefaultEngineName());
+ }
+
+ /**
+ * @see org.apache.fulcrum.script.ScriptService#eval(java.lang.String, javax.script.Namespace)
+ */
+ public Object eval(String scriptName, Namespace namespace)
+ throws IOException, ScriptException
+ {
+ Validate.notEmpty(scriptName, "scriptName");
+ Validate.notNull(namespace, "namespace");
+ String currScriptName = this.makeScriptName(scriptName);
+ ScriptCacheEntry scriptCacheEntry = this.createScriptCacheEntry(currScriptName);
+ Object result = this.doEval( scriptCacheEntry, namespace, null );
+ return result;
+
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.fulcrum.script.ScriptService#eval(java.lang.String, javax.script.ScriptContext)
+ */
+ public Object eval(String scriptName, ScriptContext context)
+ throws IOException, ScriptException
+ {
+ Validate.notEmpty(scriptName, "scriptName");
+ Validate.notNull(context, "context");
+ String currScriptName = this.makeScriptName(scriptName);
+ ScriptCacheEntry scriptCacheEntry = this.createScriptCacheEntry(currScriptName);
+ ScriptAvalonContext avalonContext = this.createScriptAvalonContext();
+ context.setAttribute("avalonContext", avalonContext, ScriptContext.ENGINE_SCOPE);
+ Object result = this.doEval( scriptCacheEntry, null, context );
+ return result;
+ }
+
+ /**
+ * @see org.apache.fulcrum.script.ScriptService#eval(java.lang.String)
+ */
+ public Object eval(String scriptName) throws IOException, ScriptException
+ {
+ Validate.notEmpty(scriptName, "scriptName");
+ String currScriptName = this.makeScriptName(scriptName);
+ ScriptCacheEntry scriptCacheEntry = this.createScriptCacheEntry(currScriptName);
+ Object result = this.doEval( scriptCacheEntry, null, null );
+ return result;
+ }
+
+ /**
+ * @see org.apache.fulcrum.script.ScriptService#call(java.lang.String, java.lang.Object[])
+ */
+ public Object call(String name, Object [] args) throws ScriptException,
+ NoSuchMethodException
+ {
+ Validate.notEmpty(name, "name");
+ return this.call(this.getDefaultEngineName(), name, args);
+
+ }
+
+ /**
+ * @see org.apache.fulcrum.script.ScriptService#getInterface(java.lang.Class)
+ */
+ public Object getInterface(Class clazz)
+ {
+ return this.getInterface(this.getDefaultEngineName(), clazz);
+ }
+
+ /**
+ * @see org.apache.fulcrum.script.ScriptService#exists(java.lang.String)
+ */
+ public boolean exists(String name)
+ {
+ Validate.notEmpty(name, "name");
+
+ String scriptName = this.makeScriptName(name);
+ String engineName = this.getEngineName(scriptName);
+ ScriptEngineEntry scriptEngineEntry = this.getScriptEngineEntry(engineName);
+ String location = scriptEngineEntry.getLocation();
+
+ if( this.getResourceManagerService().exists(location) &&
+ this.getResourceManagerService().exists(location, scriptName) )
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+
+ /////////////////////////////////////////////////////////////////////////
+ // Service Implementation
+ /////////////////////////////////////////////////////////////////////////
+
+ private ScriptEngine getScriptEngine(String engineName)
+ {
+ ScriptEngineEntry scriptEngineEntry = this.getScriptEngineEntry(engineName);
+ return scriptEngineEntry.getScriptEngine();
+ }
+
+ /**
+ * @param engineName the engine name
+ * @param clazz the inteface to implement
+ * @return a scripting object implementing the given interface
+ */
+ private Object call(String engineName, String name, Object[] args)
+ throws ScriptException, NoSuchMethodException
+ {
+ Validate.notEmpty(engineName, "engineName");
+
+ Object result = this.doCall(engineName, name, args);
+ return result;
+ }
+
+ /**
+ * @param engineName the engine name
+ * @param clazz the inteface to implement
+ * @return a scripting object implementing the given interface
+ */
+ private Object getInterface(String engineName, Class clazz)
+ {
+ Validate.notEmpty(engineName, "engineName");
+ Validate.notNull(clazz, "clazz");
+
+ Object result = this.doGetInterface(engineName, clazz);
+ return result;
+ }
+
+ /**
+ * @return an instance of the ResourceService
+ */
+ private ResourceManagerService getResourceManagerService()
+ {
+ try
+ {
+ return (ResourceManagerService) this.getServiceManager().lookup(
+ ResourceManagerService.class.getName()
+ );
+ }
+ catch (ServiceException e)
+ {
+ throw new RuntimeException( e.getMessage() );
+ }
+ }
+
+ /**
+ * Load a script from the persistent storage.
+ *
+ * @param scriptName the name of the Groovy script
+ * @return the plain content of the script
+ */
+ private String loadScript( String engineName, String scriptName )
+ throws IOException
+ {
+ ScriptEngineEntry scriptEngineEntry = this.getScriptEngineEntry(engineName);
+ String location = scriptEngineEntry.getLocation();
+
+ String result = null;
+ byte[] scriptContent = null;
+ String[] sriptContext = {};
+
+ scriptContent = this.getResourceManagerService().read(
+ location,
+ sriptContext,
+ scriptName
+ );
+
+ result = new String( scriptContent );
+ return result;
+ }
+
+ /**
+ * Creates a compiled script.
+ *
+ * @param scriptName the name of the scipt to compile
+ * @param scriptContent the name scipt to compile
+ * @return the compiled script
+ * @throws ScriptException failed to compile the script
+ */
+ private CompiledScript compileScript( String engineName, String scriptName, String scriptContent )
+ throws ScriptException
+ {
+ CompiledScript result = null;
+
+ try
+ {
+ ScriptEngineEntry scriptEngineEntry = this.getScriptEngineEntry(engineName);
+ Compilable compilable = (Compilable) scriptEngineEntry.getScriptEngine();
+ long startTime = System.currentTimeMillis();
+ result = compilable.compile(scriptContent);
+ long endTime = System.currentTimeMillis();
+
+ if( this.getLogger().isDebugEnabled() )
+ {
+ this.getLogger().debug(
+ "Compiling of " + scriptName + " took "
+ + (endTime - startTime) + " ms"
+ );
+ }
+
+ return result;
+ }
+ catch (ScriptException e)
+ {
+ String msg = "Compiling the script failed : " + scriptName;
+ this.getLogger().error( msg, e );
+ throw new RuntimeException( msg ,e );
+ }
+ }
+
+ /**
+ * Evaluates a script.
+ *
+ * @param scriptCacheEntry the script to run
+ * @param namespace the parameters to pass to the script
+ * @return the result of the executed script
+ */
+ private Object doEval( ScriptCacheEntry scriptCacheEntry, Namespace namespace, ScriptContext scriptContext ) throws ScriptException
+ {
+ Object result = null;
+ String scriptName = scriptCacheEntry.getScriptName();
+ ScriptEngineEntry scriptEngineEntry = this.getScriptEngineEntry(scriptCacheEntry.getEngineName());
+ ScriptEngine scriptEngine = scriptEngineEntry.getScriptEngine();
+
+ try
+ {
+ long startTime = System.currentTimeMillis();
+
+ if( scriptCacheEntry.isCompiled() )
+ {
+ if( namespace != null )
+ {
+ result = scriptCacheEntry.getCompiledScript().eval(namespace);
+ }
+ else if( scriptContext != null )
+ {
+ result = scriptCacheEntry.getCompiledScript().eval(scriptContext);
+ }
+ else
+ {
+ result = scriptCacheEntry.getCompiledScript().eval();
+ }
+ }
+ else
+ {
+ if( namespace != null )
+ {
+ result = scriptEngine.eval(scriptCacheEntry.getPlainScript(), namespace);
+ }
+ else if( scriptContext != null )
+ {
+ result = scriptEngine.eval(scriptCacheEntry.getPlainScript(), scriptContext);
+ }
+ else
+ {
+ result = scriptEngine.eval(scriptCacheEntry.getPlainScript());
+ }
+ }
+
+ long endTime = System.currentTimeMillis();
+
+ if( this.getLogger().isDebugEnabled() )
+ {
+ this.getLogger().debug(
+ "Execution of " + scriptName + " took "
+ + (endTime - startTime) + " ms"
+ );
+ }
+
+ return result;
+ }
+ catch( ScriptException e )
+ {
+ String msg= "Execution of the script failed : " + scriptName;
+ this.getLogger().error( msg, e );
+ throw e;
+ }
+ }
+
+ /**
+ * Invokes a global script method
+ */
+ private Object doCall( String engineName, String name, Object[] args )
+ throws ScriptException, NoSuchMethodException
+ {
+ Object result = null;
+ ScriptEngineEntry scriptEngineEntry = this.getScriptEngineEntry(engineName);
+ ScriptEngine scriptEngine = scriptEngineEntry.getScriptEngine();
+ Invocable invocable = (Invocable) scriptEngine;
+
+ try
+ {
+ long startTime = System.currentTimeMillis();
+ result = invocable.call(name, args);
+ long endTime = System.currentTimeMillis();
+
+ if( this.getLogger().isDebugEnabled() )
+ {
+ this.getLogger().debug(
+ "Calling the method " + name + " took "
+ + (endTime - startTime) + " ms"
+ );
+ }
+
+ return result;
+ }
+ catch( ScriptException e )
+ {
+ String msg= "Caling the following method failed : " + name;
+ this.getLogger().error( msg, e );
+ throw e;
+ }
+ catch( NoSuchMethodException e )
+ {
+ String msg= "Caling the following method failed : " + name;
+ this.getLogger().error( msg, e );
+ throw e;
+ }
+ }
+
+ /**
+ * Creates a script object implementing the requested interface.
+ *
+ * @param engineName the name of the scripting engine
+ * @param clazz the requested interface
+ * @return the result of the executed script
+ */
+ private Object doGetInterface( String engineName, Class clazz )
+ {
+ Object result = null;
+ ScriptEngineEntry scriptEngineEntry = this.getScriptEngineEntry(engineName);
+ ScriptEngine scriptEngine = scriptEngineEntry.getScriptEngine();
+ Invocable invocable = (Invocable) scriptEngine;
+
+ try
+ {
+ long startTime = System.currentTimeMillis();
+ result = invocable.getInterface(clazz);
+ long endTime = System.currentTimeMillis();
+
+ if( this.getLogger().isDebugEnabled() )
+ {
+ this.getLogger().debug(
+ "Creating the interface for " + clazz.getName() + " took "
+ + (endTime - startTime) + " ms"
+ );
+ }
+
+ return result;
+ }
+ catch( RuntimeException e )
+ {
+ String msg= "Creation of the interface failed : " + clazz.getName();
+ this.getLogger().error( msg, e );
+ throw e;
+ }
+ }
+
+ /**
+ * Create the Avalon specific context
+ */
+ private ScriptAvalonContext createScriptAvalonContext()
+ {
+ ScriptAvalonContextImpl result = null;
+
+ ScriptServiceManagerImpl scriptServiceManager = new ScriptServiceManagerImpl(
+ this.getServiceManager(),
+ this.excludedServices
+ );
+
+ result = new ScriptAvalonContextImpl(
+ this.getLogger().getChildLogger("script"),
+ scriptServiceManager,
+ this.getContext(),
+ this.scriptConfiguration,
+ this.getParameters()
+ );
+
+ return result;
+ }
+
+ /**
+ * @return the name of the default engine
+ */
+ private String getDefaultEngineName()
+ {
+ return this.defaultEngineName;
+ }
+
+ /**
+ * @return Returns the scriptCache.
+ */
+ private ScriptCache getScriptCache()
+ {
+ return scriptCache;
+ }
+
+ /**
+ * Creates a script to be evaluates.
+ *
+ * @param scriptName the name of the script to execute
+ * @return ScriptCacheEntry
+ */
+ private synchronized ScriptCacheEntry createScriptCacheEntry(String scriptName)
+ throws IOException, ScriptException
+ {
+ String engineName = this.getEngineName(scriptName);
+ ScriptCacheEntry scriptCacheEntry = null;
+ ScriptEngineEntry scriptEngineEntry = this.getScriptEngineEntry(engineName);
+ String plainScript = null;
+ CompiledScript compiledScript = null;
+ boolean isInCache = false;
+
+ // try to load the script from the cache or from the resource manager
+
+ if( scriptEngineEntry.isCached() == true )
+ {
+ scriptCacheEntry = this.getScriptCache().get(scriptName);
+
+ if( scriptCacheEntry == null )
+ {
+ plainScript = this.loadScript(engineName, scriptName);
+ }
+ else
+ {
+ plainScript = scriptCacheEntry.getPlainScript();
+ isInCache = true;
+ }
+ }
+ else
+ {
+ plainScript = this.loadScript(engineName, scriptName);
+ }
+
+ if( isInCache == false )
+ {
+ // compile the script if required
+
+ if( scriptEngineEntry.isCompiled())
+ {
+ compiledScript = this.compileScript(engineName, scriptName, plainScript);
+ }
+
+ scriptCacheEntry = new ScriptCacheEntry(
+ engineName,
+ scriptName,
+ plainScript,
+ compiledScript
+ );
+
+ // update the cache
+
+ if( scriptEngineEntry.isCached() )
+ {
+ this.getScriptCache().put(scriptCacheEntry);
+ }
+ }
+
+ return scriptCacheEntry;
+ }
+
+ /**
+ * Create a ScriptEngineEntry by parsing the configuration.
+ *
+ * @param configuration the configuration
+ * @return ScriptEngineEntry
+ * @throws ConfigurationException parsing the configuration failed
+ */
+ private ScriptEngineEntry createScriptEngineEntry(Configuration configuration)
+ throws ConfigurationException
+ {
+ String name = configuration.getChild("name").getValue();
+ boolean isCached = configuration.getChild("isCached").getValueAsBoolean(true);
+ boolean isCompiled = configuration.getChild("isCompiled").getValueAsBoolean(true);
+ String location = configuration.getChild("location").getValue(name);
+ ScriptEngine scriptEngine = this.getScriptEngineManager().getEngineByName(name);
+
+ if( (scriptEngine instanceof Compilable) == false )
+ {
+ isCompiled = false;
+ }
+
+ ScriptEngineEntry result = new ScriptEngineEntry(name, isCached, isCompiled, location, scriptEngine);
+
+ // evaluate the given scripts
+
+ ArrayList scriptList = new ArrayList();
+ Configuration[] scriptListConfiguration = configuration.getChild("preLoad").getChildren("script");
+ for( int i=0; i<scriptListConfiguration.length; i++ )
+ {
+ scriptList.add(scriptListConfiguration[i].getValue());
+ }
+
+ result.setScriptList(scriptList);
+
+ return result;
+ }
+
+ /**
+ * @param engineName the name of the scriptng engine
+ * @return the ScriptEngineEntry
+ */
+ private ScriptEngineEntry getScriptEngineEntry( String engineName)
+ {
+ ScriptEngineEntry result = (ScriptEngineEntry) this.getScriptEngineMap().get(engineName);
+
+ if( result == null )
+ {
+ String msg = "Unable to handle scripts for the following script engine : " + engineName;
+ this.getLogger().error(msg);
+ throw new IllegalArgumentException(msg);
+ }
+
+ return result;
+ }
+
+ /**
+ * Determines the name of the scripting engine by looking at the extension
+ *
+ * @param scriptName the script name
+ * @return name of the scripting engine
+ */
+ private String getEngineName( String scriptName )
+ {
+ int pos = scriptName.lastIndexOf('.');
+
+ if( pos > 0 )
+ {
+ return scriptName.substring(pos+1, scriptName.length());
+ }
+ else
+ {
+ return this.getDefaultEngineName();
+ }
+ }
+
+ /**
+ * @return Returns the scriptEngineMap.
+ */
+ private HashMap getScriptEngineMap()
+ {
+ return scriptEngineMap;
+ }
+
+ /**
+ * Ensure that the name has a proper extension.
+ *
+ * @param name the name of the script
+ * @return script name
+ */
+ private String makeScriptName( String name )
+ {
+ String engineName = this.getEngineName(name);
+ String extension = '.' + engineName;
+
+ if( name.endsWith(extension) )
+ {
+ return name;
+ }
+ else
+ {
+ return name + extension;
+ }
+ }
+
+ /**
+ * Preload the scripts defined in the onLoad section
+ */
+ private void preloadScripts()
+ {
+ Iterator iter = this.getScriptEngineMap().keySet().iterator();
+
+ while( iter.hasNext() )
+ {
+ String engineName = (String) iter.next();
+ ScriptEngineEntry scriptEngineEntry = this.getScriptEngineEntry(engineName);
+ ScriptEngine scriptEngine = scriptEngineEntry.getScriptEngine();
+ Object[] scriptList = scriptEngineEntry.getScriptList().toArray();
+
+ for( int i=0; i<scriptList.length; i++ )
+ {
+ String scriptName = scriptList[i].toString();
+
+ try
+ {
+ String plainScript = this.loadScript(engineName, scriptName);
+ scriptEngine.eval(plainScript);
+ }
+ catch (Exception e)
+ {
+ String msg = "Unable to load the following script : " + scriptName;
+ this.getLogger().error(msg, e);
+ throw new RuntimeException(msg);
+ }
+ }
+ }
+ }
+}
Modified: jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptServiceManagerImpl.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptServiceManagerImpl.java?view=diff&rev=535465&r1=535464&r2=535465
==============================================================================
--- jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptServiceManagerImpl.java (original)
+++ jakarta/turbine/fulcrum/trunk/script/src/java/org/apache/fulcrum/script/impl/ScriptServiceManagerImpl.java Fri May 4 23:58:06 2007
@@ -1,90 +1,92 @@
-package org.apache.fulcrum.script.impl;
-
-/*
- * Copyright 2005 Apache Software Foundation
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- * implied.
- *
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import java.util.Set;
-
-import org.apache.avalon.framework.service.ServiceException;
-import org.apache.avalon.framework.service.ServiceManager;
-
-
-/**
- * A decorator for the ServiceManager interface to restrict access
- * to Avalon services.
- *
- * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
- */
-public class ScriptServiceManagerImpl implements ServiceManager
-{
- /** the Avalon service manager to lookup service */
- private ServiceManager serviceManager;
-
- /** the excluded services */
- private Set excludedServices;
-
- /**
- * Constructor.
- *
- * @param serviceManager the Avalon service manager to lookup service
- * @param excludedServices the excluded services
- */
- public ScriptServiceManagerImpl(ServiceManager serviceManager, Set excludedServices)
- {
- this.serviceManager = serviceManager;
- this.excludedServices = excludedServices;
- }
-
- /**
- * @see org.apache.avalon.framework.service.ServiceManager#hasService(java.lang.String)
- */
- public boolean hasService(String arg)
- {
- if( this.excludedServices.contains(arg) )
- {
- return false;
- }
- else
- {
- return this.serviceManager.hasService(arg);
- }
- }
-
- /**
- * @see org.apache.avalon.framework.service.ServiceManager#lookup(java.lang.String)
- */
- public Object lookup(String arg) throws ServiceException
- {
- if( this.excludedServices.contains(arg) )
- {
- // just add a space at the end to enforce a ServiceException
- return this.serviceManager.lookup(arg + ' ');
- }
- else
- {
- return this.serviceManager.lookup(arg);
- }
- }
-
- /**
- * @see org.apache.avalon.framework.service.ServiceManager#release(java.lang.Object)
- */
- public void release(Object obj)
- {
- this.serviceManager.release(obj);
- }
-}
+package org.apache.fulcrum.script.impl;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.Set;
+
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+
+
+/**
+ * A decorator for the ServiceManager interface to restrict access
+ * to Avalon services.
+ *
+ * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
+ */
+public class ScriptServiceManagerImpl implements ServiceManager
+{
+ /** the Avalon service manager to lookup service */
+ private ServiceManager serviceManager;
+
+ /** the excluded services */
+ private Set excludedServices;
+
+ /**
+ * Constructor.
+ *
+ * @param serviceManager the Avalon service manager to lookup service
+ * @param excludedServices the excluded services
+ */
+ public ScriptServiceManagerImpl(ServiceManager serviceManager, Set excludedServices)
+ {
+ this.serviceManager = serviceManager;
+ this.excludedServices = excludedServices;
+ }
+
+ /**
+ * @see org.apache.avalon.framework.service.ServiceManager#hasService(java.lang.String)
+ */
+ public boolean hasService(String arg)
+ {
+ if( this.excludedServices.contains(arg) )
+ {
+ return false;
+ }
+ else
+ {
+ return this.serviceManager.hasService(arg);
+ }
+ }
+
+ /**
+ * @see org.apache.avalon.framework.service.ServiceManager#lookup(java.lang.String)
+ */
+ public Object lookup(String arg) throws ServiceException
+ {
+ if( this.excludedServices.contains(arg) )
+ {
+ // just add a space at the end to enforce a ServiceException
+ return this.serviceManager.lookup(arg + ' ');
+ }
+ else
+ {
+ return this.serviceManager.lookup(arg);
+ }
+ }
+
+ /**
+ * @see org.apache.avalon.framework.service.ServiceManager#release(java.lang.Object)
+ */
+ public void release(Object obj)
+ {
+ this.serviceManager.release(obj);
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org