text:
.. -*- coding: utf-8 -*-
.. URL: https://docs.docker.com/engine/userguide/containers/dockerimages/
.. SOURCE: https://github.com/docker/docker/blob/master/docs/userguide/containers/dockerimages.md
   doc version: 1.12
      https://github.com/docker/docker/commits/master/docs/userguide/containers/dockerimages.md
.. check date: 2016/06/13
.. Commits on a Mar 4, 2016 69004ff67eed6525d56a92fdc69466c41606151a
.. ----------------------------------------------------------------------------

.. Build your own images

.. _build-your-own-images:

=======================================
イメージの構築
=======================================

.. sidebar:: 目次

   .. contents::
       :depth: 3
       :local:

.. Docker images are the basis of containers. Each time you've used docker run you told it which image you wanted. In the previous sections of the guide you used Docker images that already exist, for example the ubuntu image and the training/webapp image.

Docker イメージはコンテナの土台(基盤)です。``docker run`` を実行する度に、どのイメージを使うか指定します。ガイドの前セクションでは、既存の ``ubuntu`` イメージと ``training/webapp`` イメージを使いました。

.. You also discovered that Docker stores downloaded images on the Docker host. If an image isn't already present on the host then it'll be downloaded from a registry: by default the Docker Hub Registry.

Docker はダウンロードしたイメージを Docker ホスト上に保管しており、それらを見ることができます。もしホスト上にイメージがなければ、Docker はレジストリからイメージをダウンロードします。標準のレジストリは `Docker Hub レジストリ <https://hub.docker.com/>`_ です。

.. In this section you're going to explore Docker images a bit more including:

このセクションでは Docker イメージについて、次の内容を含めて深掘りします。

..
    Managing and working with images locally on your Docker host.
    Creating basic images.
    Uploading images to Docker Hub Registry.

* ローカルの Docker ホスト上にあるイメージの管理と操作
* 基本イメージの作成
* イメージを `Docker Hub レジストリ <https://hub.docker.com/>`_ にアップロード

.. Listing images on the host

ホスト上のイメージ一覧を表示
==============================

.. Let's start with listing the images you have locally on our host. You can do this using the docker images command like so:

ローカルのホスト上にあるイメージの一覧を表示しましょう。表示するには ``docker images`` コマンドを使います。

.. code-block:: bash

   $ docker images
   REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
   ubuntu              14.04               1d073211c498        3 days ago          187.9 MB
   busybox             latest              2c5ac3f849df        5 days ago          1.113 MB
   training/webapp     latest              54bb4e8718e8        5 months ago        348.7 MB

.. You can see the images you've previously used in the user guide. Each has been downloaded from Docker Hub when you launched a container using that image. When you list images, you get three crucial pieces of information in the listing.

これまでのガイドで使用したイメージを表示します。それぞれ、コンテナでイメージを起動する時に `Docker Hub <https://hub.docker.com/>`_ からダウンロードしたものです。イメージの一覧表示には、3つの重要な情報が表示されます。

..
    What repository they came from, for example ubuntu.
    The tags for each image, for example 14.04.
    The image ID of each image.

* どのリポジトリから取得したのか(例:``ubuntu``)
* 各イメージのタグ(例:``14.04``)
* イメージごとのイメージ ID

.. Tip: You can use a third-party dockviz tool or the Image layers site to display visualizations of image data.

.. tip::

   `サード・パーティ製の dockviz tool <https://github.com/justone/dockviz>`_ や `image layers サイト <https://imagelayers.io/>`_ でイメージ・データを可視化できます。

.. A repository potentially holds multiple variants of an image. In the case of our ubuntu image you can see multiple variants covering Ubuntu 10.04, 12.04, 12.10, 13.04, 13.10 and 14.04. Each variant is identified by a tag and you can refer to a tagged image like so:

リポジトリによっては複数の派生イメージを持つ場合があります。先ほどの ``ubuntu`` イメージの場合は、Ubuntu 10.04、12.04、12.10、13.03、13.10 という、複数の異なる派生イメージがあります。それぞれの違いをタグ (tag) によって識別します。そして、次のようにイメージに対するタグとして参照できます。

.. code-block:: bash

   ubuntu:14.04

.. So when you run a container you refer to a tagged image like so:

そのため、コンテナを実行する時は、次のようにタグ付けされたイメージを参照できます。

.. code-block:: bash

   $ docker run -t -i ubuntu:14.04 /bin/bash

.. If instead you wanted to run an Ubuntu 12.04 image you'd use:

あるいは Ubuntu 12.04 イメージを使いたい場合は、次のようにします。

.. code-block:: bash

   $ docker run -t -i ubuntu:12.04 /bin/bash

.. If you don't specify a variant, for example you just use ubuntu, then Docker will default to using the ubuntu:latest image.

タグを指定しない場合、ここでは ``ubuntu`` しか指定しなければ、Docker は標準で ``ubuntu:latest`` イメージを使用します。

..     Tip: You should always specify an image tag, for example ubuntu:14.04. That way, you always know exactly what variant of an image you are using. This is useful for troubleshooting and debugging.

.. tip::

   常に ``ubuntu:14.04`` のようにイメージに対するタグを指定すべきです。タグの指定こそが、確実にイメージを使えるようにする手法だからです。トラブルシュートやデバッグに便利です。

.. Getting a new image

.. _getting-a-new-image:

新しいイメージの取得
==============================

.. So how do you get new images? Well Docker will automatically download any image you use that isn't already present on the Docker host. But this can potentially add some time to the launch of a container. If you want to pre-load an image you can download it using the docker pull command. Suppose you'd like to download the centos image.

それでは、新しいイメージをどうやって取得できるのでしょうか。Docker は Docker ホスト上に存在しないイメージを使う時、自動的にイメージをダウンロードします。しかしながら、コンテナを起動するまで少々時間がかかる場合があります。イメージをあらかじめダウンロードしたい場合は、``docker pull`` コマンドを使えます。以下は ``centos`` イメージをダウンロードする例です。

.. code-block:: bash

   $ docker pull centos
   Pulling repository centos
   b7de3133ff98: Pulling dependent layers
   5cc9e91966f7: Pulling fs layer
   511136ea3c5a: Download complete
   ef52fb1fe610: Download complete
   . . .

   Status: Downloaded newer image for centos

.. You can see that each layer of the image has been pulled down and now you can run a container from this image and you won't have to wait to download the image.

イメージの各レイヤを取得しているのが見えます。コンテナを起動する時、このイメージを使えばイメージのダウンロードのために待つ必要はありません。

.. code-block:: bash

   $ docker run -t -i centos /bin/bash
   bash-4.1#

.. Finding images

.. _finding-images:

イメージの検索
====================

.. One of the features of Docker is that a lot of people have created Docker images for a variety of purposes. Many of these have been uploaded to Docker Hub. You can search these images on the Docker Hub website.

Docker の特長の1つに、様々な目的の Docker イメージが多くの方によって作られています。大部分が `Docker Hub <https://hub.docker.com/>`_ にアップロードされています。これらイメージは `Docker Hub のウェブサイト <https://hub.docker.com/explore/>`_ から検索できます。

.. image:: search.png

.. You can also search for images on the command line using the docker search command. Suppose your team wants an image with Ruby and Sinatra installed on which to do our web application development. You can search for a suitable image by using the docker search command to find all the images that contain the term sinatra.

イメージを検索するには、コマンドライン上で ``docker search`` コマンドを使う方法もあります。チームでウェブ・アプリケーションの開発のために Ruby と Sinatra をインストールしたイメージが必要と仮定します。``docker search`` コマンドを使うことで、文字列 ``sinatra`` を含む全てのイメージを表示して、そこから適切なイメージを探せます。

.. code-block:: bash

   $ docker search sinatra
   NAME                                   DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
   training/sinatra                       Sinatra training image                          0                    [OK]
   marceldegraaf/sinatra                  Sinatra test app                                0
   mattwarren/docker-sinatra-demo                                                         0                    [OK]
   luisbebop/docker-sinatra-hello-world                                                   0                    [OK]
   bmorearty/handson-sinatra              handson-ruby + Sinatra for Hands on with D...   0
   subwiz/sinatra                                                                         0
   bmorearty/sinatra                                                                      0
   . . .

.. You can see the command returns a lot of images that use the term sinatra. You've received a list of image names, descriptions, Stars (which measure the social popularity of images - if a user likes an image then they can "star" it), and the Official and Automated build statuses. Official Repositories are a carefully curated set of Docker repositories supported by Docker, Inc. Automated repositories are Automated Builds that allow you to validate the source and content of an image.

コマンドを実行したら、``sinatra`` を含む多くのイメージを表示します。表示するのは、イメージ名の一覧、スター(イメージがソーシャル上で有名かどうか測るものです。利用者はイメージを気に入れば"スター"を付けられます )、公式(OFFICIAL)か、自動構築(AUTOMATED)といった状態です。:doc:`公式リポジトリ </docker-hub/official_repos>` とは、Docker 社のサポートよって丁寧に精査されている Docker リポジトリです。:ref:`自動構築(Automated Build) <automated-builds>` とは有効なソースコードを元に、イメージ内容が自動構築されたリポジトリです。

.. You've reviewed the images available to use and you decided to use the training/sinatra image. So far you've seen two types of images repositories, images like ubuntu, which are called base or root images. These base images are provided by Docker Inc and are built, validated and supported. These can be identified by their single word names.

さて、利用可能なイメージの内容を確認します。ここでは ``training/sinatra`` イメージを使うことにします。これまで2種類のイメージ・リポジトリがありました。``ubuntu`` のようなイメージはベース・イメージまたはルート・イメージと呼ばれます。このベース・イメージは Docker 社によって提供、構築、認証、サポートされています。これらは単一の単語名として表示されています。

.. You've also seen user images, for example the training/sinatra image you've chosen. A user image belongs to a member of the Docker community and is built and maintained by them. You can identify user images as they are always prefixed with the user name, here training, of the user that created them.

また、``training/sinatra`` イメージのようなユーザ・イメージもあります。ユーザ・イメージとは Docker コミュニティのメンバーに属するもので、メンバーによって構築、メンテナンスされます。ユーザ・イメージは、常にユーザ名がイメージの前に付きます。この例のイメージは、``training`` というユーザによって作成されました。

.. Pulling our image

.. _pulling-our-image:

イメージの取得
====================

.. You've identified a suitable image, training/sinatra, and now you can download it using the docker pull command.

適切なイメージ ``training/sinatra`` を確認したら、``docker pull`` コマンドを使ってダウンロードできます。

.. code-block:: bash

   $ docker pull training/sinatra

.. The team can now use this image by running their own containers.

これでチームはこのイメージを使い、自身でコンテナを実行できます。

.. code-block:: bash

   $ docker run -t -i training/sinatra /bin/bash
   root@a8cb6ce02d85:/#

.. Creating our own images

.. _creating-our-own-images:

イメージの作成
====================

.. The team has found the training/sinatra image pretty useful but it's not quite what they need and you need to make some changes to it. There are two ways you can update and create images.

