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
021/**
022 * Abstract superclass for all user-defined policies.
023 * @author Laurent Cohen
024 */
025public abstract class CustomPolicy extends ExecutionPolicy {
026  /**
027   * Explicit serialVersionUID.
028   */
029  private static final long serialVersionUID = 1L;
030  /**
031   * The list of user-defined arguments.
032   */
033  private String[] args = null;
034
035  /**
036   * Initialize this policy with the specified arguments.
037   * @param args the user-defined arguments for this policy.
038   */
039  public CustomPolicy(final String... args) {
040    this.args = args;
041  }
042
043  /**
044   * Perform optional initializations. This default implementation does nothing.<br>
045   * This method is called after instantiating the policy object and setting the arguments.
046   * It allows for user-defined initializations when the custom policy is built from an XML document.
047   */
048  public void initialize() {
049  }
050
051  /**
052   * Get the arguments of this policy.
053   * @return the arguments as an array of strings.
054   */
055  public final String[] getArgs() {
056    return args;
057  }
058
059  /**
060   * Set the arguments of this policy.
061   * @param args the arguments as an array of strings.
062   */
063  public final void setArgs(final String... args) {
064    this.args = args;
065  }
066
067  @Override
068  public String toString(final int n) {
069    final StringBuilder sb = new StringBuilder(indent(n)).append("<CustomRule class=\"").append(this.getClass().getName()).append("\">\n");
070    if (args != null) {
071      for (String s : args) sb.append(indent(n + 1)).append("<Arg>").append(s).append("</Arg>\n");
072    }
073    sb.append(indent(n)).append("</CustomRule>\n");
074    return sb.toString();
075  }
076}