+

Search Tips   |   Advanced Search

Delete an asset using programming

We can delete an asset from a business-level application using programming if the asset is not functioning correctly, the asset is no longer needed, and so on. An asset represents at least one binary file that implements business logic.

This task assumes a basic familiarity with command framework programming. Read about command framework programming in the API documentation.

Before we can delete an asset, you must have imported the asset. All the composition units associated with the asset must be deleted using the deleteCompUnit command before we delete the asset. Otherwise, we have to force the deletion. If we do not force the deletion, the deletion fails. If any other composition units have a dependency on a composition unit being deleted with the force option, the deletion fails. After all dependencies on the composition unit are removed, the force option succeeds.

We can delete an asset using programming, the console, or wsadmin.sh. Use this topic to delete an asset using programming.

Specify the assetID parameter of the asset that you are deleting. We might delete an asset if it is not functioning correctly, it is no longer needed, and so on.

Perform the following tasks to delete an asset using programming.

  1. Connect to the application server.

    The command framework allows the administrative command to be created and run with or without being connected to the application server. This step is optional if the application server is not running.

  2. Create the command manager.

    The command manager provides the functionality to create a new administrative command or query existing administrative commands.

  3. Optionally create the asynchronous command handler for listening to command notifications.
  4. Business-level application commands are implemented as asynchronous commands. To monitor the progress of the running command, we have to create an asynchronous command handler to receive notifications that the command generates.

  5. Create the asynchronous command client.

    An asynchronous command client provides a higher level interface to work with an asynchronous command. If we created an asynchronous command handler in the previous step, the handler is passed to the asynchronous command client. The asynchronous command client forwards the command notification to the handler and helps to control running of the command.

  6. Use the command manager created in a previous step to create and set up the command that deletes an asset.

    The command name is deleteAsset. The assetID parameter is a required parameter to specify the asset to delete. We can optionally specify the delete parameter to force deletion of an asset if composition units are still associated with the asset.

  7. Call the processCommandParameters method in the asynchronous command client to process the command parameters.

    The command framework asynchronous command model requires this call.

  8. Call the asynchronous command client to run the command that deletes an asset.

    We might have created an asynchronous command handler to implement the AsyncCommandHandlerIF interface class in a previous step. If we did, the asynchronous command client listens to command notifications and forwards the notifications to the handler. The handler performs any necessary actions while waiting for the command to complete.

  9. Check the command result when the command completes.

    When the command finishes running, control is returned to the caller. We can then check the result by calling the command.getCommandResult method.


Results

After you successfully run the code, the asset is deleted.


Example

The following example shows how to delete an asset from a business-level application based on the previous steps. Some statements are split on multiple lines for printing purposes.

package com.ibm.ws.management.application.task;

import java.util.Properties;

import com.ibm.websphere.management.AdminClient;
import com.ibm.websphere.management.AdminClientFactory;
import com.ibm.websphere.management.Session;
import com.ibm.websphere.management.cmdframework.AdminCommand;
import com.ibm.websphere.management.cmdframework.CommandMgr;
import com.ibm.websphere.management.cmdframework.CommandResult;
import com.ibm.websphere.management.cmdframework.CommandStep;
import com.ibm.websphere.management.cmdframework.TaskCommand;
import com.ibm.websphere.management.async.client.AsyncCommandClient;

public class DeleteAsset {

