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.common.launch; 020 021import org.apache.commons.lang.StringUtils; 022import org.apache.reef.runtime.common.Launcher; 023import org.apache.reef.runtime.common.launch.parameters.ClockConfigurationPath; 024import org.apache.reef.runtime.common.launch.parameters.ErrorHandlerRID; 025import org.apache.reef.runtime.common.launch.parameters.LaunchID; 026import org.apache.reef.util.EnvironmentUtils; 027 028import java.io.File; 029import java.util.ArrayList; 030import java.util.Collection; 031import java.util.List; 032 033public final class JavaLaunchCommandBuilder implements LaunchCommandBuilder { 034 private static final String DEFAULT_JAVA_PATH = System.getenv("JAVA_HOME") + "/bin/" + "java"; 035 private String stderrPath = null; 036 private String stdoutPath = null; 037 private String errorHandlerRID = null; 038 private String launchID = null; 039 private int megaBytes = 0; 040 private String evaluatorConfigurationPath = null; 041 private String javaPath = null; 042 private String classPath = null; 043 private Boolean assertionsEnabled = null; 044 045 @Override 046 public List<String> build() { 047 return new ArrayList<String>() {{ 048 049 if (javaPath == null || javaPath.isEmpty()) { 050 add(DEFAULT_JAVA_PATH); 051 } else { 052 add(javaPath); 053 } 054 055 add("-XX:PermSize=128m"); 056 add("-XX:MaxPermSize=128m"); 057 // Set Xmx based on am memory size 058 add("-Xmx" + megaBytes + "m"); 059 060 if ((assertionsEnabled != null && assertionsEnabled) 061 || EnvironmentUtils.areAssertionsEnabled()) { 062 add("-ea"); 063 } 064 065 if (classPath != null && !classPath.isEmpty()) { 066 add("-classpath"); 067 add(classPath); 068 } 069 070 Launcher.propagateProperties(this, true, "proc_reef"); 071 Launcher.propagateProperties(this, false, 072 "java.util.logging.config.file", "java.util.logging.config.class"); 073 074 add(Launcher.class.getName()); 075 076 add("-" + ErrorHandlerRID.SHORT_NAME); 077 add(errorHandlerRID); 078 add("-" + LaunchID.SHORT_NAME); 079 add(launchID); 080 add("-" + ClockConfigurationPath.SHORT_NAME); 081 add(evaluatorConfigurationPath); 082 083 if (stdoutPath != null && !stdoutPath.isEmpty()) { 084 add("1>"); 085 add(stdoutPath); 086 } 087 088 if (stderrPath != null && !stderrPath.isEmpty()) { 089 add("2>"); 090 add(stderrPath); 091 } 092 }}; 093 } 094 095 @Override 096 public JavaLaunchCommandBuilder setErrorHandlerRID(final String errorHandlerRID) { 097 this.errorHandlerRID = errorHandlerRID; 098 return this; 099 } 100 101 @Override 102 public JavaLaunchCommandBuilder setLaunchID(final String launchID) { 103 this.launchID = launchID; 104 return this; 105 } 106 107 @Override 108 public JavaLaunchCommandBuilder setMemory(final int megaBytes) { 109 this.megaBytes = megaBytes; 110 return this; 111 } 112 113 @Override 114 public JavaLaunchCommandBuilder setConfigurationFileName(final String configurationFileName) { 115 this.evaluatorConfigurationPath = configurationFileName; 116 return this; 117 } 118 119 @Override 120 public JavaLaunchCommandBuilder setStandardOut(final String standardOut) { 121 this.stdoutPath = standardOut; 122 return this; 123 } 124 125 @Override 126 public JavaLaunchCommandBuilder setStandardErr(final String standardErr) { 127 this.stderrPath = standardErr; 128 return this; 129 } 130 131 /** 132 * Set the path to the java executable. Will default to a heuristic search if not set. 133 * 134 * @param path 135 * @return this 136 */ 137 public JavaLaunchCommandBuilder setJavaPath(final String path) { 138 this.javaPath = path; 139 return this; 140 } 141 142 public JavaLaunchCommandBuilder setClassPath(final String classPath) { 143 this.classPath = classPath; 144 return this; 145 } 146 147 public JavaLaunchCommandBuilder setClassPath(final Collection<String> classPathElements) { 148 this.classPath = StringUtils.join(classPathElements, File.pathSeparatorChar); 149 return this; 150 } 151 152 /** 153 * Enable or disable assertions on the child process. 154 * If not set, the setting is taken from the JVM that executes the code. 155 * 156 * @param assertionsEnabled 157 * @return this 158 */ 159 public JavaLaunchCommandBuilder enableAssertions(final boolean assertionsEnabled) { 160 this.assertionsEnabled = assertionsEnabled; 161 return this; 162 } 163}