Search in sources :

Example 26 with TransactionFailure

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

the class CreateConnectorSecurityMap 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 (securityMapName == null) {
        report.setMessage(localStrings.getLocalString("create.connector.security.map.noSecurityMapName", "No security map name specified"));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    if (principals == null && userGroups == null) {
        report.setMessage(localStrings.getLocalString("create.connector.security.map.noPrincipalsOrGroupsMap", "Either the principal or the user group has to be specified while creating a security map." + " Both cannot be null."));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    if (principals != null && userGroups != null) {
        report.setMessage(localStrings.getLocalString("create.connector.security.map.specifyPrincipalsOrGroupsMap", "A work-security-map can have either (any number of) group mapping or (any number of) principals" + " mapping but not both. Specify --principals or --usergroups."));
        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("create.connector.security.map.duplicate", "A security map named {0} already exists for connector connection pool {1}. Please give a" + " different map name.", securityMapName, poolName));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    // get all the security maps for this pool.....
    List<SecurityMap> maps = getAllSecurityMapsForPool(poolName, ccPools);
    if (principals != null) {
        for (String principal : principals) {
            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;
            }
        }
    }
    if (userGroups != null) {
        for (String userGroup : userGroups) {
            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;
            }
        }
    }
    ConnectorConnectionPool connPool = null;
    for (ConnectorConnectionPool ccp : ccPools) {
        if (ccp.getName().equals(poolName)) {
            connPool = ccp;
        }
    }
    try {
        ConfigSupport.apply(new SingleConfigCode<ConnectorConnectionPool>() {

            public Object run(ConnectorConnectionPool ccp) throws PropertyVetoException, TransactionFailure {
                List<SecurityMap> securityMaps = ccp.getSecurityMap();
                SecurityMap newResource = ccp.createChild(SecurityMap.class);
                newResource.setName(securityMapName);
                if (principals != null) {
                    for (String p : principals) {
                        newResource.getPrincipal().add(p);
                    }
                }
                if (userGroups != null) {
                    for (String u : userGroups) {
                        newResource.getUserGroup().add(u);
                    }
                }
                BackendPrincipal backendPrincipal = newResource.createChild(BackendPrincipal.class);
                backendPrincipal.setUserName(mappedusername);
                if (mappedpassword != null && !mappedpassword.isEmpty()) {
                    backendPrincipal.setPassword(mappedpassword);
                }
                newResource.setBackendPrincipal(backendPrincipal);
                securityMaps.add(newResource);
                return newResource;
            }
        }, connPool);
    } catch (TransactionFailure tfe) {
        Object[] params = { securityMapName, poolName };
        report.setMessage(localStrings.getLocalString("create.connector.security.map.fail", "Unable to create connector security map {0} for connector connection pool {1} ", params) + " " + tfe.getLocalizedMessage());
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(tfe);
        return;
    }
    report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ConnectorConnectionPool(org.glassfish.connectors.config.ConnectorConnectionPool) SecurityMap(org.glassfish.connectors.config.SecurityMap) BackendPrincipal(org.glassfish.connectors.config.BackendPrincipal) List(java.util.List) ActionReport(org.glassfish.api.ActionReport)

Example 27 with TransactionFailure

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

the class ConnectorConnectionPoolManager method create.

