text:
<emulation>
  <emuname>NET_emu</emuname>
  <emuType>Mix</emuType>
  <emuresourceType>NET</emuresourceType>
  <!--2014-02-02T10:10:10-->
  <emustartTime>now</emustartTime>
  <!--duration in seconds -->
  <emustopTime>155</emustopTime>

  <distributions>
   <name>NET_distro</name>
     <startTime>0</startTime>
     <!--duration in seconds -->
     <duration>150</duration>
     <granularity>10</granularity>
     <minJobTime>2</minJobTime>
     <distribution href="/distributions/linear" name="linear" />
   <!--cpu utilization distribution range-->
      <startLoad>100</startLoad>
      <!-- set target bandwidth to bits per sec -->
      <stopLoad>1000</stopLoad>
      <emulator href="/emulators/iperf" name="iperf" />
    <emulator-params>
        <resourceType>NET</resourceType>
        <serverip>10.55.164.223</serverip>
   <!--Leave "0" for default 5001 port -->
   <serverport>5001</serverport>
        <clientip>127.0.0.1</clientip>
   <clientport>5001</clientport>
    </emulator-params>
  </distributions>

  <log>
   <!-- Use value "1" to enable logging(by default logging is off)  -->
   <enable>0</enable>
   <!-- Use seconds for setting probe intervals(if logging is enabled default is 3sec)  -->
   <frequency>3</frequency>
  </log>

</emulation>


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<TITLE>
darks.orm.core.aspect
</TITLE>

<META NAME="date" CONTENT="2014-09-10">

<LINK REL ="stylesheet" TYPE="text/css" HREF="../../../../stylesheet.css" TITLE="Style">

darks.orm.core.aspect

½Ó¿Ú

QueryAspect

SimpleAspect

Àà

AspectQueryContext

AspectUpdateContext

QueryAspectAdapter

UpdateAspectAdapter

redpajama_set_name: RedPajamaGithub


text:
import org.squeryl.adapters.{H2Adapter, PostgreSqlAdapter}
import org.squeryl.internals.DatabaseAdapter
import org.squeryl.{Session, SessionFactory}
import play.api.db.DB
import play.api.GlobalSettings

import play.api.Application

object Global extends GlobalSettings {

  override def onStart(app: Application) {
    SessionFactory.concreteFactory = app.configuration.getString("db.default.driver") match {
      case Some("org.h2.Driver") => Some(() => getSession(new H2Adapter, app))
      case Some("org.postgresql.Driver") => Some(() => getSession(new PostgreSqlAdapter, app))
      case _ => sys.error("Database driver must be either org.h2.Driver or org.postgresql.Driver")
    }
  }

  def getSession(adapter:DatabaseAdapter, app: Application) = Session.create(DB.getConnection()(app), adapter)

}


text:
package com.thegrid.controllers;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.thegrid.Constants;
import com.thegrid.models.Etiqueta;

import java.util.Arrays;
import java.util.List;

@Api(
        name = "partidosmanager",
        version = "v1",
        scopes = {Constants.EMAIL_SCOPE},
        clientIds = {Constants.WEB_CLIENT_ID, Constants.ANDROID_CLIENT_ID, Constants.IOS_CLIENT_ID},
        audiences = {Constants.ANDROID_AUDIENCE}
)
public class EtiquetasController {

    @ApiMethod(
            name = "etiqueta",
            path = "etiqueta",
            httpMethod = ApiMethod.HttpMethod.GET
    )
    public List<Etiqueta> getEtiquetas() {
        return Arrays.asList(
                new Etiqueta("tag1"),
                new Etiqueta("tag2"),
                new Etiqueta("tag3"),
                new Etiqueta("tag4"),
                new Etiqueta("tag5")
        );
    }

}


text:
module starter.match {
    'use strict';

    class MatchController {

        public CurrentDate: Date;
        public PastOneDaysDate: Date = new Date();
        public PastTwoDaysDate: Date = new Date();
        public FutureOneDaysDate: Date = new Date();
        public FutureTwoDaysDate: Date = new Date();
        public RemainingGuessAmount: number;
        public DisplayMatches: MatchInformation[] = [];
        public AccountInfo: account.AccountInformation;
        private _allMatches: MatchInformation[] = [];

        static $inject = ['$scope', 'starter.match.MatchServices',
            '$location', '$ionicModal',
            '$ionicTabsDelegate', 'starter.shared.AccountManagementService'];
        constructor(private $scope,
            private matchSvc: starter.match.MatchServices,
            private $location: ng.ILocationService,
            private $ionicModal,
            private $ionicTabsDelegate,
            private accountManagementSvc: shared.AccountManagementService) {

            this.GetTodayMatches();
            this.$ionicModal.fromTemplateUrl('templates/Matches/MatchPopup.html',
                {
                    scope: $scope,
                    animation: 'slide-in-up'
                }).then(function (modal): void { $scope.MatchPopup = modal; });

            this.$ionicModal.fromTemplateUrl('templates/Accounts/TieFacebookPopup.html',
                {
                    scope: $scope,
                    animation: 'slide-in-up'
                }).then(function (modal): void { $scope.TieFacebookPopup = modal; });
        }

        public LoginWithFacebook(): void {
            this.$scope.TieFacebookPopup.show();
        }

        public GetTodayMatches(): void {
            var accountInfo = this.accountManagementSvc.GetAccountInformation();
            var data = new GetMatchesRequest();
            data.UserId = accountInfo.SecretCode;
            this.matchSvc.GetMatches(data)
                .then((respond: GetMatchesRespond): void => {
                    this.updateAccountInformation(respond.AccountInfo);
                    this.updateDisplayDate(respond.CurrentDate);
                    this.updateDisplayMatches(respond.Matches);
                    this.updateRemainingGuessAmount();
                    this.$ionicTabsDelegate.$getByHandle('calendarTabs').select(2);
                    console.log('Get all matches completed.');
                });
        }

        public SelectTeam(selectedMatch: MatchInformation, selectedTeamId: number) {
            var isChangingValid = this.AccountInfo.MaximumGuessAmount > this.getSelectedTodayMatches().length
            if (!isChangingValid) {
                if (selectedMatch.TeamHome.IsSelected != !selectedMatch.TeamAway.IsSelected) {
                    this.$scope.MatchPopup.show();
                    return;
                }
            }

            var isSelectedTeamHome = selectedMatch.TeamHome.Id == selectedTeamId;
            var selectedTeam = isSelectedTeamHome ? selectedMatch.TeamHome : selectedMatch.TeamAway;
            var unselectedTeam = !isSelectedTeamHome ? selectedMatch.TeamHome : selectedMatch.TeamAway;
            var beforeChange = selectedTeam.IsSelected;
            selectedTeam.IsSelected = !selectedTeam.IsSelected;
            unselectedTeam.IsSelected = false;

            var accountInfo = this.accountManagementSvc.GetAccountInformation();
            var request = new GuessMatchRequest();
            request.UserId = accountInfo.SecretCode;
            request.MatchId = selectedMatch.Id;
            request.IsHome = isSelectedTeamHome;
            request.IsCancel = ((selectedTeamId == selectedTeam.Id) && (beforeChange)) ? true : false;

            this.matchSvc.GuessMatch(request)
                .then((respond: GuessMatchRespond): void => {
                    this.updateAccountInformation(respond.AccountInfo);
                    this.updateDisplayMatches(respond.Matches);
                    this.updateRemainingGuessAmount();
                    console.log('Send guess match completed.');
                });

            this.updateRemainingGuessAmount();
        }

        public SelectDay(days: Date) {
            this.DisplayMatches = this._allMatches.filter(it=> this.dateAreEqual(it.BeginDate, days));
            console.log('# Change display matches completed.');
        }

        public IsTodayMatch(match: MatchInformation): boolean {
            return this.dateAreEqual(match.BeginDate, this.CurrentDate);
        }

        private updateAccountInformation(accountInfo: account.AccountInformation): void {
            this.AccountInfo = accountInfo;
            var memoryAccountInfo = this.accountManagementSvc.GetAccountInformation();
            memoryAccountInfo.Points = accountInfo.Points;
            memoryAccountInfo.CurrentOrderedCoupon = accountInfo.CurrentOrderedCoupon;
            memoryAccountInfo.OAuthId = accountInfo.OAuthId;
            memoryAccountInfo.VerifiedPhoneNumber = accountInfo.VerifiedPhoneNumber;
            this.accountManagementSvc.SetAccountInformation(memoryAccountInfo);
        }

        private dateAreEqual(firstDate: Date, secondDate: Date): boolean {
            var first = new Date(firstDate.toString());
            var second = new Date(secondDate.toString());
            return first.getDay() == second.getDay();
        }

        private updateDisplayMatches(matches: MatchInformation[]): void {
            this._allMatches = matches;
            this.SelectDay(this.CurrentDate);
            console.log('# Update matches completed.');
        }

        private updateDisplayDate(currentDate: Date): void {
            this.CurrentDate = currentDate;

            currentDate = new Date(currentDate.toString());
            this.PastOneDaysDate.setDate(currentDate.getDate() - 1);
            this.PastTwoDaysDate.setDate(currentDate.getDate() - 2);
            this.FutureOneDaysDate.setDate(currentDate.getDate() + 1);
            this.FutureTwoDaysDate.setDate(currentDate.getDate() + 2);
            console.log('# Update date completed.');
        }

        private updateRemainingGuessAmount(): void {
            this.RemainingGuessAmount = this.AccountInfo.MaximumGuessAmount - this.getSelectedTodayMatches().length;
            console.log('# Update remaining guess amount completed.');
        }

        private getSelectedTodayMatches(): MatchInformation[] {
            var selectedMatchesQry = this._allMatches.filter(it=> (it.TeamHome.IsSelected || it.TeamAway.IsSelected) && this.dateAreEqual(it.BeginDate, this.CurrentDate));
            return selectedMatchesQry;
        }
    }

    angular
        .module('starter.match', [])
        .controller('starter.match.MatchController', MatchController);
}


text: select 4*9 as question


text:
using log4net.Appender;
using log4net.Config;
using log4net.Layout;

namespace Rhino.Queues.Tests.Protocol
{
    public class WithDebugging
    {
        static WithDebugging()
        {
            BasicConfigurator.Configure(new DebugAppender
            {
                Layout = new SimpleLayout()
            });
        }
    }
}


text:
/*
 * 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.
 */
/*
 * This code was generated by https://github.com/googleapis/google-api-java-client-services/
 * Modify at your own risk.
 */

package com.google.api.services.dataflow.model;

/**
 * // / Template Catalog is used to organize user TemplateVersions. / TemplateVersions that have the
 * same project_id and display_name are / belong to the same Template. / Templates with the same
 * project_id belong to the same Project. / TemplateVersion may have labels and multiple labels are
 * allowed. / Duplicated labels in the same `TemplateVersion` are not allowed. / TemplateVersion may
 * have tags and multiple tags are allowed. Duplicated / tags in the same `Template` are not
 * allowed!
 *
 *
 This is the Java data model class that specifies how to parse/serialize into the JSON that is
 * transmitted over HTTP when working with the Dataflow API. For a detailed explanation see:
 * https://developers.google.com/api-client-library/java/google-http-java-client/json
 *

 *
 * @author Google, Inc.
 */
@SuppressWarnings("javadoc")
public final class TemplateVersion extends com.google.api.client.json.GenericJson {

