-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added OSGi hooks example (#33)
* feat: Added OSGi hooks example Refs: #31 * fix: Fixed parent pom Refs: #31 * fix: Fixed parent pom Refs: #31 * fix: Fixed code smell Refs: #31 * fix: Fixed code smell Refs: #31 * fix: Workaround for duplicated code Refs: #31 * fix: Added settings property, renamed OSGi hook class Refs: #31 * fix: Added version in OSGi hook sample constructor Refs: #31
- Loading branch information
Showing
20 changed files
with
322 additions
and
15 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
hook-samples-osgi/delete-non-resolved-module-comments/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Hook in Custom Polarion Extension | ||
|
||
This hook allows for the removal of unresolved comments from the document only. The example demonstrates the technique for registering a hook in a custom Polarion extension. In this case, the hook implementation should be registered as an OSGi service and will be discovered by the interceptor-manager. | ||
To register a hook as an OSGi service in any Polarion extension, follow these steps: | ||
1. Add the dependency org.osgi/org.osgi.framework in your Maven or Gradle build with the provided scope. | ||
2. Create a BundleActivator class that implements the org.osgi.framework.BundleActivator interface. This class must register the hook implementation as an OSGi service under the IActionHook interface. See [HookBundleActivator.java](https://github.com/SchweizerischeBundesbahnen/ch.sbb.polarion.extension.interceptor-manager.hook-samples/blob/main/hook-samples-osgi/delete-non-resolved-module-comments/src/main/java/ch/sbb/polarion/extension/interceptor/hook_samples/osgi/HookBundleActivator.java) for details. | ||
3. Add the following three entries to MANIFEST.MF: | ||
``` Bundle-Activator: <BUNDLE_ACTIVATOR_CLASS> | ||
Bundle-ActivationPolicy: lazy | ||
Import-Package: org.osgi.framework | ||
``` |
76 changes: 76 additions & 0 deletions
76
hook-samples-osgi/delete-non-resolved-module-comments/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>ch.sbb.polarion.extensions</groupId> | ||
<artifactId>ch.sbb.polarion.extension.interceptor-manager.hook-samples</artifactId> | ||
<version>3.0.2-SNAPSHOT</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>ch.sbb.polarion.extension.hook-samples.osgi.delete-non-resolved-module-comments</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<properties> | ||
<maven.compiler.source>17</maven.compiler.source> | ||
<maven.compiler.target>17</maven.compiler.target> | ||
<polarion.version>2404</polarion.version> | ||
<org.osgi.framework.version>1.10.0</org.osgi.framework.version> | ||
<target.hooks.directory>${env.POLARION_HOME}/polarion/extensions/${project.artifactId}/eclipse/plugins</target.hooks.directory> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.polarion.alm.projects</groupId> | ||
<artifactId>projects</artifactId> | ||
<version>${polarion.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.polarion.alm.tracker</groupId> | ||
<artifactId>tracker</artifactId> | ||
<version>${polarion.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.polarion.platform.persistence</groupId> | ||
<artifactId>platform-persistence</artifactId> | ||
<version>${polarion.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.polarion.core.util</groupId> | ||
<artifactId>util</artifactId> | ||
<version>${polarion.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.polarion.thirdparty</groupId> | ||
<artifactId>javax.servlet_4.0.0</artifactId> | ||
<version>${polarion.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.osgi</groupId> | ||
<artifactId>org.osgi.framework</artifactId> | ||
<version>${org.osgi.framework.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<configuration combine.self="override"> | ||
<archive> | ||
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile> | ||
</archive> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
42 changes: 42 additions & 0 deletions
42
...rion/extension/interceptor/hook_samples/osgi/DeleteNonResolvedModuleCommentsHookOSGi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package ch.sbb.polarion.extension.interceptor.hook_samples.osgi; | ||
|
||
import ch.sbb.polarion.extension.interceptor_manager.model.ActionHook; | ||
import ch.sbb.polarion.extension.interceptor_manager.model.HookExecutor; | ||
import ch.sbb.polarion.extension.interceptor_manager.util.PropertiesUtils; | ||
import com.polarion.alm.tracker.model.IModuleComment; | ||
import com.polarion.core.util.logging.Logger; | ||
import com.polarion.platform.persistence.model.IPObject; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
@SuppressWarnings("unused") | ||
public class DeleteNonResolvedModuleCommentsHookOSGi extends ActionHook implements HookExecutor { | ||
private static final Logger logger = Logger.getLogger(DeleteNonResolvedModuleCommentsHookOSGi.class); | ||
|
||
private static final String DESCRIPTION = "Allow the removal of only unresolved comments from the document. Loaded using OSGi services"; | ||
private static final String COMMENT_MESSAGE = "CommentMessage"; | ||
private static final String RESOLVED_CANNOT_BE_DELETED = "'Resolved' comments can not be deleted."; | ||
private static final String VERSION = "1.0.0"; | ||
|
||
public DeleteNonResolvedModuleCommentsHookOSGi() { | ||
super(ItemType.MODULE_COMMENT, ActionType.DELETE, VERSION, DESCRIPTION); | ||
} | ||
|
||
@Override | ||
public String preAction(@NotNull IPObject object) { | ||
if (((IModuleComment) object).isResolvedComment()) { | ||
return RESOLVED_CANNOT_BE_DELETED; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public String getDefaultSettings() { | ||
return PropertiesUtils.build(COMMENT_MESSAGE, RESOLVED_CANNOT_BE_DELETED); | ||
} | ||
|
||
@Override | ||
public @NotNull HookExecutor getExecutor() { | ||
return this; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...ain/java/ch/sbb/polarion/extension/interceptor/hook_samples/osgi/HookBundleActivator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package ch.sbb.polarion.extension.interceptor.hook_samples.osgi; | ||
|
||
import ch.sbb.polarion.extension.interceptor_manager.model.IActionHook; | ||
import com.polarion.core.util.logging.Logger; | ||
import org.osgi.framework.BundleActivator; | ||
import org.osgi.framework.BundleContext; | ||
import org.osgi.framework.ServiceRegistration; | ||
|
||
import java.util.Hashtable; | ||
|
||
public class HookBundleActivator implements BundleActivator { | ||
private static final Logger logger = Logger.getLogger(HookBundleActivator.class); | ||
|
||
private ServiceRegistration<IActionHook> actionHookRegistration; | ||
|
||
@Override | ||
public void start(BundleContext context) { | ||
logger.info("Registering action hooks services."); | ||
Hashtable<String, Object> keys = new Hashtable<>(); | ||
keys.put("hook-name", "delete-non-resolved-module-comments"); | ||
|
||
actionHookRegistration = context.registerService( | ||
IActionHook.class, | ||
new DeleteNonResolvedModuleCommentsHookOSGi(), | ||
keys); | ||
|
||
logger.info("Sample action hook service have been registered."); | ||
} | ||
|
||
@Override | ||
public void stop(BundleContext context) { | ||
logger.info("Unregistering action hook service."); | ||
|
||
actionHookRegistration.unregister(); | ||
|
||
logger.info("Action hook service have been unregistered."); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...ts/src/main/java/ch/sbb/polarion/extension/interceptor/hook_samples/osgi/HookServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package ch.sbb.polarion.extension.interceptor.hook_samples.osgi; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
public class HookServlet extends HttpServlet { | ||
|
||
@Override | ||
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) { | ||
try { | ||
getServletContext().getRequestDispatcher("/hookExample.jsp").forward(req, resp); | ||
} catch (ServletException | IOException e) { | ||
try { | ||
log("doGet failed: " + e.getMessage(), e); | ||
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An internal error occurred"); | ||
} catch (IOException ex) { | ||
log("IOException occurred by sending error response: "+ e.getMessage(), e); | ||
} | ||
} | ||
} | ||
} | ||
|
15 changes: 15 additions & 0 deletions
15
...-samples-osgi/delete-non-resolved-module-comments/src/main/resources/META-INF/MANIFEST.MF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Manifest-Version: 1.0 | ||
Automatic-Module-Name: ch.sbb.polarion.extension.osgi.hook.example | ||
Bundle-ManifestVersion: 2 | ||
Bundle-SymbolicName: ch.sbb.polarion.extension.osgi.hook.example;singleton:=true | ||
Bundle-Vendor: SBB AG | ||
Bundle-Version: 1.0.0 | ||
Bundle-Name: OSGi Hook example | ||
Bundle-Activator: ch.sbb.polarion.extension.interceptor.hook_samples.osgi.HookBundleActivator | ||
Bundle-ActivationPolicy: lazy | ||
Require-Bundle: com.polarion.alm.tracker, | ||
com.polarion.alm.ui, | ||
com.polarion.portal.tomcat, | ||
ch.sbb.polarion.extension.interceptor-manager | ||
Import-Package: org.osgi.framework | ||
|
11 changes: 11 additions & 0 deletions
11
hook-samples-osgi/delete-non-resolved-module-comments/src/main/resources/plugin.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<?eclipse version="3.0"?> | ||
<plugin> | ||
<extension | ||
point="com.polarion.portal.tomcat.webapps"> | ||
<webapp | ||
name="polarion/hook-samples-osgi" | ||
contextRoot="webapp"> | ||
</webapp> | ||
</extension> | ||
</plugin> |
66 changes: 66 additions & 0 deletions
66
...amples-osgi/delete-non-resolved-module-comments/src/main/resources/webapp/WEB-INF/web.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE web-app | ||
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" | ||
"http://java.sun.com/dtd/web-app_2_3.dtd"> | ||
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://xmlns.jcp.org/xml/ns/javaee" | ||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee | ||
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" | ||
version="3.1"> | ||
|
||
<display-name>osgi-hook-example</display-name> | ||
|
||
<filter> | ||
<filter-name>DoAsFilter</filter-name> | ||
<filter-class>com.polarion.portal.tomcat.servlets.DoAsFilter</filter-class> | ||
</filter> | ||
<filter-mapping> | ||
<filter-name>DoAsFilter</filter-name> | ||
<url-pattern>/*</url-pattern> | ||
</filter-mapping> | ||
|
||
<session-config> | ||
<session-timeout>30</session-timeout> | ||
</session-config> | ||
|
||
<mime-mapping> | ||
<extension>log</extension> | ||
<mime-type>text/plain</mime-type> | ||
</mime-mapping> | ||
|
||
<servlet> | ||
<servlet-name>osgi-hook-example</servlet-name> | ||
<servlet-class>ch.sbb.polarion.extension.interceptor.hook_samples.osgi.HookServlet</servlet-class> | ||
|
||
<init-param> | ||
<param-name>debug</param-name> | ||
<param-value>0</param-value> | ||
</init-param> | ||
<load-on-startup>1</load-on-startup> | ||
</servlet> | ||
|
||
<servlet-mapping> | ||
<servlet-name>osgi-hook-example</servlet-name> | ||
<url-pattern>/ui/*</url-pattern> | ||
</servlet-mapping> | ||
|
||
<security-constraint> | ||
<web-resource-collection> | ||
<web-resource-name>All</web-resource-name> | ||
<url-pattern>/*</url-pattern> | ||
</web-resource-collection> | ||
<auth-constraint> | ||
<role-name>user</role-name> | ||
</auth-constraint> | ||
</security-constraint> | ||
|
||
<!-- Login configuration uses form-based authentication --> | ||
<login-config> | ||
<auth-method>FORM</auth-method> | ||
<realm-name>PolarionRealm</realm-name> | ||
<form-login-config> | ||
<form-login-page>/login/login</form-login-page> | ||
<form-error-page>/login/error</form-error-page> | ||
</form-login-config> | ||
</login-config> | ||
</web-app> |
18 changes: 18 additions & 0 deletions
18
...amples-osgi/delete-non-resolved-module-comments/src/main/resources/webapp/hookExample.jsp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<%@ page contentType="text/html;charset=UTF-8" language="java" %> | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Action Hook Example</title> | ||
<style> | ||
body { | ||
font-family: Arial, sans-serif; | ||
text-align: center; | ||
margin-top: 50px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>Delete non-resolved module comments</h1> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.