text:
FROM scala

ENV UID=1001 \
    NOT_ROOT_USER=owuser
ENV SWAGGER_UI_DOWNLOAD_SHA256=3d7ef5ddc59e10f132fe99771498f0f1ba7a2cbfb9585f9863d4191a574c96e7 \
    SWAGGER_UI_VERSION=3.6.0

###################################################################################################
# It's needed for lean mode where the controller is also an invoker
###################################################################################################
ENV DOCKER_VERSION=1.12.0 \
    DOCKER_DOWNLOAD_SHA256=3dd07f65ea4a7b4c8829f311ab0213bca9ac551b5b24706f3e79a97e22097f8b

RUN apk add --update openssl

# Uncomment to fetch latest version of docker instead: RUN wget -qO- https://get.docker.com | sh
# Install docker client
RUN curl -sSL -o docker-${DOCKER_VERSION}.tgz https://get.docker.com/builds/Linux/x86_64/docker-${DOCKER_VERSION}.tgz && \
echo "${DOCKER_DOWNLOAD_SHA256}  docker-${DOCKER_VERSION}.tgz" | sha256sum -c - && \
tar --strip-components 1 -xvzf docker-${DOCKER_VERSION}.tgz -C /usr/bin docker/docker && \
tar --strip-components 1 -xvzf docker-${DOCKER_VERSION}.tgz -C /usr/bin docker/docker-runc && \
rm -f docker-${DOCKER_VERSION}.tgz && \
chmod +x /usr/bin/docker && \
chmod +x /usr/bin/docker-runc
##################################################################################################

# Install swagger-ui
RUN curl -sSL -o swagger-ui-v${SWAGGER_UI_VERSION}.tar.gz --no-verbose https://github.com/swagger-api/swagger-ui/archive/v${SWAGGER_UI_VERSION}.tar.gz && \
    echo "${SWAGGER_UI_DOWNLOAD_SHA256}  swagger-ui-v${SWAGGER_UI_VERSION}.tar.gz" | sha256sum -c - && \
    mkdir swagger-ui && \
    tar zxf swagger-ui-v${SWAGGER_UI_VERSION}.tar.gz -C /swagger-ui --strip-components=2 swagger-ui-${SWAGGER_UI_VERSION}/dist && \
    rm swagger-ui-v${SWAGGER_UI_VERSION}.tar.gz && \
    sed -i s#http://petstore.swagger.io/v2/swagger.json#/api/v1/api-docs#g /swagger-ui/index.html

# Copy app jars
ADD build/distributions/controller.tar /

COPY init.sh /
RUN chmod +x init.sh

RUN adduser -D -u ${UID} -h /home/${NOT_ROOT_USER} -s /bin/bash ${NOT_ROOT_USER}

# It is possible to run as non root if you dont need invoker capabilities out of the controller today
#USER ${NOT_ROOT_USER}

EXPOSE 8080
CMD ["./init.sh", "0"]


text:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SharedFlowBundle revision="2" name="extract-consent-id-from-dynamic-scope">
    <ConfigurationVersion majorVersion="4" minorVersion="0"/>
    <CreatedAt>1627011713472</CreatedAt>
    <CreatedBy>defaultUser</CreatedBy>
    <Description>In OBBR the consent id is included in a dynamic scope. Extract it to a separate variable and remove it from the scope to make it static</Description>
    <DisplayName>extract-consent-id-from-dynamic-scope</DisplayName>
    <LastModifiedAt>1627453458047</LastModifiedAt>
    <LastModifiedBy>defaultUser</LastModifiedBy>
    <ManifestVersion>SHA-512:06bbeca73a8dbf2298e3c403d6c44f516ceefcbdd406a5eac3f814a19d20de5bcb36e91d8bcf0bec56c390ffc3639a102d7738b1588e576455e9be7d089b6ff9:ap-southeast-2</ManifestVersion>
    <Policies>
        <Policy>AM-SetStaticScope</Policy>
        <Policy>EV-ExtractConsentIdFromDynamicScope</Policy>
    </Policies>
    <Resources/>
    <Spec></Spec>
    <subType>SharedFlow</subType>
    <SharedFlows>
        <SharedFlow>default</SharedFlow>
    </SharedFlows>
</SharedFlowBundle>

redpajama_set_name: RedPajamaGithub


text:
'use strict';

// karma.conf.js
module.exports = function(config) {

  config.set({
    browsers: ['ChromeHeadless'],
    frameworks: ['mocha'],
    singleRun: false,
    reporters: ['progress'],
    logLevel: config.LOG_INFO,
//    port: 9876,  // karma web server port
    autoWatch: false,
    files: [
      '../../tests.js'
    ],
    plugins: [
      'karma-mocha',
      'karma-phantomjs-launcher',
      'karma-chrome-launcher',
    ],
    browsers: ['PhantomJS', 'Chrome']
  });

};


text:
/*
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * * Neither the name of VMware, Inc. nor the names of its contributors may be
 *   used to endorse or promote products derived from this software without
 *   specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL VMWARE, INC. OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */

/* Copyright (c) 2003-2007 Determina Corp. */
/* Copyright (c) 2002-2003 Massachusetts Institute of Technology */
/* Copyright (c) 2002 Hewlett-Packard Company */

// OptionsDlg.cpp : implementation file
//

#include "configure.h"

#ifndef DRGUI_DEMO /* around whole file */

#include "stdafx.h"
#include "DynamoRIO.h"
#include "OptionsDlg.h"
#include "LoggingDlg.h"
#include <assert.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

enum {
    HOT_THRESHOLD = 0,
    INSTRLIBNAME,
    CACHE_BB_MAX,
    CACHE_TRACE_MAX,
    LOGLEVEL,
    LOGMASK,
    PROF_COUNTS,
    PROF_PCS,
    NOASYNCH,
    NOLINK,
    NULLCALLS,
    STATS,

    NOTIFY,
    TRACEDUMP_TEXT,

    TRACEDUMP_BINARY,
    TRACEDUMP_ORIGINS,
};
static const TCHAR * names[] = {
    _T("-hot_threshold"),
    _T("-client_lib"),
    _T("-cache_bb_max"),
    _T("-cache_trace_max"),
    _T("-loglevel"),
    _T("-logmask"),
    _T("-prof_counts"),
    _T("-prof_pcs"),
    _T("-noasynch"),
    _T("-nolink"),
    _T("-nullcalls"),
    _T("-stats"),
    _T("-notify"),

    _T("-tracedump_text"),
    _T("-tracedump_binary"),

    _T("-tracedump_origins"),
};
// numeric values are always positive!  (no leading - assumed)
enum value_type {
    NOVALUE = 0,
    NUM_DECIMAL,
    NUM_HEX,
    STRING
};
static const value_type hasvalue[] = {
    NUM_DECIMAL,
    STRING,
    NUM_DECIMAL,
    NUM_DECIMAL,
    NUM_DECIMAL,
    NUM_HEX,  // logmask
    NOVALUE,
    NOVALUE,
    NOVALUE, // noasynch
    NOVALUE,
    NOVALUE,
    NOVALUE, // stats
    NOVALUE, // notify
    NOVALUE, // tracedump_text
    NOVALUE,

    NOVALUE,

};
static const int checkboxes[] = {
    IDC_HOT_THRESHOLD,
    IDC_INSTRLIBNAME,
    IDC_CACHE_BB_MAX,
    IDC_CACHE_TRACE_MAX,
    IDC_LOGLEVEL,
    IDC_LOGMASK,
    IDC_PROF_COUNTS,
    IDC_PROF_PCS,
    IDC_NOASYNCH,
    IDC_NOLINK,
    IDC_NULLCALLS,
    IDC_STATS,
    IDC_NOTIFY,

    IDC_TRACEDUMP_TEXT,
    IDC_TRACEDUMP_BINARY,

    IDC_TRACEDUMP_ORIGINS,
};
static const BOOL ok_with_release[] = {
    TRUE,
    TRUE,
    TRUE,
    TRUE,
    FALSE, // loglevel
    FALSE, // logmask
    TRUE,
    TRUE,
    TRUE,
    TRUE,
    TRUE,
    FALSE, // stats
    FALSE, // notify

    TRUE,
    TRUE,

    TRUE,
};
static const int num_options = sizeof(checkboxes) / sizeof(int);
static CString * values[num_options];

/////////////////////////////////////////////////////////////////////////////
// COptionsDlg dialog

COptionsDlg::COptionsDlg(CWnd* pParent /*=NULL*/)
    : CDialog(COptionsDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(COptionsDlg)
    m_opstring = _T("");
    m_HotThreshold = _T("50");
    m_InstrLibName = _T("");
    m_CacheBBMax = _T("0");
    m_CacheTraceMax = _T("0");
    m_LogLevel = _T("0");
    m_LogMask = _T("0x3FFF");
    //}}AFX_DATA_INIT
    values[HOT_THRESHOLD] = &m_HotThreshold;
    values[INSTRLIBNAME] = &m_InstrLibName;
    values[CACHE_BB_MAX] = &m_CacheBBMax;
    values[CACHE_TRACE_MAX] = &m_CacheTraceMax;
    values[LOGLEVEL] = &m_LogLevel;
    values[LOGMASK] = &m_LogMask;

    // rest of initialization goes in OnInitDialog, once controls are built
}

void COptionsDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(COptionsDlg)
    DDX_Control(pDX, IDOK, m_OKButton);
    DDX_Text(pDX, IDC_OPTIONS_EDIT, m_opstring);
    DDX_Text(pDX, IDC_EDIT_HOT_THRESHOLD, m_HotThreshold);
    DDX_Text(pDX, IDC_EDIT_INSTRLIBNAME, m_InstrLibName);
    DDX_Text(pDX, IDC_EDIT_CACHE_BB_MAX, m_CacheBBMax);
    DDX_Text(pDX, IDC_EDIT_CACHE_TRACE_MAX, m_CacheTraceMax);
    DDX_Text(pDX, IDC_EDIT_LOGLEVEL, m_LogLevel);
    DDX_Text(pDX, IDC_EDIT_LOGMASK, m_LogMask);
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(COptionsDlg, CDialog)
    //{{AFX_MSG_MAP(COptionsDlg)
    ON_BN_CLICKED(IDC_HOT_THRESHOLD, OnHotThreshold)
    ON_BN_CLICKED(IDC_BROWSE_INSTRLIBNAME, OnBrowseInstrlibname)
    ON_BN_CLICKED(IDC_INSTRLIBNAME, OnInstrlibname)
    ON_BN_CLICKED(IDC_LOGGING_BUTTON, OnLoggingButton)
    ON_EN_CHANGE(IDC_OPTIONS_EDIT, OnChangeOptionsEdit)
    ON_BN_CLICKED(IDC_CACHE_BB_MAX, OnCacheBBMax)
    ON_BN_CLICKED(IDC_CACHE_TRACE_MAX, OnCacheTraceMax)
    ON_BN_CLICKED(IDC_LOGLEVEL, OnLoglevel)
    ON_BN_CLICKED(IDC_LOGMASK, OnLogmask)
    ON_BN_CLICKED(IDC_PROF_PCS, OnProfPcs)
    ON_BN_CLICKED(IDC_PROF_COUNTS, OnProfCounts)
    ON_BN_CLICKED(IDC_STATS, OnStats)
    ON_BN_CLICKED(IDC_NOTIFY, OnNotify)

    ON_BN_CLICKED(IDC_NULLCALLS, OnNullcalls)
    ON_BN_CLICKED(IDC_NOLINK, OnNolink)
    ON_BN_CLICKED(IDC_NOASYNCH, OnNoasynch)
    ON_BN_CLICKED(IDC_TRACEDUMP_TEXT, OnTraceDumpText)
    ON_BN_CLICKED(IDC_TRACEDUMP_BINARY, OnTraceDumpBinary)

    ON_BN_CLICKED(IDC_TRACEDUMP_ORIGINS, OnTraceDumpOrigins)

    ON_EN_CHANGE(IDC_EDIT_INSTRLIBNAME, OnChangeEditInstrlibname)
    ON_BN_CLICKED(IDC_SET_PERMANENT, OnSetPermanent)
    ON_EN_CHANGE(IDC_EDIT_CACHE_BB_MAX, OnChangeEditCacheBbMax)
    ON_EN_CHANGE(IDC_EDIT_CACHE_TRACE_MAX, OnChangeEditCacheTraceMax)
    ON_EN_CHANGE(IDC_EDIT_HOT_THRESHOLD, OnChangeEditHotThreshold)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()

    /////////////////////////////////////////////////////////////////////////////
    // COptionsDlg message handlers

    BOOL COptionsDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // get current options string
    TCHAR path[MAX_PATH];
    int len = GetEnvironmentVariable(_T("DYNAMORIO_OPTIONS"), path, MAX_PATH);
    if (len > 0)
        m_opstring = path; // makes new storage, right?
    UpdateData(FALSE); // FALSE means set controls

    // set controls based on opstring
    if (!CheckOpstring()) {
        MessageBox(_T("Invalid DYNAMORIO_OPTIONS string!\nThis dialog ")
                   _T("may not work properly with it."), _T("Error"),
                   MB_OK | MYMBFLAGS);
    }

    // now disable appropriate options

    // always disabled, only there so can see all options
    DisableCheckbox(IDC_PROF_PCS);
    DLL_TYPE dll_type = CDynamoRIOApp::GetDllType();
    if (dll_type != DLL_PROFILE) {
        DisableCheckbox(IDC_PROF_COUNTS);
    }
    if (dll_type == DLL_RELEASE) {
        int i;
        for (i=0; i<num_options; i++) {
            if (!ok_with_release[i]) {
                DisableCheckbox(checkboxes[i]);
            }
        }
    }

    UpdateData(FALSE); // FALSE means set controls

    return TRUE;
}

void COptionsDlg::OnOK()
{
    UpdateData(TRUE); // TRUE means read from controls

    // set options string
    int res = SetEnvironmentVariable(_T("DYNAMORIO_OPTIONS"), m_opstring);
    assert(res != 0);

    CDialog::OnOK();
}

void COptionsDlg::OnChangeOptionsEdit()
{
    if (CheckOpstring()) {
        m_OKButton.EnableWindow(TRUE);
    } else {
        m_OKButton.EnableWindow(FALSE);
    }
}

static BOOL
is_quote(TCHAR ch)
{
    return (ch == _T('\'') || ch == _T('\"') || ch == _T('`'));
}

static BOOL
is_whitespace(TCHAR ch)
{
    return (ch == _T(' ') || ch == _T('\t') ||
            ch == _T('\n') || ch == _T('\r'));
}

// returns the space delimited or quote-delimited word
// starting at strpos in the string str
// maximum word length: MAX_PATH*2
static BOOL
getword(TCHAR *str, TCHAR **strpos, TCHAR *result)
{
    result[0] = _T('\0');
    int i = 0;
    TCHAR *pos = *strpos;
    TCHAR quote = _T('\0');

    if (pos < str || pos >= str + _tcslen(str))
        return FALSE;

    if (*pos == _T('\0'))
        return FALSE; /* no more words */

    /* eat leading spaces */
    while (is_whitespace(*pos)) {
        pos++;
    }

    /* extract the word */
    if (is_quote(*pos)) {
        quote = *pos;
        pos++; /* don't include surrounding quotes in word */
    }
    while (*pos) {
        if (quote != _T('\0')) {
            /* if quoted, only end on matching quote */
            if (*pos == quote) {
                pos++; /* include the quote */
                break;
            }
        } else {
            /* if not quoted, end on whitespace */
            if (is_whitespace(*pos))
                break;
        }
        result[i++] = *pos;
        pos++;
        assert(i < MAX_PATH*2);
    }
    if (i == 0)
        return NULL; /* no more words */

    result[i] = _T('\0');
    *strpos = pos;

    return TRUE;
}

// returns the beginning and ending positions of the
// parameter names[id] in the string str, including its value,
// if it has one (based on has_value[id]).
// beginning position includes leading whitespace!
// start is inclusive, end is exclusive
static BOOL
find_param(TCHAR *str, int id, int &start, int &end)
{
    TCHAR *pos = str;
    TCHAR *prev_pos = str;
    TCHAR word[MAX_PATH*2];
    while (true) {
        if (!getword(str, &pos, word))
            break;
        if (_tcscmp(word, names[id]) == 0) {
            if (hasvalue[id] != NOVALUE) {
                TCHAR *last_pos = pos;
                if (!getword(str, &pos, word)) {
                    // don't return FALSE, just return empty value
                } else if ((hasvalue[id] == NUM_DECIMAL || hasvalue[id] == NUM_HEX) &&
                           word[0] == '-') {
                    // assumption: no numeric value begins with - (no negative numbers)
                    pos = last_pos;
                }
            }
            start = (int) (prev_pos - str);
            end = (int) (pos - str);
            return TRUE;
        }
        prev_pos = pos;
    }
    return FALSE;
}

static void
expand_ws_quotes(CString str, int &start, int &end)
{
    // start is inclusive, end is exclusive

    // remove preceding quote, if present
    while (start > 0 && is_quote(str.GetAt(start-1))) {
        start--;
    }
    // now remove preceding spaces
    while (start > 0 && is_whitespace(str.GetAt(start-1))) {
        start--;
    }
    // remove following quote, if present
    while (end < str.GetLength() && is_quote(str.GetAt(end))) {
        end++;
    }
    // remove following spaces beyond a single space
    while (end + 1 < str.GetLength() &&
           (is_whitespace(str.GetAt(end)) || is_quote(str.GetAt(end))) &&
           (is_whitespace(str.GetAt(end+1)) || is_quote(str.GetAt(end+1)))) {
        end++;
    }
    // remove following space if at start
    if (start == 0 && end < str.GetLength() &&
        (is_whitespace(str.GetAt(end)) || is_quote(str.GetAt(end)))) {
        end++;
    }
}

// FIXME: share parsing code with non-static routines?
/*static*/ BOOL COptionsDlg::CheckOptionsVersusDllType(DLL_TYPE dll_type)
{
    TCHAR msg[MAX_PATH];
    // this is independent of dialog box -- so grab string separately
    int len = GetEnvironmentVariable(_T("DYNAMORIO_OPTIONS"), msg, MAX_PATH);
    if (len <= 0)
        msg[0] = _T('\0');
    CString opstring = msg;

    // now parse string
    int i;
    TCHAR param[MAX_PATH];
    TCHAR value[MAX_PATH];
    TCHAR *pos = opstring.GetBuffer(0);
    TCHAR *prev_pos = pos;
    while (TRUE) {
        if (!getword(opstring.GetBuffer(0), &pos, param))
            break;
        for (i=0; i<num_options; i++) {
            if (_tcscmp(param, names[i]) == 0) {
                if (hasvalue[i] != NOVALUE) {
                    // grab next space-delimited word
                    getword(opstring.GetBuffer(0), &pos, value);
                } else
                    value[0] = _T('\0');
                if ((dll_type == DLL_RELEASE && !ok_with_release[i]) ||
                    (dll_type != DLL_PROFILE && i == PROF_COUNTS)) {
                    _stprintf(msg, _T("Option \"%s%s%s\" is incompatible with the selected library.\n")
                              _T("Remove it?  Incompatible options cause failure.\n"),
                              param, (hasvalue[i] != NOVALUE) ? _T(" ") : _T(""), value);
                    int res = ::MessageBox(NULL, msg, _T("Confirmation"), MB_YESNO | MYMBFLAGS);
                    if (res == IDYES) {
                        int start = (prev_pos - opstring.GetBuffer(0));
                        int end = (pos - opstring.GetBuffer(0));
                        expand_ws_quotes(opstring, start, end);
                        opstring = opstring.Left(start) +
                            opstring.Right(opstring.GetLength() - end);
                        // update pos
                        pos = opstring.GetBuffer(0) + start;

                        res = SetEnvironmentVariable(_T("DYNAMORIO_OPTIONS"), opstring);
                        assert(res != 0);
                    }
                }
            }
        }
        prev_pos = pos;
    }
    return TRUE;
}