チームでは ``training/sinatra`` イメージが有用だと分かりました。しかし、イメージを自分たちが使えるようにするには、いくつかの変更が必要です。イメージの更新や作成には2つの方法があります。

..
    You can update a container created from an image and commit the results to an image.
    You can use a Dockerfile to specify instructions to create an image.

1. イメージから作成したコンテナを更新し、イメージの結果をコミットする
2. ``Dockerfile`` を使って、イメージ作成の命令を指定する

.. Updating and committing an image

.. _updating-and-committing-an-image:

更新とイメージのコミット
------------------------------

.. To update an image you first need to create a container from the image you'd like to update.

イメージを更新するには、まず更新したいイメージからコンテナを作成する必要があります。

.. code-block:: bash

   $ docker run -t -i training/sinatra /bin/bash
   root@0b2616b0e5a8:/#

..    Note: Take note of the container ID that has been created, 0b2616b0e5a8, as you'll need it in a moment.

.. note::

   作成したコンテナ ID 、ここでは ``0b2616b0e5a8`` をメモしておきます。このあとすぐ使います。

.. Inside our running container let's add the json gem.

実行しているコンテナ内に ``json`` gem を追加しましょう。

.. code-block:: bash

   root@0b2616b0e5a8:/# gem install json

.. Once this has completed let's exit our container using the exit command.

この作業が終わったら、``exit`` コマンドを使ってコンテナを終了します。

.. Now you have a container with the change you want to make. You can then commit a copy of this container to an image using the docker commit command.

以上で自分たちが必要な変更を加えたコンテナができました。次に ``docker commit`` コマンドを使い、イメージに対してこのコンテナのコピーをコミット(収容)できます。

.. code-block:: bash

   $ docker commit -m "Added json gem" -a "Kate Smith" \
   0b2616b0e5a8 ouruser/sinatra:v2
   4f177bd27a9ff0f6dc2a830403925b5360bfe0b93d476f7fc3231110e7f71b1c

.. Here you've used the docker commit command. You've specified two flags: -m and -a. The -m flag allows us to specify a commit message, much like you would with a commit on a version control system. The -a flag allows us to specify an author for our update.

ここで使った ``docker commit`` コマンドの内容を確認します。2つのフラグ ``-m`` と ``-a`` を指定しています。``-m`` フラグはコミット・メッセージを指定するもので、バージョン・コントロール・システムのようにコミットできます。``-a`` フラグは更新を行った担当者を指定できます。

.. You've also specified the container you want to create this new image from, 0b2616b0e5a8 (the ID you recorded earlier) and you've specified a target for the image:

また、新しいイメージを作成する元となるコンテナを指定します。ここでは ``0b2616b0e5a8`` (先ほど書き留めた ID)です。そして、ターゲットとなるイメージを次のように指定します。

.. code-block:: bash

   ouruser/sinatra:v2

.. Break this target down. It consists of a new user, ouruser, that you're writing this image to. You've also specified the name of the image, here you're keeping the original image name sinatra. Finally you're specifying a tag for the image: v2.

こちらの詳細を見ていきましょう。``ouruser`` は新しいユーザ名であり、このイメージを書いた人です。また、イメージに対して何らかの名前も指定します。ここではオリジナルのイメージ名 ``sinatra`` をそのまま使います。最後に、イメージに対するタグ ``v2`` を指定します。

.. You can then look at our new ouruser/sinatra image using the docker images command.

あとは ``docker images`` コマンドを使えば、作成した新しいイメージ ``ouruser/sinatra`` が見えます。

.. code-block:: bash

   $ docker images
   REPOSITORY          TAG     IMAGE ID       CREATED       SIZE
   training/sinatra    latest  5bc342fa0b91   10 hours ago  446.7 MB
   ouruser/sinatra     v2      3c59e02ddd1a   10 hours ago  446.7 MB
   ouruser/sinatra     latest  5db5f8471261   10 hours ago  446.7 MB

.. To use our new image to create a container you can then:

作成したイメージを使ってコンテナを作成するには、次のようにします。

.. code-block:: bash

   $ docker run -t -i ouruser/sinatra:v2 /bin/bash
   root@78e82f680994:/#

.. Building an image from a Dockerfile

.. _building-an-image-from-a-dockerfile:

``Dockerfile`` からイメージを構築
----------------------------------------

.. Using the docker commit command is a pretty simple way of extending an image but it's a bit cumbersome and it's not easy to share a development process for images amongst a team. Instead you can use a new command, docker build, to build new images from scratch.

``docker commit`` コマンドを使う方法は、イメージを簡単に拡張します。しかし、少々面倒なものであり、チーム内の開発プロセスでイメージを共有するのは簡単ではありません。この方法ではなく、新しいコマンド ``docker build`` を使い構築する方法や、イメージをスクラッチ(ゼロ)から作成する方法があります。

.. To do this you create a Dockerfile that contains a set of instructions that tell Docker how to build our image.

この構築コマンドを使うには ``Dockerfile`` を作成します。この中に Docker がどのようにしてイメージを構築するのか、命令セットを記述します。

.. First, create a directory and a Dockerfile.

作成するにはまず、ディレクトリと ``Dockerfile`` を作成します。

.. code-block:: bash

   $ mkdir sinatra
   $ cd sinatra
   $ touch Dockerfile

.. If you are using Docker Machine on Windows, you may access your host directory by cd to /c/Users/your_user_name.

Windows で Docker Machine を使っている場合は、ホスト・ディレクトリには ``cd`` で ``/c/Users/ユーザ名`` を指定してアクセスできるでしょう。

.. Each instruction creates a new layer of the image. Try a simple example now for building your own Sinatra image for your fictitious development team.

各々の命令で新しいイメージ層を作成します。簡単な例として、架空の開発チーム向けの Sinatra イメージを構築しましょう。

.. code-block:: bash

   # ここはコメントです
   FROM ubuntu:14.04
   MAINTAINER Kate Smith <ksmith@example.com>
   RUN apt-get update && apt-get install -y ruby ruby-dev
   RUN gem install sinatra

.. Examine what your Dockerfile does. Each instruction prefixes a statement and is capitalized.

``Dockerfile`` が何をしているか調べます。それぞれの命令(instruction)は、ステートメント(statement)の前にあり、大文字で記述します。

.. code-block:: bash

   命令 ステートメント

..    Note: You use # to indicate a comment

.. note::

   ``#`` を使ってコメントを示せます

.. The first instruction FROM tells Docker what the source of our image is, in this case you're basing our new image on an Ubuntu 14.04 image. The instruction uses the MAINTAINER instruction to specify who maintains the new image.

冒頭の ``FROM`` 命令は Docker に対して基となるイメージを伝えます。この例では、新しいイメージは Ubuntu 14.04 イメージを基にします。``MAINTAINER`` 命令は誰がこの新しいイメージを管理するか指定します。

.. Lastly, you've specified two RUN instructions. A RUN instruction executes a command inside the image, for example installing a package. Here you're updating our APT cache, installing Ruby and RubyGems and then installing the Sinatra gem.

最後に ``RUN`` 命令を指定しています。``RUN`` 命令はイメージの中で実行するコマンドを指示します。この例ではパッケージのインストールのために、まず APT キャッシュを更新します。それから、Ruby と RubyGem をインストールし、Sinatra gem をインストールします。

.. Now let's take our Dockerfile and use the docker build command to build an image.

あとは ``Dockerfile`` を用い、``docker build`` コマンドでイメージを構築します。

