Adding operation partitioning functionality

revert-70ac1926
Ace 4 years ago
parent d831ef3284
commit f40f8d02e0

@ -0,0 +1,180 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
~
~ WSO2 Inc. licenses this file to you 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">
<parent>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>task-allocation</artifactId>
<version>4.1.11-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>io.entgra.dynamic.task.allocation.framework</artifactId>
<packaging>bundle</packaging>
<name>Entgra - Task Allocation Framework</name>
<description>Entgra - Framework for partitioning tasks evenly amongst multiple nodes</description>
<url>http://entgra.io</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Bundle-Version>${carbon.device.mgt.version}</Bundle-Version>
<Bundle-Description>Entgra - Task Allocation Framework Bundle</Bundle-Description>
<Private-Package>io.entgra.dynamic.task.allocation.framework.internal</Private-Package>
<Import-Package>
org.apache.axis2.*;version="${axis2.osgi.version.range}",
org.apache.axiom.*; version="${axiom.osgi.version.range}",
org.osgi.framework,
org.osgi.service.component,
org.apache.commons.logging,
javax.xml.parsers;version="${javax.xml.parsers.import.pkg.version}";resolution:=optional,
org.wso2.carbon.context,
org.wso2.carbon.utils.*,
org.wso2.carbon.registry.api,
org.w3c.dom,
org.apache.velocity;version="${velocity.version}",
org.apache.velocity.app;version="${velocity.version}",
org.apache.velocity.context;version="${velocity.version}",
org.apache.velocity.exception;version="${velocity.version}",
org.apache.velocity.runtime.resource;version="${velocity.version}",
org.apache.velocity.runtime.resource.loader;version="${velocity.version}",
org.apache.commons.io,
org.apache.axis2.transport.mail,
org.wso2.carbon.registry.core.service,
org.wso2.carbon.registry.core.session,
org.apache.commons.collections
</Import-Package>
<Export-Package>
!io.entgra.dynamic.task.allocation.framework.internal,
io.entgra.dynamic.task.allocation.framework.*
</Export-Package>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<log4j.configuration>file:src/test/resources/log4j.properties</log4j.configuration>
</systemPropertyVariables>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
<outputDirectory>${basedir}/target/coverage-reports/site</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.eclipse.osgi</groupId>
<artifactId>org.eclipse.osgi</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.osgi</groupId>
<artifactId>org.eclipse.osgi.services</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.logging</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.utils</artifactId>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.registry.api</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.base</artifactId>
</dependency>
<dependency>
<groupId>org.apache.axis2.wso2</groupId>
<artifactId>axis2</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.orbit.org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
</dependency>
<dependency>
<groupId>commons-io.wso2</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.apache.axis2.transport</groupId>
<artifactId>axis2-transport-mail</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.registry.core</artifactId>
</dependency>
<dependency>
<groupId>commons-collections.wso2</groupId>
<artifactId>commons-collections</artifactId>
</dependency>
</dependencies>
</project>

@ -0,0 +1,45 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.dynamic.task.allocation.framework;
public class ContentProcessingInterruptedException extends Exception {
private static final long serialVersionUID = -3151279311929070298L;
public ContentProcessingInterruptedException(String msg, Exception nestedEx) {
super(msg, nestedEx);
}
public ContentProcessingInterruptedException(String message, Throwable cause) {
super(message, cause);
}
public ContentProcessingInterruptedException(String msg) {
super(msg);
}
public ContentProcessingInterruptedException() {
super();
}
public ContentProcessingInterruptedException(Throwable cause) {
super(cause);
}
}

@ -0,0 +1,55 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.dynamic.task.allocation.framework;
import java.util.Map;
public class ContentProviderInfo {
private String template;
private Map<String, TypedValue<Class<?>, Object>> params;
public ContentProviderInfo(final String template, final Map<String, TypedValue<Class<?>, Object>> params) {
if (template == null || template.isEmpty()) {
throw new IllegalArgumentException("Template name cannot be null or empty");
}
this.template = template;
if (params == null) {
throw new IllegalArgumentException("Place-holder parameter map cannot be null");
}
this.params = params;
}
public String getTemplate() {
return template;
}
public Map<String, TypedValue<Class<?>, Object>> getParams() {
return params;
}
public void addParam(String name, TypedValue<Class<?>, Object> param) {
params.put(name, param);
}
public TypedValue<Class<?>, Object> getParam(String name) {
return params.get(name);
}
}

@ -0,0 +1,28 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.dynamic.task.allocation.framework;
import java.util.Map;
public interface EmailContentProvider {
EmailData getContent(String path,
Map<String, TypedValue<Class<?>, Object>> params) throws ContentProcessingInterruptedException;
}

@ -0,0 +1,27 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.dynamic.task.allocation.framework;
public class EmailContentProviderFactory {
public static EmailContentProvider getContentProvider() {
return new VelocityBasedEmailContentProvider();
}
}

@ -0,0 +1,138 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.dynamic.task.allocation.framework;
import java.util.*;
public class EmailContext {
private Set<String> recipients;
private Properties properties;
private ContentProviderInfo contentProviderInfo;
private EmailContext(final ContentProviderInfo contentProviderInfo, final Set<String> recipients, final Properties properties) {
if (contentProviderInfo == null) {
throw new IllegalArgumentException("Content provider information cannot be null");
}
this.contentProviderInfo = contentProviderInfo;
if (recipients == null) {
throw new IllegalArgumentException("Recipient list cannot be null");
}
if (recipients.size() == 0) {
throw new IllegalArgumentException("No recipient is configured. Recipient list should carry at " +
"least one recipient");
}
this.recipients = recipients;
if (properties == null) {
throw new IllegalArgumentException("Email Context property bag cannot be null");
}
this.properties = properties;
}
private EmailContext(final ContentProviderInfo contentProviderInfo, final String recipient, final Properties properties) {
if (contentProviderInfo == null) {
throw new IllegalArgumentException("Content provider information cannot be null");
}
this.contentProviderInfo = contentProviderInfo;
if (recipient == null || recipient.isEmpty()) {
throw new IllegalArgumentException("Recipient can't be null or empty. Please specify a valid " +
"recipient email address");
}
this.recipients = new HashSet<String>() {{
add(recipient);
}};
if (properties == null) {
throw new IllegalArgumentException("Email Context property bag cannot be null");
}
this.properties = properties;
}
public Set<String> getRecipients() {
return recipients;
}
public Properties getProperties() {
return properties;
}
public String getProperty(String name) {
return (String) properties.get(name);
}
public void addProperty(String name, String value) {
properties.put(name, value);
}
public ContentProviderInfo getContentProviderInfo() {
return contentProviderInfo;
}
public static class EmailContextBuilder {
private Set<String> recipients;
private ContentProviderInfo contentProviderInfo;
private Properties properties;
public EmailContextBuilder(final ContentProviderInfo contentProviderInfo, Set<String> recipients) {
if (contentProviderInfo == null) {
throw new IllegalArgumentException("Content provider information cannot be null");
}
this.contentProviderInfo = contentProviderInfo;
if (recipients == null) {
throw new IllegalArgumentException("Recipient list cannot be null");
}
if (recipients.size() == 0) {
throw new IllegalArgumentException("No recipient is configured. Recipient list should carry at " +
"least one recipient");
}
this.recipients = recipients;
this.properties = new Properties();
}
public EmailContextBuilder(final ContentProviderInfo contentProviderInfo, final String recipient,
final Properties properties) {
if (contentProviderInfo == null) {
throw new IllegalArgumentException("Content provider information cannot be null");
}
this.contentProviderInfo = contentProviderInfo;
if (recipient == null || recipient.isEmpty()) {
throw new IllegalArgumentException("Recipient can't be null or empty. Please specify a valid " +
"recipient email address");
}
this.recipients = new HashSet<String>() {{
add(recipient);
}};
if (properties == null) {
throw new IllegalArgumentException("Email Context property bag cannot be null");
}
this.properties = properties;
}
public EmailContextBuilder addProperty(String name, String value) {
properties.setProperty(name, value);
return this;
}
public EmailContext build() {
return new EmailContext(contentProviderInfo, recipients, properties);
}
}
}

@ -0,0 +1,48 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.dynamic.task.allocation.framework;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "EmailConfig")
public class EmailData {
private String subject;
private String body;
@XmlElement(name = "Subject", required = true)
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
@XmlElement(name = "Body", required = true)
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}

