text:
package org.ecloudmanager.node.aws;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.model.*;
import com.amazonaws.services.ec2.model.Tag;
import com.amazonaws.services.route53.AmazonRoute53;
import com.amazonaws.services.route53.model.*;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import net.jodah.failsafe.Failsafe;
import net.jodah.failsafe.RetryPolicy;
import org.apache.commons.lang3.StringUtils;
import org.ecloudmanager.node.NodeBaseAPI;
import org.ecloudmanager.node.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class AWSNodeAPI implements NodeBaseAPI {
    private static final int RETRY_TIMEOUT = 10;
    static Logger log = LoggerFactory.getLogger(AWSNodeAPI.class);
    private static String DEFAULT_SECURITY_GROUP_NAME = "default";
    private static String TAG_NAME = "Name";
    private static String TAG_CREATOR = "Creator";
    private static String TAG_HOSTED_ZONE = "Hosted Zone";
    private static APIInfo API_INFO = new APIInfo().id("AWS").description("Amazon EC2");
    static {
        try {
            GitInfo gitInfo = GitInfo.get();
            API_INFO = API_INFO.version(gitInfo.getBuildVersion()).revision(gitInfo.getCommitIdAbbrev());
        } catch (IOException e) {
            log.error("Can't read Git Info", e);
        }
    }

    private static String getSubnetLabel(com.amazonaws.services.ec2.model.Subnet subnet) {
        return subnet.getTags().stream()
                .filter(t -> t.getKey().equals("Name"))
                .map(Tag::getValue)
                .findAny()
                .orElse("");
    }

    private static List<FirewallRule> fromIpPermission(IpPermission permission) {
        return permission.getIpRanges().stream().map(range ->
                new FirewallRule().type(FirewallRule.TypeEnum.IP).protocol(permission.getIpProtocol()).port(permission.getToPort()).from(range)).collect(Collectors.toList());
    }

    private static Predicate<Throwable> errorCode(String errorCode) {
        return e -> AmazonServiceException.class.isInstance(e) && errorCode.equals(AmazonServiceException.class.cast(e).getErrorCode());
    }

    @Override
    public APIInfo getAPIInfo() {
        return API_INFO;
    }

    private String getUserId(String accessKey, String secretKey) {
        try {
            return AWS.identityManagementClient(accessKey, secretKey).getUser().getUser().getUserId();
        } catch (AmazonServiceException e) {
            return e.getErrorMessage().replaceAll("^[^/]*/", "").replaceAll(" is not authorized .*", "");
        }
    }

    private void createTags(String userId, String instanceId, Map<String, String> parameters, AmazonEC2 ec2) {
        ArrayList<Tag> tags = new ArrayList<>();
        tags.add(new Tag(TAG_CREATOR, userId));
        getTagParameters().forEach(p -> {
                    if (parameters.containsKey(p.getName())) {
                        tags.add(new Tag(p.getName(), parameters.get(p.getName())));
                    }
        });
        CreateTagsRequest createTagsRequest = new CreateTagsRequest()
                .withResources(instanceId)
                .withTags(tags);
        log.info("Updating tags of AWS instance " + instanceId);
        ec2.createTags(createTagsRequest);
        log.info("Tags created:");
        createTagsRequest.getTags().forEach(t -> log.info(t.toString()));
    }

    private List<NodeParameter> getTagParameters() {
        // FIXME should be configurable
        NodeParameter costCenter = new NodeParameter().name("Cost Center").description("Cost Center").create(true).configure(true).canSuggest(true).strictSuggest(false).required(true).defaultValue("521633");
        NodeParameter group = new NodeParameter().name("Group").description("Group").create(true).configure(true).canSuggest(true).strictSuggest(false).required(true).defaultValue("rfng");
        return Lists.newArrayList(costCenter, group);
    }

    @Override
    public List<NodeParameter> getNodeParameters(Credentials credentials) {
        return Stream.concat(Arrays.stream(Parameter.values()).map(Parameter::getNodeParameter), getTagParameters().stream()).collect(Collectors.toList());
    }

    private List<String> getTagValues(AmazonEC2 ec2, String tag) {
        DescribeTagsResult describeTagsResult = ec2.describeTags(new DescribeTagsRequest().withFilters(new Filter("key", Lists.newArrayList(tag))));
        return describeTagsResult.getTags().stream()
                .map(TagDescription::getValue)
                .distinct()
                .collect(Collectors.toList());
    }

    @Override
    public List<ParameterValue> getNodeParameterValues(Credentials credentials, String parameter, Map<String, String> parameters) throws AWS.RegionNotExistException, ExecutionException {
        String accessKey = ((SecretKey) credentials).getName();
        String secretKey = ((SecretKey) credentials).getSecret();
        String region = parameters == null ? null : parameters.get(Parameter.region.name());
        AmazonEC2 amazonEC2 = region == null ? null : AWS.ec2(accessKey, secretKey, region);
        AmazonRoute53 route53Client = AWS.route53(accessKey, secretKey);
        Parameter p;
        try {
            p = Parameter.valueOf(parameter);
        } catch (IllegalArgumentException e) {
            return getTagValues(amazonEC2, parameter).stream().map(t -> new ParameterValue().value(t)).collect(Collectors.toList());
        }
        switch (p) {
            case region:
                return Stream.of(Regions.values())
                        .map(r -> new ParameterValue().value(r.getName())).collect(Collectors.toList());
            case instance_type:
                return Stream.of(AWSInstanceType.values())
                        .map(t -> new ParameterValue().value(t.getInstanceTypeName()))
                        .collect(Collectors.toList());
            case subnet:
                return amazonEC2 == null ?
                        Collections.emptyList() :
                        amazonEC2.describeSubnets().getSubnets().stream()
                                .map(s -> new ParameterValue().value(s.getSubnetId())
                                        .description(s.getSubnetId() + " : " + s.getCidrBlock() + " " + getSubnetLabel(s))).collect(Collectors.toList());
            case keypair:
                return amazonEC2 == null ?
                        Collections.emptyList() :
                        amazonEC2.describeKeyPairs().getKeyPairs().stream()
                                .map(k -> new ParameterValue().value(k.getKeyName())).collect(Collectors.toList());
            case hosted_zone:
                return route53Client.listHostedZones().getHostedZones().stream().map(z -> new ParameterValue().value(z.getName())).collect(Collectors.toList());
            case image:
//                if (amazonEC2 == null) {
//                    return Collections.emptyList();
//                }
//                try {
//                    List<Image> images = amazonEC2.describeImages().getImages();
//                    return  images.stream()
//                            .map(i -> new ParameterValue().value(i.getImageId()).description(i.getDescription())).collect(Collectors.toList());
//                } catch (Exception t) {
//                    t.printStackTrace();
//
//                }
            case storage:
            case name:
            case cpu:
            case memory:
                return Collections.emptyList();
        }
        return Collections.emptyList();
    }

    @Override
    public String createNode(Credentials credentials, Map<String, String> parameters) throws AWS.RegionNotExistException, ExecutionException {
        String accessKey = ((SecretKey) credentials).getName();
        String secretKey = ((SecretKey) credentials).getSecret();
        String region = parameters.get(Parameter.region.name());
        String subnet = parameters.get(Parameter.subnet.name());
        String name = parameters.get(Parameter.name.name());
        int storage = Integer.parseInt(parameters.get(Parameter.storage.name()));
        String image = parameters.get(Parameter.image.name());
        String instanceType = parameters.get(Parameter.instance_type.name());
        String keyPair = parameters.get(Parameter.keypair.name());
        log.info("Creating AWS security group " + name);
        AmazonEC2 ec2 = AWS.ec2(accessKey, secretKey, region);
        String securityGroupId = createSecurityGroup(ec2, subnet, name);
        log.info("Security group created with name: " + name + " id: " + securityGroupId);
        try {
            addSshAcess(ec2, securityGroupId);
            log.info("Ssh access firewall rule added");
        } catch (Exception t) {
            log.error("Can't add ssh access firewall rule", t);
            deleteSecurityGroupOnFailure(ec2, securityGroupId);
            throw t;
        }
        String vmId;
        RetryPolicy createNodeRetryPolicy = new RetryPolicy()
                .retryOn(e -> AmazonServiceException.class.isInstance(e) && "InvalidGroup.NotFound".equals(AmazonServiceException.class.cast(e).getErrorCode()))
                .withDelay(1, TimeUnit.SECONDS)
                .withMaxDuration(10, TimeUnit.MINUTES);
        try {
            vmId = Failsafe.with(createNodeRetryPolicy).get(() -> createNode(ec2, storage, securityGroupId, subnet, image, instanceType, keyPair));
            log.info("VM created with id: " + vmId);
        } catch (Exception t) {
            log.error("Can't create node", t);
            deleteSecurityGroupOnFailure(ec2, securityGroupId);
            throw t;
        }
        return region + ":" + vmId;
    }

    private void deleteSecurityGroupOnFailure(AmazonEC2 ec2, String id) {
        try {
            log.info("Security group deleted with id: " + id);
            ec2.deleteSecurityGroup(new DeleteSecurityGroupRequest().withGroupId(id));
            log.info("Security group deleted with id: " + id);
        } catch (Exception t1) {
            log.error("WARNING! Failed to delete security group with id: " + id, t1);
        }
    }

    @Override
    public NodeInfo getNode(Credentials credentials, String nodeId) throws AWS.RegionNotExistException, ExecutionException {
        String accessKey = ((SecretKey) credentials).getName();
        String secretKey = ((SecretKey) credentials).getSecret();
        String region = nodeId.split(":")[0];
        String id = nodeId.split(":")[1];
        AmazonEC2 ec2 = AWS.ec2(accessKey, secretKey, region);
        Reservation reservation = ec2.describeInstances(new DescribeInstancesRequest().withInstanceIds(id)).getReservations().get(0);
        Instance instance = reservation.getInstances().get(0);
        String vpcId = instance.getVpcId();
        String awsStatus = instance.getState().getName();
        NodeInfo.StatusEnum status;
        switch (awsStatus) {
            case "running":
                status = NodeInfo.StatusEnum.RUNNING;
                break;
            case "terminated":
                status = NodeInfo.StatusEnum.TERMINATED;
                break;
            default:
                status = NodeInfo.StatusEnum.PENDING;
        }
        NodeInfo info = new NodeInfo().status(status).ip(instance.getPrivateIpAddress()).id(nodeId);
        return info;
    }

    private IpPermission fromFirewallRule(Credentials credentials, FirewallRule rule) throws IllegalArgumentException {
        String ipRange = "";
        switch (rule.getType()) {
            case IP:
                ipRange = rule.getFrom() + "/32";
                break;
            case NODE_ID:
                try {
                    ipRange = getNode(credentials, rule.getFrom()).getIp() + "/32";
                } catch (AWS.RegionNotExistException | ExecutionException e) {
                    throw new IllegalArgumentException("Can't access node " + rule.getFrom(), e);
                }
                break;
            case ANY:
                ipRange = "0.0.0.0/0";
                break;
        }
        return new IpPermission().withFromPort(rule.getPort()).withToPort(rule.getPort()).withIpProtocol(rule.getProtocol()).withIpRanges(ipRange);
    }

    private String getCidrIp() {
        return "0.0.0.0/0";
    }

    private int getStorageSize(AmazonEC2 ec2, String vmId) {
        DescribeInstancesResult result =
                ec2.describeInstances(new DescribeInstancesRequest().withInstanceIds(vmId));
        Instance instance = result.getReservations().get(0).getInstances().get(0);
        InstanceBlockDeviceMapping blockDeviceMapping = instance.getBlockDeviceMappings().get(0);
        String volumeId = blockDeviceMapping.getEbs().getVolumeId();
        DescribeVolumesResult dvr = ec2.describeVolumes(new DescribeVolumesRequest().withVolumeIds(volumeId));
        return dvr.getVolumes().get(0).getSize();
    }

    private Instance getInstance(AmazonEC2 ec2, String vmId) {
        DescribeInstancesResult result =
                ec2.describeInstances(new DescribeInstancesRequest().withInstanceIds(vmId));
        return result.getReservations().size() > 0 ? result.getReservations().get(0).getInstances().get(0) : null;
    }

    private String getInstanceName(Instance instance) {
        Optional<Tag> tag = instance.getTags().stream().filter(t -> t.getKey().equals(TAG_NAME)).findAny();
        return tag.isPresent() ? tag.get().getValue() : null;
    }

    private void setInstanceName(AmazonEC2 ec2, String vmId, String name) {
        ec2.createTags(new CreateTagsRequest()
                .withResources(vmId)
                .withTags(new Tag(TAG_NAME, name)));
    }

    @Override
    public NodeInfo configureNode(Credentials credentials, String nodeId, Map<String, String> parameters) throws Exception {
        String accessKey = ((SecretKey) credentials).getName();
        String secretKey = ((SecretKey) credentials).getSecret();
        String region = nodeId.split(":")[0];
        String vmId = nodeId.split(":")[1];
        String userId = getUserId(accessKey, secretKey);
        AmazonEC2 ec2 = AWS.ec2(accessKey, secretKey, region);
        AmazonRoute53 route53 = AWS.route53(accessKey, secretKey);

        Instance instance = getInstance(ec2, vmId);

        String oldName = getInstanceName(instance);
        String newName = parameters.get(Parameter.name.name());
        if (newName != null && !newName.equals(oldName)) {
            setInstanceName(ec2, vmId, newName);
            log.info("Value of tag '" + TAG_NAME + "' set to " + newName);

        }
        createTags(userId, vmId, parameters, ec2);

        String oldHostedZone = getInstanceHostedZone(instance);
        String newHostedZone = parameters.get(Parameter.hosted_zone.name());
        if (!StringUtils.equals(oldHostedZone, newHostedZone)) {
            setInstanceHostedZone(ec2, vmId, newHostedZone);
            log.info("Value of tag '" + TAG_HOSTED_ZONE + "' set to " + newHostedZone);
        }
        if (!StringUtils.equals(oldName, newName) ||
                !StringUtils.equals(oldHostedZone, newHostedZone)) {
            if (oldHostedZone != null) {
                deleteDnsRecord(route53, oldName, oldHostedZone);
            }
            createDnsRecord(route53, instance.getPrivateIpAddress(), newName, newHostedZone);
            log.info("Created DNS record for " + newName + "." + newHostedZone + " with IP: " + instance.getPrivateIpAddress());
        }

        String newInstanceType = parameters.get(Parameter.instance_type.name());

        String newStorage = parameters.get(Parameter.storage.name());
        boolean needUpdateInstanceType = newInstanceType != null && !StringUtils.equals(instance.getInstanceType(), newInstanceType);

        boolean needResizeStorage = newStorage != null && getStorageSize(ec2, vmId) != Integer.parseInt(newStorage);
        if (needUpdateInstanceType || needResizeStorage) {
            log.info("Stopping AWS instance " + vmId + "(" + newName + ")");
            StopInstancesRequest stopInstancesRequest = new StopInstancesRequest().withInstanceIds(vmId);
            ec2.stopInstances(stopInstancesRequest);
            RetryPolicy untilInstanceStopped = new RetryPolicy()
                    .<DescribeInstancesResult>retryIf((r) -> !"stopped".equals(r.getReservations().get(0).getInstances().get(0).getState().getName()))
                    .withDelay(1, TimeUnit.SECONDS)
                    .withMaxDuration(10, TimeUnit.MINUTES);
            Failsafe.with(untilInstanceStopped).get(() -> ec2.describeInstances(new DescribeInstancesRequest().withInstanceIds(vmId)));
        }

        if (needUpdateInstanceType) {
            log.info("Changing instance type of AWS instance " + vmId + "(" + newName + ") to " + newInstanceType);
            ModifyInstanceAttributeRequest modifyInstanceAttributeRequest =
                    new ModifyInstanceAttributeRequest().withInstanceId(vmId).withInstanceType(newInstanceType);
            ec2.modifyInstanceAttribute(modifyInstanceAttributeRequest);
        }

        if (needResizeStorage) {
            log.info("Resizing root volume of AWS instance " + vmId + "(" + newName + ")");
            InstanceBlockDeviceMapping blockDeviceMapping = instance.getBlockDeviceMappings().get(0);
            String volumeId = blockDeviceMapping.getEbs().getVolumeId();
            log.info("Creating snapshot of AWS volume " + volumeId + " attached to instance " + vmId + "(" + newName + ")");
            CreateSnapshotRequest createSnapshotRequest = new CreateSnapshotRequest(volumeId, "Resize instance " + vmId);
            CreateSnapshotResult createSnapshotResult = ec2.createSnapshot(createSnapshotRequest);
            String snapshotId = createSnapshotResult.getSnapshot().getSnapshotId();

            RetryPolicy untilSnapshotCompleted = new RetryPolicy()
                    .<Snapshot>retryIf(s -> !"completed".equals(s.getState()))
                    .withDelay(1, TimeUnit.SECONDS)
                    .withMaxDuration(10, TimeUnit.MINUTES);
            Failsafe.with(untilSnapshotCompleted).get(() -> ec2.describeSnapshots(new DescribeSnapshotsRequest().withSnapshotIds(snapshotId)).getSnapshots().get(0));

            DescribeVolumesRequest describeVolumesRequest = new DescribeVolumesRequest().withVolumeIds(volumeId);
            DescribeVolumesResult describeVolumesResult = ec2.describeVolumes(describeVolumesRequest);
            String availabilityZone = describeVolumesResult.getVolumes().get(0).getAvailabilityZone();

            log.info("Creating new volume " + volumeId + " in availability zone " + availabilityZone + " from snapshot " + snapshotId);
            CreateVolumeRequest createVolumeRequest =
                    new CreateVolumeRequest(snapshotId, availabilityZone).withSize(Integer.parseInt(newStorage));
            CreateVolumeResult createVolumeResult = ec2.createVolume(createVolumeRequest);
            String newVolumeId = createVolumeResult.getVolume().getVolumeId();

            log.info("New volume " + newVolumeId + " was created from snapshot " + snapshotId);

            RetryPolicy untilVolumeAvailable = new RetryPolicy()
                    .<Volume>retryIf(v -> !"available".equals(v.getState()))
                    .withDelay(1, TimeUnit.SECONDS)
                    .withMaxDuration(10, TimeUnit.MINUTES);

            Failsafe.with(untilVolumeAvailable).get(() -> ec2.describeVolumes(new DescribeVolumesRequest().withVolumeIds(newVolumeId)).getVolumes().get(0));

            log.info("Detaching old volume " + volumeId + " from " + vmId + "(" + newName + ")");
            ec2.detachVolume(new DetachVolumeRequest(volumeId));

            log.info("Attaching new volume " + newVolumeId + " to instance " + vmId + "(" + newName + ")");
            AttachVolumeRequest attachVolumeRequest = new AttachVolumeRequest(newVolumeId, vmId, blockDeviceMapping.getDeviceName());
            ec2.attachVolume(attachVolumeRequest);

            log.info("Deleting old volume " + volumeId);
            ec2.deleteVolume(new DeleteVolumeRequest(volumeId));

            log.info("Deleting snapshot " + snapshotId);
            ec2.deleteSnapshot(new DeleteSnapshotRequest(snapshotId));
        }

        if (needUpdateInstanceType || needResizeStorage) {
            log.info("Starting AWS instance " + vmId + "(" + newName + ")");
            ec2.startInstances(new StartInstancesRequest().withInstanceIds(vmId));

            RetryPolicy untilInstanceReady = new RetryPolicy()
                    .<Instance>retryIf(i -> !"running".equals(i.getState().getName()) || !Strings.isNullOrEmpty(i.getPrivateIpAddress()))
                    .withDelay(1, TimeUnit.SECONDS)
                    .withMaxDuration(10, TimeUnit.MINUTES);
            Failsafe.with(untilInstanceReady).get(() -> ec2.describeInstances(new DescribeInstancesRequest().withInstanceIds(vmId)).getReservations().get(0).getInstances().get(0));
        }
        return getNode(credentials, nodeId);
    }

    private String getInstanceHostedZone(Instance instance) {
        Optional<Tag> tag = instance.getTags().stream().filter(t -> t.getKey().equals(TAG_HOSTED_ZONE)).findAny();
        return tag.isPresent() ? tag.get().getValue() : null;
    }

    private void setInstanceHostedZone(AmazonEC2 ec2, String vmId, String hostedZone) {
        ec2.createTags(new CreateTagsRequest()
                .withResources(vmId)
                .withTags(new Tag(TAG_HOSTED_ZONE, hostedZone)));
    }

    private ChangeResourceRecordSetsResult createDnsRecord(AmazonRoute53 route53, String ip, String name, String hostedZoneName) {
        log.info("Creating route53 record for " + name);

        HostedZone hostedZone = getHostedZone(hostedZoneName, route53);

        ResourceRecord resourceRecord = new ResourceRecord(ip);
        ResourceRecordSet resourceRecordSet = new ResourceRecordSet(name + "." + hostedZoneName, RRType.A).withTTL
                (60L).withResourceRecords(resourceRecord);
        ChangeBatch changeBatch = new ChangeBatch(Lists.newArrayList(new Change(ChangeAction.CREATE,
                resourceRecordSet)));
        ChangeResourceRecordSetsRequest changeResourceRecordSetsRequest = new ChangeResourceRecordSetsRequest()
                .withHostedZoneId(hostedZone.getId()).withChangeBatch(changeBatch);
        return failsafeChangeResourceRecordSets(route53, changeResourceRecordSetsRequest);
    }

    private ChangeResourceRecordSetsResult failsafeChangeResourceRecordSets(AmazonRoute53 route53, ChangeResourceRecordSetsRequest request) {
        RetryPolicy route53RetryPolicy = new RetryPolicy()
                .retryOn(PriorRequestNotCompleteException.class)
                .withDelay(1, TimeUnit.SECONDS)
                .withMaxDuration(10, TimeUnit.MINUTES);
        return Failsafe.with(route53RetryPolicy).get(() -> route53.changeResourceRecordSets(request));
    }

    private HostedZone getHostedZone(String awsHostedZone, AmazonRoute53 route53) {
        return route53.listHostedZones().getHostedZones().stream()
                .filter(z -> z.getName().equals(awsHostedZone))
                .findAny()
                .orElse(null);
    }

    private void deleteDnsRecord(AmazonRoute53 route53, String name, String hostedZoneName) {
        log.info("Deleting route53 record for " + name);
        if (StringUtils.isEmpty(name)) {
            log.error("Cannot delete route53 record - name is empty.");
            return;
        }
        HostedZone hostedZone = getHostedZone(hostedZoneName, route53);

        String recordSetName = name.toLowerCase() + "." + hostedZoneName;
        ListResourceRecordSetsRequest listResourceRecordSetsRequest =
                new ListResourceRecordSetsRequest(hostedZone.getId()).withStartRecordName(recordSetName);
        List<ResourceRecordSet> resourceRecordSets = route53.listResourceRecordSets
                (listResourceRecordSetsRequest).getResourceRecordSets();
        List<ResourceRecordSet> filtered = resourceRecordSets.stream().filter(f -> recordSetName.equals(f.getName())).collect(Collectors.toList());
        if (filtered.size() < 1) {
            log.warn("Cannot delete route53 record - record not found. Skipping this step.");
            return;
        }
        log.info("Records found:  " + filtered);
        ChangeBatch changeBatch = new ChangeBatch(Lists.newArrayList(new Change(ChangeAction.DELETE,
                filtered.get(0))));
        ChangeResourceRecordSetsRequest changeResourceRecordSetsRequest = new ChangeResourceRecordSetsRequest()
                .withHostedZoneId(hostedZone.getId()).withChangeBatch(changeBatch);
        ChangeResourceRecordSetsResult result = failsafeChangeResourceRecordSets(route53, changeResourceRecordSetsRequest);
        log.info("Submitted delete recordset request " + result.getChangeInfo().toString());
    }

    @Override
    public void deleteNode(Credentials credentials, String nodeId) throws AWS.RegionNotExistException, ExecutionException {
        String accessKey = ((SecretKey) credentials).getName();
        String secretKey = ((SecretKey) credentials).getSecret();
        String region = nodeId.split(":")[0];
        String id = nodeId.split(":")[1];
        AmazonEC2 ec2 = AWS.ec2(accessKey, secretKey, region);
        AmazonRoute53 route53 = AWS.route53(accessKey, secretKey);
        Instance instance = getInstance(ec2, id);
        if (instance == null) {
            log.warn("Instance " + id + " not found - skip deleting instance, route53 record and security group.");
            return;
        }
        String vpcId = instance.getVpcId();
        String name = getInstanceName(instance);
        String hostedZone = getInstanceHostedZone(instance);
        if (!Strings.isNullOrEmpty(hostedZone)) {
            try {
                deleteDnsRecord(route53, name, hostedZone);
            } catch (Exception e) {
                log.warn("Can't delete dns record: " + name + "." + hostedZone, e);
            }
        }

        String groupIdToDelete = getSecurityGroup(ec2, id);
        if (groupIdToDelete != null) {
            log.info("Deassociating security group");
            List<com.amazonaws.services.ec2.model.SecurityGroup> sgs = ec2.describeSecurityGroups(new DescribeSecurityGroupsRequest().withFilters(new Filter("vpc-id").withValues(vpcId))).getSecurityGroups();
            // TODO ? need better impl
            Optional<com.amazonaws.services.ec2.model.SecurityGroup> defaultSGOptional = sgs.stream().filter(sg -> !groupIdToDelete.equals(sg.getGroupId())).findFirst();
            if (defaultSGOptional.isPresent()) {
                com.amazonaws.services.ec2.model.SecurityGroup defaultSG = defaultSGOptional.get();
                log.info("Assigning " + nodeId + " to security group id: " + defaultSG.getGroupId() + " name: " + defaultSG.getGroupName());
                ec2.modifyInstanceAttribute(new ModifyInstanceAttributeRequest().withInstanceId(id).withGroups(defaultSG.getGroupId()));
                log.info("Node " + nodeId + " assigned to security group id: " + defaultSG.getGroupId() + " name: " + defaultSG.getGroupName());
                log.info("Deleting security group " + groupIdToDelete);
                ec2.deleteSecurityGroup(new DeleteSecurityGroupRequest().withGroupId(groupIdToDelete));
                log.info("Security group deleted: " + groupIdToDelete);
            }
        }
        TerminateInstancesRequest terminateInstancesRequest = new TerminateInstancesRequest().withInstanceIds(id);
        TerminateInstancesResult result = ec2.terminateInstances(terminateInstancesRequest);
        log.info("Node terminated: " + id);
    }

    @Override
    public FirewallInfo getNodeFirewallRules(Credentials credentials, String nodeId) throws AWS.RegionNotExistException, ExecutionException {
        String accessKey = ((SecretKey) credentials).getName();
        String secretKey = ((SecretKey) credentials).getSecret();
        String region = nodeId.split(":")[0];
        String id = nodeId.split(":")[1];
        AmazonEC2 ec2 = AWS.ec2(accessKey, secretKey, region);
        String groupId = getSecurityGroup(ec2, id);
        com.amazonaws.services.ec2.model.SecurityGroup sg = ec2.describeSecurityGroups(new DescribeSecurityGroupsRequest().withGroupIds(groupId)).getSecurityGroups().get(0);
        List<IpPermission> permissions = sg.getIpPermissions();
        List<FirewallRule> rules = permissions.stream().flatMap(p -> fromIpPermission(p).stream()).collect(Collectors.toList());
        return new FirewallInfo().rules(rules);
    }

    @Override
    public FirewallInfo updateNodeFirewallRules(Credentials credentials, String nodeId, FirewallUpdate firewallUpdate) throws AWS.RegionNotExistException, ExecutionException {
        String accessKey = ((SecretKey) credentials).getName();
        String secretKey = ((SecretKey) credentials).getSecret();
        String region = nodeId.split(":")[0];
        String id = nodeId.split(":")[1];
        AmazonEC2 ec2 = AWS.ec2(accessKey, secretKey, region);
        String securityGroupId = getSecurityGroup(ec2, id);
        List<IpPermission> permissions = firewallUpdate.getCreate().stream().map(p -> fromFirewallRule(credentials, p)).filter(Objects::nonNull).collect(Collectors.toList());
        List<IpPermission> permissionsToDelete = firewallUpdate.getDelete().stream().map(p -> fromFirewallRule(credentials, p)).filter(Objects::nonNull).collect(Collectors.toList());
        if (!permissions.isEmpty()) {
            AuthorizeSecurityGroupIngressRequest req = new AuthorizeSecurityGroupIngressRequest()
                    .withGroupId(securityGroupId).withIpPermissions(permissions);
            ec2.authorizeSecurityGroupIngress(req);
            log.info("Firewall rules created for node " + nodeId);
            log.info(firewallUpdate.getCreate().toString());
        }

        if (!permissionsToDelete.isEmpty()) {
            RevokeSecurityGroupIngressRequest delete = new RevokeSecurityGroupIngressRequest().withGroupId(securityGroupId).withIpPermissions(permissionsToDelete);
            ec2.revokeSecurityGroupIngress(delete);
            log.info("Firewall rules deleted for node " + nodeId);
            log.info(firewallUpdate.getDelete().toString());
        }
        return getNodeFirewallRules(credentials, nodeId);
    }

    private String getSecurityGroup(AmazonEC2 ec2, String vmId) {
        Reservation reservation = ec2.describeInstances(new DescribeInstancesRequest().withInstanceIds(vmId)).getReservations().get(0);
        Instance instance = reservation.getInstances().get(0);
        Optional<Tag> nameTag = instance.getTags().stream().filter(t -> t.getKey().equals(TAG_NAME)).findFirst();
        if (nameTag.isPresent()) {
            String name = nameTag.get().getValue();
            Optional<GroupIdentifier> group = instance.getSecurityGroups().stream().filter(g -> g.getGroupName().startsWith(name)).findAny();
            if (group.isPresent()) {
                return group.get().getGroupId();
            }
        }
        return null;
    }

    private String createSecurityGroup(AmazonEC2 ec2, String subnet, String name) {
        DescribeSubnetsResult describeSubnetsResult = ec2.describeSubnets(new DescribeSubnetsRequest().withSubnetIds(subnet));
        String vpcId = describeSubnetsResult.getSubnets().get(0).getVpcId();
        CreateSecurityGroupRequest createSecurityGroupRequest = new CreateSecurityGroupRequest(name, "Created by ecloud manager").withVpcId(vpcId);
        CreateSecurityGroupResult createSecurityGroupResult = ec2.createSecurityGroup(createSecurityGroupRequest);
        String securityGroupId = createSecurityGroupResult.getGroupId();
        return securityGroupId;
    }

    private void addSshAcess(AmazonEC2 ec2, String securityGroupId) {
        AuthorizeSecurityGroupIngressRequest sshRule = new AuthorizeSecurityGroupIngressRequest()
                .withGroupId(securityGroupId)
                .withFromPort(22)
                .withToPort(22)
                .withIpProtocol("TCP")
                .withCidrIp(getCidrIp());
        ec2.authorizeSecurityGroupIngress(sshRule);
    }

    private String createNode(AmazonEC2 ec2, int storage, String securityGroupId, String subnet, String image, String instanceType, String keyPair) {
        BlockDeviceMapping mapping = new BlockDeviceMapping().withDeviceName("/dev/sda1").withEbs(new EbsBlockDevice
                ().withDeleteOnTermination(true).withVolumeSize(storage));
        RunInstancesRequest runInstancesRequest = new RunInstancesRequest()
                .withMaxCount(1)
                .withMinCount(1)
                .withSubnetId(subnet)
                .withImageId(image)
                .withInstanceType(instanceType)
                .withKeyName(keyPair)
                .withBlockDeviceMappings(mapping)
                .withSecurityGroupIds(securityGroupId);
        RunInstancesResult runInstancesResult = ec2.runInstances(runInstancesRequest);
        Instance inst = runInstancesResult.getReservation().getInstances().get(0);
        return inst.getInstanceId();
    }

    private enum Parameter {
        name("Node name", false, true, true, null, false, false),
        region("AWS region", true, false, true, null, true, true),
        subnet("AWS subnet", true, false, true, null, true, true, region),
        storage("Storage size", true, true, true, "20", false, false),
        cpu("CPU count", true, true, false, null, false, false),
        memory("memory", true, true, false, null, false, false),
        instance_type("Instance type", true, true, true, null, true, true, cpu, memory),
        keypair("Keypair", true, true, true, null, true, true, region),
        image("AWS image", true, false, true, null, false, true, region),
        hosted_zone("AWS hosted zone", false, true, true, null, true, true),;

        private NodeParameter nodeParameter;

        Parameter(String description, boolean create, boolean configure, boolean required, String defaultValue, boolean canSuggest, boolean strictSuggest, Parameter... args) {
            List<String> argsList = Arrays.stream(args).map(Enum::name).collect(Collectors.toList());
            nodeParameter = new NodeParameter().name(name()).description(description).create(create).configure(configure)
                    .required(required).defaultValue(defaultValue)
                    .canSuggest(canSuggest).strictSuggest(strictSuggest).args(argsList);
        }

        public NodeParameter getNodeParameter() {
            return nodeParameter;
        }

    }

}


