This project has retired. For details please refer to its Attic page.
Source code
001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  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,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.runtime.multi.client;
020
021import org.apache.commons.lang.Validate;
022import org.apache.commons.lang.StringUtils;
023import org.apache.reef.annotations.Unstable;
024import org.apache.reef.runtime.multi.utils.avro.AvroMultiRuntimeDefinition;
025import org.apache.reef.runtime.multi.utils.avro.AvroRuntimeDefinition;
026import org.apache.reef.tang.Configuration;
027import org.apache.reef.tang.formats.AvroConfigurationSerializer;
028
029import java.util.ArrayList;
030import java.util.HashMap;
031import java.util.Map;
032
033/**
034 * Builder for multi runtime definition.
035 */
036@Unstable
037public final class MultiRuntimeDefinitionBuilder {
038  private Map<String, AvroRuntimeDefinition> runtimes = new HashMap<>();
039  private String defaultRuntime;
040
041  private static AvroRuntimeDefinition createRuntimeDefinition(final Configuration configModule,
042                                                             final String runtimeName) {
043    final Configuration localDriverConfiguration = configModule;
044    final AvroConfigurationSerializer serializer = new AvroConfigurationSerializer();
045    final String serializedConfig = serializer.toString(localDriverConfiguration);
046    return new AvroRuntimeDefinition(runtimeName, serializedConfig);
047  }
048
049  /**
050   * Adds runtime configuration module to the builder.
051   * @param config The configuration module
052   * @param runtimeName The name of the runtime
053   * @return The builder instance
054   */
055  public MultiRuntimeDefinitionBuilder addRuntime(final Configuration config, final String runtimeName){
056    Validate.notNull(config, "runtime configuration module should not be null");
057    Validate.isTrue(StringUtils.isNotBlank(runtimeName),
058            "runtimeName should be non empty and non blank string");
059    final AvroRuntimeDefinition rd = createRuntimeDefinition(config, runtimeName);
060    this.runtimes.put(runtimeName, rd);
061    return this;
062  }
063
064  /**
065   * Sets default runtime name.
066   * @param runtimeName The name of the default runtime
067   * @return The builder instance
068   */
069  public MultiRuntimeDefinitionBuilder setDefaultRuntimeName(final String runtimeName){
070    Validate.isTrue(StringUtils.isNotBlank(runtimeName),
071            "runtimeName should be non empty and non blank string");
072    this.defaultRuntime = runtimeName;
073    return this;
074  }
075
076  /**
077   * Builds multi runtime definition.
078   * @return The populated definition object
079   */
080  public AvroMultiRuntimeDefinition build(){
081    Validate.isTrue(this.runtimes.size() == 1 || !StringUtils.isEmpty(this.defaultRuntime), "Default runtime " +
082            "should be set if more than a single runtime provided");
083
084    if(StringUtils.isEmpty(this.defaultRuntime)){
085      // we have single runtime configured, take its name as a default
086      this.defaultRuntime = this.runtimes.keySet().iterator().next();
087    }
088
089    Validate.isTrue(this.runtimes.containsKey(this.defaultRuntime), "Default runtime should be configured");
090    return new AvroMultiRuntimeDefinition(defaultRuntime, new ArrayList<>(this.runtimes.values()));
091  }
092}