@ -0,0 +1,106 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.dynamic.task.allocation.framework;
import org.w3c.dom.Document;
import org.wso2.carbon.utils.CarbonUtils;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.File;
@XmlRootElement(name = "EmailSenderConfig")
public class EmailSenderConfig {
private int minThreads;
private int maxThreads;
private int keepAliveDuration;
private int threadQueueCapacity;
private static EmailSenderConfig config;
private static final String EMAIL_SENDER_CONFIG_PATH =
CarbonUtils.getEtcCarbonConfigDirPath() + File.separator + "email-sender-config.xml";
private EmailSenderConfig() {
}
public static EmailSenderConfig getInstance() {
if (config == null) {
throw new InvalidConfigurationStateException("Webapp Authenticator Configuration is not " +
"initialized properly");
}
return config;
}
@XmlElement(name = "MinThreads", required = true)
public int getMinThreads() {
return minThreads;
}
public void setMinThreads(int minThreads) {
this.minThreads = minThreads;
}
@XmlElement(name = "MaxThreads", required = true)
public int getMaxThreads() {
return maxThreads;
}
public void setMaxThreads(int maxThreads) {
this.maxThreads = maxThreads;
}
@XmlElement(name = "KeepAliveDuration", required = true)
public int getKeepAliveDuration() {
return keepAliveDuration;
}
public void setKeepAliveDuration(int keepAliveDuration) {
this.keepAliveDuration = keepAliveDuration;
}
@XmlElement(name = "ThreadQueueCapacity", required = true)
public int getThreadQueueCapacity() {
return threadQueueCapacity;
}
public void setThreadQueueCapacity(int threadQueueCapacity) {
this.threadQueueCapacity = threadQueueCapacity;
}
public static void init() throws EmailSenderConfigurationFailedException {
try {
File emailSenderConfig = new File(EMAIL_SENDER_CONFIG_PATH);
Document doc = EmailSenderUtil.convertToDocument(emailSenderConfig);
/* Un-marshaling Email Sender configuration */
JAXBContext ctx = JAXBContext.newInstance(EmailSenderConfig.class);
Unmarshaller unmarshaller = ctx.createUnmarshaller();
//unmarshaller.setSchema(getSchema());
config = (EmailSenderConfig) unmarshaller.unmarshal(doc);
} catch (JAXBException e) {
throw new EmailSenderConfigurationFailedException("Error occurred while un-marshalling Email " +
"Sender Config", e);
}
}
}

@ -0,0 +1,45 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.dynamic.task.allocation.framework;
public class EmailSenderConfigurationFailedException extends Exception {
private static final long serialVersionUID = -3151279312929070298L;
public EmailSenderConfigurationFailedException(String msg, Exception nestedEx) {
super(msg, nestedEx);
}
public EmailSenderConfigurationFailedException(String message, Throwable cause) {
super(message, cause);
}
public EmailSenderConfigurationFailedException(String msg) {
super(msg);
}
public EmailSenderConfigurationFailedException() {
super();
}
public EmailSenderConfigurationFailedException(Throwable cause) {
super(cause);
}
}

@ -0,0 +1,44 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.dynamic.task.allocation.framework;
import org.w3c.dom.Document;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
public class EmailSenderUtil {
public static Document convertToDocument(File file) throws EmailSenderConfigurationFailedException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
try {
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
DocumentBuilder docBuilder = factory.newDocumentBuilder();
return docBuilder.parse(file);
} catch (Exception e) {
throw new EmailSenderConfigurationFailedException("Error occurred while parsing file, while converting " +
"to a org.w3c.dom.Document", e);
}
}
}

@ -0,0 +1,45 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.dynamic.task.allocation.framework;
public class EmailSendingFailedException extends Exception {
private static final long serialVersionUID = -3151279311929070294L;
public EmailSendingFailedException(String msg, Exception nestedEx) {
super(msg, nestedEx);
}
public EmailSendingFailedException(String message, Throwable cause) {
super(message, cause);
}
public EmailSendingFailedException(String msg) {
super(msg);
}
public EmailSendingFailedException() {
super();
}
public EmailSendingFailedException(Throwable cause) {
super(cause);
}
}

@ -0,0 +1,44 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.dynamic.task.allocation.framework;
public class EmailTransportNotConfiguredException extends Exception {
private static final long serialVersionUID = -3151279311929070294L;
public EmailTransportNotConfiguredException(String msg, Exception nestedEx) {
super(msg, nestedEx);
}
public EmailTransportNotConfiguredException(String message, Throwable cause) {
super(message, cause);
}
public EmailTransportNotConfiguredException(String msg) {
super(msg);
}
public EmailTransportNotConfiguredException() {
super();
}
public EmailTransportNotConfiguredException(Throwable cause) {
super(cause);
}
}

