Search in sources :

Example 26 with ParameterMap

use of org.glassfish.api.admin.ParameterMap in project Payara by payara.

the class ListCustomResourcesTest method testExecuteSuccessListNoResource.

/**
 * Test of execute method, of class ListCustomResources.
 * delete-custom-resource Resource1
 * list-Custom-resources
 */
@Test
public void testExecuteSuccessListNoResource() {
    createCustomResource();
    org.glassfish.resources.admin.cli.ListCustomResources listCommand = habitat.getService(org.glassfish.resources.admin.cli.ListCustomResources.class);
    cr.getCommandInvocation("list-custom-resources", context.getActionReport(), adminSubject()).parameters(parameters).execute(listCommand);
    List<MessagePart> list = context.getActionReport().getTopMessagePart().getChildren();
    assertEquals(origNum + 1, list.size());
    // as we newly created a resource after test "setup".
    origNum = origNum + 1;
    deleteCustomResource();
    ParameterMap parameters = new ParameterMap();
    listCommand = habitat.getService(org.glassfish.resources.admin.cli.ListCustomResources.class);
    context = new AdminCommandContextImpl(LogDomains.getLogger(ListCustomResourcesTest.class, LogDomains.ADMIN_LOGGER), new PropsFileActionReporter());
    cr.getCommandInvocation("list-custom-resources", context.getActionReport(), adminSubject()).parameters(parameters).execute(listCommand);
    list = context.getActionReport().getTopMessagePart().getChildren();
    if ((origNum - 1) == 0) {
    // Nothing to list.
    } else {
        assertEquals(origNum - 1, list.size());
    }
    List<String> listStr = new java.util.ArrayList<String>();
    for (MessagePart mp : list) {
        listStr.add(mp.getMessage());
    }
    assertFalse(listStr.contains("custom_resource1"));
    assertEquals(ActionReport.ExitCode.SUCCESS, context.getActionReport().getActionExitCode());
}
Also used : MessagePart(org.glassfish.api.ActionReport.MessagePart) ParameterMap(org.glassfish.api.admin.ParameterMap) PropsFileActionReporter(com.sun.enterprise.v3.common.PropsFileActionReporter) AdminCommandContextImpl(org.glassfish.api.admin.AdminCommandContextImpl) Test(org.junit.Test) ConfigApiTest(org.glassfish.tests.utils.ConfigApiTest)

Example 27 with ParameterMap

use of org.glassfish.api.admin.ParameterMap in project Payara by payara.

the class RecoverTransactions method execute.

public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    if (_logger.isLoggable(Level.INFO)) {
        _logger.info("==> original target: " + destinationServer + " ... server: " + serverToRecover);
    }
    String error = validate(destinationServer, false);
    if (error != null) {
        _logger.log(Level.WARNING, localStrings.getString("recover.transactions.failed") + " " + error);
        report.setMessage(error);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    // here we are only if parameters consistent
    if (destinationServer == null)
        destinationServer = serverToRecover;
    try {
        boolean result;
        CommandRunner.CommandInvocation inv = runner.getCommandInvocation("_recover-transactions-internal", report, context.getSubject());
        final ParameterMap parameters = new ParameterMap();
        parameters.add("target", destinationServer);
        parameters.add("DEFAULT", serverToRecover);
        parameters.add("transactionlogdir", transactionLogDir);
        if (_logger.isLoggable(Level.INFO)) {
            _logger.info("==> calling _recover-transactions-internal with params: " + parameters);
        }
        inv.parameters(parameters).execute();
        if (_logger.isLoggable(Level.INFO)) {
            _logger.info("==> _recover-transactions-internal returned with: " + report.getActionExitCode());
        }
    // Exit code is set by _recover-transactions-internal
    } catch (Exception e) {
        _logger.log(Level.WARNING, localStrings.getString("recover.transactions.failed"), e);
        report.setMessage(localStrings.getString("recover.transactions.failed"));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(e);
    }
}
Also used : ParameterMap(org.glassfish.api.admin.ParameterMap) ActionReport(org.glassfish.api.ActionReport) CommandRunner(org.glassfish.api.admin.CommandRunner)

Example 28 with ParameterMap

use of org.glassfish.api.admin.ParameterMap in project Payara by payara.

the class EnableDisableTest method test.

