Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added Google Guice hook example #34

Merged
merged 3 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# 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 Guice Module and will be discovered and injected in the interceptor-manager.
To register a hook as Guice Module in any Polarion extension, follow these steps:
1. Add the dependency com.google.inject/guice in your Maven or Gradle build with the provided scope.
2. Create a ActionHookModule class that implements the com.google.inject.AbstractModule interface. This class must add binding to the hook implementation class using IActionHook interface. See [HookBundleActivator.java](https://github.com/SchweizerischeBundesbahnen/ch.sbb.polarion.extension.interceptor-manager.hook-samples/blob/main/hook-samples-juice/delete-non-resolved-module-comments/src/main/java/ch/sbb/polarion/extension/interceptor/hook_samples/guice/ActionHooksModule.java) for details.
3. Specify your module class including java package in the following entry to MANIFEST.MF:
` Guice-Modules: <MODULE_CLASS_FULLY_QUALIFIED_NAME>
`
78 changes: 78 additions & 0 deletions hook-samples-guice/delete-non-resolved-module-comments/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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.guice.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>
<google.guice.version>7.0.0</google.guice.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>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>${google.guice.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,16 @@
package ch.sbb.polarion.extension.interceptor.hook_samples.guice;

import ch.sbb.polarion.extension.interceptor_manager.model.IActionHook;
import com.google.inject.AbstractModule;
import com.google.inject.multibindings.Multibinder;
import com.polarion.core.util.logging.Logger;

public class ActionHooksModule extends AbstractModule {
public static final Logger logger = Logger.getLogger(ActionHooksModule.class);

@Override
protected void configure() {
Multibinder.newSetBinder(this.binder(), IActionHook.class).addBinding().toInstance(new DeleteNonResolvedModuleCommentsHookJuice());
logger.info("Registered DeleteNonResolvedModuleCommentsHookJuice in Google Guice module");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ch.sbb.polarion.extension.interceptor.hook_samples.guice;

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 DeleteNonResolvedModuleCommentsHookJuice extends ActionHook implements HookExecutor {
private static final Logger logger = Logger.getLogger(DeleteNonResolvedModuleCommentsHookJuice.class);

private static final String JUICE_HOOK_DESCRIPTION = "Allow the removal of only unresolved comments from the document. Loaded using Google Guice";
private static final String MESSAGE_TITLE = "CommentMessage";
private static final String CANNOT_BE_DELETED_MESSAGE = "'Resolved' comments can not be deleted.";
private static final String JOICE_HOOK_VERSION = "0.0.1";

public DeleteNonResolvedModuleCommentsHookJuice() {
super(ItemType.MODULE_COMMENT, ActionType.DELETE, JOICE_HOOK_VERSION, JUICE_HOOK_DESCRIPTION);
}

@Override
public String preAction(@NotNull IPObject object) {
return (((IModuleComment) object).isResolvedComment()) ? CANNOT_BE_DELETED_MESSAGE : null;
}

@Override
public String getDefaultSettings() {
return PropertiesUtils.build(MESSAGE_TITLE, CANNOT_BE_DELETED_MESSAGE);
}

@Override
public @NotNull HookExecutor getExecutor() {
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ch.sbb.polarion.extension.interceptor.hook_samples.guice;

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 doPost(final HttpServletRequest req, final HttpServletResponse resp) {
try {
getServletContext().getRequestDispatcher("/hookExample.jsp").forward(req, resp);
} catch (Exception e) {
try {
log("doPost failed in guice hook sample: " + e.getMessage(), e);
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An internal error occurred in guice hooks");
} catch (IOException ex) {
log("IOException occurred by sending error response in guice hook sample: "+ e.getMessage(), e);
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Manifest-Version: 1.0
Automatic-Module-Name: ch.sbb.polarion.extension.juice.hook.example
Bundle-ManifestVersion: 2
Bundle-SymbolicName: ch.sbb.polarion.extension.juice.hook.example;singleton:=true
Bundle-Vendor: SBB AG
Bundle-Version: 1.0.0
Bundle-Name: Juice Hook example
Guice-Modules: ch.sbb.polarion.extension.interceptor.hook_samples.guice.ActionHooksModule
Require-Bundle: com.polarion.alm.tracker,
com.polarion.alm.ui,
com.polarion.portal.tomcat,
com.polarion.platform.guice,
ch.sbb.polarion.extension.interceptor-manager

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-hivemind"
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.guice.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>
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<module>hook-samples/testrun</module>
<module>hook-samples/title-length-check</module>
<module>hook-samples-osgi/delete-non-resolved-module-comments</module>
<module>hook-samples-guice/delete-non-resolved-module-comments</module>
</modules>

<properties>
Expand Down
Loading