@ -0,0 +1,74 @@
/*
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.dynamic.task.allocation.framework;
public class InvalidConfigurationStateException extends RuntimeException {
private static final long serialVersionUID = -3151279311329070297L;
private String errorMessage;
private int errorCode;
public InvalidConfigurationStateException(int errorCode, String message) {
super(message);
this.errorCode = errorCode;
}
public InvalidConfigurationStateException(int errorCode, String message, Throwable cause) {
super(message, cause);
this.errorCode = errorCode;
}
public int getErrorCode() {
return errorCode;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public InvalidConfigurationStateException(String msg, Exception nestedEx) {
super(msg, nestedEx);
setErrorMessage(msg);
}
public InvalidConfigurationStateException(String message, Throwable cause) {
super(message, cause);
setErrorMessage(message);
}
public InvalidConfigurationStateException(String msg) {
super(msg);
setErrorMessage(msg);
}
public InvalidConfigurationStateException() {
super();
}
public InvalidConfigurationStateException(Throwable cause) {
super(cause);
}
}

@ -0,0 +1,71 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.dynamic.task.allocation.framework;
import org.apache.commons.collections.ExtendedProperties;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.RegistryType;
import org.wso2.carbon.registry.api.Registry;
import org.wso2.carbon.registry.api.RegistryException;
import java.io.InputStream;
public class RegistryBasedResourceLoader extends ResourceLoader {
private static final String EMAIL_CONFIG_BASE_LOCATION = "email-templates";
@Override
public void init(ExtendedProperties extendedProperties) {
}
@Override
public InputStream getResourceStream(String name) throws ResourceNotFoundException {
try {
Registry registry =
CarbonContext.getThreadLocalCarbonContext().getRegistry(RegistryType.SYSTEM_CONFIGURATION);
if (registry == null) {
throw new IllegalStateException("No valid registry instance is attached to the current carbon context");
}
if (!registry.resourceExists(EMAIL_CONFIG_BASE_LOCATION + "/" + name)) {
throw new ResourceNotFoundException("Resource '" + name + "' does not exist");
}
org.wso2.carbon.registry.api.Resource resource =
registry.get(EMAIL_CONFIG_BASE_LOCATION + "/" + name);
resource.setMediaType("text/plain");
return resource.getContentStream();
} catch (RegistryException e) {
throw new ResourceNotFoundException("Error occurred while retrieving resource", e);
}
}
@Override
public boolean isSourceModified(Resource resource) {
return false;
}
@Override
public long getLastModified(Resource resource) {
return 0;
}
}

@ -0,0 +1,50 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.dynamic.task.allocation.framework;
public class TypedValue<T, V> {
private final T type;
private final V value;
public TypedValue(T type, V value) {
this.type = type;
this.value = value;
}
public T getType() {
return type;
}
public V getValue() {
return value;
}
@Override
public int hashCode() {
return (type.hashCode() ^ value.hashCode());
}
@Override
public boolean equals(Object o) {
return o instanceof TypedValue && (this.type == ((TypedValue) o).getType() &&
this.value == ((TypedValue) o).getValue());
}
}

@ -0,0 +1,87 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.dynamic.task.allocation.framework;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.RegistryType;
import org.wso2.carbon.registry.api.Registry;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Map;
public class VelocityBasedEmailContentProvider implements EmailContentProvider {
private VelocityEngine engine;
private static final Log log = LogFactory.getLog(VelocityBasedEmailContentProvider.class);
public VelocityBasedEmailContentProvider() {
engine = new VelocityEngine();
engine.setProperty("resource.loader", "registry");
engine.setProperty("velocimacro.library", "");
engine.setProperty("registry.resource.loader.class",
"org.wso2.carbon.email.sender.core.RegistryBasedResourceLoader");
engine.init();
}
@Override
public EmailData getContent(String name, Map<String,
TypedValue<Class<?>, Object>> params) throws ContentProcessingInterruptedException {
VelocityContext ctx = new VelocityContext();
for (Map.Entry<String, TypedValue<Class<?>, Object>> param : params.entrySet()) {
ctx.put(param.getKey(), param.getValue().getValue());
}
Template template = engine.getTemplate(name);
StringWriter content = new StringWriter();
template.merge(ctx, content);
InputStream is = null;
try {
JAXBContext jaxbCtx = JAXBContext.newInstance(EmailData.class);
Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
is = new ByteArrayInputStream(content.toString().getBytes());
return (EmailData) unmarshaller.unmarshal(is);
} catch (JAXBException e) {
throw new ContentProcessingInterruptedException("Error occurred while parsing email data", e);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
log.warn("Error occurred while closing input stream used to convert email configuration " +
"to an internal object model", e);
}
}
}
}
}

@ -0,0 +1,55 @@
/*
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.dynamic.task.allocation.framework.internal;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import io.entgra.dynamic.task.allocation.framework.EmailSenderConfigurationFailedException;
import org.wso2.carbon.utils.Axis2ConfigurationContextObserver;
class EmailSenderAxis2ConfigContextObserver implements Axis2ConfigurationContextObserver {
private static final Log log = LogFactory.getLog(EmailSenderAxis2ConfigContextObserver.class);
@Override
public void creatingConfigurationContext(int tenantId) {
}
@Override
public void createdConfigurationContext(ConfigurationContext configurationContext) {
try {
EmailUtils.setupEmailTemplates();
} catch (EmailSenderConfigurationFailedException e) {
log.error("Error occurred while setting up email templates", e);
}
}
@Override
public void terminatingConfigurationContext(ConfigurationContext configurationContext) {
}
@Override
public void terminatedConfigurationContext(ConfigurationContext configurationContext) {
}
}

@ -0,0 +1,69 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.dynamic.task.allocation.framework.internal;
import io.entgra.dynamic.task.allocation.framework.service.EmailSenderService;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.utils.ConfigurationContextService;
public class EmailSenderDataHolder {
private RegistryService registryService;
private ConfigurationContextService configurationContextService;
private EmailSenderService emailServiceProvider;
private static EmailSenderDataHolder thisInstance = new EmailSenderDataHolder();
private EmailSenderDataHolder() {}
public static EmailSenderDataHolder getInstance() {
return thisInstance;
}
public RegistryService getRegistryService() {
if (registryService == null) {
throw new IllegalStateException("Registry service is not initialized properly");
}
return registryService;
}
public void setRegistryService(RegistryService registryService) {
this.registryService = registryService;
}
public ConfigurationContextService getConfigurationContextService() {
if (configurationContextService == null) {
throw new IllegalStateException("ConfigurationContext service is not initialized properly");
}
return configurationContextService;
}
public void setConfigurationContextService(ConfigurationContextService configurationContextService) {
this.configurationContextService = configurationContextService;
}
public EmailSenderService getEmailServiceProvider() {
return emailServiceProvider;
}
public void setEmailServiceProvider(EmailSenderService emailServiceProvider) {
this.emailServiceProvider = emailServiceProvider;
}
}

@ -0,0 +1,127 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.dynamic.task.allocation.framework.internal;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.service.component.ComponentContext;
import io.entgra.dynamic.task.allocation.framework.EmailSenderConfig;
import io.entgra.dynamic.task.allocation.framework.service.EmailSenderService;
import io.entgra.dynamic.task.allocation.framework.service.EmailSenderServiceImpl;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.utils.Axis2ConfigurationContextObserver;
import org.wso2.carbon.utils.ConfigurationContextService;
/**
* @scr.component name="org.wso2.carbon.email.sender.emailsendereervicecomponent"
* immediate="true"
* @scr.reference name="registry.service"
* interface="org.wso2.carbon.registry.core.service.RegistryService"
* cardinality="1..1"
* policy="dynamic"
* bind="setRegistryService"
* unbind="unsetRegistryService"
* @scr.reference name="config.context.service"
* interface="org.wso2.carbon.utils.ConfigurationContextService"
* cardinality="0..1"
* policy="dynamic"
* bind="setConfigurationContextService"
* unbind="unsetConfigurationContextService"
*/
public class EmailSenderServiceComponent {
private static Log log = LogFactory.getLog(EmailSenderServiceComponent.class);
@SuppressWarnings("unused")
protected void activate(ComponentContext componentContext) {
try {
if (log.isDebugEnabled()) {
log.debug("Initializing email sender core bundle");
}
/* Initializing email sender configuration */
EmailSenderConfig.init();
/* Setting up default email templates */
EmailUtils.setupEmailTemplates();
/* Registering declarative service instances exposed by EmailSenderServiceComponent */
this.registerServices(componentContext);
if (log.isDebugEnabled()) {
log.debug("Email sender core bundle has been successfully initialized");
}
componentContext.getBundleContext().registerService(Axis2ConfigurationContextObserver.class.getName(),
new EmailSenderAxis2ConfigContextObserver(), null);
} catch (Throwable e) {
log.error("Error occurred while initializing email sender core bundle", e);
}
}
@SuppressWarnings("unused")
protected void deactivate(ComponentContext componentContext) {
//do nothing
}
private void registerServices(ComponentContext componentContext) {
if (log.isDebugEnabled()) {
log.debug("Registering email sender service");
}
EmailSenderService emailServiceProvider = new EmailSenderServiceImpl();
EmailSenderDataHolder.getInstance().setEmailServiceProvider(emailServiceProvider);
componentContext.getBundleContext().registerService(EmailSenderService.class, emailServiceProvider, null);
}
/**
* Sets Registry Service.
*
* @param registryService An instance of RegistryService
*/
protected void setRegistryService(RegistryService registryService) {
if (log.isDebugEnabled()) {
log.debug("Setting Registry Service");
}
EmailSenderDataHolder.getInstance().setRegistryService(registryService);
}
/**
* Unsets Registry Service.
*
* @param registryService An instance of RegistryService
*/
protected void unsetRegistryService(RegistryService registryService) {
if (log.isDebugEnabled()) {
log.debug("Un setting Registry Service");
}
EmailSenderDataHolder.getInstance().setRegistryService(null);
}
protected void setConfigurationContextService(ConfigurationContextService configurationContextService) {
if (log.isDebugEnabled()) {
log.debug("Setting ConfigurationContextService");
}
EmailSenderDataHolder.getInstance().setConfigurationContextService(configurationContextService);
}
protected void unsetConfigurationContextService(ConfigurationContextService configurationContextService) {
if (log.isDebugEnabled()) {
log.debug("Un-setting ConfigurationContextService");
}
EmailSenderDataHolder.getInstance().setConfigurationContextService(null);
}
}

@ -0,0 +1,87 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.dynamic.task.allocation.framework.internal;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext;
import io.entgra.dynamic.task.allocation.framework.EmailSenderConfigurationFailedException;
import org.wso2.carbon.registry.api.Collection;
import org.wso2.carbon.registry.api.Registry;
import org.wso2.carbon.registry.api.RegistryException;
import org.wso2.carbon.registry.api.Resource;
import org.wso2.carbon.utils.CarbonUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
class EmailUtils {
private static final String EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH = "/email-templates";
private static Log log = LogFactory.getLog(EmailSenderServiceComponent.class);
static void setupEmailTemplates() throws EmailSenderConfigurationFailedException {
File templateDir =
new File(CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator +
"resources" + File.separator + "email-templates");
if (!templateDir.exists()) {
if (log.isDebugEnabled()) {
log.debug("The directory that is expected to use as the container for all email templates is not " +
"available. Therefore, no template is uploaded to the registry");
}
}
if (templateDir.canRead()) {
File[] templates = templateDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
name = name.toLowerCase();
return name.endsWith(".vm");
}
});
try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
Registry registry =
EmailSenderDataHolder.getInstance().getRegistryService().getConfigSystemRegistry(tenantId);
if (!registry.resourceExists(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH)) {
Collection collection = registry.newCollection();
registry.put(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH, collection);
for (File template : templates) {
Resource resource = registry.newResource();
resource.setMediaType("text/plain");
String contents = FileUtils.readFileToString(template);
resource.setContent(contents);
registry.put(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH + "/"
+ template.getName().replace(".vm", ""), resource);
}
}
} catch (RegistryException e) {
throw new EmailSenderConfigurationFailedException("Error occurred while setting up email templates", e);
} catch (FileNotFoundException e) {
throw new EmailSenderConfigurationFailedException("Error occurred while writing template file " +
"contents as an input stream of a resource", e);
} catch (IOException e) {
throw new EmailSenderConfigurationFailedException("Error occurred while serializing file " +
"contents to a string", e);
}
}
}
}

