Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Deck |
|
| 1.0;1 | ||||
Deck$Smart |
|
| 1.0;1 | ||||
Deck$Smart$1 |
|
| 1.0;1 | ||||
Deck$Smart$2 |
|
| 1.0;1 |
1 | /** | |
2 | * Copyright (c) 2014-2015, Thindeck.com | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: 1) Redistributions of source code must retain the above | |
8 | * copyright notice, this list of conditions and the following | |
9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
10 | * copyright notice, this list of conditions and the following | |
11 | * disclaimer in the documentation and/or other materials provided | |
12 | * with the distribution. 3) Neither the name of the thindeck.com nor | |
13 | * the names of its contributors may be used to endorse or promote | |
14 | * products derived from this software without specific prior written | |
15 | * permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 | */ | |
30 | package com.thindeck.api; | |
31 | ||
32 | import com.jcabi.aspects.Immutable; | |
33 | import com.jcabi.xml.XML; | |
34 | import com.jcabi.xml.XMLDocument; | |
35 | import com.jcabi.xml.XSD; | |
36 | import com.jcabi.xml.XSDDocument; | |
37 | import com.jcabi.xml.XSL; | |
38 | import com.jcabi.xml.XSLChain; | |
39 | import com.jcabi.xml.XSLDocument; | |
40 | import java.io.IOException; | |
41 | import java.util.Arrays; | |
42 | import java.util.Collections; | |
43 | import java.util.concurrent.atomic.AtomicReference; | |
44 | import javax.validation.constraints.NotNull; | |
45 | import org.xembly.Directive; | |
46 | import org.xembly.Directives; | |
47 | ||
48 | /** | |
49 | * Deck. | |
50 | * | |
51 | * <p>Deck is a configurable deployment of sources | |
52 | * to Docker containers. Deck should be configured through | |
53 | * {@link #exec(com.thindeck.api.Agent)}. | |
54 | * | |
55 | * <p>Deck is an XML document with data about the current | |
56 | * state of the deck. A deck can contain, for example, the | |
57 | * list of Docker containers that are running the deck at | |
58 | * the moment. | |
59 | * | |
60 | * <p>Full description of what information a deck should (and can) | |
61 | * include you can get from its XSD schema. | |
62 | * | |
63 | * @author Yegor Bugayenko (yegor@teamed.io) | |
64 | * @version $Id$ | |
65 | * @since 0.1 | |
66 | */ | |
67 | @Immutable | |
68 | public interface Deck { | |
69 | ||
70 | /** | |
71 | * Schema. | |
72 | */ | |
73 | XSD SCHEMA = XSDDocument.make( | |
74 | Deck.class.getResourceAsStream("deck.xsd") | |
75 | ); | |
76 | ||
77 | /** | |
78 | * XSL for upgrade. | |
79 | */ | |
80 | XSL UPGRADE = new XSLChain( | |
81 | Arrays.asList( | |
82 | XSLDocument.make( | |
83 | Deck.class.getResourceAsStream( | |
84 | "upgrades/001-uri-for-image.xsl" | |
85 | ) | |
86 | ) | |
87 | ) | |
88 | ); | |
89 | ||
90 | /** | |
91 | * Name, unique for the user. | |
92 | * @return Unique name of the deck | |
93 | * @throws IOException If fails | |
94 | */ | |
95 | @NotNull(message = "deck name can't be null") | |
96 | String name() throws IOException; | |
97 | ||
98 | /** | |
99 | * Execute this agent. | |
100 | * @param agent The agent | |
101 | * @throws IOException If fails | |
102 | */ | |
103 | void exec(Agent agent) throws IOException; | |
104 | ||
105 | /** | |
106 | * Get events. | |
107 | * @return Events | |
108 | */ | |
109 | Events events(); | |
110 | ||
111 | /** | |
112 | * Smart. | |
113 | */ | |
114 | final class Smart { | |
115 | /** | |
116 | * Original deck. | |
117 | */ | |
118 | private final transient Deck deck; | |
119 | /** | |
120 | * Ctor. | |
121 | * @param dck Deck | |
122 | */ | |
123 | 6 | public Smart(final Deck dck) { |
124 | 6 | this.deck = dck; |
125 | 6 | } |
126 | /** | |
127 | * Get its XML. | |
128 | * @return XML of the deck | |
129 | * @throws IOException If fails | |
130 | */ | |
131 | public XML xml() throws IOException { | |
132 | 0 | final AtomicReference<XML> xml = new AtomicReference<>(); |
133 | 0 | this.deck.exec( |
134 | 0 | new Agent() { |
135 | @Override | |
136 | public Iterable<Directive> exec(final XML doc) { | |
137 | 0 | xml.set(doc); |
138 | 0 | return Collections.emptyList(); |
139 | } | |
140 | } | |
141 | ); | |
142 | 0 | return xml.get(); |
143 | } | |
144 | /** | |
145 | * Update XML with these exact content. | |
146 | * @param xml XML to save | |
147 | * @throws IOException If fails | |
148 | */ | |
149 | public void update(final String xml) throws IOException { | |
150 | 0 | this.update( |
151 | new Directives().xpath("/*").remove().append( | |
152 | Directives.copyOf(new XMLDocument(xml).node()) | |
153 | ) | |
154 | ); | |
155 | 0 | } |
156 | /** | |
157 | * Update XML with these directives. | |
158 | * @param dirs Directives to use | |
159 | * @throws IOException If fails | |
160 | */ | |
161 | public void update(final Iterable<Directive> dirs) throws IOException { | |
162 | 6 | this.deck.exec( |
163 | 6 | new Agent() { |
164 | @Override | |
165 | public Iterable<Directive> exec(final XML xml) { | |
166 | 0 | return dirs; |
167 | } | |
168 | } | |
169 | ); | |
170 | 0 | } |
171 | } | |
172 | ||
173 | } |