<commit_before>/*
 * Copyright 2013 Jive Software
 *
 *    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.
 */

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Private

var fs = require('fs');
var path = require('path');
var express = require('express');
var q = require('q');
var bootstrap = require('./bootstrap');
var definitionConfigurator = require('./definitionSetup');
var jive = require('../api');
var log4js = require('log4js');

var app;
var rootDir = process.cwd();
var tilesDir = rootDir + '/tiles';

var _dir = function(theDir, defaultDir ) {
    theDir = theDir || defaultDir;

    if ( theDir.indexOf('/') == 0 ) {
        return rootDir + theDir;
    } else {
        return process.cwd() + '/' + theDir;
    }
};

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Public

/**
 * For managing jive communities registered with this service
 */
exports.community = jive.community;

/**
 * For managing service webhooks
 */
exports.webhooks = jive.webhooks;

/**
 * Configuration options
 */
exports.options = {};

var persistence;
/**
 * Retrieves or sets current persistence strategy, defaults to file.
 * @type {undefined}
 */
exports.persistence = function(_persistence) {

    if ( _persistence ) {
        if ( !_persistence['find']  || !_persistence['remove'] || !_persistence['save'] ) {
            throw 'Unsupported persistence strategy - must implement find, remove, save methods.';
        }
        persistence = _persistence;
    }

    if ( !persistence) {
        persistence = new jive.persistence.file();
    }

    jive.context.persistence = persistence;

    return persistence;
};

var scheduler;
/**
 * Retrieves or sets the scheduling strategy. Defaults to memory (single node). todo
 * @param _scheduler
 * @returns {*}
 */
exports.scheduler = function( _scheduler ) {
    if ( _scheduler ) {
        if ( !_scheduler['schedule'] || !_scheduler['unschedule'] || !_scheduler['getTasks'] ) {
            throw 'Unsupported scheduler strategy - must implement schedule, unschedule, isScheduled, getTasks.';
        }
        scheduler = _scheduler;

        if ( !scheduler ) {
            scheduler = new jive.scheduler.memory();
        }
        jive.context.scheduler = scheduler;
    }
    scheduler = jive.context.scheduler;
    return scheduler;
};

/**
 * @param _app
 * @param options JSON or path to options file
 * @param definitionsToAutowire optional arraylist of tile names under [app root]/tiles to autowire
 */
exports.init = function(_app, options ) {
    if (_app) {
        app = _app;
    }
    else {
        app = express();
    }
    rootDir =  rootDir || process.cwd();
    tilesDir = rootDir + '/tiles';

    // for some reason this needs to be configured earlier than later
    app.use(express.bodyParser());
    app.use(express.logger('dev'));
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.errorHandler());

    var applyDefaults = function(config) {
        if ( !config['persistence'] ) {
            config['persistence'] = 'file';
        }
    };

   var applyOverrides = function(config) {
        // override role
        var roleFromEnv = process.env['jive_sdk_service_role'];
        if ( roleFromEnv ) {
            config['role'] = roleFromEnv;
        }
    };

    var initialPromise;<commit_msg>making sure that if no scheduler is specified, memory scheduler is provided<commit_after>/*
 * Copyright 2013 Jive Software
 *
 *    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.
 */

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Private

var fs = require('fs');
var path = require('path');
var express = require('express');
var q = require('q');
var bootstrap = require('./bootstrap');
var definitionConfigurator = require('./definitionSetup');
var jive = require('../api');
var log4js = require('log4js');

var app;
var rootDir = process.cwd();
var tilesDir = rootDir + '/tiles';

var _dir = function(theDir, defaultDir ) {
    theDir = theDir || defaultDir;

    if ( theDir.indexOf('/') == 0 ) {
        return rootDir + theDir;
    } else {
        return process.cwd() + '/' + theDir;
    }
};

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Public

/**
 * For managing jive communities registered with this service
 */
exports.community = jive.community;

/**
 * For managing service webhooks
 */
exports.webhooks = jive.webhooks;

/**
 * Configuration options
 */
exports.options = {};

var persistence;
/**
 * Retrieves or sets current persistence strategy, defaults to file.
 * @type {undefined}
 */
exports.persistence = function(_persistence) {

    if ( _persistence ) {
        if ( !_persistence['find']  || !_persistence['remove'] || !_persistence['save'] ) {
            throw 'Unsupported persistence strategy - must implement find, remove, save methods.';
        }
        persistence = _persistence;
    }

    if ( !persistence) {
        persistence = new jive.persistence.file();
    }

    jive.context.persistence = persistence;

    return persistence;
};

var scheduler;
/**
 * Retrieves or sets the scheduling strategy. Defaults to memory (single node). todo
 * @param _scheduler
 * @returns {*}
 */
exports.scheduler = function( _scheduler ) {
    if ( _scheduler ) {
        if ( !_scheduler['schedule'] || !_scheduler['unschedule'] || !_scheduler['getTasks'] ) {
            throw 'Unsupported scheduler strategy - must implement schedule, unschedule, isScheduled, getTasks.';
        }
        scheduler = _scheduler;

        if ( !scheduler ) {
            scheduler = new jive.scheduler.memory();
        }
        jive.context.scheduler = scheduler;
    }
    scheduler = jive.context.scheduler;
    return scheduler;
};

/**
 * @param _app
 * @param options JSON or path to options file
 * @param definitionsToAutowire optional arraylist of tile names under [app root]/tiles to autowire
 */
exports.init = function(_app, options ) {
    if (_app) {
        app = _app;
    }
    else {
        app = express();
    }
    rootDir =  rootDir || process.cwd();
    tilesDir = rootDir + '/tiles';

    // for some reason this needs to be configured earlier than later
    app.use(express.bodyParser());
    app.use(express.logger('dev'));
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.errorHandler());

    var applyDefaults = function(config) {
        if ( !config['persistence'] ) {
            config['persistence'] = 'file';
        }
        if ( !config['scheduler'] ) {
            config['scheduler'] = 'memory';
        }
    };

   var applyOverrides = function(config) {
        // override role
        var roleFromEnv = process.env['jive_sdk_service_role'];
        if ( roleFromEnv ) {
            config['role'] = roleFromEnv;
        }
    };

    var initialPromise;
{'fraction_non_alphanumeric': 0.1733761422573475, 'fraction_numerical': 0.002716720177821684, 'mean_word_length': 2.288266341859521, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4128364', 'n_tokens_mistral': 2189, 'n_tokens_neox': 2181, 'n_words': 654}