@ -0,0 +1,28 @@
/*
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.dynamic.task.allocation.framework.service;
import io.entgra.dynamic.task.allocation.framework.EmailContext;
import io.entgra.dynamic.task.allocation.framework.EmailSendingFailedException;
import io.entgra.dynamic.task.allocation.framework.EmailTransportNotConfiguredException;
public interface EmailSenderService {
void sendEmail(EmailContext emailCtx) throws EmailSendingFailedException, EmailTransportNotConfiguredException;
}

@ -0,0 +1,150 @@
/*
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.dynamic.task.allocation.framework.service;
import io.entgra.dynamic.task.allocation.framework.ContentProcessingInterruptedException;
import io.entgra.dynamic.task.allocation.framework.ContentProviderInfo;
import io.entgra.dynamic.task.allocation.framework.EmailContentProvider;
import io.entgra.dynamic.task.allocation.framework.EmailContentProviderFactory;
import io.entgra.dynamic.task.allocation.framework.EmailContext;
import io.entgra.dynamic.task.allocation.framework.EmailData;
import io.entgra.dynamic.task.allocation.framework.EmailSenderConfig;
import io.entgra.dynamic.task.allocation.framework.EmailSendingFailedException;
import io.entgra.dynamic.task.allocation.framework.EmailTransportNotConfiguredException;
import io.entgra.dynamic.task.allocation.framework.internal.EmailSenderDataHolder;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.llom.util.AXIOMUtil;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.transport.mail.MailConstants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.email.sender.core.*;
import org.wso2.carbon.utils.ConfigurationContextService;
import javax.xml.stream.XMLStreamException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class EmailSenderServiceImpl implements EmailSenderService {
private static ThreadPoolExecutor threadPoolExecutor;
private EmailContentProvider contentProvider;
private static final String TRANSPORT_SENDER_NAME = "mailto";
static {
EmailSenderConfig config = EmailSenderConfig.getInstance();
threadPoolExecutor = new ThreadPoolExecutor(config.getMinThreads(), config.getMaxThreads(),
config.getKeepAliveDuration(), TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(config.getThreadQueueCapacity()));
}
private static final String EMAIL_URI_SCHEME = "mailto:";
private static Log log = LogFactory.getLog(EmailSenderServiceImpl.class);
public EmailSenderServiceImpl() {
this.contentProvider = EmailContentProviderFactory.getContentProvider();
}
private boolean isMailServerConfigured() {
return (EmailSenderDataHolder.getInstance().getConfigurationContextService().getServerConfigContext().
getAxisConfiguration().getTransportOut(TRANSPORT_SENDER_NAME) != null);
}
@Override
public void sendEmail(EmailContext emailCtx) throws EmailSendingFailedException,
EmailTransportNotConfiguredException {
if (this.isMailServerConfigured()) {
for (String recipient : emailCtx.getRecipients()) {
ContentProviderInfo info = emailCtx.getContentProviderInfo();
EmailData emailData;
try {
emailData = contentProvider.getContent(info.getTemplate(), info.getParams());
threadPoolExecutor.submit(new EmailSender(recipient, emailData.getSubject(), emailData.getBody()));
} catch (ContentProcessingInterruptedException e) {
throw new EmailSendingFailedException("Error occurred while retrieving email content to be " +
"sent for recipient '" + recipient + "'", e);
}
}
} else {
String msg = "Email sender transport is not configured. Please configure the 'mailto' sender" +
" transport in axis2.xml.";
log.warn(msg);
throw new EmailTransportNotConfiguredException(msg);
}
}
public static class EmailSender implements Runnable {
String to;
String subject;
String body;
EmailSender(String to, String subject, String body) {
this.to = to;
this.subject = subject;
this.body = body;
}
public void run() {
OMElement payload = null;
try {
payload = AXIOMUtil.stringToOM(body);
} catch (XMLStreamException e) {
log.error("Error occurred while converting email body contents to an XML", e);
}
try {
ConfigurationContextService configCtxService =
EmailSenderDataHolder.getInstance().getConfigurationContextService();
if (configCtxService == null) {
throw new IllegalStateException("Configuration Context Service is not available");
}
ConfigurationContext configCtx = configCtxService.getServerConfigContext();
ServiceClient serviceClient = new ServiceClient(configCtx, null);
Map<String, String> headerMap = new HashMap<>();
headerMap.put(MailConstants.MAIL_HEADER_SUBJECT, subject);
Options options = new Options();
options.setProperty(MessageContext.TRANSPORT_HEADERS, headerMap);
options.setProperty("FORCE_CONTENT_TYPE_BASED_FORMATTER", "true");
options.setProperty(Constants.Configuration.MESSAGE_TYPE, "application/xml");
options.setProperty(Constants.Configuration.CONTENT_TYPE, "text/html");
options.setTo(new EndpointReference(EMAIL_URI_SCHEME + to));
serviceClient.setOptions(options);
serviceClient.fireAndForget(payload);
if (log.isDebugEnabled()) {
log.debug("Email has been successfully sent to '" + to + "'");
}
} catch (AxisFault e) {
log.error("Error occurred while delivering the message, subject: '" + subject + "', to: '" + to +
"'", e);
}
}
}
}

@ -0,0 +1,32 @@
#
# Copyright 2009 WSO2, Inc. (http://wso2.com)
#
# 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.
#
#
# This is the log4j configuration file used by WSO2 Carbon
#
# IMPORTANT : Please do not remove or change the names of any
# of the Appenders defined here. The layout pattern & log file
# can be changed using the WSO2 Carbon Management Console, and those
# settings will override the settings in this file.
#
log4j.rootLogger=INFO, STD_OUT
# Redirect log messages to console
log4j.appender.STD_OUT=org.apache.log4j.ConsoleAppender
log4j.appender.STD_OUT.Target=System.out
log4j.appender.STD_OUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STD_OUT.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

@ -0,0 +1,29 @@
<!--
~ Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
~
~ WSO2 Inc. licenses this file to you 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.
-->
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="DeviceManagementCore">
<parameter name="useDefaultListeners" value="false"/>
<test name="EmailSenderTests" preserve-order="true">
<classes>
</classes>
</test>
</suite>

@ -0,0 +1,185 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
~
~ WSO2 Inc. licenses this file to you 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">
<parent>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>task-allocation</artifactId>
<version>4.1.11-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>io.entgra.server.bootup.heartbeat.beacon</artifactId>
<packaging>bundle</packaging>
<name>Entgra - Heartbeat Beacon</name>
<description>Entgra - Server Startup and Heartbeat Monitoring Component</description>
<url>http://wso2.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Bundle-Version>${carbon.device.mgt.version}</Bundle-Version>
<Bundle-Description>Server Startup Heart Beat Beacon Bundle</Bundle-Description>
<Private-Package>io.entgra.server.bootup.heartbeat.beacon.internal</Private-Package>
<Import-Package>
org.apache.axis2.*;version="${axis2.osgi.version.range}",
org.apache.axiom.*; version="${axiom.osgi.version.range}",
org.osgi.framework,
org.osgi.service.component,
org.apache.commons.logging,
javax.xml.parsers;version="${javax.xml.parsers.import.pkg.version}";resolution:=optional,
org.wso2.carbon.context,
org.wso2.carbon.utils.*,
org.wso2.carbon.registry.api,
org.w3c.dom,
org.apache.velocity;version="${velocity.version}",
org.apache.velocity.app;version="${velocity.version}",
org.apache.velocity.context;version="${velocity.version}",
org.apache.velocity.exception;version="${velocity.version}",
org.apache.velocity.runtime.resource;version="${velocity.version}",
org.apache.velocity.runtime.resource.loader;version="${velocity.version}",
org.apache.commons.io,
org.apache.axis2.transport.mail,
org.wso2.carbon.registry.core.service,
org.wso2.carbon.registry.core.session,
org.apache.commons.collections,
org.wso2.carbon.device.mgt.common.*
</Import-Package>
<Export-Package>
!io.entgra.server.bootup.heartbeat.beacon.internal,
io.entgra.server.bootup.heartbeat.beacon.*
</Export-Package>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<log4j.configuration>file:src/test/resources/log4j.properties</log4j.configuration>
</systemPropertyVariables>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
<outputDirectory>${basedir}/target/coverage-reports/site</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.eclipse.osgi</groupId>
<artifactId>org.eclipse.osgi</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.osgi</groupId>
<artifactId>org.eclipse.osgi.services</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.logging</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.utils</artifactId>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.registry.api</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.base</artifactId>
</dependency>
<dependency>
<groupId>org.apache.axis2.wso2</groupId>
<artifactId>axis2</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.orbit.org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
</dependency>
<dependency>
<groupId>commons-io.wso2</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.apache.axis2.transport</groupId>
<artifactId>axis2-transport-mail</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.registry.core</artifactId>
</dependency>
<dependency>
<groupId>commons-collections.wso2</groupId>
<artifactId>commons-collections</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.device.mgt.common</artifactId>
</dependency>
</dependencies>
</project>

@ -0,0 +1,96 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.server.bootup.heartbeat.beacon;
import io.entgra.server.bootup.heartbeat.beacon.dto.ServerContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Hashtable;
public class HeartBeatBeaconUtils {
private static Log log = LogFactory.getLog(HeartBeatBeaconUtils.class);
public static Document convertToDocument(File file)
throws HeartBeatBeaconConfigurationException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
try {
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
DocumentBuilder docBuilder = factory.newDocumentBuilder();
return docBuilder.parse(file);
} catch (Exception e) {
throw new HeartBeatBeaconConfigurationException("Error occurred while parsing file, while converting " +
"to a org.w3c.dom.Document", e);
}
}
/**
* Lookup datasource using name and jndi properties
*
* @param dataSourceName Name of datasource to lookup
* @param jndiProperties Hash table of JNDI Properties
* @return datasource looked
*/
public static DataSource lookupDataSource(String dataSourceName,
final Hashtable<Object, Object> jndiProperties) {
try {
if (jndiProperties == null || jndiProperties.isEmpty()) {
return (DataSource) InitialContext.doLookup(dataSourceName);
}
final InitialContext context = new InitialContext(jndiProperties);
return (DataSource) context.lookup(dataSourceName);
} catch (Exception e) {
throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e);
}
}
public static ServerContext getServerDetails() throws UnknownHostException, SocketException {
InetAddress localHost = InetAddress.getLocalHost();
NetworkInterface ni = NetworkInterface.getByInetAddress(localHost);
byte[] hardwareAddress = ni.getHardwareAddress();
String[] hexadecimal = new String[hardwareAddress.length];
for (int i = 0; i < hardwareAddress.length; i++) {
hexadecimal[i] = String.format("%02X", hardwareAddress[i]);
}
String macAddress = String.join("-", hexadecimal);
ServerContext ctx = new ServerContext();
ctx.setHostName(localHost.getHostName());
ctx.setMacAddress(macAddress);
return ctx;
}
}

