text:
package org.apache.hadoop.hdfs;

import com.google.common.collect.Lists;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.CommonPathCapabilities;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.PartialListing;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.ipc.RemoteException;
import org.apache.hadoop.security.AccessControlException;
import org.apache.hadoop.security.UserGroupInformation;
import org.hamcrest.core.StringContains;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.PrivilegedExceptionAction;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * Tests for the batched listing API.
 */
public class TestBatchedListDirectories {

  private static MiniDFSCluster cluster;
  private static Configuration conf;
  private static DistributedFileSystem dfs;

  @Rule
  public ExpectedException thrown = ExpectedException.none();

  private static final List<Path> SUBDIR_PATHS = Lists.newArrayList();
  private static final List<Path> FILE_PATHS = Lists.newArrayList();
  private static final int FIRST_LEVEL_DIRS = 2;
  private static final int SECOND_LEVEL_DIRS = 3;
  private static final int FILES_PER_DIR = 5;
  private static final Path EMPTY_DIR_PATH = new Path("/emptydir");
  private static final Path DATA_FILE_PATH = new Path("/datafile");
  private static final Path INACCESSIBLE_DIR_PATH = new Path("/noperms");
  private static final Path INACCESSIBLE_FILE_PATH =
      new Path(INACCESSIBLE_DIR_PATH, "nopermsfile");

  private static Path getSubDirName(int i, int j) {
    return new Path(String.format("/dir%d/subdir%d", i, j));
  }

  private static Path getFileName(int i, int j, int k) {
    Path dirPath = getSubDirName(i, j);
    return new Path(dirPath, "file" + k);
  }

  private static void assertSubDirEquals(int i, int j, Path p) {
    assertTrue(p.toString().startsWith("hdfs://"));
    Path expected = getSubDirName(i, j);
    assertEquals("Unexpected subdir name",
        expected.toString(), p.toUri().getPath());
  }

  private static void assertFileEquals(int i, int j, int k, Path p) {
    assertTrue(p.toString().startsWith("hdfs://"));
    Path expected = getFileName(i, j, k);
    assertEquals("Unexpected file name",
        expected.toString(), p.toUri().getPath());
  }

  private static void loadData() throws Exception {
    for (int i = 0; i < FIRST_LEVEL_DIRS; i++) {
      for (int j = 0; j < SECOND_LEVEL_DIRS; j++) {
        Path dirPath = getSubDirName(i, j);
        dfs.mkdirs(dirPath);
        SUBDIR_PATHS.add(dirPath);
        for (int k = 0; k < FILES_PER_DIR; k++) {
          Path filePath = getFileName(i, j, k);
          dfs.create(filePath, (short)1).close();
          FILE_PATHS.add(filePath);
        }
      }
    }
    dfs.mkdirs(EMPTY_DIR_PATH);
    FSDataOutputStream fsout = dfs.create(DATA_FILE_PATH, (short)1);
    fsout.write(123);
    fsout.close();

    dfs.mkdirs(INACCESSIBLE_DIR_PATH);
    dfs.create(INACCESSIBLE_FILE_PATH, (short)1).close();
    dfs.setPermission(INACCESSIBLE_DIR_PATH, new FsPermission(0000));
  }

  @BeforeClass
  public static void beforeClass() throws Exception {
    conf = new HdfsConfiguration();
    conf.setInt(DFSConfigKeys.DFS_LIST_LIMIT, 7);
    conf.setInt(DFSConfigKeys.DFS_NAMENODE_BATCHED_LISTING_LIMIT,
        FIRST_LEVEL_DIRS * SECOND_LEVEL_DIRS * FILES_PER_DIR);
    cluster = new MiniDFSCluster.Builder(conf)
        .numDataNodes(1)
        .build();
    dfs = cluster.getFileSystem();
    loadData();
  }

  @AfterClass
  public static void afterClass() {
    if (cluster != null) {
      cluster.shutdown();
    }
  }

  private static List<PartialListing<FileStatus>> getListings(List<Path> paths)
      throws IOException {
    List<PartialListing<FileStatus>> returned = Lists.newArrayList();
    RemoteIterator<PartialListing<FileStatus>> it =
        dfs.batchedListStatusIterator(paths);
    while (it.hasNext()) {
      returned.add(it.next());
    }
    return returned;
  }

  private static List<FileStatus> listingsToStatuses(
      List<PartialListing<FileStatus>> listings) throws IOException {
    List<FileStatus> returned = Lists.newArrayList();
    for (PartialListing<FileStatus> listing : listings) {
      returned.addAll(listing.get());
    }
    return returned;
  }

  private static List<FileStatus> getStatuses(List<Path> paths)
      throws IOException {
    List<PartialListing<FileStatus>> listings = getListings(paths);
    return listingsToStatuses(listings);
  }

  @Test
  public void testEmptyPath() throws Exception {
    thrown.expect(FileNotFoundException.class);
    List<Path> paths = Lists.newArrayList();
    getStatuses(paths);
  }

  @Test
  public void testEmptyDir() throws Exception {
    List<Path> paths = Lists.newArrayList(EMPTY_DIR_PATH);
    List<PartialListing<FileStatus>> listings = getListings(paths);
    assertEquals(1, listings.size());
    PartialListing<FileStatus> listing = listings.get(0);
    assertEquals(EMPTY_DIR_PATH, listing.getListedPath());
    assertEquals(0, listing.get().size());
  }
  @Test
  public void listOneFile() throws Exception {
    List<Path> paths = Lists.newArrayList();
    paths.add(FILE_PATHS.get(0));
    List<FileStatus> statuses = getStatuses(paths);
    assertEquals(1, statuses.size());
    assertFileEquals(0, 0, 0, statuses.get(0).getPath());
  }

  @Test
  public void listDoesNotExist() throws Exception {
    thrown.expect(FileNotFoundException.class);
    List<Path> paths = Lists.newArrayList();
    paths.add(new Path("/does/not/exist"));
    getStatuses(paths);
  }

  @Test
  public void listSomeDoNotExist() throws Exception {
    List<Path> paths = Lists.newArrayList();
    paths.add(new Path("/does/not/exist"));
    paths.addAll(SUBDIR_PATHS.subList(0, FIRST_LEVEL_DIRS));
    paths.add(new Path("/does/not/exist"));
    paths.addAll(SUBDIR_PATHS.subList(0, FIRST_LEVEL_DIRS));
    paths.add(new Path("/does/not/exist"));
    List<PartialListing<FileStatus>> listings = getListings(paths);
    for (int i = 0; i < listings.size(); i++) {
      PartialListing<FileStatus> partial = listings.get(i);
      if (partial.getListedPath().toString().equals("/does/not/exist")) {
        try {
          partial.get();
          fail("Expected exception");
        } catch (FileNotFoundException e) {
          assertTrue(e.getMessage().contains("/does/not/exist"));
        }
      } else {
        partial.get();
      }
    }
    try {
      listings.get(listings.size()-1).get();
      fail("Expected exception");
    } catch (FileNotFoundException e) {
      assertTrue(e.getMessage().contains("/does/not/exist"));
    }
  }

  @Test
  public void listDirRelative() throws Exception {
    dfs.setWorkingDirectory(new Path("/dir0"));
    List<Path> paths = Lists.newArrayList(new Path("."));
    List<FileStatus> statuses = getStatuses(paths);
    assertEquals("Wrong number of items",
        SECOND_LEVEL_DIRS, statuses.size());
    for (int i = 0; i < SECOND_LEVEL_DIRS; i++) {
      FileStatus stat = statuses.get(i);
      assertSubDirEquals(0, i, stat.getPath());
    }
  }

  @Test
  public void listFilesRelative() throws Exception {
    dfs.setWorkingDirectory(new Path("/dir0"));
    List<Path> paths = Lists.newArrayList(new Path("subdir0"));
    List<FileStatus> statuses = getStatuses(paths);
    assertEquals("Wrong number of items",
        FILES_PER_DIR, statuses.size());
    for (int i = 0; i < FILES_PER_DIR; i++) {
      FileStatus stat = statuses.get(i);
      assertFileEquals(0, 0, i, stat.getPath());
    }
  }

  @Test
  public void testDFSHasCapability() throws Throwable {
    assertTrue("FS does not declare PathCapability support",
        dfs.hasPathCapability(new Path("/"),
            CommonPathCapabilities.FS_EXPERIMENTAL_BATCH_LISTING));
  }

  private void listFilesInternal(int numFiles) throws Exception {
    List<Path> paths = FILE_PATHS.subList(0, numFiles);
    List<FileStatus> statuses = getStatuses(paths);
    assertEquals(paths.size(), statuses.size());
    for (int i = 0; i < paths.size(); i++) {
      Path p = paths.get(i);
      FileStatus stat = statuses.get(i);
      assertEquals(p.toUri().getPath(), stat.getPath().toUri().getPath());
    }
  }

  @Test
  public void listOneFiles() throws Exception {
    listFilesInternal(1);
  }

  @Test
  public void listSomeFiles() throws Exception {
    listFilesInternal(FILE_PATHS.size() / 2);
  }

  @Test
  public void listAllFiles() throws Exception {
    listFilesInternal(FILE_PATHS.size());
  }

  private void listDirectoriesInternal(int numDirs) throws Exception {
    List<Path> paths = SUBDIR_PATHS.subList(0, numDirs);
    List<PartialListing<FileStatus>> listings = getListings(paths);

    LinkedHashMap<Path, List<FileStatus>> listing = new LinkedHashMap<>();
    for (PartialListing<FileStatus> partialListing : listings) {
      Path parent = partialListing.getListedPath();
      if (!listing.containsKey(parent)) {
        listing.put(parent, Lists.newArrayList());
      }
      listing.get(parent).addAll(partialListing.get());
    }

    assertEquals(paths.size(), listing.size());
    int pathIdx = 0;
    for (Map.Entry<Path, List<FileStatus>> entry : listing.entrySet()) {
      Path expected = paths.get(pathIdx++);
      Path parent = entry.getKey();
      List<FileStatus> children = entry.getValue();
      assertEquals(expected, parent);
      assertEquals(FILES_PER_DIR, children.size());
    }
  }

  @Test
  public void listOneDirectory() throws Exception {
    listDirectoriesInternal(1);
  }

  @Test
  public void listSomeDirectories() throws Exception {
    listDirectoriesInternal(SUBDIR_PATHS.size() / 2);
  }

  @Test
  public void listAllDirectories() throws Exception {
    listDirectoriesInternal(SUBDIR_PATHS.size());
  }

  @Test
  public void listTooManyDirectories() throws Exception {
    thrown.expect(RemoteException.class);
    thrown.expectMessage(
        StringContains.containsString("Too many source paths"));
    List<Path> paths = Lists.newArrayList(FILE_PATHS);
    paths.add(SUBDIR_PATHS.get(0));
    getStatuses(paths);
  }

  @Test
  public void listDirsAndEmpty() throws Exception {
    List<Path> paths = Lists.newArrayList();
    paths.add(EMPTY_DIR_PATH);
    paths.add(FILE_PATHS.get(0));
    paths.add(EMPTY_DIR_PATH);
    List<PartialListing<FileStatus>> listings = getListings(paths);
    assertEquals(3, listings.size());
    assertEquals(0, listings.get(0).get().size());
    assertEquals(1, listings.get(1).get().size());
    assertEquals(FILE_PATHS.get(0).toString(),
        listings.get(1).get().get(0).getPath().toUri().getPath());
    assertEquals(0, listings.get(2).get().size());
  }

  @Test
  public void listSamePaths() throws Exception {
    List<Path> paths = Lists.newArrayList();
    paths.add(SUBDIR_PATHS.get(0));
    paths.add(SUBDIR_PATHS.get(0));
    paths.add(FILE_PATHS.get(0));
    paths.add(FILE_PATHS.get(0));
    List<FileStatus> statuses = getStatuses(paths);
    assertEquals(FILES_PER_DIR*2 + 2, statuses.size());
    List<FileStatus> slice = statuses.subList(0, FILES_PER_DIR);
    for (int i = 0; i < FILES_PER_DIR; i++) {
      assertFileEquals(0, 0, i, slice.get(i).getPath());
    }
    slice = statuses.subList(FILES_PER_DIR, FILES_PER_DIR*2);
    for (int i = 0; i < FILES_PER_DIR; i++) {
      assertFileEquals(0, 0, i, slice.get(i).getPath());
    }
    assertFileEquals(0, 0, 0, statuses.get(FILES_PER_DIR*2).getPath());
    assertFileEquals(0, 0, 0, statuses.get(FILES_PER_DIR*2+1).getPath());
  }

