Skip to content

Commit

Permalink
feat: Added Google Guice ActionHooks support (#113)
Browse files Browse the repository at this point in the history
Refs: #106
  • Loading branch information
nirikash authored Sep 30, 2024
1 parent a3c0973 commit 1d7e6af
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 7 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ Use Administration -> Interceptor Manager -> Settings page to enable/disable or
#### Standalone jar hooks
Copy hook jar to hooks folder (`<polarion_home>/polarion/extensions/ch.sbb.polarion.extension.interceptor-manager/eclipse/plugins/hooks`) and enforce hooks reloading from the settings page or restart Polarion.
By default, newly added hooks are disabled and must be enabled manually.
#### Hooks from Another Polarion Extension
Hooks can be located in another Polarion extension. In this case, the extension should register the hooks as an OSGi service using the IActionHook interface.
For more details, refer to the example at [Delete Non-resolved Module Comments as OSGi Service](https://github.com/SchweizerischeBundesbahnen/ch.sbb.polarion.extension.interceptor-manager.hook-samples/tree/main/hook-samples-osgi).
#### Hooks from Different Polarion Extension
Hooks can be located in another Polarion extension. In this case, the following two approaches to register the hooks are supported:
- OSGi Services, illustrated in example: [Delete Non-resolved Module Comments as OSGi Service](https://github.com/SchweizerischeBundesbahnen/ch.sbb.polarion.extension.interceptor-manager.hook-samples/tree/main/hook-samples-osgi)
- Google Guice, illustrated in example: [Delete Non-resolved Module Comments as Guice Module](https://github.com/SchweizerischeBundesbahnen/ch.sbb.polarion.extension.interceptor-manager.hook-samples/tree/main/hook-samples-guice)

## Upgrade

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ch.sbb.polarion.extension.interceptor_manager.guice;

import ch.sbb.polarion.extension.interceptor_manager.model.IActionHook;
import com.google.inject.Inject;
import com.polarion.core.util.logging.Logger;
import com.polarion.platform.guice.internal.GuicePlatform;
import lombok.Getter;

import java.util.HashSet;
import java.util.Set;

@Getter
public class GuiceActionHooksFactory {
private static final Logger logger = Logger.getLogger(GuiceActionHooksFactory.class);
private final Set<IActionHook> actionHooks = new HashSet<>();

private GuiceActionHooksFactory() {}

public static GuiceActionHooksFactory build() {
GuiceActionHooksFactory actionHooksFactory = new GuiceActionHooksFactory();
GuicePlatform.tryInjectMembers(actionHooksFactory);
return actionHooksFactory;
}

@Inject
@SuppressWarnings("unused")
public void setActionHooks(Set<IActionHook> actionHooks) {
if (actionHooks != null) {
this.actionHooks.addAll(actionHooks);
logger.info("Added guice action hooks: " + actionHooks.size());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ch.sbb.polarion.extension.interceptor_manager.guice;

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

public class GuiceModule extends AbstractModule {

@Override
protected void configure() {
Multibinder.newSetBinder(binder(), IActionHook.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package ch.sbb.polarion.extension.interceptor_manager.model;

import ch.sbb.polarion.extension.generic.settings.NamedSettingsRegistry;
import ch.sbb.polarion.extension.interceptor_manager.guice.GuiceActionHooksFactory;
import ch.sbb.polarion.extension.interceptor_manager.osgi.OSGiUtils;
import ch.sbb.polarion.extension.interceptor_manager.settings.HookSettings;
import ch.sbb.polarion.extension.interceptor_manager.util.HookJarUtils;
import ch.sbb.polarion.extension.interceptor_manager.osgi.OSGiUtils;
import com.polarion.core.util.logging.Logger;

import java.util.ArrayList;
Expand All @@ -22,6 +23,7 @@ public synchronized void refresh() {
hooks.clear();
hooks.addAll(HookJarUtils.loadHooks());
hooks.addAll(OSGiUtils.lookupOSGiService(IActionHook.class));
hooks.addAll(GuiceActionHooksFactory.build().getActionHooks());
logger.info(hooks.size() + " hooks loaded.");
NamedSettingsRegistry.INSTANCE.getAll().clear();
hooks.forEach(hook -> NamedSettingsRegistry.INSTANCE.getAll().add(new HookSettings(hook)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ch.sbb.polarion.extension.interceptor_manager.osgi;

import ch.sbb.polarion.extension.interceptor_manager.model.IActionHook;
import ch.sbb.polarion.extension.interceptor_manager.model.HooksRegistry;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;
Expand All @@ -13,7 +12,6 @@ public class HooksBundleActivator implements BundleActivator {
public void start(BundleContext bundleContext) {
serviceTracker = new ServiceTracker<>(bundleContext, IActionHook.class, new HooksServiceTrackerCustomizer(bundleContext));
serviceTracker.open();
HooksRegistry.HOOKS.refresh();
}

@Override
Expand All @@ -22,5 +20,4 @@ public void stop(BundleContext bundleContext) {
serviceTracker.close();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ch.sbb.polarion.extension.generic.rest.GenericRestApplication;
import ch.sbb.polarion.extension.generic.rest.controller.NamedSettingsApiController;
import ch.sbb.polarion.extension.interceptor_manager.model.HooksRegistry;
import ch.sbb.polarion.extension.interceptor_manager.rest.controller.HooksApiController;
import ch.sbb.polarion.extension.interceptor_manager.rest.controller.HooksInternalController;
import ch.sbb.polarion.extension.interceptor_manager.rest.controller.HooksSettingsApiController;
Expand All @@ -13,6 +14,11 @@

public class InterceptorManagerRestApplication extends GenericRestApplication {

public InterceptorManagerRestApplication() {
// Refresh actions hooks registry to process guice hooks after Google Guice platform was initialized
HooksRegistry.HOOKS.refresh();
}

@Override
protected @NotNull Set<Object> getExtensionControllerSingletons() {
return Set.of(
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Bundle-Name: Interceptor Manager Extension for Polarion ALM
Bundle-Activator: ch.sbb.polarion.extension.interceptor_manager.osgi.HooksBundleActivator
Bundle-ActivationPolicy: lazy
Require-Bundle: com.polarion.portal.tomcat,
com.polarion.platform.guice,
com.polarion.alm.ui,
com.polarion.alm.tracker,
org.glassfish.jersey,
Expand All @@ -13,6 +14,7 @@ Require-Bundle: com.polarion.portal.tomcat,
org.apache.commons.logging,
org.apache.hivemind,
slf4j.api
Guice-Modules: ch.sbb.polarion.extension.interceptor_manager.guice.GuiceModule
Import-Package: org.osgi.framework,
org.osgi.util.tracker
Export-Package: ch.sbb.polarion.extension.interceptor_manager,
Expand Down

0 comments on commit 1d7e6af

Please sign in to comment.