text:
require 'test/unit'
require 'test/helper'

class TC_Dir_Seek_InstanceMethod < Test::Unit::TestCase
   include Test::Helper

   def setup
      @pwd = pwd_n
      @dir = Dir.new(@pwd)
   end

   def test_seek_basic
      assert_respond_to(@dir, :seek)
      assert_nothing_raised{ @dir.seek(0) }
   end

   # This caused a segfault on Windows for some versions of Ruby.
   def test_seek
      assert_equal(".", @dir.read)
      assert_kind_of(Dir, @dir.seek(0))
   end

   def test_seek_expected_errors
      assert_raises(TypeError){ @dir.seek("bogus") }
      assert_raises(ArgumentError){ @dir.seek(0,0) }
   end

   def teardown
      @pwd = nil
      @dir = nil
   end
end

redpajama_set_name: RedPajamaGithub


text:
use Trait::foo;
//~^ ERROR `foo` is not directly importable
use Trait::Assoc;
//~^ ERROR `Assoc` is not directly importable
use Trait::C;
//~^ ERROR `C` is not directly importable

use Foo::new;
//~^ ERROR unresolved import `Foo` [E0432]
//~| not a module `Foo`

use Foo::C2;
//~^ ERROR unresolved import `Foo` [E0432]
//~| not a module `Foo`

