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 submitContextString(final String contextConfigurationString) { 083 if (StringUtils.isEmpty(contextConfigurationString)) { 084 throw new RuntimeException("empty contextConfigurationString provided."); 085 } 086 087 ((EvaluatorContext)jactiveContext).submitContext(contextConfigurationString); 088 } 089 090 public String getEvaluatorDescriptorString() { 091 final String descriptorString = Utilities.getEvaluatorDescriptorString(jactiveContext.getEvaluatorDescriptor()); 092 LOG.log(Level.FINE, "active context - serialized evaluator descriptor: " + descriptorString); 093 return descriptorString; 094 } 095 096 @Override 097 public void close() { 098 jactiveContext.close(); 099 } 100 101 @Override 102 public String getId() { 103 return jactiveContext.getId(); 104 } 105}