001 /* 002 * SonarQube, open source software quality management tool. 003 * Copyright (C) 2008-2014 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * SonarQube is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * SonarQube is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public License 017 * along with this program; if not, write to the Free Software Foundation, 018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 019 */ 020 package org.sonar.process; 021 022 import org.apache.commons.io.FileUtils; 023 import org.apache.commons.lang.StringUtils; 024 025 import java.io.File; 026 import java.io.IOException; 027 import java.util.HashMap; 028 import java.util.Map; 029 030 public class MinimumViableSystem { 031 032 private final Map<String, String> requiredJavaOptions = new HashMap<String, String>(); 033 034 public MinimumViableSystem setRequiredJavaOption(String propertyKey, String expectedValue) { 035 requiredJavaOptions.put(propertyKey, expectedValue); 036 return this; 037 } 038 039 /** 040 * Entry point for all checks 041 */ 042 public void check() { 043 checkJavaVersion(); 044 checkJavaOptions(); 045 checkWritableTempDir(); 046 } 047 048 /** 049 * Verify that temp directory is writable 050 */ 051 private void checkWritableTempDir() { 052 checkWritableDir(System.getProperty("java.io.tmpdir")); 053 } 054 055 void checkWritableDir(String tempPath) { 056 try { 057 File tempFile = File.createTempFile("check", "tmp", new File(tempPath)); 058 FileUtils.deleteQuietly(tempFile); 059 } catch (IOException e) { 060 throw new IllegalStateException(String.format("Temp directory is not writable: %s", tempPath), e); 061 } 062 } 063 064 void checkJavaOptions() { 065 for (Map.Entry<String, String> entry : requiredJavaOptions.entrySet()) { 066 String value = System.getProperty(entry.getKey()); 067 if (!StringUtils.equals(value, entry.getValue())) { 068 throw new MessageException(String.format( 069 "JVM option '%s' must be set to '%s'. Got '%s'", entry.getKey(), entry.getValue(), StringUtils.defaultString(value))); 070 } 071 } 072 } 073 074 void checkJavaVersion() { 075 String javaVersion = System.getProperty("java.specification.version"); 076 checkJavaVersion(javaVersion); 077 } 078 079 void checkJavaVersion(String javaVersion) { 080 if (!javaVersion.startsWith("1.6") && !javaVersion.startsWith("1.7") && !javaVersion.startsWith("1.8")) { 081 // still better than "java.lang.UnsupportedClassVersionError: Unsupported major.minor version 49.0 082 throw new MessageException(String.format("Supported versions of Java are 1.6, 1.7 and 1.8. Got %s.", javaVersion)); 083 } 084 } 085 086 }