pub trait Trait {
    fn foo();
    type Assoc;
    const C: u32;
}

struct Foo;

impl Foo {
    fn new() {}
    const C2: u32 = 0;
}

fn main() {}


text:
[`ITF-ultrasound_data`]()

## Title
Ultrasound Data format

## About
Data are available for the algorithm processes from 3 incoming ways

- either they are transmitted through `TCP` protocol
- either they are transmitted through `UDP` protocol
- either they are transmitted from a `CSV` file from inside the app

At the current state, data are transmitted image by image. So that, each time an int array of
`number_of_pixels_of_an_image` is filled, then it is transmitted to the algorithms pipeline, here : [`MDL-algo_scanconversion.md`]()

## Format
* First part is classical CSV __note that the first item contains the position of the line, in arbitrary unit__
* Second part is comments (starting with #).

In comments can be found meta data, storin for example the following items

* `#timestamp:XX` for the timestamp when the image was taken
* `#lines:XX` contains the number of lines in the image
* `#length:XX` contains the number of pixels per line
* `#frequency:XX` is the frequency of acquisition
* `#piezofrequency:XX` is the piezo frequency
* `#angle:XX` is the angle of the acquisition
* `#anglestep:XX` is the multiplicator between the first item of each line to obtain the position in degree.

For more informations, we refer the reader to this wiki ![page](http://wiki.echopen.org/index.php/Challenge:_Data_format#Solution:_Common_rules_for_structuring_raw_data)

## Description
Data are available for the algorithm processes from 3 incoming ways

- either they are transmitted through `TCP` protocol
- either they are transmitted through `UDP` protocol
- either they are transmitted from a `CSV` file from inside the app

At the current state, data are transmitted image by image. So that, each time an int array of
`number_of_pixels_of_an_image` is filled, then it is transmitted to the algorithms pipeline, here : [`MDL-algo_scanconversion.md`]()


text:
All notable changes to this project will be documented in this file.

## [2.1.3] - 2022-11-17

# Changed
- fixed getServiceStatistics and getDistinctServiceStatistics and getListOfServices so that the number of services changes in time as it should

## [2.1.2] - 2022-11-15

# Changed
- fixed getServiceStatistics and getDistinctServiceStatistics so that the number of services changes in time as it should

## [2.1.1] - 2022-10-19

# Added
- added unit tests to improve test coverage

## [2.1.0] - 2022-10-05

# Added
- added new REST endpoint for listing REST endpoints for basic REST(without OPENAPI) service
- added new REST endpoint for listing REST endpoints for REST(also OPENAPI) service

# Changed
- Updated related documentation and schematics

## [2.0.1] - 2022-09-20

# Changed
- fix of major and critical Sonar issues

## [2.0.0] - 2022-08-02

# Changed
- major refacto of REST apis and SOAP endpoints to have start and end date in parameters instead of just one date(since)
- cleanup of all related code

# Added
- improvement of test coverage by adding new unit tests

## [1.4.1] - 2022-03-18

# Changed
- minor updates to listDescriptors and getOrganizations endpoints

## [1.4.0] - 2022-02-08

# Added
- added new REST endpoint for listing organization/company information
- added new REST endpoint for listing organization/company information changes

## [1.3.2] - 2021-12-15

# Changed
- update listErrors endpoint to allow null values in parameters

## [1.3.1] - 2021-12-06

# Changed
- increased max Java heap size
- set the collector to fetch only at certain time interval during the night

## [1.3.0] - 2021-10-27

# Added
- added new endpoint for listing security servers
- added new endpoint for listing descriptors (data about subsystems)

# Changed
- updated listErrors endpoint to support requests for organizations not only subsystems and added support for pagination

## [1.2.2] - 2021-09-22

### Changed
- updated heartbeat endpoint to return also information about last collection dates

## [1.2.1] - 2021-09-03

### Added
- new endpoint for listing errors for a given subsystem

## [1.2.0] - 2021-08-26

### Added
- new endpoint for distinct services statistics

## [1.1.3] - 2021-07-23

### Added
- added user guide

### Changed
- reduced logging on INFO level

## [1.1.2] - 2021-02-22

### Changed
- fixed GetService in CatalogService to also work when service_version is not null but empty

## [1.1.0] - 2021-02-09

### Changed
- fixed XRoad-Catalog Collector properties files access rights
- updated license in file headers
- substituted IsSoapService and IsRestService with GetServiceType
- refacto of GetServiceStatistics and GetServiceStatisticsCSV related to having REST services instead of OTHER services
- updated documentation

## [1.0.11] - 2020-12-07

### Changed
- fixed GetErrorLog response to have information about security server, xRoadInstance etc

## [1.0.9] - 2020-11-24

### Changed
- new field to GetListOfServices response to show XRoadInstance

## [1.0.7] - 2020-11-23

### Changed
- Changed username of xroad-catalog-collector and xroad-catalog-lister services

## [1.0.4] - 2020-11-23

### Added
- bash script to automate update of project version

### Changed
- new field to GetServiceStatistics response to show number of services, which are neither REST or Soap
- refacto error_log to have additional fields like xRoadInstance, memberClass, memberCode etc.
- update changelogs and documentation

## [1.0.3] - 2020-10-29

### Changed
- update changelogs and build version
- Unignore fetchCompanies integration test due to service unavailable

## [1.0.2] - 2020-10-29

### Changed
- update changelogs and build version
- Ignore fetchCompanies integration test due to service unavailable

## [1.0.1] - 2020-10-25

### Added
- OpenAPI description for REST endpoints

### Changed
- new endpoint for statistics
- refactored getServiceStatistics endpoint from POST to GET
- refactored getServiceStatisticsCSV endpoint from POST to GET
- refactored getListOfServices endpoint from POST to GET
- refactored getListOfServicesCSV endpoint from POST to GET
- update changelogs and documentation

## [1.0.0] - 2020-10-06

### Changed
- new endpoint for statistics
- new endpoint for statistics in CSV format
- new endpoint for members with related subsystems, services and security servers
- new endpoint for members with related subsystems, services and security servers in CSV format
- update license and file headers
- update changelogs and documentation

## [0.13.6] - 2020-08-27

### Changed
- new endpoint for errors
- update changelogs and documentation

## [0.13.5] - 2020-08-19

### Changed
- update version
- update changelogs and documentation

## [0.11.9] - 2020-06-18

### Changed
- update version
- Fix GetService returning duplicates

## [0.11.8] - 2020-06-11

### Changed
- update version

## [0.11.7] - 2020-06-09

### Changed
- update version
- Add serviceVersion as input parameter to IsSoapService and IsRestService

## [0.11.6] - 2020-06-03

### Changed
- update version
- add multiple languages support to GetOrganizations and GetCompanies

## [0.11.5] - 2020-05-20

### Changed
- update version
- fix NPE for hasCompanyChanged and hasOrganizationChanged

## [0.11.4] - 2020-05-13

### Changed
- update version

## [0.11.3] - 2020-05-07

### Changed
- log errors in OrganizationUtil
- update documentation

## [0.11.3] - 2020-05-06

### Changed
- small refacto and example request files
- unit tests for new repositories and catalog services

## [0.11.3] - 2020-05-05

### Changed
- create unit test for new actor and small refacto
- fetch companies on certain time set with parameters

## [0.11.2] - 2020-05-04

### Changed
- update version
- refacto ListMethodsActor
- save company details to db

## [0.11.2] - 2020-05-01

### Added
- save company and related details to db

## [0.11.1] - 2020-04-30

### Changed
- do not fetch organizations per each customer

## [0.11.1] - 2020-04-23

### Added
- unit test for new endpoints

## [0.11.1] - 2020-04-21

### Changed
- remove hard-coded url

## [0.11.1] - 2020-04-17

### Changed
- finalize collection of organizational data to db

## [0.11.1] - 2020-04-15

### Changed
- refacto CatalogService, FetchOrganizationUtil and sql script

## [0.11.1] - 2020-04-14

### Added
- save organizationAddress related data to db
- logic for fetching and saving organizations

## [0.11.1] - 2020-04-10

### Added
- new entities and repositories for fetching organizations

## [0.11.0] - 2020-03-26

### Changed
- fix host name issue

## [0.11.0] - 2020-03-23

### Changed
- update documentation

## [0.11.0] - 2020-03-20

### Changed
refacto IsSoapProvider, IsRestProvider and getOpenAPI
refacto ListMethodsActor to also receive REST services

## [0.10.0] - 2020-03-17

### Changed
- update project version
- revert dependencies update

### Added
- add changelogs
- create getOpenApi metaservice support

## [0.10.0] - 2020-02-14

### Added

- ClientListUtil class for ListClientsActor to handle XRoad v622 response correctly

### Changed

- Update ListClientsActor to handle XRoad v622 response correctly
- Update related unit tests
- Use POST for getWSDL request
- Add timeouts to XroadClient
- Update project version
- Update metaservices wsdl. Use WSDL defined in metaservices protocol documentation
- Update dependencies
- update documentation. change xroad-catalog-lister build user jenkins -> root

## [0.9.1.1] - 2017-07-05

### Changed

- Increase catalog collector package version

## [0.9.0.1] - 2017-02-08

### Added

- JavaDoc for parameters
- SSL SocketFactory for request context
- common gradle root build
- out/ and .gradle  to .gitignore
- pool SupervisorStrategies
- dead letters logging
- read and connect timeout to RestOperations

### Changed

- project dependencies from jar dependencies to module dependencies
- changed list-clients.xsd so that generated jaxb entities (ListClients) are XmlRootElement,
  and xml will be validated when unmarshalling. This adds detection of such errors when listClients returns
  non-valid message (eg. configuration outdated) and prevents problem with bad changed-timestamps. added tests.
- improved logging
- moved example messages to src/main/doc
- Read ssl system properties from conf file
- actorSelection is no longer used for fetching actor-actor ActorRefs
- longer timeout in ApplicationConfiguration is ok since we just want to prevent complete lockups (and not hide timeouts from xroad)

### Fixed

- SSL parameter names in conf files
- fix for a task by adding if clause to FetchWsdlActor buildUri method
- incorrect ListMethods query


text:
'use strict';

var _ = require('lodash'),
    mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    ObjectId = Schema.ObjectId,
    md5 = require('md5');

module.exports = TasksSync;

function TasksSync(app, io) {

    var validIndexes = {
        _id: true,
        datecreate:true,
        datefrom:true,
        dateuntil:true,
        owner:true,
        member:true,
        priority: true,
        content:true
    };

    io.sockets.on('connection', function(socket) {

        socket.on('tasks:read', function(data, callback) {
            var attributes = data;

            var criteria = {};
            _.filter(attributes, function(v, i) {
                if (validIndexes[i]) criteria[i] = v;
            })

            app.db.model('Tasks').find(criteria)
                .exec(function(err, docs) {
                    if (err) {
                        callback(err);
                        return;
                    }
                    callback(null, docs);
                });
        });

        socket.on('tasks:update', function(data, callback) {
            var attributes = data.attrs;

            _.filter(attributes, function(v, i) {
                if (_.isUndefined(validIndexes[i])) delete attributes[i];
            });

            app.db.model('Tasks').findById(attributes, function(err, doc) {

                if (err) {
                    console.log('ERR: ~~~');
                    console.log(err);
                    console.log('END: ~~~');
                }

                if (doc) {

                    _.forEach(attributes, function(n, key) {
                        try {

                            doc[key] = n;

                        } catch (ex) {
                            console.log('EX: ~~~ 80');
                            console.log(ex);
                            console.log('END: ~~~');
                        }
                    });

                    console.log(doc);

                    doc.save(function(err) {
                        if (err) {
                            console.log('ERR: ~~~ 88');
                            console.log(err);
                            console.log('END: ~~~');
                        } else {
                            console.log('SAVED CREATE: ~~~');
                            console.log(doc);
                            callback(null, doc);
                        }

                    });

                }

            });
        });

        socket.on('tasks:create', function(data, callback) {
            var attributes = data.attrs;

            var doc = app.db.model('Tasks');
            doc = new doc(attributes);
            doc.save();
            callback(null, doc);
        });

        socket.on('tasks:delete', function(data, callback) {
            var attributes = data.attrs;

            if (err) {
                console.log('ERR: ~~~');
                console.log(err);
                console.log('END: ~~~');
            }

            app.db.model('Tasks').findById(attributes, function(err, doc) {
                if(doc) {
                    doc.remove();
                    callback(null, doc);
                }

            });
        });

    });
}


text:
tags: fun
layout: post
title: "'Election officials describe it as an isolated inc"
---

This Modern World from this week, good stuff. Along the same vein, War Cry from Ted Rall rings a few bells.


text:
define(['angular', './module', './controllers', './partials'], function (ng, module, controllers, partials) {
    'use strict';

    /**
     * And of course we define a controller for our route.
     */
    module.config(function config( $stateProvider ) {
        $stateProvider.state( 'about', {
            url: '/about',
            views: {
                "main": {
                    controller: 'AboutCtrl',
                    templateUrl: './src/app/about/about.tpl.html'
                }
            },
            data:{ pageTitle: 'What is It?' }
        });
    });

});


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

<TITLE>
com.amazonaws.services.sns (AWS SDK for Java - 1.4.7)
</TITLE>

<META NAME="date" CONTENT="2013-06-18">

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

com.amazonaws.services.sns

Interfaces

AmazonSNS

AmazonSNSAsync

Classes

AmazonSNSAsyncClient

AmazonSNSClient


text:
import logging

from pants.backend.jvm.targets.jvm_app import JvmApp
from pants.backend.jvm.targets.jvm_binary import JvmBinary
from pants.backend.jvm.tasks.jvm_task import JvmTask
from pants.base.exceptions import TaskError
from pants.base.workunit import WorkUnitLabel
from pants.fs.fs import expand_path
from pants.java.executor import CommandLineGrabber
from pants.util.dirutil import safe_open

_CWD_NOT_PRESENT = "CWD NOT PRESENT"
logger = logging.getLogger(__name__)

def is_binary(target):
    return isinstance(target, JvmBinary)

class JvmRun(JvmTask):
    @classmethod
    def register_options(cls, register):
        super().register_options(register)
        register(
            "--only-write-cmd-line",
            metavar="<file>",
            help="Instead of running, just write the cmd line to this file.",
        )
        # Note the use of implicit_value. This is so we can support three cases:
        # --cwd=<path>
        # --cwd (uses the implicit value)
        # No explicit --cwd at all (uses the default)
        register(
            "--cwd",
            default=_CWD_NOT_PRESENT,
            implicit_value="",
            help="Set the working directory. If no argument is passed, use the target path.",
        )
        register(
            "--main",
            metavar="<main class>",
            help='Invoke this class (overrides "main"" attribute in jvm_binary targets)',
        )

    @classmethod
    def supports_passthru_args(cls):
        return True

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.only_write_cmd_line = self.get_options().only_write_cmd_line
        self.args.extend(self.get_passthru_args())
        self.args.extend(self.get_options().args)

    def execute(self):
        # The called binary may block for a while, allow concurrent pants activity during this pants
        # idle period.
        #
        # TODO(John Sirois): refactor lock so that I can do:
        # with self.context.lock.yield():
        #   - blocking code
        #
        # Currently re-acquiring the lock requires a path argument that was set up by the goal
        # execution engine.  I do not want task code to learn the lock location.
        # http://jira.local.twitter.com/browse/AWESOME-1317
        target = self.require_single_root_target()

        working_dir = None
        cwd_opt = self.get_options().cwd
        if cwd_opt != _CWD_NOT_PRESENT:
            working_dir = self.get_options().cwd
            if not working_dir:
                working_dir = target.address.spec_path
        logger.debug(f"Working dir is {working_dir}")

        if isinstance(target, JvmApp):
            binary = target.binary
        else:
            binary = target

        # This task is installed in the "run" goal.
        # This means that, when invoked with ./pants run, it will run regardless of whether
        # the target is a jvm target.
        # As a result, not all targets passed here will have good defaults for extra_jvm_options
        extra_jvm_options = binary.payload.get_field_value("extra_jvm_options", [])

        # We can't throw if binary isn't a JvmBinary, because perhaps we were called on a
        # python_binary, in which case we have to no-op and let python_run do its thing.
        # TODO(benjy): Some more elegant way to coordinate how tasks claim targets.
        if isinstance(binary, JvmBinary):
            platform = binary.runtime_platform
            jvm = self.preferred_jvm_distribution([platform])
            executor = CommandLineGrabber(jvm) if self.only_write_cmd_line else None
            self.context.release_lock()
            with self.context.new_workunit(name="run", labels=[WorkUnitLabel.RUN]):
                result = jvm.execute_java(
                    classpath=self.classpath([target]),
                    main=self.get_options().main or binary.main,
                    executor=executor,
                    jvm_options=(
                        self.jvm_options + list(platform.jvm_options) + list(extra_jvm_options)
                    ),
                    args=self.args,
                    cwd=working_dir,
                    synthetic_jar_dir=self.workdir,
                    create_synthetic_jar=self.synthetic_classpath,
                )

            if self.only_write_cmd_line:
                with safe_open(expand_path(self.only_write_cmd_line), "w") as outfile:
                    outfile.write(" ".join(executor.cmd))
            elif result != 0:
                raise TaskError(
                    f"java {binary.main} ... exited non-zero ({result})", exit_code=result
                )


text:
<?php

use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\RequestContext;

/**
 * appProdUrlMatcher
 *
 * This class has been auto-generated
 * by the Symfony Routing Component.
 */
class appProdUrlMatcher extends Symfony\Bundle\FrameworkBundle\Routing\RedirectableUrlMatcher
{
    /**
     * Constructor.
     */
    public function __construct(RequestContext $context)
    {
        $this->context = $context;
    }

    public function match($pathinfo)
    {
        $allow = array();
        $pathinfo = rawurldecode($pathinfo);
        $context = $this->context;
        $request = $this->request;

        if (0 === strpos($pathinfo, '/log')) {
            if (0 === strpos($pathinfo, '/login')) {
                // fos_user_security_login
                if ($pathinfo === '/login') {
                    return array (  '_controller' => 'FOS\\UserBundle\\Controller\\SecurityController::loginAction',  '_route' => 'fos_user_security_login',);
                }

                // fos_user_security_check
                if ($pathinfo === '/login_check') {
                    if ($this->context->getMethod() != 'POST') {
                        $allow[] = 'POST';
                        goto not_fos_user_security_check;
                    }

                    return array (  '_controller' => 'FOS\\UserBundle\\Controller\\SecurityController::checkAction',  '_route' => 'fos_user_security_check',);
                }
                not_fos_user_security_check:

            }

            // fos_user_security_logout
            if ($pathinfo === '/logout') {
                return array (  '_controller' => 'FOS\\UserBundle\\Controller\\SecurityController::logoutAction',  '_route' => 'fos_user_security_logout',);
            }

        }

        if (0 === strpos($pathinfo, '/profile')) {
            // fos_user_profile_show
            if (rtrim($pathinfo, '/') === '/profile') {
                if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
                    $allow = array_merge($allow, array('GET', 'HEAD'));
                    goto not_fos_user_profile_show;
                }

                if (substr($pathinfo, -1) !== '/') {
                    return $this->redirect($pathinfo.'/', 'fos_user_profile_show');
                }

                return array (  '_controller' => 'FOS\\UserBundle\\Controller\\ProfileController::showAction',  '_route' => 'fos_user_profile_show',);
            }
            not_fos_user_profile_show:

            // fos_user_profile_edit
            if ($pathinfo === '/profile/edit') {
                return array (  '_controller' => 'FOS\\UserBundle\\Controller\\ProfileController::editAction',  '_route' => 'fos_user_profile_edit',);
            }

        }

        if (0 === strpos($pathinfo, '/re')) {
            if (0 === strpos($pathinfo, '/register')) {
                // fos_user_registration_register
                if (rtrim($pathinfo, '/') === '/register') {
                    if (substr($pathinfo, -1) !== '/') {
                        return $this->redirect($pathinfo.'/', 'fos_user_registration_register');
                    }

                    return array (  '_controller' => 'FOS\\UserBundle\\Controller\\RegistrationController::registerAction',  '_route' => 'fos_user_registration_register',);
                }

                if (0 === strpos($pathinfo, '/register/c')) {
                    // fos_user_registration_check_email
                    if ($pathinfo === '/register/check-email') {
                        if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
                            $allow = array_merge($allow, array('GET', 'HEAD'));
                            goto not_fos_user_registration_check_email;
                        }

                        return array (  '_controller' => 'FOS\\UserBundle\\Controller\\RegistrationController::checkEmailAction',  '_route' => 'fos_user_registration_check_email',);
                    }
                    not_fos_user_registration_check_email:

                    if (0 === strpos($pathinfo, '/register/confirm')) {
                        // fos_user_registration_confirm
                        if (preg_match('#^/register/confirm/(?P<token>[^/]++)$#s', $pathinfo, $matches)) {
                            if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
                                $allow = array_merge($allow, array('GET', 'HEAD'));
                                goto not_fos_user_registration_confirm;
                            }

                            return $this->mergeDefaults(array_replace($matches, array('_route' => 'fos_user_registration_confirm')), array (  '_controller' => 'FOS\\UserBundle\\Controller\\RegistrationController::confirmAction',));
                        }
                        not_fos_user_registration_confirm:

                        // fos_user_registration_confirmed
                        if ($pathinfo === '/register/confirmed') {
                            if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
                                $allow = array_merge($allow, array('GET', 'HEAD'));
                                goto not_fos_user_registration_confirmed;
                            }

                            return array (  '_controller' => 'FOS\\UserBundle\\Controller\\RegistrationController::confirmedAction',  '_route' => 'fos_user_registration_confirmed',);
                        }
                        not_fos_user_registration_confirmed:

                    }

                }

            }

            if (0 === strpos($pathinfo, '/resetting')) {
                // fos_user_resetting_request
                if ($pathinfo === '/resetting/request') {
                    if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
                        $allow = array_merge($allow, array('GET', 'HEAD'));
                        goto not_fos_user_resetting_request;
                    }

                    return array (  '_controller' => 'FOS\\UserBundle\\Controller\\ResettingController::requestAction',  '_route' => 'fos_user_resetting_request',);
                }
                not_fos_user_resetting_request:

                // fos_user_resetting_send_email
                if ($pathinfo === '/resetting/send-email') {
                    if ($this->context->getMethod() != 'POST') {
                        $allow[] = 'POST';
                        goto not_fos_user_resetting_send_email;
                    }

                    return array (  '_controller' => 'FOS\\UserBundle\\Controller\\ResettingController::sendEmailAction',  '_route' => 'fos_user_resetting_send_email',);
                }
                not_fos_user_resetting_send_email:

                // fos_user_resetting_check_email
                if ($pathinfo === '/resetting/check-email') {
                    if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
                        $allow = array_merge($allow, array('GET', 'HEAD'));
                        goto not_fos_user_resetting_check_email;
                    }

                    return array (  '_controller' => 'FOS\\UserBundle\\Controller\\ResettingController::checkEmailAction',  '_route' => 'fos_user_resetting_check_email',);
                }
                not_fos_user_resetting_check_email:

                // fos_user_resetting_reset
                if (0 === strpos($pathinfo, '/resetting/reset') && preg_match('#^/resetting/reset/(?P<token>[^/]++)$#s', $pathinfo, $matches)) {
                    if (!in_array($this->context->getMethod(), array('GET', 'POST', 'HEAD'))) {
                        $allow = array_merge($allow, array('GET', 'POST', 'HEAD'));
                        goto not_fos_user_resetting_reset;
                    }

                    return $this->mergeDefaults(array_replace($matches, array('_route' => 'fos_user_resetting_reset')), array (  '_controller' => 'FOS\\UserBundle\\Controller\\ResettingController::resetAction',));
                }
                not_fos_user_resetting_reset:

            }

        }

        // fos_user_change_password
        if ($pathinfo === '/profile/change-password') {
            if (!in_array($this->context->getMethod(), array('GET', 'POST', 'HEAD'))) {
                $allow = array_merge($allow, array('GET', 'POST', 'HEAD'));
                goto not_fos_user_change_password;
            }

            return array (  '_controller' => 'FOS\\UserBundle\\Controller\\ChangePasswordController::changePasswordAction',  '_route' => 'fos_user_change_password',);
        }
        not_fos_user_change_password:

        // moovi_moovi_homepage
        if (rtrim($pathinfo, '/') === '') {
            if (substr($pathinfo, -1) !== '/') {
                return $this->redirect($pathinfo.'/', 'moovi_moovi_homepage');
            }

            return array (  '_controller' => 'moovi\\MooviBundle\\Controller\\WelcomeController::indexAction',  '_route' => 'moovi_moovi_homepage',);
        }

        if (0 === strpos($pathinfo, '/m')) {
            if (0 === strpos($pathinfo, '/movies')) {
                // moovi_movie_list
                if ($pathinfo === '/movies') {
                    return array (  '_controller' => 'moovi\\MooviBundle\\Controller\\MovieController::indexAction',  '_route' => 'moovi_movie_list',);
                }

                // moovi_movie_add
                if ($pathinfo === '/movies/add') {
                    return array (  '_controller' => 'moovi\\MooviBundle\\Controller\\MovieController::addAction',  '_route' => 'moovi_movie_add',);
                }

                // moovi_movie_display
                if (preg_match('#^/movies/(?P<slug>[^/]++)$#s', $pathinfo, $matches)) {
                    return $this->mergeDefaults(array_replace($matches, array('_route' => 'moovi_movie_display')), array (  '_controller' => 'moovi\\MooviBundle\\Controller\\MovieController::displayAction',));
                }

                // moovi_movie_order
                if (preg_match('#^/movies/(?P<slug>[^/]++)/order$#s', $pathinfo, $matches)) {
                    return $this->mergeDefaults(array_replace($matches, array('_route' => 'moovi_movie_order')), array (  '_controller' => 'moovi\\MooviBundle\\Controller\\MovieController::orderAction',));
                }

            }

            // moovi_user_orders
            if ($pathinfo === '/my-orders') {
                return array (  '_controller' => 'moovi\\MooviBundle\\Controller\\UserController::ordersAction',  '_route' => 'moovi_user_orders',);
            }

            // moovi_movie_add_coment
            if (0 === strpos($pathinfo, '/movies') && preg_match('#^/movies/(?P<slug>[^/]++)/new\\-comment$#s', $pathinfo, $matches)) {
                return $this->mergeDefaults(array_replace($matches, array('_route' => 'moovi_movie_add_coment')), array (  '_controller' => 'mooviMooviBundle:Comment:add',));
            }

        }

        // moovi_actor_add
        if ($pathinfo === '/actors/add') {
            return array (  '_controller' => 'moovi\\MooviBundle\\Controller\\ActorController::addAction',  '_route' => 'moovi_actor_add',);
        }

        // moovi_genre_add
        if ($pathinfo === '/genres/add') {
            return array (  '_controller' => 'moovi\\MooviBundle\\Controller\\GenreController::addAction',  '_route' => 'moovi_genre_add',);
        }

        throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();
    }
}


