Search in sources :

Example 6 with ResourceRef

use of com.sun.enterprise.config.serverbeans.ResourceRef 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 ResourceRef

use of com.sun.enterprise.config.serverbeans.ResourceRef in project Payara by payara.

the class CreateJndiResourceTest method testExecuteSuccess.

/**
 * Test of execute method, of class CreateJndiResource.
 * asadmin create-jndi-resource --restype=queue --factoryclass=sampleClass --jndilookupname=sample_jndi
 * sample_jndi_resource
 */
@Test
public void testExecuteSuccess() {
    parameters.set("jndilookupname", "sample_jndi");
    parameters.set("restype", "queue");
    parameters.set("factoryclass", "sampleClass");
    parameters.set("jndi_name", "sample_jndi_resource");
    org.glassfish.resources.admin.cli.CreateJndiResource command = habitat.getService(org.glassfish.resources.admin.cli.CreateJndiResource.class);
    cr.getCommandInvocation("create-jndi-resource", context.getActionReport(), adminSubject()).parameters(parameters).execute(command);
    assertEquals(ActionReport.ExitCode.SUCCESS, context.getActionReport().getActionExitCode());
    boolean isCreated = false;
    for (Resource resource : resources.getResources()) {
        if (resource instanceof ExternalJndiResource) {
            ExternalJndiResource r = (ExternalJndiResource) resource;
            if (r.getJndiName().equals("sample_jndi_resource")) {
                assertEquals("queue", r.getResType());
                assertEquals("sample_jndi", r.getJndiLookupName());
                assertEquals("sampleClass", r.getFactoryClass());
                assertEquals("true", r.getEnabled());
                isCreated = true;
                logger.fine("Jndi Resource config bean sample_jndi_resource is created.");
                break;
            }
        }
    }
    assertTrue(isCreated);
    logger.fine("msg: " + context.getActionReport().getMessage());
    Servers servers = habitat.getService(Servers.class);
    boolean isRefCreated = false;
    for (Server server : servers.getServer()) {
        if (server.getName().equals(SystemPropertyConstants.DEFAULT_SERVER_INSTANCE_NAME)) {
            for (ResourceRef ref : server.getResourceRef()) {
                if (ref.getRef().equals("sample_jndi_resource")) {
                    assertEquals("true", ref.getEnabled());
                    isRefCreated = true;
                    break;
                }
            }
        }
    }
    assertTrue(isRefCreated);
}
Also used : BindableResource(com.sun.enterprise.config.serverbeans.BindableResource) ExternalJndiResource(org.glassfish.resources.config.ExternalJndiResource) Resource(com.sun.enterprise.config.serverbeans.Resource) ResourceRef(com.sun.enterprise.config.serverbeans.ResourceRef) ExternalJndiResource(org.glassfish.resources.config.ExternalJndiResource) Test(org.junit.Test) ConfigApiTest(org.glassfish.tests.utils.ConfigApiTest)

Example 8 with ResourceRef

use of com.sun.enterprise.config.serverbeans.ResourceRef in project Payara by payara.

the class AddInstanceToDeploymentGroupCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    Server server = domain.getServerNamed(instanceName);
    ActionReport report = context.getActionReport();
    if (server == null && env.isDas()) {
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage("Instance " + instanceName + " does not exist");
        return;
    }
    DeploymentGroup dg = domain.getDeploymentGroupNamed(deploymentGroup);
    if (dg == null && env.isDas()) {
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage("Deployment Group " + deploymentGroup + " does not exist");
        return;
    }
    // OK set up the reference
    try {
        ConfigSupport.apply((DeploymentGroup dg1) -> {
            DGServerRef ref = dg1.createChild(DGServerRef.class);
            ref.setRef(instanceName);
            dg1.getDGServerRef().add(ref);
            return ref;
        }, dg);
    } catch (TransactionFailure e) {
        report.setMessage("Failed to add instance to deployment group");
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(e);
    }
    // now run the command to add application ref to the instance
    for (ApplicationRef applicationRef : dg.getApplicationRef()) {
        CommandInvocation inv = commandRunner.getCommandInvocation("create-application-ref", report, context.getSubject());
        ParameterMap parameters = new ParameterMap();
        parameters.add("target", instanceName);
        parameters.add("name", applicationRef.getRef());
        parameters.add("virtualservers", applicationRef.getVirtualServers());
        parameters.add("enabled", applicationRef.getEnabled());
        parameters.add("lbenabled", applicationRef.getLbEnabled());
        inv.parameters(parameters).execute();
    }
    // for all resource refs add resource ref to instance
    for (ResourceRef resourceRef : dg.getResourceRef()) {
        CommandInvocation inv = commandRunner.getCommandInvocation("create-resource-ref", report, context.getSubject());
        ParameterMap parameters = new ParameterMap();
        parameters.add("target", instanceName);
        parameters.add("reference_name", resourceRef.getRef());
        parameters.add("enabled", resourceRef.getEnabled());
        inv.parameters(parameters).execute();
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Server(com.sun.enterprise.config.serverbeans.Server) DGServerRef(fish.payara.enterprise.config.serverbeans.DGServerRef) ParameterMap(org.glassfish.api.admin.ParameterMap) ResourceRef(com.sun.enterprise.config.serverbeans.ResourceRef) ActionReport(org.glassfish.api.ActionReport) ApplicationRef(com.sun.enterprise.config.serverbeans.ApplicationRef) DeploymentGroup(fish.payara.enterprise.config.serverbeans.DeploymentGroup) CommandInvocation(org.glassfish.api.admin.CommandRunner.CommandInvocation)

Example 9 with ResourceRef

use of com.sun.enterprise.config.serverbeans.ResourceRef in project Payara by payara.

the class RemoveInstanceFromDeploymentGroupCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    Server server = domain.getServerNamed(instanceName);
    ActionReport report = context.getActionReport();
    if (server == null) {
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage("Instance " + instanceName + " does not exist");
        return;
    }
    DeploymentGroup dg = domain.getDeploymentGroupNamed(deploymentGroup);
    if (dg == null) {
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage("Deployment Group " + deploymentGroup + " does not exist");
        return;
    }
    DGServerRef ref = dg.getDGServerRefByRef(instanceName);
    if (ref == null) {
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage("Deployment Group " + deploymentGroup + " does not contain server " + instanceName);
        return;
    }
    // OK set up the reference
    try {
        ConfigSupport.apply((DeploymentGroup dg1) -> {
            dg1.getDGServerRef().remove(ref);
            return null;
        }, dg);
    } catch (TransactionFailure e) {
        report.setMessage("Failed to remove instance from the deployment group");
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(e);
    }
    // now run the command to remove application ref to the instance
    for (ApplicationRef applicationRef : dg.getApplicationRef()) {
        CommandRunner.CommandInvocation inv = commandRunner.getCommandInvocation("delete-application-ref", report, context.getSubject());
        ParameterMap parameters = new ParameterMap();
        parameters.add("target", instanceName);
        parameters.add("name", applicationRef.getRef());
        inv.parameters(parameters).execute();
    }
    // now run the command to remove resource ref to the instance
    for (ResourceRef resourceRef : dg.getResourceRef()) {
        CommandRunner.CommandInvocation inv = commandRunner.getCommandInvocation("delete-resource-ref", report, context.getSubject());
        ParameterMap parameters = new ParameterMap();
        parameters.add("target", instanceName);
        parameters.add("reference_name", resourceRef.getRef());
        inv.parameters(parameters).execute();
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Server(com.sun.enterprise.config.serverbeans.Server) DGServerRef(fish.payara.enterprise.config.serverbeans.DGServerRef) ParameterMap(org.glassfish.api.admin.ParameterMap) ResourceRef(com.sun.enterprise.config.serverbeans.ResourceRef) ActionReport(org.glassfish.api.ActionReport) ApplicationRef(com.sun.enterprise.config.serverbeans.ApplicationRef) CommandRunner(org.glassfish.api.admin.CommandRunner) DeploymentGroup(fish.payara.enterprise.config.serverbeans.DeploymentGroup)

Example 10 with ResourceRef

use of com.sun.enterprise.config.serverbeans.ResourceRef in project Payara by payara.

the class ListResourceRefs method processResourceRefs.

private void processResourceRefs(ActionReport report, List<ResourceRef> resourceRefs) {
    if (resourceRefs.isEmpty()) {
        final ActionReport.MessagePart part = report.getTopMessagePart().addChild();
        part.setMessage(localStrings.getLocalString("NothingToList", "Nothing to List."));
    } else {
        for (ResourceRef ref : resourceRefs) {
            final ActionReport.MessagePart part = report.getTopMessagePart().addChild();
            part.setMessage(ref.getRef());
        }
    }
}
Also used : ResourceRef(com.sun.enterprise.config.serverbeans.ResourceRef) ActionReport(org.glassfish.api.ActionReport)

Aggregations

ResourceRef (com.sun.enterprise.config.serverbeans.ResourceRef)10 Resource (com.sun.enterprise.config.serverbeans.Resource)6 ConfigApiTest (org.glassfish.tests.utils.ConfigApiTest)6 Test (org.junit.Test)6 ParameterMap (org.glassfish.api.admin.ParameterMap)5 Server (com.sun.enterprise.config.serverbeans.Server)4 ActionReport (org.glassfish.api.ActionReport)4 DeploymentGroup (fish.payara.enterprise.config.serverbeans.DeploymentGroup)3 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)3 ApplicationRef (com.sun.enterprise.config.serverbeans.ApplicationRef)2 BindableResource (com.sun.enterprise.config.serverbeans.BindableResource)2 DGServerRef (fish.payara.enterprise.config.serverbeans.DGServerRef)2 CustomResource (org.glassfish.resources.config.CustomResource)2 MailResource (org.glassfish.resources.javamail.config.MailResource)2 Cluster (com.sun.enterprise.config.serverbeans.Cluster)1 Config (com.sun.enterprise.config.serverbeans.Config)1 Servers (com.sun.enterprise.config.serverbeans.Servers)1 PropertyVetoException (java.beans.PropertyVetoException)1 ArrayList (java.util.ArrayList)1 Logger (java.util.logging.Logger)1