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.reef.annotations.audience.Interop; 022import org.apache.reef.annotations.audience.Private; 023import org.apache.reef.driver.evaluator.EvaluatorDescriptor; 024import org.apache.reef.tang.ClassHierarchy; 025import org.apache.reef.tang.implementation.protobuf.ProtocolBufferClassHierarchy; 026import org.apache.reef.tang.proto.ClassHierarchyProto; 027 028import java.io.FileInputStream; 029import java.io.IOException; 030import java.io.InputStream; 031import java.net.InetSocketAddress; 032import java.nio.file.Files; 033import java.nio.file.Path; 034import java.nio.file.Paths; 035 036/** 037 * CLR/Java bridge utilities. 038 */ 039@Private 040@Interop 041public final class Utilities { 042 public static ClassHierarchy loadClassHierarchy(final String classHierarchyFile) { 043 // TODO[JIRA REEF-400] The file should be created via AvroClassHierarchySerializer 044 Path p = Paths.get(classHierarchyFile); 045 if (!Files.exists(p)) { 046 p = Paths.get(System.getProperty("user.dir") + "/reef/global/" + classHierarchyFile); 047 } 048 if (!Files.exists(p)) { 049 throw new RuntimeException("cannot find file " + p.toAbsolutePath()); 050 } 051 // TODO[JIRA REEF-400] Use the AvroClassHierarchySerializer in place of protobuf 052 try (final InputStream chin = new FileInputStream(p.toAbsolutePath().toString())) { 053 final ClassHierarchyProto.Node root = ClassHierarchyProto.Node.parseFrom(chin); 054 final ClassHierarchy ch = new ProtocolBufferClassHierarchy(root); 055 return ch; 056 } catch (final IOException e) { 057 final String message = "Unable to load class hierarchy from " + classHierarchyFile; 058 throw new RuntimeException(message, e); 059 } 060 } 061 062 public static String getEvaluatorDescriptorString(final EvaluatorDescriptor evaluatorDescriptor) { 063 final InetSocketAddress socketAddress = evaluatorDescriptor.getNodeDescriptor().getInetSocketAddress(); 064 return "IP=" + socketAddress.getAddress() + ", Port=" + socketAddress.getPort() + ", HostName=" + 065 socketAddress.getHostName() + ", Memory=" + evaluatorDescriptor.getMemory() + ", Core=" + 066 evaluatorDescriptor.getNumberOfCores() + ", RuntimeName=" + evaluatorDescriptor.getRuntimeName(); 067 } 068 069 /** 070 * Empty private constructor to prohibit instantiation of utility class. 071 */ 072 private Utilities() { 073 } 074}