  /**
   * Job graph and metadata if it is a legacy Template. Container image path and metadata if it is
   * flex Template.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private Artifact artifact;

  /**
   * Creation time of this TemplateVersion.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private String createTime;

  /**
   * Template description from the user.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private java.lang.String description;

  /**
   * A customized name for Template. Multiple TemplateVersions per Template.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private java.lang.String displayName;

  /**
   * Labels for the Template Version. Labels can be duplicate within Template.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private java.util.Map<String, java.lang.String> labels;

  /**
   * A unique project_id. Multiple Templates per Project.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private java.lang.String projectId;

  /**
   * Alias for version_id, helps locate a TemplateVersion.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private java.util.List<java.lang.String> tags;

  /**
   * Either LEGACY or FLEX. This should match with the type of artifact.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private java.lang.String type;

  /**
   * An auto generated version_id for TemplateVersion.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private java.lang.String versionId;

  /**
   * Job graph and metadata if it is a legacy Template. Container image path and metadata if it is
   * flex Template.
   * @return value or {@code null} for none
   */
  public Artifact getArtifact() {
    return artifact;
  }

  /**
   * Job graph and metadata if it is a legacy Template. Container image path and metadata if it is
   * flex Template.
   * @param artifact artifact or {@code null} for none
   */
  public TemplateVersion setArtifact(Artifact artifact) {
    this.artifact = artifact;
    return this;
  }

  /**
   * Creation time of this TemplateVersion.
   * @return value or {@code null} for none
   */
  public String getCreateTime() {
    return createTime;
  }

  /**
   * Creation time of this TemplateVersion.
   * @param createTime createTime or {@code null} for none
   */
  public TemplateVersion setCreateTime(String createTime) {
    this.createTime = createTime;
    return this;
  }

  /**
   * Template description from the user.
   * @return value or {@code null} for none
   */
  public java.lang.String getDescription() {
    return description;
  }

  /**
   * Template description from the user.
   * @param description description or {@code null} for none
   */
  public TemplateVersion setDescription(java.lang.String description) {
    this.description = description;
    return this;
  }

  /**
   * A customized name for Template. Multiple TemplateVersions per Template.
   * @return value or {@code null} for none
   */
  public java.lang.String getDisplayName() {
    return displayName;
  }

  /**
   * A customized name for Template. Multiple TemplateVersions per Template.
   * @param displayName displayName or {@code null} for none
   */
  public TemplateVersion setDisplayName(java.lang.String displayName) {
    this.displayName = displayName;
    return this;
  }

  /**
   * Labels for the Template Version. Labels can be duplicate within Template.
   * @return value or {@code null} for none
   */
  public java.util.Map<String, java.lang.String> getLabels() {
    return labels;
  }

  /**
   * Labels for the Template Version. Labels can be duplicate within Template.
   * @param labels labels or {@code null} for none
   */
  public TemplateVersion setLabels(java.util.Map<String, java.lang.String> labels) {
    this.labels = labels;
    return this;
  }

  /**
   * A unique project_id. Multiple Templates per Project.
   * @return value or {@code null} for none
   */
  public java.lang.String getProjectId() {
    return projectId;
  }

  /**
   * A unique project_id. Multiple Templates per Project.
   * @param projectId projectId or {@code null} for none
   */
  public TemplateVersion setProjectId(java.lang.String projectId) {
    this.projectId = projectId;
    return this;
  }

  /**
   * Alias for version_id, helps locate a TemplateVersion.
   * @return value or {@code null} for none
   */
  public java.util.List<java.lang.String> getTags() {
    return tags;
  }

  /**
   * Alias for version_id, helps locate a TemplateVersion.
   * @param tags tags or {@code null} for none
   */
  public TemplateVersion setTags(java.util.List<java.lang.String> tags) {
    this.tags = tags;
    return this;
  }

  /**
   * Either LEGACY or FLEX. This should match with the type of artifact.
   * @return value or {@code null} for none
   */
  public java.lang.String getType() {
    return type;
  }

  /**
   * Either LEGACY or FLEX. This should match with the type of artifact.
   * @param type type or {@code null} for none
   */
  public TemplateVersion setType(java.lang.String type) {
    this.type = type;
    return this;
  }

  /**
   * An auto generated version_id for TemplateVersion.
   * @return value or {@code null} for none
   */
  public java.lang.String getVersionId() {
    return versionId;
  }

  /**
   * An auto generated version_id for TemplateVersion.
   * @param versionId versionId or {@code null} for none
   */
  public TemplateVersion setVersionId(java.lang.String versionId) {
    this.versionId = versionId;
    return this;
  }

  @Override
  public TemplateVersion set(String fieldName, Object value) {
    return (TemplateVersion) super.set(fieldName, value);
  }

  @Override
  public TemplateVersion clone() {
    return (TemplateVersion) super.clone();
  }

}


text:
// Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.

define([
    "widgets/js/widget",
    "jqueryui",
    "base/js/utils",
    "bootstrap",
], function(widget, $, utils){
    "use strict";

    var BoxView = widget.DOMWidgetView.extend({
        initialize: function(){
            /**
             * Public constructor
             */
            BoxView.__super__.initialize.apply(this, arguments);
            this.children_views = new widget.ViewList(this.add_child_model, null, this);
            this.listenTo(this.model, 'change:children', function(model, value) {
                this.children_views.update(value);
            }, this);
            this.listenTo(this.model, 'change:overflow_x', function(model, value) {
                this.update_overflow_x();
            }, this);
            this.listenTo(this.model, 'change:overflow_y', function(model, value) {
                this.update_overflow_y();
            }, this);
            this.listenTo(this.model, 'change:box_style', function(model, value) {
                this.update_box_style();
            }, this);
        },

        update_attr: function(name, value) {
            /**
             * Set a css attr of the widget view.
             */
            this.$box.css(name, value);
        },

        render: function(){
            /**
             * Called when view is rendered.
             */
            this.$box = this.$el;
            this.$box.addClass('widget-box');
            this.children_views.update(this.model.get('children'));
            this.update_overflow_x();
            this.update_overflow_y();
            this.update_box_style('');
        },

        update_overflow_x: function() {
            /**
             * Called when the x-axis overflow setting is changed.
             */
            this.$box.css('overflow-x', this.model.get('overflow_x'));
        },

        update_overflow_y: function() {
            /**
             * Called when the y-axis overflow setting is changed.
             */
            this.$box.css('overflow-y', this.model.get('overflow_y'));
        },

        update_box_style: function(previous_trait_value) {
            var class_map = {
                success: ['alert', 'alert-success'],
                info: ['alert', 'alert-info'],
                warning: ['alert', 'alert-warning'],
                danger: ['alert', 'alert-danger']
            };
            this.update_mapped_classes(class_map, 'box_style', previous_trait_value, this.$box);
        },

        add_child_model: function(model) {
            /**
             * Called when a model is added to the children list.
             */
            var that = this;
            var dummy = $('<div/>');
            that.$box.append(dummy);
            return this.create_child_view(model).then(function(view) {
                dummy.replaceWith(view.el);

                // Trigger the displayed event of the child view.
                that.after_displayed(function() {
                    view.trigger('displayed');
                });
                return view;
            }).catch(utils.reject("Couldn't add child view to box", true));
        },

        remove: function() {
            /**
             * We remove this widget before removing the children as an optimization
             * we want to remove the entire container from the DOM first before
             * removing each individual child separately.
             */
            BoxView.__super__.remove.apply(this, arguments);
            this.children_views.remove();
        },
    });

    var FlexBoxView = BoxView.extend({
        render: function(){
            FlexBoxView.__super__.render.apply(this);
            this.listenTo(this.model, 'change:orientation', this.update_orientation, this);
            this.listenTo(this.model, 'change:flex', this._flex_changed, this);
            this.listenTo(this.model, 'change:pack', this._pack_changed, this);
            this.listenTo(this.model, 'change:align', this._align_changed, this);
            this._flex_changed();
            this._pack_changed();
            this._align_changed();
            this.update_orientation();
        },

        update_orientation: function(){
            var orientation = this.model.get("orientation");
            if (orientation == "vertical") {
                this.$box.removeClass("hbox").addClass("vbox");
            } else {
                this.$box.removeClass("vbox").addClass("hbox");
            }
        },

        _flex_changed: function(){
            if (this.model.previous('flex')) {
                this.$box.removeClass('box-flex' + this.model.previous('flex'));
            }
            this.$box.addClass('box-flex' + this.model.get('flex'));
        },

        _pack_changed: function(){
            if (this.model.previous('pack')) {
                this.$box.removeClass(this.model.previous('pack'));
            }
            this.$box.addClass(this.model.get('pack'));
        },

        _align_changed: function(){
            if (this.model.previous('align')) {
                this.$box.removeClass('align-' + this.model.previous('align'));
            }
            this.$box.addClass('align-' + this.model.get('align'));
        },
    });

    return {
        'BoxView': BoxView,
        'FlexBoxView': FlexBoxView,
    };
});


text:
// RUN: %clang_cc1 -fsyntax-only -verify %s

/**
When processing @synthesize, treat ivars in a class extension the same as ivars in the class @interface,
and treat ivars in a superclass extension the same as ivars in the superclass @interface.
In particular, when searching for an ivar to back an @synthesize, do look at ivars in the class's own class
extension but ignore any ivars in superclass class extensions.
*/

@interface Super {
  	int ISA;
}
@end

@interface Super() {
  int Property;		// expected-note {{previously declared 'Property' here}}
}
@end

@interface SomeClass : Super {
        int interfaceIvar1;
        int interfaceIvar2;
}
@property int Property;
@property int Property1;
@end

@interface SomeClass () {
  int Property1;
}
@end

@implementation SomeClass
@synthesize Property;	// expected-error {{property 'Property' attempting to use instance variable 'Property' declared in super class 'Super'}}
@synthesize Property1;	// OK
@end


text:
package com.leekyoungil.cachemem.util;

import com.leekyoungil.cachemem.CacheMemInterface;

import java.io.*;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.util.Scanner;

/**
 * Created by kyoungil_lee on 2014. 8. 5..
 */
public class CacheMemUtil {

    /**
     * To byte array.
     *
     * Object 를 byte array 로 변환한다. 단 해당 object 는 반드시 Serializable 을 구현 해야한다.
     *
     * @param obj the obj
     * @return the byte [ ]
     */
    public static byte[] toByteArray(Object obj) {
        byte[] bytes = null;

        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;

        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(obj);
            oos.flush();
            bytes = bos.toByteArray ();
        }
        catch (IOException ex) {
            ex.printStackTrace();
            bytes = null;
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return bytes;
    }