.. code-block:: bash

   $ docker build -t ouruser/sinatra:v2 .
   Sending build context to Docker daemon 2.048 kB
   Sending build context to Docker daemon
   Step 1 : FROM ubuntu:14.04
    ---> e54ca5efa2e9
   Step 2 : MAINTAINER Kate Smith <ksmith@example.com>
    ---> Using cache
    ---> 851baf55332b
   Step 3 : RUN apt-get update && apt-get install -y ruby ruby-dev
    ---> Running in 3a2558904e9b
   Selecting previously unselected package libasan0:amd64.
   (Reading database ... 11518 files and directories currently installed.)
   Preparing to unpack .../libasan0_4.8.2-19ubuntu1_amd64.deb ...
   Unpacking libasan0:amd64 (4.8.2-19ubuntu1) ...
   Selecting previously unselected package libatomic1:amd64.
   Preparing to unpack .../libatomic1_4.8.2-19ubuntu1_amd64.deb ...
   Unpacking libatomic1:amd64 (4.8.2-19ubuntu1) ...
   Selecting previously unselected package libgmp10:amd64.
   Preparing to unpack .../libgmp10_2%3a5.1.3+dfsg-1ubuntu1_amd64.deb ...
   Unpacking libgmp10:amd64 (2:5.1.3+dfsg-1ubuntu1) ...
   Selecting previously unselected package libisl10:amd64.
   Preparing to unpack .../libisl10_0.12.2-1_amd64.deb ...
   Unpacking libisl10:amd64 (0.12.2-1) ...
   Selecting previously unselected package libcloog-isl4:amd64.
   Preparing to unpack .../libcloog-isl4_0.18.2-1_amd64.deb ...
   Unpacking libcloog-isl4:amd64 (0.18.2-1) ...
   Selecting previously unselected package libgomp1:amd64.
   Preparing to unpack .../libgomp1_4.8.2-19ubuntu1_amd64.deb ...
   Unpacking libgomp1:amd64 (4.8.2-19ubuntu1) ...
   Selecting previously unselected package libitm1:amd64.
   Preparing to unpack .../libitm1_4.8.2-19ubuntu1_amd64.deb ...
   Unpacking libitm1:amd64 (4.8.2-19ubuntu1) ...
   Selecting previously unselected package libmpfr4:amd64.
   Preparing to unpack .../libmpfr4_3.1.2-1_amd64.deb ...
   Unpacking libmpfr4:amd64 (3.1.2-1) ...
   Selecting previously unselected package libquadmath0:amd64.
   Preparing to unpack .../libquadmath0_4.8.2-19ubuntu1_amd64.deb ...
   Unpacking libquadmath0:amd64 (4.8.2-19ubuntu1) ...
   Selecting previously unselected package libtsan0:amd64.
   Preparing to unpack .../libtsan0_4.8.2-19ubuntu1_amd64.deb ...
   Unpacking libtsan0:amd64 (4.8.2-19ubuntu1) ...
   Selecting previously unselected package libyaml-0-2:amd64.
   Preparing to unpack .../libyaml-0-2_0.1.4-3ubuntu3_amd64.deb ...
   Unpacking libyaml-0-2:amd64 (0.1.4-3ubuntu3) ...
   Selecting previously unselected package libmpc3:amd64.
   Preparing to unpack .../libmpc3_1.0.1-1ubuntu1_amd64.deb ...
   Unpacking libmpc3:amd64 (1.0.1-1ubuntu1) ...
   Selecting previously unselected package openssl.
   Preparing to unpack .../openssl_1.0.1f-1ubuntu2.4_amd64.deb ...
   Unpacking openssl (1.0.1f-1ubuntu2.4) ...
   Selecting previously unselected package ca-certificates.
   Preparing to unpack .../ca-certificates_20130906ubuntu2_all.deb ...
   Unpacking ca-certificates (20130906ubuntu2) ...
   Selecting previously unselected package manpages.
   Preparing to unpack .../manpages_3.54-1ubuntu1_all.deb ...
   Unpacking manpages (3.54-1ubuntu1) ...
   Selecting previously unselected package binutils.
   Preparing to unpack .../binutils_2.24-5ubuntu3_amd64.deb ...
   Unpacking binutils (2.24-5ubuntu3) ...
   Selecting previously unselected package cpp-4.8.
   Preparing to unpack .../cpp-4.8_4.8.2-19ubuntu1_amd64.deb ...
   Unpacking cpp-4.8 (4.8.2-19ubuntu1) ...
   Selecting previously unselected package cpp.
   Preparing to unpack .../cpp_4%3a4.8.2-1ubuntu6_amd64.deb ...
   Unpacking cpp (4:4.8.2-1ubuntu6) ...
   Selecting previously unselected package libgcc-4.8-dev:amd64.
   Preparing to unpack .../libgcc-4.8-dev_4.8.2-19ubuntu1_amd64.deb ...
   Unpacking libgcc-4.8-dev:amd64 (4.8.2-19ubuntu1) ...
   Selecting previously unselected package gcc-4.8.
   Preparing to unpack .../gcc-4.8_4.8.2-19ubuntu1_amd64.deb ...
   Unpacking gcc-4.8 (4.8.2-19ubuntu1) ...
   Selecting previously unselected package gcc.
   Preparing to unpack .../gcc_4%3a4.8.2-1ubuntu6_amd64.deb ...
   Unpacking gcc (4:4.8.2-1ubuntu6) ...
   Selecting previously unselected package libc-dev-bin.
   Preparing to unpack .../libc-dev-bin_2.19-0ubuntu6_amd64.deb ...
   Unpacking libc-dev-bin (2.19-0ubuntu6) ...
   Selecting previously unselected package linux-libc-dev:amd64.
   Preparing to unpack .../linux-libc-dev_3.13.0-30.55_amd64.deb ...
   Unpacking linux-libc-dev:amd64 (3.13.0-30.55) ...
   Selecting previously unselected package libc6-dev:amd64.
   Preparing to unpack .../libc6-dev_2.19-0ubuntu6_amd64.deb ...
   Unpacking libc6-dev:amd64 (2.19-0ubuntu6) ...
   Selecting previously unselected package ruby.
   Preparing to unpack .../ruby_1%3a1.9.3.4_all.deb ...
   Unpacking ruby (1:1.9.3.4) ...
   Selecting previously unselected package ruby1.9.1.
   Preparing to unpack .../ruby1.9.1_1.9.3.484-2ubuntu1_amd64.deb ...
   Unpacking ruby1.9.1 (1.9.3.484-2ubuntu1) ...
   Selecting previously unselected package libruby1.9.1.
   Preparing to unpack .../libruby1.9.1_1.9.3.484-2ubuntu1_amd64.deb ...
   Unpacking libruby1.9.1 (1.9.3.484-2ubuntu1) ...
   Selecting previously unselected package manpages-dev.
   Preparing to unpack .../manpages-dev_3.54-1ubuntu1_all.deb ...
   Unpacking manpages-dev (3.54-1ubuntu1) ...
   Selecting previously unselected package ruby1.9.1-dev.
   Preparing to unpack .../ruby1.9.1-dev_1.9.3.484-2ubuntu1_amd64.deb ...
   Unpacking ruby1.9.1-dev (1.9.3.484-2ubuntu1) ...
   Selecting previously unselected package ruby-dev.
   Preparing to unpack .../ruby-dev_1%3a1.9.3.4_all.deb ...
   Unpacking ruby-dev (1:1.9.3.4) ...
   Setting up libasan0:amd64 (4.8.2-19ubuntu1) ...
   Setting up libatomic1:amd64 (4.8.2-19ubuntu1) ...
   Setting up libgmp10:amd64 (2:5.1.3+dfsg-1ubuntu1) ...
   Setting up libisl10:amd64 (0.12.2-1) ...
   Setting up libcloog-isl4:amd64 (0.18.2-1) ...
   Setting up libgomp1:amd64 (4.8.2-19ubuntu1) ...
   Setting up libitm1:amd64 (4.8.2-19ubuntu1) ...
   Setting up libmpfr4:amd64 (3.1.2-1) ...
   Setting up libquadmath0:amd64 (4.8.2-19ubuntu1) ...
   Setting up libtsan0:amd64 (4.8.2-19ubuntu1) ...
   Setting up libyaml-0-2:amd64 (0.1.4-3ubuntu3) ...
   Setting up libmpc3:amd64 (1.0.1-1ubuntu1) ...
   Setting up openssl (1.0.1f-1ubuntu2.4) ...
   Setting up ca-certificates (20130906ubuntu2) ...
   debconf: unable to initialize frontend: Dialog
   debconf: (TERM is not set, so the dialog frontend is not usable.)
   debconf: falling back to frontend: Readline
   debconf: unable to initialize frontend: Readline
   debconf: (This frontend requires a controlling tty.)
   debconf: falling back to frontend: Teletype
   Setting up manpages (3.54-1ubuntu1) ...
   Setting up binutils (2.24-5ubuntu3) ...
   Setting up cpp-4.8 (4.8.2-19ubuntu1) ...
   Setting up cpp (4:4.8.2-1ubuntu6) ...
   Setting up libgcc-4.8-dev:amd64 (4.8.2-19ubuntu1) ...
   Setting up gcc-4.8 (4.8.2-19ubuntu1) ...
   Setting up gcc (4:4.8.2-1ubuntu6) ...
   Setting up libc-dev-bin (2.19-0ubuntu6) ...
   Setting up linux-libc-dev:amd64 (3.13.0-30.55) ...
   Setting up libc6-dev:amd64 (2.19-0ubuntu6) ...
   Setting up manpages-dev (3.54-1ubuntu1) ...
   Setting up libruby1.9.1 (1.9.3.484-2ubuntu1) ...
   Setting up ruby1.9.1-dev (1.9.3.484-2ubuntu1) ...
   Setting up ruby-dev (1:1.9.3.4) ...
   Setting up ruby (1:1.9.3.4) ...
   Setting up ruby1.9.1 (1.9.3.484-2ubuntu1) ...
   Processing triggers for libc-bin (2.19-0ubuntu6) ...
   Processing triggers for ca-certificates (20130906ubuntu2) ...
   Updating certificates in /etc/ssl/certs... 164 added, 0 removed; done.
   Running hooks in /etc/ca-certificates/update.d....done.
    ---> c55c31703134
   Removing intermediate container 3a2558904e9b
   Step 4 : RUN gem install sinatra
    ---> Running in 6b81cb6313e5
   unable to convert "\xC3" to UTF-8 in conversion from ASCII-8BIT to UTF-8 to US-ASCII for README.rdoc, skipping
   unable to convert "\xC3" to UTF-8 in conversion from ASCII-8BIT to UTF-8 to US-ASCII for README.rdoc, skipping
   Successfully installed rack-1.5.2
   Successfully installed tilt-1.4.1
   Successfully installed rack-protection-1.5.3
   Successfully installed sinatra-1.4.5
   4 gems installed
   Installing ri documentation for rack-1.5.2...
   Installing ri documentation for tilt-1.4.1...
   Installing ri documentation for rack-protection-1.5.3...
   Installing ri documentation for sinatra-1.4.5...
   Installing RDoc documentation for rack-1.5.2...
   Installing RDoc documentation for tilt-1.4.1...
   Installing RDoc documentation for rack-protection-1.5.3...
   Installing RDoc documentation for sinatra-1.4.5...
    ---> 97feabe5d2ed
   Removing intermediate container 6b81cb6313e5
   Successfully built 97feabe5d2ed

.. You've specified our docker build command and used the -t flag to identify our new image as belonging to the user ouruser, the repository name sinatra and given it the tag v2.

``docker build`` コマンドで  ``-t`` フラグを指定しました。ここでは新しいイメージがユーザ ``ouruser`` に属していること、リポジトリ名が ``sinatra`` 、タグを ``v2`` に指定しました。

.. You've also specified the location of our Dockerfile using the . to indicate a Dockerfile in the current directory.

また、``Dockerfile`` の場所を示すため ``.`` を使うと、現在のディレクトリにある ``Dockerfile`` の使用を指示します。

..     Note: You can also specify a path to a Dockerfile.

.. note::

   ``Dockerfile`` のパスも指定できます。

.. Now you can see the build process at work. The first thing Docker does is upload the build context: basically the contents of the directory you're building in. This is done because the Docker daemon does the actual build of the image and it needs the local context to do it.

これで構築プロセスが進行します。まず Docker が行うのは構築コンテクスト(訳者注:環境や内容物の意味)のアップロードです。典型的なコンテクストとは、構築時のディレクトリです。この指定によって、Docker デーモンが実際のイメージ構築にあたり、ローカルのコンテクストをそこに入れるために必要とします。

.. Next you can see each instruction in the Dockerfile being executed step-by-step. You can see that each step creates a new container, runs the instruction inside that container and then commits that change - just like the docker commit work flow you saw earlier. When all the instructions have executed you're left with the 97feabe5d2ed image (also helpfuly tagged as ouruser/sinatra:v2) and all intermediate containers will get removed to clean things up.

この次は ``Dockerfile`` の命令を一行ずつ実行します。それぞれのステップで、新しいコンテナを作成し、コンテナ内で命令を実行し、変更に対してコミットするのが見えるでしょう。これは先ほど見た ``docker commit`` 処理の流れです。全ての命令を実行したら、イメージ ``97feabe5d2ed`` が残ります(扱いやすいよう ``ouruser/sinatra:v2`` とタグ付けもしています)。そして、作業中に作成された全てのコンテナを削除し、きれいに片付けています。

..    Note: An image can't have more than 127 layers regardless of the storage driver. This limitation is set globally to encourage optimization of the overall size of images.

.. note::

   127 層以上のイメージはストレージ・ドライバに関わらず作成できません。この制限が幅広く適用されるのは、イメージ全体のサイズが大きくならないよう、多くの人に最適化を促すためです。

..   You can then create a container from our new image.

あとは、新しいイメージからコンテナを作成できます。

.. code-block:: bash

   $ docker run -t -i ouruser/sinatra:v2 /bin/bash
   root@8196968dac35:/#

..    Note: This is just a brief introduction to creating images. We've skipped a whole bunch of other instructions that you can use. We'll see more of those instructions in later sections of the Guide or you can refer to the Dockerfile reference for a detailed description and examples of every instruction. To help you write a clear, readable, maintainable Dockerfile, you've also written a Dockerfile Best Practices guide.