  @Test
  public void listLocatedStatus() throws Exception {
    List<Path> paths = Lists.newArrayList();
    paths.add(DATA_FILE_PATH);
    RemoteIterator<PartialListing<LocatedFileStatus>> it =
        dfs.batchedListLocatedStatusIterator(paths);
    PartialListing<LocatedFileStatus> listing = it.next();
    List<LocatedFileStatus> statuses = listing.get();
    assertEquals(1, statuses.size());
    assertTrue(statuses.get(0).getBlockLocations().length > 0);
  }

  private void listAsNormalUser(List<Path> paths)
      throws IOException, InterruptedException {
    final UserGroupInformation ugi = UserGroupInformation
        .createRemoteUser("tiffany");
    ugi.doAs(new PrivilegedExceptionAction<Void>() {
      @Override
      public Void run() throws Exception {
        // try renew with long name
        DistributedFileSystem fs = (DistributedFileSystem)
            FileSystem.get(cluster.getURI(), conf);
        RemoteIterator<PartialListing<FileStatus>> it =
            fs.batchedListStatusIterator(paths);
        PartialListing<FileStatus> listing = it.next();
        listing.get();
        return null;
      }
    });
  }

  @Test
  public void listInaccessibleDir() throws Exception {
    thrown.expect(AccessControlException.class);
    List<Path> paths = Lists.newArrayList(INACCESSIBLE_DIR_PATH);
    listAsNormalUser(paths);
  }

  @Test
  public void listInaccessibleFile() throws Exception {
    thrown.expect(AccessControlException.class);
    List<Path> paths = Lists.newArrayList(INACCESSIBLE_FILE_PATH);
    listAsNormalUser(paths);
  }
}


text:
/*
 * aria2 - The high speed download utility
 *
 * Copyright (C) 2006 Tatsuhiro Tsujikawa
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations
 * including the two.
 * You must obey the GNU General Public License in all respects
 * for all of the code used other than OpenSSL.  If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so.  If you
 * do not wish to do so, delete this exception statement from your
 * version.  If you delete this exception statement from all source
 * files in the program, then also delete it here.
 */
/* copyright --> */
#ifndef D_FILE_ENTRY_H
#define D_FILE_ENTRY_H

#include "common.h"

#include <string>
#include <deque>
#include <vector>
#include <ostream>
#include <set>
#include <memory>

#include "File.h"
#include "Request.h"
#include "URIResult.h"
#include "error_code.h"
#include "A2STR.h"
#include "TimerA2.h"
#include "util.h"
#include "a2functional.h"

namespace aria2 {

class URISelector;
class ServerStatMan;

class FileEntry {
public:
  typedef std::set<std::shared_ptr<Request>, RefLess<Request> >
  InFlightRequestSet;
private:
  std::string path_;
  std::deque<std::string> uris_;
  std::deque<std::string> spentUris_;
  int64_t length_;
  int64_t offset_;
  bool requested_;

  class RequestFaster {
  public:
    bool operator()(const std::shared_ptr<Request>& lhs,
                    const std::shared_ptr<Request>& rhs) const;
  };

  typedef std::set<std::shared_ptr<Request>, RequestFaster> RequestPool;
  RequestPool requestPool_;
  InFlightRequestSet inFlightRequests_;
  std::string contentType_;
  // URIResult is stored in the ascending order of the time when its result is
  // available.
  std::deque<URIResult> uriResults_;
  bool uniqueProtocol_;
  int maxConnectionPerServer_;
  std::string originalName_;
  Timer lastFasterReplace_;

  void storePool(const std::shared_ptr<Request>& request);
public:
  FileEntry();

  FileEntry(const std::string& path, int64_t length, int64_t offset,
            const std::vector<std::string>& uris = std::vector<std::string>());

  ~FileEntry();

  FileEntry& operator=(const FileEntry& entry);

  std::string getBasename() const;

  std::string getDirname() const;

  const std::string& getPath() const { return path_; }

  void setPath(const std::string& path);

  int64_t getLength() const { return length_; }

  void setLength(int64_t length) { length_ = length; }

  int64_t getOffset() const { return offset_; }

  void setOffset(int64_t offset) { offset_ = offset; }

  int64_t getLastOffset() { return offset_+length_; }

  bool isRequested() const { return requested_; }

  void setRequested(bool flag) { requested_ = flag; }

  const std::deque<std::string>& getRemainingUris() const
  {
    return uris_;
  }

  std::deque<std::string>& getRemainingUris()
  {
    return uris_;
  }

  const std::deque<std::string>& getSpentUris() const
  {
    return spentUris_;
  }

  // Exposed for unitest
  std::deque<std::string>& getSpentUris()
  {
    return spentUris_;
  }

  size_t setUris(const std::vector<std::string>& uris);

  template<typename InputIterator>
  size_t addUris(InputIterator first, InputIterator last)
  {
    size_t count = 0;
    for(; first != last; ++first) {
      if(addUri(*first)) {
        ++count;
      }
    }
    return count;
  }

  bool addUri(const std::string& uri);

  bool insertUri(const std::string& uri, size_t pos);

  // Inserts uris_ and spentUris_ into uris.
  void getUris(std::vector<std::string>& uris) const;

  void setContentType(const std::string& contentType);

  const std::string& getContentType() const { return contentType_; }

  // If pooled Request object is available, one of them is removed
  // from the pool and returned.  If pool is empty, then select URI
  // using selectUri(selector) and construct Request object using it
  // and return the Request object.  If referer is given, it is set to
  // newly created Request. If Request object is retrieved from the
  // pool, referer is ignored.  If method is given, it is set to newly
  // created Request. If Request object is retrieved from the pool,
  // method is ignored. If uriReuse is true and selector does not
  // returns Request object either because uris_ is empty or all URI
  // are not be usable because maxConnectionPerServer_ limit, then
  // reuse used URIs and do selection again.
  std::shared_ptr<Request> getRequest
  (URISelector* selector,
   bool uriReuse,
   const std::vector<std::pair<size_t, std::string> >& usedHosts,
   const std::string& referer = A2STR::NIL,
   const std::string& method = Request::METHOD_GET);

  // Finds pooled Request object which is faster than passed one,
  // comparing their PeerStat objects. If such Request is found, it is
  // removed from the pool and returned.
  std::shared_ptr<Request> findFasterRequest(const std::shared_ptr<Request>& base);

  // Finds faster server using ServerStatMan.
  std::shared_ptr<Request>
  findFasterRequest
  (const std::shared_ptr<Request>& base,
   const std::vector<std::pair<size_t, std::string> >& usedHosts,
   const std::shared_ptr<ServerStatMan>& serverStatMan);

  void poolRequest(const std::shared_ptr<Request>& request);

  bool removeRequest(const std::shared_ptr<Request>& request);

  size_t countInFlightRequest() const;

  size_t countPooledRequest() const;

  const InFlightRequestSet& getInFlightRequests() const
  {
    return inFlightRequests_;
  }

  bool operator<(const FileEntry& fileEntry) const;

  bool exists() const;

  // Translate global offset goff to file local offset.
  int64_t gtoloff(int64_t goff) const;

  void removeURIWhoseHostnameIs(const std::string& hostname);

  void removeIdenticalURI(const std::string& uri);

  void addURIResult(std::string uri, error_code::Value result);

  const std::deque<URIResult>& getURIResults() const
  {
    return uriResults_;
  }

  // Extracts URIResult whose _result is r and stores them into res.
  // The extracted URIResults are removed from uriResults_.
  void extractURIResult
  (std::deque<URIResult>& res, error_code::Value r);

  void setMaxConnectionPerServer(int n)
  {
    maxConnectionPerServer_ = n;
  }

  int getMaxConnectionPerServer() const
  {
    return maxConnectionPerServer_;
  }

  // Reuse URIs which have not emitted error so far and whose host
  // component is not included in ignore. The reusable URIs are
  // appended to uris_ maxConnectionPerServer_ times.
  void reuseUri(const std::vector<std::string>& ignore);

  void releaseRuntimeResource();

  // Push URIs in pooled or in-flight requests to the front of uris_.
  void putBackRequest();

  void setOriginalName(const std::string& originalName);

  const std::string& getOriginalName() const
  {
    return originalName_;
  }

  bool removeUri(const std::string& uri);

  bool emptyRequestUri() const;

  void setUniqueProtocol(bool f)
  {
    uniqueProtocol_ = f;
  }

  bool isUniqueProtocol() const
  {
    return uniqueProtocol_;
  }
};

// Returns the first FileEntry which isRequested() method returns
// true.  If no such FileEntry exists, then returns
// std::shared_ptr<FileEntry>().
template<typename InputIterator>
std::shared_ptr<FileEntry> getFirstRequestedFileEntry
(InputIterator first, InputIterator last)
{
  for(; first != last; ++first) {
    if((*first)->isRequested()) {
      return *first;
    }
  }
  return nullptr;
}

// Counts the number of files selected in the given iterator range
// [first, last).
template<typename InputIterator>
size_t countRequestedFileEntry(InputIterator first, InputIterator last)
{
  size_t count = 0;
  for(; first != last; ++first) {
    if((*first)->isRequested()) {
      ++count;
    }
  }
  return count;
}

// Returns true if at least one requested FileEntry has URIs.
template<typename InputIterator>
bool isUriSuppliedForRequsetFileEntry(InputIterator first, InputIterator last)
{
  for(; first != last; ++first) {
    if((*first)->isRequested() && !(*first)->getRemainingUris().empty()) {
      return true;
    }
  }
  return false;
}

// Writes filename to given o.  If memory is true, the output is
// "[MEMORY]" plus the basename of the filename.  If there is no
// FileEntry, writes "n/a" to o.
void writeFilePath
(std::ostream& o,
 const std::shared_ptr<FileEntry>& entry,
 bool memory);

// Writes first filename to given o.  If memory is true, the output is
// "[MEMORY]" plus the basename of the first filename.  If there is no
// FileEntry, writes "n/a" to o.  If more than 1 FileEntry are in the
// iterator range [first, last), "(Nmore)" is written at the end where
// N is the number of files in iterator range [first, last) minus 1.
template<typename InputIterator>
void writeFilePath
(InputIterator first, InputIterator last, std::ostream& o, bool memory)
{
  std::shared_ptr<FileEntry> e = getFirstRequestedFileEntry(first, last);
  if(!e) {
    o << "n/a";
  } else {
    writeFilePath(o, e, memory);
    if(!e->getPath().empty()) {
      size_t count = countRequestedFileEntry(first, last);
      if(count > 1) {
        o << " (" << count-1 << "more)";
      }
    }
  }
}

} // namespace aria2

#endif // D_FILE_ENTRY_H

redpajama_set_name: RedPajamaGithub


text:
    <meta charset="UTF-8">
    <title>Resource {{ resource_name }} has been created.</title>

    Hello!

    Resource {{ resource_name }} has been created.


text:
package io.datarouter.nodewatch.storage.tablesample;

import java.util.List;
import java.util.function.Supplier;

import io.datarouter.model.databean.BaseDatabean;
import io.datarouter.model.field.Field;
import io.datarouter.model.serialize.fielder.BaseDatabeanFielder;

public class TableSamplerTestBean extends BaseDatabean<TableSamplerTestBeanKey,TableSamplerTestBean>{

	public static class TableSamplerTestBeanFielder
	extends BaseDatabeanFielder<TableSamplerTestBeanKey,TableSamplerTestBean>{

		public TableSamplerTestBeanFielder(){
			super(TableSamplerTestBeanKey::new);
		}
		@Override
		public List<Field<?>> getNonKeyFields(TableSamplerTestBean databean){
			return List.of();
		}

	}

	@Override
	public Supplier<TableSamplerTestBeanKey> getKeySupplier(){
		return TableSamplerTestBeanKey::new;
	}

	public TableSamplerTestBean(){
		super(new TableSamplerTestBeanKey(null, null, null));
	}

	public TableSamplerTestBean(Long fieldA, Long fieldB, String other){
		super(new TableSamplerTestBeanKey(fieldA, fieldB, other));
	}

}


text:
using System.Runtime.Serialization;