text:
module.exports = function(client) {
    /**
     * Connection established, sending informations to server.
     *
     * @event logon
     */
    client.addListener('logon', function () {
        // Do your stuff.
    });
};


text:
$_L(["$wt.internal.SWTEventListener"],"$wt.custom.TextChangeListener",null,function(){
$_I($wt.custom,"TextChangeListener",$wt.internal.SWTEventListener);
});


text:
from flask import (
    Blueprint,
    redirect,
    request,
    flash,
    url_for,
    render_template)
from flask_login import (
    login_required,
    login_user,
    current_user,
    logout_user)
from flask_babel import gettext as _

from catwatch.lib.safe_next_url import safe_next_url
from catwatch.blueprints.user.decorators import anonymous_required
from catwatch.blueprints.user.models import User
from catwatch.blueprints.user.forms import (
    LoginForm,
    BeginPasswordResetForm,
    PasswordResetForm,
    SignupForm,
    WelcomeForm,
    UpdateCredentials,
    UpdateLocale)

user = Blueprint('user', __name__, template_folder='templates')

@user.route('/login', methods=['GET', 'POST'])
@anonymous_required()
def login():
    form = LoginForm(next=request.args.get('next'))

    if form.validate_on_submit():
        u = User.find_by_identity(request.form.get('identity'))

        if u and u.authenticated(password=request.form.get('password')):
            # As you can see remember me is always enabled, this was a design
            # decision I made because more often than not users want this
            # enabled. This allows for a less complicated login form.
            #
            # If however you want them to be able to select whether or not they
            # should remain logged in then perform the following 3 steps:
            # 1) Replace 'True' below with: request.form.get('remember', False)
            # 2) Uncomment the 'remember' field in user/forms.py#LoginForm
            # 3) Add a checkbox to the login form with the id/name 'remember'
            if login_user(u, remember=True):
                u.update_activity_tracking(request.remote_addr)

                # Handle optionally redirecting to the next URL safely.
                next_url = request.form.get('next')
                if next_url:
                    return redirect(safe_next_url(next_url))

                return redirect(url_for('user.settings'))
            else:
                flash(_('This account has been disabled.'), 'error')
        else:
            flash(_('Identity or password is incorrect.'), 'error')

    return render_template('user/login.jinja2', form=form)

