Search in sources :

Example 1 with AdminAuditConfiguration

use of fish.payara.audit.AdminAuditConfiguration in project Payara by payara.

the class GetAdminAuditServiceConfiguration method execute.

@Override
public void execute(AdminCommandContext context) {
    final ActionReport actionReport = context.getActionReport();
    ColumnFormatter columnFormatter = new ColumnFormatter(ATTRIBUTE_HEADERS);
    ColumnFormatter notifiersColumnFormatter = new ColumnFormatter(NOTIFIER_HEADERS);
    targetConfig = targetUtil.getConfig(target);
    AdminAuditConfiguration config = targetConfig.getExtensionByType(AdminAuditConfiguration.class);
    Object[] configValues = { config.getEnabled(), config.getAuditLevel() };
    columnFormatter.addRow(configValues);
    Map<String, Object> map = new HashMap<>();
    Properties extraProperties = new Properties();
    map.put("enabled", config.getEnabled());
    map.put("auditLevel", config.getAuditLevel());
    extraProperties.put("adminauditConfiguration", map);
    ActionReport notifiersReport = actionReport.addSubActionsReport();
    List<ServiceHandle<PayaraNotifier>> allNotifierServiceHandles = serviceLocator.getAllServiceHandles(PayaraNotifier.class);
    Properties notifierProps = new Properties();
    if (!config.getNotifierList().isEmpty()) {
        List<String> notifiers = config.getNotifierList();
        for (ServiceHandle<PayaraNotifier> serviceHandle : allNotifierServiceHandles) {
            final String notifierClassName = serviceHandle.getActiveDescriptor().getImplementationClass().getSimpleName();
            final String notifierName = NotifierUtils.getNotifierName(serviceHandle.getActiveDescriptor());
            Object[] values = new Object[2];
            values[0] = notifierName;
            values[1] = notifiers.contains(notifierName);
            notifiersColumnFormatter.addRow(values);
            Map<String, Object> mapNotifiers = new HashMap<>(2);
            mapNotifiers.put("notifierName", values[0]);
            mapNotifiers.put("notifierEnabled", values[1]);
            notifierProps.put("notifierList" + notifierClassName, mapNotifiers);
        }
    }
    notifiersReport.setMessage(notifiersColumnFormatter.toString());
    extraProperties.putAll(notifierProps);
    actionReport.setExtraProperties(extraProperties);
    actionReport.setMessage(columnFormatter.toString());
    actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
Also used : PayaraNotifier(fish.payara.internal.notification.PayaraNotifier) HashMap(java.util.HashMap) ActionReport(org.glassfish.api.ActionReport) Properties(java.util.Properties) AdminAuditConfiguration(fish.payara.audit.AdminAuditConfiguration) ServiceHandle(org.glassfish.hk2.api.ServiceHandle) ColumnFormatter(com.sun.enterprise.util.ColumnFormatter)

Example 2 with AdminAuditConfiguration

use of fish.payara.audit.AdminAuditConfiguration in project Payara by payara.

the class SetAdminAuditConfiguration method execute.

@Override
public void execute(AdminCommandContext context) {
    ActionReport report = context.getActionReport();
    targetConfig = targetUtil.getConfig(target);
    final AdminAuditConfiguration configuration = targetConfig.getExtensionByType(AdminAuditConfiguration.class);
    try {
        final Set<String> notifierNames = NotifierUtils.getNotifierNames(serviceLocator);
        ConfigSupport.apply(new SingleConfigCode<AdminAuditConfiguration>() {

            @Override
            public Object run(AdminAuditConfiguration proxy) throws PropertyVetoException, TransactionFailure {
                if (enabled != null) {
                    proxy.enabled(enabled.toString());
                }
                if (auditLevel != null) {
                    proxy.setAuditLevel(auditLevel);
                }
                List<String> notifiers = proxy.getNotifierList();
                if (enableNotifiers != null) {
                    for (String notifier : enableNotifiers) {
                        if (notifierNames.contains(notifier)) {
                            if (!notifiers.contains(notifier)) {
                                notifiers.add(notifier);
                            }
                        } else {
                            throw new PropertyVetoException("Unrecognised notifier " + notifier, new PropertyChangeEvent(proxy, "notifiers", notifiers, notifiers));
                        }
                    }
                }
                if (disableNotifiers != null) {
                    for (String notifier : disableNotifiers) {
                        if (notifierNames.contains(notifier)) {
                            notifiers.remove(notifier);
                        } else {
                            throw new PropertyVetoException("Unrecognised notifier " + notifier, new PropertyChangeEvent(proxy, "notifiers", notifiers, notifiers));
                        }
                    }
                }
                if (setNotifiers != null) {
                    notifiers.clear();
                    for (String notifier : setNotifiers) {
                        if (notifierNames.contains(notifier)) {
                            if (!notifiers.contains(notifier)) {
                                notifiers.add(notifier);
                            }
                        } else {
                            throw new PropertyVetoException("Unrecognised notifier " + notifier, new PropertyChangeEvent(proxy, "notifiers", notifiers, notifiers));
                        }
                    }
                }
                return null;
            }
        }, configuration);
        if (dynamic != null && dynamic && target.equals("server-config")) {
            auditService.setEnabled(enabled);
            auditService.setAuditLevel(AuditLevel.valueOf(auditLevel));
            Set<String> notifiers = auditService.getEnabledNotifiers();
            if (enableNotifiers != null) {
                enableNotifiers.forEach(notifiers::add);
            }
            if (disableNotifiers != null) {
                disableNotifiers.forEach(notifiers::remove);
            }
            if (setNotifiers != null) {
                notifiers.clear();
                setNotifiers.forEach(notifiers::add);
            }
        }
    } catch (TransactionFailure ex) {
        LOGGER.log(Level.SEVERE, null, ex);
        report.setMessage(ex.getCause() != null ? ex.getCause().getMessage() : ex.getMessage());
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
    }
    report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) PropertyChangeEvent(java.beans.PropertyChangeEvent) List(java.util.List) ActionReport(org.glassfish.api.ActionReport) AdminAuditConfiguration(fish.payara.audit.AdminAuditConfiguration)

Aggregations

AdminAuditConfiguration (fish.payara.audit.AdminAuditConfiguration)2 ActionReport (org.glassfish.api.ActionReport)2 ColumnFormatter (com.sun.enterprise.util.ColumnFormatter)1 PayaraNotifier (fish.payara.internal.notification.PayaraNotifier)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyVetoException (java.beans.PropertyVetoException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Properties (java.util.Properties)1 ServiceHandle (org.glassfish.hk2.api.ServiceHandle)1 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)1