<commit_before>package org.sputnikdev.bluetooth.manager.transport.tinyb;

/*-
 * #%L
 * org.sputnikdev:bluetooth-manager-tinyb
 * %%
 * Copyright (C) 2017 Sputnik Dev
 * %%
 * 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.
 * #L%
 */

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sputnikdev.bluetooth.URL;
import org.sputnikdev.bluetooth.manager.DiscoveredAdapter;
import org.sputnikdev.bluetooth.manager.DiscoveredDevice;
import org.sputnikdev.bluetooth.manager.transport.Adapter;
import org.sputnikdev.bluetooth.manager.transport.BluetoothObjectFactory;
import org.sputnikdev.bluetooth.manager.transport.Characteristic;
import org.sputnikdev.bluetooth.manager.transport.Device;
import tinyb.BluetoothAdapter;
import tinyb.BluetoothDevice;
import tinyb.BluetoothGattCharacteristic;
import tinyb.BluetoothGattService;
import tinyb.BluetoothManager;
import tinyb.BluetoothType;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

/**
 * A Bluetooth Manager Transport abstraction layer implementation based on TinyB library.
 * @author <NAME>
 */
public class TinyBFactory implements BluetoothObjectFactory {

    public static final String TINYB_PROTOCOL_NAME = "tinyb";

    private static final Logger LOGGER = LoggerFactory.getLogger(TinyBFactory.class);

    private static final ExecutorService NOTIFICATION_SERVICE = Executors.newCachedThreadPool();

    /**
     * Loads TinyB native libraries from classpath by copying them to a temp folder.
     * @return true if all libraries succesefully loaded, false otherwise
     */
    public static boolean loadNativeLibraries() {
        if (NativesLoader.isSupportedEnvironment()) {
            try {
                System.load(NativesLoader.prepare("libtinyb.so")); // $COVERAGE-IGNORE$
                System.load(NativesLoader.prepare("libjavatinyb.so")); // $COVERAGE-IGNORE$
                return true; // $COVERAGE-IGNORE$
            } catch (Throwable e) {
                LOGGER.info("Could not load TinyB native libraries.", e);
                return false;
            }
        }
        LOGGER.info("TinyB: environemnt is not supported. Only Linux OS; x86, x86_64 and arm6 architectures; "
            + "are supported.");
        return false;
    }

    @Override
    public Adapter getAdapter(URL url) {
        BluetoothAdapter adapter = (BluetoothAdapter) BluetoothManager.getBluetoothManager().getObject(
                BluetoothType.ADAPTER, null, url.getAdapterAddress(), null);
        return adapter != null ? new TinyBAdapter(adapter) : null;
    }

    @Override
    public Device getDevice(URL url) {
        BluetoothAdapter adapter = (BluetoothAdapter) BluetoothManager.getBluetoothManager().getObject(
                BluetoothType.ADAPTER, null, url.getAdapterAddress(), null);
        if (adapter == null) {
            return null;
        }
        BluetoothDevice device = (BluetoothDevice) BluetoothManager.getBluetoothManager().getObject(
                BluetoothType.DEVICE, null, url.getDeviceAddress(), adapter);
        return device != null ? new TinyBDevice(device) : null;
    }