@user.route('/logout')
@login_required
def logout():
    logout_user()
    flash(_('You have been logged out.'), 'success')
    return redirect(url_for('user.login'))

@user.route('/account/begin_password_reset', methods=['GET', 'POST'])
@anonymous_required()
def begin_password_reset():
    form = BeginPasswordResetForm()

    if form.validate_on_submit():
        u = User.initialize_password_reset(request.form.get('identity'))

        flash(_('An email has been sent to %(email)s.',
                email=u.email), 'success')
        return redirect(url_for('user.login'))

    return render_template('user/begin_password_reset.jinja2', form=form)

@user.route('/account/password_reset', methods=['GET', 'POST'])
@anonymous_required()
def password_reset():
    form = PasswordResetForm(reset_token=request.args.get('reset_token'))

    if form.validate_on_submit():
        u = User.deserialize_token(request.form.get('reset_token'))

        if u is None:
            flash(_('Your reset token has expired or was tampered with.'),
                  'error')
            return redirect(url_for('user.begin_password_reset'))

        form.populate_obj(u)
        u.password = User.encrypt_password(request.form.get('password', None))
        u.save()

        if login_user(u):
            flash(_('Your password has been reset.'), 'success')
            return redirect(url_for('user.settings'))

    return render_template('user/password_reset.jinja2', form=form)

