Skip to content

Commit

Permalink
PAYARA-848 Notification target fix (payara#1026)
Browse files Browse the repository at this point in the history
* Fixed "notification-configure" command

* Fixed "notification-configure-notifier" command

* Fixed Typo

* PAYARA-848 Notification target Fix for asadmin commands
  • Loading branch information
smillidge authored and lprimak committed Jul 5, 2017
1 parent 3411274 commit 782fca7
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ and Distribution License("CDDL") (collectively, the "License"). You
*/
package fish.payara.nucleus.notification.admin;

import com.sun.enterprise.config.serverbeans.Config;
import com.sun.enterprise.config.serverbeans.Domain;
import com.sun.enterprise.util.LocalStringManagerImpl;
import com.sun.enterprise.util.SystemPropertyConstants;
import fish.payara.nucleus.notification.NotificationService;
import fish.payara.nucleus.notification.configuration.NotificationServiceConfiguration;
import org.glassfish.api.ActionReport;
import org.glassfish.api.I18n;
import org.glassfish.api.Param;
Expand All @@ -32,38 +30,25 @@ and Distribution License("CDDL") (collectively, the "License"). You
import org.glassfish.hk2.api.PerLookup;
import org.glassfish.internal.api.Target;
import org.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.config.ConfigSupport;
import org.jvnet.hk2.config.SingleConfigCode;
import org.jvnet.hk2.config.TransactionFailure;

import javax.inject.Inject;
import java.beans.PropertyVetoException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
* Admin command to enable/disable all notification services defined in
* domain.xml.
*
* @author mertcaliskan
* @author Susan Rai
*/
@Service(name = "notification-configure-das")
@Service(name = "__enable-notification-configure-das")
@PerLookup
@CommandLock(CommandLock.LockType.NONE)
@I18n("notification.configure.das")
@I18n("__enable-notification-configure-das")
@ExecuteOn({RuntimeType.DAS})
@TargetType(value = {CommandTarget.DAS, CommandTarget.STANDALONE_INSTANCE, CommandTarget.CLUSTER, CommandTarget.CLUSTERED_INSTANCE, CommandTarget.CONFIG})
@RestEndpoints({
@RestEndpoint(configBean = Domain.class,
opType = RestEndpoint.OpType.POST,
path = "notification-configure-das",
description = "Enables/Disables Notification Service")
})
public class NotificationConfigurerDas implements AdminCommand {
public class EnableNotificationConfigurerOnDas implements AdminCommand {

final private static LocalStringManagerImpl strings = new LocalStringManagerImpl(NotificationConfigurerDas.class);
final private static LocalStringManagerImpl strings = new LocalStringManagerImpl(EnableNotificationConfigurerOnDas.class);

@Inject
NotificationService service;
Expand All @@ -73,10 +58,7 @@ public class NotificationConfigurerDas implements AdminCommand {

@Inject
protected Target targetUtil;

@Param(name = "dynamic", optional = true, defaultValue = "false")
protected Boolean dynamic;


@Param(name = "target", optional = true, defaultValue = SystemPropertyConstants.DAS_SERVER_NAME)
String target;

Expand All @@ -92,37 +74,6 @@ public void execute(AdminCommandContext context) {
actionReport.setExtraProperties(extraProperties);
}

Config config = targetUtil.getConfig(target);
final NotificationServiceConfiguration notificationServiceConfiguration = config.getExtensionByType(NotificationServiceConfiguration.class);

if (notificationServiceConfiguration != null) {
try {
ConfigSupport.apply(new SingleConfigCode<NotificationServiceConfiguration>() {
@Override
public Object run(final NotificationServiceConfiguration notificationServiceConfigurationProxy) throws
PropertyVetoException, TransactionFailure {
if (enabled != null) {
notificationServiceConfigurationProxy.enabled(enabled.toString());
}
actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
return notificationServiceConfigurationProxy;
}
}, notificationServiceConfiguration);
}
catch(TransactionFailure ex){
logger.log(Level.WARNING, "Exception during command ", ex);
actionReport.setMessage(ex.getCause().getMessage());
actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
}

if (dynamic) {
enableOnTarget(actionReport, context, enabled);
}
}

private void enableOnTarget(ActionReport actionReport, AdminCommandContext context, Boolean enabled) {
if (enabled != null) {
service.getExecutionOptions().setEnabled(enabled);
actionReport.appendMessage(strings.getLocalString("notification.configure.status.success",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
Copyright (c) 2016 C2B2 Consulting Limited. All rights reserved.
The contents of this file are subject to the terms of the Common Development
and Distribution License("CDDL") (collectively, the "License"). You
may not use this file except in compliance with the License. You can
obtain a copy of the License at
https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
or packager/legal/LICENSE.txt. See the License for the specific
language governing permissions and limitations under the License.
When distributing the software, include this License Header Notice in each
file and include the License file at packager/legal/LICENSE.txt.
*/
package fish.payara.nucleus.notification.admin;

import com.sun.enterprise.config.serverbeans.Domain;
import com.sun.enterprise.util.LocalStringManagerImpl;
import com.sun.enterprise.util.SystemPropertyConstants;
import fish.payara.nucleus.notification.NotificationService;
import org.glassfish.api.ActionReport;
import org.glassfish.api.I18n;
import org.glassfish.api.Param;
import org.glassfish.api.admin.*;
import org.glassfish.config.support.CommandTarget;
import org.glassfish.config.support.TargetType;
import org.glassfish.hk2.api.PerLookup;
import org.glassfish.internal.api.Target;
import org.jvnet.hk2.annotations.Service;

import javax.inject.Inject;
import java.util.Properties;
import java.util.logging.Logger;


/**
*
* @author Susan Rai
*/
@Service(name = "__enable-notification-configure-instance")
@PerLookup
@CommandLock(CommandLock.LockType.NONE)
@I18n("__enable-notification-configure-instance")
@ExecuteOn({RuntimeType.INSTANCE})
@TargetType(value = {CommandTarget.DAS, CommandTarget.STANDALONE_INSTANCE, CommandTarget.CLUSTER, CommandTarget.CLUSTERED_INSTANCE, CommandTarget.CONFIG})
public class EnableNotificationConfigurerOnInstance implements AdminCommand {

final private static LocalStringManagerImpl strings = new LocalStringManagerImpl(EnableNotificationConfigurerOnInstance.class);

@Inject
NotificationService service;

@Inject
protected Logger logger;

@Inject
protected Target targetUtil;

@Param(name = "target", optional = true, defaultValue = SystemPropertyConstants.DAS_SERVER_NAME)
String target;

@Param(name = "enabled", optional = false)
private Boolean enabled;

@Override
public void execute(AdminCommandContext context) {
final ActionReport actionReport = context.getActionReport();
Properties extraProperties = actionReport.getExtraProperties();
if (extraProperties == null) {
extraProperties = new Properties();
actionReport.setExtraProperties(extraProperties);
}

if (enabled != null) {
service.getExecutionOptions().setEnabled(enabled);
actionReport.appendMessage(strings.getLocalString("notification.configure.status.success",
"Notification service status is set to {0}.", enabled) + "\n");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,19 @@ and Distribution License("CDDL") (collectively, the "License"). You
import java.util.logging.Level;
import java.util.logging.Logger;


/**
* Admin command to enable/disable specific notifier given with its name
*
* @author mertcaliskan
* @author Susan Rai
*/
@ExecuteOn({RuntimeType.DAS})
@TargetType(value = {CommandTarget.DAS, CommandTarget.STANDALONE_INSTANCE, CommandTarget.CLUSTER, CommandTarget.CLUSTERED_INSTANCE, CommandTarget.CONFIG})
@Service(name = "notification-configure-notifier-das")
@Service(name = "__enable-notification-configure-notifier-das")
@CommandLock(CommandLock.LockType.NONE)
@PerLookup
@I18n("notification.configure.notifier.das")
@RestEndpoints({
@RestEndpoint(configBean = Domain.class,
opType = RestEndpoint.OpType.POST,
path = "notification-configure-notifier-das",
description = "Enables/Disables Notifier Specified With Name")
})
public class NotificationNotifierConfigurerDas implements AdminCommand {
@I18n("__enable-notification-configure-notifier-das")
public class EnableNotificationNotifierConfigurerOnDas implements AdminCommand {

final private static LocalStringManagerImpl strings = new LocalStringManagerImpl(NotificationNotifierConfigurerDas.class);
final private static LocalStringManagerImpl strings = new LocalStringManagerImpl(EnableNotificationNotifierConfigurerOnDas.class);

@Inject
NotificationService service;
Expand All @@ -89,9 +81,6 @@ public class NotificationNotifierConfigurerDas implements AdminCommand {
@Inject
private NotificationEventBus eventBus;

@Param(name = "dynamic", optional = true, defaultValue = "false")
protected Boolean dynamic;

@Param(name = "target", optional = true, defaultValue = SystemPropertyConstants.DAS_SERVER_NAME)
String target;

Expand Down Expand Up @@ -131,25 +120,18 @@ public void execute(AdminCommandContext context) {
public Object run(final NotificationServiceConfiguration notificationServiceConfigurationProxy) throws
PropertyVetoException, TransactionFailure {
NotifierConfiguration notifierProxy = (NotifierConfiguration) notificationServiceConfigurationProxy.createChild(notifierService.getNotifierConfigType());
if (notifierEnabled != null) {
notifierProxy.enabled(notifierEnabled);
}
createdNotifier[0] = notifierProxy;

List<NotifierConfiguration> notifierConfigList = notificationServiceConfigurationProxy.getNotifierConfigurationList();
NotifierConfigurationExecutionOptions executionOptions = factory.build(createdNotifier[0]);
if (notifierEnabled) {
notifierConfigList.add(createdNotifier[0]);
if (dynamic) {
service.getExecutionOptions().addNotifierConfigurationExecutionOption(executionOptions);
eventBus.register(notifierService);
}
service.getExecutionOptions().addNotifierConfigurationExecutionOption(executionOptions);
eventBus.register(notifierService);
} else {
notifierConfigList.remove(createdNotifier[0]);
if (dynamic) {
service.getExecutionOptions().removeNotifierConfigurationExecutionOption(executionOptions);
eventBus.unregister(notifierService);
}
service.getExecutionOptions().removeNotifierConfigurationExecutionOption(executionOptions);
eventBus.unregister(notifierService);
}

actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
Expand All @@ -161,19 +143,13 @@ public Object run(final NotificationServiceConfiguration notificationServiceConf
@Override
public Object run(final NotifierConfiguration notifierProxy) throws
PropertyVetoException, TransactionFailure {
if (notifierEnabled != null) {
notifierProxy.enabled(notifierEnabled);
}

if (dynamic) {
NotifierConfigurationExecutionOptions executionOptions = factory.build(notifierProxy);
if (notifierEnabled) {
service.getExecutionOptions().addNotifierConfigurationExecutionOption(executionOptions);
eventBus.register(notifierService);
} else {
service.getExecutionOptions().removeNotifierConfigurationExecutionOption(executionOptions);
eventBus.unregister(notifierService);
}
NotifierConfigurationExecutionOptions executionOptions = factory.build(notifierProxy);
if (notifierEnabled) {
service.getExecutionOptions().addNotifierConfigurationExecutionOption(executionOptions);
eventBus.register(notifierService);
} else {
service.getExecutionOptions().removeNotifierConfigurationExecutionOption(executionOptions);
eventBus.unregister(notifierService);
}

actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
Expand Down
Loading

0 comments on commit 782fca7

Please sign in to comment.