001/* 002 * SonarQube 003 * Copyright (C) 2009-2018 SonarSource SA 004 * mailto:info AT sonarsource DOT com 005 * 006 * This program 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 * This program 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 */ 020package org.sonar.api.web; 021 022import java.io.File; 023import java.io.FileNotFoundException; 024import java.io.IOException; 025import java.io.InputStream; 026import org.apache.commons.io.FileUtils; 027import org.apache.commons.io.IOUtils; 028import org.sonar.api.utils.SonarException; 029 030import static java.nio.charset.StandardCharsets.UTF_8; 031 032/** 033 * @since 1.11 034 * @deprecated since 6.3. This class is ignored. 035 */ 036@Deprecated 037public abstract class AbstractRubyTemplate { 038 039 private String cache = null; 040 041 public String getTemplate() { 042 String result = loadTemplateFromCache(); 043 try { 044 if (result == null) { 045 result = loadTemplateFromClasspath(); 046 } 047 if (result == null) { 048 result = loadTemplateFromAbsolutePath(); 049 } 050 return result; 051 052 } catch (IOException e) { 053 throw new SonarException("Can not read the file " + getTemplatePath(), e); 054 } 055 } 056 057 private String loadTemplateFromAbsolutePath() throws IOException { 058 File file = new File(getTemplatePath()); 059 if (file.exists()) { 060 // the result is not cached 061 return FileUtils.readFileToString(file, UTF_8); 062 } 063 throw new FileNotFoundException(getTemplatePath()); 064 } 065 066 private String loadTemplateFromClasspath() throws IOException { 067 InputStream input = getClass().getResourceAsStream(getTemplatePath()); 068 try { 069 if (input != null) { 070 cache = IOUtils.toString(input); 071 return cache; 072 } 073 } finally { 074 IOUtils.closeQuietly(input); 075 } 076 return null; 077 } 078 079 protected String loadTemplateFromCache() { 080 return cache; 081 } 082 083 /** 084 * the path of the template. In production environment, it's the classloader path (for example "/org/sonar/my_template.erb"). 085 * In dev mode, it's useful to return an absolute path (for example C:/temp/my_template.erb). In such a case the result is not cached. 086 */ 087 protected abstract String getTemplatePath(); 088 089}