Clover coverage report - Code Coverage for tapestry release 4.0-beta-2
Coverage timestamp: Sat Jul 9 2005 22:02:17 EDT
file stats: LOC: 126   Methods: 4
NCLOC: 65   Classes: 1
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover
 
 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.IEngine;
 25    import org.apache.tapestry.IPage;
 26    import org.apache.tapestry.IRequestCycle;
 27    import org.apache.tapestry.Tapestry;
 28    import org.apache.tapestry.coerce.ValueConverter;
 29    import org.apache.tapestry.services.BSFManagerFactory;
 30   
 31    /**
 32    * A very specialized binding that can be used as an {@link org.apache.tapestry.IActionListener},
 33    * executing a script in a scripting language, via <a href="http://jakarta.apache.org/bsf">Bean
 34    * Scripting Framework </a>.
 35    *
 36    * @author Howard Lewis Ship
 37    * @since 3.0
 38    */
 39   
 40    public class ListenerBinding extends AbstractBinding implements IActionListener
 41    {
 42    private final String _language;
 43   
 44    private final String _script;
 45   
 46    private final IComponent _component;
 47   
 48    /** @since 4.0 */
 49   
 50    private BSFManagerFactory _managerFactory;
 51   
 52  4 public ListenerBinding(String description, ValueConverter valueConverter, Location location,
 53    IComponent component, String language, String script,
 54    BSFManagerFactory managerFactory)
 55    {
 56  4 super(description, valueConverter, location);
 57   
 58  4 Defense.notNull(component, "component");
 59  4 Defense.notNull(language, "language");
 60  4 Defense.notNull(script, "script");
 61   
 62  4 _component = component;
 63  4 _language = language;
 64  4 _script = script;
 65  4 _managerFactory = managerFactory;
 66    }
 67   
 68    /**
 69    * Returns this.
 70    */
 71   
 72  4 public Object getObject()
 73    {
 74  4 return this;
 75    }
 76   
 77    /**
 78    * A ListenerBinding is also a {@link org.apache.tapestry.IActionListener}. It registers a
 79    * number of beans with the BSF manager and invokes the script.
 80    * <p>
 81    * Registers the following bean:
 82    * <ul>
 83    * <li>component - the relevant {@link IComponent}, typically the same as the page
 84    * <li>page - the {@link IPage}trigged by the request (obtained by
 85    * {@link IRequestCycle#getPage()}
 86    * <li>cycle - the {@link IRequestCycle}, from which can be found the {@link IEngine}, etc.
 87    * </ul>
 88    */
 89   
 90  4 public void actionTriggered(IComponent component, IRequestCycle cycle)
 91    {
 92  4 BSFManager bsf = _managerFactory.createBSFManager();
 93   
 94  4 Location location = getLocation();
 95   
 96  4 try
 97    {
 98  4 IPage page = cycle.getPage();
 99   
 100  4 bsf.declareBean("component", _component, _component.getClass());
 101  4 bsf.declareBean("page", page, page.getClass());
 102  4 bsf.declareBean("cycle", cycle, cycle.getClass());
 103   
 104  4 bsf.exec(
 105    _language,
 106    location.getResource().toString(),
 107    location.getLineNumber(),
 108    location.getLineNumber(),
 109    _script);
 110    }
 111    catch (BSFException ex)
 112    {
 113  2 String message = Tapestry.format("ListenerBinding.bsf-exception", location, ex
 114    .getMessage());
 115   
 116  2 throw new ApplicationRuntimeException(message, _component, getLocation(), ex);
 117    }
 118    }
 119   
 120    /** @since 4.0 */
 121   
 122  0 public Object getComponent()
 123    {
 124  0 return _component;
 125    }
 126    }