Search in sources :

Example 11 with Configured

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

the class ListFileUser 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("list.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();
    }
    if (keyFile == null) {
        report.setMessage(localStrings.getLocalString("list.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(keyFile)).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[] { keyFile, authRealmName }));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    // We have the right impl so let's try to remove one
    FileRealm fr = null;
    try {
        realmsManager.createRealms(config);
        // account for updates to realms from outside this config sharing
        // same keyfile
        CreateFileUser.refreshRealm(config.getName(), authRealmName);
        fr = (FileRealm) realmsManager.getFromLoadedRealms(config.getName(), authRealmName);
        if (fr == null) {
            throw new NoSuchRealmException(authRealmName);
        }
    } catch (NoSuchRealmException e) {
        report.setMessage(localStrings.getLocalString("list.file.user.realmnotsupported", "Configured file realm {0} is not supported.", authRealmName) + "  " + e.getLocalizedMessage());
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(e);
        return;
    }
    try {
        Enumeration users = fr.getUserNames();
        List userList = new ArrayList();
        while (users.hasMoreElements()) {
            final ActionReport.MessagePart part = report.getTopMessagePart().addChild();
            String userName = (String) users.nextElement();
            part.setMessage(userName);
            Map userMap = new HashMap();
            userMap.put("name", userName);
            try {
                userMap.put("groups", Collections.list(fr.getGroupNames(userName)));
            } catch (NoSuchUserException ex) {
            // This should never be thrown since we just got the user name from the realm
            }
            userList.add(userMap);
        }
        Properties extraProperties = new Properties();
        extraProperties.put("users", userList);
        report.setExtraProperties(extraProperties);
        report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
    } catch (BadRealmException e) {
        report.setMessage(localStrings.getLocalString("list.file.user.realmcorrupted", "Configured file realm {0} is corrupted.", authRealmName) + "  " + e.getLocalizedMessage());
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(e);
    }
}
Also used : Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) NoSuchUserException(com.sun.enterprise.security.auth.realm.NoSuchUserException) ArrayList(java.util.ArrayList) ActionReport(org.glassfish.api.ActionReport) FileRealm(com.sun.enterprise.security.auth.realm.file.FileRealm) Properties(java.util.Properties) NoSuchRealmException(com.sun.enterprise.security.auth.realm.NoSuchRealmException) BadRealmException(com.sun.enterprise.security.auth.realm.BadRealmException) ArrayList(java.util.ArrayList) List(java.util.List) Property(org.jvnet.hk2.config.types.Property) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 12 with Configured

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

the class RealmConfig method createRealms.

public static void createRealms(String defaultRealm, List<AuthRealm> realms, String configName) {
    // need at least one good realm
    String goodRealm = null;
    for (AuthRealm realm : realms) {
        String realmName = realm.getName();
        String realmClass = realm.getClassname();
        try {
            List<Property> realmProperties = realm.getProperty();
            Properties properties = new Properties();
            for (Property realmProperty : realmProperties) {
                properties.setProperty(realmProperty.getName(), realmProperty.getValue());
            }
            Realm.instantiate(realmName, realmClass, properties, configName);
            if (LOGGER.isLoggable(FINE)) {
                LOGGER.log(FINE, "Configured realm: {0}", realmName);
            }
            if (goodRealm == null) {
                goodRealm = realmName;
            }
        } catch (Exception e) {
            LOGGER.log(WARNING, realmConfigDisabledError, realmName);
            LOGGER.log(WARNING, securityExceptionError, e);
        }
    }
    if (goodRealm == null) {
        LOGGER.severe(noRealmsError);
    } else {
        try {
            Realm.getInstance(defaultRealm);
        } catch (Exception e) {
            defaultRealm = goodRealm;
        }
        Realm.setDefaultRealm(defaultRealm);
        if (LOGGER.isLoggable(FINE)) {
            LOGGER.log(FINE, "Default realm is set to: {0}", defaultRealm);
        }
    }
}
Also used : AuthRealm(com.sun.enterprise.config.serverbeans.AuthRealm) Properties(java.util.Properties) Property(org.jvnet.hk2.config.types.Property)

Example 13 with Configured

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

the class ManagedExecutorServiceBaseManager method delete.

public ResourceStatus delete(final Resources resources, final String jndiName, final String target) throws Exception {
    if (jndiName == null) {
        String msg = localStrings.getLocalString("managed.executor.service.noJndiName", "No JNDI name defined for managed executor service.");
        if (getResourceType().equals(ServerTags.MANAGED_SCHEDULED_EXECUTOR_SERVICE)) {
            msg = localStrings.getLocalString("managed.scheduled.executor.service.noJndiName", "No JNDI name defined for managed scheduled executor service.");
        }
        return new ResourceStatus(ResourceStatus.FAILURE, msg);
    }
    Resource resource = null;
    if (getResourceType().equals(ServerTags.MANAGED_EXECUTOR_SERVICE)) {
        resource = ConnectorsUtil.getResourceByName(resources, ManagedExecutorService.class, jndiName);
    } else if (getResourceType().equals(ServerTags.MANAGED_SCHEDULED_EXECUTOR_SERVICE)) {
        resource = ConnectorsUtil.getResourceByName(resources, ManagedScheduledExecutorService.class, jndiName);
    }
    // ensure we already have this resource
    if (resource == null) {
        String msg = localStrings.getLocalString("delete.managed.executor.service.notfound", "A managed executor service named {0} does not exist.", jndiName);
        if (getResourceType().equals(ServerTags.MANAGED_SCHEDULED_EXECUTOR_SERVICE)) {
            msg = localStrings.getLocalString("delete.managed.scheduled.executor.service.notfound", "A managed scheduled executor 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.managed.executor.service.resource-ref.exist", "This managed executor service [ {0} ] is referenced in an instance/cluster target, use delete-resource-ref on appropriate target", jndiName);
                if (getResourceType().equals(ServerTags.MANAGED_SCHEDULED_EXECUTOR_SERVICE)) {
                    msg = localStrings.getLocalString("delete.managed.scheduled.executor.service.resource-ref.exist", "This managed scheduled executor 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.managed.executor.service.no.resource-ref", "This managed executor service [ {0} ] is not referenced in target [ {1} ]", jndiName, target);
                if (getResourceType().equals(ServerTags.MANAGED_SCHEDULED_EXECUTOR_SERVICE)) {
                    msg = localStrings.getLocalString("delete.managed.scheduled.executor.service.no.resource-ref", "This managed scheduled executor 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.managed.executor.service.multiple.resource-refs", "This managed executor service [ {0} ] is referenced in multiple instance/cluster targets, Use delete-resource-ref on appropriate target", jndiName);
                if (getResourceType().equals(ServerTags.MANAGED_SCHEDULED_EXECUTOR_SERVICE)) {
                    msg = localStrings.getLocalString("delete.managed.scheduled.executor.service.multiple.resource-refs", "This managed scheduled executor 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 managed executor service
        if (ConfigSupport.apply(new SingleConfigCode<Resources>() {

            public Object run(Resources param) throws PropertyVetoException, TransactionFailure {
                ManagedExecutorServiceBase resource = null;
                if (getResourceType().equals(ServerTags.MANAGED_EXECUTOR_SERVICE)) {
                    resource = (ManagedExecutorService) ConnectorsUtil.getResourceByName(resources, ManagedExecutorService.class, jndiName);
                } else {
                    resource = (ManagedScheduledExecutorService) ConnectorsUtil.getResourceByName(resources, ManagedScheduledExecutorService.class, jndiName);
                }
                return param.getResources().remove(resource);
            }
        }, resources) == null) {
            String msg = localStrings.getLocalString("delete.managed.executor.service.failed", "Managed executor service {0} deletion failed", jndiName);
            if (getResourceType().equals(ServerTags.MANAGED_SCHEDULED_EXECUTOR_SERVICE)) {
                msg = localStrings.getLocalString("delete.managed.scheduled.executor.service.failed", "Managed scheduled executor service {0} deletion failed", jndiName);
            }
            return new ResourceStatus(ResourceStatus.FAILURE, msg);
        }
    } catch (TransactionFailure tfe) {
        String msg = localStrings.getLocalString("delete.managed.executor.service.failed", "Managed executor service {0} deletion failed ", jndiName);
        if (getResourceType().equals(ServerTags.MANAGED_SCHEDULED_EXECUTOR_SERVICE)) {
            msg = localStrings.getLocalString("delete.managed.scheduled.executor.service.failed", "Managed scheduled executor service {0} deletion failed ", jndiName);
        }
        ResourceStatus status = new ResourceStatus(ResourceStatus.FAILURE, msg);
        status.setException(tfe);
        return status;
    }
    String msg = localStrings.getLocalString("delete.managed.executor.service.success", "Managed executor service {0} deleted successfully", jndiName);
    if (getResourceType().equals(ServerTags.MANAGED_SCHEDULED_EXECUTOR_SERVICE)) {
        msg = localStrings.getLocalString("delete.managed.scheduled.executor.service.success", "Managed scheduled executor service {0} deleted successfully", jndiName);
    }
    return new ResourceStatus(ResourceStatus.SUCCESS, msg);
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ManagedExecutorService(org.glassfish.concurrent.config.ManagedExecutorService) SingleConfigCode(org.jvnet.hk2.config.SingleConfigCode) Resource(com.sun.enterprise.config.serverbeans.Resource) ResourceStatus(org.glassfish.resourcebase.resources.api.ResourceStatus) ManagedExecutorServiceBase(org.glassfish.concurrent.config.ManagedExecutorServiceBase) Resources(com.sun.enterprise.config.serverbeans.Resources)

Example 14 with Configured

use of org.jvnet.hk2.config.Configured 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 15 with Configured

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

the class IiopServiceSslConfigHandler method create.

@Override
public void create(final CreateSsl command, ActionReport report) {
    IiopService iiopSvc = command.config.getExtensionByType(IiopService.class);
    if (iiopSvc.getSslClientConfig() != null) {
        report.setMessage(localStrings.getLocalString("create.ssl.iiopsvc.alreadyExists", "IIOP Service " + "already has been configured with SSL configuration."));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    try {
        ConfigSupport.apply(new SingleConfigCode<IiopService>() {

            public Object run(IiopService param) throws PropertyVetoException, TransactionFailure {
                SslClientConfig newSslClientCfg = param.createChild(SslClientConfig.class);
                Ssl newSsl = newSslClientCfg.createChild(Ssl.class);
                command.populateSslElement(newSsl);
                newSslClientCfg.setSsl(newSsl);
                param.setSslClientConfig(newSslClientCfg);
                return newSsl;
            }
        }, iiopSvc);
    } catch (TransactionFailure e) {
        command.reportError(report, e);
    }
    command.reportSuccess(report);
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) SslClientConfig(com.sun.enterprise.config.serverbeans.SslClientConfig) IiopService(org.glassfish.orb.admin.config.IiopService) CreateSsl(com.sun.enterprise.admin.commands.CreateSsl) DeleteSsl(com.sun.enterprise.admin.commands.DeleteSsl) Ssl(org.glassfish.grizzly.config.dom.Ssl)

Aggregations

Property (org.jvnet.hk2.config.types.Property)12 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)10 ActionReport (org.glassfish.api.ActionReport)8 PropertyVetoException (java.beans.PropertyVetoException)7 FileRealm (com.sun.enterprise.security.auth.realm.file.FileRealm)6 Resources (com.sun.enterprise.config.serverbeans.Resources)5 NoSuchRealmException (com.sun.enterprise.security.auth.realm.NoSuchRealmException)5 SingleConfigCode (org.jvnet.hk2.config.SingleConfigCode)5 Resource (com.sun.enterprise.config.serverbeans.Resource)4 File (java.io.File)4 ResourceStatus (org.glassfish.resourcebase.resources.api.ResourceStatus)4 BadRealmException (com.sun.enterprise.security.auth.realm.BadRealmException)3 List (java.util.List)3 Properties (java.util.Properties)3 SecurityService (com.sun.enterprise.config.serverbeans.SecurityService)2 JmsService (com.sun.enterprise.connectors.jms.config.JmsService)2 ArrayList (java.util.ArrayList)2 Enumeration (java.util.Enumeration)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2