Delete a file through programming

We can delete a file from an existing application through the administrative console, the wsadmin tool, or programming. Use this example to delete a file through programming.

 

Before you begin

This task assumes a basic familiarity with MBean programming. For information on MBean programming see MBean Java application programming interface (API) documentation.

Before one can delete a file from an application on WebSphere Application Server, first install the application.

 

Overview

Perform the following tasks to delete a file through programming.

 

Procedure

  1. Connect to WebSphere Application Server.

  2. Create the application management proxy.

  3. Create the notification filter for listening to events.

  4. Add the listener.

  5. Delete the file from the application.

  6. Wait for some timeout so that the program does not end.

  7. Listen to Java Management Extensions (JMX) notifications to understand completion of the operation.

  8. When the file is deleted from the application, remove the listener and quit.

 

Result

After you successfully run the code, the file is deleted from the application.

 

Example

The following example shows how to delete a file based on the previous steps. Some statements are split on multiple lines for printing purposes.

//Inputs: 
//fileURI specifies a URI that gives the target location of the file. The URI
// provides the location of the new module after installation. The URI is
// relative to the application URL.
//appName specifies the name of the application.

String fileURI = "Increment.jar/com/acme/Foo.class";
String appName = "MyApp";

//Get the administrative client to connect to
//WebSphere Application Server.
AdminClient client = ...;

//Create the application management proxy.
AppManagement proxy = AppManagementProxy. getJMXProxyForClient (client);

//Create the notification filter.
  NotificationFilterSupport myFilter = new NotificationFilterSupport();
  myFilter.enableType (NotificationConstants.TYPE_APPMANAGEMENT);
  //Add the listener.
  NotificationListener listener = new AListener(_soapClient, myFilter, 
"Install: " + appName, AppNotification.UPDATE);

//Update the existing application, MyApp, by deleting the file.
Hashtable options = new Hashtable();
options.put (AppConstants.APPDEPL_LOCALE, Locale.getDefault());
options.put (AppConstants.APPUPDATE_CONTENTTYPE, AppConstants.APPUPDATE_CONTENT_FILE);

proxy.updateApplication (  appName, 
        fileURI, 
        null,
        AppConstants.APPUPDATE_DELETE,
        options,
        null);

// Wait for some timeout. The installation Application Programming Interface (API) is 
//  asynchronous and so returns immediately. 
// If the program does not wait here, the program ends.
  Thread.sleep(300000); // Wait so that the program does not end.
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }

}
// Specify the Java Management Extensions (JMX) notification listener for JMX events.
class AListener implements NotificationListener
{
    AdminClient _soapClient;
    NotificationFilterSupport  myFilter;
    Object handback;
    ObjectName on;
    String eventTypeToCheck;

    public AListener(AdminClient cl, NotificationFilterSupport fl, 
Object h, String eType) throws Exception
    {
        _soapClient = cl;
        myFilter = fl;
        handback = h;
        eventTypeToCheck = eType;

        Iterator iter = _soapClient.queryNames (new ObjectName(
"WebSphere:type=AppManagement,*"), null).iterator();
        on = (ObjectName)iter.next();
        System.out.println ("ObjectName: " + on);
        _soapClient.addNotificationListener (on, this, myFilter, handback);
    }

    public void handleNotification (Notification notf, Object handback)
    {
            AppNotification ev = (AppNotification) notf.getUserData();
            System.out.println ("!! JMX event Recd: (handback obj= " + handback+ "): " + ev);


            //Once the installation is done, remove the listener and quit

            if (ev.taskName.equals (eventTypeToCheck) &&
                (ev.taskStatus.equals (AppNotification.STATUS_COMPLETED) ||
                 ev.taskStatus.equals (AppNotification.STATUS_FAILED)))
            {
                    try
                    {
                            _soapClient.removeNotificationListener (on, this);
                    }
            catch (Throwable th)
                    {
                        System.out.println ("Error removing listener: " + th);
                    }
                    System.exit (0);
        }
    }
}


 

Related Tasks


Installing an application through programming
Uninstalling an application through programming
Updating an application through programming
Adding to, updating, or deleting part of an application through programming
Preparing a module and adding it to an existing application through programming
Preparing and updating a module through programming
Delete a module through programming
Adding a file through programming
Updating a file through programming