.. note::

   ここではイメージ作成の簡単な概要を紹介しました。他にも利用可能な命令がありますが、省略しています。ガイドの後半に読み進めれば、``Dockerfile`` のリファレンスから、コマンドごとに更なる詳細や例を参照いただけます。``Dockerfile`` を明らかに、読めるように、管理できるようにするためには ``Dockerfile`` :doc:`ベストプラクティス・ガイド </engine/userguide/eng-image/dockerfile_best-practice>` もお読みください。

.. Setting tag on an image

.. _setting-tag-on-an-image:

イメージにタグを設定
====================

.. You can also add a tag to an existing image after you commit or build it. We can do this using the docker tag command. Now, add a new tag to your ouruser/sinatra image.

コミットまたは構築後のイメージに対しても、タグを付けられます。タグ付けには ``docker tag`` コマンドを使います。ここでは ``ouruser/sinatra`` イメージに新しいタグを付けましょう。

.. code-block:: bash

   $ docker tag 5db5f8471261 ouruser/sinatra:devel

.. The docker tag command takes the ID of the image, here 5db5f8471261, and our user name, the repository name and the new tag.

``docker tag`` コマンドにはイメージ ID を使います。ここでは ``5db5f8471261`` です。そしてユーザ名、リポジトリ名、新しいタグを指定します。

.. Now, see your new tag using the docker images command.

それから、``docker images`` コマンドを使い新しいタグを確認します。

.. code-block:: bash

   $ docker images ouruser/sinatra
   REPOSITORY          TAG     IMAGE ID      CREATED        SIZE
   ouruser/sinatra     latest  5db5f8471261  11 hours ago   446.7 MB
   ouruser/sinatra     devel   5db5f8471261  11 hours ago   446.7 MB
   ouruser/sinatra     v2      5db5f8471261  11 hours ago   446.7 MB

.. Image Digest

.. _docker-image-digest:

イメージのダイジェスト値
==============================

.. Images that use the v2 or later format have a content-addressable identifier called a digest. As long as the input used to generate the image is unchanged, the digest value is predictable. To list image digest values, use the --digests flag:

v2 以上のフォーマットのイメージには、内容に対して ``digest`` と呼ばれる識別子が割り当て可能です。作成したイメージが長期間にわたって変更がなければ、ダイジェスト値は(変更不可能なため)予想できます。イメージの digest 値を一覧表示するには、``--digests`` フラグを使います。

.. code-block:: bash

   $ docker images --digests | head
   REPOSITORY                         TAG                 DIGEST                                                                     IMAGE ID            CREATED             SIZE
   ouruser/sinatra                    latest              sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf    5db5f8471261        11 hours ago        446.7 MB

.. When pushing or pulling to a 2.0 registry, the push or pull command output includes the image digest. You can pull using a digest value.

2.0 レジストリに対して送信(push)や取得(pull)の実行に、``push`` か ``pull`` コマンドを使うと、その出力にイメージのダイジェスト値も含みます。このダイジェストを使っても、イメージを ``pull`` できます。

.. code-block:: bash

   $ docker pull ouruser/sinatra@cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf

.. You can also reference by digest in create, run, and rmi commands, as well as the FROM image reference in a Dockerfile.

ダイジェスト値は ``create``、``run``、``rmi`` コマンドや、Dockerfile で ``FROM`` イメージの参照にも使えます。

.. Push an image to Docker Hub

.. _push-an-image-to-docker-hub:

イメージを Docker Hub に送信
==============================

.. Once you've built or created a new image you can push it to Docker Hub using the docker push command. This allows you to share it with others, either publicly, or push it into a private repository.

イメージを構築・作成したあとは、``docker push`` コマンドを使って `Docker Hub <https://hub.docker.com/>`_ に送信できます。これにより、イメージを他人と共有したり、パブリックに共有したり、あるいは `プライベート・リポジトリ <https://hub.docker.com/plans/>`_ にも送信できます。

.. code-block:: bash

   $ docker push ouruser/sinatra
   The push refers to a repository [ouruser/sinatra] (len: 1)
   Sending image list
   Pushing repository ouruser/sinatra (3 tags)
   . . .

.. Remove an image from the host

.. _remove-an-image-from-the-host:

ホストからイメージを削除
==============================

.. You can also remove images on your Docker host in a way similar to containers using the docker rmi command.

Docker ホスト上で、 :doc:`コンテナの削除 <usingdocker>` と同じように ``docker rmi`` コマンドでイメージも削除できます。

.. Delete the training/sinatra image as you don't need it anymore.

不要になった ``training/sinatra`` イメージを削除します。

.. code-block:: bash

   $ docker rmi training/sinatra
   Untagged: training/sinatra:latest
   Deleted: 5bc342fa0b91cabf65246837015197eecfa24b2213ed6a51a8974ae250fedd8d
   Deleted: ed0fffdcdae5eb2c3a55549857a8be7fc8bc4241fb19ad714364cbfd7a56b22f
   Deleted: 5c58979d73ae448df5af1d8142436d81116187a7633082650549c52c3a2418f0

..    Note: To remove an image from the host, please make sure that there are no containers actively based on it.

.. note::

   ホストからイメージを削除する時は、どのコンテナも対象となるイメージを利用していないことを確認してください。

.. Next steps

次のステップ
====================

.. Until now you've seen how to build individual applications inside Docker containers. Now learn how to build whole application stacks with Docker by networking together multiple Docker containers.

ここまでは、Docker コンテナ内に個々のアプリケーションを構築する方法を見てきました。次は、複数の Docker コンテナを結び付けるアプリケーション・スタック(積み重ね)の構築方法を学びましょう。

.. Go to Network containers.

:doc:`コンテナのネットワーク <networkingcontainers>`  に移動します。

.. seealso::

   Build your own images
      https://docs.docker.com/engine/userguide/containers/dockerimages/


text:
/**
 * This barrel file provides the exports for the shared resources (services, components).
 */
export { NavbarComponent } from './navbar/index';
export { CroppedImageComponent } from './cropped-image/index';
export { ApiService } from './api/index';

redpajama_set_name: RedPajamaGithub


text:
'use strict';

describe('networks', function () {

  var $scope, element, $routeParams, q, deferred, $httpBackend, networkFactory, zoneFactory, $location;

  beforeEach(module('templates'));

  beforeEach(module('myApp', function($provide) {
    networkFactory = {
      query: function () {
        deferred = q.defer();
        return {$promise: deferred.promise};
      },
      get: function () {
        deferred = q.defer();
        return {$promise: deferred.promise};
      },
      destroy: function () {
        deferred = q.defer();
        return {$promise: deferred.promise};
      },
      update: function () {
        deferred = q.defer();
        return {$promise: deferred.promise};
      }
    };
    zoneFactory = {
      get: function () {
        deferred = q.defer();
        return {$promise: deferred.promise};
      },
    };
    $provide.value("Zone", zoneFactory);
    $provide.value("Network", networkFactory);
  }));

  beforeEach(inject(function($injector) {
    $httpBackend = $injector.get('$httpBackend');
    $httpBackend.whenGET('/translations/en_GB.json').respond("");
  }));

  describe('lists the networks for a location', function() {
    beforeEach(inject(function($compile, $rootScope, $q, _$routeParams_, $injector) {
      $scope = $rootScope;
      q = $q;
      $routeParams = _$routeParams_;
      $routeParams.id = 'xxx';
      $routeParams.q = 'my-filter';
      $routeParams.page = '10';
      $routeParams.per = '100';
      $scope.loading = true;
      var elem = angular.element('<list-networks loading="loading"></list-networks>');
      element = $compile(elem)($rootScope);
      element.scope().$digest();
    }));

    it("should set the default scopes vals", function() {
      spyOn(networkFactory, 'get').and.callThrough();
      expect(element.isolateScope().location.slug).toEqual('xxx');

      var results = [{ id: 123 }];
      deferred.resolve(results);
      $scope.$apply();

      expect(element.isolateScope().menu.length).toEqual(3);
      expect(element.isolateScope().menu[0].name).toEqual('Edit Settings');
      expect(element.isolateScope().menu[0].type).toEqual('settings');
      expect(element.isolateScope().menu[1].name).toEqual('Change SSID');
      expect(element.isolateScope().menu[1].type).toEqual('ssid');
      expect(element.isolateScope().menu[2].name).toEqual('Delete Network');
      expect(element.isolateScope().menu[2].type).toEqual('delete');

      expect(element.isolateScope().query.page).toEqual('10');
      expect(element.isolateScope().query.limit).toEqual('100');
    });

    it("should get the networks and save to scope", function() {
      spyOn(networkFactory, 'get').and.callThrough();
      var results = [{ id: 123 }];
      deferred.resolve(results);
      $scope.$apply();
      expect(element.isolateScope().networks[0].id).toEqual(123);
    });

    it("should update the band to 2.4Ghz or 5Ghz", function() {
      element.isolateScope().updateBand('two');
      expect(element.isolateScope().band).toEqual('2.4Ghz');

      element.isolateScope().updateBand('five');
      expect(element.isolateScope().band).toEqual('5Ghz');

      element.isolateScope().updateBand();
      expect(element.isolateScope().band).toEqual('');
    });

    it("should delete a network and remove from array", function() {
      spyOn(networkFactory, 'destroy').and.callThrough();
      var network = { id: 123 };
      element.isolateScope().networks = [network];

      element.isolateScope().destroy(network)
      deferred.resolve();
      $scope.$apply();
      expect(element.isolateScope().networks.length).toEqual(0);
    });

    // To fix - breaks since we need to sort the errors popup
    xit("should not delete a network and not remove from array", function() {
      spyOn(networkFactory, 'destroy').and.callThrough();
      var network = { id: 123 };
      element.isolateScope().networks = [network];

      element.isolateScope().destroy(network)
      deferred.reject({errors: {data: [{a: 123}]}});
      $scope.$apply();
      expect(element.isolateScope().networks.length).toEqual(1);
    });

    it("should update a network", function() {
      spyOn(networkFactory, 'update').and.callThrough();
      var network = { id: 123, state: 'foo' };
      element.isolateScope().networks = [network];

      element.isolateScope().update(network)
      deferred.resolve();
      $scope.$apply();
      expect(element.isolateScope().networks[0].state).toEqual(undefined);
    });

    // To fix - breaks since we need to sort the errors popup
    xit("should fail to update a network", function() {
      spyOn(networkFactory, 'update').and.callThrough();
      var network = { id: 123, state: 'foo' };
      element.isolateScope().networks = [network];

      element.isolateScope().update(network)
      deferred.reject();
      $scope.$apply();
      expect(element.isolateScope().networks[0].state).toEqual(undefined);
    });

  });

  describe('creates new network for a location', function() {
    beforeEach(inject(function($compile, $rootScope, $q, _$routeParams_, $injector) {
      $scope = $rootScope;
      q = $q;
      $routeParams = _$routeParams_;
      $routeParams.id = 'xxx';
      $routeParams.q = 'my-filter';
      $routeParams.page = '10';
      $routeParams.per = '100';
      $scope.loading = true;
      var elem = angular.element('<new-network loading="loading"></new-network>');
      element = $compile(elem)($rootScope);
      element.scope().$digest();
    }));

    it("should set the default scopes vals", function() {
      spyOn(networkFactory, 'get').and.callThrough();
      expect(element.isolateScope().location.slug).toEqual('xxx');

      // Would prefer to not use a scope //
      element.isolateScope().init();

      var cf = [
        {key: 'Danger', value:'Danger'},
        {key: 'Adult', value: 'Adult'},
        {key: 'Security', value: 'Security'},
        {key: 'Family', value: 'Family'},
        {key: 'Off', value: 'Off'}
      ];

      expect(element.isolateScope().content_filters[0].key).toEqual(cf[0].key);
      expect(element.isolateScope().content_filters[0].value).toEqual(cf[0].value);
      expect(element.isolateScope().content_filters[1].key).toEqual(cf[1].key);
      expect(element.isolateScope().content_filters[1].value).toEqual(cf[1].value);
      expect(element.isolateScope().content_filters[2].key).toEqual(cf[2].key);
      expect(element.isolateScope().content_filters[2].value).toEqual(cf[2].value);
      expect(element.isolateScope().content_filters[3].key).toEqual(cf[3].key);
      expect(element.isolateScope().content_filters[3].value).toEqual(cf[3].value);
      expect(element.isolateScope().content_filters[4].key).toEqual(cf[4].key);
      expect(element.isolateScope().content_filters[4].value).toEqual(cf[4].value);

      var network = element.isolateScope().network
      expect(network.ssid).toEqual('My WiFi Network')
      expect(network.access_type).toEqual('password')
      expect(network.encryption_type).toEqual('psk2')
      expect(network.band_steering).toEqual(true)
      expect(network.active).toEqual(true)
      expect(network.interface_ipaddr === '').toEqual(false)
      expect(network.dhcp_enabled).toEqual(true)
      expect(network.dns_1).toEqual('8.8.8.8')
      expect(network.dns_2).toEqual('8.8.4.4')
      expect(network.interface_netmask).toEqual(24)
      expect(network.use_ps_radius).toEqual(true)
      expect(network.captive_portal_ps).toEqual(true)
      expect(network.content_filter).toEqual('Off')
      expect(network.captive_portal_enabled).toEqual(false)
    });

  });

  describe('displays the network for a location', function() {
    beforeEach(inject(function($compile, $rootScope, $q, _$routeParams_, $injector, _$location_) {
      $scope = $rootScope;
      q = $q;
      $location = _$location_
      $routeParams = _$routeParams_;
      $routeParams.id = 'xxx';
      $routeParams.q = 'my-filter';
      $routeParams.page = '10';
      $routeParams.per = '100';
      $scope.loading = true;
      var elem = angular.element('<display-network loading="loading"></display-network>');
      element = $compile(elem)($rootScope);
      element.scope().$digest();
    }));

    it("should set the default vars for the network", function() {
      spyOn(networkFactory, 'query').and.callThrough();
      expect(element.isolateScope().location.slug).toEqual('xxx');

      var network = { id: '123' };
      deferred.resolve(network);
      $scope.$apply();

      var cf = [
        { key: 'Off', value: 'off' },
        { key: 'Allow White-listed Clients', value: 'allow'},
        { key: 'Block Banned Clients', value: 'deny'}
      ];

      expect(element.isolateScope().client_filters[0].key).toEqual('Off');
      expect(element.isolateScope().client_filters[0].value).toEqual('off');
      expect(element.isolateScope().client_filters[1].key).toEqual('Allow White-listed Clients');
      expect(element.isolateScope().client_filters[1].value).toEqual('allow');
      expect(element.isolateScope().client_filters[2].key).toEqual('Block Banned Clients');
      expect(element.isolateScope().client_filters[2].value).toEqual('deny');

      expect(element.isolateScope().menu.length).toEqual(2);
      expect(element.isolateScope().menu[0].name).toEqual('Delete Network');
      expect(element.isolateScope().menu[0].type).toEqual('delete');
      expect(element.isolateScope().menu[1].name).toEqual('Test Radius');
      expect(element.isolateScope().menu[1].type).toEqual('radius');
    });

    it("should show the network", function() {
      spyOn(networkFactory, 'query').and.callThrough();

      var network = { id: '123', ssid_hidden: '1' };
      deferred.resolve(network);
      $scope.$apply();

      expect(element.isolateScope().network.id).toEqual('123');
      expect(element.isolateScope().network.ssid_hidden).toEqual(false);
      expect(element.isolateScope().short_ip).toEqual('10.168.210.');
    });

    it("should destroy the network", function() {
      spyOn(networkFactory, 'destroy').and.callThrough();

      var network = { id: '123', ssid_hidden: '1' };
      element.isolateScope().network = network;
      element.isolateScope().destroy();
      deferred.resolve(network);
      $scope.$apply();
      expect($location.path()).toEqual('/locations/xxx/networks');
    });
  });
});


