Removing the Debian package
Determining when to uninstall and remove a Debian package
The application must assume the responsibility for removing the Debian package when the application is uninstalled. An application can determine that it is being uninstalled via an AppEventListener or by checking the current state of the application in a components deactivate method.
An application should consider the differences between DISABLED and UNINSTALLED:
When a user disables an application, the application manager removes that application from the OSGi runtime environment, but the physical files that constitute that application are not removed from disk. If a user decides to ENABLE an application that has been disabled, then it is re-introduced into the OSGi runtime environment. When in a DISABLED state, there is no Java code from that application executing (at least the intent is that there is no Java code executing).
An application must determine whether to shut down or remove the installed Debian package when the application is being disabled.
When an application is uninstalled, the application must remove the installed Debian.
Implementing an AppEventListener
If an application registers an AppEventListener with the application service, it can receive notification of pending uninstall or disable actions. These notifications are made prior to shutting down the application in the OSGi runtime environment. The AppEventListener registered by the application receives notices of application events for all installed applications.
To use this method to determine when an application should remove an installed Debian package, it must filter the call-back event for the correct application ID, and then look at the type of event, as shown in the example.
To implement an AppEventListener, include code in your application that is similar to the following example:
private final AppEventListener listener = new AppListener(); ... @Activate protected void activate() throws Exception { ... appService.addAppEventListener(listener); } private final class AppListener implements AppEventListener { @Override public void handleAppEvent(ApplicationEventType event, Application app) { if (app.id().equals(APP_ID)) { if (event.equals(ApplicationEventType.UNINSTALLING) || event.equals(ApplicationEventType.DISABLING)) { removeDebian(); } } } }
Uninstalling (removing) the Debian package
The path to remove the Debian file is /
(the slash character) and requires an action string with the action word uninstall
and the name of the Debian package. The REST API will return a status of 200, and the string associated with the response is JSON structure with the current status of the controller and admin services (sdnc and sdna).
To uninstall a Debian package, include code in your application that is similar to the following example:
. . . URI uri = adminRest.uri(IpAddress.LOOPBACK_IPv4, "/"); // then we need to uninstall it ResponseData response = adminRest.post(adminRest.login(), uri, uninstallRequestBytes()); . . . private byte[] uninstallRequestBytes() throws UnsupportedEncodingException { String uninstall "{ \”action\”: " + “\”uninstall\”, \”name\”: “ + “\”” + APP_DEBIAN_FILE + “\”}"; return uninstall.getBytes(UTF8); }