Skip to content

Commit

Permalink
GH24992 Admin Logger: Temporary HK2 event service
Browse files Browse the repository at this point in the history
Until the HK2 extras OSGi manifest is fixed
  • Loading branch information
OndroMih committed Jun 22, 2024
1 parent 351803d commit 025fb1c
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
import org.glassfish.hk2.api.PostConstruct;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.api.TypeLiteral;
import org.glassfish.hk2.extras.ExtrasUtilities;
import org.glassfish.hk2.utilities.Binder;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.hk2util.SimpleTopicDistributionService;
import org.glassfish.internal.api.InternalSystemAdministrator;
import org.glassfish.internal.api.ServerContext;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer;
Expand Down Expand Up @@ -142,8 +142,13 @@ private JerseyContainer getJerseyContainer(ResourceConfig config) {
() -> this + ": Creating Jersey container for " + HttpHandler.class + " and " + config);
final GrizzlyHttpContainer httpHandler = ContainerFactory.createContainer(GrizzlyHttpContainer.class, config);
final ServiceLocator jerseyLocator = httpHandler.getApplicationHandler().getInjectionManager().getInstance(ServiceLocator.class);
ExtrasUtilities.enableTopicDistribution(jerseyLocator);
return new JerseyContainer() {
/*
We enable a temporary distribution service until the HK2 Extras package is fixed so that we can enable
the topic distribution service provided by HK2.
*/
//ExtrasUtilities.enableTopicDistribution(jerseyLocator);
SimpleTopicDistributionService.enable(jerseyLocator);
return new JerseyContainer() {
@Override
public void service(Request request, Response response) throws Exception {
httpHandler.service(request, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@
import org.glassfish.hk2.api.PostConstruct;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.api.TypeLiteral;
import org.glassfish.hk2.extras.ExtrasUtilities;
import org.glassfish.hk2.utilities.Binder;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.hk2util.SimpleTopicDistributionService;
import org.glassfish.internal.api.AdminAccessController;
import org.glassfish.internal.api.RemoteAdminAccessException;
import org.glassfish.internal.api.ServerContext;
Expand Down Expand Up @@ -282,7 +282,12 @@ private JerseyContainer getJerseyContainer(ResourceConfig config) {
() -> this + ": Creating Jersey container for " + HttpHandler.class + " and " + config);
final GrizzlyHttpContainer httpHandler = ContainerFactory.createContainer(GrizzlyHttpContainer.class, config);
final ServiceLocator jerseyLocator = httpHandler.getApplicationHandler().getInjectionManager().getInstance(ServiceLocator.class);
ExtrasUtilities.enableTopicDistribution(jerseyLocator);
/*
We enable a temporary distribution service until the HK2 Extras package is fixed so that we can enable
the topic distribution service provided by HK2.
*/
//ExtrasUtilities.enableTopicDistribution(jerseyLocator);
SimpleTopicDistributionService.enable(jerseyLocator);
return new JerseyContainer() {
@Override
public void service(Request request, Response response) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/package org.glassfish.hk2util;

import static java.lang.System.Logger.Level.WARNING;

import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.inject.Singleton;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import org.glassfish.hk2.api.IterableProvider;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.api.TypeLiteral;
import org.glassfish.hk2.api.messaging.MessageReceiver;
import org.glassfish.hk2.api.messaging.SubscribeTo;
import org.glassfish.hk2.api.messaging.Topic;
import org.glassfish.hk2.api.messaging.TopicDistributionService;
import org.glassfish.hk2.utilities.ServiceLocatorUtilities;
import org.jvnet.hk2.annotations.ContractsProvided;

/**
*
* @author Ondro Mihalyi
*/
@Singleton
@Named(SimpleTopicDistributionService.NAME)
@ContractsProvided({TopicDistributionService.class})
public class SimpleTopicDistributionService implements TopicDistributionService {

public static final String NAME = "SimpleTopicDistributionService";

@Inject
private IterableProvider<Object> receivers;

public static void enable(ServiceLocator locator) {
if (locator == null) {
throw new IllegalArgumentException();
}

if (locator.getService(SimpleTopicDistributionService.class, SimpleTopicDistributionService.NAME) != null) {
// Will not add it a second time
return;
}

ServiceLocatorUtilities.addClasses(locator, true, SimpleTopicDistributionService.class);
}

@Override
public void distributeMessage(Topic<?> topic, Object message) {
receivers.ofType(new TypeLiteral<MessageReceiver>() {
}.getType())
.forEach(receiver -> {
Arrays.stream(receiver.getClass().getMethods())
.filter(method -> {
final Parameter[] parameters = method.getParameters();
return methodReturnsVoid(method)
&& parameters.length == 1
&& method.getParameterTypes()[0].isInstance(message)
&& containsSubscribeToAnnotation(parameters[0].getAnnotations());
})
.forEach(method -> {
try {
method.invoke(receiver, message);
} catch (Exception ex) {
System.getLogger(SimpleTopicDistributionService.class.getName())
.log(WARNING, ex.getMessage(), ex);
}
});
});
}

private static boolean methodReturnsVoid(Method method) {
return method.getReturnType().equals(Void.TYPE);
}

private boolean containsSubscribeToAnnotation(Annotation[] annotations) {
for (Annotation annotation : annotations) {
if (SubscribeTo.class.isInstance(annotation)) {
return true;
}
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.glassfish.hk2.api.ServiceLocator;

import java.util.Properties;
import org.glassfish.hk2.extras.ExtrasUtilities;
import org.glassfish.hk2util.SimpleTopicDistributionService;

/**
* @author Sanjeeb.Sahoo@Sun.COM
Expand All @@ -39,7 +39,13 @@ public class GlassFishImpl implements GlassFish {
public GlassFishImpl(ModuleStartup gfKernel, ServiceLocator habitat, Properties gfProps) throws GlassFishException {
this.gfKernel = gfKernel;
this.habitat = habitat;
ExtrasUtilities.enableTopicDistribution(habitat);
/*
We enable a temporary distribution service until the HK2 Extras package is fixed so that we can enable
the topic distribution service provided by HK2.
*/
//ExtrasUtilities.enableTopicDistribution(habitat);
SimpleTopicDistributionService.enable(habitat);

configure(gfProps);
}

Expand Down

0 comments on commit 025fb1c

Please sign in to comment.