text:
<?php

namespace Oro\Bundle\SecurityBundle\Acl\Extension;

use Oro\Bundle\EntityBundle\ORM\EntityClassResolver;
use Oro\Bundle\SecurityBundle\Acl\AccessLevel;
use Oro\Bundle\SecurityBundle\Acl\Domain\ObjectIdAccessor;
use Oro\Bundle\SecurityBundle\Acl\Domain\ObjectIdentityFactory;
use Oro\Bundle\SecurityBundle\Acl\Exception\InvalidAclMaskException;
use Oro\Bundle\SecurityBundle\Acl\Group\AclGroupProviderInterface;
use Oro\Bundle\SecurityBundle\Acl\Permission\PermissionManager;
use Oro\Bundle\SecurityBundle\Annotation\Acl as AclAnnotation;
use Oro\Bundle\SecurityBundle\Entity\Permission;
use Oro\Bundle\SecurityBundle\Metadata\EntitySecurityMetadataProvider;
use Oro\Bundle\SecurityBundle\Owner\EntityOwnerAccessor;
use Oro\Bundle\SecurityBundle\Owner\Metadata\OwnershipMetadataProviderInterface;
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
use Symfony\Component\Security\Acl\Util\ClassUtils;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

/**
 * The ACL extension that works with Doctrine entities.
 *
 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
 */
class EntityAclExtension extends AbstractAccessLevelAclExtension
{
    const NAME = 'entity';

    /** @var EntityClassResolver */
    protected $entityClassResolver;

    /** @var EntitySecurityMetadataProvider */
    protected $entityMetadataProvider;

    /** @var PermissionManager */
    protected $permissionManager;

    /** @var AclGroupProviderInterface */
    protected $groupProvider;

    /** @var FieldAclExtension */
    protected $fieldAclExtension;

    /** @var int[] [permission => the identity of a permission mask builder, ...] */
    private $permissionsToIdentity;

    /** @var array [the identity of a permission mask builder => [permission, ...], ...] */
    private $identityToPermissions;

    /** @var EntityMaskBuilder[] [the identity of a permission mask builder => EntityMaskBuilder, ...] */
    private $builders = [];

    /**
     * @param ObjectIdAccessor                           $objectIdAccessor
     * @param EntityClassResolver                        $entityClassResolver
     * @param EntitySecurityMetadataProvider             $entityMetadataProvider
     * @param OwnershipMetadataProviderInterface         $metadataProvider
     * @param EntityOwnerAccessor                        $entityOwnerAccessor
     * @param AccessLevelOwnershipDecisionMakerInterface $decisionMaker
     * @param PermissionManager                          $permissionManager
     * @param AclGroupProviderInterface                  $groupProvider
     * @param FieldAclExtension                          $fieldAclExtension
     */
    public function __construct(
        ObjectIdAccessor $objectIdAccessor,
        EntityClassResolver $entityClassResolver,
        EntitySecurityMetadataProvider $entityMetadataProvider,
        OwnershipMetadataProviderInterface $metadataProvider,
        EntityOwnerAccessor $entityOwnerAccessor,
        AccessLevelOwnershipDecisionMakerInterface $decisionMaker,
        PermissionManager $permissionManager,
        AclGroupProviderInterface $groupProvider,
        FieldAclExtension $fieldAclExtension
    ) {
        parent::__construct($objectIdAccessor, $metadataProvider, $entityOwnerAccessor, $decisionMaker);
        $this->entityClassResolver = $entityClassResolver;
        $this->entityMetadataProvider = $entityMetadataProvider;
        $this->permissionManager = $permissionManager;
        $this->groupProvider = $groupProvider;
        $this->fieldAclExtension = $fieldAclExtension;
    }

    /**
     * {@inheritdoc}
     */
    public function getExtensionKey()
    {
        return self::NAME;
    }

    /**
     * {@inheritdoc}
     */
    public function getFieldExtension()
    {
        return $this->fieldAclExtension;
    }

    /**
     * {@inheritdoc}
     */
    public function supports($type, $id)
    {
        if ($type === ObjectIdentityFactory::ROOT_IDENTITY_TYPE) {
            return $id === $this->getExtensionKey();
        }

        $type = ClassUtils::getRealClass(
            ObjectIdentityHelper::removeGroupName(ObjectIdentityHelper::removeFieldName($type))
        );
        if ($id === $this->getExtensionKey()) {
            $type = $this->entityClassResolver->getEntityClass($type);
        }

        return $this->entityMetadataProvider->isProtectedEntity($type);
    }

    /**
     * {@inheritdoc}
     */
    public function getMasks($permission)
    {
        $this->buildPermissionsMap();

        return parent::getMasks($permission);
    }

    /**
     * {@inheritdoc}
     */
    public function hasMasks($permission)
    {
        $this->buildPermissionsMap();

        return parent::hasMasks($permission);
    }

    /**
     * {@inheritdoc}
     */
    public function validateMask($mask, $object, $permission = null)
    {
        if (0 === $this->removeServiceBits($mask)) {
            // zero mask
            return;
        }

        $permissions = $permission === null
            ? $this->getPermissions($mask, true)
            : [$permission];

        foreach ($permissions as $permission) {
            $validMasks = $this->getValidMasks($permission, $object);
            if (($mask | $validMasks) === $validMasks) {
                $identity = $this->getIdentityForPermission($permission);
                foreach ($this->getPermissionsToIdentityMap() as $p => $i) {
                    if ($identity === $i) {
                        $this->validateMaskAccessLevel($p, $mask, $object);
                    }
                }

                return;
            }
        }

        throw $this->createInvalidAclMaskException($mask, $object);
    }

    /**
     * {@inheritdoc}
     */
    public function getObjectIdentity($val)
    {
        if (is_string($val)) {
            return $this->fromDescriptor($val);
        } elseif ($val instanceof AclAnnotation) {
            $class = $this->entityClassResolver->getEntityClass($val->getClass());
            $group = $val->getGroup();

            return new ObjectIdentity($val->getType(), ObjectIdentityHelper::buildType($class, $group));
        }

        return $this->fromDomainObject($val);
    }

