Clover coverage report - Code Coverage for tapestry release 4.0-beta-8
Coverage timestamp: Sat Sep 24 2005 11:50:34 EDT
file stats: LOC: 125   Methods: 4
NCLOC: 64   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ListenerBinding.java - 95% 75% 91.7%
coverage coverage
 1    // Copyright 2004, 2005 The Apache Software Foundation
 2    //
 3    // Licensed under the Apache License, Version 2.0 (the "License");
 4    // you may not use this file except in compliance with the License.
 5    // You may obtain a copy of the License at
 6    //
 7    // http://www.apache.org/licenses/LICENSE-2.0
 8    //
 9    // Unless required by applicable law or agreed to in writing, software
 10    // distributed under the License is distributed on an "AS IS" BASIS,
 11    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12    // See the License for the specific language governing permissions and
 13    // limitations under the License.
 14   
 15    package org.apache.tapestry.binding;
 16   
 17    import org.apache.bsf.BSFException;
 18    import org.apache.bsf.BSFManager;
 19    import org.apache.hivemind.ApplicationRuntimeException;
 20    import org.apache.hivemind.Location;
 21    import org.apache.hivemind.util.Defense;
 22    import org.apache.tapestry.IActionListener;
 23    import org.apache.tapestry.IComponent;
 24    import org.apache.tapestry.IPage;
 25    import org.apache.tapestry.IRequestCycle;
 26    import org.apache.tapestry.Tapestry;
 27    import org.apache.tapestry.coerce.ValueConverter;
 28    import org.apache.tapestry.services.BSFManagerFactory;
 29   
 30    /**
 31    * A very specialized binding that can be used as an {@link org.apache.tapestry.IActionListener},
 32    * executing a script in a scripting language, via <a href="http://jakarta.apache.org/bsf">Bean
 33    * Scripting Framework </a>.
 34    *
 35    * @author Howard Lewis Ship
 36    * @since 3.0
 37    */
 38   
 39    public class ListenerBinding extends AbstractBinding implements IActionListener
 40    {
 41    private final String _language;
 42   
 43    private final String _script;
 44   
 45    private final IComponent _component;
 46   
 47    /** @since 4.0 */
 48   
 49    private BSFManagerFactory _managerFactory;
 50   
 51  4 public ListenerBinding(String description, ValueConverter valueConverter, Location location,
 52    IComponent component, String language, String script,
 53    BSFManagerFactory managerFactory)
 54    {
 55  4 super(description, valueConverter, location);
 56   
 57  4 Defense.notNull(component, "component");
 58  4 Defense.notNull(language, "language");
 59  4 Defense.notNull(script, "script");
 60   
 61  4 _component = component;
 62  4 _language = language;
 63  4 _script = script;
 64  4 _managerFactory = managerFactory;
 65    }
 66   
 67    /**
 68    * Returns this.
 69    */
 70   
 71  4 public Object getObject()
 72    {
 73  4 return this;
 74    }
 75   
 76    /**
 77    * A ListenerBinding is also a {@link org.apache.tapestry.IActionListener}. It registers a
 78    * number of beans with the BSF manager and invokes the script.
 79    * <p>
 80    * Registers the following bean:
 81    * <ul>
 82    * <li>component - the relevant {@link IComponent}, typically the same as the page
 83    * <li>page - the {@link IPage}trigged by the request (obtained by
 84    * {@link IRequestCycle#getPage()}
 85    * <li>cycle - the {@link IRequestCycle}, from which can be found the {@link IEngine}, etc.
 86    * </ul>
 87    */
 88   
 89  4 public void actionTriggered(IComponent component, IRequestCycle cycle)
 90    {
 91  4 BSFManager bsf = _managerFactory.createBSFManager();
 92   
 93  4 Location location = getLocation();
 94   
 95  4 try
 96    {
 97  4 IPage page = cycle.getPage();
 98   
 99  4 bsf.declareBean("component", _component, _component.getClass());
 100  4 bsf.declareBean("page", page, page.getClass());
 101  4 bsf.declareBean("cycle", cycle, cycle.getClass());
 102   
 103  4 bsf.exec(
 104    _language,
 105    location.getResource().toString(),
 106    location.getLineNumber(),
 107    location.getLineNumber(),
 108    _script);
 109    }
 110    catch (BSFException ex)
 111    {
 112  2 String message = Tapestry.format("ListenerBinding.bsf-exception", location, ex
 113    .getMessage());
 114   
 115  2 throw new ApplicationRuntimeException(message, _component, getLocation(), ex);
 116    }
 117    }
 118   
 119    /** @since 4.0 */
 120   
 121  0 public Object getComponent()
 122    {
 123  0 return _component;
 124    }
 125    }