Skip to content

Commit

Permalink
Support for initial logging setup in midpoint.home
Browse files Browse the repository at this point in the history
  • Loading branch information
semancik committed Oct 28, 2014
1 parent 0d06f72 commit 6708c81
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 48 deletions.
Expand Up @@ -51,6 +51,8 @@ public class LoggingConfigurationManager {
private static final String REQUEST_FILTER_LOGGER_CLASS_NAME = "com.evolveum.midpoint.web.util.MidPointProfilingServletFilter";
private static final String PROFILING_ASPECT_LOGGER = "com.evolveum.midpoint.util.aspect.ProfilingDataManager";
private static final String IDM_PROFILE_APPENDER = "IDM_LOG";

public static final String SYSTEM_CONFIGURATION_SKIP_REPOSITORY_LOGGING_SETTINGS = "skipRepositoryLoggingSettings";

private static String currentlyUsedVersion = null;

Expand Down Expand Up @@ -362,4 +364,8 @@ public static void resetCurrentlyUsedVersion() {
currentlyUsedVersion = null;
}

public static void setCurrentlyUsedVersion(String version) {
currentlyUsedVersion = version;
}

}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2013 Evolveum
* Copyright (c) 2010-2014 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,8 @@

public interface MidpointConfiguration {

public static final String SYSTEM_CONFIGURATION_SECTION = "midpoint.system";

public String getMidpointHome();

/**
Expand Down
5 changes: 5 additions & 0 deletions model/model-impl/pom.xml
Expand Up @@ -61,6 +61,11 @@
<artifactId>model-common</artifactId>
<version>3.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.evolveum.midpoint.repo</groupId>
<artifactId>system-init</artifactId>
<version>3.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.evolveum.midpoint.repo</groupId>
<artifactId>repo-api</artifactId>
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2013 Evolveum
* Copyright (c) 2010-2014 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,11 +27,13 @@
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;

import org.apache.commons.configuration.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.evolveum.midpoint.common.LoggingConfigurationManager;
import com.evolveum.midpoint.common.configuration.api.MidpointConfiguration;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
Expand Down Expand Up @@ -60,16 +62,27 @@ public class SystemConfigurationHandler implements ChangeHook {
@Autowired(required = true)
@Qualifier("cacheRepositoryService")
private transient RepositoryService cacheRepositoryService;
@Autowired(required = true)
private MidpointConfiguration startupConfiguration;

@PostConstruct
public void init() {
hookRegistry.registerChangeHook(HOOK_URI, this);
}

public void postInit(PrismObject<SystemConfigurationType> systemConfiguration, OperationResult parentResult) {
LoggingConfigurationType loggingConfig = ProfilingConfigurationManager.checkSystemProfilingConfiguration(systemConfiguration);
applyLoggingConfiguration(loggingConfig, systemConfiguration.asObjectable().getVersion(), parentResult);
//applyLoggingConfiguration(systemConfiguration.asObjectable().getLogging(), systemConfiguration.asObjectable().getVersion(), parentResult);
Configuration systemConfigFromFile = startupConfiguration.getConfiguration(MidpointConfiguration.SYSTEM_CONFIGURATION_SECTION);
boolean skip = false;
if (systemConfigFromFile != null) {
skip = systemConfigFromFile.getBoolean(LoggingConfigurationManager.SYSTEM_CONFIGURATION_SKIP_REPOSITORY_LOGGING_SETTINGS, false);
}
if (skip) {
LOGGER.warn("Skipping application of repository logging configuration because {}=true", LoggingConfigurationManager.SYSTEM_CONFIGURATION_SKIP_REPOSITORY_LOGGING_SETTINGS);
} else {
LoggingConfigurationType loggingConfig = ProfilingConfigurationManager.checkSystemProfilingConfiguration(systemConfiguration);
applyLoggingConfiguration(loggingConfig, systemConfiguration.asObjectable().getVersion(), parentResult);
//applyLoggingConfiguration(systemConfiguration.asObjectable().getLogging(), systemConfiguration.asObjectable().getVersion(), parentResult);
}
}

private void applyLoggingConfiguration(LoggingConfigurationType loggingConfig, String version, OperationResult parentResult) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2013 Evolveum
* Copyright (c) 2010-2014 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,18 +15,27 @@
*/
package com.evolveum.midpoint.init;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;

import com.evolveum.midpoint.common.configuration.api.MidpointConfiguration;
import com.evolveum.midpoint.util.ClassPathUtil;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.exception.SystemException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;

import org.apache.commons.configuration.*;
import org.apache.commons.lang.NotImplementedException;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilder;

import java.io.File;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Iterator;
Expand All @@ -36,6 +45,9 @@ public class StartupConfiguration implements MidpointConfiguration {
private static final Trace LOGGER = TraceManager.getTrace(StartupConfiguration.class);
private static final String USER_HOME = "user.home";
private static final String MIDPOINT_HOME = "midpoint.home";

private static final String DEFAULT_CONFIG_FILE_NAME = "config.xml";
private static final String LOGBACK_CONFIG_FILENAME = "logback.xml";

private CompositeConfiguration config = null;
private Document xmlConfigAsDocument = null; // just in case when we need to access original XML document
Expand All @@ -45,7 +57,7 @@ public class StartupConfiguration implements MidpointConfiguration {
* Default constructor
*/
public StartupConfiguration() {
this.configFilename = "config.xml";
this.configFilename = DEFAULT_CONFIG_FILE_NAME;
}

/**
Expand Down Expand Up @@ -144,29 +156,24 @@ public void init() {
}
}

loadConfiguration();
}

/**
* Load system configuration
*/
public void load() {
loadConfiguration();
}
String midpointHomeString = System.getProperty(MIDPOINT_HOME);
if (midpointHomeString != null) {
//Fix missing last slash in path
if (!midpointHomeString.endsWith("/")) {
midpointHomeString = midpointHomeString + "/";
System.setProperty(MIDPOINT_HOME, midpointHomeString);
}
}

/**
* Save system configuration
*
* @TODO not implement yet
*/
public void save() {
throw new NotImplementedException();
File midpointHome = new File(midpointHomeString);
setupInitialLogging(midpointHome);
loadConfiguration(midpointHome);
}

/**
* Loading logic
*/
private void loadConfiguration() {
private void loadConfiguration(File midpointHome) {
if (config != null) {
config.clear();
} else {
Expand All @@ -175,42 +182,33 @@ private void loadConfiguration() {

DocumentBuilder documentBuilder = DOMUtil.createDocumentBuilder(); // we need namespace-aware document builder (see GeneralChangeProcessor.java)

if (midpointHome != null) {
/* configuration logic */
// load from midpoint.home
if (null != System.getProperty(MIDPOINT_HOME)) {
try {
//Fix missing last slash in path
if (!System.getProperty(MIDPOINT_HOME).endsWith("/")) {
System.setProperty(MIDPOINT_HOME, System.getProperty(MIDPOINT_HOME) + "/");
}

//Load configuration
String path = System.getProperty(MIDPOINT_HOME) + this.getConfigFilename();
LOGGER.info("Loading midPoint configuration from file {}", path);
File f = new File(path);
File f = new File(midpointHome, this.getConfigFilename());
LOGGER.info("Loading midPoint configuration from file {}", f);
try {
if (!f.exists()) {
LOGGER.warn("Configuration file {} does not exists. Need to do extraction ...", path);
LOGGER.warn("Configuration file {} does not exists. Need to do extraction ...", f);

ApplicationHomeSetup ah = new ApplicationHomeSetup();
ah.init(MIDPOINT_HOME);
ClassPathUtil.extractFileFromClassPath(this.getConfigFilename(), path);
ClassPathUtil.extractFileFromClassPath(this.getConfigFilename(), f.getPath());

}
this.setConfigFilename(path);
//Load and parse properties
config.addProperty(MIDPOINT_HOME, System.getProperty(MIDPOINT_HOME));
createXmlConfiguration(documentBuilder);
createXmlConfiguration(documentBuilder, f.getPath());
} catch (ConfigurationException e) {
String message = "Unable to read configuration file [" + this.getConfigFilename() + "]: " + e.getMessage();
String message = "Unable to read configuration file [" + f + "]: " + e.getMessage();
LOGGER.error(message);
System.out.println(message);
throw new SystemException(message, e); // there's no point in continuing with midpoint initialization
}

} else {
// Load from class path
// Load from current directory
try {
createXmlConfiguration(documentBuilder);
createXmlConfiguration(documentBuilder, this.getConfigFilename());
} catch (ConfigurationException e) {
String message = "Unable to read configuration file [" + this.getConfigFilename() + "]: " + e.getMessage();
LOGGER.error(message);
Expand All @@ -219,15 +217,34 @@ private void loadConfiguration() {
}
}
}

private void setupInitialLogging(File midpointHome) {
File logbackConfigFile = new File(midpointHome, LOGBACK_CONFIG_FILENAME);
if (!logbackConfigFile.exists()) {
return;
}
LOGGER.info("Loading additional logging configuration from {}", logbackConfigFile);
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
try {
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(context);
configurator.doConfigure(logbackConfigFile);
} catch (JoranException je) {
// StatusPrinter will handle this
} catch (Exception ex) {
ex.printStackTrace();
}
StatusPrinter.printInCaseOfErrorsOrWarnings(context);
}

private void createXmlConfiguration(DocumentBuilder documentBuilder) throws ConfigurationException {
private void createXmlConfiguration(DocumentBuilder documentBuilder, String filename) throws ConfigurationException {
XMLConfiguration xmlConfig = new XMLConfiguration();
xmlConfig.setDocumentBuilder(documentBuilder);
xmlConfig.setFileName(this.getConfigFilename());
xmlConfig.setFileName(filename);
xmlConfig.load();
config.addConfiguration(xmlConfig);

xmlConfigAsDocument = DOMUtil.parseFile(this.getConfigFilename());
xmlConfigAsDocument = DOMUtil.parseFile(filename);
}

@Override
Expand Down
Expand Up @@ -17,6 +17,7 @@

import com.evolveum.midpoint.common.LoggingConfigurationManager;
import com.evolveum.midpoint.common.ProfilingConfigurationManager;
import com.evolveum.midpoint.common.configuration.api.MidpointConfiguration;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.repo.api.RepositoryService;
Expand All @@ -37,6 +38,8 @@

import java.util.List;

import org.apache.commons.configuration.Configuration;

/**
* Responsible for keeping the cluster consistent.
* (Clusterwide task management operations are in ExecutionManager.)
Expand Down Expand Up @@ -270,8 +273,20 @@ public void checkSystemConfigurationChanged(OperationResult parentResult) {

// we do not try to determine which one is "newer" - we simply use the one from repo
if (!versionInRepo.equals(versionApplied)) {
LoggingConfigurationType loggingConfig = ProfilingConfigurationManager.checkSystemProfilingConfiguration(config);
LoggingConfigurationManager.configure(loggingConfig, versionInRepo, result);

Configuration systemConfigFromFile = taskManager.getMidpointConfiguration().getConfiguration(MidpointConfiguration.SYSTEM_CONFIGURATION_SECTION);
boolean skip = false;
if (systemConfigFromFile != null && versionApplied == null) {
skip = systemConfigFromFile.getBoolean(LoggingConfigurationManager.SYSTEM_CONFIGURATION_SKIP_REPOSITORY_LOGGING_SETTINGS, false);
}
if (skip) {
LOGGER.warn("Skipping application of repository logging configuration because {}=true (version={})", LoggingConfigurationManager.SYSTEM_CONFIGURATION_SKIP_REPOSITORY_LOGGING_SETTINGS, versionInRepo);
// But pretend that this was applied so the next update works normally
LoggingConfigurationManager.setCurrentlyUsedVersion(versionInRepo);
} else {
LoggingConfigurationType loggingConfig = ProfilingConfigurationManager.checkSystemProfilingConfiguration(config);
LoggingConfigurationManager.configure(loggingConfig, versionInRepo, result);
}
} else {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("System configuration change check: version in repo = version currently applied = {}", versionApplied);
Expand Down

0 comments on commit 6708c81

Please sign in to comment.