    /**
     * {@inheritdoc}
     */
    public function getMaskBuilder($permission)
    {
        if (empty($permission)) {
            $permission = 'VIEW';
        }

        return clone $this->getEntityMaskBuilder($this->getIdentityForPermission($permission));
    }

    /**
     * {@inheritdoc}
     */
    public function getAllMaskBuilders()
    {
        $result = [];
        $map = $this->getIdentityToPermissionsMap();
        foreach ($map as $identity => $permissions) {
            $result[] = clone $this->getEntityMaskBuilder($identity);
        }

        return $result;
    }

    /**
     * {@inheritdoc}
     */
    public function getMaskPattern($mask)
    {
        return EntityMaskBuilder::getPatternFor($mask);
    }

    /**
     * {@inheritdoc}
     *
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     */
    public function adaptRootMask($rootMask, $object)
    {
        $permissions = $this->getPermissions($rootMask, true);
        if (!empty($permissions)) {
            $metadata = $this->getMetadata($object);
            $identity = $this->getServiceBits($rootMask);
            $maskBuilder = $this->getEntityMaskBuilder($identity);
            foreach ($permissions as $permission) {
                $mask = $rootMask & $maskBuilder->getMask('GROUP_' . $permission);
                if (!$metadata->hasOwner()) {
                    if (in_array($permission, $this->getOwnershipPermissions(), true)) {
                        $rootMask &= ~$this->removeServiceBits($mask);
                    } elseif ($this->getAccessLevel($mask) < AccessLevel::SYSTEM_LEVEL) {
                        $rootMask &= ~$this->removeServiceBits($mask);
                        $rootMask |= $maskBuilder->getMask('MASK_' . $permission . '_SYSTEM');
                    }
                } elseif ($metadata->isOrganizationOwned()) {
                    if ($this->getAccessLevel($mask) < AccessLevel::GLOBAL_LEVEL) {
                        $rootMask &= ~$this->removeServiceBits($mask);
                        $rootMask |= $maskBuilder->getMask('MASK_' . $permission . '_GLOBAL');
                    }
                } elseif ($metadata->isBusinessUnitOwned()) {
                    if ($this->getAccessLevel($mask) < AccessLevel::LOCAL_LEVEL) {
                        $rootMask &= ~$this->removeServiceBits($mask);
                        $rootMask |= $maskBuilder->getMask('MASK_' . $permission . '_LOCAL');
                    }
                }
            }
        }

        return $rootMask;
    }

    /**
     * {@inheritdoc}
     */
    public function getServiceBits($mask)
    {
        return $mask & EntityMaskBuilder::SERVICE_BITS;
    }

    /**
     * {@inheritdoc}
     */
    public function removeServiceBits($mask)
    {
        return $mask & EntityMaskBuilder::REMOVE_SERVICE_BITS;
    }

    /**
     * {@inheritdoc}
     */
    public function getAccessLevel($mask, $permission = null, $object = null)
    {
        if (0 === $this->removeServiceBits($mask)) {
            return AccessLevel::NONE_LEVEL;
        }

        $identity = $this->getServiceBits($mask);
        if (null !== $permission) {
            $mask &= $this->getEntityMaskBuilder($identity)->getMask('GROUP_' . $permission);
        }

        $mask = $this->removeServiceBits($mask);

        $result = AccessLevel::NONE_LEVEL;
        if (0 !== $mask) {
            $maskBuilder = $this->getEntityMaskBuilder($identity);
            foreach (self::ACCESS_LEVELS as $accessLevelName => $accessLevel) {
                if (0 !== ($mask & $maskBuilder->getMask('GROUP_' . $accessLevelName))) {
                    $result = $accessLevel;
                    break;
                }
            }
        }

        if (null !== $object) {
            $result = $this->metadataProvider->getMaxAccessLevel($result, $this->getObjectClassName($object));
        }

        return $result;
    }

    /**
     * {@inheritdoc}
     */
    public function getPermissions($mask = null, $setOnly = false, $byCurrentGroup = false)
    {
        if ($mask === null) {
            return array_keys($this->getPermissionsToIdentityMap($byCurrentGroup));
        }
        if (!$setOnly) {
            $result = $this->getPermissionsForIdentity($this->getServiceBits($mask));
        } else {
            $result = [];
            if (0 !== $this->removeServiceBits($mask)) {
                $identity = $this->getServiceBits($mask);
                $maskBuilder = $this->getEntityMaskBuilder($identity);
                $mask = $this->removeServiceBits($mask);
                $permissions = $this->getPermissionsForIdentity($identity);
                foreach ($permissions as $permission) {
                    if (0 !== ($mask & $maskBuilder->getMask('GROUP_' . $permission))) {
                        $result[] = $permission;
                    }
                }
            }
        }

        if ($byCurrentGroup && !empty($result)) {
            $result = array_intersect(
                $result,
                array_keys($this->permissionManager->getPermissionsMap($this->groupProvider->getGroup()))
            );
        }

        return $result;
    }

    /**
     * {@inheritdoc}
     */
    public function getAllowedPermissions(ObjectIdentity $oid, $fieldName = null, $aclGroup = null)
    {
        if ($oid->getType() === ObjectIdentityFactory::ROOT_IDENTITY_TYPE) {
            $result = array_keys($this->getPermissionsToIdentityMap());
        } else {
            $config = $this->entityMetadataProvider->getMetadata($oid->getType());
            $result = $config->getPermissions();
            if (empty($result)) {
                $result = array_keys($this->getPermissionsToIdentityMap());
            }

            $metadata = $this->getMetadata($oid);
            if (!$metadata->hasOwner()) {
                $result = array_diff($result, $this->getOwnershipPermissions());
            }
        }

        $allowed = $this->getPermissionsForType($oid->getType(), $aclGroup);

        return array_values(array_intersect($result, $allowed));
    }

    /**
     * That method returns the collection of permissions that used only if the level of ownership less than System
     *
     * @return array
     */
    protected function getOwnershipPermissions()
    {
        return ['ASSIGN'];
    }

    /**
     * @param string $type
     * @param string|null $aclGroup
     * @return array
     */
    protected function getPermissionsForType($type, $aclGroup = null)
    {
        $group = $aclGroup ?: $this->groupProvider->getGroup();

        if ($type === ObjectIdentityFactory::ROOT_IDENTITY_TYPE) {
            $permissions = $this->permissionManager->getPermissionsForGroup($group);
        } else {
            $permissions = $this->permissionManager->getPermissionsForEntity($type, $group);
        }

        return array_map(
            function (Permission $permission) {
                return $permission->getName();
            },
            $permissions
        );
    }

    /**
     * {@inheritdoc}
     */
    public function getClasses()
    {
        return $this->entityMetadataProvider->getEntities();
    }

    /**
     * {@inheritdoc}
     */
    public function decideIsGranting($triggeredMask, $object, TokenInterface $securityToken)
    {
        if (!$this->isSupportedObject($object) || null === $this->getObjectId($object)) {
            return true;
        }

        return $this->isAccessGranted($triggeredMask, $object, $securityToken);
    }

    /**
     * Constructs an ObjectIdentity for the given domain object
     *
     * @param string $descriptor
     *
     * @return ObjectIdentity
     * @throws \InvalidArgumentException
     */
    protected function fromDescriptor($descriptor)
    {
        $type = $id = $group = null;
        $this->parseDescriptor($descriptor, $type, $id, $group);

        $type = $this->entityClassResolver->getEntityClass(ClassUtils::getRealClass($type));

        if ($id === $this->getExtensionKey()) {
            return new ObjectIdentity($id, ObjectIdentityHelper::buildType($type, $group));
        }

        throw new \InvalidArgumentException(
            sprintf('Unsupported object identity descriptor: %s.', $descriptor)
        );
    }

    /**
     * Checks that the given mask represents only one access level
     *
     * @param string $permission
     * @param int    $mask
     * @param mixed  $object
     *
     * @throws InvalidAclMaskException
     */
    protected function validateMaskAccessLevel($permission, $mask, $object)
    {
        $identity = $this->getIdentityForPermission($permission);
        $maskBuilder = $this->getEntityMaskBuilder($identity);
        if (0 !== ($mask & $maskBuilder->getMask('GROUP_' . $permission))) {
            $maskAccessLevels = [];
            $clearedMask = $this->removeServiceBits($mask);

            foreach (AccessLevel::$allAccessLevelNames as $accessLevel) {
                $levelMask = $this->removeServiceBits(
                    $maskBuilder->getMask('MASK_' . $permission . '_' . $accessLevel)
                );

                if (0 !== ($clearedMask & $levelMask)) {
                    $maskAccessLevels[] = $accessLevel;
                }
            }
            if (count($maskAccessLevels) > 1) {
                throw $this->createInvalidAccessLevelAclMaskException($mask, $object, $permission, $maskAccessLevels);
            }
        }
    }

    /**
     * Gets all valid bitmasks for the given object
     *
     * @param string $permission
     * @param mixed  $object
     *
     * @return int
     */
    protected function getValidMasks($permission, $object)
    {
        $identity = $this->getIdentityForPermission($permission);

        if ($object instanceof ObjectIdentity && $object->getType() === ObjectIdentityFactory::ROOT_IDENTITY_TYPE) {
            $maskBuilder = $this->getEntityMaskBuilder($identity);

            return
                $maskBuilder->getMask('GROUP_SYSTEM')
                | $maskBuilder->getMask('GROUP_GLOBAL')
                | $maskBuilder->getMask('GROUP_DEEP')
                | $maskBuilder->getMask('GROUP_LOCAL')
                | $maskBuilder->getMask('GROUP_BASIC');
        }

        $metadata = $this->getMetadata($object);
        if (!$metadata->hasOwner()) {
            $maskBuilder = $this->getMaskBuilder($permission);
            $maskBuilder->reset()->add($maskBuilder->getMask('GROUP_SYSTEM'));

            foreach ($this->getOwnershipPermissions() as $ownershipPermission) {
                $maskName = 'MASK_' . $ownershipPermission . '_SYSTEM';

                if ($maskBuilder->hasMask($maskName)) {
                    $maskBuilder->remove($ownershipPermission . '_SYSTEM');
                }
            }

            return $maskBuilder->get();
        }

        if ($metadata->isOrganizationOwned()) {
            $maskBuilder = $this->getEntityMaskBuilder($identity);

            return
                $maskBuilder->getMask('GROUP_SYSTEM')
                | $maskBuilder->getMask('GROUP_GLOBAL');
        } elseif ($metadata->isBusinessUnitOwned()) {
            $maskBuilder = $this->getEntityMaskBuilder($identity);

            return
                $maskBuilder->getMask('GROUP_SYSTEM')
                | $maskBuilder->getMask('GROUP_GLOBAL')
                | $maskBuilder->getMask('GROUP_DEEP')
                | $maskBuilder->getMask('GROUP_LOCAL');
        } elseif ($metadata->isUserOwned()) {
            $maskBuilder = $this->getEntityMaskBuilder($identity);

            return
                $maskBuilder->getMask('GROUP_SYSTEM')
                | $maskBuilder->getMask('GROUP_GLOBAL')
                | $maskBuilder->getMask('GROUP_DEEP')
                | $maskBuilder->getMask('GROUP_LOCAL')
                | $maskBuilder->getMask('GROUP_BASIC');
        }

        return $this->getIdentityForPermission($permission);
    }