public ResourceStatus create(Resources resources, HashMap attributes, final Properties properties, String target) throws Exception {
    setParams(attributes);
    ResourceStatus validationStatus = isValid(resources, true);
    if (validationStatus.getStatus() == ResourceStatus.FAILURE) {
        return validationStatus;
    }
    try {
        ConfigSupport.apply(new SingleConfigCode<Resources>() {

            public Object run(Resources param) throws PropertyVetoException, TransactionFailure {
                return createResource(param, properties);
            }
        }, resources);
    } catch (TransactionFailure tfe) {
        Logger.getLogger(ConnectorConnectionPoolManager.class.getName()).log(Level.SEVERE, "create-connector-connection-pool failed", tfe);
        String msg = localStrings.getLocalString("create.connector.connection.pool.fail", "Connector connection pool {0} create failed: {1}", poolname) + " " + tfe.getLocalizedMessage();
        return new ResourceStatus(ResourceStatus.FAILURE, msg);
    }
    String msg = localStrings.getLocalString("create.connector.connection.pool.success", "Connector connection pool {0} created successfully", poolname);
    return new ResourceStatus(ResourceStatus.SUCCESS, msg);
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ResourceStatus(org.glassfish.resourcebase.resources.api.ResourceStatus)

Example 28 with TransactionFailure

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

the class ConnectorConnectionPoolManager method createConfigBean.

private ConnectorConnectionPool createConfigBean(Resources param, Properties properties) throws PropertyVetoException, TransactionFailure {
    ConnectorConnectionPool newResource = param.createChild(ConnectorConnectionPool.class);
    newResource.setResourceAdapterName(raname);
    newResource.setConnectionDefinitionName(connectiondefinition);
    if (validateAtmostOncePeriod != null) {
        newResource.setValidateAtmostOncePeriodInSeconds(validateAtmostOncePeriod);
    }
    newResource.setSteadyPoolSize(steadypoolsize);
    newResource.setPoolResizeQuantity(poolresize);
    newResource.setMaxWaitTimeInMillis(maxwait);
    newResource.setMaxPoolSize(maxpoolsize);
    if (maxConnectionUsageCount != null) {
        newResource.setMaxConnectionUsageCount(maxConnectionUsageCount);
    }
    newResource.setMatchConnections(matchConnections);
    if (lazyConnectionEnlistment != null) {
        newResource.setLazyConnectionEnlistment(lazyConnectionEnlistment);
    }
    if (lazyConnectionAssociation != null) {
        newResource.setLazyConnectionAssociation(lazyConnectionAssociation);
    }
    if (isconnectvalidatereq != null) {
        newResource.setIsConnectionValidationRequired(isconnectvalidatereq);
    }
    newResource.setIdleTimeoutInSeconds(idletimeout);
    newResource.setFailAllConnections(failconnection);
    if (connectionLeakTimeout != null) {
        newResource.setConnectionLeakTimeoutInSeconds(connectionLeakTimeout);
    }
    if (connectionLeakReclaim != null) {
        newResource.setConnectionLeakReclaim(connectionLeakReclaim);
    }
    if (connectionCreationRetryInterval != null) {
        newResource.setConnectionCreationRetryIntervalInSeconds(connectionCreationRetryInterval);
    }
    if (connectionCreationRetryAttempts != null) {
        newResource.setConnectionCreationRetryAttempts(connectionCreationRetryAttempts);
    }
    if (associateWithThread != null) {
        newResource.setAssociateWithThread(associateWithThread);
    }
    if (pooling != null) {
        newResource.setPooling(pooling);
    }
    if (ping != null) {
        newResource.setPing(ping);
    }
    if (transactionSupport != null) {
        newResource.setTransactionSupport(transactionSupport);
    }
    if (description != null) {
        newResource.setDescription(description);
    }
    newResource.setName(poolname);
    if (properties != null) {
        for (java.util.Map.Entry e : properties.entrySet()) {
            Property prop = newResource.createChild(Property.class);
            prop.setName((String) e.getKey());
            prop.setValue((String) e.getValue());
            newResource.getProperty().add(prop);
        }
    }
    return newResource;
}
Also used : ConnectorConnectionPool(org.glassfish.connectors.config.ConnectorConnectionPool) HashMap(java.util.HashMap) Property(org.jvnet.hk2.config.types.Property)

Example 29 with TransactionFailure

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

the class ContextServiceManager method delete.

public ResourceStatus delete(final Resources resources, final String jndiName, final String target) throws Exception {
    if (jndiName == null) {
        String msg = localStrings.getLocalString("context.service.noJndiName", "No JNDI name defined for context service.");
        return new ResourceStatus(ResourceStatus.FAILURE, msg);
    }
    Resource resource = ConnectorsUtil.getResourceByName(resources, ContextService.class, jndiName);
    // ensure we already have this resource
    if (resource == null) {
        String msg = localStrings.getLocalString("delete.context.service.notfound", "A context service named {0} does not exist.", jndiName);
        return new ResourceStatus(ResourceStatus.FAILURE, msg);
    }
    if (SYSTEM_ALL_REQ.equals(resource.getObjectType())) {
        String msg = localStrings.getLocalString("delete.concurrent.resource.notAllowed", "The {0} resource cannot be deleted as it is required to be configured in the system.", jndiName);
        return new ResourceStatus(ResourceStatus.FAILURE, msg);
    }
    if (environment.isDas()) {
        if ("domain".equals(target)) {
            if (resourceUtil.getTargetsReferringResourceRef(jndiName).size() > 0) {
                String msg = localStrings.getLocalString("delete.context.service.resource-ref.exist", "This context service [ {0} ] is referenced in an instance/cluster target, use delete-resource-ref on appropriate target", jndiName);
                return new ResourceStatus(ResourceStatus.FAILURE, msg);
            }
        } else {
            if (!resourceUtil.isResourceRefInTarget(jndiName, target)) {
                String msg = localStrings.getLocalString("delete.context.service.no.resource-ref", "This context service [ {0} ] is not referenced in target [ {1} ]", jndiName, target);
                return new ResourceStatus(ResourceStatus.FAILURE, msg);
            }
            if (resourceUtil.getTargetsReferringResourceRef(jndiName).size() > 1) {
                String msg = localStrings.getLocalString("delete.context.service.multiple.resource-refs", "This context service [ {0} ] is referenced in multiple instance/cluster targets, Use delete-resource-ref on appropriate target", jndiName);
                return new ResourceStatus(ResourceStatus.FAILURE, msg);
            }
        }
    }
    try {
        // delete resource-ref
        resourceUtil.deleteResourceRef(jndiName, target);
        // delete context-service
        if (ConfigSupport.apply(new SingleConfigCode<Resources>() {

            public Object run(Resources param) throws PropertyVetoException, TransactionFailure {
                ContextService resource = (ContextService) ConnectorsUtil.getResourceByName(resources, ContextService.class, jndiName);
                return param.getResources().remove(resource);
            }
        }, resources) == null) {
            String msg = localStrings.getLocalString("delete.context.service.failed", "Context service {0} deletion failed", jndiName);
            return new ResourceStatus(ResourceStatus.FAILURE, msg);
        }
    } catch (TransactionFailure tfe) {
        String msg = localStrings.getLocalString("delete.context.service.failed", "Context service {0} deletion failed ", jndiName);
        ResourceStatus status = new ResourceStatus(ResourceStatus.FAILURE, msg);
        status.setException(tfe);
        return status;
    }
    String msg = localStrings.getLocalString("delete.context.service.success", "Context service {0} deleted successfully", jndiName);
    return new ResourceStatus(ResourceStatus.SUCCESS, msg);
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ContextService(org.glassfish.concurrent.config.ContextService) SingleConfigCode(org.jvnet.hk2.config.SingleConfigCode) Resource(com.sun.enterprise.config.serverbeans.Resource) ResourceStatus(org.glassfish.resourcebase.resources.api.ResourceStatus) Resources(com.sun.enterprise.config.serverbeans.Resources)

Example 30 with TransactionFailure

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

the class SetBatchRuntimeConfiguration method execute.

@Override
public void execute(final AdminCommandContext context) {
    final ActionReport actionReport = context.getActionReport();
    Properties extraProperties = actionReport.getExtraProperties();
    if (extraProperties == null) {
        extraProperties = new Properties();
        actionReport.setExtraProperties(extraProperties);
    }
    if (dataSourceLookupName == null && executorServiceLookupName == null) {
        actionReport.setMessage("Either dataSourceLookupName or executorServiceLookupName must be specified.");
        actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    try {
        Config config = targetUtil.getConfig(target);
        BatchRuntimeConfiguration batchRuntimeConfiguration = config.getExtensionByType(BatchRuntimeConfiguration.class);
        if (batchRuntimeConfiguration != null) {
            ConfigSupport.apply(new SingleConfigCode<BatchRuntimeConfiguration>() {

                @Override
                public Object run(final BatchRuntimeConfiguration batchRuntimeConfigurationProxy) throws PropertyVetoException, TransactionFailure {
                    boolean encounteredError = false;
                    int tableprefixlength = 0;
                    int tablesuffixlength = 0;
                    if (dataSourceLookupName != null) {
                        try {
                            validateDataSourceLookupName(context, target, dataSourceLookupName);
                            batchRuntimeConfigurationProxy.setDataSourceLookupName(dataSourceLookupName);
                            actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
                        } catch (GlassFishBatchValidationException ex) {
                            logger.log(Level.WARNING, ex.getMessage());
                            actionReport.setMessage(dataSourceLookupName + " is not mapped to a DataSource");
                            actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
                            throw new GlassFishBatchValidationException(dataSourceLookupName + " is not mapped to a DataSource");
                        }
                    }
                    if (executorServiceLookupName != null && !encounteredError) {
                        try {
                            validateExecutorServiceLookupName(context, target, executorServiceLookupName);
                            batchRuntimeConfigurationProxy.setExecutorServiceLookupName(executorServiceLookupName);
                            actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
                        } catch (GlassFishBatchValidationException ex) {
                            logger.log(Level.WARNING, ex.getMessage());
                            actionReport.setMessage("No executor service bound to name = " + executorServiceLookupName);
                            actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
                            throw new GlassFishBatchValidationException("No executor service bound to name = " + executorServiceLookupName);
                        }
                    }
                    if (schemaName != null && !encounteredError) {
                        batchRuntimeConfigurationProxy.setSchemaName(schemaName);
                        actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
                    }
                    if (tablePrefix != null && !encounteredError) {
                        tableprefixlength = tablePrefix.length();
                        batchRuntimeConfigurationProxy.setTablePrefix(tablePrefix);
                        actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
                    }
                    if (tableSuffix != null && !encounteredError) {
                        tablesuffixlength = tableSuffix.length();
                        batchRuntimeConfigurationProxy.setTableSuffix(tableSuffix);
                        actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
                    }
                    if (targetUtil.isThisDAS() && isOracle(dataSourceLookupName)) {
                        if (tablesuffixlength + tableprefixlength + MAX_TABLE_LENGTH > 30) {
                            actionReport.setMessage("The table name cannot be greater than 30 characters in Oracle, please amend the table prefix or suffix size ");
                            actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
                            throw new GlassFishBatchValidationException("The table name cannot be greater than 30 characters in Oracle, please amend the table prefix or suffix size ");
                        }
                    }
                    return null;
                }
            }, batchRuntimeConfiguration);
        }
    } catch (TransactionFailure txfEx) {
        logger.log(Level.WARNING, "Exception during command ", txfEx);
        actionReport.setMessage(txfEx.getCause().getMessage());
        actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Config(com.sun.enterprise.config.serverbeans.Config) GlassFishBatchValidationException(org.glassfish.batch.spi.impl.GlassFishBatchValidationException) BatchRuntimeConfiguration(org.glassfish.batch.spi.impl.BatchRuntimeConfiguration) ActionReport(org.glassfish.api.ActionReport)

Aggregations

TransactionFailure (org.jvnet.hk2.config.TransactionFailure)191 PropertyVetoException (java.beans.PropertyVetoException)132 ActionReport (org.glassfish.api.ActionReport)86 Property (org.jvnet.hk2.config.types.Property)61 Config (com.sun.enterprise.config.serverbeans.Config)52 HashMap (java.util.HashMap)30 Test (org.junit.Test)27 Resources (com.sun.enterprise.config.serverbeans.Resources)25 Map (java.util.Map)25 List (java.util.List)21 CommandTarget (org.glassfish.config.support.CommandTarget)21 NetworkListener (org.glassfish.grizzly.config.dom.NetworkListener)21 Target (org.glassfish.internal.api.Target)21 NetworkConfig (org.glassfish.grizzly.config.dom.NetworkConfig)20 Protocol (org.glassfish.grizzly.config.dom.Protocol)20 ConfigBean (org.jvnet.hk2.config.ConfigBean)20 ConfigBeanProxy (org.jvnet.hk2.config.ConfigBeanProxy)18 ResourceStatus (org.glassfish.resourcebase.resources.api.ResourceStatus)17 SingleConfigCode (org.jvnet.hk2.config.SingleConfigCode)16 ParameterMap (org.glassfish.api.admin.ParameterMap)14