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.Customizer;
27  import java.beans.beancontext.BeanContext;
28  import java.beans.beancontext.BeanContextChildSupport;
29  
30  import javax.script.Namespace;
31  import javax.script.ScriptContext;
32  import javax.script.ScriptException;
33  
34  public class ScriptBeanSupport
35  extends BeanContextChildSupport
36  implements IScriptBean
37  {
38  	private static final long serialVersionUID = 1L;
39      protected String name;
40  	protected IScript script;
41  	protected Class<? extends Customizer> customizerClass;
42  	
43  	public ScriptBeanSupport()
44  	{
45  	}
46     
47  	public String getName()
48  	{
49  		return name;
50  	}
51  	
52  	public void setName(String value)
53  	{
54  		name = value;
55  	}
56  	
57  	public void setScriptParent(BeanContext parent)
58  	{
59  		if (parent instanceof ScriptBeanContext)
60  		{
61  		    ScriptBeanContext scriptBeanContext = (ScriptBeanContext) parent;
62  			ScriptContext parentContext = scriptBeanContext.getScript().getScriptContext();
63  			ScriptContext context = script.getScriptContext();
64  			Namespace parentNamespace = parentContext.getNamespace(ENGINE_SCOPE);
65  			Namespace namespace = context.getNamespace(ENGINE_SCOPE);
66  			if (namespace instanceof ScopeNamespace)
67  			{
68  				((ScopeNamespace) namespace).setParent((ScopeNamespace) parentNamespace);
69  			}
70  			else if (parentNamespace instanceof ScopeNamespace)
71  			{
72  				namespace = new ScopeNamespace((ScopeNamespace) parentNamespace);
73  			}
74  			context.setNamespace(namespace, ENGINE_SCOPE);
75  		}
76  	}
77  
78  	public BeanContext getScriptParent()
79  	{
80  		return getBeanContext();
81  	}
82  	
83  	public Object evaluate()
84  	throws ScriptException
85  	{
86  	    return (script == null) ?  null: script.evaluate();
87  	}
88  	
89  	public Class<? extends Customizer> getCustomizerClass()
90  	{
91  		return customizerClass;
92  	}
93  	
94  	public void setCustomizerClass(Class<? extends Customizer> customizer)
95  	{
96  		customizerClass = customizer;
97  	}
98  
99      /*
100      * @see net.sf.sail.test.activity.model.ScriptBean#getScript()
101      */
102     public IScript getScript()
103     {
104         return script;
105     }
106 	
107     /*
108      * @see net.sf.sail.test.activity.model.ScriptBean#setScript(net.sf.sail.test.activity.model.Script)
109      */
110     public void setScript(IScript script)
111     {
112         this.script = script;
113     }
114 }