@ -0,0 +1,40 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.server.bootup.heartbeat.beacon.config.datasource;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* Class for holding data source configuration in cdm-config.xml at parsing with JAXB
*/
@XmlRootElement(name = "DataSourceConfiguration")
public class DataSourceConfig {
private JNDILookupDefinition jndiLookupDefinition;
@XmlElement(name = "JndiLookupDefinition", nillable = true)
public JNDILookupDefinition getJndiLookupDefinition() {
return jndiLookupDefinition;
}
public void setJndiLookupDefinition(JNDILookupDefinition jndiLookupDefinition) {
this.jndiLookupDefinition = jndiLookupDefinition;
}
}

@ -0,0 +1,83 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.server.bootup.heartbeat.beacon.config.datasource;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
import java.util.List;
/**
* Class for hold JndiLookupDefinition of cdm-config.xml at parsing with JAXB
*/
@XmlRootElement(name = "JndiLookupDefinition")
public class JNDILookupDefinition {
private String jndiName;
private List<JNDIProperty> jndiProperties;
@XmlElement(name = "Name", nillable = false)
public String getJndiName() {
return jndiName;
}
public void setJndiName(String jndiName) {
this.jndiName = jndiName;
}
@XmlElementWrapper(name = "Environment", nillable = false)
@XmlElement(name = "Property", nillable = false)
public List<JNDIProperty> getJndiProperties() {
return jndiProperties;
}
public void setJndiProperties(List<JNDIProperty> jndiProperties) {
this.jndiProperties = jndiProperties;
}
@XmlRootElement(name = "Property")
public static class JNDIProperty {
private String name;
private String value;
@XmlAttribute(name = "Name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlValue
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}

@ -0,0 +1,205 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.server.bootup.heartbeat.beacon.dao;
import io.entgra.server.bootup.heartbeat.beacon.HeartBeatBeaconUtils;
import io.entgra.server.bootup.heartbeat.beacon.config.datasource.DataSourceConfig;
import io.entgra.server.bootup.heartbeat.beacon.config.datasource.JNDILookupDefinition;
import io.entgra.server.bootup.heartbeat.beacon.dao.impl.GenericHeartBeatDAOImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.common.exceptions.IllegalTransactionStateException;
import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.UnsupportedDatabaseEngineException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.List;
/**
* This class represents factory for group management data operations
*/
public class HeartBeatBeaconDAOFactory {
private static final Log log = LogFactory.getLog(HeartBeatBeaconDAOFactory.class);
private static DataSource dataSource;
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<>();
/**
* Get instance of GroupDAO
*
* @return instance of GroupDAO implementation
*/
public static HeartBeatDAO getHeartBeatDAO() {
return new GenericHeartBeatDAOImpl();
}
public static void init(DataSourceConfig config) {
dataSource = resolveDataSource(config);
}
public static void init(DataSource dtSource) {
dataSource = dtSource;
}
/**
* Begin transaction with datasource for write data
*
* @throws TransactionManagementException
*/
public static void beginTransaction() throws TransactionManagementException {
Connection conn = currentConnection.get();
if (conn != null) {
throw new IllegalTransactionStateException("A transaction is already active within the context of " +
"this particular thread. Therefore, calling 'beginTransaction/openConnection' while another " +
"transaction is already active is a sign of improper transaction handling");
}
try {
conn = dataSource.getConnection();
conn.setAutoCommit(false);
currentConnection.set(conn);
} catch (SQLException e) {
throw new TransactionManagementException("Error occurred while retrieving config.datasource connection", e);
}
}
/**
* Open connection to the datasource for read data
*
* @throws SQLException
*/
public static void openConnection() throws SQLException {
Connection conn = currentConnection.get();
if (conn != null) {
throw new IllegalTransactionStateException("A transaction is already active within the context of " +
"this particular thread. Therefore, calling 'beginTransaction/openConnection' while another " +
"transaction is already active is a sign of improper transaction handling");
}
conn = dataSource.getConnection();
currentConnection.set(conn);
}
/**
* Get current connection to datasource
*
* @return current connection
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
Connection conn = currentConnection.get();
if (conn == null) {
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " +
"This might have ideally been caused by not properly initiating the transaction via " +
"'beginTransaction'/'openConnection' methods");
}
return conn;
}
/**
* Commit current transaction to the datasource
*/
public static void commitTransaction() {
Connection conn = currentConnection.get();
if (conn == null) {
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " +
"This might have ideally been caused by not properly initiating " +
"the transaction via 'beginTransaction'/'openConnection' methods");
}
try {
conn.commit();
} catch (SQLException e) {
log.error("Error occurred while committing the transaction", e);
}
}
/**
* Rollback current transaction on failure
*/
public static void rollbackTransaction() {
Connection conn = currentConnection.get();
if (conn == null) {
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " +
"This might have ideally been caused by not properly initiating " +
"the transaction via 'beginTransaction'/'openConnection' methods");
}
try {
conn.rollback();
} catch (SQLException e) {
log.warn("Error occurred while roll-backing the transaction", e);
}
}
/**
* Close data connection associated with current transaction
*/
public static void closeConnection() {
Connection conn = currentConnection.get();
if (conn == null) {
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " +
"This might have ideally been caused by not properly " +
"initiating the transaction via " +
"'beginTransaction'/'openConnection' methods");
}
try {
conn.close();
} catch (SQLException e) {
log.warn("Error occurred while close the connection");
}
currentConnection.remove();
}
/**
* Resolve data source from the data source definition
*
* @param config data source configuration
* @return data source resolved from the data source definition
*/
private static DataSource resolveDataSource(DataSourceConfig config) {
DataSource dataSource = null;
if (config == null) {
throw new RuntimeException(
"Device Management Repository data source configuration " + "is null and " +
"thus, is not initialized");
}
JNDILookupDefinition jndiConfig = config.getJndiLookupDefinition();
if (jndiConfig != null) {
if (log.isDebugEnabled()) {
log.debug("Initializing Device Management Repository data source using the JNDI " +
"Lookup Definition");
}
List<JNDILookupDefinition.JNDIProperty> jndiPropertyList =
jndiConfig.getJndiProperties();
if (jndiPropertyList != null) {
Hashtable<Object, Object> jndiProperties = new Hashtable<>();
for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) {
jndiProperties.put(prop.getName(), prop.getValue());
}
dataSource = HeartBeatBeaconUtils.lookupDataSource(jndiConfig.getJndiName(), jndiProperties);
} else {
dataSource = HeartBeatBeaconUtils.lookupDataSource(jndiConfig.getJndiName(), null);
}
}
return dataSource;
}
}