    /**
     * To object.
     *
     * byte array 를 Object 로 변환한다.
     *
     * @param bytes the bytes
     * @return the object
     */
    public static Object toObject(byte[] bytes) {
        Object obj = null;

        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;

        try {
            bis = new ByteArrayInputStream (bytes);
            ois = new ObjectInputStream (bis);
            obj = ois.readObject();
        } catch (IOException ex) {
            ex.printStackTrace();
            obj = new String(bytes);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
            obj = null;
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return obj;
    }

    /**
     * Sets add tTL time.
     */
    public static void setAddTTLTime () {
        String fileName = "cachemem_add_ttl.txt";

        FileChannel channel = null;
        MappedByteBuffer byteByffer = null;
        Scanner scanner = null;

        try {
            Path path = FileSystems.getDefault().getPath("./", fileName);
            boolean pathExists = Files.exists(path, new LinkOption[]{LinkOption.NOFOLLOW_LINKS});

            if (pathExists) {
                channel = new FileInputStream(fileName).getChannel();
                byteByffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
                CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
                CharBuffer charBuffer = decoder.decode(byteByffer);
                scanner = new Scanner(charBuffer).useDelimiter("\n");

                while (scanner.hasNext()) {
                    String tmpData = scanner.next();

                    if (tmpData != null && !"".equals(tmpData) && tmpData.length() > 0) {
                        String arrayTmpData[] = tmpData.split(":");

                        CacheMemInterface.addTTLTime.put(arrayTmpData[0], Integer.parseInt(arrayTmpData[1]));
                    }
                }
            }
        } catch (IOException iex) {
            iex.printStackTrace();
        } finally {
            try {
                if (scanner != null) {
                    scanner.close();
                }
                if (channel != null) {
                    channel.close();
                }
            } catch (IOException iex2) {
                iex2.printStackTrace();
            }
        }
    }
}


text:
package org.sleuthkit.autopsy.report;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.text.MessageFormat;
import java.util.Map;
import java.util.logging.Level;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import org.openide.DialogDisplayer;
import org.openide.WizardDescriptor;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.actions.CallableSystemAction;
import org.openide.util.actions.Presenter;
import org.sleuthkit.autopsy.casemodule.Case;
import org.sleuthkit.autopsy.coreutils.Logger;
import org.sleuthkit.datamodel.BlackboardArtifact.ARTIFACT_TYPE;

@ActionID(category = "Tools", id = "org.sleuthkit.autopsy.report.ReportWizardAction")
@ActionRegistration(displayName = "#CTL_ReportWizardAction", lazy = false)
@ActionReferences(value = {
    @ActionReference(path = "Menu/Tools", position = 80)})
// moved into Bundle
//@NbBundle.Messages(value = "CTL_ReportWizardAction=Run Report")
public final class ReportWizardAction extends CallableSystemAction implements Presenter.Toolbar, ActionListener {

    private static final Logger logger = Logger.getLogger(ReportWizardAction.class.getName());

    private JButton toolbarButton = new JButton();
    private static final String ACTION_NAME = NbBundle.getMessage(ReportWizardAction.class, "ReportWizardAction.actionName.text");

    /**
     * When the Generate Report button or menu item is selected, open the
     * reporting wizard. When the wizard is finished, create a ReportGenerator
     * with the wizard information, and start all necessary reports.
     */
    @SuppressWarnings("unchecked")
    public static void doReportWizard() {
        WizardDescriptor wiz = new WizardDescriptor(new ReportWizardIterator());
        wiz.setTitleFormat(new MessageFormat("{0} {1}"));
        wiz.setTitle(NbBundle.getMessage(ReportWizardAction.class, "ReportWizardAction.reportWiz.title"));
        if (DialogDisplayer.getDefault().notify(wiz) == WizardDescriptor.FINISH_OPTION) {
            @SuppressWarnings("unchecked")
            ReportGenerator generator = new ReportGenerator((Map<TableReportModule, Boolean>) wiz.getProperty("tableModuleStates"), //NON-NLS
                    (Map<GeneralReportModule, Boolean>) wiz.getProperty("generalModuleStates"), //NON-NLS
                    (Map<FileReportModule, Boolean>) wiz.getProperty("fileModuleStates")); //NON-NLS
            generator.generateTableReports((Map<ARTIFACT_TYPE, Boolean>) wiz.getProperty("artifactStates"), (Map<String, Boolean>) wiz.getProperty("tagStates")); //NON-NLS
            generator.generateFileListReports((Map<FileReportDataTypes, Boolean>) wiz.getProperty("fileReportOptions")); //NON-NLS
            generator.generateGeneralReports();
            generator.displayProgressPanels();
        }
    }

    public ReportWizardAction() {
        setEnabled(false);
        Case.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals(Case.Events.CURRENT_CASE.toString())) {
                    Case newCase = (Case) evt.getNewValue();
                    setEnabled(newCase != null);

                    // Make the cases' Reoports folder, if it doesn't exist
                    if (newCase != null) {
                        boolean exists = (new File(newCase.getCaseDirectory() + File.separator + "Reports")).exists();
                        if (!exists) {
                            boolean reportCreate = (new File(newCase.getCaseDirectory() + File.separator + "Reports")).mkdirs();
                            if (!reportCreate) {
                                logger.log(Level.WARNING, "Could not create Reports directory for case. It does not exist."); //NON-NLS
                            }
                        }
                    }
                }
            }
        });

        // Initialize the Generate Report button
        toolbarButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ReportWizardAction.this.actionPerformed(e);
            }
        });
    }

    @Override
    @SuppressWarnings("unchecked")
    public void actionPerformed(ActionEvent e) {
        doReportWizard();
    }

    @Override
    public void performAction() {
    }

    @Override
    public String getName() {
        return ACTION_NAME;
    }

    @Override
    public HelpCtx getHelpCtx() {
        return HelpCtx.DEFAULT_HELP;
    }

    /**
     * Returns the toolbar component of this action
     *
     * @return component the toolbar button
     */
    @Override
    public Component getToolbarPresenter() {
        ImageIcon icon = new ImageIcon(getClass().getResource("images/btn_icon_generate_report.png")); //NON-NLS
        toolbarButton.setIcon(icon);
        toolbarButton.setText(NbBundle.getMessage(this.getClass(), "ReportWizardAction.toolBarButton.text"));
        return toolbarButton;
    }

    /**
     * Set this action to be enabled/disabled
     *
     * @param value whether to enable this action or not
     */
    @Override
    public void setEnabled(boolean value) {
        super.setEnabled(value);
        toolbarButton.setEnabled(value);
    }
}


text:
module WebUI.Scripts.JavaScript.HJSFunction
    ( HJsFunc   (..), functJS
    , HJsCall   (..), call
    , HJsNew    (..), newJS
    , HJsReturn (..), returnJS
    ) where

-- Импорт модулей
import           Prelude                                     as PRL

import           Data.Char                                      (toLower)
import           Data.String.Utils                              (strip)

import           WebUI.Scripts.HScript
import           WebUI.Scripts.JavaScript.HJSTypes
import           WebUI.Scripts.JavaScript.HJSBuilder
import           WebUI.Scripts.JavaScript.HJSUtils              (smartTabStr)

-- | Функция
data HJsFunc = forall f. (BuilderHSL f) => HJsFunc JsName [HLangJS] f

instance BuilderHSL HJsFunc where
    buildHSL hbConf (HJsFunc n args f) =
        let nf = if (length n) == 0 then "" else n ++ " " in
        "function " ++ nf ++ "(" ++ "){" ++ (hbc_entryLine hbConf) ++
        (smartTabStr (buildHSL hbConf f) (hbc_tabSpace hbConf)) ++
        "}" ++ (hbc_entryLine hbConf)

    buildHSL_L hbConf (HJsFunc n _ _) = n
    buildHSL_R hbConf (HJsFunc _ _ f) = buildHSL hbConf f

-- | Новая функция
functJS :: JsName
        -> [HLangJS]
        -> HSL HLangJS HLangJS
        -> HSL HLangJS HLangJS
functJS name args fnHSL = do
    hbConf <- ask
    s <- liftIO $ do (_, s, _) <- runRWST fnHSL hbConf HLangJS
                     return s
    fnc <- return $ HL $ HJsFunc name args s
    modify (:> fnc)
    return fnc

-- | Функция
data HJsCall = forall f a. (BuilderHSL f) => HJsCall f [HLangJS]

instance BuilderHSL HJsCall where
    buildHSL hbConf (HJsCall f args) =
        (buildHSL_L hbConf f) ++ "(" ++ (buildHSL_L hbConf args) ++ ")" ++ ";" ++ (hbc_entryLine hbConf)

    buildHSL_L hbConf (HJsCall f _   ) = buildHSL_L hbConf f
    buildHSL_R hbConf (HJsCall _ args) = buildHSL_R hbConf args

-- | Вызвать функцию
call :: (BuilderHSL f)
     => f
     -> [HLangJS]
     -> HSL HLangJS HLangJS
call f args = do
    clf <- return $ HL $ HJsCall f args
    modify (:> clf)
    return clf

-- | Синтаксис New
data HJsNew = forall n. (BuilderHSL n) => HJsNew n

instance BuilderHSL HJsNew where
    buildHSL hbConf (HJsNew n) =
        "new " ++ (buildHSL hbConf n) ++ (hbc_entryLine hbConf)

    buildHSL_L hbConf (HJsNew  _) = "new "

-- | Новый синтаксис New
newJS :: HSL HLangJS HLangJS
      -> HSL HLangJS HLangJS
newJS newHSL = do
    hbConf <- ask
    s <- liftIO $ do (_, s, _) <- runRWST newHSL hbConf HLangJS
                     return s
    rn <- return $ HL $ HJsNew s
    modify (:> rn)
    return rn

-- | Синтаксис Return
data HJsReturn = forall r. (BuilderHSL r) => HJsReturn r

instance BuilderHSL HJsReturn where
    buildHSL hbConf (HJsReturn r) =
        "return " ++ (buildHSL hbConf r) ++ ";" ++ (hbc_entryLine hbConf)

    buildHSL_L hbConf (HJsReturn  _) = "return "

-- | Новый синтаксис Return
returnJS :: HLangJS
         -> HSL HLangJS HLangJS
returnJS ret = do
    rr <- return $ HL $ HJsReturn ret
    modify (:> rr)
    return HLangJS


text:
<?php

App::uses('AppShell', 'Console/Command');
App::uses('File', 'Utility');
App::uses('Folder', 'Utility');

/**
 * The Plugin Task handles creating an empty plugin, ready to be used
 *
 * @package       Cake.Console.Command.Task
 */
