use of com.axway.ats.log.autodb.DbAppenderConfiguration in project ats-framework by Axway.
the class Test_DbAppenderConfiguration method validateNegativeNoHost.
@Test(expected = InvalidAppenderConfigurationException.class)
public void validateNegativeNoHost() throws InvalidAppenderConfigurationException {
DbAppenderConfiguration appenderConfig = new DbAppenderConfiguration();
appenderConfig.validate();
}
use of com.axway.ats.log.autodb.DbAppenderConfiguration in project ats-framework by Axway.
the class ActiveDbAppender method getCurrentInstance.
/**
* This method doesn't create a new instance,
* but returns the already created one (from log4j2) or null if there is no such.
*
* @return the current DB appender instance
*/
public static ActiveDbAppender getCurrentInstance() {
if (instance == null) {
final LoggerContext context = LoggerContext.getContext(false);
final Configuration config = context.getConfiguration();
Map<String, Appender> appenders = config.getAppenders();
// maybe we should use this one to obtain Active and Passive DB appenders?
if (appenders != null && appenders.size() > 0) {
for (Map.Entry<String, Appender> entry : appenders.entrySet()) {
Appender appender = entry.getValue();
if (appender instanceof ActiveDbAppender) {
instance = (ActiveDbAppender) appender;
isAttached = true;
return instance;
}
}
}
}
if (instance != null) {
return instance;
}
/*
* Configuration in log4j2.xml file was not found for ActiveDbAppender
* A dummy com.axway.ats.log.autodb.DbEventRequestProcessor is
* created in order to prevent NPE when invoking methods such as getRunId()
*/
new AtsConsoleLogger(ActiveDbAppender.class).warn("ATS Database appender is not specified in log4j2.xml file. " + "Methods such as ActiveDbAppender@getRunId() will not work.");
isAttached = false;
/**
* create dummy appender configuration
* This configuration will be replaced with one from log4j2.xml file
*/
DbAppenderConfiguration appenderConfig = new DbAppenderConfiguration();
appenderConfig.setHost(DUMMY_DB_HOST);
appenderConfig.setDatabase(DUMMY_DB_DATABASE);
appenderConfig.setUser(DUMMY_DB_USER);
appenderConfig.setPassword(DUMMY_DB_PASSWORD);
instance = new ActiveDbAppender(appenderConfig);
return instance;
}
use of com.axway.ats.log.autodb.DbAppenderConfiguration in project ats-framework by Axway.
the class DatabaseReadingsRepository method obtainDbConnectionHash.
private String obtainDbConnectionHash(String caller) {
DbAppenderConfiguration dbAppenderConfiguration = PassiveDbAppender.getCurrentInstance(caller).getAppenderConfig();
StringBuilder sb = new StringBuilder();
sb.append(dbAppenderConfiguration.getHost() + "|__|" + dbAppenderConfiguration.getPort() + "|__|" + dbAppenderConfiguration.getDatabase());
return sb.toString();
}
use of com.axway.ats.log.autodb.DbAppenderConfiguration in project ats-framework by Axway.
the class AgentConfigurationServiceImpl method initializeDbConnection.
/**
* This method must be called, in order a connection to the desired DB to be
* initialized. If there are any previous initialized DB connections from
* the same caller, they will be discarded.
*/
@POST
@Path("initializeDbConnection")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response initializeDbConnection(@Context HttpServletRequest request, DbConnectionPojo dbConnectionPojo) {
// we do some cleanup here, it is not be very resource consuming
cleanupExpiredSessions(dbConnectionPojo);
final String caller = getCaller(request, dbConnectionPojo, true);
ThreadsPerCaller.registerThread(caller);
try {
// we have to create a new session,
// so we call getSessionData() only for that
getSessionData(request, dbConnectionPojo);
// create DbAppenderConfiguration
DbAppenderConfiguration newAppenderConfiguration = new DbAppenderConfiguration();
newAppenderConfiguration.setHost(dbConnectionPojo.getDbHost());
newAppenderConfiguration.setPort(dbConnectionPojo.getDbPort());
newAppenderConfiguration.setDatabase(dbConnectionPojo.getDbName());
newAppenderConfiguration.setUser(dbConnectionPojo.getDbUser());
newAppenderConfiguration.setPassword(dbConnectionPojo.getDbPass());
newAppenderConfiguration.setMode(dbConnectionPojo.getMode());
newAppenderConfiguration.setLoggingThreshold(SystemLogLevel.toLevel(dbConnectionPojo.getLoggingThreshold()));
newAppenderConfiguration.setMaxNumberLogEvents(dbConnectionPojo.getMaxNumberLogEvents());
PassiveDbAppender alreadyExistingAppender = PassiveDbAppender.getCurrentInstance(caller);
// check whether PassiveDbAppender for this caller is already registered
if (alreadyExistingAppender != null) {
// check if the already registered PassiveDbAppender's apenderConfiguration is NOT the same and the new one
if (!alreadyExistingAppender.getAppenderConfig().equals(newAppenderConfiguration)) {
/* we have a request for different DB configuration,
* so remove the previous appender and append new one with the desired appender configuration
*/
dbLog.debug("Remove previously attached PassiveDbAppender for caller '" + caller + "'.");
PassiveDbAppender appender = PassiveDbAppender.getCurrentInstance(caller);
appender.stop();
Log4j2Utils.removeAppenderFromLogger(Log4j2Utils.getRootLogger().getName(), appender.getName());
attachPassiveDbAppender(newAppenderConfiguration, dbConnectionPojo.getTimestamp());
dbLog.debug("Successfully attached new PassiveDbAppender for caller '" + caller + "'.");
}
} else {
attachPassiveDbAppender(newAppenderConfiguration, dbConnectionPojo.getTimestamp());
}
} catch (Exception e) {
return Response.serverError().entity(new ErrorPojo(e)).build();
} finally {
ThreadsPerCaller.unregisterThread();
}
String uid = dbConnectionPojo.getUid();
String agentVersion = AtsVersion.getAtsVersion();
return Response.ok("{\"" + ApplicationContext.ATS_UID_SESSION_TOKEN + "\": " + "\"" + uid + "\",\"" + "agent_version" + "\": " + "\"" + agentVersion + "\"}").build();
}
Aggregations