@ -0,0 +1,42 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.server.bootup.heartbeat.beacon.dao;
import io.entgra.server.bootup.heartbeat.beacon.dao.exception.HeartBeatDAOException;
import io.entgra.server.bootup.heartbeat.beacon.dto.HeartBeatEvent;
import io.entgra.server.bootup.heartbeat.beacon.dto.ServerContext;
import java.util.List;
/**
* This interface represents the key operations associated with persisting group related information.
*/
public interface HeartBeatDAO {
String recordServerCtx(ServerContext ctx) throws HeartBeatDAOException;
boolean recordHeatBeat(HeartBeatEvent event) throws HeartBeatDAOException;
String retrieveExistingServerCtx(ServerContext ctx) throws HeartBeatDAOException;
int getActiveServerCount(int elapsedTimeInSeconds) throws HeartBeatDAOException;
List<ServerContext> getActiveServerDetails(int elapsedTimeInSeconds) throws HeartBeatDAOException;
}

@ -0,0 +1,78 @@
/*
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.server.bootup.heartbeat.beacon.dao.exception;
/**
* Custom exception class for group management data access related exceptions.
*/
public class HeartBeatDAOException extends Exception {
private static final long serialVersionUID = 2021891706072918864L;
private String message;
/**
* Constructs a new exception with the specified detail message and nested exception.
*
* @param message error message
* @param nestedException exception
*/
public HeartBeatDAOException(String message, Exception nestedException) {
super(message, nestedException);
setErrorMessage(message);
}
/**
* Constructs a new exception with the specified detail message and cause.
*
* @param message the detail message.
* @param cause the cause of this exception.
*/
public HeartBeatDAOException(String message, Throwable cause) {
super(message, cause);
setErrorMessage(message);
}
/**
* Constructs a new exception with the specified detail message
*
* @param message the detail message.
*/
public HeartBeatDAOException(String message) {
super(message);
setErrorMessage(message);
}
/**
* Constructs a new exception with the specified and cause.
*
* @param cause the cause of this exception.
*/
public HeartBeatDAOException(Throwable cause) {
super(cause);
}
public String getMessage() {
return message;
}
public void setErrorMessage(String errorMessage) {
this.message = errorMessage;
}
}

@ -0,0 +1,166 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.server.bootup.heartbeat.beacon.dao.impl;
import io.entgra.server.bootup.heartbeat.beacon.dao.HeartBeatBeaconDAOFactory;
import io.entgra.server.bootup.heartbeat.beacon.dao.HeartBeatDAO;
import io.entgra.server.bootup.heartbeat.beacon.dao.exception.HeartBeatDAOException;
import io.entgra.server.bootup.heartbeat.beacon.dao.util.HeartBeatBeaconDAOUtil;
import io.entgra.server.bootup.heartbeat.beacon.dto.HeartBeatEvent;
import io.entgra.server.bootup.heartbeat.beacon.dto.ServerContext;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* This class represents implementation of HeartBeatDAO
*/
public class GenericHeartBeatDAOImpl implements HeartBeatDAO {
@Override
public String recordServerCtx(ServerContext ctx) throws HeartBeatDAOException {
PreparedStatement stmt = null;
String uuid = null;
try {
Connection conn = HeartBeatBeaconDAOFactory.getConnection();
String sql;
sql = "INSERT INTO SERVER_HEART_BEAT_EVENTS(HOST_NAME, MAC, UUID) VALUES (?, ?, ?)";
stmt = conn.prepareStatement(sql, new String[]{"UUID"});
stmt.setString(1, ctx.getHostName());
stmt.setString(2, ctx.getMacAddress());
stmt.setString(3, UUID.randomUUID().toString());
stmt.executeUpdate();
ResultSet result = stmt.getGeneratedKeys();
if (result.next()){
uuid = result.getString("UUID");
}
} catch (SQLException e) {
throw new HeartBeatDAOException("Error occurred while persisting server context for : '" +
"mac '" + ctx.getMacAddress() + "' " +
"hostname : '" + ctx.getHostName() + "' ", e);
} finally {
HeartBeatBeaconDAOUtil.cleanupResources(stmt, null);
}
return uuid;
}
@Override
public boolean recordHeatBeat(HeartBeatEvent event) throws HeartBeatDAOException {
PreparedStatement stmt = null;
try {
Connection conn = HeartBeatBeaconDAOFactory.getConnection();
String sql;
sql = "UPDATE SERVER_HEART_BEAT_EVENTS SET LAST_UPDATED_TIMESTAMP = ? WHERE UUID = ?";
stmt = conn.prepareStatement(sql, new String[]{"ID"});
stmt.setTimestamp(1, event.getTime());
stmt.setString(2, event.getServerUUID());
return stmt.executeUpdate() > 0;
} catch (SQLException e) {
throw new HeartBeatDAOException("Error occurred while updating heartbeat event against server with UUID : '" +
event.getServerUUID() + "' and timestamp " + event.getTime(), e);
} finally {
HeartBeatBeaconDAOUtil.cleanupResources(stmt, null);
}
}
@Override
public String retrieveExistingServerCtx(ServerContext ctx) throws HeartBeatDAOException {
PreparedStatement stmt = null;
ResultSet resultSet = null;
String uuid = null;
try {
Connection conn = HeartBeatBeaconDAOFactory.getConnection();
String sql = "SELECT UUID FROM SERVER_HEART_BEAT_EVENTS WHERE HOST_NAME = ? AND MAC = ?";
stmt = conn.prepareStatement(sql, new String[]{"UUID"});
stmt.setString(1, ctx.getHostName());
stmt.setString(2, ctx.getMacAddress());
resultSet = stmt.executeQuery();
if (resultSet.next()){
uuid = resultSet.getString("UUID");
}
} catch (SQLException e) {
throw new HeartBeatDAOException("Error occurred while retrieving meta information for heart beat event from " +
"mac '" + ctx.getMacAddress() + "' " +
"hostname : '" + ctx.getHostName() + "' ", e);
} finally {
HeartBeatBeaconDAOUtil.cleanupResources(stmt, resultSet);
}
return uuid;
}
@Override
public int getActiveServerCount(int elapsedTimeInSeconds) throws HeartBeatDAOException {
PreparedStatement stmt = null;
ResultSet resultSet = null;
int count = -1;
try {
Connection conn = HeartBeatBeaconDAOFactory.getConnection();
String sql = "SELECT COUNT(ID) AS COUNT from SERVER_HEART_BEAT_EVENTS WHERE " +
"LAST_UPDATED_TIMESTAMP > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL ? SECOND)";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, elapsedTimeInSeconds);
resultSet = stmt.executeQuery();
if (resultSet.next()) {
count = resultSet.getInt("COUNT");
}
} catch (SQLException e) {
throw new HeartBeatDAOException("Error occurred while retrieving acting server count with " +
"heartbeat updates within " + elapsedTimeInSeconds + " seconds.", e);
} finally {
HeartBeatBeaconDAOUtil.cleanupResources(stmt, resultSet);
}
return count;
}
@Override
public List<ServerContext> getActiveServerDetails(int elapsedTimeInSeconds) throws HeartBeatDAOException {
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<ServerContext> ctxList = new ArrayList<>();
try {
Connection conn = HeartBeatBeaconDAOFactory.getConnection();
String sql = "SELECT (@row_number:=@row_number + 1) AS IDX, UUID, HOST_NAME, MAC from " +
"SERVER_HEART_BEAT_EVENTS WHERE LAST_UPDATED_TIMESTAMP > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL ? SECOND) " +
"ORDER BY UUID";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, elapsedTimeInSeconds);
stmt.execute("SET @row_number = 0");
resultSet = stmt.executeQuery();
while (resultSet.next()) {
ctxList.add(HeartBeatBeaconDAOUtil.populateContext(resultSet));
}
} catch (SQLException e) {
throw new HeartBeatDAOException("Error occurred while retrieving acting server count with " +
"heartbeat updates within " + elapsedTimeInSeconds + " seconds.", e);
} finally {
HeartBeatBeaconDAOUtil.cleanupResources(stmt, resultSet);
}
return ctxList;
}
}

@ -0,0 +1,91 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.server.bootup.heartbeat.beacon.dao.util;
import io.entgra.server.bootup.heartbeat.beacon.dto.ServerContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Hashtable;
/**
* This class represents utilities required to work with group management data
*/
public final class HeartBeatBeaconDAOUtil {
private static final Log log = LogFactory.getLog(HeartBeatBeaconDAOUtil.class);
/**
* Cleanup resources used to transaction
*
* @param stmt Prepared statement used
* @param rs Obtained results set
*/
public static void cleanupResources(PreparedStatement stmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
log.warn("Error occurred while closing result set", e);
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
log.warn("Error occurred while closing prepared statement", e);
}
}
}
/**
* Lookup datasource using name and jndi properties
*
* @param dataSourceName Name of datasource to lookup
* @param jndiProperties Hash table of JNDI Properties
* @return datasource looked
*/
public static DataSource lookupDataSource(String dataSourceName,
final Hashtable<Object, Object> jndiProperties) {
try {
if (jndiProperties == null || jndiProperties.isEmpty()) {
return (DataSource) InitialContext.doLookup(dataSourceName);
}
final InitialContext context = new InitialContext(jndiProperties);
return (DataSource) context.lookup(dataSourceName);
} catch (Exception e) {
throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e);
}
}
public static ServerContext populateContext(ResultSet resultSet) throws SQLException {
ServerContext ctx = new ServerContext();
ctx.setIndex(resultSet.getInt("IDX"));
ctx.setUuid(resultSet.getString("UUID"));
ctx.setHostName(resultSet.getString("HOST_NAME"));
ctx.setMacAddress(resultSet.getString("MAC"));
return ctx;
}
}

