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.javabridge;
020
021import org.apache.commons.lang3.StringUtils;
022import org.apache.reef.annotations.audience.Interop;
023import org.apache.reef.annotations.audience.Private;
024import org.apache.reef.driver.context.ActiveContext;
025import org.apache.reef.io.naming.Identifiable;
026import org.apache.reef.runtime.common.driver.context.EvaluatorContext;
027
028import java.util.logging.Level;
029import java.util.logging.Logger;
030
031/**
032 * The Java-CLR bridge object for {@link org.apache.reef.driver.context.ActiveContext}.
033 */
034@Private
035@Interop(
036    CppFiles = { "Clr2JavaImpl.h", "ActiveContextClr2Java.cpp" },
037    CsFiles = { "IActiveContextClr2Java.cs", "ActiveContext.cs" })
038public final class ActiveContextBridge extends NativeBridge implements Identifiable {
039  private static final Logger LOG = Logger.getLogger(ActiveContextBridge.class.getName());
040
041  private final ActiveContext jactiveContext;
042  private final String contextId;
043  private final String evaluatorId;
044
045  ActiveContextBridge(final ActiveContext activeContext) {
046    this.jactiveContext = activeContext;
047    this.contextId = activeContext.getId();
048    this.evaluatorId = activeContext.getEvaluatorId();
049  }
050
051  /**
052   * @return the context ID.
053   */
054  public String getContextId() {
055    return contextId;
056  }
057
058  /**
059   * @return the context ID of the parent.
060   */
061  public String getParentContextId() {
062    return jactiveContext.getParentId().get();
063  }
064
065  /**
066   * @return the Evaluator ID of the Evaluator on which the Context runs.
067   */
068  public String getEvaluatorId() {
069    return evaluatorId;
070  }
071
072  public void submitTaskString(final String taskConfigurationString) {
073    if (StringUtils.isEmpty(taskConfigurationString)) {
074      throw new RuntimeException("empty taskConfigurationString provided.");
075    }
076
077    //when submit over the bridge, we would keep the task configuration as a serialized string
078    //submitTask(String taskConfig) is not exposed in the interface. Therefore cast is necessary.
079    ((EvaluatorContext)jactiveContext).submitTask(taskConfigurationString);
080  }
081
082  public void submitContextStringAndServiceString(final String contextConfigurationString,
083                                                  final String serviceConfigurationString) {
084    if (StringUtils.isEmpty(contextConfigurationString)) {
085      throw new RuntimeException("empty contextConfigurationString provided.");
086    }
087
088    ((EvaluatorContext)jactiveContext).submitContextAndService(contextConfigurationString, serviceConfigurationString);
089  }
090
091  public String getEvaluatorDescriptorString() {
092    final String descriptorString = Utilities.getEvaluatorDescriptorString(jactiveContext.getEvaluatorDescriptor());
093    LOG.log(Level.FINE, "active context - serialized evaluator descriptor: " + descriptorString);
094    return descriptorString;
095  }
096
097  public void sendMessage(final byte[] message) {
098    jactiveContext.sendMessage(message);
099  }
100
101  @Override
102  public void close() {
103    jactiveContext.close();
104  }
105
106  @Override
107  public String getId() {
108    return jactiveContext.getId();
109  }
110}