class PluginTask extends AppShell {

/**
 * path to plugins directory
 *
 * @var array
 */
	public $path = null;

/**
 * Path to the bootstrap file. Changed in tests.
 *
 * @var string
 */
	public $bootstrap = null;

/**
 * initialize
 *
 * @return void
 */
	public function initialize() {
		$this->path = current(App::path('plugins'));
		$this->bootstrap = APP . 'Config' . DS . 'bootstrap.php';
	}

/**
 * Execution method always used for tasks
 *
 * @return void
 */
	public function execute() {
		if (isset($this->args[0])) {
			$plugin = Inflector::camelize($this->args[0]);
			$pluginPath = $this->_pluginPath($plugin);
			if (is_dir($pluginPath)) {
				$this->out(__d('cake_console', 'Plugin: %s already exists, no action taken', $plugin));
				$this->out(__d('cake_console', 'Path: %s', $pluginPath));
				return false;
			} else {
				$this->_interactive($plugin);
			}
		} else {
			return $this->_interactive();
		}
	}

/**
 * Interactive interface
 *
 * @param string $plugin
 * @return void
 */
	protected function _interactive($plugin = null) {
		while ($plugin === null) {
			$plugin = $this->in(__d('cake_console', 'Enter the name of the plugin in CamelCase format'));
		}

		if (!$this->bake($plugin)) {
			$this->error(__d('cake_console', "An error occurred trying to bake: %s in %s", $plugin, $this->path . $plugin));
		}
	}

/**
 * Bake the plugin, create directories and files
 *
 * @param string $plugin Name of the plugin in CamelCased format
 * @return boolean
 */
	public function bake($plugin) {
		$pathOptions = App::path('plugins');
		if (count($pathOptions) > 1) {
			$this->findPath($pathOptions);
		}
		$this->hr();
		$this->out(__d('cake_console', "<info>Plugin Name:</info> %s", $plugin));
		$this->out(__d('cake_console', "<info>Plugin Directory:</info> %s", $this->path . $plugin));
		$this->hr();

		$looksGood = $this->in(__d('cake_console', 'Look okay?'), array('y', 'n', 'q'), 'y');

		if (strtolower($looksGood) === 'y') {
			$Folder = new Folder($this->path . $plugin);
			$directories = array(
				'Config' . DS . 'Schema',
				'Model' . DS . 'Behavior',
				'Model' . DS . 'Datasource',
				'Console' . DS . 'Command' . DS . 'Task',
				'Controller' . DS . 'Component',
				'Lib',
				'View' . DS . 'Helper',
				'Test' . DS . 'Case' . DS . 'Controller' . DS . 'Component',
				'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper',
				'Test' . DS . 'Case' . DS . 'Model' . DS . 'Behavior',
				'Test' . DS . 'Fixture',
				'Vendor',
				'webroot'
			);

			foreach ($directories as $directory) {
				$dirPath = $this->path . $plugin . DS . $directory;
				$Folder->create($dirPath);
				new File($dirPath . DS . 'empty', true);
			}

			foreach ($Folder->messages() as $message) {
				$this->out($message, 1, Shell::VERBOSE);
			}

			$errors = $Folder->errors();
			if (!empty($errors)) {
				foreach ($errors as $message) {
					$this->error($message);
				}
				return false;
			}

			$controllerFileName = $plugin . 'AppController.php';

			$out = "<?php\n\n";
			$out .= "App::uses('AppController', 'Controller');\n\n";
			$out .= "class {$plugin}AppController extends AppController {\n\n";
			$out .= "}\n";
			$this->createFile($this->path . $plugin . DS . 'Controller' . DS . $controllerFileName, $out);

			$modelFileName = $plugin . 'AppModel.php';

			$out = "<?php\n\n";
			$out .= "App::uses('AppModel', 'Model');\n\n";
			$out .= "class {$plugin}AppModel extends AppModel {\n\n";
			$out .= "}\n";
			$this->createFile($this->path . $plugin . DS . 'Model' . DS . $modelFileName, $out);

			$this->_modifyBootstrap($plugin);

			$this->hr();
			$this->out(__d('cake_console', '<success>Created:</success> %s in %s', $plugin, $this->path . $plugin), 2);
		}

		return true;
	}

/**
 * Update the app's bootstrap.php file.
 *
 * @param string $plugin Name of plugin
 * @return void
 */
	protected function _modifyBootstrap($plugin) {
		$bootstrap = new File($this->bootstrap, false);
		$contents = $bootstrap->read();
		if (!preg_match("@\n\s*CakePlugin::loadAll@", $contents)) {
			$bootstrap->append("\nCakePlugin::load('$plugin', array('bootstrap' => false, 'routes' => false));\n");
			$this->out('');
			$this->out(__d('cake_dev', '%s modified', $this->bootstrap));
		}
	}

/**
 * find and change $this->path to the user selection
 *
 * @param array $pathOptions
 * @return void
 */
	public function findPath($pathOptions) {
		$valid = false;
		foreach ($pathOptions as $i => $path) {
			if (!is_dir($path)) {
				array_splice($pathOptions, $i, 1);
			}
		}
		$max = count($pathOptions);
		while (!$valid) {
			foreach ($pathOptions as $i => $option) {
				$this->out($i + 1 . '. ' . $option);
			}
			$prompt = __d('cake_console', 'Choose a plugin path from the paths above.');
			$choice = $this->in($prompt, null, 1);
			if (intval($choice) > 0 && intval($choice) <= $max) {
				$valid = true;
			}
		}
		$this->path = $pathOptions[$choice - 1];
	}

/**
 * get the option parser for the plugin task
 *
 * @return void
 */
	public function getOptionParser() {
		$parser = parent::getOptionParser();
		return $parser->description(__d('cake_console',
			'Create the directory structure, AppModel and AppController classes for a new plugin. ' .
			'Can create plugins in any of your bootstrapped plugin paths.'
		))->addArgument('name', array(
			'help' => __d('cake_console', 'CamelCased name of the plugin to create.')
		));
	}

}


text:
<?php
namespace TJM\Component\Utils;

use Exception;

/*
Class: BaseNConverter
Use to convert to and from a base-n number with provided character table.  Only works for integers currently.
Arguments:
	config: can be either an array of characters that make up the character table, or a string representing the configuration (see `$config`)
*/
class BaseNConverter{
	public function __construct($config){
		$this->setConfig($config);
	}

	/*
	Property: base
	'n' of base-n, calculated based on the number of characters in characterTable.
	*/
	protected $base;

	/*
	Property: characterTable
	Table of characters to be used in base-n number, with first value representing 0, second value representing 1, and so on.
	*/
	protected $characterTable;

	protected function setCharacterTable($characterTable){
		if(is_string($characterTable)){
			$this->characterTable = str_split($characterTable);
		}else{
			$this->characterTable = $characterTable;
		}
		$this->base = count($this->characterTable);
	}

	/*
	Property: config
	String representing configuration.  This string represents the entire configuration for the instance of this class, and any instances with the same config string will produce the same output given the same input.  The config consists of one or more pieces separated by '::'.  The pieces are as follows:

	1. (required) The character table.  If wrapped in '(' and ')' characters, will be considered an ordered list of characters making up the table.  Otherwise will be considered a named character table from `getNamedCharacterTableConfig()`.
	2. negativeCharacter
	*/
	protected $config;
	public function getConfig(){
		return $this->config;
	}
	protected function setConfig($config){
		if(is_array($config)){
			$this->setCharacterTable($config);
			$this->config = implode('', $config);
		}else{
			$this->config = $config;
			if(preg_match(static::NAMED_CONFIG_REGEX, $config, $matches)){
				preg_match(static::CUSTOM_TABLE_CONFIG_REGEX, $this->getNamedCharacterTableConfig($matches[1]), $namedConfigMatches);
				$this->setCharacterTable($namedConfigMatches[1]);
				if(isset($matches[3])){
					$this->negativeCharacter = $matches[3];
				}elseif(isset($namedConfigMatches[3])){
					$this->negativeCharacter = $namedConfigMatches[3];
				}
			}elseif(preg_match(static::CUSTOM_TABLE_CONFIG_REGEX, $config, $matches)){
				$this->setCharacterTable($matches[1]);
				if(isset($matches[3])){
					$this->negativeCharacter = $matches[3];
				}
			}else{
				throw new Exception("Config '{$config}' not in valid format.");
			}
		}
	}
	public function getNamedCharacterTableConfig($name){
		switch($name){
			case 'base16':
			case 'base16LC':
				return '(0123456789abcdef)::-';
			break;
			case 'base16UC':
				return '(0123456789ABCDEF)::-';
			break;
			/*
			newBase60: probably ideal when readability and URL safeness are important, like for URL shorteners.  [Tantek's creation](http://tantek.pbworks.com/w/page/19402946/NewBase60)(-@), used for his URL shortener.
			*/
			case 'newBase60':
				return '(0123456789ABCDEFGHJKLMNPQRSTUVWXYZ_abcdefghijkmnopqrstuvwxyz)::-';
			break;
			/*
			newBase64: probably ideal when readability and URL safeness are important, like for URL shorteners.  Not great for filesystem paths because of '*'.  [Tantek's creation](http://tantek.pbworks.com/w/page/24308279/NewBase64)(-@).
			*/
			case 'newBase64':
				return '(0123456789ABCDEFGHJKLMNPQRSTUVWXYZ_abcdefghijkmnopqrstuvwxyz-+*$)::~';
			break;
			/*
			tjmBase65: Provides 5 more characters than 'newBase60' ('l','I','O','.', and '-'), but should still be safe for both HTTP and filesystem paths.  Probably best for URLs when readability isn't important, like for asset versioning.
			*/
			case 'tjmBase65':
				return '(0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_)::~';
			break;
		}
	}

	/*
	Property: CUSTOM_TABLE_CONFIG_REGEX
	Regex used to match custom character table configuration format.
	*/
	const CUSTOM_TABLE_CONFIG_REGEX = '/^\((.*?)\)(::(.))?$/';
	// const CUSTOM_TABLE_CONFIG_REGEX = '/^\(((?:([.])(?!.*\\1))*)\)(::(.))?$/';

	/*
	Property: NAMED_CONFIG_REGEX
	Regex used to match named configuration format.
	*/
	const NAMED_CONFIG_REGEX = '/^(\w+)(::(.))?$/';

	/*
	Property: negativeCharacter
	Character to prepend to number to represent a negative value.
	*/
	protected $negativeCharacter = '-';

	/*=====
	==conversions
	=====*/
	/*
	Method: from
	Convert base-n representation of number to regular integer.
	-@ algorithm based on http://www.eecs.wsu.edu/~ee314/handouts/numsys.pdf
	*/
	public function from($number){
		$number = (string) $number;
		$negative = (strpos($number, $this->negativeCharacter) === 0);
		if($negative){
			$number = substr($number, strlen($this->negativeCharacter));
		}
		// if($number === $this->characterTable[0]){}
		$length = strlen($number);
		$result = 0;
		for($i = 0; $i < $length; ++$i){
			$char = $number{$i};
			$tablePosition = array_search($char, $this->characterTable);
			$charPosition = $length - $i - 1;
			$result += $tablePosition * pow($this->base, $charPosition);
		}
		if($negative){
			$result *= -1;
		}
		return $result;
	}

	/*
	Method: to
	Convert regular integer to base-n representation.
	-@ algorithm based on http://www.eecs.wsu.edu/~ee314/handouts/numsys.pdf
	*/
	public function to($number){
		if($number === 0){
			$result = $this->characterTable[$number];
		}else{
			$negative = ($number < 0);
			$result = '';
			$remaining = ($negative) ? abs($number) : $number;
			while($remaining > 0){
				$remainder = $remaining % $this->base;
				$remaining = (int) ($remaining / $this->base);
				$result = $this->characterTable[$remainder] . $result;
			}
			if($negative){
				$result = $this->negativeCharacter . $result;
			}
		}
		return $result;
	}
}


text:
package org.apache.beam.runners.fnexecution.control;