// examines m_opstring, sets checkboxes and edit boxes from it, and returns
// false if an error is found (doesn't finish reading string if error is found)
BOOL COptionsDlg::CheckOpstring()
{
    UpdateData(TRUE); // TRUE means read from controls

    // now parse string
    TCHAR param[MAX_PATH];
    TCHAR value[MAX_PATH];
    TCHAR *pos = m_opstring.GetBuffer(0);
    TCHAR *prev_pos;
    BOOL valid = TRUE;
    BOOL matched_any;
    BOOL match[num_options];
    int i;
    for (i=0; i<num_options; i++)
        match[i] = FALSE;
    DLL_TYPE dll_type = CDynamoRIOApp::GetDllType();
    while (TRUE) {
        if (!getword(m_opstring.GetBuffer(0), &pos, param))
            break;
        matched_any = FALSE;
        for (i=0; i<num_options; i++) {
            if (_tcscmp(param, names[i]) == 0) {
                CButton *button = (CButton *) GetDlgItem(checkboxes[i]);
                assert(button != NULL);
                if (button->GetCheck() == 0)
                    button->SetCheck(1);
                match[i] = TRUE;
                matched_any = TRUE;
                if (hasvalue[i] != NOVALUE) {
                    prev_pos = pos;
                    if (!getword(m_opstring.GetBuffer(0), &pos, value)) {
                        match[i] = FALSE;
                        valid = FALSE;
                        *values[i] = _T("");
                    } else {
                        *values[i] = value;
                        if (i == INSTRLIBNAME) {
                            if (!CheckLibraryExists(value, FALSE)) {
                                match[i] = FALSE;
                                valid = FALSE;
                            }
                        }
                        if (hasvalue[i] == NUM_DECIMAL || hasvalue[i] == NUM_HEX) {
                            if (value[0] == _T('-')) {
                                // value is missing, don't claim next param as value!
                                // (for string (non-numeric) values, go ahead and do so)
                                pos = prev_pos;
                                *values[i] = _T("");
                                match[i] = FALSE;
                                valid = FALSE;
                                break;
                            }
                            int j;
                            int len = _tcslen(value);
                            for (j=0; j<len; j++) {
                                if ((hasvalue[i] == NUM_DECIMAL && !_istdigit(value[j])) ||
                                    (hasvalue[i] == NUM_HEX &&
                                     ((j == 0 && value[j] != _T('0')) ||
                                      (j == 1 && value[j] != _T('x') && value[j] != _T('X')) ||
                                      (j > 1 && !_istxdigit(value[j]))))) {
                                    match[i] = FALSE;
                                    valid = FALSE;
                                    break;
                                }
                            }
                        }
                    }
                } else {
                    match[i] = TRUE;
                    matched_any = TRUE;
                }
                if (match[i]) {
                    // if made it this far, now check vs. dll type
                    if ((dll_type == DLL_RELEASE && !ok_with_release[i]) ||
                        (dll_type != DLL_PROFILE && i == PROF_COUNTS)) {
                        // leave it checked
                        valid = FALSE;
                    }
                }
                break;
            }
        }
        if (!matched_any)
            valid = FALSE;
    }
    for (i=0; i<num_options; i++) {
        if (!match[i]) {
            CButton *button = (CButton *) GetDlgItem(checkboxes[i]);
            assert(button != NULL);
            if (button->GetCheck() > 0)
                button->SetCheck(0);
        }
    }

    UpdateData(FALSE); // FALSE means set controls
    return valid;
}

// common code for all the checkboxes
void COptionsDlg::CheckOption(int param)
{
    CButton *button = (CButton *) GetDlgItem(checkboxes[param]);
    assert(button != NULL);
    BOOL has_value = (hasvalue[param] != NOVALUE);
    CString *value = values[param];
    assert(!has_value || value != NULL);

    UpdateData(TRUE); // TRUE means read from controls
    int start, end;
    BOOL found = find_param(m_opstring.GetBuffer(0), param, start, end);
    int checked = button->GetCheck();
    if (checked) {
        if (has_value && value->IsEmpty()) {
            button->SetCheck(0);
            return;
        }
        if (found) {
            // occurs if things get out of sync...try to recover
            RemoveOption(param);
            OnChangeOptionsEdit();
        }
        if (!m_opstring.IsEmpty() &&
            m_opstring.GetAt(m_opstring.GetLength()-1) != _T(' ')) {
            m_opstring += CString(_T(" "));
        }
        m_opstring += CString(names[param]);
        if (has_value) {
            // if has spaces, put quotes around
            CString val = *value;
            if (val.Find(_T(' '), 0) != -1)
                val.Format(_T("\"%s\""), *value);
            m_opstring += CString(_T(" ")) + val;
        }
    } else {
        assert(found);
        RemoveOption(param);
    }
    UpdateData(FALSE); // FALSE means set controls
}

void COptionsDlg::RemoveOption(int param)
{
    UpdateData(TRUE); // TRUE means read from controls
    int start, end;
    BOOL found = find_param(m_opstring.GetBuffer(0), param, start, end);
    if (found) {
        expand_ws_quotes(m_opstring, start, end);
        m_opstring = m_opstring.Left(start) +
            m_opstring.Right(m_opstring.GetLength() - end);
        UpdateData(FALSE); // FALSE means set controls
    }
}

BOOL COptionsDlg::UpdateValue(int param)
{
    if (hasvalue[param] == NOVALUE)
        return FALSE;

    UpdateData(TRUE); // TRUE means read from controls

    CString *value = values[param];
    // if has spaces, put quotes around
    CString newval = *value;
    if (newval.Find(_T(' '), 0) != -1)
        newval.Format(_T("\"%s\""), *value);

    int start, end;
    BOOL found = find_param(m_opstring.GetBuffer(0), param, start, end);
    if (!found)
        return FALSE;
    TCHAR *buf = m_opstring.GetBuffer(0);
    TCHAR *pos = buf + start;
    TCHAR val[MAX_PATH];
    getword(buf, &pos, val); // param
    TCHAR *val_pos = pos;
    getword(buf, &pos, val); // value
    int val_start = (val_pos - buf);
    expand_ws_quotes(m_opstring, val_start, end);
    m_opstring = m_opstring.Left(val_start) + CString(_T(" ")) + newval +
        m_opstring.Right(m_opstring.GetLength() - end);
    UpdateData(FALSE); // FALSE means set controls
    return TRUE;
}

static TCHAR szFilter[] = _T("Dynamic-Linked Libraries (*.dll)|*.dll|All Files (*.*)|*.*||");

void COptionsDlg::OnBrowseInstrlibname()
{
    CFileDialog fileDlg(TRUE, _T(".dll"), NULL,
                        // hide the "open as read-only" checkbox
                        OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,
                        szFilter);
    int res = fileDlg.DoModal();
    if (res == IDCANCEL)
        return;
    CButton *button = (CButton *) GetDlgItem(checkboxes[INSTRLIBNAME]);
    assert(button != NULL);
    if (button->GetCheck() > 0 && _tcscmp(m_InstrLibName, fileDlg.GetPathName()) != 0) {
        m_InstrLibName = fileDlg.GetPathName();
        UpdateData(FALSE); // FALSE means set controls
        RemoveOption(INSTRLIBNAME);
        CheckOption(INSTRLIBNAME);
    } else {
        m_InstrLibName = fileDlg.GetPathName();
        UpdateData(FALSE); // FALSE means set controls
    }
}

void COptionsDlg::OnInstrlibname()
{
    UpdateData(TRUE); // TRUE means read from controls
    if (CheckLibraryExists(m_InstrLibName.GetBuffer(0), TRUE))
        CheckOption(INSTRLIBNAME);
    else {
        CButton *button = (CButton *) GetDlgItem(IDC_INSTRLIBNAME);
        button->SetCheck(0);
    }
}

void COptionsDlg::OnChangeEditInstrlibname()
{
    if (UpdateValue(INSTRLIBNAME)) {
        OnChangeOptionsEdit();
    }
}

BOOL COptionsDlg::CheckLibraryExists(TCHAR *libname, BOOL notify)
{
    // make sure file exists
    TCHAR msg[MAX_PATH*2];
    CFile check;
    if (!check.Open(libname, CFile::modeRead|CFile::shareDenyNone)) {
        if (notify) {
            _stprintf(msg, _T("Library %s does not exist"), libname);
            MessageBox(msg, _T("Error"), MB_OK | MYMBFLAGS);
        }
        return FALSE;
    }
    check.Close();
    return TRUE;
}

void COptionsDlg::OnLoggingButton()
{
    UpdateData(TRUE); // get values from controls
    int level = _ttoi(m_LogLevel);
    if (level < 0)
        level = 0;
    if (level > 4)
        level = 4;
    int mask;
    int res = _stscanf(m_LogMask, _T("%X"), &mask);
    if (res <= 0 || res == EOF)
        mask = 0;
    CLoggingDlg dlg(level, mask);
    res = dlg.DoModal();
    if (res == IDCANCEL)
        return;
    m_LogLevel.Format(_T("%d"), dlg.GetLevel());
    m_LogMask.Format(_T("0x%04X"), dlg.GetMask());
    UpdateData(FALSE); // write to controls

#if 1
    UpdateValue(LOGLEVEL);
    UpdateValue(LOGMASK);
#else
    // FIXME: change in place...for now we just remove and re-add
    CButton *button = (CButton *) GetDlgItem(checkboxes[LOGLEVEL]);
    assert(button != NULL);
    if (button->GetCheck() > 0) {
        RemoveOption(LOGLEVEL);
        CheckOption(LOGLEVEL);
    }
    button = (CButton *) GetDlgItem(checkboxes[LOGMASK]);
    assert(button != NULL);
    if (button->GetCheck() > 0) {
        RemoveOption(LOGMASK);
        CheckOption(LOGMASK);
    }
#endif
}

void COptionsDlg::DisableCheckbox(int id)
{
    CButton *button = (CButton *) GetDlgItem(id);
    assert(button != NULL);
    button->EnableWindow(FALSE);
}

void COptionsDlg::OnHotThreshold() { CheckOption(HOT_THRESHOLD); }
void COptionsDlg::OnCacheBBMax() { CheckOption(CACHE_BB_MAX); }
void COptionsDlg::OnCacheTraceMax() { CheckOption(CACHE_TRACE_MAX); }
void COptionsDlg::OnLoglevel() { CheckOption(LOGLEVEL); }
void COptionsDlg::OnLogmask() { CheckOption(LOGMASK); }
void COptionsDlg::OnProfPcs() { CheckOption(PROF_PCS); }
void COptionsDlg::OnStats() { CheckOption(STATS); }
void COptionsDlg::OnNullcalls() { CheckOption(NULLCALLS); }
void COptionsDlg::OnNolink() { CheckOption(NOLINK); }
void COptionsDlg::OnNoasynch() { CheckOption(NOASYNCH); }
void COptionsDlg::OnTraceDumpOrigins() { CheckOption(TRACEDUMP_ORIGINS); }

// remind to pick trace dump too

void COptionsDlg::OnProfCounts() {

    CButton *button = (CButton *) GetDlgItem(IDC_NOTIFY);

    assert(button != NULL);

    if (button->GetCheck() == 1) {

        CButton *text = (CButton *) GetDlgItem(IDC_TRACEDUMP_TEXT);

        assert(text != NULL);

        CButton *binary = (CButton *) GetDlgItem(IDC_TRACEDUMP_BINARY);

        assert(binary != NULL);

        if (text->GetCheck() == 0 && binary->GetCheck() == 0) {

            ::MessageBox(NULL, _T("Count profiling results are only visible in a trace dump.\n")

                         _T("Don't forget to select either a text or binary trace dump."),

                         _T("Reminder"), MB_OK | MYMBFLAGS);

        }

    }

    CheckOption(PROF_COUNTS);

}