@user.route('/signup', methods=['GET', 'POST'])
@anonymous_required()
def signup():
    form = SignupForm()

    if form.validate_on_submit():
        u = User()

        form.populate_obj(u)
        u.password = User.encrypt_password(request.form.get('password', None))
        u.save()

        if login_user(u):
            flash(_('Awesome, thanks for signing up!'), 'success')
            return redirect(url_for('user.welcome'))

    return render_template('user/signup.jinja2', form=form)

@user.route('/welcome', methods=['GET', 'POST'])
@login_required
def welcome():
    if current_user.username:
        flash(_('You already picked a username.'), 'warning')
        return redirect(url_for('user.settings'))

    form = WelcomeForm()

    if form.validate_on_submit():
        current_user.username = request.form.get('username')
        current_user.save()

        flash(_('Sign up is complete, enjoy our services.'), 'success')
        return redirect(url_for('billing.pricing'))

    return render_template('user/welcome.jinja2', form=form)

@user.route('/settings')
@login_required
def settings():
    return render_template('user/settings.jinja2')

@user.route('/settings/update_credentials', methods=['GET', 'POST'])
@login_required
def update_credentials():
    form = UpdateCredentials(current_user, uid=current_user.id)

    if form.validate_on_submit():
        # We cannot form.populate_obj() because the password is optional.
        new_password = request.form.get('password', '')
        current_user.email = request.form.get('email')

        if new_password:
            current_user.password = User.encrypt_password(new_password)

        current_user.save()

        flash(_('Your sign in settings have been updated.'), 'success')
        return redirect(url_for('user.settings'))

    return render_template('user/update_credentials.jinja2', form=form)

