<commit_before>/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wso2.analytics.apim.file.adapter;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.analytics.apim.file.adapter.internal.ds.FileEventAdapterServiceValueHolder;
import org.wso2.analytics.apim.file.adapter.task.UploadedUsagePublisherExecutorTask;
import org.wso2.analytics.apim.file.adapter.util.FileEventAdapterConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.event.input.adapter.core.EventAdapterConstants;
import org.wso2.carbon.event.input.adapter.core.InputEventAdapter;
import org.wso2.carbon.event.input.adapter.core.InputEventAdapterConfiguration;
import org.wso2.carbon.event.input.adapter.core.InputEventAdapterListener;
import org.wso2.carbon.event.input.adapter.core.exception.InputEventAdapterException;
import org.wso2.carbon.event.input.adapter.core.exception.InputEventAdapterRuntimeException;
import org.wso2.carbon.event.input.adapter.core.exception.TestConnectionNotSupportedException;

import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class FileEventAdapter implements InputEventAdapter {

    private static final Log log = LogFactory.getLog(FileEventAdapter.class);
    private final InputEventAdapterConfiguration eventAdapterConfiguration;
    private final Map<String, String> globalProperties;
    private InputEventAdapterListener inputEventAdapterListener;
    String tenantDomain;
    String streamId;
    int tenantId;

    public FileEventAdapter(InputEventAdapterConfiguration eventAdapterConfiguration,
            Map<String, String> globalProperties) {
        this.eventAdapterConfiguration = eventAdapterConfiguration;
        this.globalProperties = globalProperties;
    }

    @Override
    public void init(InputEventAdapterListener inputEventAdapterListener) throws InputEventAdapterException {
        this.inputEventAdapterListener = inputEventAdapterListener;
    }

    @Override
    public void testConnect() throws TestConnectionNotSupportedException {
        throw new TestConnectionNotSupportedException("not-supported");
    }

    @Override
    public void connect() throws InputEventAdapterRuntimeException {
        tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true);
        tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
        streamId = eventAdapterConfiguration.getInputStreamIdOfWso2eventMessageFormat();
        FileEventAdapterServiceValueHolder.registerAdapterService(tenantDomain,streamId, this);
        readFileFromDatabase();
    }

    private void readFileFromDatabase() {
        TimerTask usagePublisherTask = new UploadedUsagePublisherExecutorTask(tenantDomain, tenantId);
        Timer timer = new Timer();
        String usagePublishFrequency = System
                .getProperty(FileEventAdapterConstants.UPLOADED_USAGE_PUBLISH_FREQUENCY_PROPERTY);
        if(StringUtils.isEmpty(usagePublishFrequency)) {
            log.debug("Default usage publishing frequency will be used");
            usagePublishFrequency = FileEventAdapterConstants.DEFAULT_UPLOADED_USAGE_PUBLISH_FREQUENCY;
        }
        timer.schedule(usagePublisherTask, 1000, Long.parseLong(usagePublishFrequency));
    }

    @Override
    public void disconnect() {
        String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true);
        String streamId = eventAdapterConfiguration.getInputStreamIdOfWso2eventMessageFormat();
        FileEventAdapterServiceValueHolder.unregisterAdapterService(tenantDomain, streamId, this);
    }

    @Override
    public void destroy() {

    }

    @Override
    public boolean isEventDuplicatedInCluster() {
        return Boolean.parseBoolean(eventAdapterConfiguration.getProperties().get(EventAdapterConstants.EVENTS_DUPLICATED_IN_CLUSTER));<commit_msg>Fix more formatting issues<commit_after>/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wso2.analytics.apim.file.adapter;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.analytics.apim.file.adapter.internal.ds.FileEventAdapterServiceValueHolder;
import org.wso2.analytics.apim.file.adapter.task.UploadedUsagePublisherExecutorTask;
import org.wso2.analytics.apim.file.adapter.util.FileEventAdapterConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.event.input.adapter.core.EventAdapterConstants;
import org.wso2.carbon.event.input.adapter.core.InputEventAdapter;
import org.wso2.carbon.event.input.adapter.core.InputEventAdapterConfiguration;
import org.wso2.carbon.event.input.adapter.core.InputEventAdapterListener;
import org.wso2.carbon.event.input.adapter.core.exception.InputEventAdapterException;
import org.wso2.carbon.event.input.adapter.core.exception.InputEventAdapterRuntimeException;
import org.wso2.carbon.event.input.adapter.core.exception.TestConnectionNotSupportedException;

import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class FileEventAdapter implements InputEventAdapter {

    private static final Log log = LogFactory.getLog(FileEventAdapter.class);
    private final InputEventAdapterConfiguration eventAdapterConfiguration;
    private final Map<String, String> globalProperties;
    private InputEventAdapterListener inputEventAdapterListener;
    String tenantDomain;
    String streamId;
    int tenantId;

    public FileEventAdapter(InputEventAdapterConfiguration eventAdapterConfiguration,
            Map<String, String> globalProperties) {
        this.eventAdapterConfiguration = eventAdapterConfiguration;
        this.globalProperties = globalProperties;
    }

    @Override
    public void init(InputEventAdapterListener inputEventAdapterListener) throws InputEventAdapterException {
        this.inputEventAdapterListener = inputEventAdapterListener;
    }

    @Override
    public void testConnect() throws TestConnectionNotSupportedException {
        throw new TestConnectionNotSupportedException("not-supported");
    }

    @Override
    public void connect() throws InputEventAdapterRuntimeException {
        tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true);
        tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
        streamId = eventAdapterConfiguration.getInputStreamIdOfWso2eventMessageFormat();
        FileEventAdapterServiceValueHolder.registerAdapterService(tenantDomain,streamId, this);
        readFileFromDatabase();
    }

    private void readFileFromDatabase() {
        TimerTask usagePublisherTask = new UploadedUsagePublisherExecutorTask(tenantDomain, tenantId);
        Timer timer = new Timer();
        String usagePublishFrequency = System
                .getProperty(FileEventAdapterConstants.UPLOADED_USAGE_PUBLISH_FREQUENCY_PROPERTY);
        if(StringUtils.isEmpty(usagePublishFrequency)) {
            log.debug("Default usage publishing frequency will be used");
            usagePublishFrequency = FileEventAdapterConstants.DEFAULT_UPLOADED_USAGE_PUBLISH_FREQUENCY;
        }
        timer.schedule(usagePublisherTask, 1000, Long.parseLong(usagePublishFrequency));
    }

    @Override
    public void disconnect() {
        String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true);
        String streamId = eventAdapterConfiguration.getInputStreamIdOfWso2eventMessageFormat();
        FileEventAdapterServiceValueHolder.unregisterAdapterService(tenantDomain, streamId, this);
    }

    @Override
    public void destroy() {

    }

    @Override public boolean isEventDuplicatedInCluster() {
        return Boolean.parseBoolean(
                eventAdapterConfiguration.getProperties().get(EventAdapterConstants.EVENTS_DUPLICATED_IN_CLUSTER));
{'fraction_non_alphanumeric': 0.07564857205144974, 'fraction_numerical': 0.006104207543056464, 'mean_word_length': 4.90032154340836, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2150438', 'n_tokens_mistral': 2438, 'n_tokens_neox': 2359, 'n_words': 499}