import com.google.auto.value.AutoValue;
import java.io.IOException;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.annotation.concurrent.ThreadSafe;
import org.apache.beam.model.fnexecution.v1.ProvisionApi;
import org.apache.beam.model.pipeline.v1.RunnerApi.Environment;
import org.apache.beam.model.pipeline.v1.RunnerApi.StandardEnvironments;
import org.apache.beam.runners.core.construction.BeamUrns;
import org.apache.beam.runners.core.construction.Environments;
import org.apache.beam.runners.core.construction.PipelineOptionsTranslation;
import org.apache.beam.runners.core.construction.Timer;
import org.apache.beam.runners.core.construction.graph.ExecutableStage;
import org.apache.beam.runners.fnexecution.artifact.ArtifactRetrievalService;
import org.apache.beam.runners.fnexecution.control.ProcessBundleDescriptors.ExecutableProcessBundleDescriptor;
import org.apache.beam.runners.fnexecution.control.ProcessBundleDescriptors.TimerSpec;
import org.apache.beam.runners.fnexecution.control.SdkHarnessClient.BundleProcessor;
import org.apache.beam.runners.fnexecution.data.GrpcDataService;
import org.apache.beam.runners.fnexecution.environment.DockerEnvironmentFactory;
import org.apache.beam.runners.fnexecution.environment.EmbeddedEnvironmentFactory;
import org.apache.beam.runners.fnexecution.environment.EnvironmentFactory;
import org.apache.beam.runners.fnexecution.environment.ExternalEnvironmentFactory;
import org.apache.beam.runners.fnexecution.environment.ProcessEnvironmentFactory;
import org.apache.beam.runners.fnexecution.environment.RemoteEnvironment;
import org.apache.beam.runners.fnexecution.logging.GrpcLoggingService;
import org.apache.beam.runners.fnexecution.logging.Slf4jLogWriter;
import org.apache.beam.runners.fnexecution.provisioning.JobInfo;
import org.apache.beam.runners.fnexecution.provisioning.StaticGrpcProvisionService;
import org.apache.beam.runners.fnexecution.state.GrpcStateService;
import org.apache.beam.runners.fnexecution.state.StateRequestHandler;
import org.apache.beam.sdk.coders.Coder;
import org.apache.beam.sdk.fn.IdGenerator;
import org.apache.beam.sdk.fn.IdGenerators;
import org.apache.beam.sdk.fn.data.FnDataReceiver;
import org.apache.beam.sdk.fn.server.GrpcContextHeaderAccessorProvider;
import org.apache.beam.sdk.fn.server.GrpcFnServer;
import org.apache.beam.sdk.fn.server.ServerFactory;
import org.apache.beam.sdk.fn.stream.OutboundObserverFactory;
import org.apache.beam.sdk.function.ThrowingFunction;
import org.apache.beam.sdk.options.ExperimentalOptions;
import org.apache.beam.sdk.options.PipelineOptions;
import org.apache.beam.sdk.options.PortablePipelineOptions;
import org.apache.beam.sdk.util.NoopLock;
import org.apache.beam.sdk.values.KV;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.MoreObjects;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.cache.CacheBuilder;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.cache.CacheLoader;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.cache.LoadingCache;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Sets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A {@link JobBundleFactory} for which the implementation can specify a custom {@link
 * EnvironmentFactory} for environment management. Note that returned {@link StageBundleFactory
 * stage bundle factories} are not thread-safe. Instead, a new stage factory should be created for
 * each client. {@link DefaultJobBundleFactory} initializes the Environment lazily when the forStage
 * is called for a stage.
 */
@ThreadSafe
@SuppressWarnings({
  "rawtypes", // TODO(https://issues.apache.org/jira/browse/BEAM-10556)
  "nullness" // TODO(https://issues.apache.org/jira/browse/BEAM-10402)
})
public class DefaultJobBundleFactory implements JobBundleFactory {
  private static final Logger LOG = LoggerFactory.getLogger(DefaultJobBundleFactory.class);
  private static final IdGenerator factoryIdGenerator = IdGenerators.incrementingLongs();

  private final String factoryId = factoryIdGenerator.getId();
  private final ImmutableList<EnvironmentCacheAndLock> environmentCaches;
  private final AtomicInteger stageBundleFactoryCount = new AtomicInteger();
  private final Map<String, EnvironmentFactory.Provider> environmentFactoryProviderMap;
  private final ExecutorService executor;
  private final MapControlClientPool clientPool;
  private final IdGenerator stageIdGenerator;
  private final int environmentExpirationMillis;
  private final Semaphore availableCachesSemaphore;
  private final LinkedBlockingDeque<EnvironmentCacheAndLock> availableCaches;
  private final boolean loadBalanceBundles;
  /** Clients which were evicted due to environment expiration but still had pending references. */
  private final Set<WrappedSdkHarnessClient> evictedActiveClients;

  private boolean closed;

  public static DefaultJobBundleFactory create(JobInfo jobInfo) {
    PipelineOptions pipelineOptions =
        PipelineOptionsTranslation.fromProto(jobInfo.pipelineOptions());
    Map<String, EnvironmentFactory.Provider> environmentFactoryProviderMap =
        ImmutableMap.of(
            BeamUrns.getUrn(StandardEnvironments.Environments.DOCKER),
            new DockerEnvironmentFactory.Provider(pipelineOptions),
            BeamUrns.getUrn(StandardEnvironments.Environments.PROCESS),
            new ProcessEnvironmentFactory.Provider(pipelineOptions),
            BeamUrns.getUrn(StandardEnvironments.Environments.EXTERNAL),
            new ExternalEnvironmentFactory.Provider(),
            Environments.ENVIRONMENT_EMBEDDED, // Non Public urn for testing.
            new EmbeddedEnvironmentFactory.Provider(pipelineOptions));
    return new DefaultJobBundleFactory(jobInfo, environmentFactoryProviderMap);
  }

  public static DefaultJobBundleFactory create(
      JobInfo jobInfo, Map<String, EnvironmentFactory.Provider> environmentFactoryProviderMap) {
    return new DefaultJobBundleFactory(jobInfo, environmentFactoryProviderMap);
  }

  DefaultJobBundleFactory(
      JobInfo jobInfo, Map<String, EnvironmentFactory.Provider> environmentFactoryMap) {
    IdGenerator stageIdSuffixGenerator = IdGenerators.incrementingLongs();
    this.environmentFactoryProviderMap = environmentFactoryMap;
    this.executor = Executors.newCachedThreadPool();
    this.clientPool = MapControlClientPool.create();
    this.stageIdGenerator = () -> factoryId + "-" + stageIdSuffixGenerator.getId();
    this.environmentExpirationMillis = getEnvironmentExpirationMillis(jobInfo);
    this.loadBalanceBundles = shouldLoadBalanceBundles(jobInfo);
    this.environmentCaches =
        createEnvironmentCaches(
            serverFactory -> createServerInfo(jobInfo, serverFactory),
            getMaxEnvironmentClients(jobInfo));
    this.availableCachesSemaphore = new Semaphore(environmentCaches.size(), true);
    this.availableCaches = new LinkedBlockingDeque<>(environmentCaches);
    this.evictedActiveClients = Sets.newConcurrentHashSet();
  }

  @VisibleForTesting
  DefaultJobBundleFactory(
      JobInfo jobInfo,
      Map<String, EnvironmentFactory.Provider> environmentFactoryMap,
      IdGenerator stageIdGenerator,
      ServerInfo serverInfo) {
    this.environmentFactoryProviderMap = environmentFactoryMap;
    this.executor = Executors.newCachedThreadPool();
    this.clientPool = MapControlClientPool.create();
    this.stageIdGenerator = stageIdGenerator;
    this.environmentExpirationMillis = getEnvironmentExpirationMillis(jobInfo);
    this.loadBalanceBundles = shouldLoadBalanceBundles(jobInfo);
    this.environmentCaches =
        createEnvironmentCaches(serverFactory -> serverInfo, getMaxEnvironmentClients(jobInfo));
    this.availableCachesSemaphore = new Semaphore(environmentCaches.size(), true);
    this.availableCaches = new LinkedBlockingDeque<>(environmentCaches);
    this.evictedActiveClients = Sets.newConcurrentHashSet();
  }

  private static class EnvironmentCacheAndLock {
    final Lock lock;
    final LoadingCache<Environment, WrappedSdkHarnessClient> cache;

    EnvironmentCacheAndLock(LoadingCache<Environment, WrappedSdkHarnessClient> cache, Lock lock) {
      this.lock = lock;
      this.cache = cache;
    }
  }

  private ImmutableList<EnvironmentCacheAndLock> createEnvironmentCaches(
      ThrowingFunction<ServerFactory, ServerInfo> serverInfoCreator, int count) {

    ImmutableList.Builder<EnvironmentCacheAndLock> caches = ImmutableList.builder();
    for (int i = 0; i < count; i++) {

      final Lock refLock;
      if (environmentExpirationMillis > 0) {
        // The lock ensures there is no race condition between expiring an environment and a client
        // still attempting to use it, hence referencing it.
        refLock = new ReentrantLock(true);
      } else {
        refLock = NoopLock.get();
      }

      CacheBuilder<Environment, WrappedSdkHarnessClient> cacheBuilder =
          CacheBuilder.newBuilder()
              .removalListener(
                  notification -> {
                    WrappedSdkHarnessClient client = notification.getValue();
                    final int refCount;
                    // We need to use a lock here to ensure we are not causing the environment to
                    // be removed if beforehand a StageBundleFactory has retrieved it but not yet
                    // issued ref() on it.
                    refLock.lock();
                    try {
                      refCount = client.unref();
                    } finally {
                      refLock.unlock();
                    }
                    if (refCount > 0) {
                      LOG.warn(
                          "Expiring environment {} with {} remaining bundle references. Taking note to clean it up during shutdown if the references are not removed by then.",
                          notification.getKey(),
                          refCount);
                      evictedActiveClients.add(client);
                    }
                  });

      if (environmentExpirationMillis > 0) {
        cacheBuilder.expireAfterWrite(environmentExpirationMillis, TimeUnit.MILLISECONDS);
      }

      LoadingCache<Environment, WrappedSdkHarnessClient> cache =
          cacheBuilder.build(
              new CacheLoader<Environment, WrappedSdkHarnessClient>() {
                @Override
                public WrappedSdkHarnessClient load(Environment environment) throws Exception {
                  EnvironmentFactory.Provider environmentFactoryProvider =
                      environmentFactoryProviderMap.get(environment.getUrn());
                  ServerFactory serverFactory = environmentFactoryProvider.getServerFactory();
                  ServerInfo serverInfo = serverInfoCreator.apply(serverFactory);
                  String workerId = stageIdGenerator.getId();
                  serverInfo
                      .getProvisioningServer()
                      .getService()
                      .registerEnvironment(workerId, environment);
                  EnvironmentFactory environmentFactory =
                      environmentFactoryProvider.createEnvironmentFactory(
                          serverInfo.getControlServer(),
                          serverInfo.getLoggingServer(),
                          serverInfo.getRetrievalServer(),
                          serverInfo.getProvisioningServer(),
                          clientPool,
                          stageIdGenerator);
                  return WrappedSdkHarnessClient.wrapping(
                      environmentFactory.createEnvironment(environment, workerId), serverInfo);
                }
              });

      caches.add(new EnvironmentCacheAndLock(cache, refLock));
    }
    return caches.build();
  }

  private static int getEnvironmentExpirationMillis(JobInfo jobInfo) {
    PipelineOptions pipelineOptions =
        PipelineOptionsTranslation.fromProto(jobInfo.pipelineOptions());
    return pipelineOptions.as(PortablePipelineOptions.class).getEnvironmentExpirationMillis();
  }

  private static int getMaxEnvironmentClients(JobInfo jobInfo) {
    PortablePipelineOptions pipelineOptions =
        PipelineOptionsTranslation.fromProto(jobInfo.pipelineOptions())
            .as(PortablePipelineOptions.class);
    int maxEnvironments = MoreObjects.firstNonNull(pipelineOptions.getSdkWorkerParallelism(), 1);
    Preconditions.checkArgument(maxEnvironments >= 0, "sdk_worker_parallelism must be >= 0");
    if (maxEnvironments == 0) {
      // if this is 0, use the auto behavior of num_cores - 1 so that we leave some resources
      // available for the java process
      maxEnvironments = Math.max(Runtime.getRuntime().availableProcessors() - 1, 1);
    }
    return maxEnvironments;
  }

  private static boolean shouldLoadBalanceBundles(JobInfo jobInfo) {
    PipelineOptions pipelineOptions =
        PipelineOptionsTranslation.fromProto(jobInfo.pipelineOptions());
    boolean loadBalanceBundles =
        pipelineOptions.as(PortablePipelineOptions.class).getLoadBalanceBundles();
    if (loadBalanceBundles) {
      int stateCacheSize =
          Integer.parseInt(
              MoreObjects.firstNonNull(
                  ExperimentalOptions.getExperimentValue(
                      pipelineOptions, ExperimentalOptions.STATE_CACHE_SIZE),
                  "0"));
      Preconditions.checkArgument(
          stateCacheSize == 0,
          "%s must be 0 when using bundle load balancing",
          ExperimentalOptions.STATE_CACHE_SIZE);
    }
    return loadBalanceBundles;
  }

