Search in sources :

Example 81 with SingleConfigCode

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

the class BaseHealthCheckNotifierConfigurer method execute.

@Override
public void execute(AdminCommandContext context) {
    final ActionReport actionReport = context.getActionReport();
    Properties extraProperties = actionReport.getExtraProperties();
    if (extraProperties == null) {
        extraProperties = new Properties();
        actionReport.setExtraProperties(extraProperties);
    }
    Config configuration = targetUtil.getConfig(target);
    final HealthCheckServiceConfiguration healthCheckServiceConfiguration = configuration.getExtensionByType(HealthCheckServiceConfiguration.class);
    ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
    notifierClass = (Class<C>) genericSuperclass.getActualTypeArguments()[0];
    C c = healthCheckServiceConfiguration.getNotifierByType(notifierClass);
    final C[] config = ObjectArrays.newArray(notifierClass, 1);
    try {
        if (c == null) {
            ConfigSupport.apply(new SingleConfigCode<HealthCheckServiceConfiguration>() {

                @Override
                public Object run(final HealthCheckServiceConfiguration healthCheckServiceConfigurationProxy) throws PropertyVetoException, TransactionFailure {
                    C c = healthCheckServiceConfigurationProxy.createChild(notifierClass);
                    applyValues(c);
                    healthCheckServiceConfigurationProxy.getNotifierList().add(c);
                    actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
                    return healthCheckServiceConfigurationProxy;
                }
            }, healthCheckServiceConfiguration);
        } else {
            config[0] = c;
            ConfigSupport.apply(new SingleConfigCode<C>() {

                public Object run(C cProxy) throws PropertyVetoException, TransactionFailure {
                    applyValues(cProxy);
                    actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
                    return cProxy;
                }
            }, c);
        }
        if (dynamic) {
            if (server.isDas()) {
                if (targetUtil.getConfig(target).isDas()) {
                    configureDynamically();
                }
            } else {
                configureDynamically();
            }
        }
    } catch (TransactionFailure ex) {
        logger.log(Level.WARNING, "Exception during command ", ex);
        actionReport.setMessage(ex.getCause().getMessage());
        actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Config(com.sun.enterprise.config.serverbeans.Config) HealthCheckServiceConfiguration(fish.payara.nucleus.healthcheck.configuration.HealthCheckServiceConfiguration) ActionReport(org.glassfish.api.ActionReport) Properties(java.util.Properties) ParameterizedType(java.lang.reflect.ParameterizedType) PropertyVetoException(java.beans.PropertyVetoException)

Example 82 with SingleConfigCode

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

the class UpdateConnectorWorkSecurityMap method execute.

public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    if (addPrincipals == null && addGroups == null && removeGroups == null && removePrincipals == null) {
        report.setMessage(localStrings.getLocalString("update.connector.work.security.map.noargs", "update-connector-work-security-map should be executed with atleast one optional argument of " + "either add(principals/usergroups) or remove(principals/usergroups)"));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    if (!WorkSecurityMapHelper.doesResourceAdapterNameExist(raName, domain.getResources())) {
        report.setMessage(localStrings.getLocalString("update.connector.work.security.map.noSuchRAFound", "Resource Adapter {0} does not exist. Please specify a resource adapter name.", raName));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    if (!WorkSecurityMapHelper.doesMapNameExist(raName, securityMapName, domain.getResources())) {
        report.setMessage(localStrings.getLocalString("update.connector.work.security.map.mapNotExist", "WorkSecurity map {0} does not exist for resource adapter {1}. Please give a valid map name.", securityMapName, raName));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    // check if addPrincipals and removePrincipals have the same value
    if (addPrincipals != null && removePrincipals != null) {
        Iterator it_1 = addPrincipals.entrySet().iterator();
        while (it_1.hasNext()) {
            String ap = ((Map.Entry) it_1.next()).getKey().toString();
            for (String rp : removePrincipals) {
                if (rp.equals(ap)) {
                    report.setMessage(localStrings.getLocalString("update.connector.work.security.map.samePrincipalValues", "This value {0} is given in both --addprincipals and --removeprincipals. " + "The same value cannot given for these options.", ap));
                    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                    return;
                }
            }
        }
    }
    // check if addUserGroups and removeUserGroups have the same value
    if (addGroups != null && removeGroups != null) {
        Iterator it_1 = addGroups.entrySet().iterator();
        while (it_1.hasNext()) {
            String ag = ((Map.Entry) it_1.next()).getKey().toString();
            for (String rg : removeGroups) {
                if (rg.equals(ag)) {
                    report.setMessage(localStrings.getLocalString("update.connector.work.security.map.sameUsergroupValues", "This value {0} is given in both --addusergroups and --removeusergroups. " + "The same value cannot given for these options.", ag));
                    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                    return;
                }
            }
        }
    }
    WorkSecurityMap map = WorkSecurityMapHelper.getSecurityMap(securityMapName, raName, domain.getResources());
    final List<PrincipalMap> existingPrincipals = new ArrayList(map.getPrincipalMap());
    final List<GroupMap> existingUserGroups = new ArrayList(map.getGroupMap());
    if (existingPrincipals.isEmpty() && addPrincipals != null) {
        report.setMessage(localStrings.getLocalString("update.connector.work.security.map." + "addPrincipalToExistingUserGroupsWorkSecurityMap", "Failed to add principals to a security map with user groups."));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    if (existingUserGroups.isEmpty() && addGroups != null) {
        report.setMessage(localStrings.getLocalString("update.connector.work.security.map." + "addUserGroupsToExistingPrincipalsWorkSecurityMap", "Failed to add user groups to a security map with principals."));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    if (addPrincipals == null && addGroups == null) {
        boolean principalsEmpty = false;
        boolean userGroupsEmpty = false;
        if ((removePrincipals != null) && (removePrincipals.size() == existingPrincipals.size())) {
            principalsEmpty = true;
        }
        if ((removeGroups != null) && (removeGroups.size() == existingUserGroups.size())) {
            userGroupsEmpty = true;
        }
        if (userGroupsEmpty || principalsEmpty) {
            report.setMessage(localStrings.getLocalString("" + "update.connector.work.security.map.principals_usergroups_will_be_null", "The values in your command will delete all principals and usergroups. You cannot " + "delete all principals and usergroups. Atleast one of them must exist."));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
    }
    if (removePrincipals != null) {
        for (String rp : removePrincipals) {
            boolean principalExist = false;
            for (PrincipalMap pm : existingPrincipals) {
                if (pm.getEisPrincipal().equals(rp)) {
                    principalExist = true;
                    break;
                }
            }
            if (!principalExist) {
                report.setMessage(localStrings.getLocalString("" + "update.connector.work.security.map.principalNotExists", "The principal {0} that you want to delete does not exist in security map {1}." + " Please give a valid principal name.", rp, securityMapName));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
        }
    }
    if (removeGroups != null) {
        for (String rg : removeGroups) {
            boolean usergroupExist = false;
            for (GroupMap gm : existingUserGroups) {
                if (gm.getEisGroup().equals(rg)) {
                    usergroupExist = true;
                    break;
                }
            }
            if (!usergroupExist) {
                report.setMessage(localStrings.getLocalString("" + "update.connector.work.security.map.usergroupNotExists", "The usergroup {0} that you want to delete does not exist in security map {1}. " + "Please give a valid user-group name.", rg, securityMapName));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
        }
    }
    if (addPrincipals != null) {
        for (Map.Entry e : addPrincipals.entrySet()) {
            for (PrincipalMap pm : existingPrincipals) {
                if (pm.getEisPrincipal().equals(e.getKey())) {
                    report.setMessage(localStrings.getLocalString("" + "update.connector.work.security.map.principalExists", "The principal {0} already exists in security map {1}. " + "Please give a different principal name.", e.getKey(), securityMapName));
                    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                    return;
                }
            }
        }
    }
    if (addGroups != null) {
        for (Map.Entry e : addGroups.entrySet()) {
            for (GroupMap gm : existingUserGroups) {
                if (gm.getEisGroup().equals(e.getKey())) {
                    report.setMessage(localStrings.getLocalString("" + "update.connector.work.security.map.groupExists", "The Group {0} already exists in security map {1}. " + "Please give a different group name.", e.getKey(), securityMapName));
                    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                    return;
                }
            }
        }
    }
    try {
        ConfigSupport.apply(new SingleConfigCode<WorkSecurityMap>() {

            public Object run(WorkSecurityMap wsm) throws PropertyVetoException, TransactionFailure {
                if (addGroups != null) {
                    for (Map.Entry e : addGroups.entrySet()) {
                        GroupMap gm = wsm.createChild(GroupMap.class);
                        gm.setEisGroup((String) e.getKey());
                        gm.setMappedGroup((String) e.getValue());
                        wsm.getGroupMap().add(gm);
                    }
                } else if (addPrincipals != null) {
                    for (Map.Entry e : addPrincipals.entrySet()) {
                        PrincipalMap pm = wsm.createChild(PrincipalMap.class);
                        pm.setEisPrincipal((String) e.getKey());
                        pm.setMappedPrincipal((String) e.getValue());
                        wsm.getPrincipalMap().add(pm);
                    }
                }
                if (removeGroups != null) {
                    for (String rg : removeGroups) {
                        for (GroupMap gm : existingUserGroups) {
                            if (gm.getEisGroup().equals(rg)) {
                                wsm.getGroupMap().remove(gm);
                            }
                        }
                    }
                } else if (removePrincipals != null) {
                    for (String rp : removePrincipals) {
                        for (PrincipalMap pm : existingPrincipals) {
                            if (pm.getEisPrincipal().equals(rp)) {
                                wsm.getPrincipalMap().remove(pm);
                            }
                        }
                    }
                }
                return wsm;
            }
        }, map);
        report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
    } catch (TransactionFailure tfe) {
        Object[] params = { securityMapName, raName };
        report.setMessage(localStrings.getLocalString("update.connector.work.security.map.fail", "Unable to update security map {0} for resource adapter {1}.", params) + " " + tfe.getLocalizedMessage());
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(tfe);
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) PrincipalMap(org.glassfish.connectors.config.PrincipalMap) WorkSecurityMap(org.glassfish.connectors.config.WorkSecurityMap) ActionReport(org.glassfish.api.ActionReport) PropertyVetoException(java.beans.PropertyVetoException) GroupMap(org.glassfish.connectors.config.GroupMap) PrincipalMap(org.glassfish.connectors.config.PrincipalMap) WorkSecurityMap(org.glassfish.connectors.config.WorkSecurityMap) GroupMap(org.glassfish.connectors.config.GroupMap)

Example 83 with SingleConfigCode

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

the class DeleteConnectorResource method execute.

/**
 * Executes the command with the command parameters passed as Properties
 * where the keys are the parameter names and the values the parameter values
 *
 * @param context information
 */
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    if (jndiName == null) {
        report.setMessage(localStrings.getLocalString("delete.connector.resource.noJndiName", "No JNDI name defined for connector resource."));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    // ensure we already have this resource
    Resource r = ConnectorsUtil.getResourceByName(domain.getResources(), ConnectorResource.class, jndiName);
    if (r == null) {
        report.setMessage(localStrings.getLocalString("delete.connector.resource.notfound", "A connector resource named {0} does not exist.", jndiName));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    if ("system-all-req".equals(r.getObjectType())) {
        report.setMessage(localStrings.getLocalString("delete.connector.resource.notAllowed", "The {0} resource cannot be deleted as it is required to be configured in the system.", jndiName));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    if (environment.isDas()) {
        if ("domain".equals(target)) {
            if (resourceUtil.getTargetsReferringResourceRef(jndiName).size() > 0) {
                report.setMessage(localStrings.getLocalString("delete.connector.resource.resource-ref.exist", "connector-resource [ {0} ] is referenced in an" + "instance/cluster target, Use delete-resource-ref on appropriate target", jndiName));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
        } else {
            if (!resourceUtil.isResourceRefInTarget(jndiName, target)) {
                report.setMessage(localStrings.getLocalString("delete.connector.resource.no.resource-ref", "connector-resource [ {0} ] is not referenced in target [ {1} ]", jndiName, target));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
            if (resourceUtil.getTargetsReferringResourceRef(jndiName).size() > 1) {
                report.setMessage(localStrings.getLocalString("delete.connector.resource.multiple.resource-refs", "connector resource [ {0} ] is referenced in multiple " + "instance/cluster targets, Use delete-resource-ref on appropriate target", jndiName));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
        }
    }
    try {
        // delete resource-ref
        resourceUtil.deleteResourceRef(jndiName, target);
        // delete connector-resource
        if (ConfigSupport.apply(new SingleConfigCode<Resources>() {

            public Object run(Resources param) throws PropertyVetoException, TransactionFailure {
                ConnectorResource resource = (ConnectorResource) ConnectorsUtil.getResourceByName(domain.getResources(), ConnectorResource.class, jndiName);
                return param.getResources().remove(resource);
            }
        }, domain.getResources()) == null) {
            report.setMessage(localStrings.getLocalString("delete.connector.resource.fail", "Connector resource {0} delete failed ", jndiName));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
    } catch (TransactionFailure tfe) {
        report.setMessage(localStrings.getLocalString("delete.connector.resource.fail", "Connector resource {0} delete failed ", jndiName) + " " + tfe.getLocalizedMessage());
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(tfe);
    }
    report.setMessage(localStrings.getLocalString("delete.connector.resource.success", "Connector resource {0} deleted successfully", jndiName));
    report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) SingleConfigCode(org.jvnet.hk2.config.SingleConfigCode) ConnectorResource(org.glassfish.connectors.config.ConnectorResource) Resource(com.sun.enterprise.config.serverbeans.Resource) Resources(com.sun.enterprise.config.serverbeans.Resources) ActionReport(org.glassfish.api.ActionReport) ConnectorResource(org.glassfish.connectors.config.ConnectorResource)

Example 84 with SingleConfigCode

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

the class DeleteCustomResourceTest method tearDown.

@After
public void tearDown() throws TransactionFailure {
    ConfigSupport.apply(new SingleConfigCode<Resources>() {

        public Object run(Resources param) throws PropertyVetoException, TransactionFailure {
            Resource target = null;
            for (Resource resource : param.getResources()) {
                if (resource instanceof CustomResource) {
                    CustomResource r = (CustomResource) resource;
                    if (r.getJndiName().equals("sample_custom_resource")) {
                        target = resource;
                        break;
                    }
                }
            }
            if (target != null) {
                param.getResources().remove(target);
            }
            return null;
        }
    }, resources);
    parameters = new ParameterMap();
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) CustomResource(org.glassfish.resources.config.CustomResource) CustomResource(org.glassfish.resources.config.CustomResource) Resource(com.sun.enterprise.config.serverbeans.Resource) ParameterMap(org.glassfish.api.admin.ParameterMap) After(org.junit.After)

Example 85 with SingleConfigCode

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

the class CreateIiopListener method execute.

/**
 * Executes the command with the command parameters passed as Properties
 * where the keys are the parameter names and the values the parameter values
 *
 * @param context information
 */
@Override
public void execute(AdminCommandContext context) {
    final Target targetUtil = services.getService(Target.class);
    final Config config = targetUtil.getConfig(target);
    final ActionReport report = context.getActionReport();
    IiopService iiopService = config.getExtensionByType(IiopService.class);
    // check port uniqueness, only for same address
    for (IiopListener listener : iiopService.getIiopListener()) {
        if (listener.getId().equals(listener_id)) {
            String ls = localStrings.getLocalString("create.iiop.listener.duplicate", "IIOP Listener named {0} already exists.", listener_id);
            report.setMessage(ls);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
        if (listener.getAddress().trim().equals(listeneraddress) && listener.getPort().trim().equals((iiopport))) {
            String def = "Port [{0}] is already taken by another listener: " + "[{1}] for address [{2}], choose another port.";
            String ls = localStrings.getLocalString("create.iiop.listener.port.occupied", def, iiopport, listener.getId(), listeneraddress);
            report.setMessage(ls);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
    }
    try {
        ConfigSupport.apply(new SingleConfigCode<IiopService>() {

            @Override
            public Object run(IiopService param) throws PropertyVetoException, TransactionFailure {
                IiopListener newListener = param.createChild(IiopListener.class);
                newListener.setId(listener_id);
                newListener.setAddress(listeneraddress);
                newListener.setPort(iiopport);
                newListener.setSecurityEnabled(securityenabled.toString());
                newListener.setEnabled(enabled.toString());
                newListener.setLazyInit(Boolean.toString(lazyInit));
                // add properties
                if (properties != null) {
                    for (java.util.Map.Entry entry : properties.entrySet()) {
                        Property property = newListener.createChild(Property.class);
                        property.setName((String) entry.getKey());
                        property.setValue((String) entry.getValue());
                        newListener.getProperty().add(property);
                    }
                }
                param.getIiopListener().add(newListener);
                return newListener;
            }
        }, iiopService);
        report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
    } catch (TransactionFailure e) {
        String actual = e.getMessage();
        String def = "Creation of: " + listener_id + "failed because of: " + actual;
        String msg = localStrings.getLocalString("create.iiop.listener.fail", def, listener_id, actual);
        report.setMessage(msg);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(e);
    }
}
Also used : IiopListener(org.glassfish.orb.admin.config.IiopListener) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Config(com.sun.enterprise.config.serverbeans.Config) ActionReport(org.glassfish.api.ActionReport) PropertyVetoException(java.beans.PropertyVetoException) Target(org.glassfish.internal.api.Target) CommandTarget(org.glassfish.config.support.CommandTarget) IiopService(org.glassfish.orb.admin.config.IiopService) Property(org.jvnet.hk2.config.types.Property)

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