Skip to content

Commit

Permalink
Extract log, properties, rest-api and server into commons (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasponce authored and jshaughn committed May 30, 2017
1 parent 29bab1f commit 71652f3
Show file tree
Hide file tree
Showing 91 changed files with 1,586 additions and 1,612 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@
import org.hawkular.alerts.actions.api.Sender;
import org.hawkular.alerts.api.services.ActionsService;
import org.hawkular.alerts.engine.StandaloneAlerts;
import org.hawkular.alerts.log.MsgLogger;
import org.hawkular.commons.log.MsgLogger;
import org.hawkular.commons.log.MsgLogging;

/**
* Helper class to find the classes annotated with ActionPlugin and instantiate them.
*
* @author Lucas Ponce
*/
public class ActionPlugins {
private static final MsgLogger log = MsgLogger.getLogger(ActionPlugins.class);
private static final MsgLogger log = MsgLogging.getMsgLogger(ActionPlugins.class);
private ActionsService actions;
private static ActionPlugins instance;
private Map<String, ActionPluginListener> plugins;
Expand Down Expand Up @@ -87,7 +88,7 @@ private void scan() throws IOException {
Plugin plugin = (Plugin)clazz.getAnnotation(Plugin.class);
String name = plugin.name();
Object newInstance = clazz.newInstance();
log.info("Scanning {}", clazz.getName());
log.infof("Scanning %s", clazz.getName());
if (newInstance instanceof ActionPluginListener) {
ActionPluginListener pluginInstance = (ActionPluginListener)newInstance;
injectActionPluginSender(name, pluginInstance);
Expand All @@ -98,7 +99,7 @@ private void scan() throws IOException {
}
}
} catch (Exception e) {
log.error("Error loading Handler {}. Reason: {}", className, e.toString());
log.errorf("Error loading Handler %s. Reason: %s", className, e.toString());
System.exit(1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@
import org.hawkular.alerts.api.services.ActionListener;
import org.hawkular.alerts.api.services.DefinitionsService;
import org.hawkular.alerts.engine.StandaloneAlerts;
import org.hawkular.alerts.log.MsgLogger;
import org.hawkular.alerts.log.AlertingLogger;
import org.hawkular.commons.log.MsgLogging;

/**
* Main standalone listener for plugins implementation.
*
* @author Lucas Ponce
*/
public class StandaloneActionPluginListener implements ActionListener {
private static final MsgLogger log = MsgLogger.getLogger(StandaloneActionPluginRegister.class);
private static final AlertingLogger log = MsgLogging.getMsgLogger(AlertingLogger.class, StandaloneActionPluginRegister.class);

private DefinitionsService definitions;

Expand Down Expand Up @@ -65,8 +66,7 @@ public void process(Action action) {
final ActionPluginListener plugin = plugins.get(actionPlugin);
if (plugin == null) {
if (log.isDebugEnabled()) {
log.debug("Received action [" + actionPlugin +
"] but no ActionPluginListener found on this deployment");
log.debugf("Received action [%s] but no ActionPluginListener found on this deployment", actionPlugin);
}
return;
}
Expand All @@ -77,13 +77,13 @@ public void process(Action action) {
try {
plugin.process(pluginMessage);
} catch (Exception e) {
log.debug("Error processing action: " + action.getActionPlugin(), e);
log.debugf("Error processing action: %s", action.getActionPlugin(), e);
log.errorProcessingAction(e.getMessage());
}
});
}
} catch (Exception e) {
log.debug("Error processing action: " + action.getActionPlugin(), e);
log.debugf("Error processing action: %s", action.getActionPlugin(), e);
log.errorProcessingAction(e.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@
import org.hawkular.alerts.api.services.ActionsService;
import org.hawkular.alerts.api.services.DefinitionsService;
import org.hawkular.alerts.engine.StandaloneAlerts;
import org.hawkular.alerts.log.MsgLogger;
import org.hawkular.alerts.log.AlertingLogger;
import org.hawkular.commons.log.MsgLogging;

/**
* Main standalone register for plugins implementations
*
* @author Lucas Ponce
*/
public class StandaloneActionPluginRegister {
private static final MsgLogger log = MsgLogger.getLogger(StandaloneActionPluginRegister.class);
private static final AlertingLogger log = MsgLogging.getMsgLogger(AlertingLogger.class, StandaloneActionPluginRegister.class);

private static StandaloneActionPluginRegister instance;
private static ExecutorService executor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import org.hawkular.alerts.api.json.JsonUtil;
import org.hawkular.alerts.api.model.action.Action;
import org.hawkular.alerts.api.services.ActionsService;
import org.hawkular.alerts.log.MsgLogger;
import org.hawkular.alerts.log.AlertingLogger;
import org.hawkular.commons.log.MsgLogging;

/**
* Main standalone sender for plugins implementations
Expand All @@ -31,7 +32,7 @@
* @author Lucas Ponce
*/
public class StandaloneActionPluginSender implements ActionPluginSender {
private final MsgLogger log = MsgLogger.getLogger(StandaloneActionPluginSender.class);
private final AlertingLogger log = MsgLogging.getMsgLogger(AlertingLogger.class, StandaloneActionPluginRegister.class);

private ActionsService actions;

Expand All @@ -49,12 +50,12 @@ public ActionResponseMessage createMessage(Operation operation) {

@Override
public void send(ActionResponseMessage msg) throws Exception {
log.debug("Message received: {}", msg);
log.debugf("Message received: %s", msg);
if (msg != null && msg.getPayload().containsKey("action")) {
String jsonAction = msg.getPayload().get("action");
Action updatedAction = JsonUtil.fromJson(jsonAction, Action.class);
actions.updateResult(updatedAction);
log.debug("Operation message received from plugin [{}] with payload [{}]",
log.debugf("Operation message received from plugin [%s] with payload [%s]",
updatedAction.getActionPlugin(), updatedAction.getResult());
} else {
log.warnActionResponseMessageWithoutPayload();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
import org.hawkular.alerts.api.model.event.Alert;
import org.hawkular.alerts.api.model.event.Alert.Status;
import org.hawkular.alerts.api.model.event.Event;
import org.hawkular.alerts.log.MsgLogger;
import org.hawkular.alerts.log.AlertingLogger;
import org.hawkular.commons.log.MsgLogging;

/**
* Action Email plugin.
Expand All @@ -57,6 +58,8 @@
*/
@Plugin(name = "email")
public class EmailPlugin implements ActionPluginListener {
private final AlertingLogger log = MsgLogging.getMsgLogger(AlertingLogger.class, EmailPlugin.class);

public static final String PLUGIN_NAME = "email";

/**
Expand Down Expand Up @@ -188,7 +191,7 @@ public class EmailPlugin implements ActionPluginListener {
*/
public static final String PROP_TEMPLATE_HTML = "template.html";

private final MsgLogger log = MsgLogger.getLogger(EmailPlugin.class);


Map<String, String> defaultProperties = new HashMap<>();

Expand Down Expand Up @@ -309,7 +312,7 @@ protected Message createMimeMessage(ActionMessage msg) throws Exception {

Map<String, String> props = msg.getAction().getProperties();
if (null == props || props.isEmpty()) {
log.warn("Properties empty on plugin {}.", PLUGIN_NAME);
log.warnf("Properties empty on plugin %s.", PLUGIN_NAME);
}
Event event = msg.getAction() != null ? msg.getAction().getEvent() : null;
Alert alert = null != event && (event instanceof Alert) ? (Alert) event : null;
Expand Down Expand Up @@ -367,9 +370,7 @@ protected Message createMimeMessage(ActionMessage msg) throws Exception {
if (null != subject && !subject.isEmpty()) {
email.setSubject(subject);
} else {
if (log.isDebugEnabled()) {
log.debug("Subject not found processing email on message: " + msg);
}
log.debugf("Subject not found processing email on message: %s", msg);
}

String plain = emailProcessed.get("emailBodyPlain");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
import java.util.Map;

import org.hawkular.alerts.actions.api.ActionMessage;
import org.hawkular.alerts.log.MsgLogger;
import org.hawkular.commons.log.MsgLogger;
import org.hawkular.commons.log.MsgLogging;

import freemarker.template.Configuration;
import freemarker.template.Template;
Expand Down Expand Up @@ -79,7 +80,7 @@
* @author Lucas Ponce
*/
public class EmailTemplate {
private final MsgLogger log = MsgLogger.getLogger(EmailTemplate.class);
private final MsgLogger log = MsgLogging.getMsgLogger(EmailTemplate.class);

public static final String DEFAULT_TEMPLATE_PLAIN = "template.plain.default.ftl";
public static final String DEFAULT_TEMPLATE_HTML = "template.html.default.ftl";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@
import org.hawkular.alerts.actions.api.ActionPluginListener;
import org.hawkular.alerts.actions.api.ActionPluginSender;
import org.hawkular.alerts.actions.api.ActionResponseMessage;
import org.hawkular.alerts.log.MsgLogger;
import org.hawkular.alerts.log.AlertingLogger;
import org.hawkular.alerts.actions.api.Plugin;
import org.hawkular.alerts.actions.api.Sender;
import org.hawkular.alerts.api.json.JsonUtil;
import org.hawkular.alerts.api.model.action.Action;
import org.hawkular.alerts.api.model.event.Event;
import org.hawkular.commons.log.MsgLogging;

import com.fasterxml.jackson.databind.ObjectMapper;

Expand All @@ -43,7 +44,7 @@
*/
@Plugin(name = "file")
public class FilePlugin implements ActionPluginListener {
private final MsgLogger log = MsgLogger.getLogger(FilePlugin.class);
private final AlertingLogger log = MsgLogging.getMsgLogger(AlertingLogger.class, FilePlugin.class);

private Map<String, String> defaultProperties = new HashMap<>();
private ObjectMapper objectMapper;
Expand Down
23 changes: 23 additions & 0 deletions commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,36 @@
<name>Hawkular Alerting: Commons</name>

<dependencies>

<dependency>
<groupId>org.hawkular.commons</groupId>
<artifactId>hawkular-commons-utils</artifactId>
<version>${version.org.hawkular.commons}</version>
</dependency>

<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-core</artifactId>
<scope>provided</scope>
<version>${version.org.infinispan.wildfly}</version>
</dependency>

<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>${version.org.jboss.logging}</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-annotations</artifactId>
<version>${version.org.jboss.logging.jboss-logging-tools}</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-processor</artifactId>
<version>${version.org.jboss.logging.jboss-logging-tools}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.hawkular.alerts.cache;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.hawkular.alerts.log.MsgLogger;
import org.hawkular.alerts.properties.AlertProperties;
import org.hawkular.commons.log.MsgLogger;
import org.hawkular.commons.log.MsgLogging;
import org.hawkular.commons.properties.HawkularProperties;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager;

Expand All @@ -14,9 +18,10 @@
* @author Lucas Ponce
*/
public class IspnCacheManager {
private static final MsgLogger log = MsgLogger.getLogger(IspnCacheManager.class);
private static final String ISPN_CONFIG_DISTRIBUTED = "/alerting-distributed.xml";
private static final String ISPN_CONFIG_LOCAL = "/alerting-local.xml";
private static final MsgLogger log = MsgLogging.getMsgLogger(IspnCacheManager.class);
private static final String CONFIG_PATH = "hawkular.configuration";
private static final String ISPN_CONFIG_DISTRIBUTED = "alerting-distributed.xml";
private static final String ISPN_CONFIG_LOCAL = "alerting-local.xml";
private static final String ALERTS_DISTRIBUTED = "hawkular-alerts.distributed";
private static final String ALERTS_DISTRIBUTED_ENV = "HAWKULAR_ALERTS_DISTRIBUTED";
private static final String ALERTS_DISTRIBUTED_DEFAULT = "false";
Expand All @@ -38,10 +43,20 @@ public static boolean isDistributed() {
private static synchronized void init() {
if (cacheManager == null) {
try {
distributed = Boolean.valueOf(AlertProperties.getProperty(ALERTS_DISTRIBUTED, ALERTS_DISTRIBUTED_ENV,
distributed = Boolean.valueOf(HawkularProperties.getProperty(ALERTS_DISTRIBUTED, ALERTS_DISTRIBUTED_ENV,
ALERTS_DISTRIBUTED_DEFAULT));
cacheManager = new DefaultCacheManager(IspnCacheManager.class
.getResourceAsStream(distributed ? ISPN_CONFIG_DISTRIBUTED : ISPN_CONFIG_LOCAL));
String configPath = System.getProperty(CONFIG_PATH);
InputStream is = null;
if (configPath != null) {
File configFile = new File(configPath, distributed ? ISPN_CONFIG_DISTRIBUTED : ISPN_CONFIG_LOCAL);
if (configFile.exists() && configFile.isDirectory()) {
is = new FileInputStream(configFile);
}
}
if (is == null) {
is = IspnCacheManager.class.getResourceAsStream("/" + (distributed ? ISPN_CONFIG_DISTRIBUTED : ISPN_CONFIG_LOCAL));
}
cacheManager = new DefaultCacheManager(is);
} catch (IOException e) {
log.error(e);
}
Expand Down

0 comments on commit 71652f3

Please sign in to comment.