Oracle GlassFish Server 3.0.1 Embedded Server Guide

Running asadmin Commands Using the GlassFish Server Embedded Server API

Running asadmin(1M) commands from an application enables the application to configure the embedded GlassFish Server to suit the application's requirements. For example, an application can run the required asadmin commands to create a JDBC technology connection to a database.

For more information about configuring embedded GlassFish Server, see the Oracle GlassFish Server 3.0.1 Administration Guide. For detailed information about asadmin commands, see Section 1 of the Oracle GlassFish Server 3.0.1 Reference Manual.


Note –

Ensure that your application has started an embedded GlassFish Server before the application attempts to run asadmin commands. For more information, see Running an Embedded GlassFish Server.


The org.glassfish.api.admin package contains classes that you can use to run asadmin commands. Use the following code examples as templates and change the command name, parameter names, and parameter values as needed.


Example 8 Running an asadmin create-jdbc-resource Command

This example shows code for running an asadmin create-jdbc-resource command. Code for creating and starting the server is not shown in this example. For an example of code for creating and starting the server, see Example 4.

...
import org.glassfish.api.embedded.*;
import org.glassfish.api.admin.*;
...
    String command = "create-jdbc-resource";
    ParameterMap params = new ParameterMap();
    params.add("connectionpoolid", "DerbyPool");
    params.add("jndi_name", "jdbc/DerbyPool");
    CommandRunner runner = server.getHabitat().getComponent(CommandRunner.class);
    ActionReport report = server.getHabitat().getComponent(ActionReport.class);
    runner.getCommandInvocation(command, report).parameters(params).execute();
...


Example 9 Running an asadmin set-log-level Command

This example shows code for running an asadmin set-log-level command. Code for creating and starting the server is not shown in this example. For an example of code for creating and starting the server, see Example 4.

...
import org.glassfish.api.embedded.*;
import org.glassfish.api.admin.*;
...
    String command = "set-log-level";
    ParameterMap params = new ParameterMap();
    params.add("javax\.enterprise\.system\.container\.web", "FINE");
    CommandRunner runner = server.getHabitat().getComponent(CommandRunner.class);
    ActionReport report = server.getHabitat().getComponent(ActionReport.class);
    runner.getCommandInvocation(command, report).parameters(params).execute();
...

For another way to change log levels, see Changing Log Levels in Embedded GlassFish Server.