2011-05-22 21:19:28 +02:00
|
|
|
/*
|
2011-10-28 08:17:54 +02:00
|
|
|
* This file is part of LibrePlan
|
2011-05-22 21:19:28 +02:00
|
|
|
*
|
|
|
|
|
* Copyright (C) 2011 Igalia, S.L.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2011-10-28 08:17:54 +02:00
|
|
|
package org.libreplan.web;
|
2011-05-22 21:19:28 +02:00
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.io.StringReader;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletContext;
|
|
|
|
|
import javax.servlet.ServletContextEvent;
|
|
|
|
|
import javax.servlet.ServletContextListener;
|
|
|
|
|
|
|
|
|
|
import org.apache.log4j.LogManager;
|
|
|
|
|
import org.apache.log4j.xml.DOMConfigurator;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* It tries to replace ${log-directory} property for a suitable location.
|
|
|
|
|
*
|
|
|
|
|
* @author Oscar Gonzalez Fernandez <ogonzalez@igalia.com>
|
|
|
|
|
*/
|
|
|
|
|
public class LoggingConfiguration implements ServletContextListener {
|
|
|
|
|
|
2016-11-15 11:13:21 +02:00
|
|
|
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
|
|
|
|
|
|
|
|
|
|
private static Pattern propertyPattern = Pattern.compile("\\$\\{\\s*(.+?)\\s*\\}");
|
2011-05-22 21:19:28 +02:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void contextInitialized(ServletContextEvent sce) {
|
2016-05-05 10:32:05 +03:00
|
|
|
if ( System.getProperty("libreplan-log-directory") != null ) {
|
2016-11-15 11:13:21 +02:00
|
|
|
// log4j will do the replacement automatically
|
2011-05-22 21:19:28 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-05 10:32:05 +03:00
|
|
|
Map<String, String> replacements = new HashMap<>();
|
|
|
|
|
replacements.put("libreplan-log-directory", findLogDirectory(sce.getServletContext()));
|
2011-05-22 21:19:28 +02:00
|
|
|
try {
|
2016-05-05 10:32:05 +03:00
|
|
|
StringReader newConfiguration = new StringReader(getContents(replacements));
|
|
|
|
|
new DOMConfigurator().doConfigure(newConfiguration, LogManager.getLoggerRepository());
|
2011-05-22 21:19:28 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
2016-11-15 11:13:21 +02:00
|
|
|
// Let log4j be loaded without replacements
|
2011-05-22 21:19:28 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String findLogDirectory(ServletContext servletContext) {
|
|
|
|
|
File result = logDirectoryFile(servletContext);
|
2016-11-15 11:13:21 +02:00
|
|
|
return result != null ? result.getAbsolutePath() + "/" : "";
|
2011-05-22 21:19:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private File logDirectoryFile(ServletContext servletContext) {
|
|
|
|
|
String applicationName = firstNotEmptyOrNull(
|
|
|
|
|
servletContext.getContextPath(),
|
2016-05-05 10:32:05 +03:00
|
|
|
servletContext.getServletContextName(),
|
|
|
|
|
"LibrePlan");
|
|
|
|
|
|
|
|
|
|
if ( isTomcat(servletContext) ) {
|
2011-05-22 21:19:28 +02:00
|
|
|
File logDirectory = findTomcatLogDirectory();
|
2016-05-05 10:32:05 +03:00
|
|
|
if ( logDirectory != null ) {
|
2011-05-22 21:19:28 +02:00
|
|
|
return tryToAppendApplicationName(logDirectory, applicationName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File home = new File(System.getProperty("user.home"));
|
|
|
|
|
|
2016-11-15 11:13:21 +02:00
|
|
|
return home.canWrite() ? tryToAppendApplicationName(home, applicationName) : null;
|
2011-05-22 21:19:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private File findTomcatLogDirectory() {
|
|
|
|
|
File file = new File("/var/log/");
|
2016-05-05 10:32:05 +03:00
|
|
|
if ( !file.isDirectory() ) {
|
2011-05-22 21:19:28 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-15 11:13:21 +02:00
|
|
|
File[] tomcatLogDirectories = file.listFiles(pathname -> pathname.getName().contains("tomcat"));
|
2016-05-05 10:32:05 +03:00
|
|
|
|
2016-11-15 11:13:21 +02:00
|
|
|
return tomcatLogDirectories.length == 0 ? null : tomcatLogDirectories[0];
|
2011-05-22 21:19:28 +02:00
|
|
|
}
|
|
|
|
|
|
2016-05-05 10:32:05 +03:00
|
|
|
private File tryToAppendApplicationName(File logDirectory, String applicationName) {
|
2011-05-22 21:19:28 +02:00
|
|
|
File forApplication = new File(logDirectory, applicationName);
|
2016-11-15 11:13:21 +02:00
|
|
|
return forApplication.mkdir() || forApplication.canWrite() ? forApplication : logDirectory;
|
2011-05-22 21:19:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean isTomcat(ServletContext servletContext) {
|
|
|
|
|
String serverInfo = servletContext.getServerInfo();
|
|
|
|
|
return serverInfo != null && serverInfo.contains("Tomcat");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String firstNotEmptyOrNull(String... strings) {
|
|
|
|
|
for (String each : strings) {
|
2016-05-05 10:32:05 +03:00
|
|
|
if ( each != null && !each.isEmpty() ) {
|
2011-05-22 21:19:28 +02:00
|
|
|
return each;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-05 10:32:05 +03:00
|
|
|
private String getContents(Map<String, String> replacements) throws IOException {
|
2011-05-22 21:19:28 +02:00
|
|
|
return withReplacements(replacements, getOriginalConfiguration());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private BufferedReader getOriginalConfiguration() {
|
2016-05-05 10:32:05 +03:00
|
|
|
return new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream("log4j.xml")));
|
2011-05-22 21:19:28 +02:00
|
|
|
}
|
|
|
|
|
|
2016-11-15 11:13:21 +02:00
|
|
|
private String withReplacements(Map<String, String> replacements, BufferedReader originalConfiguration) throws IOException {
|
2016-05-05 10:32:05 +03:00
|
|
|
|
2011-05-22 21:19:28 +02:00
|
|
|
StringBuilder result = new StringBuilder();
|
2016-05-05 10:32:05 +03:00
|
|
|
String line;
|
|
|
|
|
|
2011-05-22 21:19:28 +02:00
|
|
|
while ((line = originalConfiguration.readLine()) != null) {
|
2016-11-15 11:13:21 +02:00
|
|
|
result.append(doReplacement(replacements, line)).append(LINE_SEPARATOR);
|
2011-05-22 21:19:28 +02:00
|
|
|
}
|
2016-05-05 10:32:05 +03:00
|
|
|
|
2011-05-22 21:19:28 +02:00
|
|
|
return result.toString();
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-05 10:32:05 +03:00
|
|
|
private static String doReplacement(Map<String, String> propertyReplacements, String line) {
|
2011-05-22 21:19:28 +02:00
|
|
|
|
|
|
|
|
String result = line;
|
|
|
|
|
Matcher matcher = propertyPattern.matcher(line);
|
2016-05-05 10:32:05 +03:00
|
|
|
|
2011-05-22 21:19:28 +02:00
|
|
|
while (matcher.find()) {
|
|
|
|
|
String propertyName = matcher.group(1);
|
2016-05-05 10:32:05 +03:00
|
|
|
if ( propertyReplacements.containsKey(propertyName) ) {
|
|
|
|
|
result = line.replace(matcher.group(), propertyReplacements.get(propertyName));
|
2011-05-22 21:19:28 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-05-05 10:32:05 +03:00
|
|
|
|
2011-05-22 21:19:28 +02:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void contextDestroyed(ServletContextEvent sce) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|