@Test
public void test() throws Exception {
    // 1. Bootstrap GlassFish DAS in embedded mode.
    GlassFishProperties glassFishProperties = new GlassFishProperties();
    glassFishProperties.setInstanceRoot(System.getenv("S1AS_HOME") + "/domains/domain1");
    glassFishProperties.setConfigFileReadOnly(false);
    GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish(glassFishProperties);
    PrintStream sysout = System.out;
    glassfish.start();
    System.setOut(sysout);
    // 2. Deploy the PaaS application.
    File archive = new File(System.getProperty("basedir") + // TODO :: use
    "/target/enable-disable-sample.war");
    // mvn apis to
    // get the
    // archive
    // location.
    Assert.assertTrue(archive.exists());
    Deployer deployer = null;
    String appName = null;
    try {
        deployer = glassfish.getDeployer();
        appName = deployer.deploy(archive);
        System.err.println("Deployed [" + appName + "]");
        Assert.assertNotNull(appName);
        CommandResult result = null;
        CommandRunner commandRunner = glassfish.getCommandRunner();
        {
            result = commandRunner.run("list-services", "appname=" + appName, "output=STATE");
            System.out.println("\nlist-services command output [ " + result.getOutput() + "]");
            boolean notRunning = result.getOutput().toLowerCase().contains("notrunning");
            Assert.assertTrue(!notRunning);
            boolean stopped = result.getOutput().toLowerCase().contains("stopped");
            Assert.assertTrue(!stopped);
        }
        // 2.a.Check if all services of the application are in ONLY running state.
        ServiceLocator habitat = Globals.getDefaultHabitat();
        org.glassfish.api.admin.CommandRunner commandRunner1 = habitat.getService(org.glassfish.api.admin.CommandRunner.class);
        ActionReport report = habitat.getService(ActionReport.class);
        org.glassfish.api.admin.CommandRunner.CommandInvocation invocation = commandRunner1.getCommandInvocation("list-services", report);
        ParameterMap parameterMap = new ParameterMap();
        parameterMap.add("appname", appName);
        parameterMap.add("output", "state");
        parameterMap.add("scope", "application");
        invocation.parameters(parameterMap).execute();
        Assert.assertFalse(report.hasFailures());
        List<Map<String, String>> listOfMap = (List<Map<String, String>>) report.getExtraProperties().get("list");
        String state = null;
        boolean servicesRunning = false;
        for (Map<String, String> map : listOfMap) {
            servicesRunning = false;
            state = map.get("STATE");
            if (state.equalsIgnoreCase("running")) {
                servicesRunning = true;
            } else {
                break;
            }
        }
        Assert.assertTrue(servicesRunning);
        System.out.println("All services in RUNNING state");
        // 3. Access the app to make sure PaaS app is correctly provisioned.
        String HTTP_PORT = (System.getProperty("http.port") != null) ? System.getProperty("http.port") : "28080";
        String instanceIP = getLBIPAddress(glassfish);
        get("http://" + instanceIP + ":" + HTTP_PORT + "/enable-disable-sample/EnableDisableServlet", "Customer ID");
        // 4.Disable application
        ParameterMap parameterMap1 = new ParameterMap();
        parameterMap1.add("DEFAULT", appName);
        invocation = commandRunner1.getCommandInvocation("disable", report);
        invocation.parameters(parameterMap1).execute();
        Assert.assertFalse(report.hasFailures());
        System.out.println("Disabled application ' " + appName + " ' ");
        // 5.Check if NONE of the application of the service are in 'RUNNING' state
        invocation = commandRunner1.getCommandInvocation("list-services", report);
        invocation.parameters(parameterMap).execute();
        Assert.assertFalse(report.hasFailures());
        listOfMap = (List<Map<String, String>>) report.getExtraProperties().get("list");
        for (Map<String, String> map : listOfMap) {
            servicesRunning = true;
            state = map.get("STATE");
            if (state.equalsIgnoreCase("running")) {
                break;
            } else {
                servicesRunning = false;
            }
        }
        Assert.assertFalse(servicesRunning);
        System.out.println("No service in RUNNING state");
        // 6.Enable application
        invocation = commandRunner1.getCommandInvocation("enable", report);
        parameterMap1 = new ParameterMap();
        parameterMap1.add("DEFAULT", appName);
        invocation.parameters(parameterMap1).execute();
        Assert.assertFalse(report.hasFailures());
        System.out.println("Enabled application ' " + appName + " ' ");
        get("http://" + instanceIP + ":" + HTTP_PORT + "/enable-disable-sample/EnableDisableServlet", "Customer ID");
    // 8.. Undeploy the PaaS application .
    } finally {
        if (appName != null) {
            deployer.undeploy(appName);
            System.out.println("Destroying the resources created");
            System.err.println("Undeployed [" + appName + "]");
            try {
                boolean undeployClean = false;
                CommandResult commandResult = glassfish.getCommandRunner().run("list-services");
                if (commandResult.getOutput().contains("Nothing to list.")) {
                    undeployClean = true;
                }
                Assert.assertTrue(undeployClean);
            } catch (Exception e) {
                System.err.println("Couldn't varify whether undeploy succeeded");
            }
        }
        glassfish.dispose();
    }
}
Also used : PrintStream(java.io.PrintStream) ParameterMap(org.glassfish.api.admin.ParameterMap) ActionReport(org.glassfish.api.ActionReport) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) List(java.util.List) File(java.io.File) ParameterMap(org.glassfish.api.admin.ParameterMap) Map(java.util.Map) Test(org.junit.Test)

