1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.tradefed.result; 17 18 import com.android.tradefed.build.IBuildInfo; 19 import com.android.tradefed.config.OptionClass; 20 import com.android.tradefed.device.DeviceNotAvailableException; 21 import com.android.tradefed.log.LogUtil.CLog; 22 23 import com.google.common.annotations.VisibleForTesting; 24 25 import java.net.InetAddress; 26 import java.net.UnknownHostException; 27 28 29 30 /** 31 * An {@link EmailResultReporter} that will send email when invocation fails due to a device not 32 * available exception. 33 */ 34 @OptionClass(alias = "device-unavail-email") 35 public class DeviceUnavailEmailResultReporter extends EmailResultReporter { 36 37 @Override shouldSendMessage()38 protected boolean shouldSendMessage() { 39 return getInvocationException() instanceof DeviceNotAvailableException; 40 } 41 42 @Override generateEmailSubject()43 protected String generateEmailSubject() { 44 StringBuilder subj = new StringBuilder(); 45 subj.append("Device unavailable "); 46 for (IBuildInfo build : getInvocationContext().getBuildInfos()) { 47 subj.append(build.toString()); 48 } 49 subj.append(" "); 50 subj.append(String.format("hostname %s", getHostname())); 51 52 // Sample email subject: Device unavailable: mantaray-user JDQ39 53 // 015d172c980c2208 atl-034.mtv.corp.google.com 54 return subj.toString(); 55 } 56 57 /** 58 * Fetch the hostname and returns it, or returns "Unknown" if an error occurs. 59 */ 60 @VisibleForTesting getHostname()61 String getHostname() { 62 try { 63 return InetAddress.getLocalHost().getHostName(); 64 } catch (UnknownHostException e) { 65 CLog.e(e); 66 return "Unknown"; 67 } 68 } 69 } 70