Search in sources :

Example 6 with ConfigCode

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

the class UpdateResourceRef method execute.

/**
 * Execution method for updating the configuration of a ResourceRef. Will be
 * replicated if the target is a cluster.
 *
 * @param context context for the command.
 */
@Override
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    final Logger logger = context.getLogger();
    // Make a list of all ResourceRefs that need to change
    List<ResourceRef> resourceRefsToChange = new ArrayList<>();
    // Add the ResourceRef from a named server if the target is a server
    Server server = domain.getServerNamed(target);
    // if the target is a server
    if (server != null) {
        ResourceRef serverResourceRef = server.getResourceRef(name);
        // if the ResourceRef doesn't exist
        if (serverResourceRef == null) {
            report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
            return;
        }
        resourceRefsToChange.add(serverResourceRef);
    }
    // Add the ResourceRef from a named config if the target is a config
    Config config = domain.getConfigNamed(target);
    // if the target is a config
    if (config != null) {
        ResourceRef configResourceRef = config.getResourceRef(name);
        // if the ResourceRef doesn't exist
        if (configResourceRef == null) {
            report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
            return;
        }
        resourceRefsToChange.add(configResourceRef);
    }
    // Add the ResourceRefs from a named cluster if the target is a cluster
    Cluster cluster = domain.getClusterNamed(target);
    // if the target is a cluster
    if (cluster != null) {
        ResourceRef clusterResourceRef = cluster.getResourceRef(name);
        // if the ResourceRef doesn't exist
        if (clusterResourceRef == null) {
            report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
            return;
        }
        resourceRefsToChange.add(clusterResourceRef);
        for (Server instance : cluster.getInstances()) {
            ResourceRef instanceResourceRef = instance.getResourceRef(name);
            // if the server in the cluster contains the ResourceRef
            if (instanceResourceRef != null) {
                resourceRefsToChange.add(instanceResourceRef);
            }
        }
    }
    // Add the ResourceRefs from a named Deployment Group if the target is a Deployment Group
    DeploymentGroup dg = domain.getDeploymentGroupNamed(target);
    if (dg != null) {
        ResourceRef ref = dg.getResourceRef(name);
        if (ref == null) {
            report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
            return;
        }
        resourceRefsToChange.add(ref);
        for (Server instance : dg.getInstances()) {
            ResourceRef instanceResourceRef = instance.getResourceRef(name);
            // if the server in the dg contains the ResourceRef
            if (instanceResourceRef != null) {
                resourceRefsToChange.add(instanceResourceRef);
            }
        }
    }
    // Apply the configuration to the listed ResourceRefs
    try {
        ConfigSupport.apply(new ConfigCode() {

            @Override
            public Object run(ConfigBeanProxy... params) throws PropertyVetoException, TransactionFailure {
                for (ConfigBeanProxy proxy : params) {
                    if (proxy instanceof ResourceRef) {
                        ResourceRef resourceRefProxy = (ResourceRef) proxy;
                        if (enabled != null) {
                            resourceRefProxy.setEnabled(enabled.toString());
                        }
                    }
                }
                report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
                return true;
            }
        }, resourceRefsToChange.toArray(new ResourceRef[] {}));
    } catch (TransactionFailure ex) {
        report.failure(logger, ex.getLocalizedMessage());
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Server(com.sun.enterprise.config.serverbeans.Server) Config(com.sun.enterprise.config.serverbeans.Config) ArrayList(java.util.ArrayList) Cluster(com.sun.enterprise.config.serverbeans.Cluster) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) PropertyVetoException(java.beans.PropertyVetoException) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) ConfigCode(org.jvnet.hk2.config.ConfigCode) ResourceRef(com.sun.enterprise.config.serverbeans.ResourceRef) DeploymentGroup(fish.payara.enterprise.config.serverbeans.DeploymentGroup)

Example 7 with ConfigCode

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