  @Override
  public StageBundleFactory forStage(ExecutableStage executableStage) {
    return new SimpleStageBundleFactory(executableStage);
  }

  @Override
  public synchronized void close() throws Exception {
    if (closed) {
      return;
    }
    // The following code is written defensively to guard against any exceptions occurring
    // during shutdown. It is not visually appealing but unless it can be written more
    // defensively, there is no reason to change it.
    Exception exception = null;
    for (EnvironmentCacheAndLock environmentCache : environmentCaches) {
      try {
        // Clear the cache. This closes all active environments.
        // note this may cause open calls to be cancelled by the peer
        environmentCache.cache.invalidateAll();
        environmentCache.cache.cleanUp();
      } catch (Exception e) {
        if (exception != null) {
          exception.addSuppressed(e);
        } else {
          exception = e;
        }
      }
    }
    // Cleanup any left-over environments which were not properly dereferenced by the Runner, e.g.
    // when the bundle was not closed properly. This ensures we do not leak resources.
    for (WrappedSdkHarnessClient client : evictedActiveClients) {
      try {
        //noinspection StatementWithEmptyBody
        while (client.unref() > 0) {
          // Remove any pending references from the client to force closing the environment
        }
      } catch (Exception e) {
        if (exception != null) {
          exception.addSuppressed(e);
        } else {
          exception = e;
        }
      }
    }
    try {
      executor.shutdown();
    } catch (Exception e) {
      if (exception != null) {
        exception.addSuppressed(e);
      } else {
        exception = e;
      }
    }
    closed = true;
    if (exception != null) {
      throw exception;
    }
  }

  private static Map<String, RemoteOutputReceiver<?>> getOutputReceivers(
      ExecutableProcessBundleDescriptor processBundleDescriptor,
      OutputReceiverFactory outputReceiverFactory) {
    ImmutableMap.Builder<String, RemoteOutputReceiver<?>> outputReceivers = ImmutableMap.builder();
    for (Map.Entry<String, Coder> remoteOutputCoder :
        processBundleDescriptor.getRemoteOutputCoders().entrySet()) {
      String outputTransform = remoteOutputCoder.getKey();
      Coder coder = remoteOutputCoder.getValue();
      String bundleOutputPCollection =
          Iterables.getOnlyElement(
              processBundleDescriptor
                  .getProcessBundleDescriptor()
                  .getTransformsOrThrow(outputTransform)
                  .getInputsMap()
                  .values());
      FnDataReceiver outputReceiver = outputReceiverFactory.create(bundleOutputPCollection);
      outputReceivers.put(outputTransform, RemoteOutputReceiver.of(coder, outputReceiver));
    }
    return outputReceivers.build();
  }

  private static Map<KV<String, String>, RemoteOutputReceiver<Timer<?>>> getTimerReceivers(
      ExecutableProcessBundleDescriptor processBundleDescriptor,
      TimerReceiverFactory timerReceiverFactory) {
    ImmutableMap.Builder<KV<String, String>, RemoteOutputReceiver<Timer<?>>> timerReceivers =
        ImmutableMap.builder();
    for (Map.Entry<String, Map<String, TimerSpec>> transformTimerSpecs :
        processBundleDescriptor.getTimerSpecs().entrySet()) {
      for (TimerSpec timerSpec : transformTimerSpecs.getValue().values()) {
        FnDataReceiver<Timer<?>> receiver =
            (FnDataReceiver)
                timerReceiverFactory.create(timerSpec.transformId(), timerSpec.timerId());
        timerReceivers.put(
            KV.of(timerSpec.transformId(), timerSpec.timerId()),
            RemoteOutputReceiver.of(timerSpec.coder(), receiver));
      }
    }
    return timerReceivers.build();
  }

  private static class PreparedClient {
    private BundleProcessor processor;
    private ExecutableProcessBundleDescriptor processBundleDescriptor;
    private WrappedSdkHarnessClient wrappedClient;
  }

  private PreparedClient prepare(
      WrappedSdkHarnessClient wrappedClient, ExecutableStage executableStage) {
    PreparedClient preparedClient = new PreparedClient();
    try {
      preparedClient.wrappedClient = wrappedClient;
      preparedClient.processBundleDescriptor =
          ProcessBundleDescriptors.fromExecutableStage(
              stageIdGenerator.getId(),
              executableStage,
              wrappedClient.getServerInfo().getDataServer().getApiServiceDescriptor(),
              wrappedClient.getServerInfo().getStateServer().getApiServiceDescriptor());
    } catch (IOException e) {
      throw new RuntimeException("Failed to create ProcessBundleDescriptor.", e);
    }

    preparedClient.processor =
        wrappedClient
            .getClient()
            .getProcessor(
                preparedClient.processBundleDescriptor.getProcessBundleDescriptor(),
                preparedClient.processBundleDescriptor.getRemoteInputDestinations(),
                wrappedClient.getServerInfo().getStateServer().getService(),
                preparedClient.processBundleDescriptor.getTimerSpecs());
    return preparedClient;
  }

  /**
   * A {@link StageBundleFactory} for remotely processing bundles that supports environment
   * expiration.
   */
  private class SimpleStageBundleFactory implements StageBundleFactory {

    private final ExecutableStage executableStage;
    private final int environmentIndex;
    private final IdentityHashMap<WrappedSdkHarnessClient, PreparedClient> preparedClients =
        new IdentityHashMap<>();
    private volatile PreparedClient currentClient;

    private SimpleStageBundleFactory(ExecutableStage executableStage) {
      this.executableStage = executableStage;
      this.environmentIndex = stageBundleFactoryCount.getAndIncrement() % environmentCaches.size();
      WrappedSdkHarnessClient client =
          environmentCaches
              .get(environmentIndex)
              .cache
              .getUnchecked(executableStage.getEnvironment());
      this.currentClient = prepare(client, executableStage);
      this.preparedClients.put(client, currentClient);
    }

    @Override
    public RemoteBundle getBundle(
        OutputReceiverFactory outputReceiverFactory,
        TimerReceiverFactory timerReceiverFactory,
        StateRequestHandler stateRequestHandler,
        BundleProgressHandler progressHandler,
        BundleFinalizationHandler finalizationHandler,
        BundleCheckpointHandler checkpointHandler)
        throws Exception {
      // TODO: Consider having BundleProcessor#newBundle take in an OutputReceiverFactory rather
      // than constructing the receiver map here. Every bundle factory will need this.

      final EnvironmentCacheAndLock currentCache;
      final WrappedSdkHarnessClient client;
      if (loadBalanceBundles) {
        // The semaphore is used to ensure fairness, i.e. first stop first go.
        availableCachesSemaphore.acquire();
        // The blocking queue of caches for serving multiple bundles concurrently.
        currentCache = availableCaches.take();
        // Lock because the environment expiration can remove the ref for the client
        // which would close the underlying environment before we can ref it.
        currentCache.lock.lock();
        try {
          client = currentCache.cache.getUnchecked(executableStage.getEnvironment());
          client.ref();
        } finally {
          currentCache.lock.unlock();
        }

        currentClient = preparedClients.get(client);
        if (currentClient == null) {
          // we are using this client for the first time
          preparedClients.put(client, currentClient = prepare(client, executableStage));
          // cleanup any expired clients
          preparedClients.keySet().removeIf(c -> c.bundleRefCount.get() == 0);
        }

      } else {
        currentCache = environmentCaches.get(environmentIndex);
        // Lock because the environment expiration can remove the ref for the client which would
        // close the underlying environment before we can ref it.
        currentCache.lock.lock();
        try {
          client = currentCache.cache.getUnchecked(executableStage.getEnvironment());
          client.ref();
        } finally {
          currentCache.lock.unlock();
        }

        if (currentClient.wrappedClient != client) {
          // reset after environment expired
          preparedClients.clear();
          currentClient = prepare(client, executableStage);
          preparedClients.put(client, currentClient);
        }
      }

      if (environmentExpirationMillis > 0) {
        // Cleanup list of clients which were active during eviction but now do not hold references
        evictedActiveClients.removeIf(c -> c.bundleRefCount.get() == 0);
      }

      final RemoteBundle bundle =
          currentClient.processor.newBundle(
              getOutputReceivers(currentClient.processBundleDescriptor, outputReceiverFactory),
              getTimerReceivers(currentClient.processBundleDescriptor, timerReceiverFactory),
              stateRequestHandler,
              progressHandler,
              finalizationHandler,
              checkpointHandler);
      return new RemoteBundle() {
        @Override
        public String getId() {
          return bundle.getId();
        }

        @Override
        public Map<String, FnDataReceiver> getInputReceivers() {
          return bundle.getInputReceivers();
        }

        @Override
        public Map<KV<String, String>, FnDataReceiver<Timer>> getTimerReceivers() {
          return bundle.getTimerReceivers();
        }

        @Override
        public void requestProgress() {
          throw new UnsupportedOperationException();
        }

        @Override
        public void split(double fractionOfRemainder) {
          bundle.split(fractionOfRemainder);
        }

        @Override
        public void close() throws Exception {
          try {
            bundle.close();
          } finally {
            client.unref();
            if (loadBalanceBundles) {
              availableCaches.offer(currentCache);
              availableCachesSemaphore.release();
            }
          }
        }
      };
    }

    @Override
    public ExecutableProcessBundleDescriptor getProcessBundleDescriptor() {
      return currentClient.processBundleDescriptor;
    }

    @Override
    public InstructionRequestHandler getInstructionRequestHandler() {
      return currentClient.wrappedClient.getClient().getInstructionRequestHandler();
    }

    @Override
    public void close() throws Exception {
      // Clear reference to encourage cache eviction. Values are weakly referenced.
      preparedClients.clear();
    }
  }

  /**
   * Holder for an {@link SdkHarnessClient} along with its associated state and data servers. As of
   * now, there is a 1:1 relationship between data services and harness clients. The servers are
   * packaged here to tie server lifetimes to harness client lifetimes.
   */
  protected static class WrappedSdkHarnessClient {

    private final RemoteEnvironment environment;
    private final SdkHarnessClient client;
    private final ServerInfo serverInfo;
    private final AtomicInteger bundleRefCount = new AtomicInteger();

    private boolean closed;

    static WrappedSdkHarnessClient wrapping(RemoteEnvironment environment, ServerInfo serverInfo) {
      SdkHarnessClient client =
          SdkHarnessClient.usingFnApiClient(
              environment.getInstructionRequestHandler(), serverInfo.getDataServer().getService());
      return new WrappedSdkHarnessClient(environment, client, serverInfo);
    }

    private WrappedSdkHarnessClient(
        RemoteEnvironment environment, SdkHarnessClient client, ServerInfo serverInfo) {
      this.environment = environment;
      this.client = client;
      this.serverInfo = serverInfo;
      ref();
    }

    SdkHarnessClient getClient() {
      return client;
    }

    ServerInfo getServerInfo() {
      return serverInfo;
    }

