Search in sources :

Example 6 with DbAppenderConfiguration

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();
}
Also used : DbAppenderConfiguration(com.axway.ats.log.autodb.DbAppenderConfiguration) Test(org.junit.Test)

Example 7 with DbAppenderConfiguration

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;
}
Also used : Appender(org.apache.logging.log4j.core.Appender) Configuration(org.apache.logging.log4j.core.config.Configuration) DbAppenderConfiguration(com.axway.ats.log.autodb.DbAppenderConfiguration) DbAppenderConfiguration(com.axway.ats.log.autodb.DbAppenderConfiguration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) HashMap(java.util.HashMap) Map(java.util.Map) AtsConsoleLogger(com.axway.ats.core.log.AtsConsoleLogger)

Example 8 with DbAppenderConfiguration

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();
}
Also used : DbAppenderConfiguration(com.axway.ats.log.autodb.DbAppenderConfiguration)

Example 9 with DbAppenderConfiguration

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();
}
Also used : ErrorPojo(com.axway.ats.agent.webapp.restservice.model.pojo.ErrorPojo) DbAppenderConfiguration(com.axway.ats.log.autodb.DbAppenderConfiguration) PassiveDbAppender(com.axway.ats.log.appenders.PassiveDbAppender) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Aggregations

DbAppenderConfiguration (com.axway.ats.log.autodb.DbAppenderConfiguration)9 Test (org.junit.Test)6 ErrorPojo (com.axway.ats.agent.webapp.restservice.model.pojo.ErrorPojo)1 AtsConsoleLogger (com.axway.ats.core.log.AtsConsoleLogger)1 PassiveDbAppender (com.axway.ats.log.appenders.PassiveDbAppender)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Appender (org.apache.logging.log4j.core.Appender)1 LoggerContext (org.apache.logging.log4j.core.LoggerContext)1 Configuration (org.apache.logging.log4j.core.config.Configuration)1