// mutually exclusive
void COptionsDlg::OnTraceDumpText() {

    CButton *text = (CButton *) GetDlgItem(IDC_TRACEDUMP_TEXT);

    assert(text != NULL);

    CButton *binary = (CButton *) GetDlgItem(IDC_TRACEDUMP_BINARY);

    assert(binary != NULL);

    if (text->GetCheck() == 1 && binary->GetCheck() == 1) {

        ::MessageBox(NULL, _T("Trace dump must be either text or binary, not both"),

                     _T("Mutually Exclusive"), MB_OK | MYMBFLAGS);

        text->SetCheck(0);

    } else {

        CheckOption(TRACEDUMP_TEXT);

    }

}

void COptionsDlg::OnTraceDumpBinary() {

    CButton *text = (CButton *) GetDlgItem(IDC_TRACEDUMP_TEXT);

    assert(text != NULL);

    CButton *binary = (CButton *) GetDlgItem(IDC_TRACEDUMP_BINARY);

    assert(binary != NULL);

    if (text->GetCheck() == 1 && binary->GetCheck() == 1) {

        ::MessageBox(NULL, _T("Trace dump must be either text or binary, not both"),

                     _T("Mutually Exclusive"), MB_OK | MYMBFLAGS);

        binary->SetCheck(0);

    } else {

        CheckOption(TRACEDUMP_BINARY);

    }

}

// ask for confirmation since stderr sticky issue

// in fact, could not even let them set notify within GUI,

// but would be nice if env var brought in from cmd line showed up

void COptionsDlg::OnNotify() {

    TCHAR msg[MAX_PATH];

    CButton *button = (CButton *) GetDlgItem(IDC_NOTIFY);

    assert(button != NULL);

    if (button->GetCheck() == 1) {

        _stprintf(msg, _T("Printing to stderr can cause unexpected failures.\n")

                  _T("Are you sure you want to set this option?\n"));

        int res = ::MessageBox(NULL, msg, _T("Confirmation"), MB_YESNO | MYMBFLAGS);

        if (res == IDYES) {

            CheckOption(NOTIFY);

        } else {

            button->SetCheck(0);

        }

    } else {

        CheckOption(NOTIFY);

    }

}

// set env var not just for this process but permanently for this user
void COptionsDlg::OnSetPermanent()
{
    // it takes a while to broadcast the "we've changed env vars" message,
    // so set a wait cursor
    HCURSOR prev_cursor = SetCursor(LoadCursor(0, IDC_WAIT));

    TCHAR msg[MAX_PATH];
    HKEY hk;
    int res;

    // current user only
    res = RegOpenKeyEx(HKEY_CURRENT_USER, _T("Environment"), 0, KEY_WRITE, &hk);
    if (res != ERROR_SUCCESS) {
        _stprintf(msg, _T("Error writing to HKEY_CURRENT_USER\\Environment"));
        MessageBox(msg, _T("Error"), MB_OK | MYMBFLAGS);
    }

    UpdateData(TRUE); // get values from controls
    TCHAR *val = m_opstring.GetBuffer(0);
    res = RegSetValueEx(hk, _T("DYNAMORIO_OPTIONS"), 0, REG_SZ,
                        (LPBYTE) val, _tcslen(val)+1);
    assert(res == ERROR_SUCCESS);

    RegCloseKey(hk);

    // tell system that we've changed environment (else won't take effect until
    // user logs out and back in)
    DWORD dwReturnValue;
    // Code I copied this from used an ANSI string...I'm leaving
    // it like that FIXME
    SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
                       (LPARAM) "Environment", SMTO_ABORTIFHUNG, 5000, &dwReturnValue);

    SetCursor(prev_cursor);

    // set local env var too, and avoid questions of ability to
    // cancel the permanent operation
    OnOK();
}

void COptionsDlg::OnChangeEditCacheBbMax()
{
    if (UpdateValue(CACHE_BB_MAX)) {
        OnChangeOptionsEdit();
    }
}

void COptionsDlg::OnChangeEditCacheTraceMax()
{
    if (UpdateValue(CACHE_TRACE_MAX)) {
        OnChangeOptionsEdit();
    }
}

void COptionsDlg::OnChangeEditHotThreshold()
{
    if (UpdateValue(HOT_THRESHOLD)) {
        OnChangeOptionsEdit();
    }
}

#endif /* !DRGUI_DEMO */ /* around whole file */


text:
package com.purbon.pdfwriter.chart;

import java.util.List;

import org.jfree.chart.JFreeChart;

public interface Chart<V> {

	public void setHeaders(String[] headers);
	public void insertData(String key, List<V> row);

	public JFreeChart createChart(String title);

	public void setYAxisLabel(String string);
	public String getYAxisLabel();
}


text:
// THIS CODE IS MACHINE-GENERATED, DO NOT EDIT!

package fallk.jfunktion;

/**
 * Represents a function that accepts a long-valued argument and produces an
 * char-valued result.  This is the {@code long}-to-{@code char} primitive
 * specialization for {@link java.util.function.Function}.
 *
 * @see java.util.function.Function
 * @since 1.8
 */
@FunctionalInterface
public interface LongToCharFunction {

    /**
     * Applies this function to the given argument.
     *
     * @param value the function argument
     * @return the function result
     */
    char applyAsChar(long value);
}


text:
using namespace auth;

class ActionTest : public testing::Test
{
public:
    ActionTest(){
        db.start_transaction();
    }

    ~ActionTest(){
        db.rollback_transaction(false);
    }

    DB db;
};

TEST_F(ActionTest, checkIfExist){
    Action act ("new_action_not_existing_in_db", Object );

    EXPECT_FALSE( act.exists(db));
}

TEST_F(ActionTest, createNewAction){
    Action act ("new_action", Object );

    EXPECT_TRUE( act.save(db) );
    EXPECT_TRUE( act.exists(db) );
}

TEST_F(ActionTest, doubleSaving){
    Action act ("new_action", Object );

    EXPECT_TRUE( act.save(db) );
    EXPECT_FALSE( act.save(db) );
}


text:
package org.openapitools.model;

import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;

/**
 * Model tests for EmptyChangeLogSet
 */
@MicronautTest
public class EmptyChangeLogSetTest {
    private final EmptyChangeLogSet model = null;

    /**
     * Model tests for EmptyChangeLogSet
     */
    @Test
    public void testEmptyChangeLogSet() {
        // TODO: test EmptyChangeLogSet
    }

    /**
     * Test the property 'propertyClass'
     */
    @Test
    public void propertyClassTest() {
        // TODO: test propertyClass
    }

    /**
     * Test the property 'kind'
     */
    @Test
    public void kindTest() {
        // TODO: test kind
    }

}


text:
(function (global) {

    onload = function () {

        _checkUnsupported();

        global.spec = {};
        var spec = global.spec;

        spec.run = function run(name) {
            var script = document.createElement('script');
            spec.now = Date.now();
            script.src = '../deps/beez/vendor/require.js?v=' + spec.now;
            script.setAttribute('data-main', './require-config.js?v=' + spec.now);
            document.head.appendChild(script);
            spec.TestCaseName = name;

            var intervalId = setInterval(function () {
                if (spec.rerun) {
                    document.getElementById('mocha').innerHTML = '';
                    spec.rerun();
                    clearInterval(intervalId);
                }
            }, 100);
        };

    };

    // 非対応端末をチェック
    function _checkUnsupported() {

        var message = 'この端末は非対応です';
        var ua = navigator.userAgent;

        switch (true) {
            case (ua.indexOf('SC-02C') !== -1) :
                global.alert(message);
                throw message;
            default :
        }
    }

})(this);


text:
// .NAME vtkMaskPoints - selectively filter points
// .SECTION Description
// vtkMaskPoints is a filter that passes through points and point attributes
// from input dataset. (Other geometry is not passed through.) It is
// possible to mask every nth point, and to specify an initial offset
// to begin masking from.
// It is possible to also generate different random selections
// (jittered strides, real random samples, and spatially stratified
// random samples) from the input data.
// The filter can also generate vertices (topological
// primitives) as well as points. This is useful because vertices are
// rendered while points are not.

#ifndef vtkMaskPoints_h
#define vtkMaskPoints_h

#include "vtkFiltersCoreModule.h" // For export macro
#include "vtkPolyDataAlgorithm.h"

class VTKFILTERSCORE_EXPORT vtkMaskPoints : public vtkPolyDataAlgorithm
{
public:
  static vtkMaskPoints *New();
  vtkTypeMacro(vtkMaskPoints,vtkPolyDataAlgorithm);
  void PrintSelf(ostream& os, vtkIndent indent);

  // Description:
  // Turn on every nth point (strided sampling), ignored by random modes.
  vtkSetClampMacro(OnRatio,int,1,VTK_INT_MAX);
  vtkGetMacro(OnRatio,int);

  // Description:
  // Limit the number of points that can be passed through (i.e.,
  // sets the output sample size).
  vtkSetClampMacro(MaximumNumberOfPoints,vtkIdType,0,VTK_ID_MAX);
  vtkGetMacro(MaximumNumberOfPoints,vtkIdType);

  // Description:
  // Start sampling with this point. Ignored by certain random modes.
  vtkSetClampMacro(Offset,vtkIdType,0,VTK_ID_MAX);
  vtkGetMacro(Offset,vtkIdType);

  // Description:
  // Special flag causes randomization of point selection.
  vtkSetMacro(RandomMode,int);
  vtkGetMacro(RandomMode,int);
  vtkBooleanMacro(RandomMode,int);

  // Description:
  // Special mode selector that switches between random mode types.
  // 0 - randomized strides: randomly strides through the data (default);
  //     fairly certain that this is not a statistically random sample
  //     because the output depends on the order of the input and
  //     the input points do not have an equal chance to appear in the output
  //     (plus Vitter's incremental random algorithms are more complex
  //      than this, while not a proof it is good indication this isn't
  //      a statistically random sample - the closest would be algorithm S)
  // 1 - random sample: create a statistically random sample using Vitter's
  //     incremental algorithm D without A described in Vitter
  //     "Faster Mthods for Random Sampling", Communications of the ACM
  //     Volume 27, Issue 7, 1984
  //     (OnRatio and Offset are ignored) O(sample size)
  // 2 - spatially stratified random sample: create a spatially
  //     stratified random sample using the first method described in
  //     Woodring et al. "In-situ Sampling of a Large-Scale Particle
  //     Simulation for Interactive Visualization and Analysis",
  //     Computer Graphics Forum, 2011 (EuroVis 2011).
  //     (OnRatio and Offset are ignored) O(N log N)
  vtkSetClampMacro(RandomModeType, int, 0, 2);
  vtkGetMacro(RandomModeType, int);

