Skip to content

Commit

Permalink
Port Alerters architecture (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasponce authored and jshaughn committed Jun 30, 2017
1 parent a06d125 commit 32a6d1f
Show file tree
Hide file tree
Showing 37 changed files with 7,608 additions and 3 deletions.
Expand Up @@ -99,7 +99,7 @@ private void scan() throws IOException {
}
}
} catch (Exception e) {
log.errorf("Error loading Handler %s. Reason: %s", className, e.toString());
log.errorf("Error loading ActionPlugin %s. Reason: %s", className, e.toString());
System.exit(1);
}
}
Expand Down
Expand Up @@ -64,7 +64,7 @@ public void init() {
definitions.addActionPlugin(actionPlugin, properties);
}
} catch (Exception e) {
log.errorCannotRegisterPlugin(actionPlugin, e.getMessage());
log.errorCannotRegisterPlugin(actionPlugin, e.toString());
}
}
ActionListener actionListener = new StandaloneActionPluginListener(ActionPlugins.getPlugins(), executor);
Expand Down Expand Up @@ -95,5 +95,6 @@ public static synchronized void stop() {
}
});
}
instance = null;
}
}
34 changes: 34 additions & 0 deletions alerters/alerters-api/pom.xml
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2015-2017 Red Hat, Inc. and/or its affiliates
and other contributors as indicated by the @author tags.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.hawkular.alerts</groupId>
<artifactId>hawkular-alerts-alerters</artifactId>
<version>2.0.0.Final-SNAPSHOT</version>
</parent>

<artifactId>hawkular-alerts-alerters-api</artifactId>
<packaging>jar</packaging>

<name>Hawkular Alerting: Alerters Api</name>

</project>
@@ -0,0 +1,20 @@
package org.hawkular.alerts.alerters.api;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Define an alerters plugin implementation
* Plugin must have a unique name that will be used at registration phase
* Plugin must implement AlerterPlugin interface
*
* @author Jay Shaughnessy
* @author Lucas Ponce
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Alerter {
String name();
}
@@ -0,0 +1,16 @@
package org.hawkular.alerts.alerters.api;

import java.util.concurrent.ExecutorService;

import org.hawkular.alerts.api.services.AlertsService;
import org.hawkular.alerts.api.services.DefinitionsService;

/**
* @author Jay Shaughnessy
* @author Lucas Ponce
*/
public interface AlerterPlugin {

void init(DefinitionsService definitions, AlertsService alerts, ExecutorService executor);
void stop();
}
50 changes: 50 additions & 0 deletions alerters/alerters-impl/pom.xml
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2015-2017 Red Hat, Inc. and/or its affiliates
and other contributors as indicated by the @author tags.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.hawkular.alerts</groupId>
<artifactId>hawkular-alerts-alerters</artifactId>
<version>2.0.0.Final-SNAPSHOT</version>
</parent>

<artifactId>hawkular-alerts-alerters-impl</artifactId>
<packaging>jar</packaging>

<name>Hawkular Alerting: Alerters Implementation</name>

<dependencies>

<dependency>
<groupId>org.hawkular.alerts</groupId>
<artifactId>hawkular-alerts-alerters-api</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.hawkular.alerts</groupId>
<artifactId>hawkular-alerts-engine</artifactId>
<version>${project.version}</version>
</dependency>

</dependencies>

