org.wso2.carbon.data.provider.AbstractDataProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.wso2.carbon.data.provider Show documentation
Show all versions of org.wso2.carbon.data.provider Show documentation
Used to provide data for different clients from different sources according to the requirement
/*
* 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 org.wso2.carbon.data.provider;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.data.provider.bean.DataModel;
import org.wso2.carbon.data.provider.bean.DataSetMetadata;
import org.wso2.carbon.data.provider.endpoint.DataProviderEndPoint;
import org.wso2.carbon.data.provider.exception.DataProviderException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* Abstract data provider class.
*/
public abstract class AbstractDataProvider implements DataProvider {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractDataProvider.class);
private String topic;
private String sessionId;
private ScheduledExecutorService dataPublishingExecutorService = Executors.newSingleThreadScheduledExecutor();
private ScheduledExecutorService dataPurgingExecutorService = Executors.newSingleThreadScheduledExecutor();
private long publishingInterval;
private long purgingInterval;
private boolean isPurgingEnable;
private boolean isPaginationEnabled;
public DataProvider init(String topic, String sessionId, ProviderConfig providerConfig)
throws DataProviderException {
if (this.configValidator(providerConfig)) {
this.topic = topic;
this.sessionId = sessionId;
this.publishingInterval = providerConfig.getPublishingInterval();
this.purgingInterval = providerConfig.getPurgingInterval();
this.isPurgingEnable = providerConfig.isPurgingEnable();
this.isPaginationEnabled = providerConfig.isPaginationEnabled();
this.setProviderConfig(providerConfig);
return this;
} else {
throw new DataProviderException("Invalid configuration provided. Unable to complete initialization " +
"of data provider.");
}
}
@Override
public void stop() {
dataPublishingExecutorService.shutdown();
dataPurgingExecutorService.shutdown();
}
@Override
public void start() {
if (!this.isPaginationEnabled) {
this.dataPublishingExecutorService.scheduleAtFixedRate(() -> {
this.publish(this.topic, this.sessionId);
}, 0L, this.publishingInterval, TimeUnit.SECONDS);
}
if (isPurgingEnable) {
dataPublishingExecutorService.scheduleAtFixedRate(() -> {
purging();
}, 0, purgingInterval, TimeUnit.SECONDS);
}
}
public void publishToEndPoint(ArrayList