  // Description:
  // THIS ONLY WORKS WITH THE PARALLEL IMPLEMENTATION vtkPMaskPoints RUNNING
  // IN PARALLEL.
  // NOTHING WILL CHANGE IF THIS IS NOT THE PARALLEL vtkPMaskPoints.
  // Determines whether maximum number of points is taken per processor
  // (default) or if the maximum number of points is proportionally
  // taken across processors (i.e., number of points per
  // processor = points on a processor * maximum number of points /
  // total points across all processors).  In the first case,
  // the total number of points = maximum number of points *
  // number of processors.  In the second case, the total number of
  // points = maximum number of points.
  vtkSetMacro(ProportionalMaximumNumberOfPoints, int);
  vtkGetMacro(ProportionalMaximumNumberOfPoints, int);
  vtkBooleanMacro(ProportionalMaximumNumberOfPoints, int);

  // Description:
  // Generate output polydata vertices as well as points. A useful
  // convenience method because vertices are drawn (they are topology) while
  // points are not (they are geometry). By default this method is off.
  vtkSetMacro(GenerateVertices,int);
  vtkGetMacro(GenerateVertices,int);
  vtkBooleanMacro(GenerateVertices,int);

  // Description:
  // When vertex generation is enabled, by default vertices are produced
  // as multi-vertex cells (more than one per cell), if you wish to have
  // a single vertex per cell, enable this flag.
  vtkSetMacro(SingleVertexPerCell,int);
  vtkGetMacro(SingleVertexPerCell,int);
  vtkBooleanMacro(SingleVertexPerCell,int);

  // Description:
  // Set/get the desired precision for the output types. See the documentation
  // for the vtkAlgorithm::DesiredOutputPrecision enum for an explanation of
  // the available precision settings.
  vtkSetMacro(OutputPointsPrecision,int);
  vtkGetMacro(OutputPointsPrecision,int);

protected:
  vtkMaskPoints();
  ~vtkMaskPoints() {}

  virtual int RequestData(vtkInformation *, vtkInformationVector **,
                          vtkInformationVector *);
  virtual int FillInputPortInformation(int port, vtkInformation *info);

  int OnRatio;     // every OnRatio point is on; all others are off.
  vtkIdType Offset;      // offset (or starting point id)
  int RandomMode;  // turn on/off randomization
  vtkIdType MaximumNumberOfPoints;
  int GenerateVertices; //generate polydata verts
  int SingleVertexPerCell;
  int RandomModeType; // choose the random sampling mode
  int ProportionalMaximumNumberOfPoints;
  int OutputPointsPrecision;

  virtual void InternalScatter(unsigned long*, unsigned long *, int, int) {}
  virtual void InternalGather(unsigned long*, unsigned long*, int, int) {}
  virtual int InternalGetNumberOfProcesses() { return 1; };
  virtual int InternalGetLocalProcessId() { return 0; };
  virtual void InternalBarrier() {}
  unsigned long GetLocalSampleSize(vtkIdType, int);

private:
  vtkMaskPoints(const vtkMaskPoints&) VTK_DELETE_FUNCTION;
  void operator=(const vtkMaskPoints&) VTK_DELETE_FUNCTION;
};

#endif


text:
    {% capture year %}{{ site.time | date:"%Y" }}{% endcapture %}

    © <time datetime="{{ year }}" itemprop="copyrightYear">{{ year }}</time> Jesse Talavera-Greenberg.

    Built with Jekyll and
    Autm-rb


text:
// Copyright 2015 The Bazel Authors. 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 com.google.testing.junit.runner.internal.junit4;

import com.google.testing.junit.runner.util.CurrentRunningTest;
import com.google.testing.junit.runner.util.TestNameProvider;

/**
 * A {@link CurrentRunningTest} variant that allows to set the testNameProvider via a method call.
 */
public abstract class SettableCurrentRunningTest extends CurrentRunningTest {
  protected abstract void setGlobalTestNameProvider(TestNameProvider provider);
}


text:
<?php

namespace Google\Cloud\Tests\System\BigQuery;

use Google\Cloud\BigQuery\Job;

class ManageJobsTest extends BigQueryTestCase
{
    public function testListJobs()
    {
        self::$client->runQueryAsJob(
            sprintf(
                'SELECT * FROM [%s.%s]',
                self::$dataset->id(),
                self::$table->id()
            )
        );
        $jobs = self::$client->jobs();
        $job = null;

        // break early to prevent subsequent requests
        foreach ($jobs as $j) {
            $job = $j;
            break;
        }

        $this->assertInstanceOf(Job::class, $job);

        return $job;
    }

    /**
     * @depends testListJobs
     */
    public function testJobExists($job)
    {
        $this->assertTrue($job->exists());
        $this->assertFalse(self::$client->job('not_a_job')->exists());
    }

    public function testCancelsJob()
    {
        $job = self::$client->runQueryAsJob(
            sprintf(
                'SELECT * FROM [%s.%s]',
                self::$dataset->id(),
                self::$table->id()
            )
        );
        $job->cancel();
    }

    /**
     * @depends testListJobs
     */
    public function testGetsJobInfo($job)
    {
        $this->assertEquals('bigquery#job', $job->info()['kind']);
    }

    /**
     * @depends testListJobs
     */
    public function testReloadsJob($job)
    {
        $this->assertEquals('bigquery#job', $job->reload()['kind']);
    }
}


text:
/**
 * Telephone numbers are a common data point to display in HTML tables, and are
 * often formatted (e.g. `dt-string 555-1234`). Typically, when searching a
 * table a user would need to enter the number in exactly the same format it is
 * displayed in, but this is not always convenient (e.g. you might search for
 * `dt-string 5551`).
 *
 * This filtering plug-in will allow both forms to be matched be providing both
 * the formatted and de-formatted data to the table's search.
 *
 *	@summary Make phone numbers searchable formatted or unformatted
 *	@name Phone number
 *	@author Allan Jardine
 *
 *	@example
 *	$(document).ready(function() {
 *		$('#example').dataTable( {
 *		columnDefs: [
 *			{ type: 'phoneNumber', target: 4 }
 *		]
 *		} );
 *	} );
 */

jQuery.fn.DataTable.ext.type.search.phoneNumber = function ( data ) {
	return ! data ?
		'' :
		typeof data === 'string' ?
			data + data.replace(/[ \-]/g, '') :
			data;
};


text: Laravel 5.0 roles package that is for use with Laravel auth


text:
package com.payit.integration;

import static io.dropwizard.testing.ResourceHelpers.resourceFilePath;

import com.payit.ApplicationMain;
import com.payit.api.BlogPost;
import com.payit.fixture.GenerateObjects;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.dropwizard.Configuration;
import io.dropwizard.testing.junit.DropwizardAppRule;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;

import java.io.IOException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.List;
import java.util.stream.IntStream;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;

public class BlogPostEndpointTest {
    @ClassRule
    public static final DropwizardAppRule<Configuration> RULE = new DropwizardAppRule<>(ApplicationMain.class, resourceFilePath("app.yml"));
    protected static final String BASE_URL = "/posts";
    protected Client client;
    protected String serverAddress;

    @Before
    public void setUp() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException {
        client = ClientBuilder.newClient();
        serverAddress = "http://localhost:" + RULE.getLocalPort();
    }

    @After
    public void cleanUp() throws JsonProcessingException {
        getAll().stream().parallel().forEach(post -> deleteABlogPost(post.getId()));
    }

    @Test
    public void postABlogPostTest() {
        WebTarget target = client.target(serverAddress).path(BASE_URL);

        BlogPost post = target.request(MediaType.APPLICATION_JSON_TYPE)
                .post(Entity.entity(GenerateObjects.generateBlogPost(), MediaType.APPLICATION_JSON_TYPE), BlogPost.class);

        Assert.assertNotNull(post);
    }

    @Test
    public void getABlogPost() {
        BlogPost post = storeABlogPost();
        WebTarget target = client.target(serverAddress).path(BASE_URL + "/" + post.getId());

        BlogPost foundPost = target.request(MediaType.APPLICATION_JSON_TYPE).get(BlogPost.class);

        Assert.assertNotNull(foundPost);
        Assert.assertEquals(post.getId(), foundPost.getId());
    }

    @Test
    public void testGetAllBlogPosts() throws JsonProcessingException {
        int expected = 100;
        IntStream.range(0, expected).forEach(item -> storeABlogPost());

        Assert.assertEquals(expected, getAll().size());
    }

    public void deleteABlogPost(String id) {
        WebTarget target = client.target(serverAddress).path(BASE_URL + "/" + id);
        target.request(MediaType.APPLICATION_JSON_TYPE).delete();
    }

    public BlogPost storeABlogPost() {
        WebTarget target = client.target(serverAddress).path(BASE_URL);

        return target.request(MediaType.APPLICATION_JSON_TYPE)
                .post(Entity.entity(GenerateObjects.generateBlogPost(), MediaType.APPLICATION_JSON_TYPE), BlogPost.class);
    }

    public List<BlogPost> getAll() throws JsonProcessingException {
        WebTarget target = client.target(serverAddress).path(BASE_URL);
        List<BlogPost> allPosts = target.request(MediaType.APPLICATION_JSON_TYPE).get(new GenericType<List<BlogPost>>() {});

        return allPosts;
    }
}


text:
<?php

/**
 * @class AddApnsTokenTest
 * =======================
 *
 * Creates a user, logs them in, and calls add-apns-token. Then it verifies
 * that the token has been set in the database.
 *
 * Ryff API <http://www.github.com/RyffProject/ryff-api>
 * Released under the Apache License 2.0.
 */

class AddApnsTokenTest extends Test {
    /**
     * Overrides abstract function in Test.
     *
     * @return string
     */
    public function get_message() {
        return "Add APNs Token test";
    }

    /**
     * Overrides abstract function in Test.
     */
    protected function setup() {
        $this->state["user"] = $this->env->get_test_user();
        $this->env->log_user_in($this->state["user"]->username);
        $this->state["fields"] = array(
            "token" => str_repeat("0", 64),
            "uuid" => str_repeat("0", 36)
        );
    }

    /**
     * Overrides abstract function in Test.
     *
     * @return boolean
     */
    protected function test() {
        $output = true;
        $results = $this->env->post_to_api("add-apns-token", $this->state["fields"]);
        if (!$results) {
            $output = false;
        } else if (property_exists($results, "error")) {
            echo "{$results->error}\n";
            echo "Failed to add APNs token (API level).\n";
            $output = false;
        } else if (empty(PushNotification::get_apns_tokens($this->state["user"]->id))) {
            echo "Failed to add APNs token (Database level).\n";
            $output = false;
        }
        return $output;
    }

    /**
     * Overrides abstract function in Test.
     */
    protected function teardown() {
        User::delete($this->state["user"]->id);
    }
}