</project>
@@ -0,0 +1,105 @@
/*
* Copyright 2015-2016 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.alerts.alerters.standalone;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.hawkular.alerts.alerters.api.Alerter;
import org.hawkular.alerts.alerters.api.AlerterPlugin;
import org.hawkular.alerts.api.services.AlertsService;
import org.hawkular.alerts.api.services.DefinitionsService;
import org.hawkular.alerts.engine.StandaloneAlerts;
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 AlerterPlugins {
private static final MsgLogger log = MsgLogging.getMsgLogger(AlerterPlugins.class);
private static AlerterPlugins instance;
private Map<String, AlerterPlugin> plugins;
private ClassLoader cl = Thread.currentThread().getContextClassLoader();

DefinitionsService definitions;
AlertsService alerts;
ExecutorService executor;

public static synchronized Map<String, AlerterPlugin> getPlugins() {
if (instance == null) {
instance = new AlerterPlugins();
}
return Collections.unmodifiableMap(instance.plugins);
}

private AlerterPlugins() {
try {
plugins = new HashMap<>();
definitions = StandaloneAlerts.getDefinitionsService();
alerts = StandaloneAlerts.getAlertsService();
executor = StandaloneAlerts.getExecutor();
scan();
} catch (Exception e) {
throw new IllegalStateException(e);
}
}

@SuppressWarnings("unchecked")
private void scan() throws IOException {
String[] classpath = System.getProperty("java.class.path").split(":");
for (int i=0; i<classpath.length; i++) {
if (classpath[i].contains("hawkular") && classpath[i].endsWith("jar")) {
ZipInputStream zip = new ZipInputStream(new FileInputStream(classpath[i]));
for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) {
if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
String className = entry.getName().replace('/', '.'); // including ".class"
className = className.substring(0, className.length() - 6);
try {
Class clazz = cl.loadClass(className);
if (clazz.isAnnotationPresent(Alerter.class)) {
Alerter alerter = (Alerter)clazz.getAnnotation(Alerter.class);
String name = alerter.name();
Object newInstance = clazz.newInstance();
log.infof("Scanning %s", clazz.getName());
if (newInstance instanceof AlerterPlugin) {
AlerterPlugin pluginInstance = (AlerterPlugin)newInstance;
pluginInstance.init(definitions, alerts, executor);
plugins.put(name, pluginInstance);
} else {
throw new IllegalStateException("Alerter [" + name + "] is not instance of " +
"AlerterPlugin");
}
}
} catch (Exception e) {
log.errorf("Error loading AlerterPlugin %s. Reason: %s", className, e.toString());
System.exit(1);
}
}
}
}
}
}
}
@@ -0,0 +1,51 @@
package org.hawkular.alerts.alerters.standalone;

import java.util.Map;
import java.util.concurrent.ExecutorService;

import org.hawkular.alerts.alerters.api.AlerterPlugin;
import org.hawkular.commons.log.MsgLogger;
import org.hawkular.commons.log.MsgLogging;

/**
* @author Jay Shaughnessy
* @author Lucas Ponce
*/
public class StandaloneAlerterPluginRegister {
private static final MsgLogger log = MsgLogging.getMsgLogger(StandaloneAlerterPluginRegister.class);

private static StandaloneAlerterPluginRegister instance;
private static ExecutorService executor;

Map<String, AlerterPlugin> plugins;

private StandaloneAlerterPluginRegister() {
init();
}

public static void setExecutor(ExecutorService executor) {
StandaloneAlerterPluginRegister.executor = executor;
}

public void init() {
plugins = AlerterPlugins.getPlugins();
log.info("Alerter Plugins load finished");
}

public static synchronized void start() {
if (instance == null) {
instance = new StandaloneAlerterPluginRegister();
}
}

public static synchronized void stop() {
if (instance != null && instance.plugins != null) {
instance.plugins.entrySet().stream().forEach(pluginEntry -> {
log.infof("Stopping Alerter %s", pluginEntry.getKey());
pluginEntry.getValue().stop();
});
}
instance = null;
}

}
52 changes: 52 additions & 0 deletions alerters/alerters-plugins/alerters-elasticsearch/pom.xml
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2015-2017 Red Hat, Inc. and/or its affiliates
and other contributors as indicated by the @author tags.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.hawkular.alerts</groupId>
<artifactId>hawkular-alerts-alerters-plugins</artifactId>
<version>2.0.0.Final-SNAPSHOT</version>
</parent>

<artifactId>hawkular-alerts-alerters-elasticsearch</artifactId>
<packaging>jar</packaging>

<name>Hawkular Alerting: Alerter Elasticsearch Plugin</name>

<dependencies>

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>rest</artifactId>
<version>${version.org.elasticsearch.client}</version>
</dependency>

<!-- Tests -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>${version.junit}</version>
</dependency>

</dependencies>

</project>

0 comments on commit 32a6d1f

Please sign in to comment.