Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2014, JBoss Inc., and individual contributors as indicated
   * by the @authors tag.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.jboss.as.test.integration.auditlog;
 
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ACCESS;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ALLOW_RESOURCE_SERVICE_RESTART;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.APP_NAME;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.AUDIT;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.AUDIT_LOG;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.CHILD_TYPE;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.COMPOSITE;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.CORE_SERVICE;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.FACILITY;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.HANDLER;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.HOST;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.LOGGER;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.LOG_READ_ONLY;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.MANAGEMENT;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OPERATION_HEADERS;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.PORT;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.READ_CHILDREN_NAMES_OPERATION;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ROLLBACK_ON_RUNTIME_FAILURE;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.STEPS;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SYSLOG_FORMAT;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SYSLOG_HANDLER;
 
 import java.util.List;
 
Abstract parent for Syslog handler setup in AuditLog.

Author(s):
Josef Cacek
 
 public abstract class AuditLogToSyslogSetup implements ServerSetupTask {
 
     private static final String SYSLOG_HANDLER_NAME = "audit-test-syslog-handler";
     private static final String SYSLOG_HANDLER_NAME2 = "audit-test-syslog-handler2";
     private static final int SYSLOG_PORT = 9176;
 
     private static final PathAddress AUDIT_LOG_ADDRESS = PathAddress.pathAddress().append()
             .append();
             .append();
 
     private SyslogServerIF server;
 
     private static final String FORMATTER = "formatter";
     private static final String JSON_FORMATTER = "json-formatter";
 
     //Will need some tweaking in EAP
     static final String DEFAULT_APPNAME = "WildFly";

    
Returns name of syslog protocol used. It should be one of "tcp", "udp", "tls"

Returns:
 
     protected abstract String getSyslogProtocol();

    
Returns a new instance of org.productivity.java.syslog4j.server.SyslogServerConfigIF. It's not necessary to specify host or port, these attributes are configured in org.wildfly.core.testrunner.ServerSetupTask.setup(org.wildfly.core.testrunner.ManagementClient) method.

Returns:
 
    protected abstract SyslogServerConfigIF getSyslogConfig();

    
Implementation should return a org.jboss.dmr.ModelNode which configures the syslog protocol in auditlog. It's not necessary to specify host or port, these attributes are configured in org.wildfly.core.testrunner.ServerSetupTask.setup(org.wildfly.core.testrunner.ManagementClient) method.

Parameters:
syslogHandlerAddress the address of the handler for which we are configuring a protocol
Returns:
    protected abstract ModelNode addAuditlogSyslogProtocol(PathAddress syslogHandlerAddress);

    
If protocol configured in addAuditlogSyslogProtocol(org.jboss.as.controller.PathAddress) ()} has additional settings, child classes can override this method for configuration.

Parameters:
syslogHandlerAddress the address of the handler for which we are configuring a protocol
Returns:
    protected List<ModelNodeaddProtocolSettings(PathAddress syslogHandlerAddress) {
        return null;
    }

    
Starts Syslog server and configures syslog handler in the AS.
    @Override
    public void setup(ManagementClient managementClientthrows Exception {
        // clear created server instances (TCP/UDP)
        SyslogServer.shutdown();
        // start and set syslog server
        final String host = managementClient.getMgmtAddress();
        SyslogServerConfigIF config = getSyslogConfig();
        config.setUseStructuredData(true);
        config.setHost(host);
        config.setPort();
        config.addEventHandler(new BlockedSyslogServerEventHandler());
        final String syslogProtocol = getSyslogProtocol();
         = SyslogServer.createInstance(syslogProtocolconfig);
        // start syslog server
        SyslogServer.getThreadedInstance(syslogProtocol);
        // Add the normal syslog handler
        addSyslogHandler(managementClienthostnullnull);
        // Add the syslog handler we will switch to
        addSyslogHandler(managementClienthost"TestApp".);
        // Reference the first audit logger for now
        CoreUtils.applyUpdate(opmanagementClient.getControllerClient());
        op = Util.getWriteAttributeOperation(false);
        CoreUtils.applyUpdate(opmanagementClient.getControllerClient());
    }
    private void addSyslogHandler(ManagementClient managementClientPathAddress syslogHandlerAddressString host,
            String appNameFacility facilitythrows Exception {
        ModelNode op = createSyslogHandlerAddComposite(syslogHandlerAddresshostappNamefacility);
        CoreUtils.applyUpdate(opmanagementClient.getControllerClient());
        List<ModelNodeprotocolSettings = addProtocolSettings(syslogHandlerAddress);
        if (protocolSettings != null) {
            CoreUtils.applyUpdates(protocolSettingsmanagementClient.getControllerClient());
        }
    }
    private ModelNode createSyslogHandlerAddComposite(PathAddress syslogHandlerAddressString hostString appName,
            Facility facility) {
        final ModelNode compositeOp = new ModelNode();
        compositeOp.get().set();
        compositeOp.get().setEmptyList();
        ModelNode steps = compositeOp.get();
        ModelNode op = Util.createAddOperation(syslogHandlerAddress);
        op.get().set();
        op.get().set("RFC5424");
        if (appName != null) {
            op.get().set(appName);
        }
        if (facility != null) {
            op.get().set(facility.toString());
        }
        steps.add(op);
        final ModelNode auditlogSyslogProtocol = addAuditlogSyslogProtocol(syslogHandlerAddress);
        auditlogSyslogProtocol.get().set();
        auditlogSyslogProtocol.get().set(host);
        steps.add(auditlogSyslogProtocol);
        return compositeOp;
    }

    
Stops syslog server and removes auditlog configuration.
    @Override
    public void tearDown(ManagementClient managementClientthrows Exception {
        // stop syslog server
        SyslogServer.shutdown();
        .setThread(null);
        removeResource(managementClient);
        removeResource(managementClient);
        removeResource(managementClient);
        removeResource(managementClient);
                managementClient.getControllerClient());
    }
    private void removeResource(ManagementClient managementClientPathAddress addressthrows Exception {
        PathElement element = address.getLastElement();
        PathAddress parentAddress = address.subAddress(0, address.size() - 1);
        ModelNode op = Util.createOperation(parentAddress);
        op.get().set(element.getKey());
        ModelNode result = managementClient.getControllerClient().execute(op);
        if (result.hasDefined("result") && result.get("result").asList().contains(new ModelNode(element.getValue()))) {
            // It exists so remove it
            op = Util.createRemoveOperation(address);
            op.get().set(false);
            op.get().set(true);
            CoreUtils.applyUpdate(opmanagementClient.getControllerClient());
        }
    }
New to GrepCode? Check out our FAQ X