Skip to content

Commit

Permalink
GH24992 Admin Logger: Moved event class to internal API module
Browse files Browse the repository at this point in the history
So that it can be used in any module.
Added documentation on writing listeners.
  • Loading branch information
OndroMih committed Jun 22, 2024
1 parent 607a7f2 commit e969cab
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
import java.util.Optional;
import java.util.stream.Stream;
import javax.security.auth.Subject;
import org.glassfish.admin.rest.events.CommandInvokedEvent;
import org.glassfish.api.StartupRunLevel;
import org.glassfish.api.admin.ParameterMap;
import org.glassfish.config.support.TranslatedConfigView;
import org.glassfish.hk2.api.messaging.MessageReceiver;
import org.glassfish.hk2.api.messaging.SubscribeTo;
import org.glassfish.hk2.runlevel.RunLevel;
import org.glassfish.internal.api.events.CommandInvokedEvent;
import org.glassfish.security.common.UserPrincipal;
import org.jvnet.hk2.annotations.Service;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
import jakarta.ws.rs.core.*;
import jakarta.ws.rs.core.Response.ResponseBuilder;
import org.glassfish.admin.rest.RestLogging;
import org.glassfish.admin.rest.events.CommandInvokedEvent;
import org.glassfish.admin.rest.events.InvokeEventService;
import org.glassfish.admin.rest.utils.SseCommandHelper;
import org.glassfish.api.ActionReport;
import org.glassfish.api.admin.*;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.internal.api.Globals;
import org.glassfish.internal.api.events.CommandInvokedEvent;
import org.glassfish.internal.api.events.InvokeEventService;
import org.glassfish.jersey.internal.util.collection.Ref;
import org.glassfish.jersey.media.sse.SseFeature;

Expand Down Expand Up @@ -360,7 +360,7 @@ private Response executeCommand(CommandName commandName, Payload.Inbound inbound
}
InvokeEventService.get()
.getCommandInvokedTopic()
.publish(new CommandInvokedEvent(commandName.getName(), params, getSubject()));
.publish(new CommandInvokedEvent(commandName.getName(), params, getSubject(), this.getClass().getSimpleName()));
commandInvocation.outbound(outbound).parameters(params).execute();
ar = (ActionReporter) commandInvocation.report();
fixActionReporterSpecialCases(ar);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@
import static org.glassfish.admin.rest.utils.Util.getHtml;
import static org.glassfish.admin.rest.utils.Util.methodNameFromDtdName;

import org.glassfish.admin.rest.events.CommandInvokedEvent;
import org.glassfish.admin.rest.events.InvokeEventService;
import org.glassfish.internal.api.events.CommandInvokedEvent;
import org.glassfish.internal.api.events.InvokeEventService;



/**
Expand Down Expand Up @@ -227,7 +228,7 @@ public static RestActionReporter runCommand(String commandName, ParameterMap par

InvokeEventService.get()
.getCommandInvokedTopic()
.publish(new CommandInvokedEvent(commandName, parameters, subject));
.publish(new CommandInvokedEvent(commandName, parameters, subject, ResourceUtil.class.getSimpleName()));

CommandRunner cr = Globals.getDefaultHabitat().getService(CommandRunner.class);
RestActionReporter ar = new RestActionReporter();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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.internal.api.events;

import javax.security.auth.Subject;
import org.glassfish.api.admin.ParameterMap;
import org.glassfish.hk2.api.messaging.MessageReceiver;

/**
* <p>
* An HK2 event that is triggered when an Admin command is invoked via the REST interface (including Admin Console and CLI).
* </p>
* <p>
* To listen to this event, create an HK2 subscriber with the following:
* </p>
*
* <ul>
* <li>{@link Service} annotation on the class or otherwise register the class as an HK2 service
* </li>
* <li>{@link MessageReceiver} qualifier annotation on the class with the {@link CommandInvokedEvent} message type
* </li>
* <li>A listener method with void return type and a single argument of the {@link CommandInvokedEvent} type annotated with the {@link SubscribeTo} annotation
* </li>
* <li>Make sure that this subscriber is started as an HK2 service to receive the events
* </li>
* </ul>
*
* <p>
* An example listener:
* </p>
*
* <pre>
* {@code
@Service
@RunLevel(value = StartupRunLevel.VAL, mode = RunLevel.RUNLEVEL_MODE_NON_VALIDATING)
@MessageReceiver({CommandInvokedEvent.class})
public class CommandSubscriber {
public void commandInvoked(@SubscribeTo CommandInvokedEvent event) {
log(event);
}
}
* }
* </pre>
*
* @author Ondro Mihalyi
*/
public class CommandInvokedEvent {

public CommandInvokedEvent(String commandName, ParameterMap parameters, Subject subject, String source) {
this.commandName = commandName;
this.parameters = parameters;
this.subject = subject;
this.source = source;
}

private final String commandName;
private final ParameterMap parameters;
private final Subject subject;
private final String source;

public String getCommandName() {
return commandName;
}

public ParameterMap getParameters() {
return parameters;
}

public Subject getSubject() {
return subject;
}

public String getSource() {
return source;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.admin.rest.events;
package org.glassfish.internal.api.events;

import jakarta.inject.Inject;
import org.glassfish.hk2.api.messaging.Topic;
import org.glassfish.internal.api.Globals;
import org.jvnet.hk2.annotations.Service;

/**
* <p>
* A service to retrieve the correct HK2 topic to invoke global events.
* </p>
* <p>
* Do not inject this service. Instead, retrieve it using {@link InvokeEventService#get()}, which returns a service registered with the global service locator. This is needed to trigger events that can be received by any subscriber registered globally.
* </p>
*
* @author Ondro Mihalyi
*/
Expand Down

0 comments on commit e969cab

Please sign in to comment.