Skip to content

Commit

Permalink
feat: Added OSGi hooks example (#33)
Browse files Browse the repository at this point in the history
* 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
nirikash authored Sep 18, 2024
1 parent d2d9e23 commit 2812fb4
Show file tree
Hide file tree
Showing 20 changed files with 322 additions and 15 deletions.
11 changes: 11 additions & 0 deletions hook-samples-osgi/delete-non-resolved-module-comments/README.md
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 hook-samples-osgi/delete-non-resolved-module-comments/pom.xml
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>
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;
}
}
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.");
}
}
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);
}
}
}
}

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

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>
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>
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>
2 changes: 1 addition & 1 deletion hook-samples/delete-non-resolved-module-comments/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>ch.sbb.polarion.extensions</groupId>
<artifactId>ch.sbb.polarion.extension.interceptor-manager.hook-samples</artifactId>
<version>3.0.2</version>
<version>3.0.2-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class DeleteNonResolvedModuleCommentsHook extends ActionHook implements H
public static final String DESCRIPTION = "Allow the removal of only unresolved comments from the document.";

public static final Logger logger = Logger.getLogger(DeleteNonResolvedModuleCommentsHook.class);
public static final String COMMENT_MESSAGE = "CommentMessage";
public static final String RESOLVED_COMMENT_CONTENT = "'Resolved' comments can not be deleted.";

public DeleteNonResolvedModuleCommentsHook() {
super(ItemType.MODULE_COMMENT, ActionType.DELETE, DESCRIPTION);
Expand All @@ -24,7 +26,7 @@ public String preAction(@NotNull IPObject object) {
IModuleComment moduleComment = (IModuleComment) object;

if (moduleComment.isResolvedComment()) {
return "'Resolved' comments can not be deleted.";
return RESOLVED_COMMENT_CONTENT;
} else {
return null;
}
Expand All @@ -37,6 +39,6 @@ public String preAction(@NotNull IPObject object) {

@Override
public String getDefaultSettings() {
return PropertiesUtils.build();
return PropertiesUtils.build(COMMENT_MESSAGE, RESOLVED_COMMENT_CONTENT);
}
}
2 changes: 1 addition & 1 deletion hook-samples/delete-work-records/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>ch.sbb.polarion.extensions</groupId>
<artifactId>ch.sbb.polarion.extension.interceptor-manager.hook-samples</artifactId>
<version>3.0.2</version>
<version>3.0.2-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class DeleteWorkRecordsHook extends ActionHook implements HookExecutor {
public static final String DESCRIPTION = "Allow deletion of work records only from the current month.";

public static final Logger logger = Logger.getLogger(DeleteWorkRecordsHook.class);
public static final String WORK_RECORDS_WARNING = "Only work records added in the current month can be deleted.";

public DeleteWorkRecordsHook() {
super(ItemType.WORK_RECORD, ActionType.DELETE, DESCRIPTION);
Expand All @@ -29,7 +30,7 @@ public String preAction(@NotNull IPObject object) {
DateOnly workRecordDate = workRecord.getDate();

if (!isDateInCurrentMonth(workRecordDate.getDate())) {
return "Only work records added in the current month can be deleted.";
return WORK_RECORDS_WARNING;
} else {
return null;
}
Expand All @@ -52,6 +53,6 @@ private static boolean isDateInCurrentMonth(@NotNull Date date) {

@Override
public String getDefaultSettings() {
return PropertiesUtils.build();
return PropertiesUtils.build(WORK_RECORDS_WARNING);
}
}
2 changes: 1 addition & 1 deletion hook-samples/module-save-time-logger/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>ch.sbb.polarion.extensions</groupId>
<artifactId>ch.sbb.polarion.extension.interceptor-manager.hook-samples</artifactId>
<version>3.0.2</version>
<version>3.0.2-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
Loading

0 comments on commit 2812fb4

Please sign in to comment.