001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 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.node.policy;
020
021import java.io.*;
022import java.net.URL;
023
024import org.jppf.JPPFException;
025import org.jppf.utils.*;
026import org.slf4j.*;
027
028/**
029 * Utility methods to resolve, parse and manipulate execution policies.
030 * @author Laurent Cohen
031 * @ecxclude
032 */
033public class PolicyUtils {
034  /**
035   * Logger for this class.
036   */
037  private static Logger log = LoggerFactory.getLogger(PolicyUtils.class);
038
039  /**
040   * Create the execution policy form the defintiion.
041   * @param props the datasource properties.
042   * @param propertyName the identifier of the datasource in the configuration.
043   * @return an {@link ExecutionPolicy} instance, or {@code null} if none could be created.
044   */
045  public static  String resolvePolicy(final TypedProperties props, final String propertyName) {
046    String policy = null;
047    try {
048      final String s = props.getString(propertyName);
049      if (s == null) return null;
050      final String[] tokens = s.split("\\|");
051      for (int i=0; i<tokens.length; i++) tokens[i] = tokens[i].trim();
052      final String type;
053      final String source;
054      if (tokens.length >= 2) {
055        type = tokens[0].toLowerCase();
056        source = tokens[1];
057      } else {
058        type = "inline";
059        source = tokens[0];
060      }
061      try (final Reader reader = getPolicyReader(type, source)) {
062        policy = FileUtils.readTextFile(reader);
063      }
064    } catch (final Exception e) {
065      log.error("error resolving the execution policy for property name = {}\n{}", propertyName, ExceptionUtils.getStackTrace(e));
066    }
067    return policy;
068  }
069
070  /**
071   * Get a reader for the execution policy based on the type and source.
072   * @param type the type of source: one of "inline", "file", "url".
073   * @param source the source for the policy, its smeaning depends on the type.
074   * @return an execution policy parsed from the source.
075   * @throws Exception if any error occurs.
076   */
077  private static Reader getPolicyReader(final String type, final String source) throws Exception {
078    Reader reader = null;
079    switch(type) {
080      case "inline":
081        reader = new StringReader(source);
082        break;
083
084      case "file":
085        reader = FileUtils.getFileReader(source);
086        break;
087
088      case "url":
089        final URL url = new URL(source);
090        reader = new InputStreamReader(url.openConnection().getInputStream(), "utf-8");
091        break;
092
093      default:
094        throw new JPPFException("unknown soure type '" + type + "' for execution policy '" + source + "'");
095    }
096    return reader;
097  }
098}