001/*
002 * JPPF.
003 * Copyright (C) 2005-2019 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.job;
020
021import java.io.*;
022import java.util.*;
023
024import org.jppf.node.protocol.JPPFDistributedJob;
025import org.jppf.scripting.BaseScriptEvaluator;
026import org.slf4j.*;
027
028/**
029 * A job selector that uses a script to accept jobs. The script is evaluated using the {@code javax.script.*} API (JSR-223).
030 * <p>When the script is executed, it provides access to a variable named "jppfJob" which is an instance of {@link JPPFDistributedJob}.
031 * @author Laurent Cohen
032 * @since 5.1
033 */
034public class ScriptedJobSelector extends BaseScriptEvaluator implements JobSelector {
035  /**
036   * Explicit serialVersionUID.
037   */
038  private static final long serialVersionUID = 1L;
039  /**
040   * Logger for this class.
041   */
042  private static Logger log = LoggerFactory.getLogger(ScriptedJobSelector.class);
043
044  /**
045   * Initialize this selector with the specfied language and script.
046   * @param language the language of the script to evaluate.
047   * @param script the script to evaluate.
048   */
049  public ScriptedJobSelector(final String language, final String script) {
050    super(language, script);
051  }
052
053  /**
054   * Initialize this selector with the specfied language and script reader.
055   * @param language the language of the script to evaluate.
056   * @param scriptReader a reader from which to read the script to evaluate.
057   * @throws IOException if the script could not be read.
058   */
059  public ScriptedJobSelector(final String language, final Reader scriptReader) throws IOException {
060    super(language, scriptReader);
061  }
062
063  /**
064   * Initialize this selector with the specfied language and script file.
065   * @param language the language of the script to evaluate.
066   * @param scriptFile a file from which to read the script to evaluate.
067   * @throws IOException if the script could not be read.
068   */
069  public ScriptedJobSelector(final String language, final File scriptFile) throws IOException {
070    super(language, scriptFile);
071  }
072
073  @Override
074  public boolean accepts(final JPPFDistributedJob job) {
075    final Map<String, Object> variables = new HashMap<>();
076    variables.put("jppfJob", job);
077    final Object result = evaluate(variables);
078    if (result instanceof Boolean) return (Boolean) result;
079    log.error("result of scripted job selector should be a boolean but instead is " + result);
080    return false;
081  }
082}