text:
namespace electron {

// static
base::CommandLine::StringVector ElectronCommandLine::argv_;

// static
void ElectronCommandLine::Init(int argc, base::CommandLine::CharType** argv) {
  DCHECK(argv_.empty());

  // NOTE: uv_setup_args does nothing on Windows, so we don't need to call it.
  // Otherwise we'd have to convert the arguments from UTF16.
#if !defined(OS_WIN)
  // Hack around with the argv pointer. Used for process.title = "blah"
  argv = uv_setup_args(argc, argv);
#endif

  argv_.assign(argv, argv + argc);
}

#if defined(OS_LINUX)
// static
void ElectronCommandLine::InitializeFromCommandLine() {
  argv_ = base::CommandLine::ForCurrentProcess()->argv();
}
#endif

}  // namespace electron


text:
#include <aws/iot/model/ListThingPrincipalsRequest.h>
#include <aws/core/utils/json/JsonSerializer.h>

#include <utility>

using namespace Aws::IoT::Model;
using namespace Aws::Utils::Json;
using namespace Aws::Utils;

ListThingPrincipalsRequest::ListThingPrincipalsRequest() :
    m_thingNameHasBeenSet(false)
{
}

Aws::String ListThingPrincipalsRequest::SerializePayload() const
{
  return "";
}


text: export * from './meetup.module';


text:
class Incident < ActiveRecord::Base

  has_paper_trail

  before_create :generate_unique_mpv
  after_initialize do
    self.needs_review = true if self.needs_review.nil?
  end

  @@last_geocode_time = nil
  MAX_GEOCODE_CALLS_PER_SECOND = 10  # google API limit
  geocoded_by :full_street_address
  after_validation :geocode_with_rate_limit,
                   if: ->(obj) {
                     (obj.latitude.nil? ||  obj.longitude.nil? ) &&
                     (obj.incident_street_address_changed? ||
                         obj.incident_city_changed? ||
                         obj.incident_state_changed? ||
                         obj.incident_zip_changed?)
                   }

  def geocode_with_rate_limit
    if !@@last_geocode_time.nil?
      sleep_duration = @@last_geocode_time + 1.0/MAX_GEOCODE_CALLS_PER_SECOND - Time::now
      sleep(sleep_duration) unless sleep_duration < 0
    end
    geocode
    @@last_geocode_time = Time::now
  end

  def full_street_address
    [incident_street_address,
     incident_city,
     incident_state,
     incident_zip].reject(&:nil?).join(', ')
  end

  def admin_permalink
    admin_incident_path(self)
  end

  def self.permitted_params
    [ :victim_name, :victim_age, :victim_gender, :victim_race, :victim_image_url, :incident_date,
      :incident_street_address, :incident_city, :within_city_limits, :incident_state, :incident_zip, :incident_county,
      :agency_responsible, :officers_involved, :race_of_officers_involved, :gender_of_officers_involved,
      :notes_related_to_officers_involved, :cause_of_death, :alleged_victim_crime, :crime_category, :aggregate_crime_category,
      :suspect_weapon_type, :caveat, :solution, :incident_description, :official_disposition_of_death, :criminal_charges,
      :news_url, :mental_illness, :unarmed, :line_of_duty, :note, :in_custody, :arrest_related_death, :needs_review, :sort_order, :unique_identifier,
      :unique_mpv, :latitude, :longitude ]
  end

  def self.state_options
    [
        'AK', 'AL', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'HI', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY',
        'LA', 'MA', 'MD', 'ME', 'MI', 'MN', 'MO', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH',
        'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VT', 'WA', 'WI', 'WV', 'WY'
    ]
  end

  def self.crime_category_options
    ['Violent Crime (Non Lethal); Armed with Gun',
     'Violent Crime (Non Lethal); No Gun',
     'Violent Crime (Potentially Lethal); Armed with Gun',
     'Violent Crime (Potentially Lethal); No Gun',
     'No Crime',
     'Property Crime; Armed with Gun',
     'Property Crime; No Gun',
     'Traffic Violation; Armed with Gun',
     'Traffic Violation; No Gun',
     'Murder; Armed with Gun',
     'Murder; No Gun',
     'Other',
     'Unknown/Unspecified',
     'Drugs',
     'Broken Windows Offense',
     'Weapons Possession Offense']
  end

  def self.aggregate_crime_category_options
    ['Criminal Suspect with Gun',
     'Criminal Suspect with No Gun',
     'Traffic Violation with No Gun',
     'Broken Windows Offense',
     'No Crime',
     'Other',
     'Unknown/Unspecified']
  end

  def self.gender_options
    ['Male', 'Female', 'Other']
  end

  def self.race_options
    ['Black', 'White', 'Hispanic', 'Asian', 'Pacific Islander', 'Native American', 'Unknown Race']
  end

  def self.cause_of_death_options
    ['Asphyxiation', 'Beaten', 'Gunshot', 'Taser', 'Beanbag',
     'Vehicle', 'Death in Custody', 'Medical Emergency',
     'Physical restraint', 'Pepper Spray', 'Other']
  end

  def self.official_disposition_of_death_options
    ['Unreported',
     'Unknown',
     'Justified',
     'Charged',
     'Pending Investigation',
     'Other']
  end

  def self.mental_illness_options
    ['Yes', 'No', 'Unknown', 'Drug or Alcohol use']
  end

  def self.unarmed_options
    ['Unarmed',
     'Allegedly Armed',
     'Unclear',
     'Vehicle']
  end

  def self.line_of_duty_options
    ['Line of Duty',
     'Not line of duty/Accidental']
  end

  def self.in_custody_options
    ['In-custody',
     'Inmate',
     'No']
  end

  def self.criminal_charges_options
    ['No Known Charges',
     'Charged with Crime']
  end

  def to_s
    victim_name
  end

  private
  def generate_unique_mpv
    if self.unique_mpv.nil?
      self.unique_mpv = UniqueMpvSeq.next
    elsif self.unique_mpv > UniqueMpvSeq.last
      # If the existing unique_mpv is higher than anything we've seen before, advance the sequence
      # counter to match it. This will work as long as all entries with existing unique_mpv
      # values are imported prior to assigning any new unique_mpv values. For the time being, this
      # assumption is enforced in the importer. If it is violated, collisions may result.
      UniqueMpvSeq.update_last(self.unique_mpv)
    end
  end

end


text:
/*
 *  Sample cbtree custom file icon CSS
 *
 *  Usage:  See https://github.com/pjekel/cbtree/wiki/Tree-Styling
 *
 */

.fileIcon {
	background-image: url('images/fileIconsApache.gif');
	background-repeat: no-repeat;
	background-position: -400px;
	width: 16px;
}

.fileIcon.fileIconCollapsed {
	background-image: url('images/fileIconsApache.gif');
	background-repeat: no-repeat;
	background-position: -0px;
	width: 16px;
}

.fileIcon.fileIconExpanded {
	background-image: url('images/fileIconsApache.gif');
	background-repeat: no-repeat;
	background-position: -16px;
	width: 16px;
}

.fileIconDIR.fileIconDIRCollapsed {
	background-image: url('images/fileIconsApache.gif');
	background-repeat: no-repeat;
	background-position: -0px;
	width: 16px;
}

.fileIconDIR.fileIconDIRExpanded {
	background-image: url('images/fileIconsApache.gif');
	background-repeat: no-repeat;
	background-position: -16px;
	width: 16px;
}

.fileIconBin, .fileIconCgi, .fileIconExe, .fileIconDll, .fileIconObj, .fileIconO, .fileIconSo {
	background-image: url('images/fileIconsApache.gif');
	background-repeat: no-repeat;
	background-position: -32px;
	width: 16px;
}

.fileIconGz, .fileIconJar, .fileIconTgz, .fileIconZ, .fileIconZip {
	background-image: url('images/fileIconsApache.gif');
	background-repeat: no-repeat;
	background-position: -112px;
	width: 16px;
}

.fileIconDoc, .fileIconDocx, .fileIconWord {
	background-image: url('images/fileIconsApache.gif');
	background-repeat: no-repeat;
	background-position: -144px;
	width: 16px;
}

.fileIconBmp, .fileIconGif, .fileIconIco, .fileIconJpg, .fileIconJpeg, .FileIconPict, .fileIconPng,
.fileIconTif, .fileIconTiff {
	background-image: url('images/fileIconsApache.gif');
	background-repeat: no-repeat;
	background-position: -224px;
	width: 16px;
}

.fileIconAsf, .fileIconAsr, .fileIconAsx, .fileIconAvi, .fileIconMov, .fileIconMovie, .fileIconQt,
.fileIconVdo, .fileIconViv, .fileIconVivo, .fileIconWmv, .fileIconWmx, .fileIconWvx {
	background-image: url('images/fileIconsApache.gif');
	background-repeat: no-repeat;
	background-position: -272px;
	width: 16px;
}

.fileIconAif, .fileIconAifc, .fileIconAiff, .fileIconMid, .fileIconMidi, .fileIconMp2, .fileIconMp3,
.fileIconMpa, .fileIconMpe, .fileIconMpg, .fileIconMpeg, .fileIconVoc, .fileIconWav  {
	background-image: url('images/fileIconsApache.gif');
	background-repeat: no-repeat;
	background-position: -320px;
	width: 16px;
}

.fileIconTar {
	background-image: url('images/fileIconsApache.gif');
	background-repeat: no-repeat;
	background-position: -352px;
	width: 16px;
}

.fileIconCss, .fileIconJava, .fileIconJson, .fileIconMd, .fileIconPhp,
.fileIconTxt, .fileIconXml, .fileIconXsl {
	background-image: url('images/fileIconsApache.gif');
	background-repeat: no-repeat;
	background-position: -368px;
	width: 16px;
}

.fileIconUnknown {
	background-image: url('images/fileIconsApache.gif');
	background-repeat: no-repeat;
	background-position: -400px;
	width: 16px;
}

.fileIconHtm, .fileIconHtml, .fileIconPdf, .fileIconShtml {
	background-image: url('images/fileIconsApache.gif');
	background-repeat: no-repeat;
	background-position: -432px;
	width: 16px;
}

.fileIconConf, .fileIconBat, .fileIconSh, .fileIconShar, .fileIconCsh, .fileIconKsh, .fileIconTcl {
	background-image: url('images/fileIconsApache.gif');
	background-repeat: no-repeat;
	background-position: -448px;
	width: 16px;
}

.fileIconP, .fileIconPl, .fileIconPy {
	background-image: url('images/fileIconsApache.gif');
	background-repeat: no-repeat;
	background-position: -464px;
	width: 16px;
}

.fileIconC {
	background-image: url('images/fileIconsApache.gif');
	background-repeat: no-repeat;
	background-position: -480px;
	width: 16px;
}

.fileIconH {
	background-image: url('images/fileIconsApache.gif');
	background-repeat: no-repeat;
	background-position: -496px;
	width: 16px;
}

.fileIconJs {
	background-image: url('images/fileIconsApache.gif');
	background-repeat: no-repeat;
	background-position: -528px;
	width: 16px;
}