    /**
     * Gets the constant value defined in the given permission mask builder
     *
     * @param int    $identity  The permission mask builder identity
     * @param string $constName The name of a constant
     *
     * @return int
     */
    protected function getMaskBuilderConst($identity, $constName)
    {
        return $this->getEntityMaskBuilder($identity)->getMask($constName);
    }

    /**
     * Makes sure that $this->permissionsToIdentity and $this->identityToPermissions are initialized
     */
    protected function ensurePermissionsInitialized()
    {
        if (null !== $this->permissionsToIdentity) {
            return;
        }

        $levelsCount = count(AccessLevel::$allAccessLevelNames);
        $map = range(0, EntityMaskBuilder::MAX_PERMISSIONS_IN_MASK - 1);
        array_unshift($map, array_pop($map));
        $allPermissions = $this->permissionManager->getPermissionsMap();
        foreach ($allPermissions as $permission => $pk) {
            $identity = (int) (($pk - 1) / EntityMaskBuilder::MAX_PERMISSIONS_IN_MASK);
            $identity <<= $levelsCount * EntityMaskBuilder::MAX_PERMISSIONS_IN_MASK;
            $number = $map[$pk % EntityMaskBuilder::MAX_PERMISSIONS_IN_MASK];

            $this->permissionsToIdentity[$permission] = $identity;
            $this->identityToPermissions[$identity][$number] = $permission;
        }
    }

    protected function buildPermissionsMap()
    {
        if ($this->map !== null) {
            return;
        }

        $this->map = [];

        $permissions = array_keys($this->getPermissionsToIdentityMap());
        foreach ($permissions as $permission) {
            $masks = [];

            $maskBuilder = $this->getEntityMaskBuilder($this->getIdentityForPermission($permission));
            foreach (AccessLevel::$allAccessLevelNames as $accessLevel) {
                $masks[] = $maskBuilder->getMask('MASK_' . $permission . '_' . $accessLevel);
            }

            $this->map[$permission] = $masks;
        }
    }

    /**
     * @param bool $byCurrentGroup
     * @return array|int[]
     */
    protected function getPermissionsToIdentityMap($byCurrentGroup = false)
    {
        $this->ensurePermissionsInitialized();

        $map = $this->permissionsToIdentity;
        if ($byCurrentGroup) {
            $permissions = $this->permissionManager->getPermissionsMap($this->groupProvider->getGroup());

            $map = array_intersect_key($map, $permissions);
        }

        return $map;
    }

    /**
     * @param string $permission
     * @return int
     */
    protected function getIdentityForPermission($permission)
    {
        $identities = $this->getPermissionsToIdentityMap();

        return $identities[$permission];
    }

    /**
     * @return array [identity => [permission, ...], ...]
     */
    protected function getIdentityToPermissionsMap()
    {
        $this->ensurePermissionsInitialized();

        return $this->identityToPermissions;
    }

    /**
     * @param int $identity
     * @return string[]
     */
    protected function getPermissionsForIdentity($identity)
    {
        $this->ensurePermissionsInitialized();

        return $this->identityToPermissions[$identity];
    }

    /**
     * @param int $identity
     *
     * @return EntityMaskBuilder
     */
    protected function getEntityMaskBuilder($identity)
    {
        if (isset($this->builders[$identity])) {
            return $this->builders[$identity];
        }

        $maskBuilder = new EntityMaskBuilder($identity, $this->getPermissionsForIdentity($identity));
        $this->builders[$identity] = $maskBuilder;

        return $maskBuilder;
    }
}


text:
\section{Refcards}\label{refcards}
\index{Refcards}
\index{Refcards!emacs}
\index{Refcards!vi}
\index{Refcards!Makefile}
\index{Refcards!R}
\index{Refcards!Python}
\index{Refcards!Python!Data}
\index{Refcards!LaTeX}
\index{Refcards!vim}
\index{Refcards!git}
\index{Refcards!rst}
\index{Refcards!OpenStack}

\FILENAME\

We present you with a list of useful short refrence cards. This cards
can be extremly useful to remind yourself about some important commands
and features. Having them could simplify your interaction with the
systems, We not only collected here some refcards about Linux, but also
about other useful tools and services.

