問題
- APIを使用して、自分のコードからモジュールをアンデプロイしたい。
- DeployManagerUtil.undeploy(context) を使ってみましたが、何も起こりませんです。
環境
- Liferay DXP 7.1
解決策
-
6.2.xで提供されていた機能である、コンテキスト名でモジュールをアンデプロイしようとする場合、次のようにします:
- 任意のバンドルの取得 https://osgi.org/javadoc/r5/core/org/osgi/framework/FrameworkUtil.html#getBundle(java.lang.Class)
- 取得したバンドルを使ってBundleContextを取得する: https://osgi.org/javadoc/r5/core/org/osgi/framework/Bundle.html#getBundleContext()
- BundleContext を使用して、すべてのバンドルのリストを取得する: https://osgi.org/javadoc/r5/core/org/osgi/framework/BundleContext.html#getBundles()
- 適切な Web-ContextPath ヘッダーを持つバンドルを探す: https://osgi.org/javadoc/r5/core/org/osgi/framework/Bundle.html#getHeaders()
- バンドル停止: https://osgi.org/javadoc/r5/core/org/osgi/framework/Bundle.html#stop()
Groovyスクリプトとしては、このようなコードになります:
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; } }
もし、バンドルシンボル名でアンデプロイしたい場合(ポートレットにコンテキストパスが必要ないため、より広範囲に適用できます)、代わりにバンドルシンボル名と比較することになります: https://osgi.org/javadoc/r5/core/org/osgi/framework/Bundle.html#getSymbolicName()