@user.route('/settings/update_locale', methods=['GET', 'POST'])
@login_required
def update_locale():
    form = UpdateLocale(locale=current_user.locale)

    if form.validate_on_submit():
        form.populate_obj(current_user)
        current_user.save()

        flash(_('Your locale settings have been updated.'), 'success')
        return redirect(url_for('user.settings'))

    return render_template('user/update_locale.jinja2', form=form)


text:
'use strict';

var React = require('react');
var ReactBootstrap = require('react-bootstrap');

var ToolbarBreadcrumbsStore = require('../../store/toolbar/ToolbarBreadcrumbsStore.js');
var ToolbarBreadcrumbsActions = require('../../action/toolbar/ToolbarBreadcrumbsActions.js');
var DeskPageFrameActions = require('../../action/desk/DeskPageFrameActions.js');

var ToolbarBreadcrumbs = React.createClass({

    _handleClickCrumb: function(e){
        e.stopPropagation();
        e.preventDefault();
        DeskPageFrameActions.deselectComponent();
        DeskPageFrameActions.selectComponentById(e.currentTarget.attributes['data-umyid'].value);
    },

    getInitialState: function () {
        return ToolbarBreadcrumbsStore.getModel();
    },

    onModelChange: function (model) {
        this.setState(model);
    },

    componentDidMount: function () {
        this.unsubscribe = ToolbarBreadcrumbsStore.listen(this.onModelChange);
    },

    componentDidUpdate: function(){
    },

    componentWillUnmount: function () {
        this.unsubscribe();
    },

    render: function() {
        var crumbs = [];
        this.state.crumbs.map(function(item, index){
            crumbs.push(

{item.type}

            );
        }.bind(this));
        var children = [];
        this.state.activeChildren.map(function(child, index){
            children.push(

{child.type}

            )
        }.bind(this));
        var dropdownClassName = crumbs.length >= 3 ? ' dropdown-menu-right' : '';
        if(children.length > 0){
            crumbs.push(

                        {this.state.active}

                        {children}

            );
        } else {
            crumbs.push(

{this.state.active}

            );
        }
        return (

                    {crumbs}

        );
    }

});

module.exports = ToolbarBreadcrumbs;


text:
ConsolidateSQLFiles
=======

Consolidate all *.sql files from a folder into one new file.

Windows console executable program.  How to compile and use the program notes are included at the top of the source code.


text:
package pl.gda.pg.student.project.packets.movement;

import pl.gda.pg.student.project.kryonetcommon.Registerable;

@Registerable
public class ObjectMoveLeftPacket
{
    public long id;
}


text:
package org.kie.dmn.feel.lang.ast;

import org.antlr.v4.runtime.ParserRuleContext;
import org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity;
import org.kie.dmn.feel.lang.CompositeType;
import org.kie.dmn.feel.lang.EvaluationContext;
import org.kie.dmn.feel.lang.Type;
import org.kie.dmn.feel.lang.types.BuiltInType;
import org.kie.dmn.feel.util.EvalHelper;
import org.kie.dmn.feel.util.Msg;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;

public class PathExpressionNode
        extends BaseNode {

    private BaseNode expression;
    private BaseNode name;

    public PathExpressionNode(ParserRuleContext ctx, BaseNode expression, BaseNode name) {
        super( ctx );
        this.expression = expression;
        this.name = name;
    }

    public BaseNode getExpression() {
        return expression;
    }

    public void setExpression(BaseNode expression) {
        this.expression = expression;
    }

    public BaseNode getName() {
        return name;
    }

    public void setName(BaseNode name) {
        this.name = name;
    }

    @Override
    public Object evaluate(EvaluationContext ctx) {
        try {
            Object o = this.expression.evaluate( ctx );
            if ( o instanceof List ) {
                List list = (List) o;
                // list of contexts/elements as defined in the spec, page 114
                List results = new ArrayList();
                for( Object element : list ) {
                    Object r = fetchValue( element );
                    if( r != null ) {
                        results.add( r );
                    }
                }
                return results;
            } else {
                return fetchValue( o );
            }
        } catch ( Exception e ) {
            ctx.notifyEvt( astEvent(Severity.ERROR, Msg.createMessage(Msg.ERROR_EVALUATING_PATH_EXPRESSION, expression.getText(), name.getText()), e) );
        }
        return null;
    }

    private Object fetchValue(Object o) {
        if ( name instanceof NameRefNode ) {
            o = EvalHelper.getValue( o, name.getText() );
        } else if ( name instanceof QualifiedNameNode ) {
            for ( NameRefNode nr : ((QualifiedNameNode) name).getParts() ) {
                o = EvalHelper.getValue( o, nr.getText() );
            }
        }
        return o;
    }

    @Override
    public Type getResultType() {
        return findType(expression.getResultType(), name);
    }

    private static Type findType(Type startType, BaseNode accessor) {
        if (!(startType instanceof CompositeType)) {
            return BuiltInType.UNKNOWN;
        }
        CompositeType compositeType = (CompositeType) startType;

        if ( accessor instanceof NameRefNode ) {
            NameRefNode nameRefNode = (NameRefNode) accessor;
            return compositeType.getFields().getOrDefault(nameRefNode.getText() , BuiltInType.UNKNOWN);
        } else if ( accessor instanceof QualifiedNameNode ) {
            QualifiedNameNode qualifiedNameNode = (QualifiedNameNode) accessor;
            Type typeCursor = startType;
            for ( NameRefNode part : qualifiedNameNode.getParts() ) {
                typeCursor = findType(typeCursor, part);
            }
            return typeCursor;
        }
        return BuiltInType.UNKNOWN;
    }

    @Override
    public ASTNode[] getChildrenNode() {
        return new ASTNode[] { expression, name };
    }

    @Override
    public <T> T accept(Visitor<T> v) {
        return v.visit(this);
    }

}


text:
const qs            = require('querystring')
    , through2      = require('through2')
    , superagent    = require('superagent')
    , filecheck     = require('workshopper-exercise/filecheck')
    , execute       = require('workshopper-exercise/execute')
    , comparestdout = require('workshopper-exercise/comparestdout')
    , rndport       = require('../../lib/rndport')

let exercise        = require('workshopper-exercise')()

// checks that the submission file actually exists
exercise = filecheck(exercise)

// execute the solution and submission in parallel with spawn()
exercise = execute(exercise)

// set up ports to be passed to submission and solution
exercise.addSetup(function(mode, callback) {
  this.submissionPort = rndport()
  this.solutionPort   = this.submissionPort + 1

  this.submissionArgs = [this.submissionPort]
  this.solutionArgs   = [this.solutionPort]

  process.nextTick(callback)
})

// add a processor for both run and verify calls, added *before*
// the comparestdout processor so we can mess with the stdouts
exercise.addProcessor(function (mode, callback) {
  this.submissionStdout.pipe(process.stdout)

  // replace stdout with our own streams
  this.submissionStdout = through2()
  if (mode == 'verify')
    this.solutionStdout = through2()

  setTimeout(query.bind(this, mode), 1500)

  process.nextTick(function () {
    callback(null, true)
  })
})

// compare stdout of solution and submission
exercise = comparestdout(exercise)

// delayed for 500ms to wait for servers to start so we can start
// playing with them
function query (mode) {
  const exercise = this

  function connect (port, stream) {
    const url = 'http://localhost:' + port + '/form'
    const text = exercise.__('exercises.good_old_form.source').split('').reverse().join('');

    superagent
      .post(url)
      .type('form')
      .send({str: text})
      .pipe(stream)
  }

  connect(this.submissionPort, this.submissionStdout)

  if (mode == 'verify')
    connect(this.solutionPort, this.solutionStdout)
}

module.exports = exercise


text:
require "logstash/filters/base"
require "logstash/namespace"
require "lru_redux"
require "resolv"
require "timeout"

java_import 'java.net.IDN'