If you like to add new topics, let us know via your contribution (see
the contribution section).
{\footnotesize
\begin{longtable}{p{2.5cm}p{11.5cm}}
\toprule
Emacs
 &
\url{https://www.gnu.org/software/emacs/refcards/pdf/refcard.pdf}
\tabularnewline
Vi
 &
\url{http://www.ks.uiuc.edu/Training/Tutorials/Reference/virefcard.pdf}
\tabularnewline
Linux
 &
\url{http://www.cs.jhu.edu/~joanne/unixRC.pdf}
\tabularnewline
Makefile
 &
\url{http://www.tofgarion.net/lectures/IN323/refcards/refcardMakeIN323.pdf}
\tabularnewline
R
 &
\url{https://cran.r-project.org/doc/contrib/Short-refcard.pdf}
\tabularnewline
Python
 &
\url{https://dzone.com/refcardz/core-python}
\tabularnewline
Python Data
 &
\url{https://dzone.com/refcardz/data-mining-discovering-and}
\tabularnewline
SQL
 &
\url{http://www.digilife.be/quickreferences/QRC/MySQL-4.02a.pdf}
\tabularnewline
Vim
 &
\url{http://michaelgoerz.net/refcards/vimqrc.pdf}
\tabularnewline
LaTeX
 &
\url{https://wch.github.io/latexsheet/latexsheet.pdf}
\tabularnewline
Git
 &
\url{https://education.github.com/git-cheat-sheet-education.pdf}
\tabularnewline
Openstack
 &
\url{http://docs.openstack.org/user-guide/cli_cheat_sheet.html}
\tabularnewline
Openstack
 &
\url{http://cmias.free.fr/IMG/pdf/rc208_010d-openstack_2.pdf}
\tabularnewline
RST
 &
\url{https://github.com/ralsina/rst-cheatsheet/blob/master/rst-cheatsheet.pdf}
\tabularnewline
\bottomrule
\end{longtable}
}

Others:
{\footnotesize
\begin{longtable}{p{2.5cm}p{11.5cm}}
\toprule
  Numpi/Pandas &
  \url{http://www.cheat-sheets.org/saved-copy/NumPy_SciPy_Pandas_Quandl_Cheat_Sheet.pdf}
\tabularnewline
  Cheat Sheets & \url{http://www.cheat-sheets.org/}
\tabularnewline
  Python Tutorial &
  \url{http://fivedots.coe.psu.ac.th/Software.coe/learnPython/Cheat\%20Sheets/python2.pdf}
\tabularnewline
  Python &
  \url{http://www.cheat-sheets.org/saved-copy/PQRC-2.4-A4-latest.pdf}
\tabularnewline
  Python &
  \url{https://www.cheatography.com/davechild/cheat-sheets/python/pdf/}
\tabularnewline
  Python API Index & \url{http://overapi.com/python}
\tabularnewline
  Python 3 &
  \url{https://perso.limsi.fr/pointal/_media/python:cours:mementopython3-english.pdf}
\tabularnewline
\bottomrule
\end{longtable}


text:
{-# LANGUAGE OverloadedStrings #-}

-- Module:      Data.Text.Internal.Builder.Int.Digits
-- Copyright:   (c) 2013 Bryan O'Sullivan
-- License:     BSD-style
-- Maintainer:  Bryan O'Sullivan <bos@serpentine.com>
-- Stability:   experimental
-- Portability: portable
--
-- /Warning/: this is an internal module, and does not have a stable
-- API or name. Functions in this module may not check or enforce
-- preconditions expected by public modules. Use at your own risk!
--
-- This module exists because the C preprocessor does things that we
-- shall not speak of when confronted with Haskell multiline strings.

module Data.Text.Internal.Builder.Int.Digits (digits) where

import Data.ByteString.Char8 (ByteString)

digits :: ByteString
digits = "0001020304050607080910111213141516171819\
         \2021222324252627282930313233343536373839\
         \4041424344454647484950515253545556575859\
         \6061626364656667686970717273747576777879\
         \8081828384858687888990919293949596979899"


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

<TITLE>
com.google.gwt.inject.rebind.binding
</TITLE>

<META NAME="date" CONTENT="2011-02-21">

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

com.google.gwt.inject.rebind.binding

Interfaces

Binding

BindingIndex

Classes

AsyncProviderBinding

BindClassBinding

BindConstantBinding

BindingContext

BindProviderBinding

CallConstructorBinding

CallGwtDotCreateBinding

FactoryBinding

GinjectorBinding

ImplicitProviderBinding

ProviderMethodBinding

RemoteServiceProxyBinding

RequiredKeys

Annotation Types

Injectable


text:
'use strict';

/**
 * Creates a stack that is aware of its maximum value in constant time.
 */
function MaxAwareStack() {
  // Store the value and the maximum at any given point in objects of form:
  // { value: number, max: number }
  this.stack = [];
}

/**
 * Push a value to the stack.
 * @param {*} value The value to push.
 */
MaxAwareStack.prototype.push = function (value) {
  var top = this._top();
  var entry;
  if (!top) {
    entry = {
      value: value,
      max: value
    };
  } else {
    entry = {
      value: value,
      max: value > top.max ? value : top.max
    };
  }
  this.stack.push(entry);
}

/**
 * Pops a value from the stack and returns it.
 * @return {*} The popped value.
 */
MaxAwareStack.prototype.pop = function () {
  if (this.stack.length === 0) {
    return undefined;
  }
  return this.stack.pop().value;
}

/**
 * Gets the maximum value in the stack.
 * @return {*} The maximum value.
 */
MaxAwareStack.prototype.max = function () {
  if (this.stack.length === 0) {
    return undefined;
  }
  return this._top().max;
}

/**
 * A private convenience function for getting the top entry of the stack.
 * @return {Object} The top entry.
 */
MaxAwareStack.prototype._top = function () {
  return this.stack[this.stack.length - 1];
}

module.exports = MaxAwareStack;


text:
<?php

    echo 'Test side';


text:
var fs = require("fs");
var path = require("path");

var updatedPagesPath = path.join(__dirname,'..','content','.data','updatedPages.json')

module.exports = {

	add:function(shortPath) {
		if(!this.check(shortPath)) {
      var updatedPages = JSON.parse(fs.readFileSync(updatedPagesPath, 'utf8'));
      shortPath = shortPath.replace('.json','.html');
      updatedPages.data.push(shortPath);
      fs.writeFileSync(updatedPagesPath, JSON.stringify(updatedPages));
      return true;
    }
    return false;
	},
	remove:function(shortPath) {
    if(this.check(shortPath)) {
      var updatedPages = JSON.parse(fs.readFileSync(updatedPagesPath, 'utf8'));

      for (var i = 0; i < updatedPages.data.length; i++) {
        if (updatedPages.data[i] === shortPath) {
          updatedPages.data.splice(i, 1);
        }
      }

      fs.writeFileSync(updatedPagesPath, JSON.stringify(updatedPages));
      return true;
    }
    return false;
  },
	check: function(shortPath) {
		// Open the file.

    var fullPath = path.join(__dirname,'..','content', shortPath);

    try {
      fs.accessSync(updatedPagesPath, fs.F_OK);
    } catch (e) {
      if (e.code === 'ENOENT') {
        console.log('Could not fild the file: '+ e.path);
        console.log('Creating file...');
        var date = new Date();
    		var data = {
      		date: date.toJSON(),
      		data: []
    		}
        fs.writeFileSync(updatedPagesPath, JSON.stringify(data));
        console.log('File created...');

      } else {
        console.error(e);
      }
    } finally {

      var updatedPages = JSON.parse(fs.readFileSync(updatedPagesPath, 'utf8'));

      for (var i = 0; i < updatedPages.data.length; i++) {
        if (updatedPages.data[i] === shortPath) {
          return true;
        }
      }
      return false;
    }

  },
  list: function () {
    try {
      fs.accessSync(updatedPagesPath, fs.F_OK);
      var updatedPages = JSON.parse(fs.readFileSync(updatedPagesPath, 'utf8'));
      return updatedPages.data;

    } catch (e) {
      console.error(e);
    }
  },
  clear: function () {
    try {
      fs.accessSync(updatedPagesPath, fs.F_OK);
      var updatedPages = JSON.parse(fs.readFileSync(updatedPagesPath, 'utf8'));
      updatedPages.data = [];
      fs.writeFileSync(updatedPagesPath, JSON.stringify(updatedPages));

    } catch (e) {
      console.error(e);
    }
  }

};


text:
package org.ikernits.sample.mvc;

import org.apache.log4j.Logger;
import org.ikernits.sample.TestBean;
import org.joda.time.Instant;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by ikernits on 13/10/15.
 */
public class TestController extends AbstractController implements InitializingBean {

    protected TestBean testBean;
    protected String defaultProperty;
    protected String localProperty;
    protected String configProperty;

    @Required
    public void setDefaultProperty(String defaultProperty) {
        this.defaultProperty = defaultProperty;
    }

    public void setLocalProperty(String localProperty) {
        this.localProperty = localProperty;
    }

    public void setConfigProperty(String configProperty) {
        this.configProperty = configProperty;
    }

    @Required
    public void setTestBean(TestBean testBean) {
        this.testBean = testBean;
    }

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return GsonView.createModelAndView(new Output(
                testBean.getTest(),
                defaultProperty,
                localProperty,
                configProperty
        ));
    }

    protected static class Output {
        protected Instant time = new Instant();
        protected String test;
        protected String defaultP;
        protected String localP;
        protected String configP;

        public Output(String test, String defaultP, String localP, String configP) {
            this.test = test;
            this.defaultP = defaultP;
            this.localP = localP;
            this.configP = configP;
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Logger.getLogger(TestController.class).info("initializaiton done: " + defaultProperty);
    }
}


text:
#include <grpc/support/port_platform.h>

#include <grpc/support/alloc.h>
#include <grpc/support/log.h>

#include "src/core/lib/compression/stream_compression_identity.h"
#include "src/core/lib/slice/slice_internal.h"

#define OUTPUT_BLOCK_SIZE (1024)

/* Singleton context used for all identity streams. */
static grpc_stream_compression_context identity_ctx = {
    &grpc_stream_compression_identity_vtable};

static void grpc_stream_compression_pass_through(grpc_slice_buffer* in,
                                                 grpc_slice_buffer* out,
                                                 size_t* output_size,
                                                 size_t max_output_size) {
  if (max_output_size >= in->length) {
    if (output_size) {
      *output_size = in->length;
    }
    grpc_slice_buffer_move_into(in, out);
  } else {
    if (output_size) {
      *output_size = max_output_size;
    }
    grpc_slice_buffer_move_first(in, max_output_size, out);
  }
}

static bool grpc_stream_compress_identity(grpc_stream_compression_context* ctx,
                                          grpc_slice_buffer* in,
                                          grpc_slice_buffer* out,
                                          size_t* output_size,
                                          size_t max_output_size,
                                          grpc_stream_compression_flush flush) {
  if (ctx == nullptr) {
    return false;
  }
  grpc_stream_compression_pass_through(in, out, output_size, max_output_size);
  return true;
}

static bool grpc_stream_decompress_identity(
    grpc_stream_compression_context* ctx, grpc_slice_buffer* in,
    grpc_slice_buffer* out, size_t* output_size, size_t max_output_size,
    bool* end_of_context) {
  if (ctx == nullptr) {
    return false;
  }
  grpc_stream_compression_pass_through(in, out, output_size, max_output_size);
  if (end_of_context) {
    *end_of_context = false;
  }
  return true;
}

static grpc_stream_compression_context*
grpc_stream_compression_context_create_identity(
    grpc_stream_compression_method method) {
  GPR_ASSERT(method == GRPC_STREAM_COMPRESSION_IDENTITY_COMPRESS ||
             method == GRPC_STREAM_COMPRESSION_IDENTITY_DECOMPRESS);
  /* No context needed in this case. Use fake context instead. */
  return &identity_ctx;
}

static void grpc_stream_compression_context_destroy_identity(
    grpc_stream_compression_context* ctx) {
  return;
}

const grpc_stream_compression_vtable grpc_stream_compression_identity_vtable = {
    grpc_stream_compress_identity, grpc_stream_decompress_identity,
    grpc_stream_compression_context_create_identity,
    grpc_stream_compression_context_destroy_identity};


text:
package com.zuoan.account.model;

import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;

import java.util.Date;

/**
 * Created by XUJY on 2016/1/19.
 */
public class UserDO extends BaseDO {
    private String userId;      //用户ID
    private String userName;    //用户名称
    private String userPswd;    //用户登录密码
    private Date registerTime;  //注册时间

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPswd() {
        return userPswd;
    }

    public void setUserPswd(String userPswd) {
        this.userPswd = userPswd;
    }

    public Date getRegisterTime() {
        return registerTime;
    }

    public void setRegisterTime(Date registerTime) {
        this.registerTime = registerTime;
    }
}


text:
<?php

// autoload.php @generated by Composer

require_once __DIR__ . '/composer' . '/autoload_real.php';

return ComposerAutoloaderInit2b16e9d1df7ab052a5989d04542c9e02::getLoader();


text:
/**
 *
 */
package me.learn.personal.month3;

/**
 * Title 680 :
 *
 * Given a non-empty string s, you may delete at most one character. Judge
 * whether you can make it a palindrome.
 *
 * Example 1:
 *
 * Input: "aba" Output: True
 *
 * Example 2:
 *
 * Input: "abca" Output: True Explanation: You could delete the character 'c'.
 *
 * Note:
 *
 * The string will only contain lowercase characters a-z. The maximum length of
 * the string is 50000.
 *
 *
 * @author bramanarayan
 * @date Jul 2, 2020
 */
public class ValidPalindromeTwo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		ValidPalindromeTwo solution = new ValidPalindromeTwo();
		System.out.println(solution.validPalindrome("aba"));
		System.out.println(solution.validPalindrome("abca"));
		System.out.println(solution.validPalindrome("abc"));

	}

	public boolean validPalindrome(String s) {
		int l = 0;
		int r = s.length() - 1;
		while (l <= r) {
			if (s.charAt(l) == s.charAt(r)) {
				l++;
				r--;
			} else {
				// not equal then check by removing on left or the right side
				return isPalindrome(s, l, r - 1) || isPalindrome(s, l + 1, r);
			}
		}
		return true;
	}

	// THis is needed as - you already noted one difference.
	// You cannot have another diff - so this method checks it
    private boolean isPalindrome(String str, int low, int high){
        while (low <= high){
            if (str.charAt(low) == str.charAt(high)){
                low++;
                high--;
            }
            else
                return false;
        }

        return true;
    }

}


text:
package org.apache.ignite.internal.processors.cache.mvcc;

import org.apache.ignite.cache.CacheAtomicityMode;
import org.apache.ignite.internal.processors.cache.query.continuous.IgniteCacheContinuousQueryClientReconnectTest;
import org.junit.Test;

import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL_SNAPSHOT;

/**
 * Mvcc CQ client reconnect test.
 */
public class CacheMvccContinuousQueryClientReconnectTest extends IgniteCacheContinuousQueryClientReconnectTest {
    /** {@inheritDoc} */
    @Override protected CacheAtomicityMode atomicMode() {
        return TRANSACTIONAL_SNAPSHOT;
    }

    /** {@inheritDoc} */
    @Test
    @Override public void testReconnectClient() throws Exception {
        super.testReconnectClient();
    }

    /** {@inheritDoc} */
    @Test
    @Override public void testReconnectClientAndLeftRouter() throws Exception {
        super.testReconnectClientAndLeftRouter();
    }
}


text:
import re

from galaxy import util

from ..instrumenters import InstrumentPlugin
from ...metrics import formatting

MEMINFO_LINE = re.compile(r"(\w+)\s*\:\s*(\d+) kB")

# Important (non-verbose) meminfo property titles.
MEMINFO_TITLES = {
    "memtotal": "Total System Memory",
    "swaptotal": "Total System Swap"
}

class MemInfoFormatter( formatting.JobMetricFormatter ):

    def format( self, key, value ):
        title = MEMINFO_TITLES.get( key, key )
        return title, util.nice_size( value * 1000 )  # kB = *1000, KB = *1024 - wikipedia

class MemInfoPlugin( InstrumentPlugin ):
    """ Gather information about processor configuration from /proc/cpuinfo.
    Linux only.
    """
    plugin_type = "meminfo"
    formatter = MemInfoFormatter()

    def __init__( self, **kwargs ):
        self.verbose = util.asbool( kwargs.get( "verbose", False ) )

    def pre_execute_instrument( self, job_directory ):
        return "cat /proc/meminfo > '%s'" % self.__instrument_meminfo_path( job_directory )

    def job_properties( self, job_id, job_directory ):
        properties = {}
        with open( self.__instrument_meminfo_path( job_directory ) ) as f:
            for line in f:
                line = line.strip()
                if not line:  # Skip empty lines
                    continue
                line_match = MEMINFO_LINE.match( line )
                if not line_match:
                    continue
                key = line_match.group( 1 ).lower()
                # By default just grab important meminfo properties with titles
                # defined for formatter. Grab everything in verbose mode for
                # an arbitrary snapshot of memory at beginning of run.
                if key in MEMINFO_TITLES or self.verbose:
                    value = long( line_match.group( 2 ) )
                    properties[ key ] = value
        return properties

    def __instrument_meminfo_path( self, job_directory ):
        return self._instrument_file_path( job_directory, "meminfo" )

__all__ = [ MemInfoPlugin ]