@ -0,0 +1,49 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.server.bootup.heartbeat.beacon.dto;
import java.sql.Timestamp;
public class HeartBeatEvent {
private String serverUUID;
private Timestamp time;
public HeartBeatEvent(String serverUUID){
this.serverUUID = serverUUID;
this.time = new Timestamp(System.currentTimeMillis());
}
public String getServerUUID() {
return serverUUID;
}
public void setServerUUID(String serverUUID) {
this.serverUUID = serverUUID;
}
public Timestamp getTime() {
return time;
}
public void setTime(Timestamp time) {
this.time = time;
}
}

@ -0,0 +1,68 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.server.bootup.heartbeat.beacon.dto;
public class ServerContext {
private String hostName;
private String macAddress;
private String serverHash;
private String uuid;
private int index;
public String getHostName() {
return hostName;
}
public void setHostName(String hostName) {
this.hostName = hostName;
}
public String getMacAddress() {
return macAddress;
}
public void setMacAddress(String macAddress) {
this.macAddress = macAddress;
}
public String getServerHash() {
return serverHash;
}
public void setServerHash(String serverHash) {
this.serverHash = serverHash;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}

@ -0,0 +1,45 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.server.bootup.heartbeat.beacon.exception;
public class HeartBeatManagementException extends Exception {
private static final long serialVersionUID = 5304352685379661115L;
public HeartBeatManagementException(String msg, Exception nestedEx) {
super(msg, nestedEx);
}
public HeartBeatManagementException(String message, Throwable cause) {
super(message, cause);
}
public HeartBeatManagementException(String msg) {
super(msg);
}
public HeartBeatManagementException() {
super();
}
public HeartBeatManagementException(Throwable cause) {
super(cause);
}
}

@ -0,0 +1,74 @@
/*
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.server.bootup.heartbeat.beacon.exception;
public class InvalidConfigurationStateException extends RuntimeException {
private static final long serialVersionUID = -3151279311329070297L;
private String errorMessage;
private int errorCode;
public InvalidConfigurationStateException(int errorCode, String message) {
super(message);
this.errorCode = errorCode;
}
public InvalidConfigurationStateException(int errorCode, String message, Throwable cause) {
super(message, cause);
this.errorCode = errorCode;
}
public int getErrorCode() {
return errorCode;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public InvalidConfigurationStateException(String msg, Exception nestedEx) {
super(msg, nestedEx);
setErrorMessage(msg);
}
public InvalidConfigurationStateException(String message, Throwable cause) {
super(message, cause);
setErrorMessage(message);
}
public InvalidConfigurationStateException(String msg) {
super(msg);
setErrorMessage(msg);
}
public InvalidConfigurationStateException() {
super();
}
public InvalidConfigurationStateException(Throwable cause) {
super(cause);
}
}

@ -0,0 +1,45 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.server.bootup.heartbeat.beacon.exception;
public class ServerStatusUpdationFailedException extends Exception {
private static final long serialVersionUID = -2610630531027402610L;
public ServerStatusUpdationFailedException(String msg, Exception nestedEx) {
super(msg, nestedEx);
}
public ServerStatusUpdationFailedException(String message, Throwable cause) {
super(message, cause);
}
public ServerStatusUpdationFailedException(String msg) {
super(msg);
}
public ServerStatusUpdationFailedException() {
super();
}
public ServerStatusUpdationFailedException(Throwable cause) {
super(cause);
}
}

@ -0,0 +1,72 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.server.bootup.heartbeat.beacon.internal;
import io.entgra.server.bootup.heartbeat.beacon.HeartBeatBeaconConfig;
import io.entgra.server.bootup.heartbeat.beacon.HeartBeatBeaconUtils;
import io.entgra.server.bootup.heartbeat.beacon.service.HeartBeatManagementService;
import io.entgra.server.bootup.heartbeat.beacon.service.HeartBeatManagementServiceImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.service.component.ComponentContext;
/**
* @scr.component name="io.entgra.server.bootup.heartbeat.beacon.heartbeatBeaconComponent"
* immediate="true"
*/
public class HeartBeatBeaconComponent {
private static Log log = LogFactory.getLog(HeartBeatBeaconComponent.class);
@SuppressWarnings("unused")
protected void activate(ComponentContext componentContext) {
try {
if (log.isDebugEnabled()) {
log.debug("Initializing email sender core bundle");
}
//heart beat notifier configuration */
HeartBeatBeaconConfig.init();
this.registerHeartBeatServices(componentContext);
//Setting up executors to notify heart beat status */
HeartBeatInternalUtils.setUpNotifiers(HeartBeatBeaconUtils.getServerDetails());
if (log.isDebugEnabled()) {
log.debug("Email sender core bundle has been successfully initialized");
}
} catch (Throwable e) {
log.error("Error occurred while initializing email sender core bundle", e);
}
}
@SuppressWarnings("unused")
protected void deactivate(ComponentContext componentContext) {
//do nothing
}
private void registerHeartBeatServices(ComponentContext componentContext) {
if (log.isDebugEnabled()) {
log.debug("Registering Heart Beat Management service");
}
HeartBeatManagementService heartBeatServiceProvider = new HeartBeatManagementServiceImpl();
HeartBeatBeaconDataHolder.getInstance().setHeartBeatManagementService(heartBeatServiceProvider);
componentContext.getBundleContext().registerService(HeartBeatManagementService.class, heartBeatServiceProvider, null);
}
}

@ -0,0 +1,52 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.server.bootup.heartbeat.beacon.internal;
import io.entgra.server.bootup.heartbeat.beacon.dto.ServerContext;
import io.entgra.server.bootup.heartbeat.beacon.service.HeartBeatManagementService;
public class HeartBeatBeaconDataHolder {
private HeartBeatManagementService heartBeatManagementService;
private String localServerUUID;
private static HeartBeatBeaconDataHolder thisInstance = new HeartBeatBeaconDataHolder();
private HeartBeatBeaconDataHolder() {}
public static HeartBeatBeaconDataHolder getInstance() {
return thisInstance;
}
public HeartBeatManagementService getHeartBeatManagementService() {
return heartBeatManagementService;
}
public void setHeartBeatManagementService(HeartBeatManagementService heartBeatManagementService) {
this.heartBeatManagementService = heartBeatManagementService;
}
public String getLocalServerUUID() {
return localServerUUID;
}
public void setLocalServerUUID(String localServerUUID) {
this.localServerUUID = localServerUUID;
}
}

@ -0,0 +1,77 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.server.bootup.heartbeat.beacon.internal;
import com.sun.security.ntlm.Server;
import io.entgra.server.bootup.heartbeat.beacon.HeartBeatBeaconConfig;
import io.entgra.server.bootup.heartbeat.beacon.HeartBeatBeaconConfigurationException;
import io.entgra.server.bootup.heartbeat.beacon.dto.HeartBeatEvent;
import io.entgra.server.bootup.heartbeat.beacon.dto.ServerContext;
import io.entgra.server.bootup.heartbeat.beacon.exception.HeartBeatManagementException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class HeartBeatInternalUtils {
private static Log log = LogFactory.getLog(HeartBeatInternalUtils.class);
private static final int DEFAULT__NOTIFIER_INTERVAL = 5;
private static final int DEFAULT_NOTIFIER_DELAY = 5;
private static HeartBeatBeaconConfig CONFIG;
static {
CONFIG = HeartBeatBeaconConfig.getInstance();
}
static void setUpNotifiers(ServerContext ctx) throws HeartBeatBeaconConfigurationException {
ScheduledExecutorService executor =
Executors.newSingleThreadScheduledExecutor();
if(CONFIG == null){
throw new HeartBeatBeaconConfigurationException("Error while initiating schedule taks for recording heartbeats.");
}
try {
String uuid = HeartBeatBeaconDataHolder.getInstance().getHeartBeatManagementService().updateServerContext(ctx);
HeartBeatBeaconDataHolder.getInstance().setLocalServerUUID(uuid);
Runnable periodicTask = new Runnable() {
public void run() {
try {
recordHeartBeat(uuid);
} catch (HeartBeatManagementException e) {
log.error("Error while executing record heart beat task. This will result in schedule operation malfunction.", e);
}
}
};
executor.scheduleAtFixedRate(periodicTask,
CONFIG.getNotifierDelay() != 0 ? CONFIG.getNotifierDelay() : DEFAULT_NOTIFIER_DELAY,
CONFIG.getNotifierFrequency() != 0 ? CONFIG.getNotifierFrequency() : DEFAULT__NOTIFIER_INTERVAL,
TimeUnit.SECONDS);
} catch (HeartBeatManagementException e) {
throw new HeartBeatBeaconConfigurationException("Error occured while updating initial server context.");
}
}
static void recordHeartBeat(String uuid) throws HeartBeatManagementException {
HeartBeatBeaconDataHolder.getInstance().getHeartBeatManagementService().recordHeartBeat(new HeartBeatEvent(uuid));
}
}

@ -0,0 +1,34 @@
/*
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.server.bootup.heartbeat.beacon.service;
import io.entgra.server.bootup.heartbeat.beacon.dto.HeartBeatEvent;
import io.entgra.server.bootup.heartbeat.beacon.exception.HeartBeatManagementException;
import io.entgra.server.bootup.heartbeat.beacon.dto.ServerContext;
public interface HeartBeatManagementService {
int getActiveServerCount() throws HeartBeatManagementException;
int getServerLocalHashIndex() throws HeartBeatManagementException;
String updateServerContext(ServerContext ctx) throws HeartBeatManagementException;
boolean recordHeartBeat(HeartBeatEvent event) throws HeartBeatManagementException;
}

@ -0,0 +1,135 @@
/*
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 io.entgra.server.bootup.heartbeat.beacon.service;
import io.entgra.server.bootup.heartbeat.beacon.HeartBeatBeaconConfig;
import io.entgra.server.bootup.heartbeat.beacon.dao.HeartBeatBeaconDAOFactory;
import io.entgra.server.bootup.heartbeat.beacon.dao.HeartBeatDAO;
import io.entgra.server.bootup.heartbeat.beacon.dao.exception.HeartBeatDAOException;
import io.entgra.server.bootup.heartbeat.beacon.dto.HeartBeatEvent;
import io.entgra.server.bootup.heartbeat.beacon.exception.HeartBeatManagementException;
import io.entgra.server.bootup.heartbeat.beacon.dto.ServerContext;
import io.entgra.server.bootup.heartbeat.beacon.internal.HeartBeatBeaconDataHolder;
import java.sql.SQLException;
import java.util.List;
public class HeartBeatManagementServiceImpl implements HeartBeatManagementService {
@Override
public int getActiveServerCount() throws HeartBeatManagementException {
HeartBeatDAO heartBeatDAO;
int activeServerCount = -1;
try {
HeartBeatBeaconDAOFactory.openConnection();
heartBeatDAO = HeartBeatBeaconDAOFactory.getHeartBeatDAO();
int timeOutIntervalInSeconds = HeartBeatBeaconConfig.getInstance().getServerTimeOutIntervalInSeconds();
activeServerCount = heartBeatDAO.getActiveServerCount(timeOutIntervalInSeconds);
} catch (SQLException e) {
String msg = "Error occurred while opening a connection to the underlying data source";
throw new HeartBeatManagementException(msg, e);
} catch (HeartBeatDAOException e) {
String msg = "Error Occured while retrieving active server count.";
throw new HeartBeatManagementException(msg, e);
} finally {
HeartBeatBeaconDAOFactory.closeConnection();
}
return activeServerCount;
}
@Override
public int getServerLocalHashIndex() throws HeartBeatManagementException {
HeartBeatDAO heartBeatDAO;
int hashIndex = -1;
ServerContext localServerCtx = null;
try {
HeartBeatBeaconDAOFactory.openConnection();
heartBeatDAO = HeartBeatBeaconDAOFactory.getHeartBeatDAO();
int timeOutIntervalInSeconds = HeartBeatBeaconConfig.getInstance().getServerTimeOutIntervalInSeconds();
String localServerUUID = HeartBeatBeaconDataHolder.getInstance().getLocalServerUUID();
List<ServerContext> serverCtxList = heartBeatDAO.getActiveServerDetails(timeOutIntervalInSeconds);
for(ServerContext ctx : serverCtxList){
if(ctx.getUuid() == localServerUUID){
localServerCtx = ctx;
break;
}
}
if(localServerCtx != null){
hashIndex = localServerCtx.getIndex();
}
} catch (SQLException e) {
String msg = "Error occurred while opening a connection to the underlying data source";
throw new HeartBeatManagementException(msg, e);
} catch (HeartBeatDAOException e) {
String msg = "Error Occured while retrieving active server count.";
throw new HeartBeatManagementException(msg, e);
} finally {
HeartBeatBeaconDAOFactory.closeConnection();
}
return hashIndex;
}
@Override
public String updateServerContext(ServerContext ctx) throws HeartBeatManagementException {
HeartBeatDAO heartBeatDAO;
String uuid = null;
try {
HeartBeatBeaconDAOFactory.openConnection();
heartBeatDAO = HeartBeatBeaconDAOFactory.getHeartBeatDAO();
uuid = heartBeatDAO.retrieveExistingServerCtx(ctx);
if(uuid == null){
uuid = heartBeatDAO.recordServerCtx(ctx);
}
} catch (SQLException e) {
String msg = "Error occurred while opening a connection to the underlying data source";
throw new HeartBeatManagementException(msg, e);
} catch (HeartBeatDAOException e) {
String msg = "Error Occured while retrieving active server count.";
throw new HeartBeatManagementException(msg, e);
} finally {
HeartBeatBeaconDAOFactory.closeConnection();
}
return uuid;
}
@Override
public boolean recordHeartBeat(HeartBeatEvent event) throws HeartBeatManagementException {
HeartBeatDAO heartBeatDAO;
boolean operationSuccess = false;
try {
HeartBeatBeaconDAOFactory.openConnection();
heartBeatDAO = HeartBeatBeaconDAOFactory.getHeartBeatDAO();
operationSuccess = heartBeatDAO.recordHeatBeat(event);
} catch (SQLException e) {
String msg = "Error occurred while opening a connection to the underlying data source";
throw new HeartBeatManagementException(msg, e);
} catch (HeartBeatDAOException e) {
String msg = "Error Occured while retrieving active server count.";
throw new HeartBeatManagementException(msg, e);
} finally {
HeartBeatBeaconDAOFactory.closeConnection();
}
return operationSuccess;
}
}

@ -0,0 +1,32 @@
#
# Copyright 2009 WSO2, Inc. (http://wso2.com)
#
# 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.
#
#
# This is the log4j configuration file used by WSO2 Carbon
#
# IMPORTANT : Please do not remove or change the names of any
# of the Appenders defined here. The layout pattern & log file
# can be changed using the WSO2 Carbon Management Console, and those
# settings will override the settings in this file.
#
log4j.rootLogger=INFO, STD_OUT
# Redirect log messages to console
log4j.appender.STD_OUT=org.apache.log4j.ConsoleAppender
log4j.appender.STD_OUT.Target=System.out
log4j.appender.STD_OUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STD_OUT.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

@ -0,0 +1,29 @@
<!--
~ Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
~
~ WSO2 Inc. licenses this file to you 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.
-->
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="DeviceManagementCore">
<parameter name="useDefaultListeners" value="false"/>
<test name="HeartBeatBeaconTests" preserve-order="true">
<classes>
</classes>
</test>
</suite>

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
~
~ WSO2 Inc. licenses this file to you 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">
<parent>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>carbon-devicemgt</artifactId>
<version>4.1.11-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>task-allocation</artifactId>
<packaging>pom</packaging>
<name>Entgra - Task Allocation Framework</name>
<url>http://wso2.org</url>
<modules>
<module>io.entgra.dynamic.task.allocation.framework</module>
<module>io.entgra.server.bootup.heartbeat.beacon</module>
</modules>
</project>

@ -45,6 +45,7 @@
<module>components/webapp-authenticator-framework</module> <module>components/webapp-authenticator-framework</module>
<module>components/email-sender</module> <module>components/email-sender</module>
<module>components/ui-request-interceptor</module> <module>components/ui-request-interceptor</module>
<module>components/task-allocation</module>
<module>features/device-mgt</module> <module>features/device-mgt</module>
<module>features/apimgt-extensions</module> <module>features/apimgt-extensions</module>
<module>features/application-mgt</module> <module>features/application-mgt</module>

Loading…
Cancel
Save