namespace System.ComponentModel.DataAnnotations
{
    /// <summary>
    ///     Exception used for validation using <see cref="ValidationAttribute" />.
    /// </summary>
    [Serializable]
    [System.Runtime.CompilerServices.TypeForwardedFrom("System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
    public class ValidationException : Exception
    {
        private ValidationResult _validationResult;

        /// <summary>
        ///     Constructor that accepts a structured <see cref="ValidationResult" /> describing the problem.
        /// </summary>
        /// <param name="validationResult">The value describing the validation error</param>
        /// <param name="validatingAttribute">The attribute that triggered this exception</param>
        /// <param name="value">The value that caused the validating attribute to trigger the exception</param>
        public ValidationException(ValidationResult validationResult, ValidationAttribute validatingAttribute,
            object value)
            : this(validationResult.ErrorMessage, validatingAttribute, value)
        {
            _validationResult = validationResult;
        }

        /// <summary>
        ///     Constructor that accepts an error message, the failing attribute, and the invalid value.
        /// </summary>
        /// <param name="errorMessage">The localized error message</param>
        /// <param name="validatingAttribute">The attribute that triggered this exception</param>
        /// <param name="value">The value that caused the validating attribute to trigger the exception</param>
        public ValidationException(string errorMessage, ValidationAttribute validatingAttribute, object value)
            : base(errorMessage)
        {
            Value = value;
            ValidationAttribute = validatingAttribute;
        }

        /// <summary>
        ///     Default constructor.
        /// </summary>
        /// <remarks>The long form of this constructor is preferred because it gives better error reporting.</remarks>
        public ValidationException()
            : base()
        {
        }

        /// <summary>
        ///     Constructor that accepts only a localized message
        /// </summary>
        /// <remarks>The long form of this constructor is preferred because it gives better error reporting.</remarks>
        /// <param name="message">The localized message</param>
        public ValidationException(string message)
            : base(message)
        {
        }

        /// <summary>
        ///     Constructor that accepts a localized message and an inner exception
        /// </summary>
        /// <remarks>The long form of this constructor is preferred because it gives better error reporting</remarks>
        /// <param name="message">The localized error message</param>
        /// <param name="innerException">inner exception</param>
        public ValidationException(string message, Exception innerException)
            : base(message, innerException)
        {
        }

        /// <summary>
        ///     Constructor that takes a SerializationInfo.
        /// </summary>
        /// <param name="info">The SerializationInfo.</param>
        /// <param name="context">The StreamingContext.</param>
        protected ValidationException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        /// <summary>
        ///     Gets the <see>ValidationAttribute</see> instance that triggered this exception.
        /// </summary>
        public ValidationAttribute ValidationAttribute { get; }

        /// <summary>
        ///     Gets the <see cref="ValidationResult" /> instance that describes the validation error.
        /// </summary>
        /// <value>
        ///     This property will never be null.
        /// </value>
        public ValidationResult ValidationResult
        {
            get
            {
                if (_validationResult == null)
                {
                    _validationResult = new ValidationResult(Message);
                }
                return _validationResult;
            }
        }

        /// <summary>
        ///     Gets the value that caused the validating attribute to trigger the exception
        /// </summary>
        public object Value { get; }
    }
}


text:
package redhat.jee_migration_example.management.itemStoreManager;

import org.aries.bean.Proxy;
import org.aries.tx.service.ejb.EJBProxy;

public class ItemStoreManagerProxyForEJB extends EJBProxy implements Proxy<ItemStoreManager> {

	private ItemStoreManagerInterceptor itemStoreManagerInterceptor;

	public ItemStoreManagerProxyForEJB(String serviceId, String host, int port) {
		super(serviceId, host, port);
		createDelegate();
	}

	protected void createDelegate() {
		itemStoreManagerInterceptor = new ItemStoreManagerInterceptor();
		itemStoreManagerInterceptor.setProxy(this);
	}

	@Override
	public ItemStoreManager getDelegate() {
		return itemStoreManagerInterceptor;
	}

}


text:
/*
 * PLUGIN EDIT
 *
 * Dutch language file.
 *
 * Author: rascalli (rascallim@gmail.com)
 */

 theUILang.EditTrackers 		= "Bewerk torrent ...";
 theUILang.EditTorrentProperties	= "Torrent Eigenschappen";
 theUILang.errorAddTorrent		= "Fout bij het toevoegen van het torrent-bestand";
 theUILang.errorWriteTorrent		= "Fout bij schrijven van het torrent bestand";
 theUILang.errorReadTorrent		= "Fout bij het lezen van het torrent bestand";
 theUILang.cantFindTorrent		= "Bron torrent-bestand voor deze download niet gevonden."

thePlugins.get("edit").langLoaded();


text:
package main // import "github.com/janeczku/go-dnsmasq"

import (
	"fmt"
	"log/syslog"
	"net"
	"os"
	"os/signal"
	"runtime"
	"strconv"
	"strings"
	"syscall"
	"time"

	log "github.com/Sirupsen/logrus"
	logrus_syslog "github.com/Sirupsen/logrus/hooks/syslog"
	"github.com/codegangsta/cli"
	"github.com/miekg/dns"

	"github.com/janeczku/go-dnsmasq/hostsfile"
	"github.com/janeczku/go-dnsmasq/resolvconf"
	"github.com/janeczku/go-dnsmasq/server"
	"github.com/janeczku/go-dnsmasq/stats"
)

// set at build time
var Version = "dev"

var (
	nameservers   = []string{}
	searchDomains = []string{}
	hostPort      = ""
	listen        = ""
)

var exitErr error

func init() {
	log.SetOutput(os.Stdout)
}

func main() {
	app := cli.NewApp()
	app.Name = "go-dnsmasq"
	app.Usage = "Lightweight caching DNS server and forwarder\n   Website: http://github.com/janeczku/go-dnsmasq"
	app.UsageText = "go-dnsmasq [global options]"
	app.Version = Version
	app.Author, app.Email = "", ""
	app.Flags = []cli.Flag{
		cli.StringFlag{
			Name:   "listen, l",
			Value:  "127.0.0.1:53",
			Usage:  "Listen on this `address` <host[:port]>",
			EnvVar: "DNSMASQ_LISTEN",
		},
		cli.BoolFlag{
			Name:   "default-resolver, d",
			Usage:  "Update /etc/resolv.conf with the address of go-dnsmasq as nameserver",
			EnvVar: "DNSMASQ_DEFAULT",
		},
		cli.StringFlag{
			Name:   "nameservers, n",
			Value:  "",
			Usage:  "Comma delimited list of `nameservers` <host[:port][,host[:port]]> (supersedes resolv.conf)",
			EnvVar: "DNSMASQ_SERVERS",
		},
		cli.StringSliceFlag{
			Name:   "stubzones, z",
			Usage:  "Use different nameservers for given domains <domain[,domain]/host[:port][,host[:port]]>",
			EnvVar: "DNSMASQ_STUB",
		},
		cli.StringFlag{
			Name:   "hostsfile, f",
			Value:  "",
			Usage:  "Path to a hosts `file` (e.g. /etc/hosts)",
			EnvVar: "DNSMASQ_HOSTSFILE",
		},
		cli.IntFlag{
			Name:   "hostsfile-poll, p",
			Value:  0,
			Usage:  "How frequently to poll hosts file (`seconds`, '0' to disable)",
			EnvVar: "DNSMASQ_POLL",
		},
		cli.StringFlag{
			Name:   "search-domains, s",
			Value:  "",
			Usage:  "List of search domains <domain[,domain]> (supersedes resolv.conf)",
			EnvVar: "DNSMASQ_SEARCH_DOMAINS,DNSMASQ_SEARCH,", // deprecated DNSMASQ_SEARCH
		},
		cli.BoolFlag{ // deprecated
			Name:   "append-search-domains, a",
			Usage:  "Resolve queries using search domains",
			EnvVar: "DNSMASQ_APPEND",
			Hidden: true,
		},
		cli.BoolFlag{
			Name:   "enable-search, search",
			Usage:  "Qualify names with search domains to resolve queries",
			EnvVar: "DNSMASQ_ENABLE_SEARCH",
		},
		cli.IntFlag{
			Name:   "rcache, r",
			Value:  0,
			Usage:  "Response cache `capacity` ('0' disables caching)",
			EnvVar: "DNSMASQ_RCACHE",
		},
		cli.IntFlag{
			Name:   "rcache-ttl",
			Value:  60,
			Usage:  "TTL in `seconds` for response cache entries",
			EnvVar: "DNSMASQ_RCACHE_TTL",
		},
		cli.BoolFlag{
			Name:   "no-rec",
			Usage:  "Disable recursion",
			EnvVar: "DNSMASQ_NOREC",
		},
		cli.IntFlag{
			Name:   "fwd-ndots",
			Value:  0,
			Usage:  "Number of `dots` a name must have before the query is forwarded",
			EnvVar: "DNSMASQ_FWD_NDOTS",
		},
		cli.IntFlag{
			Name:   "ndots",
			Value:  1,
			Usage:  "Number of `dots` a name must have before doing an initial absolute query (supersedes resolv.conf)",
			EnvVar: "DNSMASQ_NDOTS",
		},
		cli.BoolFlag{
			Name:   "round-robin",
			Usage:  "Enable round robin of A/AAAA records",
			EnvVar: "DNSMASQ_RR",
		},
		cli.BoolFlag{
			Name:   "systemd",
			Usage:  "Bind to socket activated by Systemd (supersedes '--listen')",
			EnvVar: "DNSMASQ_SYSTEMD",
		},
		cli.BoolFlag{
			Name:   "verbose",
			Usage:  "Enable verbose logging",
			EnvVar: "DNSMASQ_VERBOSE",
		},
		cli.BoolFlag{
			Name:   "syslog",
			Usage:  "Enable syslog logging",
			EnvVar: "DNSMASQ_SYSLOG",
		},
		cli.BoolFlag{
			Name:   "multithreading",
			Usage:  "Enable multithreading (experimental)",
			EnvVar: "DNSMASQ_MULTITHREADING",
		},
	}
	app.Action = func(c *cli.Context) error {
		exitReason := make(chan error)
		go func() {
			c := make(chan os.Signal, 1)
			signal.Notify(c, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
			sig := <-c
			log.Infoln("Application exit requested by signal:", sig)
			exitReason <- nil
		}()

		var enableSearch bool
		if c.IsSet("append-search-domains") {
			log.Info("The flag '--append-search-domains' is deprecated. Please use '--enable-search' or '-search' instead.")
			enableSearch = c.Bool("append-search-domains")
		} else {
			enableSearch = c.Bool("enable-search")
		}

		if c.Bool("multithreading") {
			runtime.GOMAXPROCS(runtime.NumCPU() + 1)
		}

		if c.Bool("verbose") {
			log.SetLevel(log.DebugLevel)
		}

		if c.Bool("syslog") {
			log.SetFormatter(&log.TextFormatter{DisableTimestamp: true, DisableColors: true})
			hook, err := logrus_syslog.NewSyslogHook("", "", syslog.LOG_DAEMON|syslog.LOG_INFO, "go-dnsmasq")
			if err != nil {
				log.Error("Unable to connect to local syslog daemon")
			} else {
				log.AddHook(hook)
			}
		} else {
			log.SetFormatter(&log.TextFormatter{})
		}

		if ns := c.String("nameservers"); ns != "" {
			for _, hostPort := range strings.Split(ns, ",") {
				hostPort = strings.TrimSpace(hostPort)
				if strings.HasSuffix(hostPort, "]") {
					hostPort += ":53"
				} else if !strings.Contains(hostPort, ":") {
					hostPort += ":53"
				}
				if err := validateHostPort(hostPort); err != nil {
					log.Fatalf("Nameserver is invalid: %s", err)
				}

				nameservers = append(nameservers, hostPort)
			}
		}

		if sd := c.String("search-domains"); sd != "" {
			for _, domain := range strings.Split(sd, ",") {
				if dns.CountLabel(domain) < 2 {
					log.Fatalf("Search domain must have at least one dot in name: %s", domain)
				}
				domain = strings.TrimSpace(domain)
				domain = dns.Fqdn(strings.ToLower(domain))
				searchDomains = append(searchDomains, domain)
			}
		}

		listen = c.String("listen")
		if strings.HasSuffix(listen, "]") {
			listen += ":53"
		} else if !strings.Contains(listen, ":") {
			listen += ":53"
		}

		if err := validateHostPort(listen); err != nil {
			log.Fatalf("Listen address is invalid: %s", err)
		}

		config := &server.Config{
			DnsAddr:         listen,
			DefaultResolver: c.Bool("default-resolver"),
			Nameservers:     nameservers,
			Systemd:         c.Bool("systemd"),
			SearchDomains:   searchDomains,
			EnableSearch:    enableSearch,
			Hostsfile:       c.String("hostsfile"),
			PollInterval:    c.Int("hostsfile-poll"),
			RoundRobin:      c.Bool("round-robin"),
			NoRec:           c.Bool("no-rec"),
			FwdNdots:        c.Int("fwd-ndots"),
			Ndots:           c.Int("ndots"),
			ReadTimeout:     2 * time.Second,
			RCache:          c.Int("rcache"),
			RCacheTtl:       c.Int("rcache-ttl"),
			Verbose:         c.Bool("verbose"),
		}

		resolvconf.Clean()
		if err := server.ResolvConf(config, c); err != nil {
			if !os.IsNotExist(err) {
				log.Warnf("Error parsing resolv.conf: %s", err.Error())
			}
		}

		if err := server.CheckConfig(config); err != nil {
			log.Fatal(err.Error())
		}

		if stubzones := c.StringSlice("stubzones"); len(stubzones) > 0 {
			stubmap := make(map[string][]string)
			for _, stubzone := range stubzones {
				segments := strings.Split(stubzone, "/")
				if len(segments) != 2 || len(segments[0]) == 0 || len(segments[1]) == 0 {
					log.Fatalf("Invalid value for --stubzones")
				}

				hosts := strings.Split(segments[1], ",")
				for _, hostPort := range hosts {
					hostPort = strings.TrimSpace(hostPort)
					if strings.HasSuffix(hostPort, "]") {
						hostPort += ":53"
					} else if !strings.Contains(hostPort, ":") {
						hostPort += ":53"
					}

					if err := validateHostPort(hostPort); err != nil {
						log.Fatalf("Stubzone server address is invalid: %s", err)
					}

					for _, sdomain := range strings.Split(segments[0], ",") {
						if dns.CountLabel(sdomain) < 1 {
							log.Fatalf("Stubzone domain is not a fully-qualified domain name: %s", sdomain)
						}
						sdomain = strings.TrimSpace(sdomain)
						sdomain = dns.Fqdn(sdomain)
						stubmap[sdomain] = append(stubmap[sdomain], hostPort)
					}
				}
			}
			config.Stub = &stubmap
		}

		log.Infof("Starting go-dnsmasq server %s", Version)
		log.Infof("Nameservers: %v", config.Nameservers)
		if config.EnableSearch {
			log.Infof("Search domains: %v", config.SearchDomains)
		}

		hf, err := hosts.NewHostsfile(config.Hostsfile, &hosts.Config{
			Poll:    config.PollInterval,
			Verbose: config.Verbose,
		})
		if err != nil {
			log.Fatalf("Error loading hostsfile: %s", err)
		}

		s := server.New(hf, config, Version)

		defer s.Stop()

		stats.Collect()

		if config.DefaultResolver {
			address, _, _ := net.SplitHostPort(config.DnsAddr)
			err := resolvconf.StoreAddress(address)
			if err != nil {
				log.Warnf("Failed to register as default nameserver: %s", err)
			}

			defer func() {
				log.Info("Restoring /etc/resolv.conf")
				resolvconf.Clean()
			}()
		}

		go func() {
			if err := s.Run(); err != nil {
				exitReason <- err
			}
		}()

		exitErr = <-exitReason
		if exitErr != nil {
			log.Fatalf("Server error: %s", err)
		}

		return nil
	}

	app.Run(os.Args)
}

func validateHostPort(hostPort string) error {
	host, port, err := net.SplitHostPort(hostPort)
	if err != nil {
		return err
	}
	if ip := net.ParseIP(host); ip == nil {
		return fmt.Errorf("Bad IP address: %s", host)
	}

	if p, _ := strconv.Atoi(port); p < 1 || p > 65535 {
		return fmt.Errorf("Bad port number %s", port)
	}
	return nil
}


text:
#include <string.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <sys/time.h>
#include <assert.h>
#include "filter.h"

#ifdef DARWIN
#define __MAC_10_8 1080
#include <Availability.h>
#if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_8
#define CONST_DIRENT_T struct dirent
#else
#define CONST_DIRENT_T const struct dirent
#endif
#else
#define CONST_DIRENT_T const struct dirent
#endif

/*
 * Generates the folder name, given a filter name.
 */
static const char* FILTER_FOLDER_NAME = "bloomd.%s";

/**
 * Format for the data file names.
 */
static const char* DATA_FILE_NAME = "data.%03d.mmap";

/*
 * Generates the config file name
 */
static const char* CONFIG_FILENAME = "config.ini";

/*
 * Static delarations
 */
static int thread_safe_fault(bloom_filter *f);
static int discover_existing_filters(bloom_filter *f);
static int create_sbf(bloom_filter *f, int num, bloom_bloomfilter **filters);
static int bloomf_sbf_callback(void* in, uint64_t bytes, bloom_bitmap *out);
static int timediff_msec(struct timeval *t1, struct timeval *t2);

static int filter_out_special(CONST_DIRENT_T *d);

/**
 * Initializes a bloom filter wrapper.
 * @arg config The configuration to use
 * @arg filter_name The name of the filter
 * @arg discover Should existing data files be discovered. Otherwise
 * they will be faulted in on-demand.
 * @arg filter Output parameter, the new filter
 * @return 0 on success
 */
int init_bloom_filter(bloom_config *config, char *filter_name, int discover, bloom_filter **filter) {
    // Allocate the buffers
    bloom_filter *f = *filter = calloc(1, sizeof(bloom_filter));

    // Store the things
    f->config = config;
    f->filter_name = strdup(filter_name);

    // Copy filter configs
    f->filter_config.initial_capacity = config->initial_capacity;
    f->filter_config.capacity = config->initial_capacity;
    f->filter_config.default_probability = config->default_probability;
    f->filter_config.scale_size = config->scale_size;
    f->filter_config.probability_reduction = config->probability_reduction;
    f->filter_config.in_memory = config->in_memory;

    // Get the folder name
    char *folder_name = NULL;
    int res;
    res = asprintf(&folder_name, FILTER_FOLDER_NAME, f->filter_name);
    assert(res != -1);

    // Compute the full path
    f->full_path = join_path(config->data_dir, folder_name);
    free(folder_name);

    // Initialize the locks
    INIT_BLOOM_SPIN(&f->counter_lock);
    pthread_mutex_init(&f->sbf_lock, NULL);

    // Try to create the folder path
    res = mkdir(f->full_path, 0755);
    if (res && errno != EEXIST) {
        syslog(LOG_ERR, "Failed to create filter directory '%s'. Err: %d [%d]", f->full_path, res, errno);
        return res;
    }

    // Read in the filter_config
    char *config_name = join_path(f->full_path, (char*)CONFIG_FILENAME);
    res = filter_config_from_filename(config_name, &f->filter_config);
    free(config_name);
    if (res && res != -ENOENT) {
        syslog(LOG_ERR, "Failed to read filter '%s' configuration. Err: %d [%d]", f->filter_name, res, errno);
        return res;
    }

    // Discover the existing filters if we need to
    res = 0;
    if (discover) {
        res = thread_safe_fault(f);
        if (res) {
            syslog(LOG_ERR, "Failed to fault in the filter '%s'. Err: %d", f->filter_name, res);
        }
    }

    // Trigger a flush on first instantiation. This will create
    // a new ini file for first time filters.
    if (!res) {
        res = bloomf_flush(f);
    }

    return res;
}

/**
 * Destroys a bloom filter
 * @arg filter The filter to destroy
 * @return 0 on success
 */
int destroy_bloom_filter(bloom_filter *filter) {
    // Close first
    bloomf_close(filter);

    // Cleanup
    free(filter->filter_name);
    free(filter->full_path);
    free(filter);
    return 0;
}

/**
 * Gets the counters that belong to a filter
 * @notes Thread safe, but may be inconsistent.
 * @arg filter The filter
 * @return A reference to the counters of a filter
 */
filter_counters* bloomf_counters(bloom_filter *filter) {
    return &filter->counters;
}

/**
 * Checks if a filter is currectly mapped into
 * memory or if it is proxied.
 * @notes Thread safe.
 * @return 0 if in-memory, 1 if proxied.
 */
int bloomf_is_proxied(bloom_filter *filter) {
    return !(filter->sbf);
}

/**
 * Flushes the filter. Idempotent if the
 * filter is proxied or not dirty.
 * @arg filter The filter to close
 * @return 0 on success.
 */
int bloomf_flush(bloom_filter *filter) {
    // Only do things if we are non-proxied
    if (filter->sbf) {
        // Time how long this takes
        struct timeval start, end;
        gettimeofday(&start, NULL);

        // If our size has not changed, there is no need to flush
        uint64_t new_size = bloomf_size(filter);
        if (new_size == filter->filter_config.size && filter->filter_config.bytes != 0) {
            return 0;
        }

        // Store our properties for a future unmap
        filter->filter_config.size = new_size;
        filter->filter_config.capacity = bloomf_capacity(filter);
        filter->filter_config.bytes = bloomf_byte_size(filter);

        // Write out filter_config
        char *config_name = join_path(filter->full_path, (char*)CONFIG_FILENAME);
        int res = update_filename_from_filter_config(config_name, &filter->filter_config);
        free(config_name);
        if (res) {
            syslog(LOG_ERR, "Failed to write filter '%s' configuration. Err: %d.",
                    filter->filter_name, res);
        }

        // Flush the filter
        res = 0;
        if (!filter->filter_config.in_memory) {
            res = sbf_flush((bloom_sbf*)filter->sbf);
        }

        // Compute the elapsed time
        gettimeofday(&end, NULL);
        syslog(LOG_INFO, "Flushed filter '%s'. Total time: %d msec.",
                filter->filter_name, timediff_msec(&start, &end));
        return res;
    }
    return 0;
}

/**
 * Gracefully closes a bloom filter.
 * @arg filter The filter to close
 * @return 0 on success.
 */
int bloomf_close(bloom_filter *filter) {
    // Acquire lock
    pthread_mutex_lock(&filter->sbf_lock);

    // Only act if we are non-proxied
    if (filter->sbf) {
        bloomf_flush(filter);

        bloom_sbf *sbf = (bloom_sbf*)filter->sbf;
        filter->sbf = NULL;

        sbf_close(sbf);
        free(sbf);

        filter->counters.page_outs += 1;
    }

    // Release lock
    pthread_mutex_unlock(&filter->sbf_lock);
    return 0;
}

/**
 * Deletes the bloom filter with
 * extreme prejudice.
 * @arg filter The filter to delete
 * @return 0 on success.
 */
int bloomf_delete(bloom_filter *filter) {
    // Close first
    bloomf_close(filter);

    // Delete the files
    struct dirent **namelist = NULL;
    int num;

    // Filter only data dirs, in sorted order
    num = scandir(filter->full_path, &namelist, filter_out_special, NULL);
    syslog(LOG_INFO, "Deleting %d files for filter %s.", num, filter->filter_name);

    // Free the memory associated with scandir
    for (int i=0; i < num; i++) {
        char *file_path = join_path(filter->full_path, namelist[i]->d_name);
        syslog(LOG_INFO, "Deleting: %s.", file_path);
        if (unlink(file_path)) {
            syslog(LOG_ERR, "Failed to delete: %s. %s", file_path, strerror(errno));
        }
        free(file_path);
    }

    // Free the memory associated with scandir
    for (int i=0; i < num; i++) {
        free(namelist[i]);
    }
    if (namelist)
        free(namelist);

    // Delete the directory
    if (rmdir(filter->full_path)) {
        syslog(LOG_ERR, "Failed to delete: %s. %s", filter->full_path, strerror(errno));
    }

    return 0;
}

/**
 * Checks if the filter contains a given key
 * @note Thread safe, as long as bloomf_add is not invoked.
 * @arg filter The filter to check
 * @arg key The key to check
 * @return 0 if not contained, 1 if contained.
 */
int bloomf_contains(bloom_filter *filter, char *key) {
    if (!filter->sbf) {
        if (thread_safe_fault(filter) != 0) return -1;
    }

    // Check the SBF
    int res = sbf_contains((bloom_sbf*)filter->sbf, key);

    // Safely update the counters
    LOCK_BLOOM_SPIN(&filter->counter_lock);
    if (res == 1)
        filter->counters.check_hits += 1;
    else if (res == 0)
        filter->counters.check_misses += 1;
    UNLOCK_BLOOM_SPIN(&filter->counter_lock);

    return res;
}

/**
 * Adds a key to the given filter
 * @arg filter The filter to add to
 * @arg key The key to add
 * @return 0 if not added, 1 if added.
 */
int bloomf_add(bloom_filter *filter, char *key) {
    if (!filter->sbf) {
        if (thread_safe_fault(filter) != 0) return -1;
    }

    // Add the SBF
    int res = sbf_add((bloom_sbf*)filter->sbf, key);

    // Safely update the counters
    LOCK_BLOOM_SPIN(&filter->counter_lock);
    if (res == 1)
        filter->counters.set_hits += 1;
    else if (res == 0)
        filter->counters.set_misses += 1;
    UNLOCK_BLOOM_SPIN(&filter->counter_lock);

    return res;
}

/**
 * Gets the size of the filter in keys
 * @note Thread safe.
 * @arg filter The filter to check
 * @return The total size of the filter
 */
uint64_t bloomf_size(bloom_filter *filter) {
    if (filter->sbf) {
        return sbf_size((bloom_sbf*)filter->sbf);
    } else {
        return filter->filter_config.size;
    }
}

/**
 * Gets the maximum capacity of the filter
 * @note Thread safe.
 * @arg filter The filter to check
 * @return The total capacity of the filter
 */
uint64_t bloomf_capacity(bloom_filter *filter) {
    if (filter->sbf) {
        return sbf_total_capacity((bloom_sbf*)filter->sbf);
    } else {
        return filter->filter_config.capacity;
    }
}

/**
 * Gets the current byte size of the filter
 * @note Thread safe.
 * @arg filter The filter
 * @return The total byte size of the filter
 */
uint64_t bloomf_byte_size(bloom_filter *filter) {
    if (filter->sbf) {
        return sbf_total_byte_size((bloom_sbf*)filter->sbf);
    } else {
        return filter->filter_config.bytes;
    }
}

/**
 * Provides a thread safe faulting of filters.
 * The main use case of this is to allow
 * bloomf_contains to be safe.
 */
static int thread_safe_fault(bloom_filter *f) {
    // Acquire lock
    pthread_mutex_lock(&f->sbf_lock);

    int res = 0;
    if (!f->sbf) {
        if (f->filter_config.in_memory) {
            res = create_sbf(f, 0, NULL);
        } else {
            res = discover_existing_filters(f);
        }
    }

    // Release lock
    pthread_mutex_unlock(&f->sbf_lock);
    return res;
}

/**
 * Discovers existing filters, and faults them in.
 */
static uint64_t get_size(char* filename) {
    struct stat buf;
    buf.st_size = 0;
    stat(filename, &buf);
    return buf.st_size;
}

/**
 * Works with scandir to filter out special files
 */
static int filter_out_special(CONST_DIRENT_T *d) {
    // Get the file name
    char *name = (char*)d->d_name;

    // Make sure its not speci
    if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
        return 0;
    }
    return 1;
}

/**
 * Works with scandir to filter out non-data files.
 */
static int filter_data_files(CONST_DIRENT_T *d) {
    // Get the file name
    char *name = (char*)d->d_name;

    // Look if it ends in ".data"
    int name_len = strlen(name);

    // Too short
    if (name_len < 6) return 0;

    // Compare the ending
    if (strcmp(name+(name_len-5), ".mmap") == 0) {
        return 1;
    }

    // Do not store
    return 0;
}

/**
 * This beast mode method scans the data directory
 * belonging to this filter for any existing filters,
 * and restores the SBF
 * @return 0 on success. -1 on error.
 */
static int discover_existing_filters(bloom_filter *f) {
    // Scan through the folder looking for data files
    struct dirent **namelist;
    int num;

    // Filter only data dirs, in sorted order
    num = scandir(f->full_path, &namelist, filter_data_files, alphasort);
    if (num == -1) {
        syslog(LOG_ERR, "Failed to scan files for filter '%s'. %s",
                f->filter_name, strerror(errno));
        return -1;
    }
    syslog(LOG_INFO, "Found %d files for filter %s.", num, f->filter_name);

    // Speical case when there are no filters
    if (num == 0) {
        int res = create_sbf(f, 0, NULL);
        return res;
    }

    // Allocate space for all the filter
    bloom_bitmap **maps = malloc(num * sizeof(bloom_bitmap*));
    bloom_bloomfilter **filters = malloc(num * sizeof(bloom_bloomfilter*));

    // Initialize the bitmaps and bloom filters
    int res;
    int err = 0;
    uint64_t size;
    bitmap_mode mode = (f->config->use_mmap) ? SHARED : PERSISTENT;
    for (int i=0; i < num && !err; i++) {
        // Get the full path to the bitmap
        char *bitmap_path = join_path(f->full_path, namelist[i]->d_name);
        syslog(LOG_INFO, "Discovered bloom filter: %s.", bitmap_path);

        // Get the size
        size = get_size(bitmap_path);
        if (size == 0) {
            err = 1;
            syslog(LOG_ERR, "Failed to get the filesize for: %s. %s", bitmap_path, strerror(errno));
            free(bitmap_path);
            break;
        }

        // Create the bitmap
        bloom_bitmap *bitmap = maps[num - i - 1] = malloc(sizeof(bloom_bitmap));
        res = bitmap_from_filename(bitmap_path, size, 0, mode, bitmap);
        if (res != 0) {
            err = 1;
            syslog(LOG_ERR, "Failed to load bitmap for: %s. %s", bitmap_path, strerror(errno));
            free(bitmap);
            free(bitmap_path);
            break;
        }

        // Create the bloom filter
        bloom_bloomfilter *filter = filters[num - i - 1] = malloc(sizeof(bloom_bloomfilter));
        res = bf_from_bitmap(bitmap, 1, 0, filter);
        if (res != 0) {
            err = 1;
            syslog(LOG_ERR, "Failed to load bloom filter for: %s. [%d]", bitmap_path, res);
            free(filter);
            bitmap_close(bitmap);
            free(bitmap);
            free(bitmap_path);
            break;
        }

        // Cleanup
        free(bitmap_path);
    }

    // Free the memory associated with scandir
    for (int i=0; i < num; i++) free(namelist[i]);
    free(namelist);

    // Return if there was an error
    if (err) return -1;

    // Create the SBF
    res = create_sbf(f, num, filters);

    // Cleanup on err
    if (res != 0) {
        syslog(LOG_ERR, "Failed to make scalable bloom filter for: %s.", f->filter_name);

        // For fucks sake. We need to clean up so much shit now.
        for (int i=0; i < num; i++) {
            bf_close(filters[i]);
            bitmap_close(maps[i]);
            free(filters[i]);
            free(maps[i]);
        }
    }

    // Increase our page ins
    f->counters.page_ins += 1;

    // Remove the filters list
    free(maps);
    free(filters);
    return (err) ? -1 : 0;
}

/**
 * Internal method to create the SBF
 */
static int create_sbf(bloom_filter *f, int num, bloom_bloomfilter **filters) {
    // Setup the SBF params
    bloom_sbf_params params = {
        f->filter_config.initial_capacity,
        f->filter_config.default_probability,
        f->filter_config.scale_size,
        f->filter_config.probability_reduction
    };

    // Create the SBF
    f->sbf = malloc(sizeof(bloom_sbf));
    int res = sbf_from_filters(&params, bloomf_sbf_callback, f, num, filters, (bloom_sbf*)f->sbf);

    // Handle a failure
    if (res != 0) {
        syslog(LOG_ERR, "Failed to create SBF: %s. Err: %d", f->filter_name, res);
        free((bloom_sbf*)f->sbf);
        f->sbf = NULL;
    } else {
        syslog(LOG_INFO, "Loaded SBF: %s. Num filters: %d.", f->filter_name, num);
    }

    return res;
}

/**
 * Callback used with SBF to generate file names.
 */
static int bloomf_sbf_callback(void* in, uint64_t bytes, bloom_bitmap *out) {
    // Cast the input pointer
    bloom_filter *filt = in;

    // Check if we are in-memory
    if (filt->filter_config.in_memory) {
        syslog(LOG_INFO, "Creating new in-memory bitmap for filter %s. Size: %llu",
            filt->filter_name, (unsigned long long)bytes);
        return bitmap_from_file(-1, bytes, ANONYMOUS, out);
    }

    // Scan through the folder looking for data files
    struct dirent **namelist = NULL;
    int num_files;

    // Filter only data dirs, in sorted order
    num_files = scandir(filt->full_path, &namelist, filter_data_files, NULL);
    syslog(LOG_INFO, "Found %d files for filter %s.", num_files, filt->filter_name);
    if (num_files < 0) {
        syslog(LOG_ERR, "Error discovering files for filter '%s'. %s",
                filt->filter_name, strerror(errno));
        return -1;
    }

    // Free the memory associated with scandir
    for (int i=0; i < num_files; i++) {
        free(namelist[i]);
    }
    if (namelist) free(namelist);

    // Generate the new file name
    char *filename = NULL;
    int file_name_len;
    file_name_len = asprintf(&filename, DATA_FILE_NAME, num_files);
    assert(file_name_len != -1);

    // Get the full path
    char *full_path = join_path(filt->full_path, filename);
    free(filename);
    syslog(LOG_INFO, "Creating new file: %s for filter %s. Size: %llu",
            full_path, filt->filter_name, (unsigned long long)bytes);

    // Create the bitmap
    bitmap_mode mode = (filt->config->use_mmap) ? SHARED : PERSISTENT;
    int res = bitmap_from_filename(full_path, bytes, 1, mode, out);
    if (res) {
        syslog(LOG_CRIT, "Failed to create new file: %s for filter %s. Err: %s",
            full_path, filt->filter_name, strerror(errno));
    }
    free(full_path);
    return res;
}

/**
 * Computes the difference in time in milliseconds
 * between two timeval structures.
 */
static int timediff_msec(struct timeval *t1, struct timeval *t2) {
    uint64_t micro1 = t1->tv_sec * 1000000 + t1->tv_usec;
    uint64_t micro2= t2->tv_sec * 1000000 + t2->tv_usec;
    return (micro2-micro1) / 1000;
}


text:
import moment from 'moment';

export default function *() {

	this.on('action.Events.fetchMyEvents', function *() {

		try {
			var res = yield this.request
				.get('/api/self/events')
				.query();

			var events = [];
			var data = res.body.events;
			for (var index in data) {
				var event = data[index];

				// Date type
				var m = moment(event.start);
				var dm = moment(event.deadline);

				var expired = false;
				if (moment().diff(m, 'days') > 0)
					expired = true;

				events.push({
					_id: event['_id'],
					name: event['name'],
					desc: event['desc'],
					location: event['loc'],
					address: event['address'],
					start: m.valueOf(),
					startdate: m.format('YYYY/MM/DD'),
					registration: event['registration'],
					website: event['website'],
					expired: expired,
					pos: event['geo'],
					quota: event['quota'],
					available: event['available'],
					deadline: dm.format('YYYY/MM/DD HH:mm')
				});
			}

			this.dispatch('action.Events.receiveMyEvents', events);
		} catch(e) {
			console.log(e);
		}
	});

	this.on('store.Events.fetchAll', function *() {

		try {
			var res = yield this.request
				.get('/api/events')
				.query();

			var events = [];

			var data = res.body.events;
			for (var index in data) {
				var event = data[index];

				// Date type
				var m = moment(event.start);
				var dm = moment(event.deadline);

				var expired = false;
				if (moment().diff(m, 'days') > 0)
					expired = true;

				events.push({
					_id: event['_id'],
					name: event['name'],
					desc: event['desc'],
					location: event['loc'],
					address: event['address'],
					start: m.valueOf(),
					startdate: m.format('YYYY/MM/DD'),
					registration: event['registration'],
					website: event['website'],
					expired: expired,
					pos: event['geo'],
					quota: event['quota'],
					available: event['available'],
					deadline: dm.format('YYYY/MM/DD HH:mm'),
				});
			}

			this.dispatch('action.Events.receiveAll', events);
		} catch(e) {
			console.log(e);
		}
	});
};


text:
<?php

class test_permission_model extends CodeIgniterUnitTestCase {

	private $tmp_tables = array("permissions","role_permissions","roles");
	private $pf = null; // database prefix
	private $mpf = null; // database + mock prefix

	//--------------------------------------------------------------------

	public function __construct()
	{
		parent::__construct();

		//load the models
		$this->ci->load->model('permissions/permission_model', 'permission_model', true);
		$this->ci->load->model('roles/role_model', 'role_model', true);
		$this->ci->load->model('roles/role_permission_model', 'role_permission_model', true);

		// use the temp tables, not production tables
		$this->ci->role_model->set_table('mock_roles');
		$this->ci->permission_model->set_table('mock_permissions');
		$this->ci->role_permission_model->set_table('mock_role_permissions');

		// set the database prefix and the mock prefix
		$this->pf = $this->ci->db->dbprefix;
		$this->mpf = $this->pf . 'mock_';
	}

	//--------------------------------------------------------------------

	public function test_create_tmp_tables()
	{
		foreach($this->tmp_tables as $table) {
			$this->assertTrue($this->ci->db->query("CREATE TEMPORARY TABLE IF NOT EXISTS ".$this->mpf.$table." SELECT * FROM ".$this->pf.$table), "Temporary Table ".$this->mpf.$table." created");
		}
	}

	//--------------------------------------------------------------------

	public function test_permission_model_is_loaded()
	{
		$this->assertTrue(class_exists('Permission_model',false), 'Permission Model is fully loaded');
	}

	//--------------------------------------------------------------------

	public function test_role_model_is_loaded()
	{
		$this->assertTrue(class_exists('Role_model',false), 'Role Model is fully loaded');
	}

	//--------------------------------------------------------------------

	public function test_role_permission_model_is_loaded()
	{
		$this->assertTrue(class_exists('Role_permission_model',false), 'Role Permission Model is fully loaded');
	}

	//--------------------------------------------------------------------

	public function test_update_returns_true_on_success_if_permission_status_is_inactive()
	{
		$role = $this->ci->role_model->find_all();
		$perm = $this->ci->permission_model->find_all();
		$data = array(
			'permission_id' => $perm[0]->permission_id,
			'status' => 'inactive'
		);
		$role_id = $role[0]->role_id;

		$this->assertTrue($this->ci->permission_model->update($role_id, $data), "Verified setting permission status to inactive");
	}

	//--------------------------------------------------------------------

	public function test_update_returns_true_on_success_if_permission_status_is_active()
	{
		$role = $this->ci->role_model->find_all();
		$perm = $this->ci->permission_model->find_all();
		$data = array(
			'permission_id' => $perm[0]->permission_id,
			'status' => 'active'
		);
		$role_id = $role[0]->role_id;

		$this->assertTrue($this->ci->permission_model->update($role_id, $data), "Verified setting permission status to active");
	}

	//--------------------------------------------------------------------

	/* this should be the last function to be sure all testing is finished with the temporary tables */
	public function test_remove_tmp_tables()
	{
		foreach($this->tmp_tables as $table) {
			$this->assertTrue($this->ci->db->query("DROP TABLE ".$this->mpf.$table), "Temporary Table ".$this->mpf.$table." deleted");
		}
	}

	//--------------------------------------------------------------------

}


text:
package org.globsframework.model.delta;

import org.globsframework.metamodel.Field;
import org.globsframework.metamodel.GlobType;
import org.globsframework.metamodel.fields.FieldValueVisitor;
import org.globsframework.model.FieldValue;
import org.globsframework.model.FieldValues;
import org.globsframework.model.impl.AbstractFieldValues;
import org.globsframework.utils.Unset;
import org.globsframework.utils.exceptions.ItemNotFound;

class DeltaFieldValuesFromArray extends AbstractFieldValues {
    private GlobType type;
    private Object[] values;

    public DeltaFieldValuesFromArray(GlobType type, Object[] values) {
        this.type = type;
        this.values = values;
    }

    public Object getValue(Field field) throws ItemNotFound {
        return doCheckedGet(field);
    }

    protected Object doCheckedGet(Field field) {
        Object value = values[field.getIndex()];
        if (value == Unset.VALUE) {
            throw new ItemNotFound(field.getName() + " not set.");
        }
        return value;
    }

    public boolean isSet(Field field) throws ItemNotFound {
        return values[field.getIndex()] != Unset.VALUE;
    }

    public boolean contains(Field field) {
        if (field.isKeyField()) {
            return false;
        }
        return values[field.getIndex()] != Unset.VALUE;
    }

    public int size() {
        int count = 0;
        for (Field field : type.getFields()) {
            if (!field.isKeyField() && values[field.getIndex()] != Unset.VALUE) {
                count++;
            }
        }
        return count;
    }

    public <T extends FieldValueVisitor> T accept(T functor) throws Exception {
        for (Field field : type.getFields()) {
            Object value = values[field.getIndex()];
            if (value != Unset.VALUE && !field.isKeyField()) {
                field.visit(functor, value);
            }
        }
        return functor;
    }

    public <T extends FieldValues.Functor>
    T apply(T functor) throws Exception {
        for (Field field : type.getFields()) {
            Object value = values[field.getIndex()];
            if (value != Unset.VALUE && !field.isKeyField()) {
                functor.process(field, value);
            }
        }
        return functor;
    }

    public FieldValue[] toArray() {
        FieldValue[] fieldValues = new FieldValue[size()];
        int i = 0;
        for (Field field : type.getFields()) {
            Object value = values[field.getIndex()];
            if (value != Unset.VALUE && !field.isKeyField()) {
                fieldValues[i] = new FieldValue(field, value);
                i++;
            }
        }
        return fieldValues;
    }

    public String toString() {
        StringBuilder builder = new StringBuilder();
        for (Field field : type.getFields()) {
            builder.append(field.getName()).append(":").append(values[field.getIndex()]).append(("\n"));
        }
        return builder.toString();
    }
}


text:
'use strict';

N.wire.on('io.error', function (err) {
  switch (err.code) {
  case N.io.INVALID_CSRF_TOKEN:
    N.runtime.csrf = err.data.token;
    N.wire.emit('notify', t('invalid_csrf_token'));
    break;

  case N.io.APP_ERROR:
    N.wire.emit('notify', t('application_fuckup'));
    break;

  case N.io.ECOMMUNICATION:
    N.wire.emit('notify', t('communication_timeout'));
    break;

  default:
    if (err.message) {
      N.wire.emit('notify', t('system_error_with_message', {
        code:    err.code
      , message: err.message
      }));
    } else {
      N.wire.emit('notify', t('system_error', { code: err.code }));
    }
    break;
  }
});


text:
package amberdb.model;

import static org.junit.Assert.*;

import java.io.IOException;
import java.util.*;

import amberdb.AbstractDatabaseIntegrationTest;
import org.apache.commons.collections.IteratorUtils;
import org.junit.Before;
import org.junit.Test;

import com.google.common.collect.Lists;
import com.tinkerpop.blueprints.Direction;

import amberdb.AmberSession;
import amberdb.InvalidSubtypeException;
import amberdb.NoSuchObjectException;
import amberdb.enums.BibLevel;
import amberdb.enums.CaptureStatus;
import amberdb.enums.ChecksumType;
import amberdb.enums.Compression;
import amberdb.enums.CopyRole;
import amberdb.enums.CopyrightPolicy;
import amberdb.enums.SubType;

import static org.hamcrest.CoreMatchers.*;

public class WorkTest extends AbstractDatabaseIntegrationTest {

    private Work workCollection;
    private Work bookBlinkyBill;
    private Work chapterBlinkyBill;
    private Page workFrontCover;
    private Page workTitlePage;
    private Map<String, Object> expectedResults = new HashMap<String, Object>();
    private int expectedNoOfPages = 0;
    private int expectedNoOfPagesForSection = 0;

    private Work journalNLAOH;

    @Before
    public void setup() throws IOException, InstantiationException {
        setTestDataInH2(amberSession);
    }

    @Test
    public void testLoadPagedWork() {
        try {
            journalNLAOH.loadPagedWork();
        } catch (Exception e) {
             System.out.println(e.getCause().getCause().toString());
        }

        List<String> subTypes = new ArrayList<String>();
        subTypes.add("page");
        List<Work> pages = journalNLAOH.getPartsOf(subTypes);
        if (pages != null) {
            for (Work page : pages) {
                System.out.println("page: " + page.getId());
            }
        }
        subTypes.remove("page");
        subTypes.add("article");
        List<Work> articles = journalNLAOH.getPartsOf(subTypes);
        if (articles != null) {
            for (Work article : articles) {
                System.out.println("article: " + article.getId());
            }
        }
    }

    @Test
    public void testGetLeafsForBlinkyBill() {
        Iterable<Work> leafs = bookBlinkyBill.getLeafs(SubType.PAGE);
        int noOfPages = 0;
        for (Work leaf : leafs) {
            noOfPages++;
        }
        assertEquals(expectedNoOfPages, noOfPages);
    }

    @Test
    public void testGetLeafsOfSubTypesForBlinkyBill() {

        Long start = new Date().getTime();

        // try loading the book first
        try {
            bookBlinkyBill.loadPagedWork();
        } catch (InvalidSubtypeException e) {
            e.printStackTrace();
        }

        List<Work> leaves = bookBlinkyBill.getPartsOf(Arrays.asList(new String[] {SubType.PAGE.code(), SubType.CHAPTER.code()}));

        int noOfLeaves = 0;
        for (Work leaf : leaves) {
            noOfLeaves++;
        }

        assertEquals(noOfLeaves, 3);

        // Check output bits
        leaves.get(0).getExistsOn(SubType.PAGE.code());
    }

    @Test
    public void testGetLeafsForBlinkyBillAssoication() {
        Iterable<Section> sections = bookBlinkyBill.getSections(SubType.CHAPTER);
        Section theNewArrival = null;
        for (Section section : sections) {
            if (section.getTitle().equalsIgnoreCase("The New Arrival"))
                theNewArrival = section;
        }
        Iterable<Work> leafs = theNewArrival.getLeafs(SubType.PAGE.code());
        int noOfPages = 0;
        assertEquals(expectedNoOfPagesForSection, noOfPages);
    }

    @Test
    public void testGetWorkAsSection() {
        Section theChapter = chapterBlinkyBill.asSection();
        assertEquals(chapterBlinkyBill.getId(), theChapter.getId());

        // Also verify bibId can be returned as a String
        assertEquals(bookBlinkyBill.getBibId().getClass().getName(), "java.lang.String");
    }

    @Test
    public void testGetSetParentEdges() throws IOException {
            Work work = amberSession.addWork();
            Work parentWork = amberSession.addWork();
            parentWork.addChild(work);
            assertTrue(null == parentWork.getParentEdge());
            assertEquals(parentWork, work.getParentEdge().getSource());
    }

    @Test
    public void testDeattachPage() {

        int expectedNoOfPagesBfrRemoval = 3;
        int expectedNoOfPagesAftRemoval = 2;

        int actualNoOfPagesBfrRemoval = 0;
        Iterable<Page> pagesBI = bookBlinkyBill.getPages();
        for (Page page : pagesBI) {
            actualNoOfPagesBfrRemoval++;
        }
        assertEquals(expectedNoOfPagesBfrRemoval, actualNoOfPagesBfrRemoval);
        bookBlinkyBill.removePage(workTitlePage);

        int actualNoOfPagesAftRemoval = 0;
        Iterable<Page> pagesAI = bookBlinkyBill.getPages();
        for (Page page : pagesAI) {
            actualNoOfPagesAftRemoval++;
        }
        assertEquals(expectedNoOfPagesAftRemoval, actualNoOfPagesAftRemoval);
        workTitlePage = bookBlinkyBill.addPage();
        workTitlePage.setSubType(SubType.PAGE.code());
        workTitlePage.setTitle("Blinky Bill Page 2");
        workTitlePage.setSubType(SubType.PAGE.code());
        workTitlePage.setSubUnitType("Title Page");
    }

    @Test
    public void testDeattachLeaf() {

        int expectedNoOfPagesBfrRemoval = 2;
        int expectedNoOfPagesAftRemoval = 1;

        int actualNoOfPagesBfrRemoval = 0;
        Iterable<Work> pagesBI = bookBlinkyBill.getLeafs(SubType.PAGE);
        for (Work page : pagesBI) {
            actualNoOfPagesBfrRemoval++;
        }
        assertEquals(expectedNoOfPagesBfrRemoval, actualNoOfPagesBfrRemoval);
        bookBlinkyBill.removePart(bookBlinkyBill.getLeaf(SubType.PAGE, 1));

        int actualNoOfPagesAftRemoval = 0;
        Iterable<Work> pagesAI = bookBlinkyBill.getLeafs(SubType.PAGE);
        for (Work page : pagesAI) {
            actualNoOfPagesAftRemoval++;
        }
        assertEquals(expectedNoOfPagesAftRemoval, actualNoOfPagesAftRemoval);
        workTitlePage = bookBlinkyBill.addPage();
        workTitlePage.setSubType(SubType.PAGE.code());
        workTitlePage.setTitle("Blinky Bill Page 2");
        workTitlePage.setSubType(SubType.PAGE.code());
        workTitlePage.setSubUnitType("Title Page");
    }

    @Test
    public void testCountCopies() {

        Work work = bookBlinkyBill;
        Page page = workTitlePage;

        /* parent work has no copies */
        assertEquals(work.countCopies(), 0);

        int counter = 0;
        Iterable<Copy> pageCopies = page.getCopies();
        for (Copy copy : pageCopies) {
            counter++;
        }
        /* we count it the same way the Work class does */
        assertEquals(page.countCopies(), counter);

        /* Check page _has_ copies.
           (otherwise this test is a waste of time that proves nothing */
        assertNotEquals(page.countCopies(), 0);
    }

    @Test(expected = NoSuchObjectException.class)
    public void testDeleteWork() {

        Work work = bookBlinkyBill;
        long workVertexId = work.getId();

        amberSession.deleteWork(work);

        /* expects a NoSuchObjectException */
        amberSession.findWork(workVertexId);
    }

    @Test
    public void testRepresentWork() {
        Work newWork = amberSession.addWork();
        Copy representativeCopy = workFrontCover.getCopy(CopyRole.MASTER_COPY);
        newWork.addRepresentation(representativeCopy);
        Iterable<Work> representedWorks = representativeCopy.getRepresentedWorks();
        assertNotNull(representedWorks);

        // assert representedWorks has at least one element
        Iterator<Work> representedIt = representedWorks.iterator();
        assertTrue(representedIt.hasNext());
        assertEquals(newWork, representedIt.next());

        // assert representedWorks has only one element
        assertFalse(representedIt.hasNext());
    }

    @Test
    public void testEmptyIterableNotNull() {

        Work work = amberSession.addWork();
        Copy c = work.addCopy();

        // test if problem with empty iterable
        for (Work w : c.getRepresentedWorks()) {
            // noop
        }
    }

    @Test
    public void testGetWorksRepresentedByCopiesOf() {
        Work newWork = amberSession.addWork();
        Copy representativeCopy = workFrontCover.getCopy(CopyRole.MASTER_COPY);
        newWork.addRepresentation(representativeCopy);
        Map<Long, Long> reps = amberSession.getWorksRepresentedByCopiesOf(workFrontCover);
        assertEquals((Long) reps.get(newWork.getId()), (Long) representativeCopy.getId());
    }

    @Test(expected = NoSuchObjectException.class)
    public void testDeleteWorkShouldNotDeleteCopyFromOtherWork() {
        // create new work and assign its representative copy
        Work newWork = amberSession.addWork();
        String newWorkId = newWork.getObjId();
        String workFrontCoverId = workFrontCover.getObjId();
        Copy representativeCopy = workFrontCover.getCopy(CopyRole.MASTER_COPY);
        newWork.addRepresentation(representativeCopy);

        // delete new work
        amberSession.deleteWork(newWork);
        amberSession.commit();

        // verify the representative copy still exist in amberSession
        Work foundWorkFrontCover = amberSession.findWork(workFrontCoverId);
        Copy afterDeleteCopy = foundWorkFrontCover.getCopy(CopyRole.MASTER_COPY);
        assertEquals(representativeCopy, afterDeleteCopy);

        // verify the new work is deleted through NoSuchObjectException thrown
        Work afterDeleteNewWork = amberSession.findWork(newWorkId);
    }

    @Test(expected = NoSuchObjectException.class)
    public void testDeletePage() {

        Page page = workTitlePage;
        long workVertexId = page.getId();
        assertEquals(workVertexId, page.asVertex().getId());
        List<Long> copyVertexIds = new ArrayList<Long>();
        List<Long> fileVertexIds = new ArrayList<Long>();
        Iterable<Copy> copies = page.getCopies();
        for (Copy copy : copies) {
            File file = copy.getFile();
            copyVertexIds.add(copy.getId());

            if (file != null) {
                fileVertexIds.add(file.getId());
                assertEquals(file.getId(), file.asVertex().getId());
            }
            assertEquals(copy.getId(), copy.asVertex().getId());
        }

        amberSession.deletePage(page);
        amberSession.findWork(workVertexId);
    }

    @Test
    public void testOrderChildren() {

        List<Work> pages = new ArrayList<>();

        Work book = amberSession.addWork();
        for (int i = 0; i < 20; i++) {
            Page page = book.addPage();
            page.setTitle("page " + i);
            pages.add(page); // pages List will hold the pages in order
        }

        // set order
        book.orderParts(pages);

        // check ordering worked
        for (int i = 0; i < 20; i++) {
            assertEquals(pages.get(i).getOrder(book, "isPartOf", Direction.OUT), (Integer) (i + 1));
        }

        // reverse order
        Collections.reverse(pages);
        book.orderParts(pages);

        // Check reordering worked
        for (int i = 0; i < 20; i++) {
            assertEquals(((Page) pages.get(i)).getTitle(), "page "+(19-i));
        }
    }

    @Test
    public void testAddDeliveryWork() {
        Work work = amberSession.addWork();

        Work deliveryWork = amberSession.addWork();
        work.addDeliveryWork(deliveryWork);

        Collection<Work> list = IteratorUtils.toList(work.getDeliveryWorks().iterator());

        assertFalse(list.isEmpty());
    }

    @Test
    public void testRemoveDeliveryWork() {
        Work work = amberSession.addWork();

        Work deliveryWork = amberSession.addWork();
        work.addDeliveryWork(deliveryWork);

        Collection<Work> list = IteratorUtils.toList(work.getDeliveryWorks().iterator());
        assertFalse(list.isEmpty());

        work.removeDeliveryWork(deliveryWork);

        list = IteratorUtils.toList(work.getDeliveryWorks().iterator());
        assertTrue(list.isEmpty());
    }

    @Test
    public void testRetrieveDeliveryWorkInterview() {
        Work work = amberSession.addWork();

        Work deliveryWork = amberSession.addWork();
        deliveryWork.setDeliveryWorkParent(work);
        work.addDeliveryWork(deliveryWork);

        assertTrue(deliveryWork.getDeliveryWorkParent() != null);
        assertTrue(deliveryWork.getDeliveryWorkParent().equals(work));
    }

    @Test
    public void testVendorIdWasSet() {
        assertEquals("101", bookBlinkyBill.getVendorId());
    }

    private void setTestDataInH2(AmberSession sess) {
        workCollection = sess.addWork();

        workCollection.setSubType("title");
        workCollection.setTitle("nla.books");

        journalNLAOH = sess.addWork();
        bookBlinkyBill = sess.addWork();
        bookBlinkyBill.setSubType(SubType.BOOK.code());
        bookBlinkyBill.setTitle("Blinky Bill");
        bookBlinkyBill.setBibId("73");
        bookBlinkyBill.setBibLevel(BibLevel.ITEM.code());
        bookBlinkyBill.setCopyrightPolicy(CopyrightPolicy.OUTOFCOPYRIGHT.code());
        bookBlinkyBill.setDigitalStatus(CaptureStatus.CAPTURED.code());
        bookBlinkyBill.setVendorId("101");
        workCollection.addChild(bookBlinkyBill);

        chapterBlinkyBill = sess.addWork();
        chapterBlinkyBill.setSubType(SubType.CHAPTER.code());
        chapterBlinkyBill.setTitle("The New Arrival");
        bookBlinkyBill.addChild(chapterBlinkyBill);

        workFrontCover = bookBlinkyBill.addPage();
        workTitlePage = bookBlinkyBill.addPage();
        workFrontCover.setSubType(SubType.PAGE.code());
        workFrontCover.setTitle("Blinky Bill Page 1");
        workFrontCover.setSubUnitType("Front Cover");
        workTitlePage.setSubType(SubType.PAGE.code());
        workTitlePage.setTitle("Blinky Bill Page 2");
        workTitlePage.setSubType(SubType.PAGE.code());
        workTitlePage.setSubUnitType("Title Page");

        Copy workFrontCoverMasterCopy = workFrontCover.addCopy();
        Copy workFrontCoverOCRJsonCopy = workFrontCover.addCopy();
        Copy workFrontCoverAccessCopy = workFrontCover.addCopy();
        workFrontCoverMasterCopy.setCopyRole(CopyRole.MASTER_COPY.code());
        workFrontCoverMasterCopy.setCarrier("Online");
        workFrontCoverOCRJsonCopy.setCopyRole(CopyRole.OCR_JSON_COPY.code());
        workFrontCoverOCRJsonCopy.setCarrier("Online");
        workFrontCoverAccessCopy.setCopyRole(CopyRole.ACCESS_COPY.code());
        workFrontCoverAccessCopy.setCarrier("Online");

        Copy workTitlePageMasterCopy = workTitlePage.addCopy();
        Copy workTitlePageOCRJsonCopy = workTitlePage.addCopy();
        Copy workTitlePageAccessCopy = workTitlePage.addCopy();
        workTitlePageMasterCopy.setCopyRole(CopyRole.MASTER_COPY.code());
        workTitlePageMasterCopy.setCarrier("Online");
        workTitlePageOCRJsonCopy.setCopyRole(CopyRole.OCR_JSON_COPY.code());
        workTitlePageOCRJsonCopy.setCarrier("Online");
        workTitlePageAccessCopy.setCopyRole(CopyRole.ACCESS_COPY.code());
        workTitlePageAccessCopy.setCarrier("Online");

        File workFrontCoverMasterCopyFile = workFrontCoverMasterCopy.addFile();
        File workFrontCoverOCRJsonCopyFile = workFrontCoverOCRJsonCopy.addFile();
        File workFrontCoverAccessCopyFile = workFrontCoverAccessCopy.addFile();
        workFrontCoverMasterCopyFile.setChecksumType(ChecksumType.SHA_1.code());
        workFrontCoverMasterCopyFile.setCompression(Compression.NONE.code());

        expectedResults.put("workFrontCover_getSubType", workFrontCover.getSubType());
        expectedResults.put("workFrontCover_getSubUnitType", workFrontCover.getSubUnitType());
        expectedResults.put("copy_carrier", workFrontCoverAccessCopy.getCarrier());
        expectedResults.put("workFrontCover_OCR_JSON_COPY_ID", workFrontCoverOCRJsonCopy.getId());
        expectedResults.put("workFrontCover_MASTER_COPY_ID", workFrontCoverMasterCopy.getId());
        expectedResults.put("workFrontCover_ACCESS_COPY_ID", workFrontCoverAccessCopy.getId());
        expectedResults.put("workFrontCover_OCR_JSON_COPY_FILE_ID", workFrontCoverOCRJsonCopyFile.getId());
        expectedResults.put("workFrontCover_MASTER_COPY_FILE_ID", workFrontCoverMasterCopyFile.getId());
        expectedResults.put("workFrontCover_ACCESS_COPY_FILE_ID", workFrontCoverAccessCopyFile.getId());

        expectedResults.put("workTitlePage_getSubType", workTitlePage.getSubType());
        expectedResults.put("workTitlePage_getSubUnitType", workTitlePage.getSubUnitType());
        expectedResults.put("workTitlePage_ACCESS_COPY_ID", workTitlePageAccessCopy.getId());

        expectedResults.put("workTitlePage_OCR_JSON_COPY_ID", workTitlePageOCRJsonCopy.getId());
        expectedResults.put("workTitlePage_MASTER_COPY_ID", workTitlePageMasterCopy.getId());

        expectedNoOfPages = 2;
        expectedNoOfPagesForSection = 0;
    }

    @Test
    public void testToEnsureDCMLegacyDataFieldsExist() throws IOException {
        Work work = amberSession.addWork();

        Date date = new Date();

        work.setDcmDateTimeCreated(date);
        assertEquals(date, work.getDcmDateTimeCreated());

        work.setDcmWorkPid("12345");
        assertEquals("12345", work.getDcmWorkPid());

        work.setDcmDateTimeUpdated(date);
        assertEquals(date, work.getDcmDateTimeUpdated());

        work.setDcmRecordCreator("creator");
        assertEquals("creator", work.getDcmRecordCreator());

        work.setDcmRecordUpdater("updater");
        assertEquals("updater", work.getDcmRecordUpdater());

        List<String> list = new ArrayList<String>();
        list.add("pi-1");
        list.add("pi-2");
        work.setDcmAltPi(list);

        assertEquals(2, work.getDcmAltPi().size());
        assertEquals(list, work.getDcmAltPi());
    }

    @Test
    public void testGetSetConstraint() throws IOException {
        Work work = amberSession.addWork();
        Set<String> constraints = new LinkedHashSet<>();
        assertEquals(0, work.getConstraint().size());
        assertFalse(constraints.contains("testingc"));
        constraints.add("testing");
        constraints.add("testinga");
        constraints.add("testingb");
        constraints.add("testingc");
        constraints.add("testingd");
        work.setConstraint(constraints);
        constraints = work.getConstraint();
        assertEquals(5, constraints.size());
        assertTrue(constraints.contains("testingc"));
        constraints.add("octopus");
        work.setConstraint(constraints);
        constraints = work.getConstraint();
        assertEquals(6, constraints.size());
        assertTrue(constraints.contains("octopus"));
    }

    @Test
    public void testSetOrder() {
        Work work = amberSession.addWork();
        work.setOrder(0);  // should not throw a NullPointerException

        Work child1 = amberSession.addWork();
        work.addChild(child1);
        Work child2 = amberSession.addWork();
        work.addChild(child2);

        child2.setOrder(0);
        child1.setOrder(1);

        Iterator<Work> it = work.getChildren().iterator();
        assertEquals(child2, it.next());
        assertEquals(child1, it.next());
    }

    @Test
    public void hasCopyRoleList(){
        Work work = amberSession.addWork();
        Copy copy1 = work.addCopy();
        copy1.setCopyRole(CopyRole.ACCESS_COPY.code());
        assertTrue(work.hasCopyRole(Arrays.asList(CopyRole.MASTER_COPY, CopyRole.ACCESS_COPY)));
        assertFalse(work.hasCopyRole(Arrays.asList(CopyRole.MASTER_COPY, CopyRole.ORIGINAL_COPY)));
    }

    @Test
    public void getFirstExistingCopy(){
        Work work = amberSession.addWork();
        Copy masterCopy = work.addCopy();
        masterCopy.setCopyRole(CopyRole.MASTER_COPY.code());
        Copy copy = work.getFirstExistingCopy(CopyRole.ACCESS_COPY, CopyRole.MASTER_COPY);
        assertThat(copy.getCopyRole(), is(CopyRole.MASTER_COPY.code()));
    }

    @Test
    public void getFirstExistingCopyWorkHasNoCopy(){
        Work work = amberSession.addWork();
        Copy copy = work.getFirstExistingCopy(CopyRole.ACCESS_COPY, CopyRole.MASTER_COPY);
        assertThat (copy, is(nullValue()));
    }

    @Test
    public void getFirstExistingNoCopy(){
        Work work = amberSession.addWork();
        Copy masterCopy = work.addCopy();
        masterCopy.setCopyRole(CopyRole.MASTER_COPY.code());
        Copy copy = work.getFirstExistingCopy(null);
        assertThat (copy, is(nullValue()));
    }

    @Test
    public void getFirstExistingCopyNoMatchingCopy(){
        Work work = amberSession.addWork();
        Copy masterCopy = work.addCopy();
        masterCopy.setCopyRole(CopyRole.MASTER_COPY.code());
        Copy copy = work.getFirstExistingCopy(CopyRole.ACCESS_COPY, CopyRole.ORIGINAL_COPY);
        assertThat (copy, is(nullValue()));
    }

    @Test
    public void removeCopies(){
        Work work = amberSession.addWork();
        Copy masterCopy = work.addCopy();
        masterCopy.setCopyRole(CopyRole.MASTER_COPY.code());
        Copy accessCopy1 = work.addCopy();
        accessCopy1.setCopyRole(CopyRole.ACCESS_COPY.code());
        Copy originalCopy = work.addCopy();
        originalCopy.setCopyRole(CopyRole.ORIGINAL_COPY.code());
        work.removeCopies(Arrays.asList(CopyRole.MASTER_COPY, CopyRole.ACCESS_COPY));
        List<Copy> copies = Lists.newArrayList(work.getCopies().iterator());
        assertThat (copies.size(), is (1));
        assertThat (copies.get(0).getCopyRole(), is(CopyRole.ORIGINAL_COPY.code()));
    }

}


text:
package org.terasology.launcher.util;

public final class DownloadException extends RuntimeException {

    private static final long serialVersionUID = -6597132435025903769L;

    public DownloadException() {
        super();
    }

    public DownloadException(String message) {
        super(message);
    }

    public DownloadException(String message, Throwable cause) {
        super(message, cause);
    }

    public DownloadException(Throwable cause) {
        super(cause);
    }
}


text:
/**
 *
 */
package org.corpus_tools.atomic.grideditor.data;

import org.corpus_tools.atomic.grideditor.data.annotationgrid.AnnotationGrid;
import org.corpus_tools.salt.common.SDocumentGraph;
import org.corpus_tools.salt.common.SSpan;
import org.corpus_tools.salt.common.SToken;
import org.corpus_tools.salt.core.SAnnotation;
import org.eclipse.nebula.widgets.nattable.data.IDataProvider;

/**
 * @author Stephan Druskat
 *
 * // TODO Add description
 *
 */
public class AnnotationGridDataProvider implements IDataProvider {

	private AnnotationGrid annotationGrid;

	public AnnotationGridDataProvider(AnnotationGrid annotationGrid) {
		this.annotationGrid = annotationGrid;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.nebula.widgets.nattable.data.IDataProvider#getDataValue(int, int)
	 */
	@Override
	public Object getDataValue(int columnIndex, int rowIndex) {
		return annotationGrid.get(rowIndex, columnIndex);
	}

//	private Object getColumnIndex() {
//		colKeySet = annotationGrid.columnKeySet()
//		return null;
//	}

	/* (non-Javadoc)
	 * @see org.eclipse.nebula.widgets.nattable.data.IDataProvider#setDataValue(int, int, java.lang.Object)
	 */
	@Override
	public void setDataValue(int columnIndex, int rowIndex, Object newValue) {
		Object previous = getDataValue(columnIndex, rowIndex);
		SDocumentGraph graph = annotationGrid.getGraph();
		if (previous instanceof SAnnotation) {
			((SAnnotation) previous).setValue(newValue);
			annotationGrid.record(rowIndex, columnIndex, annotationGrid.getColumnHeaderMap().get(columnIndex), previous);
		}
		else if (previous == null) {
			SToken token = graph.getSortedTokenByText().get(rowIndex);
			SSpan span = graph.createSpan(token);
			String namespace = null;
			String name = null;
			String[] headerSplit = annotationGrid.getColumnHeaderMap().get(columnIndex).split("::");
			if (headerSplit.length == 2) {
				namespace = headerSplit[0].equals("null") ? null : headerSplit[0];
				name = headerSplit[1];
			}
			else if (headerSplit.length == 1) {
				name = headerSplit[0];
			}
			SAnnotation newAnno = span.createAnnotation(namespace, name, newValue);
			annotationGrid.record(rowIndex, columnIndex, annotationGrid.getColumnHeaderMap().get(columnIndex), newAnno);
		}
		else {
			// Not null, not an SAnnotation
			throw new UnsupportedOperationException("Not supported yet!");
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.nebula.widgets.nattable.data.IDataProvider#getColumnCount()
	 */
	@Override
	public int getColumnCount() {
		return annotationGrid != null ? annotationGrid.getColumnHeaderMap().size() : 0;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.nebula.widgets.nattable.data.IDataProvider#getRowCount()
	 */
	@Override
	public int getRowCount() {
		return annotationGrid != null ? annotationGrid.getRowMap().size() : 0;
	}

//	/* (non-Javadoc)
//	 * @see org.eclipse.nebula.widgets.nattable.data.ISpanningDataProvider#getCellByPosition(int, int)
//	 */
//	@Override
//	public DataCell getCellByPosition(int columnPosition, int rowPosition) {
//		// TODO Auto-generated method stub
//		return null;
//	}

	/**
	 * @return the annotationGrid
	 */
	public AnnotationGrid getAnnotationTable() {
		return annotationGrid;
	}

	/**
	 * @param annotationGrid the annotationGrid to set
	 */
	public void setAnnotationTable(AnnotationGrid annotationTable) {
		this.annotationGrid = annotationTable;
	}

}


text:
from tinkerforge.bricklet_nfc_rfid import BrickletNFCRFID
from modules.navigation import StateModule

class RFIDModule(StateModule):
    readers = {}
    previous = None
    tick_count = 0
    tag_type = 0
    auth = "Nobody"

    users = {
        "411713624212177128": "Ben",
        "45014924212177128": "Mike",
        "41166424212177128": "Francesco",
        "19316121643": "Alvise",
    }

    def draw(self, clear=True):
        self.controller.screen.draw(
            "values",
            {"title": "Hello,", "value": self.auth, })

    def read_card(self, state, idle, nr):
        if idle:
            self.tag_type = (self.tag_type + 1) % 3
            nr.request_tag_id(self.tag_type)

        if state == nr.STATE_REQUEST_TAG_ID_READY:
            ret = nr.get_tag_id()
            card = str("".join(map(str, ret.tid[:ret.tid_length])))
            if card != self.previous:
                self.previous = card
                self.controller.event("rfid-read", [card])
                if card in self.users:
                    self.auth = self.users[card]
                else:
                    self.auth = "Unknown"
                self.controller.change_module("RFIDModule")

    def try_bricklet(self, uid, device_identifier, position):
        if device_identifier == 246:
            self.readers["card"] = {
                "instance": BrickletNFCRFID(uid, self.controller.ipcon),
                "name": "card",
                "relay": 0,
            }
            self.readers["card"]["instance"].register_callback(
                self.readers["card"]["instance"].CALLBACK_STATE_CHANGED,
                lambda x, y: self.read_card(
                    x, y,
                    self.readers["card"]["instance"]))
            self.readers["card"]["instance"].request_tag_id(
                self.readers["card"]["instance"].TAG_TYPE_MIFARE_CLASSIC)
            # print("Created Card Reader")

    def navigate(self, direction):
        self.controller.change_module("MenuModule")

    def tick(self):
        self.tick_count += 1
        if self.tick_count > 5:
            self.previous = None
            self.tick_count = 0
            self.controller.change_module(None)


text:
table, td {
	width: auto;
}
table.statistics, div.referee {
	display:none;
}
.ticker td.localtime {
	display:none;
}
.ticker td {
	padding:5px;
	font-size:30px !important;
}
table.resulttable td.time {
	font-size:80px;
}
table.resulttable td.result {
	font-size:120pt;
}
table.resulttable .spectators {
	font-size:28pt;
}


text:
package org.gradle.internal.jvm.inspection;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import org.gradle.api.GradleException;
import org.gradle.internal.ErroringAction;
import org.gradle.internal.IoActions;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
import static org.objectweb.asm.Opcodes.ACC_STATIC;
import static org.objectweb.asm.Opcodes.ACC_SUPER;
import static org.objectweb.asm.Opcodes.ALOAD;
import static org.objectweb.asm.Opcodes.GETSTATIC;
import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
import static org.objectweb.asm.Opcodes.RETURN;
import static org.objectweb.asm.Opcodes.V1_1;

class MetadataProbe {

    public static final String MARKER_PREFIX = "GRADLE_PROBE_VALUE:";

    private final Supplier<byte[]> probeClass = Suppliers.memoize(() -> createProbeClass());

    public File writeClass(File outputDirectory) {
        File probeFile = new File(outputDirectory, "JavaProbe.class");
        try {
            IoActions.withResource(new FileOutputStream(probeFile), new ErroringAction<FileOutputStream>() {
                @Override
                protected void doExecute(FileOutputStream thing) throws Exception {
                    thing.write(probeClass.get());
                }
            });
        } catch (FileNotFoundException e) {
            throw new GradleException("Unable to write Java probe file", e);
        }
        return probeFile;
    }

    private static byte[] createProbeClass() {
        ClassWriter cw = new ClassWriter(0);
        createClassHeader(cw);
        createConstructor(cw);
        createMainMethod(cw);
        cw.visitEnd();
        return cw.toByteArray();
    }

    private static void createClassHeader(ClassWriter cw) {
        cw.visit(V1_1, ACC_PUBLIC + ACC_SUPER, "JavaProbe", null, "java/lang/Object", null);
    }

    private static void createMainMethod(ClassWriter cw) {
        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        for (ProbedSystemProperty type : ProbedSystemProperty.values()) {
            if (type != ProbedSystemProperty.Z_ERROR) {
                dumpProperty(mv, type.getSystemPropertyKey());
            }
        }
        mv.visitInsn(RETURN);
        Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitLocalVariable("args", "[Ljava/lang/String;", null, l0, l3, 0);
        mv.visitMaxs(3, 1);
        mv.visitEnd();
    }

    private static void dumpProperty(MethodVisitor mv, String property) {
        mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitLdcInsn(MARKER_PREFIX);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false);

        mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitLdcInsn(property);
        mv.visitLdcInsn("unknown");
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/System", "getProperty", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
    }

    private static void createConstructor(ClassWriter cw) {
        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "LJavaProbe;", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }

}