    public synchronized void close() {
      if (closed) {
        return;
      }
      // DO NOT ADD ANYTHING HERE WHICH MIGHT CAUSE THE BLOCK BELOW TO NOT BE EXECUTED.
      // If we exit prematurely (e.g. due to an exception), resources won't be cleaned up properly.
      // Please make an AutoCloseable and add it to the try statement below.
      // These will be closed in the reverse creation order:
      try (AutoCloseable envCloser = environment;
          AutoCloseable provisioningServer = serverInfo.getProvisioningServer();
          AutoCloseable retrievalServer = serverInfo.getRetrievalServer();
          AutoCloseable stateServer = serverInfo.getStateServer();
          AutoCloseable dataServer = serverInfo.getDataServer();
          AutoCloseable controlServer = serverInfo.getControlServer();
          // Close the logging server first to prevent spaming the logs with error messages
          AutoCloseable loggingServer = serverInfo.getLoggingServer()) {
        // Wrap resources in try-with-resources to ensure all are cleaned up.
        // This will close _all_ of these even in the presence of exceptions.
        // The first exception encountered will be the base exception,
        // the next one will be added via Throwable#addSuppressed.
        closed = true;
      } catch (Exception e) {
        LOG.warn("Error cleaning up servers {}", environment.getEnvironment(), e);
      }
      // TODO: Wait for executor shutdown?
    }

    private int ref() {
      return bundleRefCount.incrementAndGet();
    }

    private int unref() {
      int refCount = bundleRefCount.decrementAndGet();
      Preconditions.checkState(refCount >= 0, "Reference count must not be negative.");
      if (refCount == 0) {
        // Close environment after it was removed from cache and all bundles finished.
        LOG.info("Closing environment {}", environment.getEnvironment());
        close();
      }
      return refCount;
    }
  }

  private ServerInfo createServerInfo(JobInfo jobInfo, ServerFactory serverFactory)
      throws IOException {
    Preconditions.checkNotNull(serverFactory, "serverFactory can not be null");

    PortablePipelineOptions portableOptions =
        PipelineOptionsTranslation.fromProto(jobInfo.pipelineOptions())
            .as(PortablePipelineOptions.class);

    GrpcFnServer<FnApiControlClientPoolService> controlServer =
        GrpcFnServer.allocatePortAndCreateFor(
            FnApiControlClientPoolService.offeringClientsToPool(
                clientPool.getSink(), GrpcContextHeaderAccessorProvider.getHeaderAccessor()),
            serverFactory);
    GrpcFnServer<GrpcLoggingService> loggingServer =
        GrpcFnServer.allocatePortAndCreateFor(
            GrpcLoggingService.forWriter(Slf4jLogWriter.getDefault()), serverFactory);
    GrpcFnServer<ArtifactRetrievalService> retrievalServer =
        GrpcFnServer.allocatePortAndCreateFor(new ArtifactRetrievalService(), serverFactory);
    ProvisionApi.ProvisionInfo.Builder provisionInfo = jobInfo.toProvisionInfo().toBuilder();
    provisionInfo.setLoggingEndpoint(loggingServer.getApiServiceDescriptor());
    provisionInfo.setArtifactEndpoint(retrievalServer.getApiServiceDescriptor());
    provisionInfo.setControlEndpoint(controlServer.getApiServiceDescriptor());
    GrpcFnServer<StaticGrpcProvisionService> provisioningServer =
        GrpcFnServer.allocatePortAndCreateFor(
            StaticGrpcProvisionService.create(
                provisionInfo.build(), GrpcContextHeaderAccessorProvider.getHeaderAccessor()),
            serverFactory);
    GrpcFnServer<GrpcDataService> dataServer =
        GrpcFnServer.allocatePortAndCreateFor(
            GrpcDataService.create(
                portableOptions, executor, OutboundObserverFactory.serverDirect()),
            serverFactory);
    GrpcFnServer<GrpcStateService> stateServer =
        GrpcFnServer.allocatePortAndCreateFor(GrpcStateService.create(), serverFactory);

    ServerInfo serverInfo =
        new AutoValue_DefaultJobBundleFactory_ServerInfo.Builder()
            .setControlServer(controlServer)
            .setLoggingServer(loggingServer)
            .setRetrievalServer(retrievalServer)
            .setProvisioningServer(provisioningServer)
            .setDataServer(dataServer)
            .setStateServer(stateServer)
            .build();
    return serverInfo;
  }

  /** A container for EnvironmentFactory and its corresponding Grpc servers. */
  @AutoValue
  public abstract static class ServerInfo {
    abstract GrpcFnServer<FnApiControlClientPoolService> getControlServer();

    abstract GrpcFnServer<GrpcLoggingService> getLoggingServer();

    abstract GrpcFnServer<ArtifactRetrievalService> getRetrievalServer();

    abstract GrpcFnServer<StaticGrpcProvisionService> getProvisioningServer();

    abstract GrpcFnServer<GrpcDataService> getDataServer();

    abstract GrpcFnServer<GrpcStateService> getStateServer();

    abstract Builder toBuilder();

    @AutoValue.Builder
    abstract static class Builder {
      abstract Builder setControlServer(GrpcFnServer<FnApiControlClientPoolService> server);

      abstract Builder setLoggingServer(GrpcFnServer<GrpcLoggingService> server);

      abstract Builder setRetrievalServer(GrpcFnServer<ArtifactRetrievalService> server);

      abstract Builder setProvisioningServer(GrpcFnServer<StaticGrpcProvisionService> server);

      abstract Builder setDataServer(GrpcFnServer<GrpcDataService> server);

      abstract Builder setStateServer(GrpcFnServer<GrpcStateService> server);

      abstract ServerInfo build();
    }
  }
}


text:
The `argocd-application-controller` has been converted to StatefulSet. That means you need to manually delete `argocd-application-controller` Deployment after upgrading.
Similarly if you decided to rollback to v1.7 don't forget to delete `argocd-application-controller` StatefulSet.

## Health assessment of argoproj.io/Application CRD has been removed