Example 29 with ParameterMap

use of org.glassfish.api.admin.ParameterMap in project Payara by payara.

the class ExtSharedServiceEnableDisableTest method testSharedAndExternalService.

private void testSharedAndExternalService() {
    System.out.println("$$$$$$$$$$$$$ TEST SHARED AND EXTERNAL SERVICES $$$$$$$$$$$$$$$");
    ServiceLocator habitat = Globals.getDefaultHabitat();
    org.glassfish.api.admin.CommandRunner commandRunner = habitat.getService(org.glassfish.api.admin.CommandRunner.class);
    ActionReport report = habitat.getService(ActionReport.class);
    // Disable the application and try stopping  the shared service. Command should succeed
    org.glassfish.api.admin.CommandRunner.CommandInvocation invocation = commandRunner.getCommandInvocation("disable", report);
    ParameterMap parameterMap = new ParameterMap();
    parameterMap.add("DEFAULT", "ext-shared-service-enable-disable-test");
    invocation.parameters(parameterMap).execute();
    System.out.println("Disabled application ext-shared-service-enable-disable-test: " + !report.hasFailures());
    Assert.assertFalse(report.hasFailures());
    invocation = commandRunner.getCommandInvocation("stop-shared-service", report);
    parameterMap = new ParameterMap();
    parameterMap.add("DEFAULT", "my-shared-lb-service");
    invocation.parameters(parameterMap).execute();
    Assert.assertFalse(report.hasFailures());
    System.out.println("MSG: " + report.getMessage());
    // try deleting a external service when an app is using it. it should 'FAIL'
    invocation = commandRunner.getCommandInvocation("delete-external-service", report);
    parameterMap = new ParameterMap();
    parameterMap.add("DEFAULT", "my-external-db-service");
    invocation.parameters(parameterMap).execute();
    Assert.assertTrue(report.hasFailures());
    System.out.println("Expected Failure Msg: " + report.getMessage());
    invocation = commandRunner.getCommandInvocation("stop-shared-service", report);
    parameterMap = new ParameterMap();
    parameterMap.add("DEFAULT", "my-shared-gf-service");
    invocation.parameters(parameterMap).execute();
    Assert.assertFalse(report.hasFailures());
    System.out.println("MSG: " + report.getMessage());
    // List the services and check the status of both the services - it should be 'STOPPED'
    parameterMap = new ParameterMap();
    parameterMap.add("scope", "shared");
    parameterMap.add("output", "service-name,state");
    invocation = commandRunner.getCommandInvocation("list-services", report);
    invocation.parameters(parameterMap).execute();
    boolean sharedServiceStopped = false;
    List<Map<String, String>> list = (List<Map<String, String>>) report.getExtraProperties().get("list");
    for (Map<String, String> map : list) {
        sharedServiceStopped = false;
        String state = map.get("STATE");
        if ("STOPPED".equalsIgnoreCase(state)) {
            sharedServiceStopped = true;
        } else {
            sharedServiceStopped = false;
            break;
        }
    }
    // check if the shared services are stopped
    Assert.assertTrue(sharedServiceStopped);
    // Start the shared services.
    report = habitat.getService(ActionReport.class);
    invocation = commandRunner.getCommandInvocation("start-shared-service", report);
    parameterMap = new ParameterMap();
    parameterMap.add("DEFAULT", "my-shared-gf-service");
    invocation.parameters(parameterMap).execute();
    Assert.assertFalse(report.hasFailures());
    System.out.println("MSG: " + report.getMessage());
    parameterMap = new ParameterMap();
    parameterMap.add("DEFAULT", "my-shared-lb-service");
    invocation.parameters(parameterMap).execute();
    Assert.assertFalse(report.hasFailures());
    System.out.println("MSG: " + report.getMessage());
    // List the services and check the status of both the services - it should be 'STARTED'
    parameterMap = new ParameterMap();
    parameterMap.add("scope", "shared");
    parameterMap.add("output", "service-name,state");
    invocation = commandRunner.getCommandInvocation("list-services", report);
    invocation.parameters(parameterMap).execute();
    boolean sharedServiceStarted = false;
    list = (List<Map<String, String>>) report.getExtraProperties().get("list");
    for (Map<String, String> map : list) {
        sharedServiceStarted = false;
        String state = map.get("STATE");
        if ("STARTED".equalsIgnoreCase(state) || "RUNNING".equalsIgnoreCase(state)) {
            sharedServiceStarted = true;
        } else {
            break;
        }
    }
    // check if the shared services are started.
    Assert.assertTrue(sharedServiceStarted);
    // Enable the application and try stopping  accessing
    invocation = commandRunner.getCommandInvocation("enable", report);
    parameterMap = new ParameterMap();
    parameterMap.add("DEFAULT", "ext-shared-service-enable-disable-test");
    invocation.parameters(parameterMap).execute();
    System.out.println("Enabled application ext-shared-service-enable-disable-test: " + !report.hasFailures());
    Assert.assertFalse(report.hasFailures());
    {
        // List the services and check the status of both the services - it should be 'RUNNING'
        invocation = commandRunner.getCommandInvocation("list-services", report);
        parameterMap = new ParameterMap();
        parameterMap.add("scope", "shared");
        parameterMap.add("output", "service-name,state");
        invocation.parameters(parameterMap).execute();
        sharedServiceStarted = false;
        list = (List<Map<String, String>>) report.getExtraProperties().get("list");
        for (Map<String, String> map : list) {
            sharedServiceStarted = false;
            String state = map.get("STATE");
            if ("RUNNING".equalsIgnoreCase(state)) {
                sharedServiceStarted = true;
            } else {
                break;
            }
        }
        // check if the shared services are started.
        Assert.assertTrue(sharedServiceStarted);
    }
}
Also used : ParameterMap(org.glassfish.api.admin.ParameterMap) ActionReport(org.glassfish.api.ActionReport) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) CommandRunner(org.glassfish.embeddable.CommandRunner) ParameterMap(org.glassfish.api.admin.ParameterMap)