the class UpdateConnectorSecurityMap 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
 */
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    if (securityMapName == null) {
        report.setMessage(localStrings.getLocalString("create.connector.security.map.noSecurityMapName", "No security map name specified"));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    Collection<ConnectorConnectionPool> ccPools = domain.getResources().getResources(ConnectorConnectionPool.class);
    if (!doesPoolNameExist(poolName, ccPools)) {
        report.setMessage(localStrings.getLocalString("create.connector.security.map.noSuchPoolFound", "Connector connection pool {0} does not exist. Please specify a valid pool name.", poolName));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    if (!doesMapNameExist(poolName, securityMapName, ccPools)) {
        report.setMessage(localStrings.getLocalString("update.connector.security.map.map_does_not_exist", "Security map {0} does not exist for connector connection pool {1}. Please give a valid map name.", securityMapName, poolName));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    // get all the security maps for this pool.....
    List<SecurityMap> maps = getAllSecurityMapsForPool(poolName, ccPools);
    // check if addPrincipals and removePrincipals have the same value
    if (addPrincipals != null && removePrincipals != null) {
        for (String ap : addPrincipals) {
            for (String rp : removePrincipals) {
                if (rp.equals(ap)) {
                    report.setMessage(localStrings.getLocalString("update.connector.security.map.same_principal_values", "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 (addUserGroups != null && removeUserGroups != null) {
        for (String aug : addUserGroups) {
            for (String rug : removeUserGroups) {
                if (rug.equals(aug)) {
                    report.setMessage(localStrings.getLocalString("update.connector.security.map.same_usergroup_values", "This value {0} is given in both --addusergroups and --removeusergroups. The same value cannot given for these options.", aug));
                    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                    return;
                }
            }
        }
    }
    // make sure that the principals to be added are not existing in any map ...
    if (addPrincipals != null) {
        for (String principal : addPrincipals) {
            if (isPrincipalExisting(principal, maps)) {
                report.setMessage(localStrings.getLocalString("create.connector.security.map.principal_exists", "The principal {0} already exists in connector connection pool {1}. Please give a different principal name.", principal, poolName));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
        }
    }
    // make sure that the user groups to be added are not existing in any map ...
    if (addUserGroups != null) {
        for (String userGroup : addUserGroups) {
            if (isUserGroupExisting(userGroup, maps)) {
                report.setMessage(localStrings.getLocalString("create.connector.security.map.usergroup_exists", "The user-group {0} already exists in connector connection pool {1}. Please give a different user-group name.", userGroup, poolName));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
        }
    }
    SecurityMap map = getSecurityMap(securityMapName, poolName, ccPools);
    final List<String> existingPrincipals = new ArrayList(map.getPrincipal());
    final List<String> existingUserGroups = new ArrayList(map.getUserGroup());
    // check if there is any invalid principal in removePrincipals.
    if (removePrincipals != null) {
        boolean principalExists = true;
        String principal = null;
        for (String p : removePrincipals) {
            if (!existingPrincipals.contains(p)) {
                principalExists = false;
                principal = p;
                break;
            }
        }
        if (!principalExists) {
            report.setMessage(localStrings.getLocalString("update.connector.security.map.principal_does_not_exists", "The principal {0} that you want to delete does not exist in connector connection pool {1}. Please give a valid principal name.", principal, poolName));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
    }
    // check if there is any invalid usergroup in removeUserGroups.
    if (removeUserGroups != null) {
        boolean userGroupExists = true;
        String userGroup = null;
        for (String ug : removeUserGroups) {
            if (!existingUserGroups.contains(ug)) {
                userGroupExists = false;
                userGroup = ug;
                break;
            }
        }
        if (!userGroupExists) {
            report.setMessage(localStrings.getLocalString("update.connector.security.map.usergroup_does_not_exists", "The usergroup {0} that you want to delete does not exist in connector connection pool {1}. Please give a valid user-group name.", userGroup, poolName));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
    }
    if (addPrincipals == null && addUserGroups == null) {
        boolean principalsEmpty = false;
        boolean userGroupsEmpty = false;
        if (removePrincipals == null && existingPrincipals.isEmpty()) {
            principalsEmpty = true;
        }
        if (removeUserGroups == null && existingUserGroups.isEmpty()) {
            userGroupsEmpty = true;
        }
        if ((removePrincipals != null) && (removePrincipals.size() == existingPrincipals.size())) {
            principalsEmpty = true;
        }
        if ((removeUserGroups != null) && (removeUserGroups.size() == existingUserGroups.size())) {
            userGroupsEmpty = true;
        }
        if (userGroupsEmpty && principalsEmpty) {
            report.setMessage(localStrings.getLocalString("update.connector.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;
        }
    }
    // add principals to the existingPrincipals arraylist.
    if (addPrincipals != null) {
        for (String principal : addPrincipals) {
            if (!existingPrincipals.contains(principal)) {
                existingPrincipals.add(principal);
            } else {
                report.setMessage(localStrings.getLocalString("create.connector.security.map.principal_exists", "The principal {0} already exists in connector connection pool {1}. Please give a different principal name.", principal, poolName));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
        }
    }
    // removing principals from existingPrincipals arraylist.
    if (removePrincipals != null) {
        for (String principal : removePrincipals) {
            existingPrincipals.remove(principal);
        }
    }
    // adding user-groups....
    if (addUserGroups != null) {
        for (String userGroup : addUserGroups) {
            if (!existingUserGroups.contains(userGroup)) {
                existingUserGroups.add(userGroup);
            } else {
                report.setMessage(localStrings.getLocalString("create.connector.security.map.usergroup_exists", "The user-group {0} already exists in connector connection pool {1}. Please give a different user-group name.", userGroup, poolName));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
        }
    }
    // removing user-groups....
    if (removeUserGroups != null) {
        for (String userGroup : removeUserGroups) {
            existingUserGroups.remove(userGroup);
        }
    }
    // the update-connector-security-map command.
    if (!hasOnlyPrincipalsOrOnlyUserGroups(report, existingPrincipals, existingUserGroups)) {
        return;
    }
    BackendPrincipal backendPrincipal = map.getBackendPrincipal();
    try {
        ConfigSupport.apply(new ConfigCode() {

            public Object run(ConfigBeanProxy... params) throws PropertyVetoException, TransactionFailure {
                SecurityMap sm = (SecurityMap) params[0];
                BackendPrincipal bp = (BackendPrincipal) params[1];
                // setting the updated principal user-group arrays....
                if (existingPrincipals != null) {
                    sm.getPrincipal().clear();
                    for (String principal : existingPrincipals) {
                        sm.getPrincipal().add(principal);
                    }
                }
                if (existingUserGroups != null) {
                    sm.getUserGroup().clear();
                    for (String userGroup : existingUserGroups) {
                        sm.getUserGroup().add(userGroup);
                    }
                }
                // get the backend principal for the given security map and pool...
                if (mappedusername != null && !mappedusername.isEmpty()) {
                    bp.setUserName(mappedusername);
                }
                if (mappedpassword != null) {
                    if (mappedpassword.isEmpty()) {
                        bp.setPassword(null);
                    } else {
                        bp.setPassword(mappedpassword);
                    }
                }
                return sm;
            }
        }, map, backendPrincipal);
        report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
    } catch (TransactionFailure tfe) {
        Object[] params = { securityMapName, poolName };
        report.setMessage(localStrings.getLocalString("update.connector.security.map.fail", "Unable to update security map {0} for connector connection pool {1}.", params) + " " + tfe.getLocalizedMessage());
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(tfe);
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ConnectorConnectionPool(org.glassfish.connectors.config.ConnectorConnectionPool) ArrayList(java.util.ArrayList) ActionReport(org.glassfish.api.ActionReport) PropertyVetoException(java.beans.PropertyVetoException) SecurityMap(org.glassfish.connectors.config.SecurityMap) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) ConfigCode(org.jvnet.hk2.config.ConfigCode) BackendPrincipal(org.glassfish.connectors.config.BackendPrincipal)

Example 8 with ConfigCode

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

the class DeleteNetworkListener 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
 */
public void execute(AdminCommandContext context) {
    Target targetUtil = services.getService(Target.class);
    Config newConfig = targetUtil.getConfig(target);
    if (newConfig != null) {
        config = newConfig;
    }
    ActionReport report = context.getActionReport();
    NetworkListeners networkListeners = config.getNetworkConfig().getNetworkListeners();
    try {
        if (findListener(networkListeners, report)) {
            final Protocol httpProtocol = listenerToBeRemoved.findHttpProtocol();
            final VirtualServer virtualServer = config.getHttpService().getVirtualServerByName(httpProtocol.getHttp().getDefaultVirtualServer());
            ConfigSupport.apply(new ConfigCode() {

                public Object run(ConfigBeanProxy... params) throws PropertyVetoException {
                    final NetworkListeners listeners = (NetworkListeners) params[0];
                    final VirtualServer server = (VirtualServer) params[1];
                    listeners.getNetworkListener().remove(listenerToBeRemoved);
                    server.removeNetworkListener(listenerToBeRemoved.getName());
                    return listenerToBeRemoved;
                }
            }, networkListeners, virtualServer);
        }
        report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
    } catch (TransactionFailure e) {
        report.setMessage(MessageFormat.format(rb.getString(LogFacade.DELETE_NETWORK_LISTENER_FAIL), networkListenerName));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(e);
    }
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Target(org.glassfish.internal.api.Target) CommandTarget(org.glassfish.config.support.CommandTarget) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) ConfigCode(org.jvnet.hk2.config.ConfigCode) Config(com.sun.enterprise.config.serverbeans.Config) NetworkListeners(org.glassfish.grizzly.config.dom.NetworkListeners) ActionReport(org.glassfish.api.ActionReport) Protocol(org.glassfish.grizzly.config.dom.Protocol) VirtualServer(com.sun.enterprise.config.serverbeans.VirtualServer)

Example 9 with ConfigCode

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

the class DeleteProtocolFinder method execute.

@Override
public void execute(AdminCommandContext context) {
    Target targetUtil = services.getService(Target.class);
    Config newConfig = targetUtil.getConfig(target);
    if (newConfig != null) {
        config = newConfig;
    }
    report = context.getActionReport();
    try {
        final Protocols protocols = config.getNetworkConfig().getProtocols();
        final Protocol protocol = protocols.findProtocol(protocolName);
        validate(protocol, LogFacade.CREATE_HTTP_FAIL_PROTOCOL_NOT_FOUND, protocolName);
        PortUnification pu = getPortUnification(protocol);
        ConfigSupport.apply(new ConfigCode() {

            @Override
            public Object run(ConfigBeanProxy... params) {
                final Protocol prot = (Protocol) params[0];
                final PortUnification portUnification = (PortUnification) params[1];
                final List<ProtocolFinder> oldList = portUnification.getProtocolFinder();
                List<ProtocolFinder> newList = new ArrayList<ProtocolFinder>();
                for (final ProtocolFinder finder : oldList) {
                    if (!name.equals(finder.getName())) {
                        newList.add(finder);
                    }
                }
                if (oldList.size() == newList.size()) {
                    throw new RuntimeException(String.format("No finder named %s found for protocol %s", name, protocolName));
                }
                if (newList.isEmpty()) {
                    prot.setPortUnification(null);
                } else {
                    portUnification.setProtocolFinder(newList);
                }
                return null;
            }
        }, protocol, pu);
        cleanPortUnification(pu);
    } catch (ValidationFailureException e) {
        return;
    } catch (Exception e) {
        e.printStackTrace();
        report.setMessage(MessageFormat.format(rb.getString(LogFacade.DELETE_FAIL), name, e.getMessage() == null ? "No reason given" : e.getMessage()));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(e);
        return;
    }
}
Also used : Protocols(org.glassfish.grizzly.config.dom.Protocols) PortUnification(org.glassfish.grizzly.config.dom.PortUnification) Config(com.sun.enterprise.config.serverbeans.Config) ProtocolFinder(org.glassfish.grizzly.config.dom.ProtocolFinder) Target(org.glassfish.internal.api.Target) CommandTarget(org.glassfish.config.support.CommandTarget) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) SingleConfigCode(org.jvnet.hk2.config.SingleConfigCode) ConfigCode(org.jvnet.hk2.config.ConfigCode) ArrayList(java.util.ArrayList) List(java.util.List) Protocol(org.glassfish.grizzly.config.dom.Protocol)

Example 10 with ConfigCode

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

the class UpdateApplicationRefCommand method execute.

/**
 * Execution method for updating the configuration of an ApplicationRef.
 * Will be replicated if the target is a cluster.
 *
 * @param context context for the command.
 */
@Override
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    final Logger logger = context.getLogger();
    // Make a list of all ApplicationRefs that need to change
    List<ApplicationRef> applicationRefsToChange = new ArrayList<>();
    // Add the ApplicationRef which is being immediately targetted
    {
        ApplicationRef primaryApplicationRef = domain.getApplicationRefInTarget(name, target);
        if (primaryApplicationRef == null) {
            report.failure(logger, LOCAL_STRINGS.getLocalString("appref.not.exists", "Target {1} does not have a reference to application {0}.", name, target));
            return;
        }
        applicationRefsToChange.add(primaryApplicationRef);
    }
    // Add the implicitly targetted ApplicationRefs if the target is in a cluster or deployment group
    {
        Cluster cluster = domain.getClusterNamed(target);
        // if the target is a cluster
        if (cluster != null) {
            for (Server server : cluster.getInstances()) {
                ApplicationRef instanceAppRef = server.getApplicationRef(name);
                // if the server in the cluster contains the ApplicationRef
                if (instanceAppRef != null) {
                    applicationRefsToChange.add(instanceAppRef);
                }
            }
        }
        DeploymentGroup dg = domain.getDeploymentGroupNamed(target);
        if (dg != null) {
            for (Server server : dg.getInstances()) {
                ApplicationRef instanceAppRef = server.getApplicationRef(name);
                // if the server in the dg contains the ApplicationRef
                if (instanceAppRef != null) {
                    applicationRefsToChange.add(instanceAppRef);
                }
            }
        }
    }
    // Apply the configuration to the listed ApplicationRefs
    try {
        ConfigSupport.apply(new ConfigCode() {

            @Override
            public Object run(ConfigBeanProxy... params) throws PropertyVetoException, TransactionFailure {
                for (ConfigBeanProxy proxy : params) {
                    if (proxy instanceof ApplicationRef) {
                        ApplicationRef applicationRefProxy = (ApplicationRef) proxy;
                        if (enabled != null) {
                            applicationRefProxy.setEnabled(enabled.toString());
                        }
                        if (virtualservers != null) {
                            applicationRefProxy.setVirtualServers(virtualservers);
                        }
                        if (lbenabled != null) {
                            applicationRefProxy.setLbEnabled(lbenabled.toString());
                        }
                    }
                }
                report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
                return true;
            }
        }, applicationRefsToChange.toArray(new ApplicationRef[] {}));
    } catch (TransactionFailure ex) {
        report.failure(logger, ex.getLocalizedMessage());
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Server(com.sun.enterprise.config.serverbeans.Server) ArrayList(java.util.ArrayList) Cluster(com.sun.enterprise.config.serverbeans.Cluster) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) ApplicationRef(com.sun.enterprise.config.serverbeans.ApplicationRef) PropertyVetoException(java.beans.PropertyVetoException) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) ConfigCode(org.jvnet.hk2.config.ConfigCode) DeploymentGroup(fish.payara.enterprise.config.serverbeans.DeploymentGroup)

Aggregations

ConfigBeanProxy (org.jvnet.hk2.config.ConfigBeanProxy)10 ConfigCode (org.jvnet.hk2.config.ConfigCode)10 PropertyVetoException (java.beans.PropertyVetoException)9 ActionReport (org.glassfish.api.ActionReport)5 Protocol (org.glassfish.grizzly.config.dom.Protocol)5 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)5 Config (com.sun.enterprise.config.serverbeans.Config)4 Server (com.sun.enterprise.config.serverbeans.Server)4 ArrayList (java.util.ArrayList)4 NetworkListeners (org.glassfish.grizzly.config.dom.NetworkListeners)4 Application (com.sun.enterprise.config.serverbeans.Application)3 ApplicationRef (com.sun.enterprise.config.serverbeans.ApplicationRef)3 CommandTarget (org.glassfish.config.support.CommandTarget)3 NetworkListener (org.glassfish.grizzly.config.dom.NetworkListener)3 Protocols (org.glassfish.grizzly.config.dom.Protocols)3 Target (org.glassfish.internal.api.Target)3 Cluster (com.sun.enterprise.config.serverbeans.Cluster)2 SystemApplications (com.sun.enterprise.config.serverbeans.SystemApplications)2 DeploymentGroup (fish.payara.enterprise.config.serverbeans.DeploymentGroup)2 Logger (java.util.logging.Logger)2