View Javadoc

1   /*
2    *  Copyright (C) 2004  The Concord Consortium, Inc.,
3    *  10 Concord Crossing, Concord, MA 01741
4    *
5    *  Web Site: http://www.concord.org
6    *  Email: info@concord.org
7    *
8    *  This library is free software; you can redistribute it and/or
9    *  modify it under the terms of the GNU Lesser General Public
10   *  License as published by the Free Software Foundation; either
11   *  version 2.1 of the License, or (at your option) any later version.
12   *
13   *  This library is distributed in the hope that it will be useful,
14   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16   *  Lesser General Public License for more details.
17   *
18   *  You should have received a copy of the GNU Lesser General Public
19   *  License along with this library; if not, write to the Free Software
20   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21   *
22   */
23  
24  package net.sf.sail.core.activity.model;
25  
26  import java.beans.beancontext.BeanContext;
27  import java.beans.beancontext.BeanContextChildSupport;
28  
29  import javax.script.Namespace;
30  import javax.script.ScriptContext;
31  import javax.script.ScriptException;
32  
33  public class ScriptBeanSupport
34  extends BeanContextChildSupport
35  implements IScriptBean
36  {
37  	private static final long serialVersionUID = 1L;
38      protected String name;
39  	protected IScript script;
40  	protected Class customizerClass;
41  	
42  	public ScriptBeanSupport()
43  	{
44  	}
45     
46  	public String getName()
47  	{
48  		return name;
49  	}
50  	
51  	public void setName(String value)
52  	{
53  		name = value;
54  	}
55  	
56  	public void setScriptParent(BeanContext parent)
57  	{
58  		if (parent instanceof ScriptBeanContext)
59  		{
60  		    ScriptBeanContext scriptBeanContext = (ScriptBeanContext) parent;
61  			ScriptContext parentContext = scriptBeanContext.getScript().getScriptContext();
62  			ScriptContext context = script.getScriptContext();
63  			Namespace parentNamespace = parentContext.getNamespace(ENGINE_SCOPE);
64  			Namespace namespace = context.getNamespace(ENGINE_SCOPE);
65  			if (namespace instanceof ScopeNamespace)
66  			{
67  				((ScopeNamespace) namespace).setParent((ScopeNamespace) parentNamespace);
68  			}
69  			else if (parentNamespace instanceof ScopeNamespace)
70  			{
71  				namespace = new ScopeNamespace((ScopeNamespace) parentNamespace);
72  			}
73  			context.setNamespace(namespace, ENGINE_SCOPE);
74  		}
75  	}
76  
77  	public BeanContext getScriptParent()
78  	{
79  		return getBeanContext();
80  	}
81  	
82  	public Object evaluate()
83  	throws ScriptException
84  	{
85  	    return (script == null) ?  null: script.evaluate();
86  	}
87  	
88  	public Class getCustomizerClass()
89  	{
90  		return customizerClass;
91  	}
92  	
93  	public void setCustomizerClass(Class customizer)
94  	{
95  		customizerClass = customizer;
96  	}
97  
98      /*
99       * @see net.sf.sail.test.activity.model.ScriptBean#getScript()
100      */
101     public IScript getScript()
102     {
103         return script;
104     }
105 	
106     /*
107      * @see net.sf.sail.test.activity.model.ScriptBean#setScript(net.sf.sail.test.activity.model.Script)
108      */
109     public void setScript(IScript script)
110     {
111         this.script = script;
112     }
113 }