text:
module Kitchen
  class Banana < ActiveRecord::Base
    has_paper_trail class_name: "Kitchen::BananaVersion"
  end
end


text:
<?php

declare(strict_types=1);

namespace Zephir\Operators\Other;

use ReflectionException;
use Zephir\CompilationContext;
use Zephir\CompiledExpression;
use Zephir\Exception;
use Zephir\Exception\CompilerException;
use Zephir\Expression;
use Zephir\Expression\Builder\BuilderFactory;
use Zephir\Operators\AbstractOperator;

/**
 * Obtains the type of dynamic variable.
 */
class TypeOfOperator extends AbstractOperator
{
    /**
     * @param $expression
     * @param CompilationContext $compilationContext
     *
     * @return CompiledExpression
     *
     * @throws Exception
     * @throws ReflectionException
     */
    public function compile($expression, CompilationContext $compilationContext): CompiledExpression
    {
        if (!isset($expression['left'])) {
            throw new CompilerException("Invalid 'left' operand for 'typeof' expression", $expression['left']);
        }

        $functionCall = BuilderFactory::getInstance()->statements()->functionCall('gettype', [$expression['left']]);
        $expression = new Expression($functionCall->build());

        return $expression->compile($compilationContext);
    }
}


text: Working with hijacked user '{{ request.user.username }}'!


text:
package com.netflix.simianarmy.aws.janitor.crawler.edda;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.netflix.simianarmy.Resource;
import com.netflix.simianarmy.ResourceType;
import com.netflix.simianarmy.aws.AWSResource;
import com.netflix.simianarmy.aws.AWSResourceType;
import com.netflix.simianarmy.basic.BasicSimianArmyContext;
import com.netflix.simianarmy.client.edda.EddaClient;
import com.netflix.simianarmy.janitor.JanitorCrawler;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.codehaus.jackson.JsonNode;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * The crawler to crawl AWS AMIs for janitor monkey using Edda. Only images that are not currently referenced
 * by any existing instances or launch configurations are returned.
 */
public class EddaImageJanitorCrawler implements JanitorCrawler {

    /** The Constant LOGGER. */
    private static final Logger LOGGER = LoggerFactory.getLogger(EddaImageJanitorCrawler.class);

    /** The name representing the additional field name for the last reference time by instance. */
    public static final String AMI_FIELD_LAST_INSTANCE_REF_TIME = "Last_Instance_Reference_Time";

    /** The name representing the additional field name for the last reference time by launch config. */
    public static final String AMI_FIELD_LAST_LC_REF_TIME = "Last_Launch_Config_Reference_Time";

    /** The name representing the additional field name for whether the image is a base image. **/
    public static final String AMI_FIELD_BASE_IMAGE = "Base_Image";

    private static final int BATCH_SIZE = 500;

    private final EddaClient eddaClient;
    private final List<String> regions = Lists.newArrayList();

    private final Set<String> usedByInstance = Sets.newHashSet();
    private final Set<String> usedByLaunchConfig = Sets.newHashSet();
    private final Set<String> usedNames = Sets.newHashSet();
    private final Map<String, String> imageIdToName = Maps.newHashMap();
    private final Map<String, Long> imageIdToCreationTime = Maps.newHashMap();
    private final Set<String> ancestorImageIds = Sets.newHashSet();

    private String ownerId;
    private final int daysBack;

    private static final String IMAGE_ID = "ami-[a-z0-9]{8}";
    private static final Pattern BASE_AMI_ID_PATTERN = Pattern.compile("^.*?base_ami_id=(" + IMAGE_ID + ").*?");
    private static final Pattern ANCESTOR_ID_PATTERN = Pattern.compile("^.*?ancestor_id=(" + IMAGE_ID + ").*?$");

    /**
     * Instantiates a new basic AMI crawler.
     * @param eddaClient
     *            the Edda client
     * @param daysBack
     *            the number of days that the crawler checks back in history stored in Edda
     * @param regions
     *            the regions the crawler will crawl resources for
     */
    public EddaImageJanitorCrawler(EddaClient eddaClient, String ownerId, int daysBack, String... regions) {
        Validate.notNull(eddaClient);
        this.eddaClient = eddaClient;
        this.ownerId = ownerId;
        Validate.isTrue(daysBack >= 0);
        this.daysBack = daysBack;
        Validate.notNull(regions);
        for (String region : regions) {
            this.regions.add(region);
        }
    }

    @Override
    public EnumSet<? extends ResourceType> resourceTypes() {
        return EnumSet.of(AWSResourceType.IMAGE);
    }

    @Override
    public List<Resource> resources(ResourceType resourceType) {
        if ("IMAGE".equals(resourceType.name())) {
            return getAMIResources();
        }
        return Collections.emptyList();
    }

    @Override
    public List<Resource> resources(String... imageIds) {
        return getAMIResources(imageIds);
    }

    @Override
    public String getOwnerEmailForResource(Resource resource) {
        Validate.notNull(resource);
        return resource.getTag(BasicSimianArmyContext.GLOBAL_OWNER_TAGKEY);
    }

    private List<Resource> getAMIResources(String... imageIds) {
        refreshIdToNameMap();
        refreshAMIsUsedByInstance();
        refreshAMIsUsedByLC();
        refreshIdToCreationTime();
        for (String excludedId : getExcludedImageIds()) {
            String name = imageIdToName.get(excludedId);
            usedNames.add(name);
        }
        LOGGER.info(String.format("%d image names are used across the %d regions.",
                usedNames.size(), regions.size()));
        Collection<String> excludedImageIds = getExcludedImageIds();
        List<Resource> resources = Lists.newArrayList();
        for (String region : regions) {
            resources.addAll(getAMIResourcesInRegion(region, excludedImageIds, imageIds));
        }
        return resources;
    }

    /**
     * The method allows users to put their own logic to exclude a set of images from being
     * cleaned up by Janitor Monkey. In some cases, images are not used but still need to be
     * kept longer.
     * @return a collection of image ids that need to be excluded from Janitor Monkey
     */
    protected Collection<String> getExcludedImageIds() {
        return Sets.newHashSet();
    }

    private JsonNode getImagesInJson(String region, String... imageIds) {
        String url = eddaClient.getBaseUrl(region) + "/aws/images";
        if (imageIds != null && imageIds.length != 0) {
            url += StringUtils.join(imageIds, ',');
            LOGGER.info(String.format("Getting unreferenced AMIs in region %s for %d ids", region, imageIds.length));
        } else {
            LOGGER.info(String.format("Getting all unreferenced AMIs in region %s", region));
            if (StringUtils.isNotBlank(ownerId)) {
                url += ";ownerId=" + ownerId;
            }
        }
        url += ";_expand:(imageId,name,description,state,tags:(key,value))";

        JsonNode jsonNode = null;
        try {
            jsonNode = eddaClient.getJsonNodeFromUrl(url);
        } catch (Exception e) {
            LOGGER.error(String.format(
                    "Failed to get Jason node from edda for AMIs in region %s.", region), e);
        }

        if (jsonNode == null || !jsonNode.isArray()) {
            throw new RuntimeException(String.format("Failed to get valid document from %s, got: %s", url, jsonNode));
        }
        return jsonNode;
    }

    private void refreshIdToNameMap() {
        imageIdToName.clear();
        for (String region : regions) {
            JsonNode jsonNode = getImagesInJson(region);
            for (Iterator<JsonNode> it = jsonNode.getElements(); it.hasNext();) {
                JsonNode ami = it.next();
                String imageId = ami.get("imageId").getTextValue();
                String name = ami.get("name").getTextValue();
                imageIdToName.put(imageId, name);
            }
        }
        LOGGER.info(String.format("Got mapping from image id to name for %d ids", imageIdToName.size()));
    }

    /**
     * AWS doesn't provide creation time for images. We use the ctime (the creation time of the image record in Edda)
     * to approximate the creation time of the image.
     */
    private void refreshIdToCreationTime() {
        for (String region : regions) {
            String url = eddaClient.getBaseUrl(region) + "/aws/images";
            LOGGER.info(String.format("Getting the creation time for all AMIs in region %s", region));
            if (StringUtils.isNotBlank(ownerId)) {
                url += ";data.ownerId=" + ownerId;
            }
            url += ";_expand;_meta:(ctime,data:(imageId))";

            JsonNode jsonNode = null;
            try {
                jsonNode = eddaClient.getJsonNodeFromUrl(url);
            } catch (Exception e) {
                LOGGER.error(String.format(
                        "Failed to get Jason node from edda for creation time of AMIs in region %s.", region), e);
            }

            if (jsonNode == null || !jsonNode.isArray()) {
                throw new RuntimeException(String.format("Failed to get valid document from %s, got: %s",
                        url, jsonNode));
            }

            for (Iterator<JsonNode> it = jsonNode.getElements(); it.hasNext();) {
                JsonNode elem = it.next();
                JsonNode data = elem.get("data");
                String imageId = data.get("imageId").getTextValue();
                JsonNode ctimeNode = elem.get("ctime");
                if (ctimeNode != null && !ctimeNode.isNull()) {
                    long ctime = ctimeNode.asLong();
                    LOGGER.debug(String.format("The image record of %s was created in Edda at %s",
                            imageId, new DateTime(ctime)));
                    imageIdToCreationTime.put(imageId, ctime);
                }
            }
        }
        LOGGER.info(String.format("Got creation time for %d images", imageIdToCreationTime.size()));
    }

    private List<Resource> getAMIResourcesInRegion(
            String region, Collection<String> excludedImageIds, String... imageIds) {
        JsonNode jsonNode = getImagesInJson(region, imageIds);
        List<Resource> resources = Lists.newArrayList();
        for (Iterator<JsonNode> it = jsonNode.getElements(); it.hasNext();) {
            JsonNode ami = it.next();
            String imageId = ami.get("imageId").getTextValue();
            Resource resource = parseJsonElementToresource(region, ami);
            String name = ami.get("name").getTextValue();
            if (excludedImageIds.contains(imageId)) {
                LOGGER.info(String.format("Image %s is excluded from being managed by Janitor Monkey, ignore.",
                        imageId));
                continue;
            }
            if (usedByInstance.contains(imageId) || usedByLaunchConfig.contains(imageId)) {
                LOGGER.info(String.format("AMI %s is referenced by existing instance or launch configuration.",
                        imageId));
            } else {
                LOGGER.info(String.format("AMI %s is not referenced by existing instance or launch configuration.",
                        imageId));
                if (usedNames.contains(name)) {
                    LOGGER.info(String.format("The same AMI name %s is used in another region", name));
                } else {
                    resources.add(resource);
                }
            }
        }
        long since = DateTime.now().minusDays(daysBack).getMillis();
        addLastReferenceInfo(resources, since);

        // Mark the base AMIs that are used as the ancestor of other images
        for (Resource resource : resources) {
            if (ancestorImageIds.contains(resource.getId())) {
                resource.setAdditionalField(AMI_FIELD_BASE_IMAGE, "true");
            }
        }

        return resources;
    }