    public static void main(String [] args) {

        try {


            // Connect to the application server.   
            // This step is optional if you use the local    
            // command manager. Comment out the lines to and including 
            // CommandMgr cmdMgr = CommandMgr.getClientCommandMgr(
            // soapClient);
            // to get the soapClient soap client if you use the local 
            // command manager.

            String host = "localhost";
            String port = "8880"; // Change to the port number if it is 
                                  // not 8880.
  
            Properties config = new Properties();
            config.put(AdminClient.CONNECTOR_HOST, host);
            config.put(AdminClient.CONNECTOR_PORT, port);
            config.put(AdminClient.CONNECTOR_TYPE, 
                       AdminClient.CONNECTOR_TYPE_SOAP);
            System.out.println("Config: " + config);
            AdminClient soapClient =     
                              AdminClientFactory.createAdminClient(config);
  
            // Create the command manager.
            CommandMgr cmdMgr = CommandMgr.getClientCommandMgr(soapClient);
  
            // Comment out the previous lines to create a client command             // manager if you are using a local command manager.
            // Uncomment the following line to create a local command             // manager.
            //
            // CommandMgr cmdMgr = CommandMgr.getCommandMgr();
            System.out.println("\nCreated command manager");
  
            // Optionally create an asynchronous command handler.
            // Comment out the following line if no further handling
            // of command notification is required.
            AsyncCmdTaskHandler listener = new AsyncCmdTaskHandler();
  
            // Create an asynchronous command client.
  
            // Set up the session.
            String id = Long.toHexString(System.currentTimeMillis());
            String user = "content" + id;
            Session session = new Session(user, true);
       
            // If no command handler is used, replace listener with             // null for the following AsyncCommandClient object:
            AsyncCommandClient asyncCmdClientHelper = new  
            AsyncCommandClient(session, listener);
            System.out.println("\nCreated async command client");
  
            // Create the command that deletes the asset.
            String cmdName = "deleteAsset";
            AdminCommand cmd = cmdMgr.createCommand(cmdName);
            cmd.setConfigSession(session); // Delete the asset from the 
                    // business-level application using the session created.
            System.out.println("\nCreated " + cmdName);

            // Set the assetID  parameter to the asset that is to be 
            // deleted.
            // Examples of valid formats for the assetID parameter are:
            // - aName             
            // - assetname=aName             
            // - WebSphere:assetname=aName
            // This parameter will accept an incomplete ID as long as            // the incomplete ID can resolve to a unique asset
            // in the business-level application.
            String assetID  = "as1";
            cmd.setParameter("assetID", assetID );
        
            System.out.println("\nSet assetID  parameter to "
                                + cmd.getParameter("assetID"));
            // Uncomment the following line of code to set the force parameter 
            // to force the deletion even if there are composition units
            // associated with this asset.
            // 
            // cmd.setParameter("force", "true");
        
            // Call the asynchronous client helper to process parameters.
            try {             
                asyncCmdClientHelper.processCommandParameters(cmd);
                System.out.println("\nCompleted process command " + 
                                           "parameters");
            } catch (Throwable th) {
                System.out.println("Failed from " +   
                    "asyncCmdClientHelper.processCommandParameters(cmd).");
                th.printStackTrace();
                System.exit(-1);
            }
        
            // Call the asynchronous command client to run the command.
            asyncCmdClientHelper.execute(cmd);
            System.out.println("\nCompleted running the command");
  
            // Check the command result.
            CommandResult result = cmd.getCommandResult();
            if (result != null) {
                if (result.isSuccessful()) {
                    System.out.println("\nCommand ran successfully "
                                   + "with result\n" + result.getResult());
                } 
                else {
                    System.out.println("\nCommand ran with " + 
                                         "Exception");
                    result.getException().printStackTrace();
                }
           }
       } catch (Exception e) {
           e.printStackTrace();
       }
    }
}


package com.ibm.ws.management.application.task;

import com.ibm.websphere.management.cmdframework.provider.CommandNotification;
import com.ibm.websphere.management.async.client.AsyncCommandHandlerIF;

public class AsyncCmdTaskHandler implements AsyncCommandHandlerIF {

    public void handleNotification(CommandNotification notification) {
        // Add our own code here to handle the received notification
        System.out.println("\nEXAMPLE: notification received: " +   
                            notification);
    }
}


What to do next

We can complete other steps associated with assets in business-level applications, such as adding or deleting other assets, listing assets, exporting assets, and so on.


Related concepts

  • Additional APIs


    Related tasks

  • Deploy and administering business-level applications
  • Administer business-level applications using programming
  • Import an asset using programming
  • Export an asset using programming
  • Listing assets using programming
  • Edit an asset using programming
  • View an asset using programming
  • Manage assets

  • BLAManagement (AdminTask)