Example 30 with ParameterMap

use of org.glassfish.api.admin.ParameterMap in project Payara by payara.

the class ExtSharedServiceEnableDisableTest method deleteSharedAndExternalService.

private void deleteSharedAndExternalService() {
    ServiceLocator habitat = Globals.getDefaultHabitat();
    org.glassfish.api.admin.CommandRunner commandRunner = habitat.getService(org.glassfish.api.admin.CommandRunner.class);
    ActionReport report = habitat.getService(ActionReport.class);
    org.glassfish.api.admin.CommandRunner.CommandInvocation invocation = commandRunner.getCommandInvocation("delete-shared-service", report);
    ParameterMap parameterMap = new ParameterMap();
    parameterMap.add("DEFAULT", "my-shared-lb-service");
    invocation.parameters(parameterMap).execute();
    Assert.assertFalse(report.hasFailures());
    invocation = commandRunner.getCommandInvocation("delete-external-service", report);
    parameterMap = new ParameterMap();
    parameterMap.add("DEFAULT", "my-external-db-service");
    invocation.parameters(parameterMap).execute();
    Assert.assertFalse(report.hasFailures());
    invocation = commandRunner.getCommandInvocation("delete-shared-service", report);
    parameterMap = new ParameterMap();
    parameterMap.add("DEFAULT", "my-shared-gf-service");
    invocation.parameters(parameterMap).execute();
    Assert.assertFalse(report.hasFailures());
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) ParameterMap(org.glassfish.api.admin.ParameterMap) ActionReport(org.glassfish.api.ActionReport) CommandRunner(org.glassfish.embeddable.CommandRunner)

Aggregations

ParameterMap (org.glassfish.api.admin.ParameterMap)149 ActionReport (org.glassfish.api.ActionReport)68 CommandRunner (org.glassfish.api.admin.CommandRunner)37 Test (org.junit.Test)25 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)24 Map (java.util.Map)20 PropsFileActionReporter (com.sun.enterprise.v3.common.PropsFileActionReporter)19 AdminCommandContextImpl (org.glassfish.api.admin.AdminCommandContextImpl)18 List (java.util.List)16 ArrayList (java.util.ArrayList)15 CommandRunner (org.glassfish.embeddable.CommandRunner)15 IOException (java.io.IOException)14 ConfigApiTest (org.glassfish.tests.utils.ConfigApiTest)13 Before (org.junit.Before)13 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)13 CommandException (org.glassfish.api.admin.CommandException)12 File (java.io.File)11 MessagePart (org.glassfish.api.ActionReport.MessagePart)11 Resource (com.sun.enterprise.config.serverbeans.Resource)10 Logger (java.util.logging.Logger)9