    private Resource parseJsonElementToresource(String region, JsonNode jsonNode) {
        Validate.notNull(jsonNode);

        String imageId = jsonNode.get("imageId").getTextValue();

        Resource resource = new AWSResource().withId(imageId).withRegion(region)
                .withResourceType(AWSResourceType.IMAGE);

        Long creationTime = imageIdToCreationTime.get(imageId);
        if (creationTime != null) {
            resource.setLaunchTime(new Date(creationTime));
        }

        JsonNode tags = jsonNode.get("tags");
        if (tags == null || !tags.isArray() || tags.size() == 0) {
            LOGGER.debug(String.format("No tags is found for %s", resource.getId()));
        } else {
            for (Iterator<JsonNode> it = tags.getElements(); it.hasNext();) {
                JsonNode tag = it.next();
                String key = tag.get("key").getTextValue();
                String value = tag.get("value").getTextValue();
                resource.setTag(key, value);
            }
        }

        JsonNode descNode = jsonNode.get("description");
        if (descNode != null && !descNode.isNull()) {
            String description = descNode.getTextValue();
            resource.setDescription(description);
            String ancestorImageId = getBaseAmiIdFromDescription(description);
            if (ancestorImageId != null && !ancestorImageIds.contains(ancestorImageId)) {
                LOGGER.info(String.format("Found base AMI id %s from description '%s'", ancestorImageId, description));
                ancestorImageIds.add(ancestorImageId);
            }
        }
        ((AWSResource) resource).setAWSResourceState(jsonNode.get("state").getTextValue());

        String owner = getOwnerEmailForResource(resource);
        if (owner != null) {
            resource.setOwnerEmail(owner);
        }
        return resource;
    }

    private void refreshAMIsUsedByInstance() {
        usedByInstance.clear();
        for (String region : regions) {
            LOGGER.info(String.format("Getting AMIs used by instances in region %s", region));
            String url = eddaClient.getBaseUrl(region) + "/view/instances/;_expand:(imageId)";

            JsonNode jsonNode = null;
            try {
                jsonNode = eddaClient.getJsonNodeFromUrl(url);
            } catch (Exception e) {
                LOGGER.error(String.format(
                        "Failed to get Jason node from edda for AMIs used by instances in region %s.", region), e);
            }

            if (jsonNode == null || !jsonNode.isArray()) {
                throw new RuntimeException(String.format("Failed to get valid document from %s, got: %s",
                        url, jsonNode));
            }

            for (Iterator<JsonNode> it = jsonNode.getElements(); it.hasNext();) {
                JsonNode img = it.next();
                String id = img.get("imageId").getTextValue();
                usedByInstance.add(id);
                usedNames.add(imageIdToName.get(id));
            }
        }
        LOGGER.info(String.format("Found %d image ids used by instance from Edda", usedByInstance.size()));
    }

    private void refreshAMIsUsedByLC() {
        usedByLaunchConfig.clear();
        for (String region : regions) {
            LOGGER.info(String.format("Getting AMIs used by launch configs in region %s", region));
            String url = eddaClient.getBaseUrl(region) + "/aws/launchConfigurations;_expand:(imageId)";

            JsonNode jsonNode = null;
            try {
                jsonNode = eddaClient.getJsonNodeFromUrl(url);
            } catch (Exception e) {
                LOGGER.error(String.format(
                        "Failed to get Jason node from edda for AMIs used by launch configs in region %s.", region), e);
            }

            if (jsonNode == null || !jsonNode.isArray()) {
                throw new RuntimeException(String.format("Failed to get valid document from %s, got: %s",
                        url, jsonNode));
            }

            for (Iterator<JsonNode> it = jsonNode.getElements(); it.hasNext();) {
                JsonNode img = it.next();
                String id = img.get("imageId").getTextValue();
                usedByLaunchConfig.add(id);
                usedNames.add(imageIdToName.get(id));
            }
        }
        LOGGER.info(String.format("Found %d image ids used by launch config from Edda", usedByLaunchConfig.size()));
    }

    private void addLastReferenceInfo(List<Resource> resources, long since) {
        Validate.notNull(resources);
        LOGGER.info(String.format("Updating the latest reference info for %d images", resources.size()));
        Map<String, List<Resource>> regionToResources = Maps.newHashMap();
        for (Resource resource : resources) {
            List<Resource> regionalList = regionToResources.get(resource.getRegion());
            if (regionalList == null) {
                regionalList = Lists.newArrayList();
                regionToResources.put(resource.getRegion(), regionalList);
            }
            regionalList.add(resource);
        }
        //
        for (Map.Entry<String, List<Resource>> entry : regionToResources.entrySet()) {
            String region = entry.getKey();
            LOGGER.info(String.format("Updating the latest reference info for %d images in region %s",
                    resources.size(), region));
            for (List<Resource> batch : Lists.partition(entry.getValue(), BATCH_SIZE)) {
                LOGGER.info(String.format("Processing batch of size %d", batch.size()));
                updateReferenceTimeByInstance(region, batch, since);
                updateReferenceTimeByLaunchConfig(region, batch, since);
            }
        }
    }

    private void updateReferenceTimeByInstance(String region, List<Resource> batch, long since) {
        LOGGER.info(String.format("Getting the last reference time by instance for batch of size %d", batch.size()));
        String batchUrl = getInstanceBatchUrl(region, batch, since);
        JsonNode batchResult = null;
        Map<String, Resource> idToResource = Maps.newHashMap();
        for (Resource resource : batch) {
            idToResource.put(resource.getId(), resource);
        }
        try {
            batchResult = eddaClient.getJsonNodeFromUrl(batchUrl);
        } catch (IOException e) {
            LOGGER.error("Failed to get response for the batch.", e);
        }
        if (batchResult == null || !batchResult.isArray()) {
            throw new RuntimeException(String.format("Failed to get valid document from %s, got: %s",
                    batchUrl, batchResult));
        }
        for (Iterator<JsonNode> it = batchResult.getElements(); it.hasNext();) {
            JsonNode elem = it.next();
            JsonNode data = elem.get("data");
            String imageId = data.get("imageId").getTextValue();
            String instanceId = data.get("instanceId").getTextValue();
            JsonNode ltimeNode = elem.get("ltime");
            if (ltimeNode != null && !ltimeNode.isNull()) {
                long ltime = ltimeNode.asLong();
                Resource ami = idToResource.get(imageId);
                String lastRefTimeByInstance = ami.getAdditionalField(
                        AMI_FIELD_LAST_INSTANCE_REF_TIME);
                if (lastRefTimeByInstance == null || Long.parseLong(lastRefTimeByInstance) < ltime) {
                    LOGGER.info(String.format("The last time that the image %s was referenced by instance %s is %d",
                            imageId, instanceId, ltime));
                    ami.setAdditionalField(AMI_FIELD_LAST_INSTANCE_REF_TIME, String.valueOf(ltime));
                }
            }
        }
    }

    private void updateReferenceTimeByLaunchConfig(String region, List<Resource> batch, long since) {
        LOGGER.info(String.format("Getting the last reference time by launch config for batch of size %d",
                batch.size()));
        String batchUrl = getLaunchConfigBatchUrl(region, batch, since);
        JsonNode batchResult = null;
        Map<String, Resource> idToResource = Maps.newHashMap();
        for (Resource resource : batch) {
            idToResource.put(resource.getId(), resource);
        }
        try {
            batchResult = eddaClient.getJsonNodeFromUrl(batchUrl);
        } catch (IOException e) {
            LOGGER.error("Failed to get response for the batch.", e);
        }
        if (batchResult == null || !batchResult.isArray()) {
            throw new RuntimeException(String.format("Failed to get valid document from %s, got: %s",
                    batchUrl, batchResult));
        }
        for (Iterator<JsonNode> it = batchResult.getElements(); it.hasNext();) {
            JsonNode elem = it.next();
            JsonNode data = elem.get("data");
            String imageId = data.get("imageId").getTextValue();
            String launchConfigurationName = data.get("launchConfigurationName").getTextValue();
            JsonNode ltimeNode = elem.get("ltime");
            if (ltimeNode != null && !ltimeNode.isNull()) {
                long ltime = ltimeNode.asLong();
                Resource ami = idToResource.get(imageId);
                String lastRefTimeByLC = ami.getAdditionalField(AMI_FIELD_LAST_LC_REF_TIME);
                if (lastRefTimeByLC == null || Long.parseLong(lastRefTimeByLC) < ltime) {
                    LOGGER.info(String.format(
                            "The last time that the image %s was referenced by launch config %s is %d",
                            imageId, launchConfigurationName, ltime));
                    ami.setAdditionalField(AMI_FIELD_LAST_LC_REF_TIME, String.valueOf(ltime));
                }
            }
        }
    }

    private String getInstanceBatchUrl(String region, List<Resource> batch, long since) {
        StringBuilder batchUrl = new StringBuilder(eddaClient.getBaseUrl(region)
                + "/view/instances/;data.imageId=");
        batchUrl.append(getImageIdsString(batch));
        batchUrl.append(String.format(";data.state.name=terminated;_since=%d;_expand;_meta:"
                + "(ltime,data:(imageId,instanceId))", since));
        return batchUrl.toString();
    }

    private String getLaunchConfigBatchUrl(String region, List<Resource> batch, long since) {
        StringBuilder batchUrl = new StringBuilder(eddaClient.getBaseUrl(region)
                + "/aws/launchConfigurations/;data.imageId=");
        batchUrl.append(getImageIdsString(batch));
        batchUrl.append(String.format(";_since=%d;_expand;_meta:(ltime,data:(imageId,launchConfigurationName))",
                since));
        return batchUrl.toString();
    }

    private String getImageIdsString(List<Resource> resources) {
        StringBuilder sb = new StringBuilder();
        boolean isFirst = true;
        for (Resource resource : resources) {
            if (!isFirst) {
                sb.append(',');
            } else {
                isFirst = false;
            }
            sb.append(resource.getId());
        }
        return sb.toString();
    }

    private static String getBaseAmiIdFromDescription(String imageDescription) {
        // base_ami_id=ami-1eb75c77,base_ami_name=servicenet-roku-qadd.dc.81210.10.44
        Matcher matcher = BASE_AMI_ID_PATTERN.matcher(imageDescription);
        if (matcher.matches()) {
            return matcher.group(1);
        }
        // store=ebs,ancestor_name=ebs-centosbase-x86_64-20101124,ancestor_id=ami-7b4eb912
        matcher = ANCESTOR_ID_PATTERN.matcher(imageDescription);
        if (matcher.matches()) {
            return matcher.group(1);
        }
        return null;
    }

}