Issue
- I want to undeploy a module from my code, usig API
- I tried using DeployManagerUtil.undeploy(context), but nothing happens
Environment
- Liferay DXP 7.1
Resolution
-
If you are trying to undeploy a module by its context name, which was the functionality provided in 6.2.x, you would do the following:
- Retrieve any bundle: https://osgi.org/javadoc/r5/core/org/osgi/framework/FrameworkUtil.html#getBundle(java.lang.Class)
- Use the retrieved bundle to retrieve a BundleContext: https://osgi.org/javadoc/r5/core/org/osgi/framework/Bundle.html#getBundleContext()
- Use the BundleContext to retrieve a list of all bundles: https://osgi.org/javadoc/r5/core/org/osgi/framework/BundleContext.html#getBundles()
- Find the bundle with the appropriate Web-ContextPath header: https://osgi.org/javadoc/r5/core/org/osgi/framework/Bundle.html#getHeaders()
- Stop the bundle: https://osgi.org/javadoc/r5/core/org/osgi/framework/Bundle.html#stop()
This is what the code looks like as a Groovy script:
String stopWebContextPath = "/blogs-web" import com.liferay.portal.profile.PortalProfile; import java.util.Dictionary; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.FrameworkUtil; Bundle randomBundle = FrameworkUtil.getBundle(PortalProfile.class); BundleContext bundleContext = randomBundle.getBundleContext(); for (Bundle bundle : bundleContext.getBundles()) { Dictionary<String, String> headers = bundle.getHeaders(); String webContextPath = headers.get("Web-ContextPath"); if (webContextPath == null) { continue; } if (webContextPath.equals(stopWebContextPath)) { out.println("stopping bundle: " + bundle.getSymbolicName()); bundle.stop(); break; } }
If you want to undeploy by bundle symbolic name instead (which is more broadly applicable, now that portlets aren't required to have context paths), then you would instead compare against bundle symbolic names: https://osgi.org/javadoc/r5/core/org/osgi/framework/Bundle.html#getSymbolicName()