    @Override
    public Characteristic getCharacteristic(URL url) {
        BluetoothAdapter adapter = (BluetoothAdapter) BluetoothManager.getBluetoothManager().getObject(
                BluetoothType.ADAPTER, null, url.getAdapterAddress(), null);<commit_msg>Try loading system native libraries when bundled not available<commit_after>package org.sputnikdev.bluetooth.manager.transport.tinyb;

/*-
 * #%L
 * org.sputnikdev:bluetooth-manager-tinyb
 * %%
 * Copyright (C) 2017 <NAME>
 * %%
 * 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.
 * #L%
 */

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sputnikdev.bluetooth.URL;
import org.sputnikdev.bluetooth.manager.DiscoveredAdapter;
import org.sputnikdev.bluetooth.manager.DiscoveredDevice;
import org.sputnikdev.bluetooth.manager.transport.Adapter;
import org.sputnikdev.bluetooth.manager.transport.BluetoothObjectFactory;
import org.sputnikdev.bluetooth.manager.transport.Characteristic;
import org.sputnikdev.bluetooth.manager.transport.Device;
import tinyb.BluetoothAdapter;
import tinyb.BluetoothDevice;
import tinyb.BluetoothGattCharacteristic;
import tinyb.BluetoothGattService;
import tinyb.BluetoothManager;
import tinyb.BluetoothType;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

/**
 * A Bluetooth Manager Transport abstraction layer implementation based on TinyB library.
 * @author <NAME>
 */
public class TinyBFactory implements BluetoothObjectFactory {

    public static final String TINYB_PROTOCOL_NAME = "tinyb";

    private static final Logger LOGGER = LoggerFactory.getLogger(TinyBFactory.class);

    private static final ExecutorService NOTIFICATION_SERVICE = Executors.newCachedThreadPool();

    /**
     * Loads TinyB bundeled native libraries from classpath by copying them to a temp folder.
     * @return true if all libraries succesefully loaded, false otherwise
     */
    private static boolean loadBundeledNativeLibraries() {
        if (!NativesLoader.isSupportedEnvironment()) {
            return false;
        }

        final String[] libs = {"libtinyb.so", "libjavatinyb.so"};
        for (String lib : libs) {
            try {
                System.load(NativesLoader.prepare(lib)); // $COVERAGE-IGNORE$
            } catch (Throwable e) {
                LOGGER.info("Could not load bundled TinyB native libraries.", e);
                return false;
            }
        }
        return true; // $COVERAGE-IGNORE$
    }

    /**
     * Loads TinyB native libraries from system paths.
     * @return true if all libraries succesefully loaded, false otherwise
     */
    private static boolean loadSystemNativeLibraries() {
        LOGGER.info("TinyB: environment is not supported out of the box. Attempting to load system libs.");

        final String[] libs = {"tinyb", "javatinyb"};
        for (String lib : libs) {
            try {
                System.loadLibrary(lib); // $COVERAGE-IGNORE$
            } catch (Throwable e) {
                LOGGER.info("TinyB: Could not load system libraries. Thus, environemnt is not supported. "
                    + "Only Linux OS; x86, x86_64 and arm6 architectures are supported out of the box. Consider "
                    + "providing own " + lib + ". in one of " + System.getProperty("java.library.path"), e);
                return false;
            }
        }
        return true; // $COVERAGE-IGNORE$
    }

    /**
     * Loads TinyB native libraries (either bundeled or system ones).
     * @return true if all libraries succesefully loaded, false otherwise
     */
    public static boolean loadNativeLibraries() {
        return loadBundeledNativeLibraries() || loadSystemNativeLibraries();
    }

    @Override
    public Adapter getAdapter(URL url) {
        BluetoothAdapter adapter = (BluetoothAdapter) BluetoothManager.getBluetoothManager().getObject(
                BluetoothType.ADAPTER, null, url.getAdapterAddress(), null);
        return adapter != null ? new TinyBAdapter(adapter) : null;
    }

    @Override
    public Device getDevice(URL url) {
        BluetoothAdapter adapter = (BluetoothAdapter) BluetoothManager.getBluetoothManager().getObject(
                BluetoothType.ADAPTER, null, url.getAdapterAddress(), null);
        if (adapter == null) {
            return null;
        }
        BluetoothDevice device = (BluetoothDevice) BluetoothManager.getBluetoothManager().getObject(
                BluetoothType.DEVICE, null, url.getDeviceAddress(), adapter);
        return device != null ? new TinyBDevice(device) : null;
    }

    @Override
    public Characteristic getCharacteristic(URL url) {
        BluetoothAdapter adapter = (BluetoothAdapter) BluetoothManager.getBluetoothManager().getObject(
                BluetoothType.ADAPTER, null, url.getAdapterAddress(), null);
{'fraction_non_alphanumeric': 0.09040227372103192, 'fraction_numerical': 0.0037166593790992565, 'mean_word_length': 3.2415391747797866, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1922126', 'n_tokens_mistral': 2542, 'n_tokens_neox': 2447, 'n_words': 699}