/* $$ Clover has instrumented this file $$ */// Copyright 2004 The 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.
package org.apache.tapestry.binding;
import org.apache.bsf.BSFException;
import org.apache.bsf.BSFManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.Location;
import org.apache.tapestry.BindingException;
import org.apache.tapestry.IActionListener;
import org.apache.tapestry.IComponent;
import org.apache.tapestry.IEngine;
import org.apache.tapestry.IPage;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.Tapestry;
import org.apache.tapestry.services.ObjectPool;
/**
* A very specialized binding that can be used as an {@link org.apache.tapestry.IActionListener},
* executing a script in a scripting language, via
* <a href="http://jakarta.apache.org/bsf">Bean Scripting Framework</a>.
*
* @author Howard Lewis Ship
* @since 3.0
*
**/
public class ListenerBinding extends AbstractBinding implements IActionListener
{public static com.cortexeb.tools.clover.d __CLOVER_53_0 = com.cortexeb.tools.clover.aq.getRecorder(new char[] {67,58,92,119,111,114,107,115,112,97,99,101,92,106,97,107,97,114,116,97,45,116,97,112,101,115,116,114,121,92,102,114,97,109,101,119,111,114,107,92,116,97,114,103,101,116,92,99,108,111,118,101,114,45,100,98},1096998272901L);
private static final Log LOG = LogFactory.getLog(ListenerBinding.class);
private static final String BSF_POOL_KEY = "org.apache.tapestry.BSFManager";
private String _language;
private String _script;
private IComponent _component;
public ListenerBinding(IComponent component, String language, String script, Location location)
{
super(location);__CLOVER_53_0.S[1329]++;try { __CLOVER_53_0.M[323]++;
__CLOVER_53_0.S[1330]++;_component = component;
__CLOVER_53_0.S[1331]++;_language = language;
__CLOVER_53_0.S[1332]++;_script = script;
} finally { }}
/**
* Always returns true.
*
**/
public boolean getBoolean()
{try { __CLOVER_53_0.M[324]++;
__CLOVER_53_0.S[1333]++;return true;
} finally { }}
public int getInt()
{try { __CLOVER_53_0.M[325]++;
__CLOVER_53_0.S[1334]++;throw new BindingException(
Tapestry.format("ListenerBinding.invalid-access", "getInt()"),
this);
} finally { }}
public double getDouble()
{try { __CLOVER_53_0.M[326]++;
__CLOVER_53_0.S[1335]++;throw new BindingException(
Tapestry.format("ListenerBinding.invalid-access", "getDouble()"),
this);
} finally { }}
/**
* Returns the underlying script.
*
**/
public String getString()
{try { __CLOVER_53_0.M[327]++;
__CLOVER_53_0.S[1336]++;return _script;
} finally { }}
/**
* Returns this.
*
**/
public Object getObject()
{try { __CLOVER_53_0.M[328]++;
__CLOVER_53_0.S[1337]++;return this;
} finally { }}
/**
* A ListenerBinding is also a {@link org.apache.tapestry.IActionListener}. It
* registers a number of beans with the BSF manager and invokes the
* script.
*
* <p>
* Registers the following bean:
* <ul>
* <li>component - the relevant {@link IComponent}, typically the same as the page
* <li>page - the {@link IPage} trigged by the request (obtained by {@link IRequestCycle#getPage()}
* <li>cycle - the {@link IRequestCycle}, from which can be found
* the {@link IEngine}, etc.
* </ul>
*
**/
public void actionTriggered(IComponent component, IRequestCycle cycle)
{try { __CLOVER_53_0.M[329]++;
__CLOVER_53_0.S[1338]++;boolean debug = LOG.isDebugEnabled();
__CLOVER_53_0.S[1339]++;long startTime = (((debug ) && (++__CLOVER_53_0.CT[264] != 0)) || (++__CLOVER_53_0.CF[264] == 0))? System.currentTimeMillis() : 0;
__CLOVER_53_0.S[1340]++;BSFManager bsf = obtainBSFManager(cycle);
__CLOVER_53_0.S[1341]++;Location location = getLocation();
__CLOVER_53_0.S[1342]++;try
{
__CLOVER_53_0.S[1343]++;IPage page = cycle.getPage();
__CLOVER_53_0.S[1344]++;bsf.declareBean("component", _component, _component.getClass());
__CLOVER_53_0.S[1345]++;bsf.declareBean("page", page, page.getClass());
__CLOVER_53_0.S[1346]++;bsf.declareBean("cycle", cycle, cycle.getClass());
__CLOVER_53_0.S[1347]++;bsf.exec(
_language,
location.getResource().toString(),
location.getLineNumber(),
location.getLineNumber(),
_script);
}
catch (BSFException ex)
{
__CLOVER_53_0.S[1348]++;String message =
Tapestry.format("ListenerBinding.bsf-exception", location, ex.getMessage());
__CLOVER_53_0.S[1349]++;throw new ApplicationRuntimeException(message, _component, getLocation(), ex);
}
finally
{
__CLOVER_53_0.S[1350]++;if ((((LOG.isDebugEnabled()) && (++__CLOVER_53_0.CT[265] != 0)) || (++__CLOVER_53_0.CF[265] == 0))){
__CLOVER_53_0.S[1351]++;LOG.debug("Cleaning up " + bsf);}
__CLOVER_53_0.S[1352]++;undeclare(bsf, "component");
__CLOVER_53_0.S[1353]++;undeclare(bsf, "page");
__CLOVER_53_0.S[1354]++;undeclare(bsf, "cycle");
__CLOVER_53_0.S[1355]++;cycle.getEngine().getPool().store(BSF_POOL_KEY, bsf);
__CLOVER_53_0.S[1356]++;if ((((debug) && (++__CLOVER_53_0.CT[266] != 0)) || (++__CLOVER_53_0.CF[266] == 0))){
{
__CLOVER_53_0.S[1357]++;long endTime = System.currentTimeMillis();
__CLOVER_53_0.S[1358]++;LOG.debug(
"Execution of \"" + location + "\" took " + (endTime - startTime) + " millis");
}}
}
} finally { }}
private void undeclare(BSFManager bsf, String name)
{try { __CLOVER_53_0.M[330]++;
__CLOVER_53_0.S[1359]++;try
{
__CLOVER_53_0.S[1360]++;bsf.undeclareBean(name);
}
catch (BSFException ex)
{
__CLOVER_53_0.S[1361]++;LOG.warn(Tapestry.format("ListenerBinding.unable-to-undeclare-bean", ex));
}
} finally { }}
/**
* TODO: remove the use of the pool; the BSFManager should be injected in, and be
* a pooled HiveMind service.
*/
private BSFManager obtainBSFManager(IRequestCycle cycle)
{try { __CLOVER_53_0.M[331]++;
__CLOVER_53_0.S[1362]++;IEngine engine = cycle.getEngine();
__CLOVER_53_0.S[1363]++;ObjectPool pool = engine.getPool();
__CLOVER_53_0.S[1364]++;BSFManager result = (BSFManager) pool.get(BSF_POOL_KEY);
__CLOVER_53_0.S[1365]++;if ((((result == null) && (++__CLOVER_53_0.CT[267] != 0)) || (++__CLOVER_53_0.CF[267] == 0))){
{
__CLOVER_53_0.S[1366]++;LOG.debug("Creating new BSFManager instance.");
__CLOVER_53_0.S[1367]++;result = new BSFManager();
__CLOVER_53_0.S[1368]++;result.setClassLoader(engine.getClassResolver().getClassLoader());
}}
__CLOVER_53_0.S[1369]++;return result;
} finally { }}
}