The health assessment of `argoproj.io/Application` CRD has been removed (see [#3781](https://github.com/argoproj/argo-cd/issues/3781) for more information).
You might need to restore it if you are using app-of-apps pattern and orchestrating synchronization using sync waves. Add the following resource customization in
`argocd-cm` ConfigMap:

```yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-cm
  namespace: argocd
  labels:
    app.kubernetes.io/name: argocd-cm
    app.kubernetes.io/part-of: argocd
data:
  resource.customizations: |
    argoproj.io/Application:
      health.lua: |
        hs = {}
        hs.status = "Progressing"
        hs.message = ""
        if obj.status ~= nil then
          if obj.status.health ~= nil then
            hs.status = obj.status.health.status
            if obj.status.health.message ~= nil then
              hs.message = obj.status.health.message
            end
          end
        end
        return hs
```
> To modify an existing installation with no existing `resources.customizations`, you can save the `data:` stanza to file and patch the configmap with e.g.: `kubectl -n argocd patch configmaps argocd-cm --patch-file argocd-cm-patch.yaml`

## gRPC metrics are disabled by default

The gRPC metrics are not exposed by default by `argocd-server` and `argocd-repo-server` anymore. These metrics appear
to be too expensive so we've decided to disable them by default. Metrics can be enabled using
`ARGOCD_ENABLE_GRPC_TIME_HISTOGRAM=true` environment variable.

From here on you can follow the [regular upgrade process](./overview.md).


text:
#include "Logs.h"
#include "Util.h"
#include "string.h"

using namespace std;
using namespace Poco;

namespace Cumulus {

Logger*			Logs::_PLogger(NULL);
Logs::DumpMode	Logs::_DumpMode(NOTHING);
UInt8			Logs::_Level(Logger::PRIO_INFO); // default log level

Logs::Logs() {
}

Logs::~Logs() {
}

void Logs::Dump(const UInt8* data,UInt32 size,const char* header,bool middle) {
	UInt8 type = middle ? MIDDLE : EXTERNAL;
	if(GetLogger() && _DumpMode&type) {
		vector<UInt8> out;
		Util::Dump(data,size,out,header);
		if(out.size()>0)
			GetLogger()->dumpHandler(&out[0],out.size());
	}
}

} // namespace Cumulus


text:
module Bouquet
  module Gate
    class ApplicationRecord < ActiveRecord::Base
      self.abstract_class = true
    end
  end
end


text:
/**
 * The base implementation of `_.findIndex` and `_.findLastIndex` without
 * support for callback shorthands and `this` binding.
 *
 * @private
 * @param {Array} array The array to search.
 * @param {Function} predicate The function invoked per iteration.
 * @param {boolean} [fromRight] Specify iterating from right to left.
 * @returns {number} Returns the index of the matched value, else `-1`.
 */
function baseFindIndex(array, predicate, fromRight) {
  var length = array.length,
      index = fromRight ? length : -1;

  while ((fromRight ? i-- : ++i < length)) {
    if (predicate(array[i], i, array)) {
      return i;
    }
  }
  return -1;
}

module.exports = baseFindIndex;


text:
<?php

namespace BingAds\CampaignManagement;

/**
 * Defines a site link to include in an ad. The link should take the user to content in your website that is relevant in the context of the ad.
 * @link http://msdn.microsoft.com/en-us/library/jj134381(v=msads.90).aspx SiteLink Data Object
 */
final class SiteLink
{
    /**
     * The URL of the webpage that users are taken to when they click the site link. The URL can contain a maximum of 1,024 characters.
     * @var string
     */
    public $DestinationUrl;

    /**
     * This element determines whether the preference is for site links to be displayed on mobile devices or all devices.
     * @var long
     */
    public $DevicePreference;

    /**
     * The site-link text displayed in the ad. The text can contain a maximum of 35 characters.
     * @var string
     */
    public $DisplayText;
}


text:
\n
static void __init mp_sfi_register_lapic(u8 id)
static int __init sfi_parse_cpus(struct sfi_table_header *table)
static int __init sfi_parse_ioapic(struct sfi_table_header *table)
int __init sfi_platform_init(void)
\n
      2 struct sfi_table_header *table
      1 void
      1 u8 id


text:
<?php

return [
    'interfaces' => [
        'google.ads.googleads.v12.services.PaymentsAccountService' => [],
    ],
];


text:
<?php

namespace Drupal\FunctionalJavascriptTests\Tests;

use Behat\Mink\Element\NodeElement;
use Behat\Mink\Exception\ElementHtmlException;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;

/**
 * Tests for the JSWebAssert class.
 *
 * @group javascript
 */
class JSWebAssertTest extends WebDriverTestBase {

  /**
   * Required modules.
   *
   * @var array
   */
  protected static $modules = ['js_webassert_test'];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * Tests that JSWebAssert assertions work correctly.
   */
  public function testJsWebAssert() {
    $this->drupalGet('js_webassert_test_form');

    $session = $this->getSession();
    $assert_session = $this->assertSession();
    $page = $session->getPage();

    $assert_session->elementExists('css', '[data-drupal-selector="edit-test-assert-no-element-after-wait-pass"]');
    $page->findButton('Test assertNoElementAfterWait: pass')->press();
    $assert_session->assertNoElementAfterWait('css', '[data-drupal-selector="edit-test-assert-no-element-after-wait-pass"]', 1000);

    $assert_session->elementExists('css', '[data-drupal-selector="edit-test-assert-no-element-after-wait-fail"]');
    $page->findButton('Test assertNoElementAfterWait: fail')->press();
    try {
      $assert_session->assertNoElementAfterWait('css', '[data-drupal-selector="edit-test-assert-no-element-after-wait-fail"]', 500, 'Element exists on page after too short wait.');
      $this->fail('Element not exists on page after too short wait.');
    }
    catch (ElementHtmlException $e) {
      $this->assertSame('Element exists on page after too short wait.', $e->getMessage());
    }

    $assert_session->assertNoElementAfterWait('css', '[data-drupal-selector="edit-test-assert-no-element-after-wait-fail"]', 2500, 'Element remove after another wait.ss');

    $test_button = $page->findButton('Add button');
    $test_link = $page->findButton('Add link');
    $test_field = $page->findButton('Add field');
    $test_id = $page->findButton('Add ID');
    $test_wait_on_ajax = $page->findButton('Test assertWaitOnAjaxRequest');
    $test_wait_on_element_visible = $page->findButton('Test waitForElementVisible');

    // Test the wait...() methods by first checking the fields aren't available
    // and then are available after the wait method.
    $result = $page->findButton('Added button');
    $this->assertEmpty($result);
    $test_button->click();
    $result = $assert_session->waitForButton('Added button');
    $this->assertNotEmpty($result);
    $this->assertInstanceOf(NodeElement::class, $result);

    $result = $page->findLink('Added link');
    $this->assertEmpty($result);
    $test_link->click();
    $result = $assert_session->waitForLink('Added link');
    $this->assertNotEmpty($result);
    $this->assertInstanceOf(NodeElement::class, $result);

    $result = $page->findField('added_field');
    $this->assertEmpty($result);
    $test_field->click();
    $result = $assert_session->waitForField('added_field');
    $this->assertNotEmpty($result);
    $this->assertInstanceOf(NodeElement::class, $result);

    $result = $page->findById('js_webassert_test_field_id');
    $this->assertEmpty($result);
    $test_id->click();
    $result = $assert_session->waitForId('js_webassert_test_field_id');
    $this->assertNotEmpty($result);
    $this->assertInstanceOf(NodeElement::class, $result);

    // Test waitOnAjaxRequest. Verify the element is available after the wait
    // and the behaviors have run on completing by checking the value.
    $result = $page->findField('test_assert_wait_on_ajax_input');
    $this->assertEmpty($result);
    $test_wait_on_ajax->click();
    $assert_session->assertWaitOnAjaxRequest();
    $result = $page->findField('test_assert_wait_on_ajax_input');
    $this->assertNotEmpty($result);
    $this->assertInstanceOf(NodeElement::class, $result);
    $this->assertEquals('js_webassert_test', $result->getValue());

    $result = $page->findButton('Added WaitForElementVisible');
    $this->assertEmpty($result);
    $test_wait_on_element_visible->click();
    $result = $assert_session->waitForElementVisible('named', ['button', 'Added WaitForElementVisible']);
    $this->assertNotEmpty($result);
    $this->assertInstanceOf(NodeElement::class, $result);
    $this->assertEquals(TRUE, $result->isVisible());

    $this->drupalGet('js_webassert_test_page');
    $result = $assert_session->waitForElementVisible('named', ['id', 'test_text']);
    $this->assertSame('test_text', $result->getAttribute('id'));
    // Ensure that the javascript has replaced the element 1100 times.
    $assert_session->pageTextContains('New Text!! 1100');
  }

}


text: var Kawari7 = function(){


text:
@interface Harmony_CombosAppDelegate : NSObject <NSApplicationDelegate> {
    NSWindow *window;
    Midi_Interface *playInterface;
    lilyInterface *lilyInstance;
    NSMenuItem *showMainWndMenu;
    NSDictionary *interfaceStrings;
	IBOutlet NSTextField *bassoMin, *bassoMax, *tenoreMin, *tenoreMax, *altoMin, *altoMax, *sopranoMin, *sopranoMax;
	IBOutlet NSPopUpButton *popUpButton;
	IBOutlet NSTabView *tabView;
}
@property (assign) IBOutlet NSWindow *window;
-(void)showMainWindow:(id)sender;
@end


text:
from django.template import Context, loader
from myblog.models import blogpost
from django.http import HttpResponse
from django.shortcuts import render_to_response

def index(request):
    latest_blog_list = blogpost.objects.all().order_by('-pub_date')[:5]
    return render_to_response('myblog/index.html', {'latest_blog_list': latest_blog_list,
                                                     'user': request.user})

def detail(request, blogpost_id):
    post=blogpost.objects.filter(id=blogpost_id)[0]
    return render_to_response('myblog/details.html', {'post': post,
                                                      'user': request.user})


text:
package com.networknt.light.rule.page;

import com.networknt.light.model.CacheObject;
import com.networknt.light.rule.Rule;
import com.networknt.light.util.ServiceLocator;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.Headers;
import io.undertow.util.HttpString;

import java.util.Map;

/**
 * Created by steve on 10/24/2014.
 *
 * AccessLevel A
 *
 */
public class GetPageRule extends AbstractPageRule implements Rule {
    public boolean execute (Object ...objects) throws Exception {
        Map<String, Object> inputMap = (Map<String, Object>)objects[0];
        Map<String, Object> data = (Map<String, Object>)inputMap.get("data");
        HttpServerExchange exchange = (HttpServerExchange)inputMap.get("exchange");
        String pageId = (String)data.get("pageId");
        OrientGraph graph = ServiceLocator.getInstance().getGraph();
        CacheObject co = null;
        try {
            co = getPageById(graph, pageId);
        } catch (Exception e) {
            logger.error("Exception:", e);
            throw e;
        } finally {
            graph.shutdown();
        }
        if(co != null) {
            if(!matchEtag(inputMap, co)) {
                inputMap.put("result", co.getData());
            }
            return true;
        } else {
            inputMap.put("responseCode", 404);
            inputMap.put("result", "Page with id " + pageId + " cannot be found.");
            return false;
        }
    }

}


text:
"""Example DAG demonstrating the usage of the AirbyteTriggerSyncOperator."""
from __future__ import annotations

import os
from datetime import datetime, timedelta

from airflow import DAG
from airflow.providers.airbyte.operators.airbyte import AirbyteTriggerSyncOperator
from airflow.providers.airbyte.sensors.airbyte import AirbyteJobSensor

ENV_ID = os.environ.get("SYSTEM_TESTS_ENV_ID")
DAG_ID = "example_airbyte_operator"
CONN_ID = '15bc3800-82e4-48c3-a32d-620661273f28'

with DAG(
    dag_id=DAG_ID,
    schedule=None,
    start_date=datetime(2021, 1, 1),
    dagrun_timeout=timedelta(minutes=60),
    tags=['example'],
    catchup=False,
) as dag:

    # [START howto_operator_airbyte_synchronous]
    sync_source_destination = AirbyteTriggerSyncOperator(
        task_id='airbyte_sync_source_dest_example',
        connection_id=CONN_ID,
    )
    # [END howto_operator_airbyte_synchronous]

    # [START howto_operator_airbyte_asynchronous]
    async_source_destination = AirbyteTriggerSyncOperator(
        task_id='airbyte_async_source_dest_example',
        connection_id=CONN_ID,
        asynchronous=True,
    )

    airbyte_sensor = AirbyteJobSensor(
        task_id='airbyte_sensor_source_dest_example',
        airbyte_job_id=async_source_destination.output,
    )
    # [END howto_operator_airbyte_asynchronous]

    # Task dependency created via `XComArgs`:
    #   async_source_destination >> airbyte_sensor

from tests.system.utils import get_test_run  # noqa: E402

# Needed to run the example DAG with pytest (see: tests/system/README.md#run_via_pytest)
test_run = get_test_run(dag)


text:
ACCEPTED

#### According to
Index Fungorum

#### Published in
Mem. N. Y. bot. Gdn 24: 166 (1972)

#### Original name
Psathyrella iterata var. ochraceispora A.H. Sm.

### Remarks
null


text:
mpris-ctrl
==========

Command-line utility for controlling media players supporting MPRIS (vlc, audacity, bmp, xmms2, spotify (partial), etc.)

## About
`mpris-ctrl` simplifies common `D-Bus` message commands that are supported by the [MPRIS D-Bus interface specification](http://specifications.freedesktop.org/mpris-spec/latest/)

## Installing
`mpris-ctrl` is a standalone script, and is installed by placing it (or symlinking to) somewhere in the `PATH` variable.

A suggested way to download and install for all users:
```
sudo git clone https://github.com/swarminglogic/mpris-ctrl.git /opt/mpris-ctrl
sudo ln -s /opt/mpris-ctrl/mpris-ctrl.sh /usr/local/bin/mpris-ctrl
```

## Usage / Supported Commands
```
USAGE:
mpris-ctrl [OPTIONS]
mpris-ctrl PLAYER COMMAND [ARGUMENTS]

OPTIONS:
-h, --help              Show help
-v, --version           Print version information
-l, --list              Lists available NAME targets

COMMANDS (ROOT):
raise                   Raises player window
quit                    Quits player

COMMANDS (PLAYER):
play                    Issues play command
pause                   Issues pause command
stop                    Issues stop command
play-pause              Toggles between playing and pausing
next                    Plays next item in playlist
prev[ious]              Plays previous item in playlist
status                  Displays the status of the player
volumeup [VALUE]        Increase volume level by [VALUE].
volumedown [VALUE]      Decrease volume level by [VALUE].
                        VALUE should be in range [0.1, 1.0].
                        If no [VALUE] is set then the default is 0.05
volume                  Get volume level.
volume [LEVEL]          Set volume level.
                        LEVEL should be in range [0.0, 1.0]

COMMANDS (PLAYLIST):
tracks                  Displays tracks
add-track PATH [PLAY]   Appends file PATH to playlist.
                        PLAY should be one of {true, false(default)}
                        If true: append and skip to this item in the playlist.
                        If false, append the item to the playlist.
```

## Examples
Listing active and supported MPRIS player targets:
```
$ mpris-ctrl -l
```

Toggling spotify play-pause:
```
$ mpris-ctrl spotify play-pause
```

Jump to next spotify track:
```
$ mpris-ctrl spotify next
```

Append video to vlc playlist:
```
$ mpris-ctrl vlc add-track video.mp4
```

## License
The `mpris-ctrl` utility is licensed under the MIT License.

See [`LICENSE`](LICENSE)


text:
        {% if include.title %}
{{ include.title | upcase }}
{% endif %}

Plano Vire-Lata

Plano Raça Pura

Plano Pedigree

        {% for features in site.data.features-ecom %}

{{ features.title }}

{{ features.check1 }}

{{ features.check2 }}

{{ features.check3 }}

		{% endfor %}


text:
'use strict'

exports.SentimentAnalyzer = require('./SentimentAnalyzer')


text:
package types

// DO NOT EDIT. THIS FILE WAS AUTOMATICALLY GENERATED

import (
	time "time"

	types "github.com/sudorandom/softlayer-go/slapi/types"
)

// SoftLayer_Metric_Tracking_Object_Data_Network_ContentDelivery_Account -
// SoftLayer_Metric_Tracking_Object_Data_Network_ContentDelivery_Account models usage data polled from
// the CDN system.
type SoftLayer_Metric_Tracking_Object_Data_Network_ContentDelivery_Account struct {

	// Counter - no documentation
	Counter types.Float64 `json:"counter,omitempty"`

	// DateTime - no documentation
	DateTime *time.Time `json:"dateTime,omitempty"`

	// FileName - The name of a file. This value is only populated in file-based bandwidth reports.
	FileName string `json:"fileName,omitempty"`

	// PopId - The internal identifier of a CDN POP (Points of Presence).
	PopId int `json:"popId,omitempty"`

	// Type - no documentation
	Type string `json:"type,omitempty"`
}

func (softlayer_metric_tracking_object_data_network_contentdelivery_account *SoftLayer_Metric_Tracking_Object_Data_Network_ContentDelivery_Account) String() string {
	return "SoftLayer_Metric_Tracking_Object_Data_Network_ContentDelivery_Account"
}


text: support jdk8 features for jdk7