# The DNS filter performs a lookup (either an A record/CNAME record lookup
# or a reverse lookup at the PTR record) on records specified under the
# `reverse` arrays or respectively under the `resolve` arrays.
#
# The config should look like this:
# [source,ruby]
#     filter {
#       dns {
#         reverse => [ "source_host", "field_with_address" ]
#         resolve => [ "field_with_fqdn" ]
#         action => "replace"
#       }
#     }
#
# This filter, like all filters, only processes 1 event at a time, so the use
# of this plugin can significantly slow down your pipeline's throughput if you
# have a high latency network. By way of example, if each DNS lookup takes 2
# milliseconds, the maximum throughput you can achieve with a single filter
# worker is 500 events per second (1000 milliseconds / 2 milliseconds).
class LogStash::Filters::DNS < LogStash::Filters::Base
  # TODO(sissel): The timeout limitation does seem to be fixed in here: http://redmine.ruby-lang.org/issues/5100 # but isn't currently in JRuby.
  # TODO(sissel): make `action` required? This was always the intent, but it
  # due to a typo it was never enforced. Thus the default behavior in past
  # versions was `append` by accident.

  config_name "dns"

  # Reverse resolve one or more fields.
  config :reverse, :validate => :array

  # Forward resolve one or more fields.
  config :resolve, :validate => :array

  # Determine what action to do: append or replace the values in the fields
  # specified under `reverse` and `resolve`.
  config :action, :validate => [ "append", "replace" ], :default => "append"

  # Use custom nameserver(s). For example: `["8.8.8.8", "8.8.4.4"]`
  config :nameserver, :validate => :array

  # `resolv` calls will be wrapped in a timeout instance
  config :timeout, :validate => :number, :default => 0.5

  # number of times to retry a failed resolve/reverse
  config :max_retries, :validate => :number, :default => 2

  # set the size of cache for successful requests
  config :hit_cache_size, :validate => :number, :default => 0

  # how long to cache successful requests (in seconds)
  config :hit_cache_ttl, :validate => :number, :default => 60

  # cache size for failed requests (Resolv::
  config :failed_cache_size, :validate => :number, :default => 0

  # how long to cache failed requests (in seconds)
  config :failed_cache_ttl, :validate => :number, :default => 5

  # Use custom hosts file(s). For example: `["/var/db/my_custom_hosts"]`
  config :hostsfile, :validate => :array

  public
  def register
    if @nameserver.nil? && @hostsfile.nil?
      @resolv = Resolv.new
    else
      @resolv = Resolv.new(build_resolvers)
    end

    if @hit_cache_size > 0
      @hit_cache = LruRedux::ThreadSafeCache.new(@hit_cache_size, @hit_cache_ttl)
    end

    if @failed_cache_size > 0
      @failed_cache = LruRedux::ThreadSafeCache.new(@failed_cache_size, @failed_cache_ttl)
    end

    @ip_validator = Resolv::AddressRegex
  end # def register

  public
  def filter(event)

    if @resolve
      return if resolve(event).nil?
    end

    if @reverse
      return if reverse(event).nil?
    end

    filter_matched(event)
  end

  private

  def build_resolvers
    build_user_host_resolvers.concat([::Resolv::Hosts.new]).concat(build_user_dns_resolver)
  end

  def build_user_host_resolvers
    return [] if @hostsfile.nil? || @hostsfile.empty?
    @hostsfile.map{|fn| ::Resolv::Hosts.new(fn)}
  end

  def build_user_dns_resolver
    return [] if @nameserver.nil? || @nameserver.empty?
    [::Resolv::DNS.new(:nameserver => @nameserver, :search => [], :ndots => 1)]
  end

  def resolve(event)
    @resolve.each do |field|
      is_array = false
      raw = event.get(field)
      if raw.is_a?(Array)
        is_array = true
        if raw.length > 1
          @logger.warn("DNS: skipping resolve, can't deal with multiple values", :field => field, :value => raw)
          return
        end
        raw = raw.first
      end

      begin
        return if @failed_cache && @failed_cache[raw] # recently failed resolv, skip
        if @hit_cache
          address = @hit_cache.getset(raw) { retriable_getaddress(raw) }
        else
          address = retriable_getaddress(raw)
        end
      rescue Resolv::ResolvError
        @failed_cache[raw] = true if @failed_cache
        @logger.debug("DNS: couldn't resolve the hostname.",
                      :field => field, :value => raw)
        return
      rescue Resolv::ResolvTimeout, Timeout::Error
        @logger.error("DNS: timeout on resolving the hostname.",
                      :field => field, :value => raw)
        return
      rescue SocketError => e
        @logger.error("DNS: Encountered SocketError.",
                      :field => field, :value => raw, :message => e.message)
        return
      end

      if @action == "replace"
        if is_array
          event.set(field, [address])
        else
          event.set(field, address)
        end
      else
        if !is_array
          event.set(field, [event.get(field), address])
        else
          arr = event.get(field)
          arr << address
          event.set(field, arr)
        end
      end

    end
  end

  private
  def reverse(event)
    @reverse.each do |field|
      raw = event.get(field)
      is_array = false
      if raw.is_a?(Array)
          is_array = true
          if raw.length > 1
            @logger.warn("DNS: skipping reverse, can't deal with multiple values", :field => field, :value => raw)
            return
          end
          raw = raw.first
      end

      if ! @ip_validator.match(raw)
        @logger.debug("DNS: not an address",
                      :field => field, :value => event.get(field))
        return
      end
      begin
        return if @failed_cache && @failed_cache.key?(raw) # recently failed resolv, skip
        if @hit_cache
          hostname = @hit_cache.getset(raw) { retriable_getname(raw) }
        else
          hostname = retriable_getname(raw)
        end
      rescue Resolv::ResolvError
        @failed_cache[raw] = true if @failed_cache
        @logger.debug("DNS: couldn't resolve the address.",
                      :field => field, :value => raw)
        return
      rescue Resolv::ResolvTimeout, Timeout::Error
        @logger.error("DNS: timeout on resolving address.",
                      :field => field, :value => raw)
        return
      rescue SocketError => e
        @logger.error("DNS: Encountered SocketError.",
                      :field => field, :value => raw, :message => e.message)
        return
      end

      if @action == "replace"
        if is_array
          event.set(field, [hostname])
        else
          event.set(field, hostname)
        end
      else
        if !is_array
          event.set(field, [event.get(field), hostname])
        else
          arr = event.get(field)
          arr << hostname
          event.set(field, arr)
        end
      end
    end
  end

  private
  def retriable_request(&block)
    tries = 0
    begin
      Timeout::timeout(@timeout) do
        block.call
      end
    rescue Timeout::Error, SocketError
      if tries < @max_retries
        tries = tries + 1
        retry
      else
        raise
      end
    end
  end

  private
  def retriable_getname(address)
    retriable_request do
      getname(address)
    end
  end

  private
  def retriable_getaddress(name)
    retriable_request do
      getaddress(name)
    end
  end

  private
  def getname(address)
    name = @resolv.getname(address).force_encoding(Encoding::UTF_8)
    IDN.toUnicode(name)
  end

  private
  def getaddress(name)
    idn = IDN.toASCII(name)
    @resolv.getaddress(idn).force_encoding(Encoding::UTF_8)
  end
end # class LogStash::Filters::DNS


text:
Whio
====
![](https://raw.github.com/Lerc/whio/master/whio_logo.png)
A library to provide a gentle introduction to programming.

The idea behind Whio is to provide an evironment where people learning to program can write short
programs that they can easily understand.

To do this, much of the architecture of DOM based Javascript is hidden.  Programs using whio can just have a single line of
```javascript
  print("hello");
```

Whio provides a great deal of globally scoped functions to enable people to write working programs before they have encountered
the concepts of Objects and Methods.

A program to continually update the mouse position on the screen can be done with
```javascript
  function update() {
      clear();
      print(getMousePosition());
  }

  run(update);
```

There is a wiki page showing the functions Whio provides at http://fingswotidun.com/code/index.php/API

Whio includes the [sugarjs](http://sugarjs.com/) library, to make things nicer when the programmer starts to use the methods
of implicitly created objects.

Web Worker Whio
====
This is a second implementation of the same library.  The web worker version is for running a given program inside a web worker.
To handle I/O the worker communicates via messages to the host javascript.    This enables a page to run a whio program without
the risk of the program interferring with the host page.   A significant problem with JavaScript is that people learning to program
can inadvertantly generate an infinite loop locking out other javascript code.  A web worker mitigates this problem somewhat.
It is a little more flexible than using an iframe since the target canvas is controlled by the host and could be placed/transformed/composited as it sees fit.

The downside of the Web Worker interface is there are inherent difficulties with transferring I/O though messages.
Whio tries to minimise these as much as possible.

MediaWiki Plugin
====
There is a plugin for mediawiki which enables code to be included in a wiki page, and edited on the spot.  Running the code launches the Web Worker version of Whio

This plugin is used on the Wiki at http://fingswotidun.com/code which has the beginnings to a guide to programming  for beginners.

Is this code untidy?
=====
Oh yes indeedie it is.  It evolved though a bunch of odd paths, working on tidyups now.


text:
package bwyap.gridgame.render;

import bwyap.gridgame.render.entity.base.DrawableInterface;

/**
 * Interface for a canvas which can render Drawable items
 * @author bwyap
 *
 */
public interface CanvasInterface {

	/**
	 * Add a drawable item to be rendered on the canvas
	 * @param item the drawable item
	 */
	public void addDrawable(DrawableInterface item);

	/**
	 * Remove a drawable item from the canvas.
	 * @param id the id of the drawable item
	 * @return the drawable item if it exists, null otherwise
	 */
	public DrawableInterface removeDrawable(String id);

}


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

<title>Uses of Package org.apache.poi.hwpf.usermodel (POI API Documentation)</title>
<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">

JavaScript is disabled on your browser.

Overview

Package

Class

Use

Tree

Deprecated

Index

Help

Prev

Next

Frames

No Frames

All Classes

Uses of Package
org.apache.poi.hwpf.usermodel

Packages that use org.apache.poi.hwpf.usermodel

Package
Description

org.apache.poi.hwpf

org.apache.poi.hwpf.converter

org.apache.poi.hwpf.dev

org.apache.poi.hwpf.extractor

org.apache.poi.hwpf.model

org.apache.poi.hwpf.model.types

org.apache.poi.hwpf.sprm

org.apache.poi.hwpf.usermodel

Classes in org.apache.poi.hwpf.usermodel used by org.apache.poi.hwpf

Class and Description

Bookmarks

User-friendly interface to access document bookmarks

Fields

User-friendly interface to access document Fields

HWPFList

This class is used to create a list in a Word document.

Notes

User-friendly interface to access document notes information

ObjectPoolImpl

ObjectsPool

OfficeDrawings

User-friendly interface to access document part's office drawings

OfficeDrawingsImpl

Range

This class is the central class of the HWPF object model.

Classes in org.apache.poi.hwpf.usermodel used by org.apache.poi.hwpf.converter

Class and Description

Bookmark

User friendly interface to access information about document bookmarks

BorderCode

Mapping class for BRC80 structure (Border Code for Word 97)

 Comments are copied out from the binary format specification.

CharacterRun

This class represents a run of text that share common properties.

Field

HWPFList

This class is used to create a list in a Word document.

OfficeDrawing

User-friendly interface to office drawing objects.

Paragraph

Picture

Represents embedded picture extracted from Word Document

PictureType

Picture types supported by MS Word format

Range

This class is the central class of the HWPF object model.

Section

Table

TableCell

TableRow

Classes in org.apache.poi.hwpf.usermodel used by org.apache.poi.hwpf.dev

Class and Description

ParagraphProperties

Classes in org.apache.poi.hwpf.usermodel used by org.apache.poi.hwpf.extractor

Class and Description

Range

This class is the central class of the HWPF object model.

Classes in org.apache.poi.hwpf.usermodel used by org.apache.poi.hwpf.model

Class and Description

CharacterProperties

CharacterRun

This class represents a run of text that share common properties.

ParagraphProperties

Picture

Represents embedded picture extracted from Word Document

SectionProperties

Shape

Deprecated.

Use OfficeDrawing instead

Classes in org.apache.poi.hwpf.usermodel used by org.apache.poi.hwpf.model.types

Class and Description

BorderCode

Mapping class for BRC80 structure (Border Code for Word 97)

 Comments are copied out from the binary format specification.

DateAndTime

This class is used to represent a date and time in a Word document.

DropCapSpecifier

This data structure is used by a paragraph to determine how it should drop
 its first letter.

LineSpacingDescriptor

This class is used to determine line spacing for a paragraph.

ShadingDescriptor

The SHD is a substructure of the CHP, PAP, and TC for Word 2000.

TableAutoformatLookSpecifier

TableCellDescriptor

Classes in org.apache.poi.hwpf.usermodel used by org.apache.poi.hwpf.sprm

Class and Description

CharacterProperties

ParagraphProperties

SectionProperties

TableProperties

Classes in org.apache.poi.hwpf.usermodel used by org.apache.poi.hwpf.usermodel

Class and Description

Bookmark

User friendly interface to access information about document bookmarks

Bookmarks

User-friendly interface to access document bookmarks

BorderCode

Mapping class for BRC80 structure (Border Code for Word 97)

 Comments are copied out from the binary format specification.

CharacterProperties

CharacterRun

This class represents a run of text that share common properties.

DropCapSpecifier

This data structure is used by a paragraph to determine how it should drop
 its first letter.

Field

Fields

User-friendly interface to access document Fields

HWPFList

This class is used to create a list in a Word document.

LineSpacingDescriptor

This class is used to determine line spacing for a paragraph.

ListEntry

Notes

User-friendly interface to access document notes information

ObjectsPool

OfficeDrawing

User-friendly interface to office drawing objects.

OfficeDrawing.HorizontalPositioning

OfficeDrawing.HorizontalRelativeElement

OfficeDrawing.VerticalPositioning

OfficeDrawing.VerticalRelativeElement

OfficeDrawings

User-friendly interface to access document part's office drawings

Paragraph

ParagraphProperties

PictureType

Picture types supported by MS Word format

Range

This class is the central class of the HWPF object model.

Section

ShadingDescriptor

The SHD is a substructure of the CHP, PAP, and TC for Word 2000.

ShadingDescriptor80

The SHD80 is a substructure of the CHP and PAP, and TC for Word 97.

Table

TableAutoformatLookSpecifier

TableCell

TableCellDescriptor

TableProperties

TableRow

Overview

Package

Class

Use

Tree

Deprecated

Index

Help

Prev

Next

Frames

No Frames

All Classes

                Copyright 2014 The Apache Software Foundation or
        its licensors, as applicable.