Search in sources :

Example 51 with SingleConfigCode

use of org.jvnet.hk2.config.SingleConfigCode in project Payara by payara.

the class HoggingThreadsConfigurer method execute.

@Override
public void execute(AdminCommandContext context) {
    Config config = targetUtil.getConfig(target);
    HoggingThreadsHealthCheck service = habitat.getService(HoggingThreadsHealthCheck.class);
    final ActionReport actionReport = context.getActionReport();
    if (service == null) {
        actionReport.appendMessage(strings.getLocalString("healthcheck.hoggingthreads.configure.status.error", "Hogging Threads Checker Service could not be found"));
        actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    // Warn about deprecated option
    if (name != null) {
        actionReport.appendMessage("\n--name parameter is decremented, please begin using the --checkerName option\n");
    }
    try {
        HealthCheckServiceConfiguration healthCheckServiceConfiguration = config.getExtensionByType(HealthCheckServiceConfiguration.class);
        HoggingThreadsChecker hoggingThreadConfiguration = healthCheckServiceConfiguration.getCheckerByType(HoggingThreadsChecker.class);
        if (hoggingThreadConfiguration == null) {
            ConfigSupport.apply(new SingleConfigCode<HealthCheckServiceConfiguration>() {

                @Override
                public Object run(final HealthCheckServiceConfiguration healthCheckServiceConfigurationProxy) throws PropertyVetoException, TransactionFailure {
                    HoggingThreadsChecker checkerProxy = healthCheckServiceConfigurationProxy.createChild(HoggingThreadsChecker.class);
                    applyValues(checkerProxy);
                    healthCheckServiceConfigurationProxy.getCheckerList().add(checkerProxy);
                    actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
                    return healthCheckServiceConfigurationProxy;
                }
            }, healthCheckServiceConfiguration);
        } else {
            ConfigSupport.apply(new SingleConfigCode<HoggingThreadsChecker>() {

                @Override
                public Object run(final HoggingThreadsChecker hoggingThreadConfigurationProxy) throws PropertyVetoException, TransactionFailure {
                    applyValues(hoggingThreadConfigurationProxy);
                    actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
                    return hoggingThreadConfigurationProxy;
                }
            }, hoggingThreadConfiguration);
        }
        if (dynamic) {
            if (server.isDas()) {
                if (targetUtil.getConfig(target).isDas()) {
                    HoggingThreadsChecker checkerByType = healthCheckServiceConfiguration.getCheckerByType(HoggingThreadsChecker.class);
                    service.setOptions(service.constructOptions(checkerByType));
                    healthCheckService.registerCheck(checkerByType.getName(), service);
                    healthCheckService.reboot();
                }
            } else {
                // it implicitly targetted to us as we are not the DAS
                // restart the service
                HoggingThreadsChecker checkerByType = healthCheckServiceConfiguration.getCheckerByType(HoggingThreadsChecker.class);
                service.setOptions(service.constructOptions(hoggingThreadConfiguration));
                healthCheckService.registerCheck(checkerByType.getName(), service);
                healthCheckService.reboot();
            }
        }
    } catch (TransactionFailure ex) {
        logger.log(Level.WARNING, "Exception during command ", ex);
        actionReport.setMessage(ex.getCause().getMessage());
        actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
    }
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) HoggingThreadsChecker(fish.payara.nucleus.healthcheck.configuration.HoggingThreadsChecker) Config(com.sun.enterprise.config.serverbeans.Config) HoggingThreadsHealthCheck(fish.payara.nucleus.healthcheck.preliminary.HoggingThreadsHealthCheck) HealthCheckServiceConfiguration(fish.payara.nucleus.healthcheck.configuration.HealthCheckServiceConfiguration) ActionReport(org.glassfish.api.ActionReport)

Example 52 with SingleConfigCode

use of org.jvnet.hk2.config.SingleConfigCode in project Payara by payara.

the class DisablePhoneHome method execute.

@Override
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    try {
        ConfigSupport.apply(new SingleConfigCode<PhoneHomeRuntimeConfiguration>() {

            @Override
            public Object run(PhoneHomeRuntimeConfiguration configurationProxy) throws PropertyVetoException, TransactionFailure {
                configurationProxy.setEnabled("false");
                return configurationProxy;
            }
        }, configuration);
    } catch (TransactionFailure ex) {
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
    }
    service.stop();
    report.setMessage("Phone Home Service is disabled");
    report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ActionReport(org.glassfish.api.ActionReport) PhoneHomeRuntimeConfiguration(fish.payara.nucleus.phonehome.PhoneHomeRuntimeConfiguration)

Example 53 with SingleConfigCode

use of org.jvnet.hk2.config.SingleConfigCode in project Payara by payara.

the class EnablePhoneHome method execute.

@Override
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    try {
        ConfigSupport.apply(new SingleConfigCode<PhoneHomeRuntimeConfiguration>() {

            @Override
            public Object run(PhoneHomeRuntimeConfiguration configurationProxy) throws PropertyVetoException, TransactionFailure {
                configurationProxy.setEnabled("true");
                return configurationProxy;
            }
        }, configuration);
    } catch (TransactionFailure ex) {
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
    }
    service.start();
    report.setMessage("Phone Home Service is enabled");
    report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ActionReport(org.glassfish.api.ActionReport) PhoneHomeRuntimeConfiguration(fish.payara.nucleus.phonehome.PhoneHomeRuntimeConfiguration)

Example 54 with SingleConfigCode

use of org.jvnet.hk2.config.SingleConfigCode in project Payara by payara.

the class CreateFileUser method execute.

/**
 * Executes the command with the command parameters passed as Properties where the keys are the paramter names and the
 * values the parameter values
 *
 * @param context information
 */
@Override
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    // Get FileRealm class name, match it with what is expected.
    String fileRealmClassName = fileAuthRealm.getClassname();
    // Report error if provided impl is not the one expected
    if (fileRealmClassName != null && !fileRealmClassName.equals("com.sun.enterprise.security.auth.realm.file.FileRealm")) {
        report.setMessage(localStrings.getLocalString("create.file.user.realmnotsupported", "Configured file realm {0} is not supported.", fileRealmClassName));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    // Ensure we have the file associated with the authrealm
    String keyFile = null;
    for (Property fileProp : fileAuthRealm.getProperty()) {
        if (fileProp.getName().equals("file"))
            keyFile = fileProp.getValue();
    }
    final String kf = keyFile;
    if (keyFile == null) {
        report.setMessage(localStrings.getLocalString("create.file.user.keyfilenotfound", "There is no physical file associated with this file realm {0} ", authRealmName));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    boolean exists = (new File(kf)).exists();
    if (!exists) {
        report.setMessage(localStrings.getLocalString("file.realm.keyfilenonexistent", "The specified physical file {0} associated with the file realm {1} does not exist.", new Object[] { kf, authRealmName }));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    // Now get all inputs ready. userid and groups are straightforward but
    // password is tricky. It is stored in the file passwordfile passed
    // through the CLI options. It is stored under the name
    // AS_ADMIN_USERPASSWORD. Fetch it from there.
    // fetchPassword(report);
    final String password = userpassword;
    if (password == null) {
        report.setMessage(localStrings.getLocalString("create.file.user.keyfilenotreadable", "Password for user {0} " + "has to be specified in --userpassword option or supplied " + "through AS_ADMIN_USERPASSWORD property in the file specified " + "in --passwordfile option", userName));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    // Issue 17525 Fix - Check for null passwords for admin-realm if secureadmin is enabled
    secureAdmin = domain.getSecureAdmin();
    if ((SecureAdmin.Util.isEnabled(secureAdmin)) && (authRealmName.equals(adminService.getAuthRealmName()))) {
        if (password.isEmpty()) {
            report.setMessage(localStrings.getLocalString("null_empty_password", "The admin user password is null or empty"));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
    }
    // now adding user
    try {
        // even though create-file-user is not an update to the security-service
        // do we need to make it transactional by referncing the securityservice
        // hypothetically ?.
        ConfigSupport.apply(new SingleConfigCode<SecurityService>() {

            public Object run(SecurityService param) throws PropertyVetoException, TransactionFailure {
                try {
                    realmsManager.createRealms(config);
                    // If the (shared) keyfile is updated by an external process, load the users first
                    refreshRealm(config.getName(), authRealmName);
                    FileRealm fileRealm = (FileRealm) realmsManager.getFromLoadedRealms(config.getName(), authRealmName);
                    CreateFileUser.handleAdminGroup(authRealmName, groups);
                    String[] groups1 = groups.toArray(new String[groups.size()]);
                    fileRealm.addUser(userName, password.toCharArray(), groups1);
                    fileRealm.persist();
                    report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
                } catch (Exception e) {
                    String localalizedErrorMsg = (e.getLocalizedMessage() == null) ? "" : e.getLocalizedMessage();
                    report.setMessage(localStrings.getLocalString("create.file.user.useraddfailed", "Adding User {0} to the file realm {1} failed", userName, authRealmName) + "  " + localalizedErrorMsg);
                    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                    report.setFailureCause(e);
                }
                return null;
            }
        }, securityService);
    } catch (Exception e) {
        report.setMessage(localStrings.getLocalString("create.file.user.useraddfailed", "Adding User {0} to the file realm {1} failed", userName, authRealmName) + "  " + e.getLocalizedMessage());
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(e);
    }
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) SecurityService(com.sun.enterprise.config.serverbeans.SecurityService) ActionReport(org.glassfish.api.ActionReport) FileRealm(com.sun.enterprise.security.auth.realm.file.FileRealm) Property(org.jvnet.hk2.config.types.Property) File(java.io.File) BadRealmException(com.sun.enterprise.security.auth.realm.BadRealmException) PropertyVetoException(java.beans.PropertyVetoException) NoSuchRealmException(com.sun.enterprise.security.auth.realm.NoSuchRealmException)

Example 55 with SingleConfigCode

use of org.jvnet.hk2.config.SingleConfigCode in project Payara by payara.

the class CreateMessageSecurityProvider method execute.

/**
 * Executes the command with the command parameters passed as Properties
 * where the keys are parameter names and the values the parameter values
 *
 * @param context information
 */
@Override
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    List<MessageSecurityConfig> mscs = secService.getMessageSecurityConfig();
    // Let's find the correct MessageSecurityConfig. As of now,
    // there can be only two of them - one for SOAP and one for
    // HttpServlet
    MessageSecurityConfig msgSecCfg = null;
    for (MessageSecurityConfig msc : mscs) {
        if (msc.getAuthLayer().equals(authLayer)) {
            msgSecCfg = msc;
        }
    }
    // then, add a new provider config under it provided it is not duplicate
    if (msgSecCfg != null) {
        // check if there exists a provider config by the
        // specified provider name; if so return failure.
        List<ProviderConfig> pcs = msgSecCfg.getProviderConfig();
        for (ProviderConfig pc : pcs) {
            if (pc.getProviderId().equals(providerId)) {
                report.setMessage(localStrings.getLocalString("create.message.security.provider.duplicatefound", "Message security provider named {0} exists. " + "Cannot add duplicate.", providerId));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
        }
        // No duplicate message security providers found. So add one.
        try {
            ConfigSupport.apply(new SingleConfigCode<MessageSecurityConfig>() {

                public Object run(MessageSecurityConfig param) throws PropertyVetoException, TransactionFailure {
                    ProviderConfig newPC = param.createChild(ProviderConfig.class);
                    populateProviderConfigElement(newPC);
                    param.getProviderConfig().add(newPC);
                    // security config object
                    if (isDefaultProvider) {
                        if (providerType.equals(SERVER) || providerType.equals(CLIENT_SERVER)) {
                            param.setDefaultProvider(providerId);
                        }
                        if (providerType.equals(CLIENT) || providerType.equals(CLIENT_SERVER)) {
                            param.setDefaultClientProvider(providerId);
                        }
                    }
                    return newPC;
                }
            }, msgSecCfg);
        } catch (TransactionFailure e) {
            report.setMessage(localStrings.getLocalString("create.message.security.provider.fail", "Creation of message security provider named {0} failed", providerId));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setFailureCause(e);
            return;
        }
        report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
        report.setMessage(localStrings.getLocalString("create.message.security.provider.success", "Creation of message security provider named {0} completed " + "successfully", providerId));
    } else // Now if there is NO message security config for this type of layer
    // then, first add a message security config for the layer and then
    // add a provider config under this message security config
    {
        try {
            ConfigSupport.apply(new SingleConfigCode<SecurityService>() {

                public Object run(SecurityService param) throws PropertyVetoException, TransactionFailure {
                    MessageSecurityConfig newMSC = param.createChild(MessageSecurityConfig.class);
                    newMSC.setAuthLayer(authLayer);
                    param.getMessageSecurityConfig().add(newMSC);
                    ProviderConfig newPC = newMSC.createChild(ProviderConfig.class);
                    populateProviderConfigElement(newPC);
                    newMSC.getProviderConfig().add(newPC);
                    // security config object
                    if (isDefaultProvider) {
                        if (providerType.equals(SERVER) || providerType.equals(CLIENT_SERVER)) {
                            newMSC.setDefaultProvider(providerId);
                        }
                        if (providerType.equals(CLIENT) || providerType.equals(CLIENT_SERVER)) {
                            newMSC.setDefaultClientProvider(providerId);
                        }
                    }
                    return newMSC;
                }
            }, secService);
        } catch (TransactionFailure e) {
            report.setMessage(localStrings.getLocalString("create.message.security.provider.fail", "Creation of message security provider named {0} failed", providerId));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setFailureCause(e);
            return;
        }
        report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
    /* report.setMessage(localStrings.getLocalString(
                "create.message.security.provider.success",
                "Creation of message security provider named {0} completed " +
                "successfully", providerId));  */
    }
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ProviderConfig(com.sun.enterprise.config.serverbeans.ProviderConfig) SecurityService(com.sun.enterprise.config.serverbeans.SecurityService) MessageSecurityConfig(com.sun.enterprise.config.serverbeans.MessageSecurityConfig) ActionReport(org.glassfish.api.ActionReport)

Aggregations

TransactionFailure (org.jvnet.hk2.config.TransactionFailure)153 PropertyVetoException (java.beans.PropertyVetoException)130 ActionReport (org.glassfish.api.ActionReport)76 Config (com.sun.enterprise.config.serverbeans.Config)47 Property (org.jvnet.hk2.config.types.Property)27 Resources (com.sun.enterprise.config.serverbeans.Resources)25 List (java.util.List)23 SingleConfigCode (org.jvnet.hk2.config.SingleConfigCode)19 ResourceStatus (org.glassfish.resourcebase.resources.api.ResourceStatus)17 CommandTarget (org.glassfish.config.support.CommandTarget)15 NetworkListener (org.glassfish.grizzly.config.dom.NetworkListener)15 Protocol (org.glassfish.grizzly.config.dom.Protocol)15 Target (org.glassfish.internal.api.Target)15 Test (org.junit.Test)15 NetworkConfig (org.glassfish.grizzly.config.dom.NetworkConfig)13 ArrayList (java.util.ArrayList)11 Protocols (org.glassfish.grizzly.config.dom.Protocols)11 Resource (com.sun.enterprise.config.serverbeans.Resource)9 SecurityService (com.sun.enterprise.config.serverbeans.SecurityService)9 Properties (java.util.Properties)9