001: /*
002: * $Id: MatchRegExpImpl.java,v 1.5 2007/03/16 10:06:08 agoubard Exp $
003: */
004: package com.mycompany.toolbox.api;
005:
006: import java.io.IOException;
007: import java.io.InputStream;
008: import java.util.Map;
009:
010: import javax.script.Bindings;
011: import javax.script.ScriptEngine;
012: import javax.script.ScriptEngineManager;
013: import javax.script.SimpleBindings;
014:
015: import org.xins.common.BeanUtils;
016: import org.xins.common.collections.InvalidPropertyValueException;
017: import org.xins.common.collections.MissingRequiredPropertyException;
018: import org.xins.common.collections.PropertyReader;
019: import org.xins.common.io.IOReader;
020: import org.xins.common.manageable.InitializationException;
021:
022: /**
023: * Implementation of the <code>MatchRegExp</code> function.
024: * This example is using the new JavaSE 6 javax.script API, meaning that in order
025: * to compile and run this API you need at least JavaSE 6:
026: * http://java.sun.com/javase/downloads/index.jsp
027: *
028: * <p>Description: Returns whether a text matches a regular expression.
029: *
030: * @version $Revision: 1.5 $ $Date: 2007/03/16 10:06:08 $
031: * @author TODO
032: */
033: public final class MatchRegExpImpl extends MatchRegExp {
034:
035: /**
036: * The scripting engine.
037: */
038: private ScriptEngine _groovyEngine;
039:
040: /**
041: * The script.
042: */
043: private String _matchScript;
044:
045: /**
046: * Constructs a new <code>MatchRegExpImpl</code> instance.
047: *
048: * @param api
049: * the API to which this function belongs, guaranteed to be not
050: * <code>null</code>.
051: */
052: public MatchRegExpImpl(APIImpl api) {
053: super (api);
054: }
055:
056: protected void initImpl(PropertyReader properties)
057: throws MissingRequiredPropertyException,
058: InvalidPropertyValueException, InitializationException {
059: ScriptEngineManager scriptingManager = new ScriptEngineManager();
060: _groovyEngine = scriptingManager.getEngineByExtension("groovy");
061: if (_groovyEngine == null) {
062: throw new InitializationException(
063: "Cannot find scripting library.");
064: }
065: try {
066: InputStream matchStream = getAPI()
067: .getResourceAsStream(
068: "/WEB-INF/classes/com/mycompany/toolbox/api/match.groovy");
069: if (matchStream == null) {
070: throw new InitializationException(
071: "Cannot find script at /WEB-INF/classes/com/mycompany/toolbox/api/match.groovy");
072: }
073: _matchScript = IOReader.readFully(matchStream);
074: } catch (IOException ioe) {
075: throw new InitializationException(ioe);
076: }
077: }
078:
079: /**
080: * Calls this function. If the function fails, it may throw any kind of
081: * exception. All exceptions will be handled by the caller.
082: *
083: * @param request
084: * the request, never <code>null</code>.
085: *
086: * @return
087: * the result of the function call, should never be <code>null</code>.
088: *
089: * @throws Throwable
090: * if anything went wrong.
091: */
092: public Result call(Request request) throws Throwable {
093: SuccessfulResult result = new SuccessfulResult();
094:
095: // Put the request values in the script
096: Bindings bindings = new SimpleBindings(BeanUtils
097: .getParameters(request));
098:
099: // You may also want to catch the possible exceptions thrown
100: Map<String, Object> values = (Map<String, Object>) _groovyEngine
101: .eval(_matchScript, bindings);
102:
103: BeanUtils.setParameters(values, result);
104: return result;
105: }
106: }
|